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 "base/command_line.h"
8 #include "base/logging.h"
9 #include "base/metrics/histogram.h"
10 #include "base/values.h"
11 #include "chrome/browser/chromeos/login/default_user_images.h"
12 #include "chrome/browser/chromeos/login/user.h"
13 #include "chrome/browser/chromeos/login/webui_login_display.h"
14 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h"
15 #include "chrome/common/chrome_switches.h"
16 #include "chrome/common/url_constants.h"
17 #include "grit/generated_resources.h"
18 #include "net/base/data_url.h"
19 #include "ui/base/l10n/l10n_util.h"
24 const char kJsScreenPath
[] = "login.UserImageScreen";
30 UserImageScreenHandler::UserImageScreenHandler()
31 : BaseScreenHandler(kJsScreenPath
),
37 UserImageScreenHandler::~UserImageScreenHandler() {
39 screen_
->OnActorDestroyed(this);
42 void UserImageScreenHandler::Initialize() {
45 show_on_init_
= false;
49 void UserImageScreenHandler::SetDelegate(
50 UserImageScreenActor::Delegate
* screen
) {
54 void UserImageScreenHandler::Show() {
55 if (!page_is_ready()) {
59 screen_show_time_
= base::Time::Now();
60 ShowScreen(OobeUI::kScreenUserImagePicker
, NULL
);
62 // When shown, query camera presence.
65 screen_
->CheckCameraPresence();
67 screen_
->OnScreenReady();
70 void UserImageScreenHandler::Hide() {
73 void UserImageScreenHandler::PrepareToShow() {
76 void UserImageScreenHandler::DeclareLocalizedValues(
77 LocalizedValuesBuilder
* builder
) {
78 // TODO(ivankr): string should be renamed to something like
79 // IDS_USER_IMAGE_SCREEN_TITLE (currently used for Take Photo dialog).
80 builder
->Add("userImageScreenTitle", IDS_OOBE_PICTURE
);
81 builder
->Add("userImageScreenDescription",
82 IDS_OPTIONS_CHANGE_PICTURE_DIALOG_TEXT
);
83 builder
->Add("takePhoto", IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO
);
84 builder
->Add("discardPhoto", IDS_OPTIONS_CHANGE_PICTURE_DISCARD_PHOTO
);
85 builder
->Add("flipPhoto", IDS_OPTIONS_CHANGE_PICTURE_FLIP_PHOTO
);
86 builder
->Add("profilePhoto", IDS_IMAGE_SCREEN_PROFILE_PHOTO
);
87 builder
->Add("profilePhotoLoading",
88 IDS_IMAGE_SCREEN_PROFILE_LOADING_PHOTO
);
89 builder
->Add("okButtonText", IDS_OK
);
90 builder
->Add("authorCredit", IDS_OPTIONS_SET_WALLPAPER_AUTHOR_TEXT
);
91 builder
->Add("photoFromCamera", IDS_OPTIONS_CHANGE_PICTURE_PHOTO_FROM_CAMERA
);
92 builder
->Add("photoCaptureAccessibleText",
93 IDS_OPTIONS_PHOTO_CAPTURE_ACCESSIBLE_TEXT
);
94 builder
->Add("photoDiscardAccessibleText",
95 IDS_OPTIONS_PHOTO_DISCARD_ACCESSIBLE_TEXT
);
96 builder
->Add("syncingPreferences", IDS_IMAGE_SCREEN_SYNCING_PREFERENCES
);
99 void UserImageScreenHandler::RegisterMessages() {
100 AddCallback("getImages", &UserImageScreenHandler::HandleGetImages
);
101 AddCallback("screenReady", &UserImageScreenHandler::HandleScreenReady
);
102 AddCallback("photoTaken", &UserImageScreenHandler::HandlePhotoTaken
);
103 AddCallback("selectImage", &UserImageScreenHandler::HandleSelectImage
);
104 AddCallback("checkCameraPresence",
105 &UserImageScreenHandler::HandleCheckCameraPresence
);
106 AddCallback("onUserImageAccepted",
107 &UserImageScreenHandler::HandleImageAccepted
);
108 AddCallback("onUserImageScreenShown",
109 &UserImageScreenHandler::HandleScreenShown
);
112 void UserImageScreenHandler::SelectImage(int index
) {
114 CallJS("setSelectedImage", GetDefaultImageUrl(index
));
117 void UserImageScreenHandler::SendProfileImage(const std::string
& data_url
) {
119 CallJS("setProfileImage", data_url
);
122 void UserImageScreenHandler::OnProfileImageAbsent() {
123 if (page_is_ready()) {
124 scoped_ptr
<base::Value
> null_value(base::Value::CreateNullValue());
125 CallJS("setProfileImage", *null_value
);
129 // TODO(antrim) : It looks more like parameters for "Init" rather than callback.
130 void UserImageScreenHandler::HandleGetImages() {
131 base::ListValue image_urls
;
132 for (int i
= kFirstDefaultImageIndex
; i
< kDefaultImagesCount
; ++i
) {
133 scoped_ptr
<base::DictionaryValue
> image_data(new base::DictionaryValue
);
134 image_data
->SetString("url", GetDefaultImageUrl(i
));
135 image_data
->SetString(
136 "author", l10n_util::GetStringUTF16(kDefaultImageAuthorIDs
[i
]));
137 image_data
->SetString(
138 "website", l10n_util::GetStringUTF16(kDefaultImageWebsiteIDs
[i
]));
139 image_data
->SetString("title", GetDefaultImageDescription(i
));
140 image_urls
.Append(image_data
.release());
142 CallJS("setDefaultImages", image_urls
);
145 if (screen_
->selected_image() != User::kInvalidImageIndex
)
146 SelectImage(screen_
->selected_image());
148 if (screen_
->profile_picture_data_url() != content::kAboutBlankURL
)
149 SendProfileImage(screen_
->profile_picture_data_url());
150 else if (screen_
->profile_picture_absent())
151 OnProfileImageAbsent();
154 void UserImageScreenHandler::HandleScreenReady() {
157 screen_
->OnScreenReady();
160 void UserImageScreenHandler::HandlePhotoTaken(const std::string
& image_url
) {
161 std::string mime_type
, charset
, raw_data
;
162 if (!net::DataURL::Parse(GURL(image_url
), &mime_type
, &charset
, &raw_data
))
164 DCHECK_EQ("image/png", mime_type
);
167 screen_
->OnPhotoTaken(raw_data
);
170 void UserImageScreenHandler::HandleCheckCameraPresence() {
173 screen_
->CheckCameraPresence();
176 void UserImageScreenHandler::HandleSelectImage(const std::string
& image_url
,
177 const std::string
& image_type
,
178 bool is_user_selection
) {
180 screen_
->OnImageSelected(image_type
, image_url
, is_user_selection
);
183 void UserImageScreenHandler::HandleImageAccepted() {
185 screen_
->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::SetCameraPresent(bool present
) {
197 CallJS("setCameraPresent", present
);
200 void UserImageScreenHandler::HideCurtain() {
201 CallJS("hideCurtain");
204 void UserImageScreenHandler::SetProfilePictureEnabled(bool enabled
) {
205 CallJS("setProfilePictureEnabled", enabled
);
208 } // namespace chromeos