1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This class keeps track of the currently-active profiles in the runtime.
7 #ifndef CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
8 #define CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_
13 #include "base/basictypes.h"
14 #include "base/containers/hash_tables.h"
15 #include "base/files/file_path.h"
16 #include "base/gtest_prod_util.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/message_loop/message_loop.h"
20 #include "base/threading/non_thread_safe.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/profile_shortcut_manager.h"
23 #include "chrome/browser/ui/browser_list_observer.h"
24 #include "content/public/browser/notification_observer.h"
25 #include "content/public/browser/notification_registrar.h"
27 class NewProfileLauncher
;
28 class ProfileInfoCache
;
30 class ProfileManager
: public base::NonThreadSafe
,
31 public content::NotificationObserver
,
32 public Profile::Delegate
{
34 typedef base::Callback
<void(Profile
*, Profile::CreateStatus
)> CreateCallback
;
36 explicit ProfileManager(const base::FilePath
& user_data_dir
);
37 ~ProfileManager() override
;
39 #if defined(ENABLE_SESSION_SERVICE)
40 // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles.
41 static void ShutdownSessionServices();
44 // Physically remove deleted profile directories from disk.
45 static void NukeDeletedProfilesFromDisk();
47 // Same as instance method but provides the default user_data_dir as well.
48 // If the Profile is going to be used to open a new window then consider using
49 // GetLastUsedProfileAllowedByPolicy() instead.
50 static Profile
* GetLastUsedProfile();
52 // Same as GetLastUsedProfile() but returns the incognito Profile if
53 // incognito mode is forced. This should be used if the last used Profile
54 // will be used to open new browser windows.
55 static Profile
* GetLastUsedProfileAllowedByPolicy();
57 // Same as instance method but provides the default user_data_dir as well.
58 static std::vector
<Profile
*> GetLastOpenedProfiles();
60 // Get the profile for the user which created the current session.
61 // Note that in case of a guest account this will return a 'suitable' profile.
62 // This function is temporary and will soon be moved to ash. As such avoid
63 // using it at all cost.
64 // TODO(skuhne): Move into ash's new user management function.
65 static Profile
* GetPrimaryUserProfile();
67 // Get the profile for the currently active user.
68 // Note that in case of a guest account this will return a 'suitable' profile.
69 // This function is temporary and will soon be moved to ash. As such avoid
70 // using it at all cost.
71 // TODO(skuhne): Move into ash's new user management function.
72 static Profile
* GetActiveUserProfile();
74 // Returns a profile for a specific profile directory within the user data
75 // dir. This will return an existing profile it had already been created,
76 // otherwise it will create and manage it.
77 // Because this method might synchronously create a new profile, it should
78 // only be called for the initial profile or in tests, where blocking is
80 // TODO(bauerb): Migrate calls from other code to GetProfileByPath(), then
81 // make this method private.
82 Profile
* GetProfile(const base::FilePath
& profile_dir
);
84 // Returns total number of profiles available on this machine.
85 size_t GetNumberOfProfiles();
87 // Explicit asynchronous creation of a profile located at |profile_path|.
88 // If the profile has already been created then callback is called
89 // immediately. Should be called on the UI thread.
90 void CreateProfileAsync(const base::FilePath
& profile_path
,
91 const CreateCallback
& callback
,
92 const base::string16
& name
,
93 const base::string16
& icon_url
,
94 const std::string
& supervised_user_id
);
96 // Returns true if the profile pointer is known to point to an existing
98 bool IsValidProfile(Profile
* profile
);
100 // Returns the directory where the first created profile is stored,
101 // relative to the user data directory currently in use.
102 base::FilePath
GetInitialProfileDir();
104 // Get the Profile last used (the Profile to which owns the most recently
105 // focused window) with this Chrome build. If no signed profile has been
106 // stored in Local State, hand back the Default profile.
107 Profile
* GetLastUsedProfile(const base::FilePath
& user_data_dir
);
109 // Get the path of the last used profile, or if that's undefined, the default
111 base::FilePath
GetLastUsedProfileDir(const base::FilePath
& user_data_dir
);
113 // Get the Profiles which are currently open, i.e., have open browsers, or
114 // were open the last time Chrome was running. The Profiles appear in the
115 // order they were opened. The last used profile will be on the list, but its
116 // index on the list will depend on when it was opened (it is not necessarily
118 std::vector
<Profile
*> GetLastOpenedProfiles(
119 const base::FilePath
& user_data_dir
);
121 // Returns created and fully initialized profiles. Note, profiles order is NOT
122 // guaranteed to be related with the creation order.
123 std::vector
<Profile
*> GetLoadedProfiles() const;
125 // If a profile with the given path is currently managed by this object and
126 // fully initialized, return a pointer to the corresponding Profile object;
127 // otherwise return null.
128 Profile
* GetProfileByPath(const base::FilePath
& path
) const;
130 // Creates a new profile in the next available multiprofile directory.
131 // Directories are named "profile_1", "profile_2", etc., in sequence of
132 // creation. (Because directories can be removed, however, it may be the case
133 // that at some point the list of numbered profiles is not continuous.)
134 // |callback| may be invoked multiple times (for CREATE_STATUS_INITIALIZED
135 // and CREATE_STATUS_CREATED) so binding parameters with bind::Passed() is
136 // prohibited. Returns the file path to the profile that will be created
138 static base::FilePath
CreateMultiProfileAsync(
139 const base::string16
& name
,
140 const base::string16
& icon_url
,
141 const CreateCallback
& callback
,
142 const std::string
& supervised_user_id
);
144 // Returns the full path to be used for guest profiles.
145 static base::FilePath
GetGuestProfilePath();
147 // Returns the full path to be used for system profiles.
148 static base::FilePath
GetSystemProfilePath();
150 // Get the path of the next profile directory and increment the internal
152 // Lack of side effects:
153 // This function doesn't actually create the directory or touch the file
155 base::FilePath
GenerateNextProfileDirectoryPath();
157 // Returns a ProfileInfoCache object which can be used to get information
158 // about profiles without having to load them from disk.
159 ProfileInfoCache
& GetProfileInfoCache();
161 // Returns a ProfileShortcut Manager that enables the caller to create
162 // profile specfic desktop shortcuts.
163 ProfileShortcutManager
* profile_shortcut_manager();
165 // Schedules the profile at the given path to be deleted on shutdown. If we're
166 // deleting the last profile, a new one will be created in its place, and in
167 // that case the callback will be called when profile creation is complete.
168 void ScheduleProfileForDeletion(const base::FilePath
& profile_dir
,
169 const CreateCallback
& callback
);
171 // Called on start-up if there are any stale ephemeral profiles to be deleted.
172 // This can be the case if the browser has crashed and the clean-up code had
173 // no chance to run then.
174 static void CleanUpStaleProfiles(
175 const std::vector
<base::FilePath
>& profile_paths
);
177 // Autoloads profiles if they are running background apps.
178 void AutoloadProfiles();
180 // Initializes user prefs of |profile|. This includes profile name and
182 void InitProfileUserPrefs(Profile
* profile
);
184 // Register and add testing profile to the ProfileManager. Use ONLY in tests.
185 // This allows the creation of Profiles outside of the standard creation path
186 // for testing. If |addToCache|, adds to ProfileInfoCache as well.
187 // If |start_deferred_task_runners|, starts the deferred task runners.
188 // Use ONLY in tests.
189 void RegisterTestingProfile(Profile
* profile
,
191 bool start_deferred_task_runners
);
193 const base::FilePath
& user_data_dir() const { return user_data_dir_
; }
195 // For ChromeOS, determines if the user has logged in to a real profile.
196 bool IsLoggedIn() const { return logged_in_
; }
198 // content::NotificationObserver implementation.
199 void Observe(int type
,
200 const content::NotificationSource
& source
,
201 const content::NotificationDetails
& details
) override
;
203 // Profile::Delegate implementation:
204 void OnProfileCreated(Profile
* profile
,
206 bool is_new_profile
) override
;
209 // Does final initial actions.
210 virtual void DoFinalInit(Profile
* profile
, bool go_off_the_record
);
211 virtual void DoFinalInitForServices(Profile
* profile
, bool go_off_the_record
);
212 virtual void DoFinalInitLogging(Profile
* profile
);
214 // Creates a new profile by calling into the profile's profile creation
215 // method. Virtual so that unittests can return a TestingProfile instead
216 // of the Profile's result.
217 virtual Profile
* CreateProfileHelper(const base::FilePath
& path
);
219 // Creates a new profile asynchronously by calling into the profile's
220 // asynchronous profile creation method. Virtual so that unittests can return
221 // a TestingProfile instead of the Profile's result.
222 virtual Profile
* CreateProfileAsyncHelper(const base::FilePath
& path
,
226 friend class TestingProfileManager
;
227 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest
, DeleteAllProfiles
);
228 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest
, SwitchToProfile
);
230 // This struct contains information about profiles which are being loaded or
233 ProfileInfo(Profile
* profile
, bool created
);
237 scoped_ptr
<Profile
> profile
;
238 // Whether profile has been fully loaded (created and initialized).
240 // List of callbacks to run when profile initialization is done. Note, when
241 // profile is fully loaded this vector will be empty.
242 std::vector
<CreateCallback
> callbacks
;
245 DISALLOW_COPY_AND_ASSIGN(ProfileInfo
);
248 // Returns the profile of the active user and / or the off the record profile
249 // if needed. This adds the profile to the ProfileManager if it doesn't
250 // already exist. The method will return NULL if the profile doesn't exist
251 // and we can't create it.
252 // The profile used can be overridden by using --login-profile on cros.
253 Profile
* GetActiveUserOrOffTheRecordProfileFromPath(
254 const base::FilePath
& user_data_dir
);
256 // Adds a pre-existing Profile object to the set managed by this
257 // ProfileManager. This ProfileManager takes ownership of the Profile.
258 // The Profile should not already be managed by this ProfileManager.
259 // Returns true if the profile was added, false otherwise.
260 bool AddProfile(Profile
* profile
);
262 // Synchronously creates and returns a profile. This handles both the full
263 // creation and adds it to the set managed by this ProfileManager.
264 Profile
* CreateAndInitializeProfile(const base::FilePath
& profile_dir
);
266 // Schedules the profile at the given path to be deleted on shutdown,
267 // and marks the new profile as active.
268 void FinishDeletingProfile(const base::FilePath
& profile_dir
,
269 const base::FilePath
& new_active_profile_dir
);
271 // Registers profile with given info. Returns pointer to created ProfileInfo
273 ProfileInfo
* RegisterProfile(Profile
* profile
, bool created
);
275 // Returns ProfileInfo associated with given |path|, registered earlier with
277 ProfileInfo
* GetProfileInfoByPath(const base::FilePath
& path
) const;
279 // Returns a registered profile. In contrast to GetProfileByPath(), this will
280 // also return a profile that is not fully initialized yet, so this method
281 // should be used carefully.
282 Profile
* GetProfileByPathInternal(const base::FilePath
& path
) const;
284 // Adds |profile| to the profile info cache if it hasn't been added yet.
285 void AddProfileToCache(Profile
* profile
);
287 // Apply settings for (desktop) Guest User profile.
288 void SetGuestProfilePrefs(Profile
* profile
);
290 // For ChromeOS, determines if profile should be otr.
291 bool ShouldGoOffTheRecord(Profile
* profile
);
293 void RunCallbacks(const std::vector
<CreateCallback
>& callbacks
,
295 Profile::CreateStatus status
);
297 #if !defined(OS_ANDROID) && !defined(OS_IOS)
298 // Updates the last active user of the current session.
299 // On Chrome OS updating this user will have no effect since when browser is
300 // restored after crash there's another preference that is taken into account.
301 // See kLastActiveUser in UserManagerBase.
302 void UpdateLastUser(Profile
* last_active
);
304 class BrowserListObserver
: public chrome::BrowserListObserver
{
306 explicit BrowserListObserver(ProfileManager
* manager
);
307 ~BrowserListObserver() override
;
309 // chrome::BrowserListObserver implementation.
310 void OnBrowserAdded(Browser
* browser
) override
;
311 void OnBrowserRemoved(Browser
* browser
) override
;
312 void OnBrowserSetLastActive(Browser
* browser
) override
;
315 ProfileManager
* profile_manager_
;
316 DISALLOW_COPY_AND_ASSIGN(BrowserListObserver
);
318 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
320 // If the |loaded_profile| has been loaded successfully (according to
321 // |status|) and isn't already scheduled for deletion, then finishes adding
322 // |profile_to_delete_dir| to the queue of profiles to be deleted, and updates
323 // the kProfileLastUsed preference based on
324 // |last_non_supervised_profile_path|.
325 void OnNewActiveProfileLoaded(
326 const base::FilePath
& profile_to_delete_path
,
327 const base::FilePath
& last_non_supervised_profile_path
,
328 const CreateCallback
& original_callback
,
329 Profile
* loaded_profile
,
330 Profile::CreateStatus status
);
332 content::NotificationRegistrar registrar_
;
334 // The path to the user data directory (DIR_USER_DATA).
335 const base::FilePath user_data_dir_
;
337 // Indicates that a user has logged in and that the profile specified
338 // in the --login-profile command line argument should be used as the
342 #if !defined(OS_ANDROID) && !defined(OS_IOS)
343 BrowserListObserver browser_list_observer_
;
344 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
346 // Maps profile path to ProfileInfo (if profile has been created). Use
347 // RegisterProfile() to add into this map. This map owns all loaded profile
348 // objects in a running instance of Chrome.
349 typedef std::map
<base::FilePath
, linked_ptr
<ProfileInfo
> > ProfilesInfoMap
;
350 ProfilesInfoMap profiles_info_
;
352 // Object to cache various information about profiles. Contains information
353 // about every profile which has been created for this instance of Chrome,
354 // if it has not been explicitly deleted.
355 scoped_ptr
<ProfileInfoCache
> profile_info_cache_
;
357 // Manages the process of creating, deleteing and updating Desktop shortcuts.
358 scoped_ptr
<ProfileShortcutManager
> profile_shortcut_manager_
;
360 // For keeping track of the last active profiles.
361 std::map
<Profile
*, int> browser_counts_
;
362 // On startup we launch the active profiles in the order they became active
363 // during the last run. This is why they are kept in a list, not in a set.
364 std::vector
<Profile
*> active_profiles_
;
365 bool closing_all_browsers_
;
367 DISALLOW_COPY_AND_ASSIGN(ProfileManager
);
370 // Same as the ProfileManager, but doesn't initialize some services of the
371 // profile. This one is useful in unittests.
372 class ProfileManagerWithoutInit
: public ProfileManager
{
374 explicit ProfileManagerWithoutInit(const base::FilePath
& user_data_dir
);
377 void DoFinalInitForServices(Profile
*, bool) override
{}
378 void DoFinalInitLogging(Profile
*) override
{}
381 #endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_