Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / webui / chromeos / login / supervised_user_creation_screen_handler.cc
blobc672a2410c7a5579461e73013af56f35b32a5655
1 // Copyright 2014 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/supervised_user_creation_screen_handler.h"
7 #include "ash/audio/sounds.h"
8 #include "base/strings/utf_string_conversions.h"
9 #include "base/values.h"
10 #include "chrome/browser/chromeos/login/screens/user_selection_screen.h"
11 #include "chrome/browser/chromeos/login/supervised/supervised_user_creation_flow.h"
12 #include "chrome/browser/chromeos/login/users/chrome_user_manager.h"
13 #include "chrome/browser/chromeos/login/users/supervised_user_manager.h"
14 #include "chrome/browser/chromeos/login/users/wallpaper/wallpaper_manager.h"
15 #include "chrome/browser/chromeos/settings/cros_settings.h"
16 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.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_manager.h"
22 #include "components/user_manager/user_type.h"
23 #include "google_apis/gaia/gaia_auth_util.h"
24 #include "grit/browser_resources.h"
25 #include "net/base/data_url.h"
26 #include "net/base/escape.h"
27 #include "ui/base/l10n/l10n_util.h"
28 #include "ui/base/resource/resource_bundle.h"
30 const char kJsScreenPath[] = "login.SupervisedUserCreationScreen";
32 namespace chromeos {
34 SupervisedUserCreationScreenHandler::SupervisedUserCreationScreenHandler()
35 : BaseScreenHandler(kJsScreenPath),
36 delegate_(NULL) {
37 ui::ResourceBundle& bundle = ui::ResourceBundle::GetSharedInstance();
38 media::SoundsManager* manager = media::SoundsManager::Get();
39 manager->Initialize(SOUND_OBJECT_DELETE,
40 bundle.GetRawDataResource(IDR_SOUND_OBJECT_DELETE_WAV));
41 manager->Initialize(SOUND_CAMERA_SNAP,
42 bundle.GetRawDataResource(IDR_SOUND_CAMERA_SNAP_WAV));
45 SupervisedUserCreationScreenHandler::~SupervisedUserCreationScreenHandler() {
46 if (delegate_) {
47 delegate_->OnActorDestroyed(this);
51 void SupervisedUserCreationScreenHandler::DeclareLocalizedValues(
52 ::login::LocalizedValuesBuilder* builder) {
53 builder->Add(
54 "supervisedUserCreationFlowRetryButtonTitle",
55 IDS_CREATE_SUPERVISED_USER_CREATION_ERROR_RETRY_BUTTON_TITLE);
56 builder->Add(
57 "supervisedUserCreationFlowCancelButtonTitle",
58 IDS_CREATE_SUPERVISED_USER_CREATION_ERROR_CANCEL_BUTTON_TITLE);
59 builder->Add(
60 "supervisedUserCreationFlowGotitButtonTitle",
61 IDS_CREATE_SUPERVISED_USER_CREATION_GOT_IT_BUTTON_TITLE);
63 builder->Add("createSupervisedUserIntroTextTitle",
64 IDS_CREATE_SUPERVISED_INTRO_TEXT_TITLE);
65 builder->Add("createSupervisedUserIntroAlternateText",
66 IDS_CREATE_SUPERVISED_INTRO_ALTERNATE_TEXT);
67 builder->Add("createSupervisedUserIntroText1",
68 IDS_CREATE_SUPERVISED_INTRO_TEXT_1);
69 builder->Add("createSupervisedUserIntroManagerItem1",
70 IDS_CREATE_SUPERVISED_INTRO_MANAGER_ITEM_1);
71 builder->Add("createSupervisedUserIntroManagerItem2",
72 IDS_CREATE_SUPERVISED_INTRO_MANAGER_ITEM_2);
73 builder->Add("createSupervisedUserIntroManagerItem3",
74 IDS_CREATE_SUPERVISED_INTRO_MANAGER_ITEM_3);
75 builder->Add("createSupervisedUserIntroText2",
76 IDS_CREATE_SUPERVISED_INTRO_TEXT_2);
77 builder->AddF("createSupervisedUserIntroText3",
78 IDS_CREATE_SUPERVISED_INTRO_TEXT_3,
79 base::UTF8ToUTF16(
80 chrome::kLegacySupervisedUserManagementDisplayURL));
82 builder->Add("createSupervisedUserPickManagerTitle",
83 IDS_CREATE_SUPERVISED_USER_CREATE_PICK_MANAGER_TITLE);
84 builder->AddF("createSupervisedUserPickManagerTitleExplanation",
85 IDS_CREATE_SUPERVISED_USER_CREATE_PICK_MANAGER_EXPLANATION,
86 base::UTF8ToUTF16(
87 chrome::kLegacySupervisedUserManagementDisplayURL));
88 builder->Add("createSupervisedUserManagerPasswordHint",
89 IDS_CREATE_SUPERVISED_USER_CREATE_MANAGER_PASSWORD_HINT);
90 builder->Add("createSupervisedUserWrongManagerPasswordText",
91 IDS_CREATE_SUPERVISED_USER_MANAGER_PASSWORD_ERROR);
93 builder->Add("createSupervisedUserNameTitle",
94 IDS_CREATE_SUPERVISED_USER_CREATE_ACCOUNT_NAME_TITLE);
95 builder->Add("createSupervisedUserNameAccessibleTitle",
96 IDS_CREATE_SUPERVISED_USER_SETUP_ACCESSIBLE_TITLE);
97 builder->Add("createSupervisedUserNameExplanation",
98 IDS_CREATE_SUPERVISED_USER_CREATE_ACCOUNT_NAME_EXPLANATION);
99 builder->Add("createSupervisedUserNameHint",
100 IDS_CREATE_SUPERVISED_USER_CREATE_ACCOUNT_NAME_HINT);
101 builder->Add("createSupervisedUserPasswordTitle",
102 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_TITLE);
103 builder->Add("createSupervisedUserPasswordExplanation",
104 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_EXPLANATION);
105 builder->Add("createSupervisedUserPasswordHint",
106 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_HINT);
107 builder->Add("createSupervisedUserPasswordConfirmHint",
108 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_CONFIRM_HINT);
109 builder->Add("supervisedUserCreationFlowProceedButtonTitle",
110 IDS_CREATE_SUPERVISED_USER_CREATE_CONTINUE_BUTTON_TEXT);
111 builder->Add("supervisedUserCreationFlowStartButtonTitle",
112 IDS_CREATE_SUPERVISED_USER_CREATE_START_BUTTON_TEXT);
113 builder->Add("supervisedUserCreationFlowPrevButtonTitle",
114 IDS_CREATE_SUPERVISED_USER_CREATE_PREVIOUS_BUTTON_TEXT);
115 builder->Add("supervisedUserCreationFlowNextButtonTitle",
116 IDS_CREATE_SUPERVISED_USER_CREATE_NEXT_BUTTON_TEXT);
117 builder->Add("supervisedUserCreationFlowHandleErrorButtonTitle",
118 IDS_CREATE_SUPERVISED_USER_CREATE_HANDLE_ERROR_BUTTON_TEXT);
119 builder->Add("createSupervisedUserPasswordMismatchError",
120 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_MISMATCH_ERROR);
122 builder->Add("createSupervisedUserCreatedText1",
123 IDS_CREATE_SUPERVISED_USER_CREATED_1_TEXT_1);
124 builder->Add("createSupervisedUserCreatedText2",
125 IDS_CREATE_SUPERVISED_USER_CREATED_1_TEXT_2);
126 builder->Add("createSupervisedUserCreatedText3",
127 IDS_CREATE_SUPERVISED_USER_CREATED_1_TEXT_3);
129 builder->Add("importExistingSupervisedUserTitle",
130 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_TITLE);
131 builder->Add("importSupervisedUserLink",
132 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_TITLE);
133 builder->Add("importExistingSupervisedUserText",
134 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_TEXT);
135 builder->Add("supervisedUserCreationFlowImportButtonTitle",
136 IDS_IMPORT_EXISTING_LEGACY_SUPERVISED_USER_OK);
137 builder->Add("createSupervisedUserLink",
138 IDS_CREATE_NEW_LEGACY_SUPERVISED_USER_LINK);
139 builder->Add("importBubbleText",
140 IDS_SUPERVISED_USER_IMPORT_BUBBLE_TEXT);
141 builder->Add("importUserExists",
142 IDS_SUPERVISED_USER_IMPORT_USER_EXIST);
143 builder->Add("importUsernameExists",
144 IDS_SUPERVISED_USER_IMPORT_USERNAME_EXIST);
146 builder->Add("managementURL",
147 chrome::kLegacySupervisedUserManagementDisplayURL);
149 // TODO(antrim) : this is an explicit code duplications with UserImageScreen.
150 // It should be removed by issue 251179.
151 builder->Add("takePhoto", IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO);
152 builder->Add("discardPhoto", IDS_OPTIONS_CHANGE_PICTURE_DISCARD_PHOTO);
153 builder->Add("flipPhoto", IDS_OPTIONS_CHANGE_PICTURE_FLIP_PHOTO);
154 builder->Add("photoFlippedAccessibleText",
155 IDS_OPTIONS_PHOTO_FLIP_ACCESSIBLE_TEXT);
156 builder->Add("photoFlippedBackAccessibleText",
157 IDS_OPTIONS_PHOTO_FLIPBACK_ACCESSIBLE_TEXT);
158 builder->Add("photoCaptureAccessibleText",
159 IDS_OPTIONS_PHOTO_CAPTURE_ACCESSIBLE_TEXT);
160 builder->Add("photoDiscardAccessibleText",
161 IDS_OPTIONS_PHOTO_DISCARD_ACCESSIBLE_TEXT);
164 void SupervisedUserCreationScreenHandler::Initialize() {}
166 void SupervisedUserCreationScreenHandler::RegisterMessages() {
167 AddCallback("finishLocalSupervisedUserCreation",
168 &SupervisedUserCreationScreenHandler::
169 HandleFinishLocalSupervisedUserCreation);
170 AddCallback("abortLocalSupervisedUserCreation",
171 &SupervisedUserCreationScreenHandler::
172 HandleAbortLocalSupervisedUserCreation);
173 AddCallback("hideLocalSupervisedUserCreation",
174 &SupervisedUserCreationScreenHandler::
175 HandleHideLocalSupervisedUserCreation);
176 AddCallback("checkSupervisedUserName",
177 &SupervisedUserCreationScreenHandler::
178 HandleCheckSupervisedUserName);
179 AddCallback("authenticateManagerInSupervisedUserCreationFlow",
180 &SupervisedUserCreationScreenHandler::
181 HandleAuthenticateManager);
182 AddCallback("specifySupervisedUserCreationFlowUserData",
183 &SupervisedUserCreationScreenHandler::
184 HandleCreateSupervisedUser);
185 AddCallback("managerSelectedOnSupervisedUserCreationFlow",
186 &SupervisedUserCreationScreenHandler::
187 HandleManagerSelected);
188 AddCallback("userSelectedForImportInSupervisedUserCreationFlow",
189 &SupervisedUserCreationScreenHandler::
190 HandleImportUserSelected);
191 AddCallback("importSupervisedUser",
192 &SupervisedUserCreationScreenHandler::
193 HandleImportSupervisedUser);
194 AddCallback("importSupervisedUserWithPassword",
195 &SupervisedUserCreationScreenHandler::
196 HandleImportSupervisedUserWithPassword);
199 // TODO(antrim) : this is an explicit code duplications with UserImageScreen.
200 // It should be removed by issue 251179.
201 AddCallback("supervisedUserGetImages",
202 &SupervisedUserCreationScreenHandler::HandleGetImages);
204 AddCallback("supervisedUserPhotoTaken",
205 &SupervisedUserCreationScreenHandler::HandlePhotoTaken);
206 AddCallback("supervisedUserTakePhoto",
207 &SupervisedUserCreationScreenHandler::HandleTakePhoto);
208 AddCallback("supervisedUserDiscardPhoto",
209 &SupervisedUserCreationScreenHandler::HandleDiscardPhoto);
210 AddCallback("supervisedUserSelectImage",
211 &SupervisedUserCreationScreenHandler::HandleSelectImage);
212 AddCallback("currentSupervisedUserPage",
213 &SupervisedUserCreationScreenHandler::
214 HandleCurrentSupervisedUserPage);
217 void SupervisedUserCreationScreenHandler::PrepareToShow() {}
219 void SupervisedUserCreationScreenHandler::Show() {
220 scoped_ptr<base::DictionaryValue> data(new base::DictionaryValue());
221 scoped_ptr<base::ListValue> users_list(new base::ListValue());
222 const user_manager::UserList& users =
223 ChromeUserManager::Get()->GetUsersAllowedForSupervisedUsersCreation();
224 std::string owner;
225 chromeos::CrosSettings::Get()->GetString(chromeos::kDeviceOwner, &owner);
227 for (user_manager::UserList::const_iterator it = users.begin();
228 it != users.end();
229 ++it) {
230 bool is_owner = ((*it)->email() == owner);
231 base::DictionaryValue* user_dict = new base::DictionaryValue();
232 UserSelectionScreen::FillUserDictionary(
233 *it, is_owner, false, /* is_signin_to_add */
234 proximity_auth::ScreenlockBridge::LockHandler::OFFLINE_PASSWORD,
235 NULL, /* public_session_recommended_locales */
236 user_dict);
237 users_list->Append(user_dict);
239 data->Set("managers", users_list.release());
240 ShowScreen(OobeUI::kScreenSupervisedUserCreationFlow, data.get());
242 if (!delegate_)
243 return;
246 void SupervisedUserCreationScreenHandler::Hide() {
249 void SupervisedUserCreationScreenHandler::ShowIntroPage() {
250 CallJS("showIntroPage");
253 void SupervisedUserCreationScreenHandler::ShowManagerPasswordError() {
254 CallJS("showManagerPasswordError");
257 void SupervisedUserCreationScreenHandler::ShowStatusMessage(
258 bool is_progress,
259 const base::string16& message) {
260 if (is_progress)
261 CallJS("showProgress", message);
262 else
263 CallJS("showStatusError", message);
266 void SupervisedUserCreationScreenHandler::ShowUsernamePage() {
267 CallJS("showUsernamePage");
270 void SupervisedUserCreationScreenHandler::ShowTutorialPage() {
271 CallJS("showTutorialPage");
274 void SupervisedUserCreationScreenHandler::ShowErrorPage(
275 const base::string16& title,
276 const base::string16& message,
277 const base::string16& button_text) {
278 CallJS("showErrorPage", title, message, button_text);
281 void SupervisedUserCreationScreenHandler::SetDelegate(Delegate* delegate) {
282 delegate_ = delegate;
285 void SupervisedUserCreationScreenHandler::
286 HandleFinishLocalSupervisedUserCreation() {
287 delegate_->FinishFlow();
290 void SupervisedUserCreationScreenHandler::
291 HandleAbortLocalSupervisedUserCreation() {
292 delegate_->AbortFlow();
295 void SupervisedUserCreationScreenHandler::
296 HandleHideLocalSupervisedUserCreation() {
297 delegate_->HideFlow();
300 void SupervisedUserCreationScreenHandler::HandleManagerSelected(
301 const std::string& manager_id) {
302 if (!delegate_)
303 return;
304 WallpaperManager::Get()->SetUserWallpaperNow(manager_id);
307 void SupervisedUserCreationScreenHandler::HandleImportUserSelected(
308 const std::string& user_id) {
309 if (!delegate_)
310 return;
313 void SupervisedUserCreationScreenHandler::HandleCheckSupervisedUserName(
314 const base::string16& name) {
315 std::string user_id;
316 if (NULL !=
317 ChromeUserManager::Get()->GetSupervisedUserManager()->FindByDisplayName(
318 base::CollapseWhitespace(name, true))) {
319 CallJS("supervisedUserNameError", name,
320 l10n_util::GetStringUTF16(
321 IDS_CREATE_SUPERVISED_USER_CREATE_USERNAME_ALREADY_EXISTS));
322 } else if (net::EscapeForHTML(name) != name) {
323 CallJS("supervisedUserNameError", name,
324 l10n_util::GetStringUTF16(
325 IDS_CREATE_SUPERVISED_USER_CREATE_ILLEGAL_USERNAME));
326 } else if (delegate_ && delegate_->FindUserByDisplayName(
327 base::CollapseWhitespace(name, true), &user_id)) {
328 CallJS("supervisedUserSuggestImport", name, user_id);
329 } else {
330 CallJS("supervisedUserNameOk", name);
334 void SupervisedUserCreationScreenHandler::HandleCreateSupervisedUser(
335 const base::string16& new_raw_user_name,
336 const std::string& new_user_password) {
337 if (!delegate_)
338 return;
339 const base::string16 new_user_name =
340 base::CollapseWhitespace(new_raw_user_name, true);
341 if (NULL !=
342 ChromeUserManager::Get()->GetSupervisedUserManager()->FindByDisplayName(
343 new_user_name)) {
344 CallJS("supervisedUserNameError", new_user_name,
345 l10n_util::GetStringFUTF16(
346 IDS_CREATE_SUPERVISED_USER_CREATE_USERNAME_ALREADY_EXISTS,
347 new_user_name));
348 return;
350 if (net::EscapeForHTML(new_user_name) != new_user_name) {
351 CallJS("supervisedUserNameError", new_user_name,
352 l10n_util::GetStringUTF16(
353 IDS_CREATE_SUPERVISED_USER_CREATE_ILLEGAL_USERNAME));
354 return;
357 if (new_user_password.length() == 0) {
358 CallJS("showPasswordError",
359 l10n_util::GetStringUTF16(
360 IDS_CREATE_SUPERVISED_USER_CREATE_PASSWORD_TOO_SHORT));
361 return;
364 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16(
365 IDS_CREATE_SUPERVISED_USER_CREATION_CREATION_PROGRESS_MESSAGE));
367 delegate_->CreateSupervisedUser(new_user_name, new_user_password);
370 void SupervisedUserCreationScreenHandler::HandleImportSupervisedUser(
371 const std::string& user_id) {
372 if (!delegate_)
373 return;
375 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16(
376 IDS_CREATE_SUPERVISED_USER_CREATION_CREATION_PROGRESS_MESSAGE));
378 delegate_->ImportSupervisedUser(user_id);
381 void SupervisedUserCreationScreenHandler::
382 HandleImportSupervisedUserWithPassword(
383 const std::string& user_id,
384 const std::string& password) {
385 if (!delegate_)
386 return;
388 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16(
389 IDS_CREATE_SUPERVISED_USER_CREATION_CREATION_PROGRESS_MESSAGE));
391 delegate_->ImportSupervisedUserWithPassword(user_id, password);
394 void SupervisedUserCreationScreenHandler::HandleAuthenticateManager(
395 const std::string& raw_manager_username,
396 const std::string& manager_password) {
397 const std::string manager_username =
398 gaia::SanitizeEmail(raw_manager_username);
399 delegate_->AuthenticateManager(manager_username, manager_password);
402 // TODO(antrim) : this is an explicit code duplications with UserImageScreen.
403 // It should be removed by issue 251179.
404 void SupervisedUserCreationScreenHandler::HandleGetImages() {
405 base::ListValue image_urls;
406 for (int i = user_manager::kFirstDefaultImageIndex;
407 i < user_manager::kDefaultImagesCount;
408 ++i) {
409 scoped_ptr<base::DictionaryValue> image_data(new base::DictionaryValue);
410 image_data->SetString("url", user_manager::GetDefaultImageUrl(i));
411 image_data->SetString(
412 "author",
413 l10n_util::GetStringUTF16(user_manager::kDefaultImageAuthorIDs[i]));
414 image_data->SetString(
415 "website",
416 l10n_util::GetStringUTF16(user_manager::kDefaultImageWebsiteIDs[i]));
417 image_data->SetString("title", user_manager::GetDefaultImageDescription(i));
418 image_urls.Append(image_data.release());
420 CallJS("setDefaultImages", image_urls);
423 void SupervisedUserCreationScreenHandler::HandlePhotoTaken
424 (const std::string& image_url) {
425 std::string mime_type, charset, raw_data;
426 if (!net::DataURL::Parse(GURL(image_url), &mime_type, &charset, &raw_data))
427 NOTREACHED();
428 DCHECK_EQ("image/png", mime_type);
430 if (delegate_)
431 delegate_->OnPhotoTaken(raw_data);
434 void SupervisedUserCreationScreenHandler::HandleTakePhoto() {
435 ash::PlaySystemSoundIfSpokenFeedback(SOUND_CAMERA_SNAP);
438 void SupervisedUserCreationScreenHandler::HandleDiscardPhoto() {
439 ash::PlaySystemSoundIfSpokenFeedback(SOUND_OBJECT_DELETE);
442 void SupervisedUserCreationScreenHandler::HandleSelectImage(
443 const std::string& image_url,
444 const std::string& image_type) {
445 if (delegate_)
446 delegate_->OnImageSelected(image_type, image_url);
449 void SupervisedUserCreationScreenHandler::HandleCurrentSupervisedUserPage(
450 const std::string& page) {
451 if (delegate_)
452 delegate_->OnPageSelected(page);
455 void SupervisedUserCreationScreenHandler::ShowPage(
456 const std::string& page) {
457 CallJS("showPage", page);
460 void SupervisedUserCreationScreenHandler::SetCameraPresent(bool present) {
461 CallJS("setCameraPresent", present);
464 void SupervisedUserCreationScreenHandler::ShowExistingSupervisedUsers(
465 const base::ListValue* users) {
466 CallJS("setExistingSupervisedUsers", *users);
469 } // namespace chromeos