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"
28 class NewProfileLauncher
;
29 class ProfileInfoCache
;
31 class ProfileManager
: public base::NonThreadSafe
,
32 public content::NotificationObserver
,
33 public Profile::Delegate
{
35 typedef base::Callback
<void(Profile
*, Profile::CreateStatus
)> CreateCallback
;
37 explicit ProfileManager(const base::FilePath
& user_data_dir
);
38 virtual ~ProfileManager();
40 #if defined(ENABLE_SESSION_SERVICE)
41 // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles.
42 static void ShutdownSessionServices();
45 // Physically remove deleted profile directories from disk.
46 static void NukeDeletedProfilesFromDisk();
48 // Same as instance method but provides the default user_data_dir as well.
49 // If the Profile is going to be used to open a new window then consider using
50 // GetLastUsedProfileAllowedByPolicy() instead.
51 static Profile
* GetLastUsedProfile();
53 // Same as GetLastUsedProfile() but returns the incognito Profile if
54 // incognito mode is forced. This should be used if the last used Profile
55 // will be used to open new browser windows.
56 static Profile
* GetLastUsedProfileAllowedByPolicy();
58 // Same as instance method but provides the default user_data_dir as well.
59 static std::vector
<Profile
*> GetLastOpenedProfiles();
61 // Get the profile for the user which created the current session.
62 // Note that in case of a guest account this will return a 'suitable' profile.
63 // This function is temporary and will soon be moved to ash. As such avoid
64 // using it at all cost.
65 // TODO(skuhne): Move into ash's new user management function.
66 static Profile
* GetPrimaryUserProfile();
68 // Get the profile for the currently active user.
69 // Note that in case of a guest account this will return a 'suitable' profile.
70 // This function is temporary and will soon be moved to ash. As such avoid
71 // using it at all cost.
72 // TODO(skuhne): Move into ash's new user management function.
73 static Profile
* GetActiveUserProfile();
75 // Returns a profile for a specific profile directory within the user data
76 // dir. This will return an existing profile it had already been created,
77 // otherwise it will create and manage it.
78 Profile
* GetProfile(const base::FilePath
& profile_dir
);
80 // Returns total number of profiles available on this machine.
81 size_t GetNumberOfProfiles();
83 // Explicit asynchronous creation of a profile located at |profile_path|.
84 // If the profile has already been created then callback is called
85 // immediately. Should be called on the UI thread.
86 void CreateProfileAsync(const base::FilePath
& profile_path
,
87 const CreateCallback
& callback
,
88 const base::string16
& name
,
89 const base::string16
& icon_url
,
90 const std::string
& managed_user_id
);
92 // Returns true if the profile pointer is known to point to an existing
94 bool IsValidProfile(Profile
* profile
);
96 // Returns the directory where the first created profile is stored,
97 // relative to the user data directory currently in use..
98 base::FilePath
GetInitialProfileDir();
100 // Get the Profile last used (the Profile to which owns the most recently
101 // focused window) with this Chrome build. If no signed profile has been
102 // stored in Local State, hand back the Default profile.
103 Profile
* GetLastUsedProfile(const base::FilePath
& user_data_dir
);
105 // Get the path of the last used profile, or if that's undefined, the default
107 base::FilePath
GetLastUsedProfileDir(const base::FilePath
& user_data_dir
);
109 // Get the Profiles which are currently open, i.e., have open browsers, or
110 // were open the last time Chrome was running. The Profiles appear in the
111 // order they were opened. The last used profile will be on the list, but its
112 // index on the list will depend on when it was opened (it is not necessarily
114 std::vector
<Profile
*> GetLastOpenedProfiles(
115 const base::FilePath
& user_data_dir
);
117 // Returns created profiles. Note, profiles order is NOT guaranteed to be
118 // related with the creation order.
119 std::vector
<Profile
*> GetLoadedProfiles() const;
121 // If a profile with the given path is currently managed by this object,
122 // return a pointer to the corresponding Profile object;
123 // otherwise return NULL.
124 Profile
* GetProfileByPath(const base::FilePath
& path
) const;
126 // Creates a new profile in the next available multiprofile directory.
127 // Directories are named "profile_1", "profile_2", etc., in sequence of
128 // creation. (Because directories can be removed, however, it may be the case
129 // that at some point the list of numbered profiles is not continuous.)
130 // |callback| may be invoked multiple times (for CREATE_STATUS_INITIALIZED
131 // and CREATE_STATUS_CREATED) so binding parameters with bind::Passed() is
132 // prohibited. Returns the file path to the profile that will be created
134 static base::FilePath
CreateMultiProfileAsync(
135 const base::string16
& name
,
136 const base::string16
& icon_url
,
137 const CreateCallback
& callback
,
138 const std::string
& managed_user_id
);
140 // Returns the full path to be used for guest profiles.
141 static base::FilePath
GetGuestProfilePath();
143 // Get the path of the next profile directory and increment the internal
145 // Lack of side effects:
146 // This function doesn't actually create the directory or touch the file
148 base::FilePath
GenerateNextProfileDirectoryPath();
150 // Returns a ProfileInfoCache object which can be used to get information
151 // about profiles without having to load them from disk.
152 ProfileInfoCache
& GetProfileInfoCache();
154 // Returns a ProfileShortcut Manager that enables the caller to create
155 // profile specfic desktop shortcuts.
156 ProfileShortcutManager
* profile_shortcut_manager();
158 // Schedules the profile at the given path to be deleted on shutdown. If we're
159 // deleting the last profile, a new one will be created in its place, and in
160 // that case the callback will be called when profile creation is complete.
161 void ScheduleProfileForDeletion(const base::FilePath
& profile_dir
,
162 const CreateCallback
& callback
);
164 // Called on start-up if there are any stale ephemeral profiles to be deleted.
165 // This can be the case if the browser has crashed and the clean-up code had
166 // no chance to run then.
167 static void CleanUpStaleProfiles(
168 const std::vector
<base::FilePath
>& profile_paths
);
170 // Autoloads profiles if they are running background apps.
171 void AutoloadProfiles();
173 // Initializes user prefs of |profile|. This includes profile name and
175 void InitProfileUserPrefs(Profile
* profile
);
177 // Register and add testing profile to the ProfileManager. Use ONLY in tests.
178 // This allows the creation of Profiles outside of the standard creation path
179 // for testing. If |addToCache|, adds to ProfileInfoCache as well.
180 // If |start_deferred_task_runners|, starts the deferred task runners.
181 // Use ONLY in tests.
182 void RegisterTestingProfile(Profile
* profile
,
184 bool start_deferred_task_runners
);
186 const base::FilePath
& user_data_dir() const { return user_data_dir_
; }
188 // For ChromeOS, determines if the user has logged in to a real profile.
189 bool IsLoggedIn() const { return logged_in_
; }
191 // content::NotificationObserver implementation.
192 virtual void Observe(int type
,
193 const content::NotificationSource
& source
,
194 const content::NotificationDetails
& details
) OVERRIDE
;
196 // Profile::Delegate implementation:
197 virtual void OnProfileCreated(Profile
* profile
,
199 bool is_new_profile
) OVERRIDE
;
202 // Does final initial actions.
203 virtual void DoFinalInit(Profile
* profile
, bool go_off_the_record
);
204 virtual void DoFinalInitForServices(Profile
* profile
, bool go_off_the_record
);
205 virtual void DoFinalInitLogging(Profile
* profile
);
207 // Creates a new profile by calling into the profile's profile creation
208 // method. Virtual so that unittests can return a TestingProfile instead
209 // of the Profile's result.
210 virtual Profile
* CreateProfileHelper(const base::FilePath
& path
);
212 // Creates a new profile asynchronously by calling into the profile's
213 // asynchronous profile creation method. Virtual so that unittests can return
214 // a TestingProfile instead of the Profile's result.
215 virtual Profile
* CreateProfileAsyncHelper(const base::FilePath
& path
,
219 friend class TestingProfileManager
;
220 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest
, DeleteAllProfiles
);
221 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest
, SwitchToProfile
);
223 // This struct contains information about profiles which are being loaded or
226 ProfileInfo(Profile
* profile
, bool created
);
230 scoped_ptr
<Profile
> profile
;
231 // Whether profile has been fully loaded (created and initialized).
233 // Whether or not this profile should have a shortcut.
234 bool create_shortcut
;
235 // List of callbacks to run when profile initialization is done. Note, when
236 // profile is fully loaded this vector will be empty.
237 std::vector
<CreateCallback
> callbacks
;
240 DISALLOW_COPY_AND_ASSIGN(ProfileInfo
);
243 // Returns the profile of the active user and / or the off the record profile
244 // if needed. This adds the profile to the ProfileManager if it doesn't
245 // already exist. The method will return NULL if the profile doesn't exist
246 // and we can't create it.
247 // The profile used can be overridden by using --login-profile on cros.
248 Profile
* GetActiveUserOrOffTheRecordProfileFromPath(
249 const base::FilePath
& user_data_dir
);
251 // Adds a pre-existing Profile object to the set managed by this
252 // ProfileManager. This ProfileManager takes ownership of the Profile.
253 // The Profile should not already be managed by this ProfileManager.
254 // Returns true if the profile was added, false otherwise.
255 bool AddProfile(Profile
* profile
);
257 // Schedules the profile at the given path to be deleted on shutdown.
258 void FinishDeletingProfile(const base::FilePath
& profile_dir
);
260 // Registers profile with given info. Returns pointer to created ProfileInfo
262 ProfileInfo
* RegisterProfile(Profile
* profile
, bool created
);
264 // Returns ProfileInfo associated with given |path|, registred earlier with
266 ProfileInfo
* GetProfileInfoByPath(const base::FilePath
& path
) const;
268 // Adds |profile| to the profile info cache if it hasn't been added yet.
269 void AddProfileToCache(Profile
* profile
);
271 // Apply settings for (desktop) Guest User profile.
272 void SetGuestProfilePrefs(Profile
* profile
);
274 // For ChromeOS, determines if profile should be otr.
275 bool ShouldGoOffTheRecord(Profile
* profile
);
277 void RunCallbacks(const std::vector
<CreateCallback
>& callbacks
,
279 Profile::CreateStatus status
);
281 #if !defined(OS_ANDROID) && !defined(OS_IOS)
282 class BrowserListObserver
: public chrome::BrowserListObserver
{
284 explicit BrowserListObserver(ProfileManager
* manager
);
285 virtual ~BrowserListObserver();
287 // chrome::BrowserListObserver implementation.
288 virtual void OnBrowserAdded(Browser
* browser
) OVERRIDE
;
289 virtual void OnBrowserRemoved(Browser
* browser
) OVERRIDE
;
290 virtual void OnBrowserSetLastActive(Browser
* browser
) OVERRIDE
;
293 ProfileManager
* profile_manager_
;
294 DISALLOW_COPY_AND_ASSIGN(BrowserListObserver
);
296 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
298 #if defined(OS_MACOSX)
299 // If the |loaded_profile| has been loaded successfully (according to
300 // |status|) and isn't already scheduled for deletion, then finishes adding
301 // |profile_to_delete_dir| to the queue of profiles to be deleted, and updates
302 // the kProfileLastUsed preference based on |last_non_managed_profile_path|.
303 void OnNewActiveProfileLoaded(
304 const base::FilePath
& profile_to_delete_path
,
305 const base::FilePath
& last_non_managed_profile_path
,
306 const CreateCallback
& original_callback
,
307 Profile
* loaded_profile
,
308 Profile::CreateStatus status
);
311 content::NotificationRegistrar registrar_
;
313 // The path to the user data directory (DIR_USER_DATA).
314 const base::FilePath user_data_dir_
;
316 // Indicates that a user has logged in and that the profile specified
317 // in the --login-profile command line argument should be used as the
321 #if !defined(OS_ANDROID) && !defined(OS_IOS)
322 BrowserListObserver browser_list_observer_
;
323 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
325 // Maps profile path to ProfileInfo (if profile has been created). Use
326 // RegisterProfile() to add into this map. This map owns all loaded profile
327 // objects in a running instance of Chrome.
328 typedef std::map
<base::FilePath
, linked_ptr
<ProfileInfo
> > ProfilesInfoMap
;
329 ProfilesInfoMap profiles_info_
;
331 // Object to cache various information about profiles. Contains information
332 // about every profile which has been created for this instance of Chrome,
333 // if it has not been explicitly deleted.
334 scoped_ptr
<ProfileInfoCache
> profile_info_cache_
;
336 // Manages the process of creating, deleteing and updating Desktop shortcuts.
337 scoped_ptr
<ProfileShortcutManager
> profile_shortcut_manager_
;
339 // For keeping track of the last active profiles.
340 std::map
<Profile
*, int> browser_counts_
;
341 // On startup we launch the active profiles in the order they became active
342 // during the last run. This is why they are kept in a list, not in a set.
343 std::vector
<Profile
*> active_profiles_
;
344 bool closing_all_browsers_
;
346 DISALLOW_COPY_AND_ASSIGN(ProfileManager
);
349 // Same as the ProfileManager, but doesn't initialize some services of the
350 // profile. This one is useful in unittests.
351 class ProfileManagerWithoutInit
: public ProfileManager
{
353 explicit ProfileManagerWithoutInit(const base::FilePath
& user_data_dir
);
356 virtual void DoFinalInitForServices(Profile
*, bool) OVERRIDE
{}
357 virtual void DoFinalInitLogging(Profile
*) OVERRIDE
{}
360 #endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_