Popular sites on the NTP: check that experiment group StartsWith (rather than IS...
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / user_image_screen_handler.cc
blobc91ef404fcd612a0fcd17339f691ed9d2be49ac6
1 // Copyright (c) 2013 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.
5 #include "chrome/browser/ui/webui/chromeos/login/user_image_screen_handler.h"
7 #include "ash/audio/sounds.h"
8 #include "base/command_line.h"
9 #include "base/logging.h"
10 #include "base/metrics/histogram.h"
11 #include "base/values.h"
12 #include "chrome/browser/chromeos/login/existing_user_controller.h"
13 #include "chrome/browser/chromeos/login/screens/user_image_model.h"
14 #include "chrome/browser/chromeos/login/ui/webui_login_display.h"
15 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/url_constants.h"
18 #include "chrome/grit/generated_resources.h"
19 #include "chromeos/audio/chromeos_sounds.h"
20 #include "components/login/localized_values_builder.h"
21 #include "components/user_manager/user.h"
22 #include "components/user_manager/user_image/default_user_images.h"
23 #include "grit/browser_resources.h"
24 #include "media/audio/sounds/sounds_manager.h"
25 #include "net/base/data_url.h"
26 #include "ui/base/l10n/l10n_util.h"
27 #include "ui/base/resource/resource_bundle.h"
28 #include "url/gurl.h"
30 namespace {
32 const char kJsScreenPath[] = "login.UserImageScreen";
34 } // namespace
36 namespace chromeos {
38 UserImageScreenHandler::UserImageScreenHandler()
39 : BaseScreenHandler(kJsScreenPath),
40 model_(nullptr),
41 show_on_init_(false),
42 is_ready_(false) {
43 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
44 media::SoundsManager* manager = media::SoundsManager::Get();
45 manager->Initialize(SOUND_OBJECT_DELETE,
46 bundle.GetRawDataResource(IDR_SOUND_OBJECT_DELETE_WAV));
47 manager->Initialize(SOUND_CAMERA_SNAP,
48 bundle.GetRawDataResource(IDR_SOUND_CAMERA_SNAP_WAV));
51 UserImageScreenHandler::~UserImageScreenHandler() {
52 if (model_)
53 model_->OnViewDestroyed(this);
56 void UserImageScreenHandler::Initialize() {
57 if (show_on_init_) {
58 Show();
59 show_on_init_ = false;
63 void UserImageScreenHandler::Bind(UserImageModel& model) {
64 model_ = &model;
65 BaseScreenHandler::SetBaseScreen(model_);
68 void UserImageScreenHandler::Unbind() {
69 model_ = nullptr;
70 BaseScreenHandler::SetBaseScreen(nullptr);
73 void UserImageScreenHandler::Show() {
74 if (!page_is_ready()) {
75 show_on_init_ = true;
76 return;
78 screen_show_time_ = base::Time::Now();
79 ShowScreen(OobeUI::kScreenUserImagePicker, NULL);
81 // When shown, query camera presence.
82 if (model_ && is_ready_)
83 model_->OnScreenReady();
86 void UserImageScreenHandler::Hide() {
89 void UserImageScreenHandler::PrepareToShow() {
92 void UserImageScreenHandler::DeclareLocalizedValues(
93 ::login::LocalizedValuesBuilder* builder) {
94 builder->Add("userImageScreenTitle", IDS_USER_IMAGE_SCREEN_TITLE);
95 builder->Add("userImageScreenDescription",
96 IDS_OPTIONS_CHANGE_PICTURE_DIALOG_TEXT);
97 builder->Add("takePhoto", IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO);
98 builder->Add("discardPhoto", IDS_OPTIONS_CHANGE_PICTURE_DISCARD_PHOTO);
99 builder->Add("flipPhoto", IDS_OPTIONS_CHANGE_PICTURE_FLIP_PHOTO);
100 builder->Add("profilePhoto", IDS_IMAGE_SCREEN_PROFILE_PHOTO);
101 builder->Add("profilePhotoLoading",
102 IDS_IMAGE_SCREEN_PROFILE_LOADING_PHOTO);
103 builder->Add("okButtonText", IDS_OK);
104 builder->Add("authorCredit", IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT);
105 builder->Add("photoFromCamera", IDS_OPTIONS_CHANGE_PICTURE_PHOTO_FROM_CAMERA);
106 builder->Add("photoFlippedAccessibleText",
107 IDS_OPTIONS_PHOTO_FLIP_ACCESSIBLE_TEXT);
108 builder->Add("photoFlippedBackAccessibleText",
109 IDS_OPTIONS_PHOTO_FLIPBACK_ACCESSIBLE_TEXT);
110 builder->Add("photoCaptureAccessibleText",
111 IDS_OPTIONS_PHOTO_CAPTURE_ACCESSIBLE_TEXT);
112 builder->Add("photoDiscardAccessibleText",
113 IDS_OPTIONS_PHOTO_DISCARD_ACCESSIBLE_TEXT);
114 builder->Add("syncingPreferences", IDS_IMAGE_SCREEN_SYNCING_PREFERENCES);
115 builder->Add("syncingPreferencesNewGaia",
116 IDS_IMAGE_SCREEN_SYNCING_PREFERENCES_NEW_GAIA_FLOW);
119 void UserImageScreenHandler::RegisterMessages() {
120 AddCallback("getImages", &UserImageScreenHandler::HandleGetImages);
121 AddCallback("screenReady", &UserImageScreenHandler::HandleScreenReady);
122 AddCallback("takePhoto", &UserImageScreenHandler::HandleTakePhoto);
123 AddCallback("discardPhoto", &UserImageScreenHandler::HandleDiscardPhoto);
124 AddCallback("photoTaken", &UserImageScreenHandler::HandlePhotoTaken);
125 AddCallback("selectImage", &UserImageScreenHandler::HandleSelectImage);
126 AddCallback("onUserImageAccepted",
127 &UserImageScreenHandler::HandleImageAccepted);
128 AddCallback("onUserImageScreenShown",
129 &UserImageScreenHandler::HandleScreenShown);
132 // TODO(antrim) : It looks more like parameters for "Init" rather than callback.
133 void UserImageScreenHandler::HandleGetImages() {
134 base::ListValue image_urls;
135 for (int i = user_manager::kFirstDefaultImageIndex;
136 i < user_manager::kDefaultImagesCount;
137 ++i) {
138 scoped_ptr<base::DictionaryValue> image_data(new base::DictionaryValue);
139 image_data->SetString("url", user_manager::GetDefaultImageUrl(i));
140 image_data->SetString(
141 "author",
142 l10n_util::GetStringUTF16(user_manager::kDefaultImageAuthorIDs[i]));
143 image_data->SetString(
144 "website",
145 l10n_util::GetStringUTF16(user_manager::kDefaultImageWebsiteIDs[i]));
146 image_data->SetString("title", user_manager::GetDefaultImageDescription(i));
147 image_urls.Append(image_data.release());
149 CallJS("setDefaultImages", image_urls);
152 void UserImageScreenHandler::HandleScreenReady() {
153 is_ready_ = true;
154 if (model_)
155 model_->OnScreenReady();
158 void UserImageScreenHandler::HandlePhotoTaken(const std::string& image_url) {
159 std::string mime_type, charset, raw_data;
160 if (!net::DataURL::Parse(GURL(image_url), &mime_type, &charset, &raw_data))
161 NOTREACHED();
162 DCHECK_EQ("image/png", mime_type);
164 if (model_)
165 model_->OnPhotoTaken(raw_data);
168 void UserImageScreenHandler::HandleTakePhoto() {
169 ash::PlaySystemSoundIfSpokenFeedback(SOUND_CAMERA_SNAP);
172 void UserImageScreenHandler::HandleDiscardPhoto() {
173 ash::PlaySystemSoundIfSpokenFeedback(SOUND_OBJECT_DELETE);
176 void UserImageScreenHandler::HandleSelectImage(const std::string& image_url,
177 const std::string& image_type,
178 bool is_user_selection) {
179 if (model_)
180 model_->OnImageSelected(image_type, image_url, is_user_selection);
183 void UserImageScreenHandler::HandleImageAccepted() {
184 if (model_)
185 model_->OnImageAccepted();
188 void UserImageScreenHandler::HandleScreenShown() {
189 DCHECK(!screen_show_time_.is_null());
191 base::TimeDelta delta = base::Time::Now() - screen_show_time_;
192 VLOG(1) << "Screen load time: " << delta.InSecondsF();
193 UMA_HISTOGRAM_TIMES("UserImage.ScreenIsShownTime", delta);
196 void UserImageScreenHandler::HideCurtain() {
197 CallJS("hideCurtain");
200 } // namespace chromeos