1// Copyright (c) 2012 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#ifndef CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_CHANGE_PICTURE_OPTIONS_HANDLER_H_
6#define CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_CHANGE_PICTURE_OPTIONS_HANDLER_H_
7
8#include "chrome/browser/chromeos/camera_presence_notifier.h"
9#include "chrome/browser/image_decoder.h"
10#include "chrome/browser/ui/webui/options/options_ui.h"
11#include "content/public/browser/notification_observer.h"
12#include "content/public/browser/notification_registrar.h"
13#include "ui/gfx/image/image_skia.h"
14#include "ui/gfx/native_widget_types.h"
15#include "ui/shell_dialogs/select_file_dialog.h"
16
17namespace base {
18class DictionaryValue;
19class ListValue;
20}
21
22namespace user_manager {
23class User;
24}
25
26namespace chromeos {
27
28namespace options {
29
30// ChromeOS user image options page UI handler.
31class ChangePictureOptionsHandler : public ::options::OptionsPageUIHandler,
32                                    public ui::SelectFileDialog::Listener,
33                                    public content::NotificationObserver,
34                                    public ImageDecoder::Delegate,
35                                    public CameraPresenceNotifier::Observer {
36 public:
37  ChangePictureOptionsHandler();
38  virtual ~ChangePictureOptionsHandler();
39
40  // OptionsPageUIHandler implementation.
41  virtual void GetLocalizedValues(
42      base::DictionaryValue* localized_strings) OVERRIDE;
43
44  // WebUIMessageHandler implementation.
45  virtual void RegisterMessages() OVERRIDE;
46
47  // CameraPresenceNotifier::Observer implementation:
48  virtual void OnCameraPresenceCheckDone(bool is_camera_present) OVERRIDE;
49
50 private:
51  // Sends list of available default images to the page.
52  void SendDefaultImages();
53
54  // Sends current selection to the page.
55  void SendSelectedImage();
56
57  // Sends the profile image to the page. If |should_select| is true then
58  // the profile image element is selected.
59  void SendProfileImage(const gfx::ImageSkia& image, bool should_select);
60
61  // Starts profile image update and shows the last downloaded profile image,
62  // if any, on the page. Shouldn't be called before |SendProfileImage|.
63  void UpdateProfileImage();
64
65  // Sends previous user image to the page.
66  void SendOldImage(const std::string& image_url);
67
68  // Starts camera presence check.
69  void CheckCameraPresence();
70
71  // Updates UI with camera presence state.
72  void SetCameraPresent(bool present);
73
74  // Opens a file selection dialog to choose user image from file.
75  void HandleChooseFile(const base::ListValue* args);
76
77  // Handles 'take-photo' button click.
78  void HandleTakePhoto(const base::ListValue* args);
79
80  // Handles photo taken with WebRTC UI.
81  void HandlePhotoTaken(const base::ListValue* args);
82
83  // Handles 'discard-photo' button click.
84  void HandleDiscardPhoto(const base::ListValue* args);
85
86  // Gets the list of available user images and sends it to the page.
87  void HandleGetAvailableImages(const base::ListValue* args);
88
89  // Handles page initialized event.
90  void HandlePageInitialized(const base::ListValue* args);
91
92  // Handles page shown event.
93  void HandlePageShown(const base::ListValue* args);
94
95  // Handles page hidden event.
96  void HandlePageHidden(const base::ListValue* args);
97
98  // Selects one of the available images as user's.
99  void HandleSelectImage(const base::ListValue* args);
100
101  // SelectFileDialog::Delegate implementation.
102  virtual void FileSelected(
103      const base::FilePath& path,
104      int index, void* params) OVERRIDE;
105
106  // content::NotificationObserver implementation.
107  virtual void Observe(int type,
108                       const content::NotificationSource& source,
109                       const content::NotificationDetails& details) OVERRIDE;
110
111  // Sets user image to photo taken from camera.
112  void SetImageFromCamera(const gfx::ImageSkia& photo);
113
114  // Returns handle to browser window or NULL if it can't be found.
115  gfx::NativeWindow GetBrowserWindow() const;
116
117  // Overriden from ImageDecoder::Delegate:
118  virtual void OnImageDecoded(const ImageDecoder* decoder,
119                              const SkBitmap& decoded_image) OVERRIDE;
120  virtual void OnDecodeImageFailed(const ImageDecoder* decoder) OVERRIDE;
121
122  // Returns user related to current WebUI. If this user doesn't exist,
123  // returns active user.
124  user_manager::User* GetUser() const;
125
126  scoped_refptr<ui::SelectFileDialog> select_file_dialog_;
127
128  // Previous user image from camera/file and its data URL.
129  gfx::ImageSkia previous_image_;
130  std::string previous_image_url_;
131
132  // Index of the previous user image.
133  int previous_image_index_;
134
135  // Last user photo, if taken.
136  gfx::ImageSkia user_photo_;
137
138  // Data URL for |user_photo_|.
139  std::string user_photo_data_url_;
140
141  content::NotificationRegistrar registrar_;
142
143  // Last ImageDecoder instance used to decode an image blob received by
144  // HandlePhotoTaken.
145  scoped_refptr<ImageDecoder> image_decoder_;
146
147  DISALLOW_COPY_AND_ASSIGN(ChangePictureOptionsHandler);
148};
149
150}  // namespace options
151}  // namespace chromeos
152
153#endif  // CHROME_BROWSER_UI_WEBUI_OPTIONS_CHROMEOS_CHANGE_PICTURE_OPTIONS_HANDLER_H_
154