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
|
// Copyright (c) 2012 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.
// This class keeps track of the currently-active profiles in the runtime.
#ifndef CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
#define CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
#include <list>
#include <vector>
#include "base/basictypes.h"
#include "base/files/file_path.h"
#include "base/gtest_prod_util.h"
#include "base/hash_tables.h"
#include "base/memory/linked_ptr.h"
#include "base/memory/scoped_ptr.h"
#include "base/message_loop.h"
#include "base/threading/non_thread_safe.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/profiles/profile_shortcut_manager.h"
#include "chrome/browser/ui/browser_list_observer.h"
#include "chrome/browser/ui/host_desktop.h"
#include "chrome/browser/ui/startup/startup_types.h"
#include "content/public/browser/notification_observer.h"
#include "content/public/browser/notification_registrar.h"
class CommandLine;
class NewProfileLauncher;
class ProfileInfoCache;
class ProfileManager : public base::NonThreadSafe,
public content::NotificationObserver,
public Profile::Delegate {
public:
typedef base::Callback<void(Profile*, Profile::CreateStatus)> CreateCallback;
explicit ProfileManager(const base::FilePath& user_data_dir);
virtual ~ProfileManager();
#if defined(ENABLE_SESSION_SERVICE)
// Invokes SessionServiceFactory::ShutdownForProfile() for all profiles.
static void ShutdownSessionServices();
#endif
// Physically remove deleted profile directories from disk.
static void NukeDeletedProfilesFromDisk();
// DEPRECATED: DO NOT USE unless in ChromeOS.
// Returns the default profile. This adds the profile to the
// ProfileManager if it doesn't already exist. This method returns NULL if
// the profile doesn't exist and we can't create it.
// The profile used can be overridden by using --login-profile on cros.
Profile* GetDefaultProfile(const base::FilePath& user_data_dir);
// DEPRECATED: DO NOT USE unless in ChromeOS.
// Same as instance method but provides the default user_data_dir as well.
static Profile* GetDefaultProfile();
// DEPRECATED: DO NOT USE unless in ChromeOS.
// Same as GetDefaultProfile() but returns OffTheRecord profile
// if guest login.
static Profile* GetDefaultProfileOrOffTheRecord();
// Returns a profile for a specific profile directory within the user data
// dir. This will return an existing profile it had already been created,
// otherwise it will create and manage it.
Profile* GetProfile(const base::FilePath& profile_dir);
// Returns total number of profiles available on this machine.
size_t GetNumberOfProfiles();
// Explicit asynchronous creation of a profile located at |profile_path|.
// If the profile has already been created then callback is called
// immediately. Should be called on the UI thread.
void CreateProfileAsync(const base::FilePath& profile_path,
const CreateCallback& callback,
const string16& name,
const string16& icon_url,
bool is_managed);
// Initiates profile creation identified by |active_profile_username_hash_|.
// If profile has already been created then the callback is called
// immediately. Should be called on the UI thread.
// This method is only used on Chrome OS where every user profile
// has username_hash associated with it.
static void CreateDefaultProfileAsync(const CreateCallback& callback);
// Returns true if the profile pointer is known to point to an existing
// profile.
bool IsValidProfile(Profile* profile);
// Returns the directory where the first created profile is stored,
// relative to the user data directory currently in use..
base::FilePath GetInitialProfileDir();
// Get the Profile last used (the Profile to which owns the most recently
// focused window) with this Chrome build. If no signed profile has been
// stored in Local State, hand back the Default profile.
Profile* GetLastUsedProfile(const base::FilePath& user_data_dir);
// Same as instance method but provides the default user_data_dir as well.
static Profile* GetLastUsedProfile();
// Get the path of the last used profile, or if that's undefined, the default
// profile.
base::FilePath GetLastUsedProfileDir(const base::FilePath& user_data_dir);
// Get the Profiles which are currently open, i.e., have open browsers, or
// were open the last time Chrome was running. The Profiles appear in the
// order they were opened. The last used profile will be on the list, but its
// index on the list will depend on when it was opened (it is not necessarily
// the last one).
std::vector<Profile*> GetLastOpenedProfiles(
const base::FilePath& user_data_dir);
// Same as instance method but provides the default user_data_dir as well.
static std::vector<Profile*> GetLastOpenedProfiles();
// Returns created profiles. Note, profiles order is NOT guaranteed to be
// related with the creation order.
std::vector<Profile*> GetLoadedProfiles() const;
// content::NotificationObserver implementation.
virtual void Observe(int type,
const content::NotificationSource& source,
const content::NotificationDetails& details) OVERRIDE;
// Returns true if the given command line indicates that this is a short-lived
// profile import process.
static bool IsImportProcess(const CommandLine& command_line);
// Indicate that an import process will run for the next created Profile.
void SetWillImport();
bool will_import() { return will_import_; }
// Indicate that the import process for |profile| has completed.
void OnImportFinished(Profile* profile);
// ------------------ static utility functions -------------------
// Returns the path to the default profile directory, based on the given
// user data directory.
static base::FilePath GetDefaultProfileDir(
const base::FilePath& user_data_dir);
// Returns the path to the preferences file given the user profile directory.
static base::FilePath GetProfilePrefsPath(const base::FilePath& profile_dir);
// If a profile with the given path is currently managed by this object,
// return a pointer to the corresponding Profile object;
// otherwise return NULL.
Profile* GetProfileByPath(const base::FilePath& path) const;
// Activates a window for |profile| on the desktop specified by
// |desktop_type|. If no such window yet exists, or if |always_create| is
// true, this first creates a new window, then activates
// that. If activating an exiting window and multiple windows exists then the
// window that was most recently active is activated. This is used for
// creation of a window from the multi-profile dropdown menu.
static void FindOrCreateNewWindowForProfile(
Profile* profile,
chrome::startup::IsProcessStartup process_startup,
chrome::startup::IsFirstRun is_first_run,
chrome::HostDesktopType desktop_type,
bool always_create);
// Profile::Delegate implementation:
virtual void OnProfileCreated(Profile* profile,
bool success,
bool is_new_profile) OVERRIDE;
// Add or remove a profile launcher to/from the list of launchers waiting for
// new profiles to be created from the multi-profile menu.
void AddProfileLauncher(NewProfileLauncher* profile_launcher);
void RemoveProfileLauncher(NewProfileLauncher* profile_launcher);
// Creates a new profile in the next available multiprofile directory.
// Directories are named "profile_1", "profile_2", etc., in sequence of
// creation. (Because directories can be removed, however, it may be the case
// that at some point the list of numbered profiles is not continuous.)
// |callback| may be invoked multiple times (for CREATE_STATUS_INITIALIZED
// and CREATE_STATUS_CREATED) so binding parameters with bind::Passed() is
// prohibited.
static void CreateMultiProfileAsync(
const string16& name,
const string16& icon_url,
const CreateCallback& callback,
chrome::HostDesktopType desktop_type,
bool is_managed);
// Register multi-profile related preferences in Local State.
static void RegisterPrefs(PrefRegistrySimple* registry);
// Returns a ProfileInfoCache object which can be used to get information
// about profiles without having to load them from disk.
ProfileInfoCache& GetProfileInfoCache();
// Returns a ProfileShortcut Manager that enables the caller to create
// profile specfic desktop shortcuts.
ProfileShortcutManager* profile_shortcut_manager();
// Schedules the profile at the given path to be deleted on shutdown.
void ScheduleProfileForDeletion(const base::FilePath& profile_dir,
chrome::HostDesktopType desktop_type);
// Checks if multiple profiles is enabled.
static bool IsMultipleProfilesEnabled();
// Autoloads profiles if they are running background apps.
void AutoloadProfiles();
// Register and add testing profile to the ProfileManager. Use ONLY in tests.
// This allows the creation of Profiles outside of the standard creation path
// for testing. If |addToCache|, add to ProfileInfoCache as well.
void RegisterTestingProfile(Profile* profile, bool addToCache);
const base::FilePath& user_data_dir() const { return user_data_dir_; }
// For ChromeOS, determines if the user has logged in to a real profile.
bool IsLoggedIn() const { return logged_in_; }
protected:
// Does final initial actions.
virtual void DoFinalInit(Profile* profile, bool go_off_the_record);
virtual void DoFinalInitForServices(Profile* profile, bool go_off_the_record);
virtual void DoFinalInitLogging(Profile* profile);
// Creates a new profile by calling into the profile's profile creation
// method. Virtual so that unittests can return a TestingProfile instead
// of the Profile's result.
virtual Profile* CreateProfileHelper(const base::FilePath& path);
// Creates a new profile asynchronously by calling into the profile's
// asynchronous profile creation method. Virtual so that unittests can return
// a TestingProfile instead of the Profile's result.
virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path,
Delegate* delegate);
private:
friend class TestingProfileManager;
FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, DeleteAllProfiles);
// This struct contains information about profiles which are being loaded or
// were loaded.
struct ProfileInfo {
ProfileInfo(Profile* profile, bool created);
~ProfileInfo();
scoped_ptr<Profile> profile;
// Whether profile has been fully loaded (created and initialized).
bool created;
// Whether or not this profile should have a shortcut.
bool create_shortcut;
// List of callbacks to run when profile initialization is done. Note, when
// profile is fully loaded this vector will be empty.
std::vector<CreateCallback> callbacks;
private:
DISALLOW_COPY_AND_ASSIGN(ProfileInfo);
};
// Adds a pre-existing Profile object to the set managed by this
// ProfileManager. This ProfileManager takes ownership of the Profile.
// The Profile should not already be managed by this ProfileManager.
// Returns true if the profile was added, false otherwise.
bool AddProfile(Profile* profile);
// Registers profile with given info. Returns pointer to created ProfileInfo
// entry.
ProfileInfo* RegisterProfile(Profile* profile, bool created);
// Returns ProfileInfo associated with given |path|, registred earlier with
// RegisterProfile.
ProfileInfo* GetProfileInfoByPath(const base::FilePath& path) const;
typedef std::pair<base::FilePath, string16> ProfilePathAndName;
typedef std::vector<ProfilePathAndName> ProfilePathAndNames;
ProfilePathAndNames GetSortedProfilesFromDirectoryMap();
static bool CompareProfilePathAndName(
const ProfileManager::ProfilePathAndName& pair1,
const ProfileManager::ProfilePathAndName& pair2);
// Adds |profile| to the profile info cache if it hasn't been added yet.
void AddProfileToCache(Profile* profile);
// Initializes user prefs of |profile|. This includes profile name and
// avatar values
void InitProfileUserPrefs(Profile* profile);
// For ChromeOS, determines if profile should be otr.
bool ShouldGoOffTheRecord();
// Get the path of the next profile directory and increment the internal
// count.
// Lack of side effects:
// This function doesn't actually create the directory or touch the file
// system.
base::FilePath GenerateNextProfileDirectoryPath();
void RunCallbacks(const std::vector<CreateCallback>& callbacks,
Profile* profile,
Profile::CreateStatus status);
content::NotificationRegistrar registrar_;
// The path to the user data directory (DIR_USER_DATA).
const base::FilePath user_data_dir_;
// Indicates that a user has logged in and that the profile specified
// in the --login-profile command line argument should be used as the
// default.
bool logged_in_;
// True if an import process will be run.
bool will_import_;
// Maps profile path to ProfileInfo (if profile has been created). Use
// RegisterProfile() to add into this map. This map owns all loaded profile
// objects in a running instance of Chrome.
typedef std::map<base::FilePath, linked_ptr<ProfileInfo> > ProfilesInfoMap;
ProfilesInfoMap profiles_info_;
// Object to cache various information about profiles. Contains information
// about every profile which has been created for this instance of Chrome,
// if it has not been explicitly deleted.
scoped_ptr<ProfileInfoCache> profile_info_cache_;
// Manages the process of creating, deleteing and updating Desktop shortcuts.
scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_;
#if !defined(OS_ANDROID) && !defined(OS_IOS)
class BrowserListObserver : public chrome::BrowserListObserver {
public:
explicit BrowserListObserver(ProfileManager* manager);
virtual ~BrowserListObserver();
// chrome::BrowserListObserver implementation.
virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
private:
ProfileManager* profile_manager_;
DISALLOW_COPY_AND_ASSIGN(BrowserListObserver);
};
BrowserListObserver browser_list_observer_;
#endif // !defined(OS_ANDROID) && !defined(OS_IOS)
// For keeping track of the last active profiles.
std::map<Profile*, int> browser_counts_;
// On startup we launch the active profiles in the order they became active
// during the last run. This is why they are kept in a list, not in a set.
std::vector<Profile*> active_profiles_;
bool closing_all_browsers_;
#if defined(OS_CHROMEOS)
// Identifies active profile on Chrome OS.
std::string active_profile_username_hash_;
#endif
DISALLOW_COPY_AND_ASSIGN(ProfileManager);
};
// Same as the ProfileManager, but doesn't initialize some services of the
// profile. This one is useful in unittests.
class ProfileManagerWithoutInit : public ProfileManager {
public:
explicit ProfileManagerWithoutInit(const base::FilePath& user_data_dir);
protected:
virtual void DoFinalInitForServices(Profile*, bool) OVERRIDE {}
virtual void DoFinalInitLogging(Profile*) OVERRIDE {}
};
#endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
|