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 gathers state related to a single user profile. 6 7 #ifndef CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ 8 #define CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ 9 10 #include <string> 11 12 #include "base/files/file_path.h" 13 #include "base/gtest_prod_util.h" 14 #include "base/memory/ref_counted.h" 15 #include "base/memory/scoped_ptr.h" 16 #include "base/prefs/pref_change_registrar.h" 17 #include "base/timer/timer.h" 18 #include "chrome/browser/profiles/profile.h" 19 #include "chrome/browser/profiles/profile_impl_io_data.h" 20 #include "content/public/browser/content_browser_client.h" 21 #include "content/public/browser/host_zoom_map.h" 22 23 class NetPrefObserver; 24 class PrefService; 25 class PrefServiceSyncable; 26 class SSLConfigServiceManager; 27 28 #if defined(OS_CHROMEOS) 29 namespace chromeos { 30 class EnterpriseExtensionObserver; 31 class LocaleChangeGuard; 32 class Preferences; 33 } 34 #endif 35 36 namespace base { 37 class SequencedTaskRunner; 38 } 39 40 namespace extensions { 41 class ExtensionSystem; 42 } 43 44 namespace policy { 45 class CloudPolicyManager; 46 class ProfilePolicyConnector; 47 } 48 49 namespace user_prefs { 50 class refRegistrySyncable; 51 } 52 53 // The default profile implementation. 54 class ProfileImpl : public Profile { 55 public: 56 // Value written to prefs when the exit type is EXIT_NORMAL. Public for tests. 57 static const char* const kPrefExitTypeNormal; 58 59 virtual ~ProfileImpl(); 60 61 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 62 63 // content::BrowserContext implementation: 64 virtual base::FilePath GetPath() const OVERRIDE; 65 virtual content::DownloadManagerDelegate* 66 GetDownloadManagerDelegate() OVERRIDE; 67 virtual net::URLRequestContextGetter* GetRequestContext() OVERRIDE; 68 virtual net::URLRequestContextGetter* GetRequestContextForRenderProcess( 69 int renderer_child_id) OVERRIDE; 70 virtual net::URLRequestContextGetter* GetMediaRequestContext() OVERRIDE; 71 virtual net::URLRequestContextGetter* GetMediaRequestContextForRenderProcess( 72 int renderer_child_id) OVERRIDE; 73 virtual net::URLRequestContextGetter* 74 GetMediaRequestContextForStoragePartition( 75 const base::FilePath& partition_path, 76 bool in_memory) OVERRIDE; 77 virtual void RequestMIDISysExPermission( 78 int render_process_id, 79 int render_view_id, 80 const GURL& requesting_frame, 81 const MIDISysExPermissionCallback& callback) OVERRIDE; 82 virtual content::ResourceContext* GetResourceContext() OVERRIDE; 83 virtual content::GeolocationPermissionContext* 84 GetGeolocationPermissionContext() OVERRIDE; 85 virtual quota::SpecialStoragePolicy* GetSpecialStoragePolicy() OVERRIDE; 86 87 // Profile implementation: 88 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() OVERRIDE; 89 // Note that this implementation returns the Google-services username, if any, 90 // not the Chrome user's display name. 91 virtual std::string GetProfileName() OVERRIDE; 92 virtual bool IsOffTheRecord() const OVERRIDE; 93 virtual Profile* GetOffTheRecordProfile() OVERRIDE; 94 virtual void DestroyOffTheRecordProfile() OVERRIDE; 95 virtual bool HasOffTheRecordProfile() OVERRIDE; 96 virtual Profile* GetOriginalProfile() OVERRIDE; 97 virtual bool IsManaged() OVERRIDE; 98 virtual history::TopSites* GetTopSites() OVERRIDE; 99 virtual history::TopSites* GetTopSitesWithoutCreating() OVERRIDE; 100 virtual ExtensionService* GetExtensionService() OVERRIDE; 101 virtual ExtensionSpecialStoragePolicy* 102 GetExtensionSpecialStoragePolicy() OVERRIDE; 103 virtual PrefService* GetPrefs() OVERRIDE; 104 virtual PrefService* GetOffTheRecordPrefs() OVERRIDE; 105 virtual net::URLRequestContextGetter* 106 GetRequestContextForExtensions() OVERRIDE; 107 virtual net::SSLConfigService* GetSSLConfigService() OVERRIDE; 108 virtual HostContentSettingsMap* GetHostContentSettingsMap() OVERRIDE; 109 virtual bool IsSameProfile(Profile* profile) OVERRIDE; 110 virtual base::Time GetStartTime() const OVERRIDE; 111 virtual net::URLRequestContextGetter* CreateRequestContext( 112 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; 113 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition( 114 const base::FilePath& partition_path, 115 bool in_memory, 116 content::ProtocolHandlerMap* protocol_handlers) OVERRIDE; 117 virtual base::FilePath last_selected_directory() OVERRIDE; 118 virtual void set_last_selected_directory(const base::FilePath& path) OVERRIDE; 119 virtual chrome_browser_net::Predictor* GetNetworkPredictor() OVERRIDE; 120 virtual void ClearNetworkingHistorySince( 121 base::Time time, 122 const base::Closure& completion) OVERRIDE; 123 virtual GURL GetHomePage() OVERRIDE; 124 virtual bool WasCreatedByVersionOrLater(const std::string& version) OVERRIDE; 125 virtual void SetExitType(ExitType exit_type) OVERRIDE; 126 virtual ExitType GetLastSessionExitType() OVERRIDE; 127 128 #if defined(OS_CHROMEOS) 129 virtual void ChangeAppLocale(const std::string& locale, 130 AppLocaleChangedVia) OVERRIDE; 131 virtual void OnLogin() OVERRIDE; 132 virtual void SetupChromeOSEnterpriseExtensionObserver() OVERRIDE; 133 virtual void InitChromeOSPreferences() OVERRIDE; 134 #endif // defined(OS_CHROMEOS) 135 136 virtual PrefProxyConfigTracker* GetProxyConfigTracker() OVERRIDE; 137 138 private: 139 friend class Profile; 140 friend class BetterSessionRestoreCrashTest; 141 FRIEND_TEST_ALL_PREFIXES(StartupBrowserCreatorTest, 142 ProfilesLaunchedAfterCrash); 143 FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest, ProfileReadmeCreated); 144 FRIEND_TEST_ALL_PREFIXES(ProfileBrowserTest, 145 ProfileDeletedBeforeReadmeCreated); 146 147 // Delay, in milliseconds, before README file is created for a new profile. 148 // This is non-const for testing purposes. 149 static int create_readme_delay_ms; 150 151 ProfileImpl(const base::FilePath& path, 152 Delegate* delegate, 153 CreateMode create_mode, 154 base::SequencedTaskRunner* sequenced_task_runner); 155 156 // Does final initialization. Should be called after prefs were loaded. 157 void DoFinalInit(); 158 159 void InitHostZoomMap(); 160 161 void OnDefaultZoomLevelChanged(); 162 void OnZoomLevelChanged( 163 const content::HostZoomMap::ZoomLevelChange& change); 164 165 void OnInitializationCompleted(PrefService* pref_service, 166 bool succeeded); 167 168 // Does final prefs initialization and calls Init(). 169 void OnPrefsLoaded(bool success); 170 171 base::FilePath GetPrefFilePath(); 172 173 #if defined(ENABLE_SESSION_SERVICE) 174 void StopCreateSessionServiceTimer(); 175 176 void EnsureSessionServiceCreated(); 177 #endif 178 179 180 void EnsureRequestContextCreated() { 181 GetRequestContext(); 182 } 183 184 void UpdateProfileUserNameCache(); 185 186 // Updates the ProfileInfoCache with data from this profile. 187 void UpdateProfileNameCache(); 188 void UpdateProfileAvatarCache(); 189 190 void GetCacheParameters(bool is_media_context, 191 base::FilePath* cache_path, 192 int* max_size); 193 194 PrefProxyConfigTracker* CreateProxyConfigTracker(); 195 196 content::HostZoomMap::ZoomLevelChangedCallback zoom_callback_; 197 PrefChangeRegistrar pref_change_registrar_; 198 199 base::FilePath path_; 200 base::FilePath base_cache_path_; 201 202 // !!! BIG HONKING WARNING !!! 203 // The order of the members below is important. Do not change it unless 204 // you know what you're doing. Also, if adding a new member here make sure 205 // that the declaration occurs AFTER things it depends on as destruction 206 // happens in reverse order of declaration. 207 208 // TODO(mnissler, joaodasilva): The |profile_policy_connector_| provides the 209 // PolicyService that the |prefs_| depend on, and must outlive |prefs_|. 210 // This can be removed once |prefs_| becomes a BrowserContextKeyedService too. 211 // |profile_policy_connector_| in turn depends on |cloud_policy_manager_|. 212 #if defined(ENABLE_CONFIGURATION_POLICY) 213 scoped_ptr<policy::CloudPolicyManager> cloud_policy_manager_; 214 #endif 215 scoped_ptr<policy::ProfilePolicyConnector> profile_policy_connector_; 216 217 // Keep |prefs_| on top for destruction order because |extension_prefs_|, 218 // |net_pref_observer_|, |io_data_| and others store pointers to |prefs_| and 219 // shall be destructed first. 220 scoped_refptr<user_prefs::PrefRegistrySyncable> pref_registry_; 221 scoped_ptr<PrefServiceSyncable> prefs_; 222 scoped_ptr<PrefServiceSyncable> otr_prefs_; 223 ProfileImplIOData::Handle io_data_; 224 scoped_refptr<ExtensionSpecialStoragePolicy> 225 extension_special_storage_policy_; 226 scoped_ptr<NetPrefObserver> net_pref_observer_; 227 scoped_ptr<SSLConfigServiceManager> ssl_config_service_manager_; 228 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 229 scoped_refptr<history::ShortcutsBackend> shortcuts_backend_; 230 231 // Exit type the last time the profile was opened. This is set only once from 232 // prefs. 233 ExitType last_session_exit_type_; 234 235 #if defined(ENABLE_SESSION_SERVICE) 236 base::OneShotTimer<ProfileImpl> create_session_service_timer_; 237 #endif 238 239 scoped_ptr<Profile> off_the_record_profile_; 240 241 // See GetStartTime for details. 242 base::Time start_time_; 243 244 scoped_refptr<history::TopSites> top_sites_; // For history and thumbnails. 245 246 #if defined(OS_CHROMEOS) 247 scoped_ptr<chromeos::Preferences> chromeos_preferences_; 248 249 scoped_ptr<chromeos::EnterpriseExtensionObserver> 250 chromeos_enterprise_extension_observer_; 251 252 scoped_ptr<chromeos::LocaleChangeGuard> locale_change_guard_; 253 #endif 254 255 scoped_ptr<PrefProxyConfigTracker> pref_proxy_config_tracker_; 256 257 // STOP!!!! DO NOT ADD ANY MORE ITEMS HERE!!!! 258 // 259 // Instead, make your Service/Manager/whatever object you're hanging off the 260 // Profile use our new BrowserContextKeyedServiceFactory system instead. 261 // You can find the design document here: 262 // 263 // https://sites.google.com/a/chromium.org/dev/developers/design-documents/profile-architecture 264 // 265 // and you can read the raw headers here: 266 // 267 // components/browser_context_keyed_service/browser_context_dependency_manager.{h,cc} 268 // components/browser_context_keyed_service/browser_context_keyed_service.h 269 // components/browser_context_keyed_service/browser_context_keyed_service_factory.{h,cc} 270 271 Profile::Delegate* delegate_; 272 273 chrome_browser_net::Predictor* predictor_; 274 275 DISALLOW_COPY_AND_ASSIGN(ProfileImpl); 276 }; 277 278 #endif // CHROME_BROWSER_PROFILES_PROFILE_IMPL_H_ 279