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. 4 5 // This class keeps track of the currently-active profiles in the runtime. 6 7 #ifndef CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 8 #define CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 9 10 #include <list> 11 #include <vector> 12 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" 26 27 class CommandLine; 28 class NewProfileLauncher; 29 class ProfileInfoCache; 30 31 class ProfileManager : public base::NonThreadSafe, 32 public content::NotificationObserver, 33 public Profile::Delegate { 34 public: 35 typedef base::Callback<void(Profile*, Profile::CreateStatus)> CreateCallback; 36 37 explicit ProfileManager(const base::FilePath& user_data_dir); 38 virtual ~ProfileManager(); 39 40 #if defined(ENABLE_SESSION_SERVICE) 41 // Invokes SessionServiceFactory::ShutdownForProfile() for all profiles. 42 static void ShutdownSessionServices(); 43 #endif 44 45 // Physically remove deleted profile directories from disk. 46 static void NukeDeletedProfilesFromDisk(); 47 48 // Only to be used in ChromeOS: 49 // This is a temporary solution to get rid of the DEPRICATED GetDefaultProfile 50 // functions below. Going forward we will do architectural changes to reduce 51 // the Profile to get the browser context instead and by moving it into ash, 52 // but for the short time fix we add these two functions here, enabling us to 53 // get rid of GetDefaultProfile as a first step. 54 // TODO(skuhne): Move into ash's new user management function. 55 56 // Get the profile for the user which created the current session. 57 // Note that in case of a guest account this will return a 'suitable' profile. 58 static Profile* GetPrimaryUserProfile(); 59 60 // Get the profile for the currently active user. 61 // Note that in case of a guest account this will return a 'suitable' profile. 62 static Profile* GetActiveUserProfile(); 63 64 // Same as GetPrimaryUserProfile. Do not use! 65 static Profile* GetPrimaryUserProfileOrOffTheRecord(); 66 67 // Same as GetPrimaryUserProfile. Do not use! 68 static Profile* GetActiveUserProfileOrOffTheRecord(); 69 70 // The following DEPRECATED functions should be removed: crbug.com/83792. 71 72 // DEPRECATED: DO NOT USE unless in ChromeOS. 73 // Returns the default profile. This adds the profile to the 74 // ProfileManager if it doesn't already exist. This method returns NULL if 75 // the profile doesn't exist and we can't create it. 76 // The profile used can be overridden by using --login-profile on cros. 77 Profile* GetDefaultProfile(const base::FilePath& user_data_dir); 78 79 // DEPRECATED: Temporary measure to ensure that GetDefaultProfile() is not 80 // called before CreateProfile() is called in chrome_browser_main.cc. 81 // If GetDefaultProfile() or GetDefaultProfileOrOffTheRecord() is called 82 // before this, a CHECK will be triggered. 83 static void AllowGetDefaultProfile(); 84 static bool IsGetDefaultProfileAllowed(); 85 86 // DEPRECATED: DO NOT USE unless in ChromeOS. 87 // Same as instance method but provides the default user_data_dir as well. 88 // Note that in case of a guest account this will return a 'suitable' profile. 89 static Profile* GetDefaultProfile(); 90 91 // DEPRECATED: DO NOT USE unless in ChromeOS. 92 // Same as GetDefaultProfile(). Do not use. 93 static Profile* GetDefaultProfileOrOffTheRecord(); 94 95 // Returns a profile for a specific profile directory within the user data 96 // dir. This will return an existing profile it had already been created, 97 // otherwise it will create and manage it. 98 Profile* GetProfile(const base::FilePath& profile_dir); 99 100 // Returns total number of profiles available on this machine. 101 size_t GetNumberOfProfiles(); 102 103 // Explicit asynchronous creation of a profile located at |profile_path|. 104 // If the profile has already been created then callback is called 105 // immediately. Should be called on the UI thread. 106 void CreateProfileAsync(const base::FilePath& profile_path, 107 const CreateCallback& callback, 108 const base::string16& name, 109 const base::string16& icon_url, 110 const std::string& managed_user_id); 111 112 // Returns true if the profile pointer is known to point to an existing 113 // profile. 114 bool IsValidProfile(Profile* profile); 115 116 // Returns the directory where the first created profile is stored, 117 // relative to the user data directory currently in use.. 118 base::FilePath GetInitialProfileDir(); 119 120 // Get the Profile last used (the Profile to which owns the most recently 121 // focused window) with this Chrome build. If no signed profile has been 122 // stored in Local State, hand back the Default profile. 123 Profile* GetLastUsedProfile(const base::FilePath& user_data_dir); 124 125 // Same as instance method but provides the default user_data_dir as well. 126 // If the Profile is going to be used to open a new window then consider using 127 // GetLastUsedProfileAllowedByPolicy() instead. 128 static Profile* GetLastUsedProfile(); 129 130 // Same as GetLastUsedProfile() but returns the incognito Profile if 131 // incognito mode is forced. This should be used if the last used Profile 132 // will be used to open new browser windows. 133 static Profile* GetLastUsedProfileAllowedByPolicy(); 134 135 // Get the path of the last used profile, or if that's undefined, the default 136 // profile. 137 base::FilePath GetLastUsedProfileDir(const base::FilePath& user_data_dir); 138 139 // Get the Profiles which are currently open, i.e., have open browsers, or 140 // were open the last time Chrome was running. The Profiles appear in the 141 // order they were opened. The last used profile will be on the list, but its 142 // index on the list will depend on when it was opened (it is not necessarily 143 // the last one). 144 std::vector<Profile*> GetLastOpenedProfiles( 145 const base::FilePath& user_data_dir); 146 147 // Same as instance method but provides the default user_data_dir as well. 148 static std::vector<Profile*> GetLastOpenedProfiles(); 149 150 // Returns created profiles. Note, profiles order is NOT guaranteed to be 151 // related with the creation order. 152 std::vector<Profile*> GetLoadedProfiles() const; 153 154 // content::NotificationObserver implementation. 155 virtual void Observe(int type, 156 const content::NotificationSource& source, 157 const content::NotificationDetails& details) OVERRIDE; 158 159 // If a profile with the given path is currently managed by this object, 160 // return a pointer to the corresponding Profile object; 161 // otherwise return NULL. 162 Profile* GetProfileByPath(const base::FilePath& path) const; 163 164 // Profile::Delegate implementation: 165 virtual void OnProfileCreated(Profile* profile, 166 bool success, 167 bool is_new_profile) OVERRIDE; 168 169 // Add or remove a profile launcher to/from the list of launchers waiting for 170 // new profiles to be created from the multi-profile menu. 171 void AddProfileLauncher(NewProfileLauncher* profile_launcher); 172 void RemoveProfileLauncher(NewProfileLauncher* profile_launcher); 173 174 // Creates a new profile in the next available multiprofile directory. 175 // Directories are named "profile_1", "profile_2", etc., in sequence of 176 // creation. (Because directories can be removed, however, it may be the case 177 // that at some point the list of numbered profiles is not continuous.) 178 // |callback| may be invoked multiple times (for CREATE_STATUS_INITIALIZED 179 // and CREATE_STATUS_CREATED) so binding parameters with bind::Passed() is 180 // prohibited. Returns the file path to the profile that will be created 181 // asynchronously. 182 static base::FilePath CreateMultiProfileAsync( 183 const base::string16& name, 184 const base::string16& icon_url, 185 const CreateCallback& callback, 186 const std::string& managed_user_id); 187 188 // Returns the full path to be used for guest profiles. 189 static base::FilePath GetGuestProfilePath(); 190 191 // Get the path of the next profile directory and increment the internal 192 // count. 193 // Lack of side effects: 194 // This function doesn't actually create the directory or touch the file 195 // system. 196 base::FilePath GenerateNextProfileDirectoryPath(); 197 198 // Returns a ProfileInfoCache object which can be used to get information 199 // about profiles without having to load them from disk. 200 ProfileInfoCache& GetProfileInfoCache(); 201 202 // Returns a ProfileShortcut Manager that enables the caller to create 203 // profile specfic desktop shortcuts. 204 ProfileShortcutManager* profile_shortcut_manager(); 205 206 // Schedules the profile at the given path to be deleted on shutdown. If we're 207 // deleting the last profile, a new one will be created in its place, and in 208 // that case the callback will be called when profile creation is complete. 209 void ScheduleProfileForDeletion(const base::FilePath& profile_dir, 210 const CreateCallback& callback); 211 212 // Called on start-up if there are any stale ephemeral profiles to be deleted. 213 // This can be the case if the browser has crashed and the clean-up code had 214 // no chance to run then. 215 static void CleanUpStaleProfiles( 216 const std::vector<base::FilePath>& profile_paths); 217 218 // Autoloads profiles if they are running background apps. 219 void AutoloadProfiles(); 220 221 // Sign-Out a profile against use until re-authentication. 222 void SignOutProfile(Profile* profile); 223 224 // Initializes user prefs of |profile|. This includes profile name and 225 // avatar values. 226 void InitProfileUserPrefs(Profile* profile); 227 228 // Register and add testing profile to the ProfileManager. Use ONLY in tests. 229 // This allows the creation of Profiles outside of the standard creation path 230 // for testing. If |addToCache|, adds to ProfileInfoCache as well. 231 // If |start_deferred_task_runners|, starts the deferred task runners. 232 // Use ONLY in tests. 233 void RegisterTestingProfile(Profile* profile, 234 bool addToCache, 235 bool start_deferred_task_runners); 236 237 const base::FilePath& user_data_dir() const { return user_data_dir_; } 238 239 // For ChromeOS, determines if the user has logged in to a real profile. 240 bool IsLoggedIn() const { return logged_in_; } 241 242 protected: 243 // Does final initial actions. 244 virtual void DoFinalInit(Profile* profile, bool go_off_the_record); 245 virtual void DoFinalInitForServices(Profile* profile, bool go_off_the_record); 246 virtual void DoFinalInitLogging(Profile* profile); 247 248 // Creates a new profile by calling into the profile's profile creation 249 // method. Virtual so that unittests can return a TestingProfile instead 250 // of the Profile's result. 251 virtual Profile* CreateProfileHelper(const base::FilePath& path); 252 253 // Creates a new profile asynchronously by calling into the profile's 254 // asynchronous profile creation method. Virtual so that unittests can return 255 // a TestingProfile instead of the Profile's result. 256 virtual Profile* CreateProfileAsyncHelper(const base::FilePath& path, 257 Delegate* delegate); 258 259 private: 260 friend class TestingProfileManager; 261 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, DeleteAllProfiles); 262 FRIEND_TEST_ALL_PREFIXES(ProfileManagerBrowserTest, SwitchToProfile); 263 264 // This struct contains information about profiles which are being loaded or 265 // were loaded. 266 struct ProfileInfo { 267 ProfileInfo(Profile* profile, bool created); 268 269 ~ProfileInfo(); 270 271 scoped_ptr<Profile> profile; 272 // Whether profile has been fully loaded (created and initialized). 273 bool created; 274 // Whether or not this profile should have a shortcut. 275 bool create_shortcut; 276 // List of callbacks to run when profile initialization is done. Note, when 277 // profile is fully loaded this vector will be empty. 278 std::vector<CreateCallback> callbacks; 279 280 private: 281 DISALLOW_COPY_AND_ASSIGN(ProfileInfo); 282 }; 283 284 // Adds a pre-existing Profile object to the set managed by this 285 // ProfileManager. This ProfileManager takes ownership of the Profile. 286 // The Profile should not already be managed by this ProfileManager. 287 // Returns true if the profile was added, false otherwise. 288 bool AddProfile(Profile* profile); 289 290 // Schedules the profile at the given path to be deleted on shutdown. 291 void FinishDeletingProfile(const base::FilePath& profile_dir); 292 293 // Registers profile with given info. Returns pointer to created ProfileInfo 294 // entry. 295 ProfileInfo* RegisterProfile(Profile* profile, bool created); 296 297 // Returns ProfileInfo associated with given |path|, registred earlier with 298 // RegisterProfile. 299 ProfileInfo* GetProfileInfoByPath(const base::FilePath& path) const; 300 301 typedef std::pair<base::FilePath, base::string16> ProfilePathAndName; 302 typedef std::vector<ProfilePathAndName> ProfilePathAndNames; 303 ProfilePathAndNames GetSortedProfilesFromDirectoryMap(); 304 305 static bool CompareProfilePathAndName( 306 const ProfileManager::ProfilePathAndName& pair1, 307 const ProfileManager::ProfilePathAndName& pair2); 308 309 // Adds |profile| to the profile info cache if it hasn't been added yet. 310 void AddProfileToCache(Profile* profile); 311 312 // Apply settings for (desktop) Guest User profile. 313 void SetGuestProfilePrefs(Profile* profile); 314 315 // For ChromeOS, determines if profile should be otr. 316 bool ShouldGoOffTheRecord(Profile* profile); 317 318 void RunCallbacks(const std::vector<CreateCallback>& callbacks, 319 Profile* profile, 320 Profile::CreateStatus status); 321 322 // If the |loaded_profile| has been loaded succesfully (according to |status|) 323 // and isn't already scheduled for deletion, then finishes adding 324 // |profile_to_delete_dir| to the queue of profiles to be deleted, and updates 325 // the kProfileLastUsed preference based on |last_non_managed_profile_path|. 326 void OnNewActiveProfileLoaded( 327 const base::FilePath& profile_to_delete_path, 328 const base::FilePath& last_non_managed_profile_path, 329 const CreateCallback& original_callback, 330 Profile* loaded_profile, 331 Profile::CreateStatus status); 332 333 content::NotificationRegistrar registrar_; 334 335 // The path to the user data directory (DIR_USER_DATA). 336 const base::FilePath user_data_dir_; 337 338 // Indicates that a user has logged in and that the profile specified 339 // in the --login-profile command line argument should be used as the 340 // default. 341 bool logged_in_; 342 343 // Maps profile path to ProfileInfo (if profile has been created). Use 344 // RegisterProfile() to add into this map. This map owns all loaded profile 345 // objects in a running instance of Chrome. 346 typedef std::map<base::FilePath, linked_ptr<ProfileInfo> > ProfilesInfoMap; 347 ProfilesInfoMap profiles_info_; 348 349 // Object to cache various information about profiles. Contains information 350 // about every profile which has been created for this instance of Chrome, 351 // if it has not been explicitly deleted. 352 scoped_ptr<ProfileInfoCache> profile_info_cache_; 353 354 // Manages the process of creating, deleteing and updating Desktop shortcuts. 355 scoped_ptr<ProfileShortcutManager> profile_shortcut_manager_; 356 357 #if !defined(OS_ANDROID) && !defined(OS_IOS) 358 class BrowserListObserver : public chrome::BrowserListObserver { 359 public: 360 explicit BrowserListObserver(ProfileManager* manager); 361 virtual ~BrowserListObserver(); 362 363 // chrome::BrowserListObserver implementation. 364 virtual void OnBrowserAdded(Browser* browser) OVERRIDE; 365 virtual void OnBrowserRemoved(Browser* browser) OVERRIDE; 366 virtual void OnBrowserSetLastActive(Browser* browser) OVERRIDE; 367 368 private: 369 ProfileManager* profile_manager_; 370 DISALLOW_COPY_AND_ASSIGN(BrowserListObserver); 371 }; 372 373 BrowserListObserver browser_list_observer_; 374 #endif // !defined(OS_ANDROID) && !defined(OS_IOS) 375 376 // For keeping track of the last active profiles. 377 std::map<Profile*, int> browser_counts_; 378 // On startup we launch the active profiles in the order they became active 379 // during the last run. This is why they are kept in a list, not in a set. 380 std::vector<Profile*> active_profiles_; 381 bool closing_all_browsers_; 382 383 DISALLOW_COPY_AND_ASSIGN(ProfileManager); 384 }; 385 386 // Same as the ProfileManager, but doesn't initialize some services of the 387 // profile. This one is useful in unittests. 388 class ProfileManagerWithoutInit : public ProfileManager { 389 public: 390 explicit ProfileManagerWithoutInit(const base::FilePath& user_data_dir); 391 392 protected: 393 virtual void DoFinalInitForServices(Profile*, bool) OVERRIDE {} 394 virtual void DoFinalInitLogging(Profile*) OVERRIDE {} 395 }; 396 397 #endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 398