Revert 168224 - Update V8 to version 3.15.4.
[chromium-blink-merge.git] / chrome / browser / profiles / profile_manager.h
blob0c01e33f0ae0fb2236d73e46658897fe6b47f865
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_
10 #include <list>
11 #include <vector>
13 #include "base/basictypes.h"
14 #include "base/file_path.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/hash_tables.h"
17 #include "base/memory/linked_ptr.h"
18 #include "base/memory/scoped_ptr.h"
19 #include "base/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 "chrome/browser/ui/host_desktop.h"
25 #include "chrome/browser/ui/startup/startup_types.h"
26 #include "content/public/browser/notification_observer.h"
27 #include "content/public/browser/notification_registrar.h"
29 class NewProfileLauncher;
30 class ProfileInfoCache;
32 class ProfileManager : public base::NonThreadSafe,
33 public content::NotificationObserver,
34 public Profile::Delegate {
35 public:
36 typedef base::Callback<void(Profile*, Profile::CreateStatus)> CreateCallback;
38 explicit ProfileManager(const FilePath& user_data_dir);
39 virtual ~ProfileManager();
41 #if defined(ENABLE_SESSION_SERVICE)
42 // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles.
43 static void ShutdownSessionServices();
44 #endif
46 // Physically remove deleted profile directories from disk.
47 static void NukeDeletedProfilesFromDisk();
49 // DEPRECATED: DO NOT USE unless in ChromeOS.
50 // Returns the default profile. This adds the profile to the
51 // ProfileManager if it doesn't already exist. This method returns NULL if
52 // the profile doesn't exist and we can't create it.
53 // The profile used can be overridden by using --login-profile on cros.
54 Profile* GetDefaultProfile(const FilePath& user_data_dir);
56 // DEPRECATED: DO NOT USE unless in ChromeOS.
57 // Same as instance method but provides the default user_data_dir as well.
58 static Profile* GetDefaultProfile();
60 // DEPRECATED: DO NOT USE unless in ChromeOS.
61 // Same as GetDefaultProfile() but returns OffTheRecord profile
62 // if guest login.
63 static Profile* GetDefaultProfileOrOffTheRecord();
65 // Returns a profile for a specific profile directory within the user data
66 // dir. This will return an existing profile it had already been created,
67 // otherwise it will create and manage it.
68 Profile* GetProfile(const FilePath& profile_dir);
70 // Returns total number of profiles available on this machine.
71 size_t GetNumberOfProfiles();
73 // Explicit asynchronous creation of a profile located at |profile_path|.
74 // If the profile has already been created then callback is called
75 // immediately. Should be called on the UI thread.
76 void CreateProfileAsync(const FilePath& profile_path,
77 const CreateCallback& callback,
78 const string16& name,
79 const string16& icon_url);
81 // Initiates default profile creation. If default profile has already been
82 // created then the callback is called immediately. Should be called on the
83 // UI thread.
84 static void CreateDefaultProfileAsync(const CreateCallback& callback);
86 // Returns true if the profile pointer is known to point to an existing
87 // profile.
88 bool IsValidProfile(Profile* profile);
90 // Returns the directory where the first created profile is stored,
91 // relative to the user data directory currently in use..
92 FilePath GetInitialProfileDir();
94 // Get the Profile last used (the Profile to which owns the most recently
95 // focused window) with this Chrome build. If no signed profile has been
96 // stored in Local State, hand back the Default profile.
97 Profile* GetLastUsedProfile(const FilePath& user_data_dir);
99 // Same as instance method but provides the default user_data_dir as well.
100 static Profile* GetLastUsedProfile();
102 // Get the Profiles which are currently open, i.e., have open browsers, or
103 // were open the last time Chrome was running. The Profiles appear in the
104 // order they were opened. The last used profile will be on the list, but its
105 // index on the list will depend on when it was opened (it is not necessarily
106 // the last one).
107 std::vector<Profile*> GetLastOpenedProfiles(const FilePath& user_data_dir);
109 // Same as instance method but provides the default user_data_dir as well.
110 static std::vector<Profile*> GetLastOpenedProfiles();
112 // Returns created profiles. Note, profiles order is NOT guaranteed to be
113 // related with the creation order.
114 std::vector<Profile*> GetLoadedProfiles() const;
116 // content::NotificationObserver implementation.
117 virtual void Observe(int type,
118 const content::NotificationSource& source,
119 const content::NotificationDetails& details) OVERRIDE;
121 // Indicate that an import process will run for the next created Profile.
122 void SetWillImport();
123 bool will_import() { return will_import_; }
125 // Indicate that the import process for |profile| has completed.
126 void OnImportFinished(Profile* profile);
128 // ------------------ static utility functions -------------------
130 // Returns the path to the default profile directory, based on the given
131 // user data directory.
132 static FilePath GetDefaultProfileDir(const FilePath& user_data_dir);
134 // Returns the path to the preferences file given the user profile directory.
135 static FilePath GetProfilePrefsPath(const FilePath& profile_dir);
137 // If a profile with the given path is currently managed by this object,
138 // return a pointer to the corresponding Profile object;
139 // otherwise return NULL.
140 Profile* GetProfileByPath(const FilePath& path) const;
142 // Activates a window for |profile| on the desktop specified by
143 // |desktop_type|. If no such window yet exists, or if |always_create| is
144 // true, this first creates a new window, then activates
145 // that. If activating an exiting window and multiple windows exists then the
146 // window that was most recently active is activated. This is used for
147 // creation of a window from the multi-profile dropdown menu.
148 static void FindOrCreateNewWindowForProfile(
149 Profile* profile,
150 chrome::startup::IsProcessStartup process_startup,
151 chrome::startup::IsFirstRun is_first_run,
152 chrome::HostDesktopType desktop_type,
153 bool always_create);
155 // Profile::Delegate implementation:
156 virtual void OnProfileCreated(Profile* profile,
157 bool success,
158 bool is_new_profile) OVERRIDE;
160 // Add or remove a profile launcher to/from the list of launchers waiting for
161 // new profiles to be created from the multi-profile menu.
162 void AddProfileLauncher(NewProfileLauncher* profile_launcher);
163 void RemoveProfileLauncher(NewProfileLauncher* profile_launcher);
165 // Creates a new profile in the next available multiprofile directory.
166 // Directories are named "profile_1", "profile_2", etc., in sequence of
167 // creation. (Because directories can be removed, however, it may be the case
168 // that at some point the list of numbered profiles is not continuous.)
169 static void CreateMultiProfileAsync(
170 const string16& name,
171 const string16& icon_url,
172 const CreateCallback& callback,
173 chrome::HostDesktopType desktop_type);
175 // Register multi-profile related preferences in Local State.
176 static void RegisterPrefs(PrefService* prefs);
178 // Returns a ProfileInfoCache object which can be used to get information
179 // about profiles without having to load them from disk.
180 ProfileInfoCache& GetProfileInfoCache();
182 // Returns a ProfileShortcut Manager that enables the caller to create
183 // profile specfic desktop shortcuts.
184 ProfileShortcutManager* profile_shortcut_manager();
186 // Schedules the profile at the given path to be deleted on shutdown.
187 void ScheduleProfileForDeletion(const FilePath& profile_dir,
188 chrome::HostDesktopType desktop_type);
190 // Checks if multiple profiles is enabled.
191 static bool IsMultipleProfilesEnabled();
193 // Autoloads profiles if they are running background apps.
194 void AutoloadProfiles();
196 // Register and add testing profile to the ProfileManager. Use ONLY in tests.
197 // This allows the creation of Profiles outside of the standard creation path
198 // for testing. If |addToCache|, add to ProfileInfoCache as well.
199 void RegisterTestingProfile(Profile* profile, bool addToCache);
201 const FilePath& user_data_dir() const { return user_data_dir_; }
203 protected:
204 // Does final initial actions.
205 virtual void DoFinalInit(Profile* profile, bool go_off_the_record);
206 virtual void DoFinalInitForServices(Profile* profile, bool go_off_the_record);
207 virtual void DoFinalInitLogging(Profile* profile);
209 // Creates a new profile by calling into the profile's profile creation
210 // method. Virtual so that unittests can return a TestingProfile instead
211 // of the Profile's result.
212 virtual Profile* CreateProfileHelper(const FilePath& path);
214 // Creates a new profile asynchronously by calling into the profile's
215 // asynchronous profile creation method. Virtual so that unittests can return
216 // a TestingProfile instead of the Profile's result.
217 virtual Profile* CreateProfileAsyncHelper(const FilePath& path,
218 Delegate* delegate);
220 private:
221 friend class TestingProfileManager;
222 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, DeleteAllProfiles);
224 // This struct contains information about profiles which are being loaded or
225 // were loaded.
226 struct ProfileInfo {
227 ProfileInfo(Profile* profile, bool created);
229 ~ProfileInfo();
231 scoped_ptr<Profile> profile;
232 // Whether profile has been fully loaded (created and initialized).
233 bool created;
234 // Whether or not this profile should have a shortcut.
235 bool create_shortcut;
236 // List of callbacks to run when profile initialization is done. Note, when
237 // profile is fully loaded this vector will be empty.
238 std::vector<CreateCallback> callbacks;
240 private:
241 DISALLOW_COPY_AND_ASSIGN(ProfileInfo);
244 // Adds a pre-existing Profile object to the set managed by this
245 // ProfileManager. This ProfileManager takes ownership of the Profile.
246 // The Profile should not already be managed by this ProfileManager.
247 // Returns true if the profile was added, false otherwise.
248 bool AddProfile(Profile* profile);
250 // Registers profile with given info. Returns pointer to created ProfileInfo
251 // entry.
252 ProfileInfo* RegisterProfile(Profile* profile, bool created);
254 typedef std::pair<FilePath, string16> ProfilePathAndName;
255 typedef std::vector<ProfilePathAndName> ProfilePathAndNames;
256 ProfilePathAndNames GetSortedProfilesFromDirectoryMap();
258 static bool CompareProfilePathAndName(
259 const ProfileManager::ProfilePathAndName& pair1,
260 const ProfileManager::ProfilePathAndName& pair2);
262 // Adds |profile| to the profile info cache if it hasn't been added yet.
263 void AddProfileToCache(Profile* profile);
265 // Initializes user prefs of |profile|. This includes profile name and
266 // avatar values
267 void InitProfileUserPrefs(Profile* profile);
269 // For ChromeOS, determines if profile should be otr.
270 bool ShouldGoOffTheRecord();
272 // Get the path of the next profile directory and increment the internal
273 // count.
274 // Lack of side effects:
275 // This function doesn't actually create the directory or touch the file
276 // system.
277 FilePath GenerateNextProfileDirectoryPath();
279 void RunCallbacks(const std::vector<CreateCallback>& callbacks,
280 Profile* profile,
281 Profile::CreateStatus status);
283 content::NotificationRegistrar registrar_;
285 // The path to the user data directory (DIR_USER_DATA).
286 const FilePath user_data_dir_;
288 // Indicates that a user has logged in and that the profile specified
289 // in the --login-profile command line argument should be used as the
290 // default.
291 bool logged_in_;
293 // True if an import process will be run.
294 bool will_import_;
296 // Maps profile path to ProfileInfo (if profile has been created). Use
297 // RegisterProfile() to add into this map. This map owns all loaded profile
298 // objects in a running instance of Chrome.
299 typedef std::map<FilePath, linked_ptr<ProfileInfo> > ProfilesInfoMap;
300 ProfilesInfoMap profiles_info_;
302 // Object to cache various information about profiles. Contains information
303 // about every profile which has been created for this instance of Chrome,
304 // if it has not been explicitly deleted.
305 scoped_ptr<ProfileInfoCache> profile_info_cache_;
307 // Manages the process of creating, deleteing and updating Desktop shortcuts.
308 scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_;
310 #if !defined(OS_ANDROID) && !defined(OS_IOS)
311 class BrowserListObserver : public chrome::BrowserListObserver {
312 public:
313 explicit BrowserListObserver(ProfileManager* manager);
314 virtual ~BrowserListObserver();
316 // chrome::BrowserListObserver implementation.
317 virtual void OnBrowserAdded(Browser* browser) OVERRIDE;
318 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE;
319 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE;
321 private:
322 ProfileManager* profile_manager_;
323 DISALLOW_COPY_AND_ASSIGN(BrowserListObserver);
326 BrowserListObserver browser_list_observer_;
327 #endif // !defined(OS_ANDROID) && !defined(OS_IOS)
329 // For keeping track of the last active profiles.
330 std::map<Profile*, int> browser_counts_;
331 // On startup we launch the active profiles in the order they became active
332 // during the last run. This is why they are kept in a list, not in a set.
333 std::vector<Profile*> active_profiles_;
334 bool closing_all_browsers_;
336 DISALLOW_COPY_AND_ASSIGN(ProfileManager);
339 // Same as the ProfileManager, but doesn't initialize some services of the
340 // profile. This one is useful in unittests.
341 class ProfileManagerWithoutInit : public ProfileManager {
342 public:
343 explicit ProfileManagerWithoutInit(const FilePath& user_data_dir);
345 protected:
346 virtual void DoFinalInitForServices(Profile*, bool) OVERRIDE {}
347 virtual void DoFinalInitLogging(Profile*) OVERRIDE {}
350 #endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_