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