Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "chrome/browser/ui/webui/options/create_profile_handler.h" | 5 #include "chrome/browser/ui/webui/options/create_profile_handler.h" |
| 6 | 6 |
| 7 #include "base/bind.h" | 7 #include "base/bind.h" |
| 8 #include "base/files/file_path.h" | 8 #include "base/files/file_path.h" |
| 9 #include "base/metrics/histogram.h" | 9 #include "base/metrics/histogram.h" |
| 10 #include "base/prefs/pref_service.h" | 10 #include "base/prefs/pref_service.h" |
| 11 #include "base/value_conversions.h" | 11 #include "base/value_conversions.h" |
| 12 #include "base/values.h" | 12 #include "base/values.h" |
| 13 #include "chrome/browser/browser_process.h" | 13 #include "chrome/browser/browser_process.h" |
| 14 #include "chrome/browser/profiles/profile_manager.h" | 14 #include "chrome/browser/profiles/profile_manager.h" |
| 15 #include "chrome/browser/profiles/profile_metrics.h" | 15 #include "chrome/browser/profiles/profile_metrics.h" |
| 16 #include "chrome/browser/profiles/profiles_state.h" | 16 #include "chrome/browser/profiles/profiles_state.h" |
| 17 #include "chrome/browser/sync/profile_sync_service.h" | |
| 18 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 19 #include "chrome/browser/ui/webui/options/options_handlers_helper.h" | |
| 20 #include "chrome/common/pref_names.h" | |
| 21 #include "content/public/browser/web_ui.h" | |
| 22 #include "grit/generated_resources.h" | |
| 23 #include "ui/base/l10n/l10n_util.h" | |
| 24 | |
| 25 #if defined(ENABLE_MANAGED_USERS) | |
| 17 #include "chrome/browser/supervised_user/supervised_user_registration_utility.h" | 26 #include "chrome/browser/supervised_user/supervised_user_registration_utility.h" |
| 18 #include "chrome/browser/supervised_user/supervised_user_service.h" | 27 #include "chrome/browser/supervised_user/supervised_user_service.h" |
| 19 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" | 28 #include "chrome/browser/supervised_user/supervised_user_service_factory.h" |
| 20 #include "chrome/browser/supervised_user/supervised_user_sync_service.h" | 29 #include "chrome/browser/supervised_user/supervised_user_sync_service.h" |
| 21 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h" | 30 #include "chrome/browser/supervised_user/supervised_user_sync_service_factory.h" |
| 22 #include "chrome/browser/sync/profile_sync_service.h" | 31 #endif |
| 23 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
| 24 #include "chrome/browser/ui/webui/options/options_handlers_helper.h" | |
| 25 #include "chrome/common/pref_names.h" | |
| 26 #include "grit/generated_resources.h" | |
| 27 #include "ui/base/l10n/l10n_util.h" | |
| 28 | 32 |
| 29 namespace options { | 33 namespace options { |
| 30 | 34 |
| 31 CreateProfileHandler::CreateProfileHandler() | 35 CreateProfileHandler::CreateProfileHandler() |
| 32 : profile_creation_type_(NO_CREATION_IN_PROGRESS), | 36 : profile_creation_type_(NO_CREATION_IN_PROGRESS), |
| 33 weak_ptr_factory_(this) { | 37 weak_ptr_factory_(this) { |
| 34 } | 38 } |
| 35 | 39 |
| 36 CreateProfileHandler::~CreateProfileHandler() { | 40 CreateProfileHandler::~CreateProfileHandler() { |
| 37 CancelProfileRegistration(false); | 41 CancelProfileRegistration(false); |
| 38 } | 42 } |
| 39 | 43 |
| 40 void CreateProfileHandler::GetLocalizedValues( | 44 void CreateProfileHandler::GetLocalizedValues( |
| 41 base::DictionaryValue* localized_strings) { | 45 base::DictionaryValue* localized_strings) { |
| 42 } | 46 } |
| 43 | 47 |
| 44 void CreateProfileHandler::RegisterMessages() { | 48 void CreateProfileHandler::RegisterMessages() { |
| 45 web_ui()->RegisterMessageCallback( | 49 web_ui()->RegisterMessageCallback( |
| 46 "cancelCreateProfile", | 50 "cancelCreateProfile", |
| 47 base::Bind(&CreateProfileHandler::HandleCancelProfileCreation, | 51 base::Bind(&CreateProfileHandler::HandleCancelProfileCreation, |
| 48 base::Unretained(this))); | 52 base::Unretained(this))); |
| 49 web_ui()->RegisterMessageCallback( | 53 web_ui()->RegisterMessageCallback( |
| 50 "createProfile", | 54 "createProfile", |
| 51 base::Bind(&CreateProfileHandler::CreateProfile, | 55 base::Bind(&CreateProfileHandler::CreateProfile, |
| 52 base::Unretained(this))); | 56 base::Unretained(this))); |
| 53 } | 57 } |
| 54 | 58 |
| 55 void CreateProfileHandler::CreateProfile(const base::ListValue* args) { | 59 void CreateProfileHandler::CreateProfile(const base::ListValue* args) { |
| 60 #if defined(ENABLE_MANAGED_USERS) | |
| 56 // This handler could have been called for a supervised user, for example | 61 // This handler could have been called for a supervised user, for example |
| 57 // because the user fiddled with the web inspector. Silently return in this | 62 // because the user fiddled with the web inspector. Silently return. |
| 58 // case. | 63 if (Profile::FromWebUI(web_ui())->IsSupervised()) |
| 59 Profile* current_profile = Profile::FromWebUI(web_ui()); | |
| 60 if (current_profile->IsSupervised()) | |
| 61 return; | 64 return; |
| 65 #endif | |
| 62 | 66 |
| 63 if (!profiles::IsMultipleProfilesEnabled()) | 67 if (!profiles::IsMultipleProfilesEnabled()) |
| 64 return; | 68 return; |
| 65 | 69 |
| 66 // We can have only one in progress profile creation | 70 // We can have only one in progress profile creation |
| 67 // at any given moment, if new ones are initiated just | 71 // at any given moment, if new ones are initiated just |
| 68 // ignore them until we are done with the old one. | 72 // ignore them until we are done with the old one. |
| 69 if (profile_creation_type_ != NO_CREATION_IN_PROGRESS) | 73 if (profile_creation_type_ != NO_CREATION_IN_PROGRESS) |
| 70 return; | 74 return; |
| 71 | 75 |
| 72 profile_creation_type_ = NON_SUPERVISED_PROFILE_CREATION; | 76 profile_creation_type_ = NON_SUPERVISED_PROFILE_CREATION; |
| 73 | 77 |
| 74 DCHECK(profile_path_being_created_.empty()); | 78 DCHECK(profile_path_being_created_.empty()); |
| 75 profile_creation_start_time_ = base::TimeTicks::Now(); | 79 profile_creation_start_time_ = base::TimeTicks::Now(); |
| 76 | 80 |
| 77 base::string16 name; | 81 base::string16 name; |
| 78 base::string16 icon; | 82 base::string16 icon; |
| 83 bool create_shortcut = false; | |
| 84 if (args->GetString(0, &name) && args->GetString(1, &icon)) { | |
| 85 args->GetBoolean(2, &create_shortcut); | |
| 86 } | |
| 79 std::string supervised_user_id; | 87 std::string supervised_user_id; |
| 80 bool create_shortcut = false; | 88 if (!GetSupervisedUserIdFromArgs(args, supervised_user_id)) |
| 81 bool supervised_user = false; | 89 return; |
| 82 if (args->GetString(0, &name) && args->GetString(1, &icon)) { | |
| 83 if (args->GetBoolean(2, &create_shortcut)) { | |
| 84 bool success = args->GetBoolean(3, &supervised_user); | |
| 85 DCHECK(success); | |
| 86 success = args->GetString(4, &supervised_user_id); | |
| 87 DCHECK(success); | |
| 88 } | |
| 89 } | |
| 90 | |
| 91 if (supervised_user) { | |
| 92 if (!IsValidExistingSupervisedUserId(supervised_user_id)) | |
| 93 return; | |
| 94 | |
| 95 profile_creation_type_ = SUPERVISED_PROFILE_IMPORT; | |
| 96 if (supervised_user_id.empty()) { | |
| 97 profile_creation_type_ = SUPERVISED_PROFILE_CREATION; | |
| 98 supervised_user_id = | |
| 99 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(); | |
| 100 | |
| 101 // If sync is not yet fully initialized, the creation may take extra time, | |
| 102 // so show a message. Import doesn't wait for an acknowledgement, so it | |
| 103 // won't have the same potential delay. | |
| 104 ProfileSyncService* sync_service = | |
| 105 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 106 current_profile); | |
| 107 ProfileSyncService::SyncStatusSummary status = | |
| 108 sync_service->QuerySyncStatusSummary(); | |
| 109 if (status == ProfileSyncService::DATATYPES_NOT_INITIALIZED) { | |
| 110 ShowProfileCreationWarning(l10n_util::GetStringUTF16( | |
| 111 IDS_PROFILES_CREATE_SUPERVISED_JUST_SIGNED_IN)); | |
| 112 } | |
| 113 } | |
| 114 } | |
| 115 | 90 |
| 116 ProfileMetrics::LogProfileAddNewUser(ProfileMetrics::ADD_NEW_USER_DIALOG); | 91 ProfileMetrics::LogProfileAddNewUser(ProfileMetrics::ADD_NEW_USER_DIALOG); |
| 117 | 92 |
| 118 profile_path_being_created_ = ProfileManager::CreateMultiProfileAsync( | 93 profile_path_being_created_ = ProfileManager::CreateMultiProfileAsync( |
| 119 name, icon, | 94 name, icon, |
| 120 base::Bind(&CreateProfileHandler::OnProfileCreated, | 95 base::Bind(&CreateProfileHandler::OnProfileCreated, |
| 121 weak_ptr_factory_.GetWeakPtr(), | 96 weak_ptr_factory_.GetWeakPtr(), |
| 122 create_shortcut, | 97 create_shortcut, |
| 123 helper::GetDesktopType(web_ui()), | 98 helper::GetDesktopType(web_ui()), |
| 124 supervised_user_id), | 99 supervised_user_id), |
| 125 supervised_user_id); | 100 supervised_user_id); |
| 126 } | 101 } |
| 127 | 102 |
| 128 void CreateProfileHandler::OnProfileCreated( | 103 void CreateProfileHandler::OnProfileCreated( |
| 129 bool create_shortcut, | 104 bool create_shortcut, |
| 130 chrome::HostDesktopType desktop_type, | 105 chrome::HostDesktopType desktop_type, |
| 131 const std::string& supervised_user_id, | 106 const std::string& supervised_user_id, |
| 132 Profile* profile, | 107 Profile* profile, |
| 133 Profile::CreateStatus status) { | 108 Profile::CreateStatus status) { |
| 134 if (status != Profile::CREATE_STATUS_CREATED) | 109 if (status != Profile::CREATE_STATUS_CREATED) |
| 135 RecordProfileCreationMetrics(status); | 110 RecordProfileCreationMetrics(status); |
| 136 | 111 |
| 137 switch (status) { | 112 switch (status) { |
| 138 case Profile::CREATE_STATUS_LOCAL_FAIL: { | 113 case Profile::CREATE_STATUS_LOCAL_FAIL: { |
| 139 ShowProfileCreationError(profile, | 114 ShowProfileCreationError(profile, GetProfileCreationErrorMessageLocal()); |
| 140 GetProfileCreationErrorMessage(LOCAL_ERROR)); | |
| 141 break; | 115 break; |
| 142 } | 116 } |
| 143 case Profile::CREATE_STATUS_CREATED: { | 117 case Profile::CREATE_STATUS_CREATED: { |
| 144 // Do nothing for an intermediate status. | 118 // Do nothing for an intermediate status. |
| 145 break; | 119 break; |
| 146 } | 120 } |
| 147 case Profile::CREATE_STATUS_INITIALIZED: { | 121 case Profile::CREATE_STATUS_INITIALIZED: { |
| 148 HandleProfileCreationSuccess(create_shortcut, desktop_type, | 122 HandleProfileCreationSuccess(create_shortcut, desktop_type, |
| 149 supervised_user_id, profile); | 123 supervised_user_id, profile); |
| 150 break; | 124 break; |
| (...skipping 15 matching lines...) Expand all Loading... | |
| 166 bool create_shortcut, | 140 bool create_shortcut, |
| 167 chrome::HostDesktopType desktop_type, | 141 chrome::HostDesktopType desktop_type, |
| 168 const std::string& supervised_user_id, | 142 const std::string& supervised_user_id, |
| 169 Profile* profile) { | 143 Profile* profile) { |
| 170 switch (profile_creation_type_) { | 144 switch (profile_creation_type_) { |
| 171 case NON_SUPERVISED_PROFILE_CREATION: { | 145 case NON_SUPERVISED_PROFILE_CREATION: { |
| 172 DCHECK(supervised_user_id.empty()); | 146 DCHECK(supervised_user_id.empty()); |
| 173 CreateShortcutAndShowSuccess(create_shortcut, desktop_type, profile); | 147 CreateShortcutAndShowSuccess(create_shortcut, desktop_type, profile); |
| 174 break; | 148 break; |
| 175 } | 149 } |
| 150 #if defined(ENABLE_MANAGED_USERS) | |
| 176 case SUPERVISED_PROFILE_CREATION: | 151 case SUPERVISED_PROFILE_CREATION: |
| 177 case SUPERVISED_PROFILE_IMPORT: | 152 case SUPERVISED_PROFILE_IMPORT: |
| 178 RegisterSupervisedUser(create_shortcut, desktop_type, | 153 RegisterSupervisedUser(create_shortcut, desktop_type, |
| 179 supervised_user_id, profile); | 154 supervised_user_id, profile); |
| 180 break; | 155 break; |
| 156 #endif | |
| 181 case NO_CREATION_IN_PROGRESS: | 157 case NO_CREATION_IN_PROGRESS: |
| 182 NOTREACHED(); | 158 NOTREACHED(); |
| 183 break; | 159 break; |
| 184 } | 160 } |
| 185 } | 161 } |
| 186 | 162 |
| 187 void CreateProfileHandler::RegisterSupervisedUser( | |
| 188 bool create_shortcut, | |
| 189 chrome::HostDesktopType desktop_type, | |
| 190 const std::string& supervised_user_id, | |
| 191 Profile* new_profile) { | |
| 192 DCHECK_EQ(profile_path_being_created_.value(), | |
| 193 new_profile->GetPath().value()); | |
| 194 | |
| 195 SupervisedUserService* supervised_user_service = | |
| 196 SupervisedUserServiceFactory::GetForProfile(new_profile); | |
| 197 | |
| 198 // Register the supervised user using the profile of the custodian. | |
| 199 supervised_user_registration_utility_ = | |
| 200 SupervisedUserRegistrationUtility::Create(Profile::FromWebUI(web_ui())); | |
| 201 supervised_user_service->RegisterAndInitSync( | |
| 202 supervised_user_registration_utility_.get(), | |
| 203 Profile::FromWebUI(web_ui()), | |
| 204 supervised_user_id, | |
| 205 base::Bind(&CreateProfileHandler::OnSupervisedUserRegistered, | |
| 206 weak_ptr_factory_.GetWeakPtr(), | |
| 207 create_shortcut, | |
| 208 desktop_type, | |
| 209 new_profile)); | |
| 210 } | |
| 211 | |
| 212 void CreateProfileHandler::OnSupervisedUserRegistered( | |
| 213 bool create_shortcut, | |
| 214 chrome::HostDesktopType desktop_type, | |
| 215 Profile* profile, | |
| 216 const GoogleServiceAuthError& error) { | |
| 217 GoogleServiceAuthError::State state = error.state(); | |
| 218 RecordSupervisedProfileCreationMetrics(state); | |
| 219 if (state == GoogleServiceAuthError::NONE) { | |
| 220 CreateShortcutAndShowSuccess(create_shortcut, desktop_type, profile); | |
| 221 return; | |
| 222 } | |
| 223 | |
| 224 base::string16 error_msg; | |
| 225 if (state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | |
| 226 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || | |
| 227 state == GoogleServiceAuthError::ACCOUNT_DELETED || | |
| 228 state == GoogleServiceAuthError::ACCOUNT_DISABLED) { | |
| 229 error_msg = GetProfileCreationErrorMessage(SIGNIN_ERROR); | |
| 230 } else { | |
| 231 error_msg = GetProfileCreationErrorMessage(REMOTE_ERROR); | |
| 232 } | |
| 233 ShowProfileCreationError(profile, error_msg); | |
| 234 } | |
| 235 | |
| 236 void CreateProfileHandler::CreateShortcutAndShowSuccess( | 163 void CreateProfileHandler::CreateShortcutAndShowSuccess( |
| 237 bool create_shortcut, | 164 bool create_shortcut, |
| 238 chrome::HostDesktopType desktop_type, | 165 chrome::HostDesktopType desktop_type, |
| 239 Profile* profile) { | 166 Profile* profile) { |
| 240 if (create_shortcut) { | 167 if (create_shortcut) { |
| 241 ProfileShortcutManager* shortcut_manager = | 168 ProfileShortcutManager* shortcut_manager = |
| 242 g_browser_process->profile_manager()->profile_shortcut_manager(); | 169 g_browser_process->profile_manager()->profile_shortcut_manager(); |
| 243 | 170 |
| 244 if (shortcut_manager) | 171 if (shortcut_manager) |
| 245 shortcut_manager->CreateProfileShortcut(profile->GetPath()); | 172 shortcut_manager->CreateProfileShortcut(profile->GetPath()); |
| 246 } | 173 } |
| 247 | 174 |
| 248 DCHECK_EQ(profile_path_being_created_.value(), profile->GetPath().value()); | 175 DCHECK_EQ(profile_path_being_created_.value(), profile->GetPath().value()); |
| 249 profile_path_being_created_.clear(); | 176 profile_path_being_created_.clear(); |
| 250 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); | 177 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); |
| 251 base::DictionaryValue dict; | 178 base::DictionaryValue dict; |
| 252 dict.SetString("name", | 179 dict.SetString("name", |
| 253 profile->GetPrefs()->GetString(prefs::kProfileName)); | 180 profile->GetPrefs()->GetString(prefs::kProfileName)); |
| 254 dict.Set("filePath", base::CreateFilePathValue(profile->GetPath())); | 181 dict.Set("filePath", base::CreateFilePathValue(profile->GetPath())); |
| 182 #if defined(ENABLE_MANAGED_USERS) | |
| 255 bool is_supervised = | 183 bool is_supervised = |
| 256 profile_creation_type_ == SUPERVISED_PROFILE_CREATION || | 184 profile_creation_type_ == SUPERVISED_PROFILE_CREATION || |
| 257 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT; | 185 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT; |
| 258 dict.SetBoolean("isManaged", is_supervised); | 186 dict.SetBoolean("isManaged", is_supervised); |
| 187 #endif | |
| 259 web_ui()->CallJavascriptFunction( | 188 web_ui()->CallJavascriptFunction( |
| 260 GetJavascriptMethodName(PROFILE_CREATION_SUCCESS), dict); | 189 GetJavascriptMethodName(PROFILE_CREATION_SUCCESS), dict); |
| 261 | 190 |
| 262 // If the new profile is a supervised user, instead of opening a new window | 191 // If the new profile is a supervised user, instead of opening a new window |
| 263 // right away, a confirmation overlay will be shown by JS from the creation | 192 // right away, a confirmation overlay will be shown by JS from the creation |
| 264 // dialog. If we are importing an existing supervised profile or creating a | 193 // dialog. If we are importing an existing supervised profile or creating a |
| 265 // new non-supervised user profile we don't show any confirmation, so open | 194 // new non-supervised user profile we don't show any confirmation, so open |
| 266 // the new window now. | 195 // the new window now. |
| 267 if (profile_creation_type_ != SUPERVISED_PROFILE_CREATION) { | 196 bool should_open_new_window = true; |
| 197 #if defined(ENABLE_MANAGED_USERS) | |
| 198 if (profile_creation_type_ == SUPERVISED_PROFILE_CREATION) | |
| 199 should_open_new_window = false; | |
| 200 #endif | |
| 201 | |
| 202 if (should_open_new_window) { | |
| 268 // Opening the new window must be the last action, after all callbacks | 203 // Opening the new window must be the last action, after all callbacks |
| 269 // have been run, to give them a chance to initialize the profile. | 204 // have been run, to give them a chance to initialize the profile. |
| 270 helper::OpenNewWindowForProfile(desktop_type, | 205 helper::OpenNewWindowForProfile(desktop_type, |
| 271 profile, | 206 profile, |
| 272 Profile::CREATE_STATUS_INITIALIZED); | 207 Profile::CREATE_STATUS_INITIALIZED); |
| 273 } | 208 } |
| 274 profile_creation_type_ = NO_CREATION_IN_PROGRESS; | 209 profile_creation_type_ = NO_CREATION_IN_PROGRESS; |
| 275 } | 210 } |
| 276 | 211 |
| 277 void CreateProfileHandler::ShowProfileCreationError( | 212 void CreateProfileHandler::ShowProfileCreationError( |
| 278 Profile* profile, | 213 Profile* profile, |
| 279 const base::string16& error) { | 214 const base::string16& error) { |
| 280 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); | 215 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); |
| 281 profile_creation_type_ = NO_CREATION_IN_PROGRESS; | 216 profile_creation_type_ = NO_CREATION_IN_PROGRESS; |
| 282 profile_path_being_created_.clear(); | 217 profile_path_being_created_.clear(); |
| 283 web_ui()->CallJavascriptFunction( | 218 web_ui()->CallJavascriptFunction( |
| 284 GetJavascriptMethodName(PROFILE_CREATION_ERROR), | 219 GetJavascriptMethodName(PROFILE_CREATION_ERROR), |
| 285 base::StringValue(error)); | 220 base::StringValue(error)); |
| 286 helper::DeleteProfileAtPath(profile->GetPath(), web_ui()); | 221 helper::DeleteProfileAtPath(profile->GetPath(), web_ui()); |
| 287 } | 222 } |
| 288 | 223 |
| 289 void CreateProfileHandler::ShowProfileCreationWarning( | |
| 290 const base::string16& warning) { | |
| 291 DCHECK_EQ(SUPERVISED_PROFILE_CREATION, profile_creation_type_); | |
| 292 web_ui()->CallJavascriptFunction("BrowserOptions.showCreateProfileWarning", | |
| 293 base::StringValue(warning)); | |
| 294 } | |
| 295 | |
| 296 void CreateProfileHandler::HandleCancelProfileCreation( | 224 void CreateProfileHandler::HandleCancelProfileCreation( |
| 297 const base::ListValue* args) { | 225 const base::ListValue* args) { |
| 298 CancelProfileRegistration(true); | 226 CancelProfileRegistration(true); |
| 299 } | 227 } |
| 300 | 228 |
| 229 void CreateProfileHandler::RecordProfileCreationMetrics( | |
| 230 Profile::CreateStatus status) { | |
| 231 UMA_HISTOGRAM_ENUMERATION("Profile.CreateResult", | |
| 232 status, | |
| 233 Profile::MAX_CREATE_STATUS); | |
| 234 UMA_HISTOGRAM_MEDIUM_TIMES( | |
| 235 "Profile.CreateTimeNoTimeout", | |
| 236 base::TimeTicks::Now() - profile_creation_start_time_); | |
| 237 } | |
| 238 | |
| 239 base::string16 CreateProfileHandler::GetProfileCreationErrorMessageLocal() | |
| 240 const { | |
| 241 int message_id = IDS_PROFILES_CREATE_LOCAL_ERROR; | |
| 242 #if defined(ENABLE_MANAGED_USERS) | |
| 243 // Local errors can occur during supervised profile import. | |
| 244 if (profile_creation_type_ == SUPERVISED_PROFILE_IMPORT) | |
| 245 message_id = IDS_SUPERVISED_USER_IMPORT_LOCAL_ERROR; | |
| 246 #endif | |
| 247 return l10n_util::GetStringUTF16(message_id); | |
| 248 } | |
| 249 | |
| 250 std::string CreateProfileHandler::GetJavascriptMethodName( | |
| 251 ProfileCreationStatus status) const { | |
| 252 switch (profile_creation_type_) { | |
| 253 #if defined(ENABLE_MANAGED_USERS) | |
| 254 case SUPERVISED_PROFILE_IMPORT: | |
| 255 switch (status) { | |
| 256 case PROFILE_CREATION_SUCCESS: | |
| 257 return "BrowserOptions.showManagedUserImportSuccess"; | |
| 258 case PROFILE_CREATION_ERROR: | |
| 259 return "BrowserOptions.showManagedUserImportError"; | |
| 260 } | |
| 261 break; | |
| 262 #endif | |
| 263 default: | |
| 264 switch (status) { | |
| 265 case PROFILE_CREATION_SUCCESS: | |
| 266 return "BrowserOptions.showCreateProfileSuccess"; | |
| 267 case PROFILE_CREATION_ERROR: | |
| 268 return "BrowserOptions.showCreateProfileError"; | |
| 269 } | |
| 270 break; | |
| 271 } | |
| 272 | |
| 273 NOTREACHED(); | |
| 274 return std::string(); | |
| 275 } | |
| 276 | |
| 277 #if defined(ENABLE_MANAGED_USERS) | |
| 278 bool CreateProfileHandler::GetSupervisedUserIdFromArgs( | |
| 279 const base::ListValue* args, std::string& supervised_user_id) { | |
| 280 bool supervised_user = false; | |
| 281 if (args->GetSize() == 4) { | |
| 282 bool success = args->GetBoolean(3, &supervised_user); | |
| 283 DCHECK(success); | |
| 284 | |
| 285 success = args->GetString(4, &supervised_user_id); | |
| 286 DCHECK(success); | |
| 287 } | |
| 288 | |
| 289 if (supervised_user) { | |
| 290 if (!IsValidExistingSupervisedUserId(supervised_user_id)) | |
| 291 return false; | |
| 292 | |
| 293 profile_creation_type_ = SUPERVISED_PROFILE_IMPORT; | |
| 294 if (supervised_user_id.empty()) { | |
| 295 profile_creation_type_ = SUPERVISED_PROFILE_CREATION; | |
| 296 supervised_user_id = | |
| 297 SupervisedUserRegistrationUtility::GenerateNewSupervisedUserId(); | |
| 298 | |
| 299 // If sync is not yet fully initialized, the creation may take extra time, | |
| 300 // so show a message. Import doesn't wait for an acknowledgment, so it | |
| 301 // won't have the same potential delay. | |
| 302 ProfileSyncService* sync_service = | |
| 303 ProfileSyncServiceFactory::GetInstance()->GetForProfile( | |
| 304 Profile::FromWebUI(web_ui())); | |
| 305 ProfileSyncService::SyncStatusSummary status = | |
| 306 sync_service->QuerySyncStatusSummary(); | |
| 307 if (status == ProfileSyncService::DATATYPES_NOT_INITIALIZED) { | |
| 308 ShowProfileCreationWarning(l10n_util::GetStringUTF16( | |
| 309 IDS_PROFILES_CREATE_SUPERVISED_JUST_SIGNED_IN)); | |
| 310 } | |
| 311 } | |
| 312 } | |
| 313 return true; | |
| 314 } | |
| 315 | |
| 316 // Non-supervised user creation cannot be canceled. (Creating a non-supervised | |
| 317 // profile shouldn't take significant time, and it can easily be deleted | |
| 318 // afterward.) | |
| 301 void CreateProfileHandler::CancelProfileRegistration(bool user_initiated) { | 319 void CreateProfileHandler::CancelProfileRegistration(bool user_initiated) { |
| 302 if (profile_path_being_created_.empty()) | 320 if (profile_path_being_created_.empty()) |
| 303 return; | 321 return; |
| 304 | 322 |
| 305 ProfileManager* manager = g_browser_process->profile_manager(); | 323 ProfileManager* manager = g_browser_process->profile_manager(); |
| 306 Profile* new_profile = manager->GetProfileByPath(profile_path_being_created_); | 324 Profile* new_profile = manager->GetProfileByPath(profile_path_being_created_); |
| 307 if (!new_profile) | 325 if (!new_profile || !new_profile->IsSupervised()) |
| 308 return; | 326 return; |
| 309 | 327 |
| 310 // Non-supervised user creation cannot be canceled. (Creating a non-supervised | 328 DCHECK(supervised_user_registration_utility_.get()); |
| 311 // profile shouldn't take significant time, and it can easily be deleted | 329 supervised_user_registration_utility_.reset(); |
| 312 // afterward.) | |
| 313 if (!new_profile->IsSupervised()) | |
| 314 return; | |
| 315 | 330 |
| 316 if (user_initiated) { | 331 if (user_initiated) { |
| 317 UMA_HISTOGRAM_MEDIUM_TIMES( | 332 UMA_HISTOGRAM_MEDIUM_TIMES( |
| 318 "Profile.CreateTimeCanceledNoTimeout", | 333 "Profile.CreateTimeCanceledNoTimeout", |
| 319 base::TimeTicks::Now() - profile_creation_start_time_); | 334 base::TimeTicks::Now() - profile_creation_start_time_); |
| 320 RecordProfileCreationMetrics(Profile::CREATE_STATUS_CANCELED); | 335 RecordProfileCreationMetrics(Profile::CREATE_STATUS_CANCELED); |
| 321 } | 336 } |
| 322 | 337 |
| 323 DCHECK(supervised_user_registration_utility_.get()); | |
| 324 supervised_user_registration_utility_.reset(); | |
| 325 | |
| 326 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); | 338 DCHECK_NE(NO_CREATION_IN_PROGRESS, profile_creation_type_); |
| 327 profile_creation_type_ = NO_CREATION_IN_PROGRESS; | 339 profile_creation_type_ = NO_CREATION_IN_PROGRESS; |
| 328 | 340 |
| 329 // Cancelling registration means the callback passed into | 341 // Cancelling registration means the callback passed into |
| 330 // RegisterAndInitSync() won't be called, so the cleanup must be done here. | 342 // RegisterAndInitSync() won't be called, so the cleanup must be done here. |
| 331 profile_path_being_created_.clear(); | 343 profile_path_being_created_.clear(); |
| 332 helper::DeleteProfileAtPath(new_profile->GetPath(), web_ui()); | 344 helper::DeleteProfileAtPath(new_profile->GetPath(), web_ui()); |
| 333 } | 345 } |
| 334 | 346 |
| 335 void CreateProfileHandler::RecordProfileCreationMetrics( | 347 void CreateProfileHandler::RegisterSupervisedUser( |
| 336 Profile::CreateStatus status) { | 348 bool create_shortcut, |
| 337 UMA_HISTOGRAM_ENUMERATION("Profile.CreateResult", | 349 chrome::HostDesktopType desktop_type, |
| 338 status, | 350 const std::string& supervised_user_id, |
| 339 Profile::MAX_CREATE_STATUS); | 351 Profile* new_profile) { |
| 340 UMA_HISTOGRAM_MEDIUM_TIMES( | 352 DCHECK_EQ(profile_path_being_created_.value(), |
| 341 "Profile.CreateTimeNoTimeout", | 353 new_profile->GetPath().value()); |
| 342 base::TimeTicks::Now() - profile_creation_start_time_); | 354 |
| 355 SupervisedUserService* supervised_user_service = | |
| 356 SupervisedUserServiceFactory::GetForProfile(new_profile); | |
| 357 | |
| 358 // Register the supervised user using the profile of the custodian. | |
| 359 supervised_user_registration_utility_ = | |
| 360 SupervisedUserRegistrationUtility::Create(Profile::FromWebUI(web_ui())); | |
| 361 supervised_user_service->RegisterAndInitSync( | |
| 362 supervised_user_registration_utility_.get(), | |
| 363 Profile::FromWebUI(web_ui()), | |
| 364 supervised_user_id, | |
| 365 base::Bind(&CreateProfileHandler::OnSupervisedUserRegistered, | |
| 366 weak_ptr_factory_.GetWeakPtr(), | |
| 367 create_shortcut, | |
| 368 desktop_type, | |
| 369 new_profile)); | |
| 370 } | |
| 371 | |
| 372 void CreateProfileHandler::OnSupervisedUserRegistered( | |
| 373 bool create_shortcut, | |
| 374 chrome::HostDesktopType desktop_type, | |
| 375 Profile* profile, | |
| 376 const GoogleServiceAuthError& error) { | |
| 377 GoogleServiceAuthError::State state = error.state(); | |
| 378 RecordSupervisedProfileCreationMetrics(state); | |
| 379 if (state == GoogleServiceAuthError::NONE) { | |
| 380 CreateShortcutAndShowSuccess(create_shortcut, desktop_type, profile); | |
| 381 return; | |
| 382 } | |
| 383 | |
| 384 base::string16 error_msg; | |
| 385 if (state == GoogleServiceAuthError::INVALID_GAIA_CREDENTIALS || | |
| 386 state == GoogleServiceAuthError::USER_NOT_SIGNED_UP || | |
| 387 state == GoogleServiceAuthError::ACCOUNT_DELETED || | |
| 388 state == GoogleServiceAuthError::ACCOUNT_DISABLED) { | |
| 389 error_msg = GetProfileCreationErrorMessageSignin(); | |
| 390 } else { | |
| 391 error_msg = GetProfileCreationErrorMessageRemote(); | |
| 392 } | |
| 393 ShowProfileCreationError(profile, error_msg); | |
| 394 } | |
| 395 | |
| 396 void CreateProfileHandler::ShowProfileCreationWarning( | |
| 397 const base::string16& warning) { | |
| 398 DCHECK_EQ(SUPERVISED_PROFILE_CREATION, profile_creation_type_); | |
| 399 web_ui()->CallJavascriptFunction("BrowserOptions.showCreateProfileWarning", | |
| 400 base::StringValue(warning)); | |
| 343 } | 401 } |
| 344 | 402 |
| 345 void CreateProfileHandler::RecordSupervisedProfileCreationMetrics( | 403 void CreateProfileHandler::RecordSupervisedProfileCreationMetrics( |
| 346 GoogleServiceAuthError::State error_state) { | 404 GoogleServiceAuthError::State error_state) { |
| 347 if (profile_creation_type_ == SUPERVISED_PROFILE_CREATION) { | 405 if (profile_creation_type_ == SUPERVISED_PROFILE_CREATION) { |
| 348 UMA_HISTOGRAM_ENUMERATION("Profile.SupervisedProfileCreateError", | 406 UMA_HISTOGRAM_ENUMERATION("Profile.SupervisedProfileCreateError", |
| 349 error_state, | 407 error_state, |
| 350 GoogleServiceAuthError::NUM_STATES); | 408 GoogleServiceAuthError::NUM_STATES); |
| 351 UMA_HISTOGRAM_MEDIUM_TIMES( | 409 UMA_HISTOGRAM_MEDIUM_TIMES( |
| 352 "Profile.SupervisedProfileTotalCreateTime", | 410 "Profile.SupervisedProfileTotalCreateTime", |
| 353 base::TimeTicks::Now() - profile_creation_start_time_); | 411 base::TimeTicks::Now() - profile_creation_start_time_); |
| 354 } else { | 412 } else { |
| 355 DCHECK_EQ(SUPERVISED_PROFILE_IMPORT, profile_creation_type_); | 413 DCHECK_EQ(SUPERVISED_PROFILE_IMPORT, profile_creation_type_); |
| 356 UMA_HISTOGRAM_ENUMERATION("Profile.SupervisedProfileImportError", | 414 UMA_HISTOGRAM_ENUMERATION("Profile.SupervisedProfileImportError", |
| 357 error_state, | 415 error_state, |
| 358 GoogleServiceAuthError::NUM_STATES); | 416 GoogleServiceAuthError::NUM_STATES); |
| 359 UMA_HISTOGRAM_MEDIUM_TIMES( | 417 UMA_HISTOGRAM_MEDIUM_TIMES( |
| 360 "Profile.SupervisedProfileTotalImportTime", | 418 "Profile.SupervisedProfileTotalImportTime", |
| 361 base::TimeTicks::Now() - profile_creation_start_time_); | 419 base::TimeTicks::Now() - profile_creation_start_time_); |
| 362 } | 420 } |
| 363 } | 421 } |
| 364 | 422 |
| 365 base::string16 CreateProfileHandler::GetProfileCreationErrorMessage( | |
| 366 ProfileCreationErrorType error) const { | |
| 367 int message_id = -1; | |
| 368 switch (error) { | |
| 369 case SIGNIN_ERROR: | |
| 370 message_id = | |
| 371 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 372 IDS_SUPERVISED_USER_IMPORT_SIGN_IN_ERROR : | |
| 373 IDS_PROFILES_CREATE_SIGN_IN_ERROR; | |
| 374 break; | |
| 375 case REMOTE_ERROR: | |
| 376 message_id = | |
| 377 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 378 IDS_SUPERVISED_USER_IMPORT_REMOTE_ERROR : | |
| 379 IDS_PROFILES_CREATE_REMOTE_ERROR; | |
| 380 break; | |
| 381 case LOCAL_ERROR: | |
| 382 message_id = | |
| 383 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 384 IDS_SUPERVISED_USER_IMPORT_LOCAL_ERROR : | |
| 385 IDS_PROFILES_CREATE_LOCAL_ERROR; | |
| 386 break; | |
| 387 } | |
| 388 | |
| 389 return l10n_util::GetStringUTF16(message_id); | |
| 390 } | |
| 391 | |
| 392 std::string CreateProfileHandler::GetJavascriptMethodName( | |
| 393 ProfileCreationStatus status) const { | |
| 394 switch (status) { | |
| 395 case PROFILE_CREATION_SUCCESS: | |
| 396 return profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 397 "BrowserOptions.showManagedUserImportSuccess" : | |
| 398 "BrowserOptions.showCreateProfileSuccess"; | |
| 399 case PROFILE_CREATION_ERROR: | |
| 400 return profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 401 "BrowserOptions.showManagedUserImportError" : | |
| 402 "BrowserOptions.showCreateProfileError"; | |
| 403 } | |
| 404 | |
| 405 NOTREACHED(); | |
| 406 return std::string(); | |
| 407 } | |
| 408 | |
| 409 bool CreateProfileHandler::IsValidExistingSupervisedUserId( | 423 bool CreateProfileHandler::IsValidExistingSupervisedUserId( |
| 410 const std::string& existing_supervised_user_id) const { | 424 const std::string& existing_supervised_user_id) const { |
| 411 if (existing_supervised_user_id.empty()) | 425 if (existing_supervised_user_id.empty()) |
| 412 return true; | 426 return true; |
| 413 | 427 |
| 414 Profile* profile = Profile::FromWebUI(web_ui()); | 428 Profile* profile = Profile::FromWebUI(web_ui()); |
| 415 const base::DictionaryValue* dict = | 429 const base::DictionaryValue* dict = |
| 416 SupervisedUserSyncServiceFactory::GetForProfile(profile)-> | 430 SupervisedUserSyncServiceFactory::GetForProfile(profile)-> |
| 417 GetSupervisedUsers(); | 431 GetSupervisedUsers(); |
| 418 if (!dict->HasKey(existing_supervised_user_id)) | 432 if (!dict->HasKey(existing_supervised_user_id)) |
| 419 return false; | 433 return false; |
| 420 | 434 |
| 421 // Check if this supervised user already exists on this machine. | 435 // Check if this supervised user already exists on this machine. |
| 422 const ProfileInfoCache& cache = | 436 const ProfileInfoCache& cache = |
| 423 g_browser_process->profile_manager()->GetProfileInfoCache(); | 437 g_browser_process->profile_manager()->GetProfileInfoCache(); |
| 424 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { | 438 for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) { |
| 425 if (existing_supervised_user_id == | 439 if (existing_supervised_user_id == |
| 426 cache.GetSupervisedUserIdOfProfileAtIndex(i)) | 440 cache.GetSupervisedUserIdOfProfileAtIndex(i)) |
| 427 return false; | 441 return false; |
| 428 } | 442 } |
| 429 return true; | 443 return true; |
| 430 } | 444 } |
| 431 | 445 |
| 446 base::string16 CreateProfileHandler::GetProfileCreationErrorMessageRemote() | |
| 447 const { | |
| 448 return l10n_util::GetStringUTF16( | |
| 449 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 450 IDS_SUPERVISED_USER_IMPORT_REMOTE_ERROR : | |
| 451 IDS_PROFILES_CREATE_REMOTE_ERROR); | |
| 452 } | |
| 453 | |
| 454 base::string16 CreateProfileHandler::GetProfileCreationErrorMessageSignin() | |
| 455 const { | |
| 456 return l10n_util::GetStringUTF16( | |
| 457 profile_creation_type_ == SUPERVISED_PROFILE_IMPORT ? | |
| 458 IDS_SUPERVISED_USER_IMPORT_SIGN_IN_ERROR : | |
| 459 IDS_PROFILES_CREATE_SIGN_IN_ERROR); | |
| 460 } | |
| 461 #else | |
| 462 bool CreateProfileHandler::GetSupervisedUserIdFromArgs( | |
|
Bernhard Bauer
2014/07/21 08:55:15
This is meant as a starting point for future refac
mckev
2014/08/05 01:09:14
Fair point. As an alternative I've ifdef'd their
| |
| 463 const base::ListValue* args, std::string& supervised_user_id) { | |
| 464 // An empty string is the expected value when supervised users aren't enabled. | |
| 465 return true; | |
| 466 } | |
| 467 void CreateProfileHandler::CancelProfileRegistration(bool user_initiated) { | |
| 468 // Since cancellation is only supported for supervised users, cancellation | |
| 469 // here is a no-op. | |
| 470 } | |
| 471 #endif | |
| 472 | |
| 432 } // namespace options | 473 } // namespace options |
| OLD | NEW |