profile.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_H_ 8#define CHROME_BROWSER_PROFILES_PROFILE_H_ 9 10#include <string> 11 12#include "base/basictypes.h" 13#include "base/hash_tables.h" 14#include "base/logging.h" 15#include "chrome/browser/net/pref_proxy_config_tracker.h" 16#include "chrome/browser/ui/webui/chrome_url_data_manager_factory.h" 17#include "content/public/browser/browser_context.h" 18 19class ChromeAppCacheService; 20class ChromeURLDataManager; 21class ExtensionService; 22class ExtensionSpecialStoragePolicy; 23class FaviconService; 24class GAIAInfoUpdateService; 25class HostContentSettingsMap; 26class PasswordStore; 27class PrefService; 28class PromoCounter; 29class ProtocolHandlerRegistry; 30class TestingProfile; 31class WebDataService; 32 33namespace android { 34class TabContentsProvider; 35} 36 37namespace base { 38class SequencedTaskRunner; 39class Time; 40} 41 42namespace chrome_browser_net { 43class Predictor; 44} 45 46namespace chromeos { 47class LibCrosServiceLibraryImpl; 48class ResetDefaultProxyConfigServiceTask; 49} 50 51namespace content { 52class WebUI; 53} 54 55namespace fileapi { 56class FileSystemContext; 57} 58 59namespace history { 60class ShortcutsBackend; 61class TopSites; 62} 63 64namespace net { 65class SSLConfigService; 66} 67 68namespace policy { 69class ManagedModePolicyProvider; 70class PolicyService; 71class UserCloudPolicyManager; 72} 73 74class Profile : public content::BrowserContext { 75 public: 76 // Profile services are accessed with the following parameter. This parameter 77 // defines what the caller plans to do with the service. 78 // The caller is responsible for not performing any operation that would 79 // result in persistent implicit records while using an OffTheRecord profile. 80 // This flag allows the profile to perform an additional check. 81 // 82 // It also gives us an opportunity to perform further checks in the future. We 83 // could, for example, return an history service that only allow some specific 84 // methods. 85 enum ServiceAccessType { 86 // The caller plans to perform a read or write that takes place as a result 87 // of the user input. Use this flag when the operation you are doing can be 88 // performed while incognito. (ex: creating a bookmark) 89 // 90 // Since EXPLICIT_ACCESS means "as a result of a user action", this request 91 // always succeeds. 92 EXPLICIT_ACCESS, 93 94 // The caller plans to call a method that will permanently change some data 95 // in the profile, as part of Chrome's implicit data logging. Use this flag 96 // when you are about to perform an operation which is incompatible with the 97 // incognito mode. 98 IMPLICIT_ACCESS 99 }; 100 101 enum CreateStatus { 102 // Profile services were not created. 103 CREATE_STATUS_FAIL, 104 // Profile created but before initializing extensions and promo resources. 105 CREATE_STATUS_CREATED, 106 // Profile is created, extensions and promo resources are initialized. 107 CREATE_STATUS_INITIALIZED, 108 }; 109 110 enum CreateMode { 111 CREATE_MODE_SYNCHRONOUS, 112 CREATE_MODE_ASYNCHRONOUS 113 }; 114 115 enum ExitType { 116 // A normal shutdown. The user clicked exit/closed last window of the 117 // profile. 118 EXIT_NORMAL, 119 120 // The exit was the result of the system shutting down. 121 EXIT_SESSION_ENDED, 122 123 EXIT_CRASHED, 124 }; 125 126 class Delegate { 127 public: 128 // Called when creation of the profile is finished. 129 virtual void OnProfileCreated(Profile* profile, 130 bool success, 131 bool is_new_profile) = 0; 132 }; 133 134 // Key used to bind profile to the widget with which it is associated. 135 static const char* const kProfileKey; 136 137 Profile(); 138 virtual ~Profile() {} 139 140 // Profile prefs are registered as soon as the prefs are loaded for the first 141 // time. 142 static void RegisterUserPrefs(PrefService* prefs); 143 144 // Gets task runner for I/O operations associated with |profile|. 145 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForProfile( 146 Profile* profile); 147 148 // Create a new profile given a path. If |create_mode| is 149 // CREATE_MODE_ASYNCHRONOUS then the profile is initialized asynchronously. 150 static Profile* CreateProfile(const FilePath& path, 151 Delegate* delegate, 152 CreateMode create_mode); 153 154 // Returns the profile corresponding to the given browser context. 155 static Profile* FromBrowserContext(content::BrowserContext* browser_context); 156 157 // Returns the profile corresponding to the given WebUI. 158 static Profile* FromWebUI(content::WebUI* web_ui); 159 160 // content::BrowserContext implementation ------------------------------------ 161 162 // Typesafe upcast. 163 virtual TestingProfile* AsTestingProfile(); 164 165 // Returns sequenced task runner where browser context dependent I/O 166 // operations should be performed. 167 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() = 0; 168 169 // Returns the name associated with this profile. This name is displayed in 170 // the browser frame. 171 virtual std::string GetProfileName() = 0; 172 173 // Return the incognito version of this profile. The returned pointer 174 // is owned by the receiving profile. If the receiving profile is off the 175 // record, the same profile is returned. 176 // 177 // WARNING: This will create the OffTheRecord profile if it doesn't already 178 // exist. If this isn't what you want, you need to check 179 // HasOffTheRecordProfile() first. 180 virtual Profile* GetOffTheRecordProfile() = 0; 181 182 // Destroys the incognito profile. 183 virtual void DestroyOffTheRecordProfile() = 0; 184 185 // True if an incognito profile exists. 186 virtual bool HasOffTheRecordProfile() = 0; 187 188 // Return the original "recording" profile. This method returns this if the 189 // profile is not incognito. 190 virtual Profile* GetOriginalProfile() = 0; 191 192 // Returns a pointer to the TopSites (thumbnail manager) instance 193 // for this profile. 194 virtual history::TopSites* GetTopSites() = 0; 195 196 // Variant of GetTopSites that doesn't force creation. 197 virtual history::TopSites* GetTopSitesWithoutCreating() = 0; 198 199 // DEPRECATED. Instead, use ExtensionSystem::extension_service(). 200 // Retrieves a pointer to the ExtensionService associated with this 201 // profile. The ExtensionService is created at startup. 202 // TODO(yoz): remove this accessor (bug 104095). 203 virtual ExtensionService* GetExtensionService() = 0; 204 205 // Accessor. The instance is created upon first access. 206 virtual ExtensionSpecialStoragePolicy* 207 GetExtensionSpecialStoragePolicy() = 0; 208 209 // Accessor. The instance is created upon first access. 210 virtual GAIAInfoUpdateService* GetGAIAInfoUpdateService() = 0; 211 212 // Returns the UserCloudPolicyManager (if any) that handles this profile's 213 // connection to the cloud-based management service. 214 virtual policy::UserCloudPolicyManager* GetUserCloudPolicyManager() = 0; 215 216 // Returns the ManagedModePolicyProvider for this profile, if it exists. 217 virtual policy::ManagedModePolicyProvider* GetManagedModePolicyProvider() = 0; 218 219 // Returns the PolicyService that provides policies for this profile. 220 virtual policy::PolicyService* GetPolicyService() = 0; 221 222 // Retrieves a pointer to the PrefService that manages the preferences 223 // for this user profile. 224 virtual PrefService* GetPrefs() = 0; 225 226 // Retrieves a pointer to the PrefService that manages the preferences 227 // for OffTheRecord Profiles. This PrefService is lazily created the first 228 // time that this method is called. 229 virtual PrefService* GetOffTheRecordPrefs() = 0; 230 231 // Returns the main request context. 232 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 233 234 // Returns the request context used for extension-related requests. This 235 // is only used for a separate cookie store currently. 236 virtual net::URLRequestContextGetter* GetRequestContextForExtensions() = 0; 237 238 // Returns the request context used within |partition_id|. 239 virtual net::URLRequestContextGetter* GetRequestContextForStoragePartition( 240 const FilePath& partition_path, 241 bool in_memory) = 0; 242 243 // Returns the SSLConfigService for this profile. 244 virtual net::SSLConfigService* GetSSLConfigService() = 0; 245 246 // Returns the Hostname <-> Content settings map for this profile. 247 virtual HostContentSettingsMap* GetHostContentSettingsMap() = 0; 248 249 // Returns the ProtocolHandlerRegistry, creating if not yet created. 250 // TODO(smckay): replace this with access via ProtocolHandlerRegistryFactory. 251 virtual ProtocolHandlerRegistry* GetProtocolHandlerRegistry() = 0; 252 253 // Return whether 2 profiles are the same. 2 profiles are the same if they 254 // represent the same profile. This can happen if there is pointer equality 255 // or if one profile is the incognito version of another profile (or vice 256 // versa). 257 virtual bool IsSameProfile(Profile* profile) = 0; 258 259 // Returns the time the profile was started. This is not the time the profile 260 // was created, rather it is the time the user started chrome and logged into 261 // this profile. For the single profile case, this corresponds to the time 262 // the user started chrome. 263 virtual base::Time GetStartTime() const = 0; 264 265 // Start up service that gathers data from a promo resource feed. 266 virtual void InitPromoResources() = 0; 267 268 // Returns the last directory that was chosen for uploading or opening a file. 269 virtual FilePath last_selected_directory() = 0; 270 virtual void set_last_selected_directory(const FilePath& path) = 0; 271 272#if defined(OS_CHROMEOS) 273 enum AppLocaleChangedVia { 274 // Caused by chrome://settings change. 275 APP_LOCALE_CHANGED_VIA_SETTINGS, 276 // Locale has been reverted via LocaleChangeGuard. 277 APP_LOCALE_CHANGED_VIA_REVERT, 278 // From login screen. 279 APP_LOCALE_CHANGED_VIA_LOGIN, 280 // Source unknown. 281 APP_LOCALE_CHANGED_VIA_UNKNOWN 282 }; 283 284 // Changes application locale for a profile. 285 virtual void ChangeAppLocale( 286 const std::string& locale, AppLocaleChangedVia via) = 0; 287 288 // Called after login. 289 virtual void OnLogin() = 0; 290 291 // Creates ChromeOS's EnterpriseExtensionListener. 292 virtual void SetupChromeOSEnterpriseExtensionObserver() = 0; 293 294 // Initializes Chrome OS's preferences. 295 virtual void InitChromeOSPreferences() = 0; 296#endif // defined(OS_CHROMEOS) 297 298 // Returns the helper object that provides the proxy configuration service 299 // access to the the proxy configuration possibly defined by preferences. 300 virtual PrefProxyConfigTracker* GetProxyConfigTracker() = 0; 301 302 // Returns the Predictor object used for dns prefetch. 303 virtual chrome_browser_net::Predictor* GetNetworkPredictor() = 0; 304 305 // Deletes all network related data since |time|. It deletes transport 306 // security state since |time| and it also deletes HttpServerProperties data. 307 // Works asynchronously, however if the |completion| callback is non-null, it 308 // will be posted on the UI thread once the removal process completes. 309 // Be aware that theoretically it is possible that |completion| will be 310 // invoked after the Profile instance has been destroyed. 311 virtual void ClearNetworkingHistorySince(base::Time time, 312 const base::Closure& completion) = 0; 313 314 // Returns the home page for this profile. 315 virtual GURL GetHomePage() = 0; 316 317 // Returns whether or not the profile was created by a version of Chrome 318 // more recent (or equal to) the one specified. 319 virtual bool WasCreatedByVersionOrLater(const std::string& version) = 0; 320 321 std::string GetDebugName(); 322 323 // Returns whether it is a guest session. 324 static bool IsGuestSession(); 325 326 // Did the user restore the last session? This is set by SessionRestore. 327 void set_restored_last_session(bool restored_last_session) { 328 restored_last_session_ = restored_last_session; 329 } 330 bool restored_last_session() const { 331 return restored_last_session_; 332 } 333 334 // Sets the ExitType for the profile. This may be invoked multiple times 335 // during shutdown; only the first such change (the transition from 336 // EXIT_CRASHED to one of the other values) is written to prefs, any 337 // later calls are ignored. 338 // 339 // NOTE: this is invoked internally on a normal shutdown, but is public so 340 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION), 341 // or to handle backgrounding/foregrounding on mobile. 342 virtual void SetExitType(ExitType exit_type) = 0; 343 344 // Returns how the last session was shutdown. 345 virtual ExitType GetLastSessionExitType() = 0; 346 347 // Stop sending accessibility events until ResumeAccessibilityEvents(). 348 // Calls to Pause nest; no events will be sent until the number of 349 // Resume calls matches the number of Pause calls received. 350 void PauseAccessibilityEvents() { 351 accessibility_pause_level_++; 352 } 353 354 void ResumeAccessibilityEvents() { 355 DCHECK_GT(accessibility_pause_level_, 0); 356 accessibility_pause_level_--; 357 } 358 359 bool ShouldSendAccessibilityEvents() { 360 return 0 == accessibility_pause_level_; 361 } 362 363 // Checks whether sync is configurable by the user. Returns false if sync is 364 // disabled or controlled by configuration management. 365 bool IsSyncAccessible(); 366 367 // Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not 368 // already been sent. It is necessary because most Profiles are destroyed by 369 // ProfileDestroyer, but in tests, some are not. 370 void MaybeSendDestroyedNotification(); 371 372 // Creates an OffTheRecordProfile which points to this Profile. 373 Profile* CreateOffTheRecordProfile(); 374 375 protected: 376 // TODO(erg, willchan): Remove friendship once |ProfileIOData| is made into 377 // a |ProfileKeyedService|. 378 friend class ChromeURLDataManagerFactory; 379 friend class OffTheRecordProfileImpl; 380 381 // Returns a callback to a method returning a |ChromeURLDataManagerBackend|. 382 // Used to create a |ChromeURLDataManager| for this |Profile|. 383 // TODO(erg, willchan): Remove this once |ProfileIOData| is made into a 384 // |ProfileKeyedService|. 385 virtual base::Callback<ChromeURLDataManagerBackend*(void)> 386 GetChromeURLDataManagerBackendGetter() const = 0; 387 388 private: 389 bool restored_last_session_; 390 391 // Used to prevent the notification that this Profile is destroyed from 392 // being sent twice. 393 bool sent_destroyed_notification_; 394 395 // Accessibility events will only be propagated when the pause 396 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents 397 // increment and decrement the level, respectively, rather than set it to 398 // true or false, so that calls can be nested. 399 int accessibility_pause_level_; 400}; 401 402#if defined(COMPILER_GCC) 403namespace BASE_HASH_NAMESPACE { 404 405template<> 406struct hash<Profile*> { 407 std::size_t operator()(Profile* const& p) const { 408 return reinterpret_cast<std::size_t>(p); 409 } 410}; 411 412} // namespace BASE_HASH_NAMESPACE 413#endif 414 415#endif // CHROME_BROWSER_PROFILES_PROFILE_H_ 416