Home | History | Annotate | Download | only in options
      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/options/take_photo_dialog.h"
      6 
      7 #include "base/string_util.h"
      8 #include "base/utf_string_conversions.h"
      9 #include "chrome/browser/chromeos/login/helper.h"
     10 #include "chrome/browser/chromeos/login/user_manager.h"
     11 #include "content/common/notification_service.h"
     12 #include "content/common/notification_type.h"
     13 #include "grit/chromium_strings.h"
     14 #include "grit/generated_resources.h"
     15 #include "grit/locale_settings.h"
     16 #include "ui/base/accessibility/accessible_view_state.h"
     17 #include "ui/base/l10n/l10n_util.h"
     18 #include "views/layout/layout_constants.h"
     19 
     20 namespace chromeos {
     21 
     22 namespace {
     23 
     24 // The resolution of the picture we want to get from the camera.
     25 const int kFrameWidth = 480;
     26 const int kFrameHeight = 480;
     27 
     28 }  // namespace
     29 
     30 TakePhotoDialog::TakePhotoDialog()
     31     : take_photo_view_(NULL),
     32       camera_controller_(this) {
     33   camera_controller_.set_frame_width(kFrameWidth);
     34   camera_controller_.set_frame_height(kFrameHeight);
     35   registrar_.Add(
     36       this,
     37       NotificationType::SCREEN_LOCK_STATE_CHANGED,
     38       NotificationService::AllSources());
     39 }
     40 
     41 bool TakePhotoDialog::IsDialogButtonEnabled(
     42     MessageBoxFlags::DialogButton button) const {
     43   if (button == MessageBoxFlags::DIALOGBUTTON_CANCEL)
     44     return true;
     45   else if (button == MessageBoxFlags::DIALOGBUTTON_OK)
     46     return !take_photo_view_->is_capturing();
     47   NOTREACHED();
     48   return false;
     49 }
     50 
     51 bool TakePhotoDialog::Cancel() {
     52   camera_controller_.Stop();
     53   return true;
     54 }
     55 
     56 bool TakePhotoDialog::Accept() {
     57   camera_controller_.Stop();
     58 
     59   UserManager* user_manager = UserManager::Get();
     60   DCHECK(user_manager);
     61 
     62   const UserManager::User& user = user_manager->logged_in_user();
     63   DCHECK(!user.email().empty());
     64 
     65   const SkBitmap& image = take_photo_view_->GetImage();
     66   user_manager->SetLoggedInUserImage(image);
     67   user_manager->SaveUserImage(user.email(), image);
     68   return true;
     69 }
     70 
     71 views::View* TakePhotoDialog::GetContentsView() {
     72   // Lazy initialization upon request.
     73   if (!take_photo_view_) {
     74     take_photo_view_ = new TakePhotoView(this);
     75     take_photo_view_->Init();
     76     AddChildView(take_photo_view_);
     77     InitCamera();
     78   }
     79   return this;
     80 }
     81 
     82 void TakePhotoDialog::GetAccessibleState(ui::AccessibleViewState* state) {
     83   state->name = l10n_util::GetStringUTF16(
     84       IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO);
     85   state->role = ui::AccessibilityTypes::ROLE_DIALOG;
     86 }
     87 
     88 void TakePhotoDialog::OnCapturingStarted() {
     89   GetDialogClientView()->ok_button()->SetEnabled(false);
     90 }
     91 
     92 void TakePhotoDialog::OnCapturingStopped() {
     93   GetDialogClientView()->ok_button()->SetEnabled(true);
     94   GetDialogClientView()->ok_button()->RequestFocus();
     95 }
     96 
     97 void TakePhotoDialog::OnCaptureSuccess() {
     98   SkBitmap frame;
     99   camera_controller_.GetFrame(&frame);
    100   if (!frame.isNull())
    101     take_photo_view_->UpdateVideoFrame(frame);
    102 }
    103 
    104 void TakePhotoDialog::OnCaptureFailure() {
    105   take_photo_view_->ShowCameraError();
    106 }
    107 
    108 void TakePhotoDialog::Observe(NotificationType type,
    109                               const NotificationSource& source,
    110                               const NotificationDetails& details) {
    111   if (type != NotificationType::SCREEN_LOCK_STATE_CHANGED)
    112     return;
    113 
    114   bool is_screen_locked = *Details<bool>(details).ptr();
    115   if (is_screen_locked)
    116     camera_controller_.Stop();
    117   else
    118     InitCamera();
    119 }
    120 
    121 void TakePhotoDialog::Layout() {
    122   int left = views::kPanelHorizMargin;
    123   int top = views::kPanelVertMargin;
    124   int contents_width = width() - 2 * left;
    125   int contents_height = height() - 2 * top;
    126   take_photo_view_->SetBounds(left, top, contents_width, contents_height);
    127 }
    128 
    129 gfx::Size TakePhotoDialog::GetPreferredSize() {
    130   return gfx::Size(login::kUserImageSize * 2, (login::kUserImageSize * 3 / 2));
    131 }
    132 
    133 void TakePhotoDialog::InitCamera() {
    134   take_photo_view_->ShowCameraInitializing();
    135   camera_controller_.Start();
    136 }
    137 
    138 }  // namespace chromeos
    139 
    140