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