1 // Copyright (c) 2011 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 #pragma once 10 11 #include <vector> 12 13 #include "base/basictypes.h" 14 #include "base/file_path.h" 15 #include "base/hash_tables.h" 16 #include "base/memory/linked_ptr.h" 17 #include "base/memory/scoped_ptr.h" 18 #include "base/message_loop.h" 19 #include "base/threading/non_thread_safe.h" 20 #include "chrome/browser/profiles/profile.h" 21 #include "content/common/notification_observer.h" 22 #include "content/common/notification_registrar.h" 23 #include "ui/base/system_monitor/system_monitor.h" 24 25 class FilePath; 26 27 class ProfileManager : public base::NonThreadSafe, 28 public ui::SystemMonitor::PowerObserver, 29 public NotificationObserver, 30 public Profile::Delegate { 31 public: 32 class Observer { 33 public: 34 // This method is called when profile is ready. If profile creation has been 35 // failed, method is called with |profile| equals to NULL. 36 virtual void OnProfileCreated(Profile* profile) = 0; 37 }; 38 39 ProfileManager(); 40 virtual ~ProfileManager(); 41 42 // Invokes ShutdownSessionService() on all profiles. 43 static void ShutdownSessionServices(); 44 45 // Returns the default profile. This adds the profile to the 46 // ProfileManager if it doesn't already exist. This method returns NULL if 47 // the profile doesn't exist and we can't create it. 48 // The profile used can be overridden by using --login-profile on cros. 49 Profile* GetDefaultProfile(const FilePath& user_data_dir); 50 51 // Same as instance method but provides the default user_data_dir as well. 52 static Profile* GetDefaultProfile(); 53 54 // Returns a profile for a specific profile directory within the user data 55 // dir. This will return an existing profile it had already been created, 56 // otherwise it will create and manage it. 57 Profile* GetProfile(const FilePath& profile_dir); 58 59 // Explicit asynchronous creation of the profile. |observer| is called 60 // when profile is created. If profile has already been created, observer 61 // is called immediately. Should be called on the UI thread. 62 void CreateProfileAsync(const FilePath& user_data_dir, 63 Observer* observer); 64 65 // Initiates default profile creation. If default profile has already been 66 // created, observer is called immediately. Should be called on the UI thread. 67 static void CreateDefaultProfileAsync(Observer* observer); 68 69 // Returns the profile with the given |profile_id| or NULL if no such profile 70 // exists. 71 Profile* GetProfileWithId(ProfileId profile_id); 72 73 // Returns true if the profile pointer is known to point to an existing 74 // profile. 75 bool IsValidProfile(Profile* profile); 76 77 // Returns the directory where the currently active profile is 78 // stored, relative to the user data directory currently in use.. 79 FilePath GetCurrentProfileDir(); 80 81 // Returns created profiles. Note, profiles order is NOT guaranteed to be 82 // related with the creation order. 83 std::vector<Profile*> GetLoadedProfiles() const; 84 85 // PowerObserver notifications 86 virtual void OnSuspend(); 87 virtual void OnResume(); 88 89 // NotificationObserver implementation. 90 virtual void Observe(NotificationType type, 91 const NotificationSource& source, 92 const NotificationDetails& details); 93 94 // ------------------ static utility functions ------------------- 95 96 // Returns the path to the default profile directory, based on the given 97 // user data directory. 98 static FilePath GetDefaultProfileDir(const FilePath& user_data_dir); 99 100 // Returns the path to the preferences file given the user profile directory. 101 static FilePath GetProfilePrefsPath(const FilePath& profile_dir); 102 103 // If a profile with the given path is currently managed by this object, 104 // return a pointer to the corresponding Profile object; 105 // otherwise return NULL. 106 Profile* GetProfileByPath(const FilePath& path) const; 107 108 // Profile::Delegate implementation: 109 virtual void OnProfileCreated(Profile* profile, bool success); 110 111 protected: 112 // Does final initial actions. 113 virtual void DoFinalInit(Profile* profile); 114 115 private: 116 friend class ExtensionEventRouterForwarderTest; 117 118 // This struct contains information about profiles which are being loaded or 119 // were loaded. 120 struct ProfileInfo { 121 ProfileInfo(Profile* profile, bool created) 122 : profile(profile), created(created) { 123 } 124 125 ~ProfileInfo() {} 126 127 scoped_ptr<Profile> profile; 128 // Whether profile has been fully loaded (created and initialized). 129 bool created; 130 // List of observers which should be notified when profile initialization is 131 // done. Note, when profile is fully loaded this vector will be empty. 132 std::vector<Observer*> observers; 133 134 private: 135 DISALLOW_COPY_AND_ASSIGN(ProfileInfo); 136 }; 137 138 // Adds a pre-existing Profile object to the set managed by this 139 // ProfileManager. This ProfileManager takes ownership of the Profile. 140 // The Profile should not already be managed by this ProfileManager. 141 // Returns true if the profile was added, false otherwise. 142 bool AddProfile(Profile* profile); 143 144 // Registers profile with given info. Returns pointer to created ProfileInfo 145 // entry. 146 ProfileInfo* RegisterProfile(Profile* profile, bool created); 147 148 NotificationRegistrar registrar_; 149 150 // Indicates that a user has logged in and that the profile specified 151 // in the --login-profile command line argument should be used as the 152 // default. 153 bool logged_in_; 154 155 // Maps profile path to ProfileInfo (if profile has been created). Use 156 // RegisterProfile() to add into this map. 157 typedef std::map<FilePath, linked_ptr<ProfileInfo> > ProfilesInfoMap; 158 ProfilesInfoMap profiles_info_; 159 160 DISALLOW_COPY_AND_ASSIGN(ProfileManager); 161 }; 162 163 // Same as the ProfileManager, but doesn't initialize some services of the 164 // profile. This one is useful in unittests. 165 class ProfileManagerWithoutInit : public ProfileManager { 166 protected: 167 virtual void DoFinalInit(Profile*) {} 168 }; 169 170 #endif // CHROME_BROWSER_PROFILES_PROFILE_MANAGER_H_ 171