1dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// Use of this source code is governed by a BSD-style license that can be
3c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// found in the LICENSE file.
4c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
5c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/chromeos/login/user_image_screen.h"
6c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
7c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "base/compiler_specific.h"
8ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen#include "chrome/browser/chromeos/login/default_user_images.h"
9c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/chromeos/login/login_utils.h"
10c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/chromeos/login/screen_observer.h"
11c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/chromeos/login/user_image_view.h"
12c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch#include "chrome/browser/chromeos/login/user_manager.h"
13dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/common/notification_service.h"
14dc0f95d653279beabeb9817299e2902918ba123eKristian Monsen#include "content/common/notification_type.h"
15513209b27ff55e2841eac0e4120199c23acce758Ben Murdoch#include "grit/theme_resources.h"
1672a454cd3513ac24fbdd0e0cb9ad70b86a99b801Kristian Monsen#include "ui/base/resource/resource_bundle.h"
17c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
18c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace chromeos {
19c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
20c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochnamespace {
21c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
22c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch// The resolution of the picture we want to get from the camera.
23c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochconst int kFrameWidth = 480;
24c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochconst int kFrameHeight = 480;
25c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
26c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace
27c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
28c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochUserImageScreen::UserImageScreen(WizardScreenDelegate* delegate)
29c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    : ViewScreen<UserImageView>(delegate),
30ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      camera_controller_(this) {
31ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.set_frame_width(kFrameWidth);
32ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.set_frame_height(kFrameHeight);
33c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  registrar_.Add(
34c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      this,
35c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      NotificationType::SCREEN_LOCK_STATE_CHANGED,
36c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch      NotificationService::AllSources());
37c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
38c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
39c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochUserImageScreen::~UserImageScreen() {
40c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
41c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
42c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid UserImageScreen::Refresh() {
43ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(view());
44ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  UserManager* user_manager = UserManager::Get();
45ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  std::string logged_in_user = user_manager->logged_in_user().email();
46ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  view()->OnImageSelected(
47ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      user_manager->GetUserDefaultImageIndex(logged_in_user));
48c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
49c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
50c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid UserImageScreen::Hide() {
51ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.Stop();
52c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  ViewScreen<UserImageView>::Hide();
53c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
54c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
55c407dc5cd9bdc5668497f21b26b09d988ab439deBen MurdochUserImageView* UserImageScreen::AllocateView() {
56c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  return new UserImageView(this);
57c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
58c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
59ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid UserImageScreen::StartCamera() {
60ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(view());
61ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  view()->ShowCameraInitializing();
62ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.Start();
63731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick}
64731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
65ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid UserImageScreen::StopCamera() {
66ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.Stop();
67731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick}
68731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
69513209b27ff55e2841eac0e4120199c23acce758Ben Murdochvoid UserImageScreen::OnCaptureSuccess() {
70ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(view());
71ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  SkBitmap frame;
72ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.GetFrame(&frame);
73ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  if (!frame.isNull())
74ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    view()->UpdateVideoFrame(frame);
75c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
76c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
77731df977c0511bca2206b5f333555b1205ff1f43Iain Merrickvoid UserImageScreen::OnCaptureFailure() {
78ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(view());
79ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  view()->ShowCameraError();
80731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick}
81731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
82ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid UserImageScreen::OnPhotoTaken(const SkBitmap& image) {
83ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.Stop();
84ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
85c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  UserManager* user_manager = UserManager::Get();
86731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  DCHECK(user_manager);
87731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
88731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  const UserManager::User& user = user_manager->logged_in_user();
89731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  DCHECK(!user.email().empty());
90731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick
91201ade2fbba22bfb27ae029f4d23fca6ded109a0Ben Murdoch  user_manager->SetLoggedInUserImage(image);
92731df977c0511bca2206b5f333555b1205ff1f43Iain Merrick  user_manager->SaveUserImage(user.email(), image);
93c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (delegate())
94c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    delegate()->GetObserver(this)->OnExit(ScreenObserver::USER_IMAGE_SELECTED);
95c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
96c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
97ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsenvoid UserImageScreen::OnDefaultImageSelected(int index) {
98ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  camera_controller_.Stop();
99ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
100ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  UserManager* user_manager = UserManager::Get();
101ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(user_manager);
102ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
103ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const UserManager::User& user = user_manager->logged_in_user();
104ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  DCHECK(!user.email().empty());
105ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen
106ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  const SkBitmap* image = ResourceBundle::GetSharedInstance().GetBitmapNamed(
107ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      kDefaultImageResources[index]);
108ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  user_manager->SetLoggedInUserImage(*image);
109ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  user_manager->SaveUserImagePath(
110ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      user.email(),
111ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen      GetDefaultImagePath(static_cast<size_t>(index)));
112c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (delegate())
113ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    delegate()->GetObserver(this)->OnExit(ScreenObserver::USER_IMAGE_SELECTED);
114c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
115c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
116c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdochvoid UserImageScreen::Observe(NotificationType type,
117c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                              const NotificationSource& source,
118c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch                              const NotificationDetails& details) {
119ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  if (type != NotificationType::SCREEN_LOCK_STATE_CHANGED)
120c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch    return;
121c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
122c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  bool is_screen_locked = *Details<bool>(details).ptr();
123c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch  if (is_screen_locked)
124ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    StopCamera();
125ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen  else if (view()->IsCapturing())
126ddb351dbec246cf1fab5ec20d2d5520909041de1Kristian Monsen    StartCamera();
127c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}
128c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
129c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch}  // namespace chromeos
130c407dc5cd9bdc5668497f21b26b09d988ab439deBen Murdoch
131