-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprofile_window.cc
548 lines (491 loc) · 20.7 KB
/
profile_window.cc
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
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
// 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/profiles/profile_window.h"
#include "base/command_line.h"
#include "base/files/file_path.h"
#include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "chrome/browser/about_flags.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/lifetime/application_lifetime.h"
#include "chrome/browser/pref_service_flags_storage.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_avatar_icon_util.h"
#include "chrome/browser/profiles/profile_manager.h"
#include "chrome/browser/signin/account_reconcilor_factory.h"
#include "chrome/browser/signin/account_tracker_service_factory.h"
#include "chrome/browser/sync/profile_sync_service.h"
#include "chrome/browser/sync/profile_sync_service_factory.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_dialogs.h"
#include "chrome/browser/ui/profile_chooser_constants.h"
#include "chrome/browser/ui/user_manager.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/pref_names.h"
#include "chrome/common/url_constants.h"
#include "components/signin/core/browser/account_reconcilor.h"
#include "components/signin/core/browser/account_tracker_service.h"
#include "components/signin/core/common/profile_management_switches.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/user_metrics.h"
#if defined(ENABLE_EXTENSIONS)
#include "chrome/browser/extensions/extension_service.h"
#include "extensions/browser/extension_prefs.h"
#include "extensions/browser/extension_registry.h"
#include "extensions/browser/extension_registry_factory.h"
#include "extensions/browser/extension_system.h"
#endif // defined(ENABLE_EXTENSIONS)
#if !defined(OS_IOS)
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_list.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/browser_window.h"
#include "chrome/browser/ui/startup/startup_browser_creator.h"
#endif // !defined (OS_IOS)
using base::UserMetricsAction;
using content::BrowserThread;
namespace {
const char kNewProfileManagementExperimentInternalName[] =
"enable-new-profile-management";
#if defined(ENABLE_EXTENSIONS)
void BlockExtensions(Profile* profile) {
ExtensionService* extension_service =
extensions::ExtensionSystem::Get(profile)->extension_service();
extension_service->BlockAllExtensions();
}
void UnblockExtensions(Profile* profile) {
ExtensionService* extension_service =
extensions::ExtensionSystem::Get(profile)->extension_service();
extension_service->UnblockAllExtensions();
}
#endif // defined(ENABLE_EXTENSIONS)
// Handles running a callback when a new Browser for the given profile
// has been completely created.
class BrowserAddedForProfileObserver : public chrome::BrowserListObserver {
public:
BrowserAddedForProfileObserver(
Profile* profile,
ProfileManager::CreateCallback callback)
: profile_(profile),
callback_(callback) {
DCHECK(!callback_.is_null());
BrowserList::AddObserver(this);
}
~BrowserAddedForProfileObserver() override {}
private:
// Overridden from BrowserListObserver:
void OnBrowserAdded(Browser* browser) override {
if (browser->profile() == profile_) {
BrowserList::RemoveObserver(this);
callback_.Run(profile_, Profile::CREATE_STATUS_INITIALIZED);
base::MessageLoop::current()->DeleteSoon(FROM_HERE, this);
}
}
// Profile for which the browser should be opened.
Profile* profile_;
ProfileManager::CreateCallback callback_;
DISALLOW_COPY_AND_ASSIGN(BrowserAddedForProfileObserver);
};
void OpenBrowserWindowForProfile(
ProfileManager::CreateCallback callback,
bool always_create,
bool is_new_profile,
chrome::HostDesktopType desktop_type,
Profile* profile,
Profile::CreateStatus status) {
DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
if (status != Profile::CREATE_STATUS_INITIALIZED)
return;
chrome::startup::IsProcessStartup is_process_startup =
chrome::startup::IS_NOT_PROCESS_STARTUP;
chrome::startup::IsFirstRun is_first_run = chrome::startup::IS_NOT_FIRST_RUN;
// If this is a brand new profile, then start a first run window.
if (is_new_profile) {
is_process_startup = chrome::startup::IS_PROCESS_STARTUP;
is_first_run = chrome::startup::IS_FIRST_RUN;
}
#if defined(ENABLE_EXTENSIONS)
// The signin bit will still be set if the profile is being unlocked and the
// browser window for it is opening. As part of this unlock process, unblock
// all the extensions.
const ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
int index = cache.GetIndexOfProfileWithPath(profile->GetPath());
if (!profile->IsGuestSession() &&
cache.ProfileIsSigninRequiredAtIndex(index)) {
UnblockExtensions(profile);
}
#endif // defined(ENABLE_EXTENSIONS)
// If |always_create| is false, and we have a |callback| to run, check
// whether a browser already exists so that we can run the callback. We don't
// want to rely on the observer listening to OnBrowserSetLastActive in this
// case, as you could manually activate an incorrect browser and trigger
// a false positive.
if (!always_create) {
Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
if (browser) {
browser->window()->Activate();
if (!callback.is_null())
callback.Run(profile, Profile::CREATE_STATUS_INITIALIZED);
return;
}
}
// If there is a callback, create an observer to make sure it is only
// run when the browser has been completely created. This observer will
// delete itself once that happens. This should not leak, because we are
// passing |always_create| = true to FindOrCreateNewWindow below, which ends
// up calling LaunchBrowser and opens a new window. If for whatever reason
// that fails, either something has crashed, or the observer will be cleaned
// up when a different browser for this profile is opened.
if (!callback.is_null())
new BrowserAddedForProfileObserver(profile, callback);
// We already dealt with the case when |always_create| was false and a browser
// existed, which means that here a browser definitely needs to be created.
// Passing true for |always_create| means we won't duplicate the code that
// tries to find a browser.
profiles::FindOrCreateNewWindowForProfile(
profile,
is_process_startup,
is_first_run,
desktop_type,
true);
}
// Called after a |system_profile| is available to be used by the user manager.
// Based on the value of |tutorial_mode| we determine a url to be displayed
// by the webui and run the |callback|, if it exists. After opening a profile,
// perform |profile_open_action|.
void OnUserManagerSystemProfileCreated(
const base::FilePath& profile_path_to_focus,
profiles::UserManagerTutorialMode tutorial_mode,
profiles::UserManagerProfileSelected profile_open_action,
const base::Callback<void(Profile*, const std::string&)>& callback,
Profile* system_profile,
Profile::CreateStatus status) {
if (status != Profile::CREATE_STATUS_INITIALIZED || callback.is_null())
return;
// Tell the webui which user should be focused.
std::string page = chrome::kChromeUIUserManagerURL;
if (tutorial_mode == profiles::USER_MANAGER_TUTORIAL_OVERVIEW) {
page += profiles::kUserManagerDisplayTutorial;
} else if (!profile_path_to_focus.empty()) {
const ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
size_t index = cache.GetIndexOfProfileWithPath(profile_path_to_focus);
if (index != std::string::npos) {
page += "#";
page += base::IntToString(index);
}
} else if (profile_open_action ==
profiles::USER_MANAGER_SELECT_PROFILE_TASK_MANAGER) {
page += profiles::kUserManagerSelectProfileTaskManager;
} else if (profile_open_action ==
profiles::USER_MANAGER_SELECT_PROFILE_ABOUT_CHROME) {
page += profiles::kUserManagerSelectProfileAboutChrome;
} else if (profile_open_action ==
profiles::USER_MANAGER_SELECT_PROFILE_CHROME_SETTINGS) {
page += profiles::kUserManagerSelectProfileChromeSettings;
} else if (profile_open_action ==
profiles::USER_MANAGER_SELECT_PROFILE_CHROME_MEMORY) {
page += profiles::kUserManagerSelectProfileChromeMemory;
} else if (profile_open_action ==
profiles::USER_MANAGER_SELECT_PROFILE_APP_LAUNCHER) {
page += profiles::kUserManagerSelectProfileAppLauncher;
}
callback.Run(system_profile, page);
}
// Updates Chrome services that require notification when
// the new_profile_management's status changes.
void UpdateServicesWithNewProfileManagementFlag(Profile* profile,
bool new_flag_status) {
AccountReconcilor* account_reconcilor =
AccountReconcilorFactory::GetForProfile(profile);
account_reconcilor->OnNewProfileManagementFlagChanged(new_flag_status);
}
} // namespace
namespace profiles {
// User Manager parameters are prefixed with hash.
const char kUserManagerDisplayTutorial[] = "#tutorial";
const char kUserManagerSelectProfileTaskManager[] = "#task-manager";
const char kUserManagerSelectProfileAboutChrome[] = "#about-chrome";
const char kUserManagerSelectProfileChromeSettings[] = "#chrome-settings";
const char kUserManagerSelectProfileChromeMemory[] = "#chrome-memory";
const char kUserManagerSelectProfileAppLauncher[] = "#app-launcher";
void FindOrCreateNewWindowForProfile(
Profile* profile,
chrome::startup::IsProcessStartup process_startup,
chrome::startup::IsFirstRun is_first_run,
chrome::HostDesktopType desktop_type,
bool always_create) {
#if defined(OS_IOS)
NOTREACHED();
#else
DCHECK(profile);
if (!always_create) {
Browser* browser = chrome::FindTabbedBrowser(profile, false, desktop_type);
if (browser) {
browser->window()->Activate();
return;
}
}
content::RecordAction(UserMetricsAction("NewWindow"));
base::CommandLine command_line(base::CommandLine::NO_PROGRAM);
StartupBrowserCreator browser_creator;
browser_creator.LaunchBrowser(
command_line, profile, base::FilePath(), process_startup, is_first_run);
#endif // defined(OS_IOS)
}
void SwitchToProfile(const base::FilePath& path,
chrome::HostDesktopType desktop_type,
bool always_create,
ProfileManager::CreateCallback callback,
ProfileMetrics::ProfileOpen metric) {
ProfileMetrics::LogProfileSwitch(metric,
g_browser_process->profile_manager(),
path);
g_browser_process->profile_manager()->CreateProfileAsync(
path,
base::Bind(&OpenBrowserWindowForProfile,
callback,
always_create,
false,
desktop_type),
base::string16(),
base::string16(),
std::string());
}
void SwitchToGuestProfile(chrome::HostDesktopType desktop_type,
ProfileManager::CreateCallback callback) {
const base::FilePath& path = ProfileManager::GetGuestProfilePath();
ProfileMetrics::LogProfileSwitch(ProfileMetrics::SWITCH_PROFILE_GUEST,
g_browser_process->profile_manager(),
path);
g_browser_process->profile_manager()->CreateProfileAsync(
path,
base::Bind(&OpenBrowserWindowForProfile,
callback,
false,
false,
desktop_type),
base::string16(),
base::string16(),
std::string());
}
bool HasProfileSwitchTargets(Profile* profile) {
size_t min_profiles = profile->IsGuestSession() ? 1 : 2;
size_t number_of_profiles =
g_browser_process->profile_manager()->GetNumberOfProfiles();
return number_of_profiles < min_profiles;
}
void CreateAndSwitchToNewProfile(chrome::HostDesktopType desktop_type,
ProfileManager::CreateCallback callback,
ProfileMetrics::ProfileAdd metric) {
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
int placeholder_avatar_index = profiles::GetPlaceholderAvatarIndex();
ProfileManager::CreateMultiProfileAsync(
cache.ChooseNameForNewProfile(placeholder_avatar_index),
base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(
placeholder_avatar_index)),
base::Bind(&OpenBrowserWindowForProfile,
callback,
true,
true,
desktop_type),
std::string());
ProfileMetrics::LogProfileAddNewUser(metric);
}
//start NCSU
void CreateAndSwitchToNewProfileIncognito(chrome::HostDesktopType desktop_type,
ProfileManager::CreateCallback callback,
ProfileMetrics::ProfileAdd metric) {
ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
int placeholder_avatar_index = profiles::GetPlaceholderAvatarIndex();
ProfileManager::CreateMultiProfileAsyncIncognito(
cache.ChooseNameForNewProfile(placeholder_avatar_index),
base::UTF8ToUTF16(profiles::GetDefaultAvatarIconUrl(
placeholder_avatar_index)),
base::Bind(&OpenBrowserWindowForProfile,
callback,
true,
true,
desktop_type),
std::string());
ProfileMetrics::LogProfileAddNewUser(metric);
}
//end NCSU
void GuestBrowserCloseSuccess(const base::FilePath& profile_path) {
UserManager::Show(base::FilePath(),
profiles::USER_MANAGER_NO_TUTORIAL,
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
}
void CloseGuestProfileWindows() {
ProfileManager* profile_manager = g_browser_process->profile_manager();
Profile* profile = profile_manager->GetProfileByPath(
ProfileManager::GetGuestProfilePath());
if (profile) {
BrowserList::CloseAllBrowsersWithProfile(
profile, base::Bind(&GuestBrowserCloseSuccess));
}
}
void LockBrowserCloseSuccess(const base::FilePath& profile_path) {
ProfileManager* profile_manager = g_browser_process->profile_manager();
ProfileInfoCache* cache = &profile_manager->GetProfileInfoCache();
cache->SetProfileSigninRequiredAtIndex(
cache->GetIndexOfProfileWithPath(profile_path), true);
#if defined(ENABLE_EXTENSIONS)
// Profile guaranteed to exist for it to have been locked.
BlockExtensions(profile_manager->GetProfileByPath(profile_path));
#endif // defined(ENABLE_EXTENSIONS)
chrome::HideTaskManager();
UserManager::Show(profile_path,
profiles::USER_MANAGER_NO_TUTORIAL,
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
}
void LockProfile(Profile* profile) {
DCHECK(profile);
if (profile) {
BrowserList::CloseAllBrowsersWithProfile(
profile, base::Bind(&LockBrowserCloseSuccess));
}
}
bool IsLockAvailable(Profile* profile) {
DCHECK(profile);
if (!switches::IsNewProfileManagement())
return false;
if (profile->IsGuestSession())
return false;
const ProfileInfoCache& cache =
g_browser_process->profile_manager()->GetProfileInfoCache();
std::string hosted_domain = profile->GetPrefs()->
GetString(prefs::kGoogleServicesHostedDomain);
// TODO(mlerman): After one release remove any hosted_domain reference to the
// pref, since all users will have this in the AccountTrackerService.
if (hosted_domain.empty()) {
AccountTrackerService* account_tracker =
AccountTrackerServiceFactory::GetForProfile(profile);
int profile_index = cache.GetIndexOfProfileWithPath(profile->GetPath());
hosted_domain = account_tracker->FindAccountInfoByEmail(base::UTF16ToUTF8(
cache.GetUserNameOfProfileAtIndex(profile_index))).hosted_domain;
}
// TODO(mlerman): Prohibit only users who authenticate using SAML. Until then,
// prohibited users who use hosted domains (aside from google.com).
if (hosted_domain != Profile::kNoHostedDomainFound &&
hosted_domain != "google.com") {
return false;
}
for (size_t i = 0; i < cache.GetNumberOfProfiles(); ++i) {
if (cache.ProfileIsSupervisedAtIndex(i))
return true;
}
return false;
}
void CreateSystemProfileForUserManager(
const base::FilePath& profile_path_to_focus,
profiles::UserManagerTutorialMode tutorial_mode,
profiles::UserManagerProfileSelected profile_open_action,
const base::Callback<void(Profile*, const std::string&)>& callback) {
// Create the system profile, if necessary, and open the User Manager
// from the system profile.
g_browser_process->profile_manager()->CreateProfileAsync(
ProfileManager::GetSystemProfilePath(),
base::Bind(&OnUserManagerSystemProfileCreated,
profile_path_to_focus,
tutorial_mode,
profile_open_action,
callback),
base::string16(),
base::string16(),
std::string());
}
void ShowUserManagerMaybeWithTutorial(Profile* profile) {
// Guest users cannot appear in the User Manager, nor display a tutorial.
if (!profile || profile->IsGuestSession()) {
UserManager::Show(base::FilePath(),
profiles::USER_MANAGER_NO_TUTORIAL,
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
return;
}
UserManager::Show(base::FilePath(),
profiles::USER_MANAGER_TUTORIAL_OVERVIEW,
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
}
void EnableNewProfileManagementPreview(Profile* profile) {
#if defined(OS_ANDROID)
NOTREACHED();
#else
// TODO(rogerta): instead of setting experiment flags and command line
// args, we should set a profile preference.
const about_flags::Experiment experiment = {
kNewProfileManagementExperimentInternalName,
0, // string id for title of experiment
0, // string id for description of experiment
0, // supported platforms
about_flags::Experiment::ENABLE_DISABLE_VALUE,
switches::kEnableNewProfileManagement,
"", // not used with ENABLE_DISABLE_VALUE type
switches::kDisableNewProfileManagement,
"", // not used with ENABLE_DISABLE_VALUE type
NULL, // not used with ENABLE_DISABLE_VALUE type
3
};
about_flags::PrefServiceFlagsStorage flags_storage(
g_browser_process->local_state());
about_flags::SetExperimentEnabled(
&flags_storage,
experiment.NameForChoice(1),
true);
switches::EnableNewProfileManagementForTesting(
base::CommandLine::ForCurrentProcess());
UserManager::Show(base::FilePath(),
profiles::USER_MANAGER_TUTORIAL_OVERVIEW,
profiles::USER_MANAGER_SELECT_PROFILE_NO_ACTION);
UpdateServicesWithNewProfileManagementFlag(profile, true);
#endif
}
void DisableNewProfileManagementPreview(Profile* profile) {
about_flags::PrefServiceFlagsStorage flags_storage(
g_browser_process->local_state());
about_flags::SetExperimentEnabled(
&flags_storage,
kNewProfileManagementExperimentInternalName,
false);
chrome::AttemptRestart();
UpdateServicesWithNewProfileManagementFlag(profile, false);
}
void BubbleViewModeFromAvatarBubbleMode(
BrowserWindow::AvatarBubbleMode mode,
BubbleViewMode* bubble_view_mode,
TutorialMode* tutorial_mode) {
*tutorial_mode = TUTORIAL_MODE_NONE;
switch (mode) {
case BrowserWindow::AVATAR_BUBBLE_MODE_ACCOUNT_MANAGEMENT:
*bubble_view_mode = BUBBLE_VIEW_MODE_ACCOUNT_MANAGEMENT;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_SIGNIN:
*bubble_view_mode = BUBBLE_VIEW_MODE_GAIA_SIGNIN;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_ADD_ACCOUNT:
*bubble_view_mode = BUBBLE_VIEW_MODE_GAIA_ADD_ACCOUNT;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_REAUTH:
*bubble_view_mode = BUBBLE_VIEW_MODE_GAIA_REAUTH;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_CONFIRM_SIGNIN:
*bubble_view_mode = BUBBLE_VIEW_MODE_PROFILE_CHOOSER;
*tutorial_mode = TUTORIAL_MODE_CONFIRM_SIGNIN;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_SHOW_ERROR:
*bubble_view_mode = BUBBLE_VIEW_MODE_PROFILE_CHOOSER;
*tutorial_mode = TUTORIAL_MODE_SHOW_ERROR;
return;
case BrowserWindow::AVATAR_BUBBLE_MODE_FAST_USER_SWITCH:
*bubble_view_mode = profiles::BUBBLE_VIEW_MODE_FAST_PROFILE_CHOOSER;
return;
default:
*bubble_view_mode = profiles::BUBBLE_VIEW_MODE_PROFILE_CHOOSER;
}
}
} // namespace profiles