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