summaryrefslogtreecommitdiffstats
path: root/chrome/browser/chromeos/login/user_image_screen.cc
blob: 7151b4d2ed4836122bb63c06f21836bec56aa13d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/chromeos/login/user_image_screen.h"

#include "app/resource_bundle.h"
#include "base/compiler_specific.h"
#include "base/time.h"
#include "chrome/browser/chromeos/login/login_utils.h"
#include "chrome/browser/chromeos/login/screen_observer.h"
#include "chrome/browser/chromeos/login/user_image_downloader.h"
#include "chrome/browser/chromeos/login/user_image_view.h"
#include "chrome/browser/chromeos/login/user_manager.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/notification_type.h"
#include "grit/theme_resources.h"

namespace chromeos {

namespace {

// The resolution of the picture we want to get from the camera.
const int kFrameWidth = 480;
const int kFrameHeight = 480;

// Maximum number of capture failures we ignore before we try to initialize
// the camera again.
const int kMaxCaptureFailureCounter = 5;

// Maximum number of camera initialization retries.
const int kMaxCameraInitFailureCounter = 3;

// Name for camera thread.
const char kCameraThreadName[] = "Chrome_CameraThread";

}  // namespace

UserImageScreen::UserImageScreen(WizardScreenDelegate* delegate)
    : ViewScreen<UserImageView>(delegate),
      capture_failure_counter_(0),
      camera_init_failure_counter_(0),
      camera_thread_(kCameraThreadName) {
  registrar_.Add(
      this,
      NotificationType::SCREEN_LOCK_STATE_CHANGED,
      NotificationService::AllSources());
}

UserImageScreen::~UserImageScreen() {
  if (camera_.get())
    camera_->set_delegate(NULL);
}

void UserImageScreen::Refresh() {
  camera_thread_.Start();
  camera_ = new Camera(this, &camera_thread_, true);
  InitCamera();
}

void UserImageScreen::Hide() {
  if (camera_.get())
    camera_->StopCapturing();
  ViewScreen<UserImageView>::Hide();
}

UserImageView* UserImageScreen::AllocateView() {
  return new UserImageView(this);
}

void UserImageScreen::OnInitializeSuccess() {
  if (camera_.get())
    camera_->StartCapturing();
}

void UserImageScreen::OnInitializeFailure() {
  ++camera_init_failure_counter_;
  if (camera_init_failure_counter_ > kMaxCameraInitFailureCounter) {
    if (view())
      view()->ShowCameraError();
    return;
  }
  // Retry initializing the camera.
  if (camera_.get()) {
    camera_->Uninitialize();
    InitCamera();
  }
}

void UserImageScreen::OnStartCapturingSuccess() {
}

void UserImageScreen::OnStartCapturingFailure() {
  // Try to reinitialize camera.
  OnInitializeFailure();
}

void UserImageScreen::OnCaptureSuccess() {
  capture_failure_counter_ = 0;
  camera_init_failure_counter_ = 0;
  if (view() && camera_.get()) {
    SkBitmap frame;
    camera_->GetFrame(&frame);
    if (!frame.isNull())
      view()->UpdateVideoFrame(frame);
  }
}

void UserImageScreen::OnCaptureFailure() {
  ++capture_failure_counter_;
  if (capture_failure_counter_ < kMaxCaptureFailureCounter)
    return;

  capture_failure_counter_ = 0;
  OnInitializeFailure();
}

void UserImageScreen::OnOK(const SkBitmap& image) {
  if (camera_.get())
    camera_->Uninitialize();
  UserManager* user_manager = UserManager::Get();
  DCHECK(user_manager);

  const UserManager::User& user = user_manager->logged_in_user();
  DCHECK(!user.email().empty());

  user_manager->SaveUserImage(user.email(), image);
  if (delegate())
    delegate()->GetObserver(this)->OnExit(ScreenObserver::USER_IMAGE_SELECTED);
}

void UserImageScreen::OnSkip() {
  if (camera_.get())
    camera_->Uninitialize();
  UserManager* user_manager = UserManager::Get();
  DCHECK(user_manager);

  const UserManager::User& user = user_manager->logged_in_user();
  DCHECK(!user.email().empty());

  user_manager->SetDefaultUserImage(user.email());
  if (delegate())
    delegate()->GetObserver(this)->OnExit(ScreenObserver::USER_IMAGE_SKIPPED);
}

void UserImageScreen::Observe(NotificationType type,
                              const NotificationSource& source,
                              const NotificationDetails& details) {
  if (type != NotificationType::SCREEN_LOCK_STATE_CHANGED ||
      !camera_.get())
    return;

  bool is_screen_locked = *Details<bool>(details).ptr();
  if (is_screen_locked)
    camera_->Uninitialize();
  else
    InitCamera();
}

void UserImageScreen::InitCamera() {
  if (view())
    view()->ShowCameraInitializing();
  camera_->Initialize(kFrameWidth, kFrameHeight);
}

}  // namespace chromeos