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 #include "chrome/browser/ui/webui/chromeos/login/locally_managed_user_creation_screen_handler.h" 6 7 #include "base/strings/utf_string_conversions.h" 8 #include "base/values.h" 9 #include "chrome/browser/chromeos/login/managed/locally_managed_user_creation_flow.h" 10 #include "chrome/browser/chromeos/login/supervised_user_manager.h" 11 #include "chrome/browser/chromeos/login/user_manager.h" 12 #include "chrome/browser/chromeos/login/wallpaper_manager.h" 13 #include "chrome/browser/chromeos/settings/cros_settings.h" 14 #include "chrome/browser/ui/webui/chromeos/login/oobe_ui.h" 15 #include "chrome/browser/ui/webui/chromeos/login/signin_screen_handler.h" 16 #include "chrome/common/url_constants.h" 17 #include "google_apis/gaia/gaia_auth_util.h" 18 #include "grit/generated_resources.h" 19 #include "net/base/data_url.h" 20 #include "net/base/escape.h" 21 #include "ui/base/l10n/l10n_util.h" 22 23 const char kJsScreenPath[] = "login.LocallyManagedUserCreationScreen"; 24 25 namespace chromeos { 26 27 LocallyManagedUserCreationScreenHandler:: 28 LocallyManagedUserCreationScreenHandler() 29 : BaseScreenHandler(kJsScreenPath), 30 delegate_(NULL) { 31 } 32 33 LocallyManagedUserCreationScreenHandler:: 34 ~LocallyManagedUserCreationScreenHandler() { 35 if (delegate_) 36 delegate_->OnActorDestroyed(this); 37 } 38 39 void LocallyManagedUserCreationScreenHandler::DeclareLocalizedValues( 40 LocalizedValuesBuilder* builder) { 41 builder->Add( 42 "managedUserCreationFlowRetryButtonTitle", 43 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_ERROR_RETRY_BUTTON_TITLE); 44 builder->Add( 45 "managedUserCreationFlowCancelButtonTitle", 46 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_ERROR_CANCEL_BUTTON_TITLE); 47 builder->Add( 48 "managedUserCreationFlowGotitButtonTitle", 49 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_GOT_IT_BUTTON_TITLE); 50 51 builder->Add("createManagedUserIntroTextTitle", 52 IDS_CREATE_LOCALLY_MANAGED_INTRO_TEXT_TITLE); 53 builder->Add("createManagedUserIntroAlternateText", 54 IDS_CREATE_LOCALLY_MANAGED_INTRO_ALTERNATE_TEXT); 55 builder->Add("createManagedUserIntroText1", 56 IDS_CREATE_LOCALLY_MANAGED_INTRO_TEXT_1); 57 builder->Add("createManagedUserIntroManagerItem1", 58 IDS_CREATE_LOCALLY_MANAGED_INTRO_MANAGER_ITEM_1); 59 builder->Add("createManagedUserIntroManagerItem2", 60 IDS_CREATE_LOCALLY_MANAGED_INTRO_MANAGER_ITEM_2); 61 builder->Add("createManagedUserIntroManagerItem3", 62 IDS_CREATE_LOCALLY_MANAGED_INTRO_MANAGER_ITEM_3); 63 builder->Add("createManagedUserIntroText2", 64 IDS_CREATE_LOCALLY_MANAGED_INTRO_TEXT_2); 65 builder->AddF("createManagedUserIntroText3", 66 IDS_CREATE_LOCALLY_MANAGED_INTRO_TEXT_3, 67 UTF8ToUTF16(chrome::kSupervisedUserManagementDisplayURL)); 68 69 builder->Add("createManagedUserPickManagerTitle", 70 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PICK_MANAGER_TITLE); 71 builder->AddF("createManagedUserPickManagerTitleExplanation", 72 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PICK_MANAGER_EXPLANATION, 73 UTF8ToUTF16(chrome::kSupervisedUserManagementDisplayURL)); 74 builder->Add("createManagedUserManagerPasswordHint", 75 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_MANAGER_PASSWORD_HINT); 76 builder->Add("createManagedUserWrongManagerPasswordText", 77 IDS_CREATE_LOCALLY_MANAGED_USER_MANAGER_PASSWORD_ERROR); 78 79 builder->Add("createManagedUserNameTitle", 80 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_ACCOUNT_NAME_TITLE); 81 builder->Add("createManagedUserNameExplanation", 82 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_ACCOUNT_NAME_EXPLANATION); 83 builder->Add("createManagedUserPasswordTitle", 84 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_TITLE); 85 builder->Add("createManagedUserPasswordExplanation", 86 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_EXPLANATION); 87 builder->Add("createManagedUserPasswordHint", 88 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_HINT); 89 builder->Add("createManagedUserPasswordConfirmHint", 90 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_CONFIRM_HINT); 91 builder->Add("managedUserCreationFlowProceedButtonTitle", 92 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_CONTINUE_BUTTON_TEXT); 93 builder->Add("managedUserCreationFlowStartButtonTitle", 94 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_START_BUTTON_TEXT); 95 builder->Add("managedUserCreationFlowPrevButtonTitle", 96 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PREVIOUS_BUTTON_TEXT); 97 builder->Add("managedUserCreationFlowNextButtonTitle", 98 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_NEXT_BUTTON_TEXT); 99 builder->Add("managedUserCreationFlowHandleErrorButtonTitle", 100 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_HANDLE_ERROR_BUTTON_TEXT); 101 builder->Add("createManagedUserPasswordMismatchError", 102 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_MISMATCH_ERROR); 103 104 builder->Add("createManagedUserCreatedText1", 105 IDS_CREATE_LOCALLY_MANAGED_USER_CREATED_1_TEXT_1); 106 builder->Add("createManagedUserCreatedText2", 107 IDS_CREATE_LOCALLY_MANAGED_USER_CREATED_1_TEXT_2); 108 builder->Add("createManagedUserCreatedText3", 109 IDS_CREATE_LOCALLY_MANAGED_USER_CREATED_1_TEXT_3); 110 111 builder->Add("importExistingSupervisedUserTitle", 112 IDS_IMPORT_EXISTING_MANAGED_USER_TITLE); 113 builder->Add("importExistingSupervisedUserText", 114 IDS_IMPORT_EXISTING_MANAGED_USER_TEXT); 115 builder->Add("managedUserCreationFlowImportButtonTitle", 116 IDS_IMPORT_EXISTING_MANAGED_USER_OK); 117 builder->Add("importSupervisedUserLink", 118 IDS_PROFILES_IMPORT_EXISTING_MANAGED_USER_LINK); 119 builder->Add("createSupervisedUserLink", 120 IDS_CREATE_NEW_USER_LINK); 121 builder->Add("importBubbleText", IDS_SUPERVISED_USER_IMPORT_BUBBLE_TEXT); 122 builder->Add("importUserExists", IDS_SUPERVISED_USER_IMPORT_USER_EXIST); 123 builder->Add("importUsernameExists", 124 IDS_SUPERVISED_USER_IMPORT_USERNAME_EXIST); 125 126 builder->Add("managementURL", chrome::kSupervisedUserManagementDisplayURL); 127 128 // TODO(antrim) : this is an explicit code duplications with UserImageScreen. 129 // It should be removed by issue 251179. 130 builder->Add("takePhoto", IDS_OPTIONS_CHANGE_PICTURE_TAKE_PHOTO); 131 builder->Add("discardPhoto", IDS_OPTIONS_CHANGE_PICTURE_DISCARD_PHOTO); 132 builder->Add("flipPhoto", IDS_OPTIONS_CHANGE_PICTURE_FLIP_PHOTO); 133 } 134 135 void LocallyManagedUserCreationScreenHandler::Initialize() {} 136 137 void LocallyManagedUserCreationScreenHandler::RegisterMessages() { 138 AddCallback("finishLocalManagedUserCreation", 139 &LocallyManagedUserCreationScreenHandler:: 140 HandleFinishLocalManagedUserCreation); 141 AddCallback("abortLocalManagedUserCreation", 142 &LocallyManagedUserCreationScreenHandler:: 143 HandleAbortLocalManagedUserCreation); 144 AddCallback("checkLocallyManagedUserName", 145 &LocallyManagedUserCreationScreenHandler:: 146 HandleCheckLocallyManagedUserName); 147 AddCallback("authenticateManagerInLocallyManagedUserCreationFlow", 148 &LocallyManagedUserCreationScreenHandler:: 149 HandleAuthenticateManager); 150 AddCallback("specifyLocallyManagedUserCreationFlowUserData", 151 &LocallyManagedUserCreationScreenHandler:: 152 HandleCreateManagedUser); 153 AddCallback("managerSelectedOnLocallyManagedUserCreationFlow", 154 &LocallyManagedUserCreationScreenHandler:: 155 HandleManagerSelected); 156 AddCallback("userSelectedForImportInManagedUserCreationFlow", 157 &LocallyManagedUserCreationScreenHandler:: 158 HandleImportUserSelected); 159 AddCallback("importSupervisedUser", 160 &LocallyManagedUserCreationScreenHandler:: 161 HandleImportSupervisedUser); 162 AddCallback("importSupervisedUserWithPassword", 163 &LocallyManagedUserCreationScreenHandler:: 164 HandleImportSupervisedUserWithPassword); 165 166 167 // TODO(antrim) : this is an explicit code duplications with UserImageScreen. 168 // It should be removed by issue 251179. 169 AddCallback("supervisedUserGetImages", 170 &LocallyManagedUserCreationScreenHandler:: 171 HandleGetImages); 172 173 AddCallback("supervisedUserPhotoTaken", 174 &LocallyManagedUserCreationScreenHandler::HandlePhotoTaken); 175 AddCallback("supervisedUserSelectImage", 176 &LocallyManagedUserCreationScreenHandler::HandleSelectImage); 177 AddCallback("supervisedUserCheckCameraPresence", 178 &LocallyManagedUserCreationScreenHandler:: 179 HandleCheckCameraPresence); 180 AddCallback("currentSupervisedUserPage", 181 &LocallyManagedUserCreationScreenHandler:: 182 HandleCurrentSupervisedUserPage); 183 } 184 185 void LocallyManagedUserCreationScreenHandler::PrepareToShow() {} 186 187 void LocallyManagedUserCreationScreenHandler::Show() { 188 scoped_ptr<DictionaryValue> data(new base::DictionaryValue()); 189 scoped_ptr<ListValue> users_list(new base::ListValue()); 190 const UserList& users = UserManager::Get()->GetUsers(); 191 std::string owner; 192 chromeos::CrosSettings::Get()->GetString(chromeos::kDeviceOwner, &owner); 193 194 for (UserList::const_iterator it = users.begin(); it != users.end(); ++it) { 195 if ((*it)->GetType() != User::USER_TYPE_REGULAR) 196 continue; 197 bool is_owner = ((*it)->email() == owner); 198 DictionaryValue* user_dict = new DictionaryValue(); 199 SigninScreenHandler::FillUserDictionary(*it, is_owner, user_dict); 200 users_list->Append(user_dict); 201 } 202 data->Set("managers", users_list.release()); 203 ShowScreen(OobeUI::kScreenManagedUserCreationFlow, data.get()); 204 205 if (!delegate_) 206 return; 207 delegate_->CheckCameraPresence(); 208 } 209 210 void LocallyManagedUserCreationScreenHandler::Hide() {} 211 212 void LocallyManagedUserCreationScreenHandler::ShowIntroPage() { 213 CallJS("showIntroPage"); 214 } 215 216 void LocallyManagedUserCreationScreenHandler::ShowManagerPasswordError() { 217 CallJS("showManagerPasswordError"); 218 } 219 220 void LocallyManagedUserCreationScreenHandler::ShowStatusMessage( 221 bool is_progress, 222 const base::string16& message) { 223 if (is_progress) 224 CallJS("showProgress", message); 225 else 226 CallJS("showStatusError", message); 227 } 228 229 void LocallyManagedUserCreationScreenHandler::ShowUsernamePage() { 230 CallJS("showUsernamePage"); 231 } 232 233 void LocallyManagedUserCreationScreenHandler::ShowTutorialPage() { 234 CallJS("showTutorialPage"); 235 } 236 237 void LocallyManagedUserCreationScreenHandler::ShowErrorPage( 238 const base::string16& title, 239 const base::string16& message, 240 const base::string16& button_text) { 241 CallJS("showErrorPage", title, message, button_text); 242 } 243 244 void LocallyManagedUserCreationScreenHandler::SetDelegate(Delegate* delegate) { 245 delegate_ = delegate; 246 } 247 248 void LocallyManagedUserCreationScreenHandler:: 249 HandleFinishLocalManagedUserCreation() { 250 delegate_->FinishFlow(); 251 } 252 253 void LocallyManagedUserCreationScreenHandler:: 254 HandleAbortLocalManagedUserCreation() { 255 delegate_->AbortFlow(); 256 } 257 258 void LocallyManagedUserCreationScreenHandler::HandleManagerSelected( 259 const std::string& manager_id) { 260 if (!delegate_) 261 return; 262 WallpaperManager::Get()->SetUserWallpaper(manager_id); 263 } 264 265 void LocallyManagedUserCreationScreenHandler::HandleImportUserSelected( 266 const std::string& user_id) { 267 if (!delegate_) 268 return; 269 } 270 271 void LocallyManagedUserCreationScreenHandler::HandleCheckLocallyManagedUserName( 272 const base::string16& name) { 273 std::string user_id; 274 if (NULL != UserManager::Get()->GetSupervisedUserManager()-> 275 FindByDisplayName(CollapseWhitespace(name, true))) { 276 CallJS("managedUserNameError", name, 277 l10n_util::GetStringUTF16( 278 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_USERNAME_ALREADY_EXISTS)); 279 } else if (net::EscapeForHTML(name) != name) { 280 CallJS("managedUserNameError", name, 281 l10n_util::GetStringUTF16( 282 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_ILLEGAL_USERNAME)); 283 } else if (delegate_ && delegate_->FindUserByDisplayName( 284 CollapseWhitespace(name, true), &user_id)) { 285 CallJS("managedUserSuggestImport", name, user_id); 286 } else { 287 CallJS("managedUserNameOk", name); 288 } 289 } 290 291 void LocallyManagedUserCreationScreenHandler::HandleCreateManagedUser( 292 const base::string16& new_raw_user_name, 293 const std::string& new_user_password) { 294 if (!delegate_) 295 return; 296 const base::string16 new_user_name = 297 CollapseWhitespace(new_raw_user_name, true); 298 if (NULL != UserManager::Get()->GetSupervisedUserManager()-> 299 FindByDisplayName(new_user_name)) { 300 CallJS("managedUserNameError", new_user_name, 301 l10n_util::GetStringFUTF16( 302 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_USERNAME_ALREADY_EXISTS, 303 new_user_name)); 304 return; 305 } 306 if (net::EscapeForHTML(new_user_name) != new_user_name) { 307 CallJS("managedUserNameError", new_user_name, 308 l10n_util::GetStringUTF16( 309 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_ILLEGAL_USERNAME)); 310 return; 311 } 312 313 if (new_user_password.length() == 0) { 314 CallJS("showPasswordError", 315 l10n_util::GetStringUTF16( 316 IDS_CREATE_LOCALLY_MANAGED_USER_CREATE_PASSWORD_TOO_SHORT)); 317 return; 318 } 319 320 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16( 321 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_CREATION_PROGRESS_MESSAGE)); 322 323 delegate_->CreateManagedUser(new_user_name, new_user_password); 324 } 325 326 void LocallyManagedUserCreationScreenHandler::HandleImportSupervisedUser( 327 const std::string& user_id) { 328 if (!delegate_) 329 return; 330 331 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16( 332 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_CREATION_PROGRESS_MESSAGE)); 333 334 delegate_->ImportManagedUser(user_id); 335 } 336 337 void LocallyManagedUserCreationScreenHandler:: 338 HandleImportSupervisedUserWithPassword( 339 const std::string& user_id, 340 const std::string& password) { 341 if (!delegate_) 342 return; 343 344 ShowStatusMessage(true /* progress */, l10n_util::GetStringUTF16( 345 IDS_CREATE_LOCALLY_MANAGED_USER_CREATION_CREATION_PROGRESS_MESSAGE)); 346 347 delegate_->ImportManagedUserWithPassword(user_id, password); 348 } 349 350 void LocallyManagedUserCreationScreenHandler::HandleAuthenticateManager( 351 const std::string& raw_manager_username, 352 const std::string& manager_password) { 353 const std::string manager_username = 354 gaia::SanitizeEmail(raw_manager_username); 355 356 UserFlow* flow = new LocallyManagedUserCreationFlow(manager_username); 357 UserManager::Get()->SetUserFlow(manager_username, flow); 358 359 delegate_->AuthenticateManager(manager_username, manager_password); 360 } 361 362 // TODO(antrim) : this is an explicit code duplications with UserImageScreen. 363 // It should be removed by issue 251179. 364 void LocallyManagedUserCreationScreenHandler::HandleGetImages() { 365 base::ListValue image_urls; 366 for (int i = kFirstDefaultImageIndex; i < kDefaultImagesCount; ++i) { 367 scoped_ptr<base::DictionaryValue> image_data(new base::DictionaryValue); 368 image_data->SetString("url", GetDefaultImageUrl(i)); 369 image_data->SetString( 370 "author", l10n_util::GetStringUTF16(kDefaultImageAuthorIDs[i])); 371 image_data->SetString( 372 "website", l10n_util::GetStringUTF16(kDefaultImageWebsiteIDs[i])); 373 image_data->SetString("title", GetDefaultImageDescription(i)); 374 image_urls.Append(image_data.release()); 375 } 376 CallJS("setDefaultImages", image_urls); 377 } 378 379 void LocallyManagedUserCreationScreenHandler::HandlePhotoTaken 380 (const std::string& image_url) { 381 std::string mime_type, charset, raw_data; 382 if (!net::DataURL::Parse(GURL(image_url), &mime_type, &charset, &raw_data)) 383 NOTREACHED(); 384 DCHECK_EQ("image/png", mime_type); 385 386 if (delegate_) 387 delegate_->OnPhotoTaken(raw_data); 388 } 389 390 void LocallyManagedUserCreationScreenHandler::HandleCheckCameraPresence() { 391 if (!delegate_) 392 return; 393 delegate_->CheckCameraPresence(); 394 } 395 396 void LocallyManagedUserCreationScreenHandler::HandleSelectImage( 397 const std::string& image_url, 398 const std::string& image_type) { 399 if (delegate_) 400 delegate_->OnImageSelected(image_type, image_url); 401 } 402 403 void LocallyManagedUserCreationScreenHandler::HandleCurrentSupervisedUserPage( 404 const std::string& page) { 405 if (delegate_) 406 delegate_->OnPageSelected(page); 407 } 408 409 void LocallyManagedUserCreationScreenHandler::ShowPage( 410 const std::string& page) { 411 CallJS("showPage", page); 412 } 413 414 void LocallyManagedUserCreationScreenHandler::SetCameraPresent(bool present) { 415 CallJS("setCameraPresent", present); 416 } 417 418 void LocallyManagedUserCreationScreenHandler::ShowExistingManagedUsers( 419 const base::ListValue* users) { 420 CallJS("setExistingManagedUsers", *users); 421 } 422 423 } // namespace chromeos 424