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 gathers state related to a single user profile. 6 7 #ifndef CHROME_BROWSER_PROFILES_PROFILE_H_ 8 #define CHROME_BROWSER_PROFILES_PROFILE_H_ 9 #pragma once 10 11 #include "base/basictypes.h" 12 #include "base/logging.h" 13 #ifndef ANDROID 14 #include "chrome/common/extensions/extension.h" 15 #endif 16 17 namespace base { 18 class Time; 19 } 20 21 namespace content { 22 class ResourceContext; 23 } 24 25 namespace fileapi { 26 class FileSystemContext; 27 class SandboxedFileSystemContext; 28 } 29 30 namespace history { 31 class TopSites; 32 } 33 34 namespace net { 35 class TransportSecurityState; 36 class SSLConfigService; 37 } 38 39 namespace policy { 40 class ProfilePolicyConnector; 41 } 42 43 namespace prerender { 44 class PrerenderManager; 45 } 46 47 namespace webkit_database { 48 class DatabaseTracker; 49 } 50 51 #ifdef ANDROID 52 struct UnloadedExtensionInfo { 53 enum Reason { 54 DISABLE, // The extension is being disabled. 55 UPDATE, // The extension is being updated to a newer version. 56 UNINSTALL, // The extension is being uninstalled. 57 }; 58 }; 59 #endif 60 61 class AutocompleteClassifier; 62 class BookmarkModel; 63 class BrowserSignin; 64 class ChromeAppCacheService; 65 class ChromeBlobStorageContext; 66 class ChromeURLDataManager; 67 class CloudPrintProxyService; 68 class DownloadManager; 69 class Extension; 70 class ExtensionDevToolsManager; 71 class ExtensionEventRouter; 72 class ExtensionInfoMap; 73 class ExtensionMessageService; 74 class ExtensionPrefValueMap; 75 class ExtensionProcessManager; 76 class ExtensionService; 77 class ExtensionSpecialStoragePolicy; 78 class FaviconService; 79 class FilePath; 80 class FindBarState; 81 class GeolocationContentSettingsMap; 82 class GeolocationPermissionContext; 83 class HistoryService; 84 class HostContentSettingsMap; 85 class HostZoomMap; 86 class NTPResourceCache; 87 class NavigationController; 88 class PasswordStore; 89 class PersonalDataManager; 90 class PrefProxyConfigTracker; 91 class PrefService; 92 class ProfileSyncFactory; 93 class ProfileSyncService; 94 class PromoCounter; 95 class ProtocolHandlerRegistry; 96 class SQLitePersistentCookieStore; 97 class SSLConfigServiceManager; 98 class SSLHostState; 99 class SessionService; 100 class SpellCheckHost; 101 class StatusTray; 102 class TabRestoreService; 103 class TemplateURLFetcher; 104 class TemplateURLModel; 105 class TokenService; 106 class TransportSecurityPersister; 107 class UserScriptMaster; 108 class UserStyleSheetWatcher; 109 class VisitedLinkEventListener; 110 class VisitedLinkMaster; 111 class WebDataService; 112 class WebKitContext; 113 class PromoResourceService; 114 115 namespace net { 116 class URLRequestContextGetter; 117 } 118 119 typedef intptr_t ProfileId; 120 121 class Profile { 122 public: 123 // Profile services are accessed with the following parameter. This parameter 124 // defines what the caller plans to do with the service. 125 // The caller is responsible for not performing any operation that would 126 // result in persistent implicit records while using an OffTheRecord profile. 127 // This flag allows the profile to perform an additional check. 128 // 129 // It also gives us an opportunity to perform further checks in the future. We 130 // could, for example, return an history service that only allow some specific 131 // methods. 132 enum ServiceAccessType { 133 // The caller plans to perform a read or write that takes place as a result 134 // of the user input. Use this flag when the operation you are doing can be 135 // performed while incognito. (ex: creating a bookmark) 136 // 137 // Since EXPLICIT_ACCESS means "as a result of a user action", this request 138 // always succeeds. 139 EXPLICIT_ACCESS, 140 141 // The caller plans to call a method that will permanently change some data 142 // in the profile, as part of Chrome's implicit data logging. Use this flag 143 // when you are about to perform an operation which is incompatible with the 144 // incognito mode. 145 IMPLICIT_ACCESS 146 }; 147 148 class Delegate { 149 public: 150 // Called when creation of the profile is finished. 151 virtual void OnProfileCreated(Profile* profile, bool success) = 0; 152 #ifdef ANDROID 153 virtual ~Delegate() {} 154 #endif 155 }; 156 157 // Key used to bind profile to the widget with which it is associated. 158 static const char* kProfileKey; 159 160 // Value that represents no profile Id. 161 static const ProfileId kInvalidProfileId; 162 163 Profile(); 164 virtual ~Profile() {} 165 166 // Profile prefs are registered as soon as the prefs are loaded for the first 167 // time. 168 static void RegisterUserPrefs(PrefService* prefs); 169 170 // Create a new profile given a path. 171 static Profile* CreateProfile(const FilePath& path); 172 173 // Same as above, but uses async initialization. 174 static Profile* CreateProfileAsync(const FilePath& path, 175 Delegate* delegate); 176 177 // Returns the request context for the "default" profile. This may be called 178 // from any thread. This CAN return NULL if a first request context has not 179 // yet been created. If necessary, listen on the UI thread for 180 // NOTIFY_DEFAULT_REQUEST_CONTEXT_AVAILABLE. 181 static net::URLRequestContextGetter* GetDefaultRequestContext(); 182 183 // Returns a unique Id that can be used to identify this profile at runtime. 184 // This Id is not persistent and will not survive a restart of the browser. 185 virtual ProfileId GetRuntimeId() = 0; 186 187 // Returns the path of the directory where this profile's data is stored. 188 virtual FilePath GetPath() = 0; 189 190 // Return whether this profile is incognito. Default is false. 191 virtual bool IsOffTheRecord() = 0; 192 193 // Return the incognito version of this profile. The returned pointer 194 // is owned by the receiving profile. If the receiving profile is off the 195 // record, the same profile is returned. 196 virtual Profile* GetOffTheRecordProfile() = 0; 197 198 // Destroys the incognito profile. 199 virtual void DestroyOffTheRecordProfile() = 0; 200 201 // True if an incognito profile exists. 202 virtual bool HasOffTheRecordProfile() = 0; 203 204 // Return the original "recording" profile. This method returns this if the 205 // profile is not incognito. 206 virtual Profile* GetOriginalProfile() = 0; 207 208 // Returns a pointer to the ChromeAppCacheService instance for this profile. 209 virtual ChromeAppCacheService* GetAppCacheService() = 0; 210 211 // Returns a pointer to the DatabaseTracker instance for this profile. 212 virtual webkit_database::DatabaseTracker* GetDatabaseTracker() = 0; 213 214 // Returns a pointer to the TopSites (thumbnail manager) instance 215 // for this profile. 216 virtual history::TopSites* GetTopSites() = 0; 217 218 // Variant of GetTopSites that doesn't force creation. 219 virtual history::TopSites* GetTopSitesWithoutCreating() = 0; 220 221 // Retrieves a pointer to the VisitedLinkMaster associated with this 222 // profile. The VisitedLinkMaster is lazily created the first time 223 // that this method is called. 224 virtual VisitedLinkMaster* GetVisitedLinkMaster() = 0; 225 226 // Retrieves a pointer to the ExtensionService associated with this 227 // profile. The ExtensionService is created at startup. 228 virtual ExtensionService* GetExtensionService() = 0; 229 230 // Retrieves a pointer to the UserScriptMaster associated with this 231 // profile. The UserScriptMaster is lazily created the first time 232 // that this method is called. 233 virtual UserScriptMaster* GetUserScriptMaster() = 0; 234 235 // Retrieves a pointer to the ExtensionDevToolsManager associated with this 236 // profile. The instance is created at startup. 237 virtual ExtensionDevToolsManager* GetExtensionDevToolsManager() = 0; 238 239 // Retrieves a pointer to the ExtensionProcessManager associated with this 240 // profile. The instance is created at startup. 241 virtual ExtensionProcessManager* GetExtensionProcessManager() = 0; 242 243 // Retrieves a pointer to the ExtensionMessageService associated with this 244 // profile. The instance is created at startup. 245 virtual ExtensionMessageService* GetExtensionMessageService() = 0; 246 247 // Accessor. The instance is created at startup. 248 virtual ExtensionEventRouter* GetExtensionEventRouter() = 0; 249 250 // Accessor. The instance is created upon first access. 251 virtual ExtensionSpecialStoragePolicy* 252 GetExtensionSpecialStoragePolicy() = 0; 253 254 // Retrieves a pointer to the SSLHostState associated with this profile. 255 // The SSLHostState is lazily created the first time that this method is 256 // called. 257 virtual SSLHostState* GetSSLHostState() = 0; 258 259 // Retrieves a pointer to the TransportSecurityState associated with 260 // this profile. The TransportSecurityState is lazily created the 261 // first time that this method is called. 262 virtual net::TransportSecurityState* GetTransportSecurityState() = 0; 263 264 // Retrieves a pointer to the FaviconService associated with this 265 // profile. The FaviconService is lazily created the first time 266 // that this method is called. 267 // 268 // Although FaviconService is refcounted, this will not addref, and callers 269 // do not need to do any reference counting as long as they keep the pointer 270 // only for the local scope (which they should do anyway since the browser 271 // process may decide to shut down). 272 // 273 // |access| defines what the caller plans to do with the service. See 274 // the ServiceAccessType definition above. 275 virtual FaviconService* GetFaviconService(ServiceAccessType access) = 0; 276 277 // Retrieves a pointer to the HistoryService associated with this 278 // profile. The HistoryService is lazily created the first time 279 // that this method is called. 280 // 281 // Although HistoryService is refcounted, this will not addref, and callers 282 // do not need to do any reference counting as long as they keep the pointer 283 // only for the local scope (which they should do anyway since the browser 284 // process may decide to shut down). 285 // 286 // |access| defines what the caller plans to do with the service. See 287 // the ServiceAccessType definition above. 288 virtual HistoryService* GetHistoryService(ServiceAccessType access) = 0; 289 290 // Similar to GetHistoryService(), but won't create the history service if it 291 // doesn't already exist. 292 virtual HistoryService* GetHistoryServiceWithoutCreating() = 0; 293 294 // Retrieves a pointer to the AutocompleteClassifier associated with this 295 // profile. The AutocompleteClassifier is lazily created the first time that 296 // this method is called. 297 virtual AutocompleteClassifier* GetAutocompleteClassifier() = 0; 298 299 // Returns the WebDataService for this profile. This is owned by 300 // the Profile. Callers that outlive the life of this profile need to be 301 // sure they refcount the returned value. 302 // 303 // |access| defines what the caller plans to do with the service. See 304 // the ServiceAccessType definition above. 305 virtual WebDataService* GetWebDataService(ServiceAccessType access) = 0; 306 307 // Similar to GetWebDataService(), but won't create the web data service if it 308 // doesn't already exist. 309 virtual WebDataService* GetWebDataServiceWithoutCreating() = 0; 310 311 // Returns the PasswordStore for this profile. This is owned by the Profile. 312 virtual PasswordStore* GetPasswordStore(ServiceAccessType access) = 0; 313 314 // Retrieves a pointer to the PrefService that manages the preferences 315 // for this user profile. The PrefService is lazily created the first 316 // time that this method is called. 317 virtual PrefService* GetPrefs() = 0; 318 319 // Retrieves a pointer to the PrefService that manages the preferences 320 // for OffTheRecord Profiles. This PrefService is lazily created the first 321 // time that this method is called. 322 virtual PrefService* GetOffTheRecordPrefs() = 0; 323 324 // Returns the TemplateURLModel for this profile. This is owned by the 325 // the Profile. 326 virtual TemplateURLModel* GetTemplateURLModel() = 0; 327 328 // Returns the TemplateURLFetcher for this profile. This is owned by the 329 // profile. 330 virtual TemplateURLFetcher* GetTemplateURLFetcher() = 0; 331 332 // Returns the DownloadManager associated with this profile. 333 virtual DownloadManager* GetDownloadManager() = 0; 334 virtual bool HasCreatedDownloadManager() const = 0; 335 336 // Returns the PersonalDataManager associated with this profile. 337 virtual PersonalDataManager* GetPersonalDataManager() = 0; 338 339 // Returns the FileSystemContext associated to this profile. The context 340 // is lazily created the first time this method is called. This is owned 341 // by the profile. 342 virtual fileapi::FileSystemContext* GetFileSystemContext() = 0; 343 344 // Returns the BrowserSignin object assigned to this profile. 345 virtual BrowserSignin* GetBrowserSignin() = 0; 346 347 // Returns the request context information associated with this profile. Call 348 // this only on the UI thread, since it can send notifications that should 349 // happen on the UI thread. 350 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 351 352 // Returns the request context appropriate for the given app. If installed_app 353 // is null or installed_app->is_storage_isolated() returns false, this is 354 // equivalent to calling GetRequestContext(). 355 // TODO(creis): After isolated app storage is no longer an experimental 356 // feature, consider making this the default contract for GetRequestContext. 357 virtual net::URLRequestContextGetter* GetRequestContextForPossibleApp( 358 const Extension* installed_app) = 0; 359 360 // Returns the request context for media resources asociated with this 361 // profile. 362 virtual net::URLRequestContextGetter* GetRequestContextForMedia() = 0; 363 364 // Returns the request context used for extension-related requests. This 365 // is only used for a separate cookie store currently. 366 virtual net::URLRequestContextGetter* GetRequestContextForExtensions() = 0; 367 368 // Returns the request context used within an installed app that has 369 // requested isolated storage. 370 virtual net::URLRequestContextGetter* GetRequestContextForIsolatedApp( 371 const std::string& app_id) = 0; 372 373 #ifndef ANDROID 374 virtual const content::ResourceContext& GetResourceContext() = 0; 375 #endif 376 377 // Called by the ExtensionService that lives in this profile. Gives the 378 // profile a chance to react to the load event before the EXTENSION_LOADED 379 // notification has fired. The purpose for handling this event first is to 380 // avoid race conditions by making sure URLRequestContexts learn about new 381 // extensions before anything else needs them to know. 382 virtual void RegisterExtensionWithRequestContexts( 383 const Extension* extension) {} 384 385 // Called by the ExtensionService that lives in this profile. Lets the 386 // profile clean up its RequestContexts once all the listeners to the 387 // EXTENSION_UNLOADED notification have finished running. 388 virtual void UnregisterExtensionWithRequestContexts( 389 const std::string& extension_id, 390 const UnloadedExtensionInfo::Reason) {} 391 392 // Returns the SSLConfigService for this profile. 393 virtual net::SSLConfigService* GetSSLConfigService() = 0; 394 395 // Returns the Hostname <-> Content settings map for this profile. 396 virtual HostContentSettingsMap* GetHostContentSettingsMap() = 0; 397 398 // Returns the Hostname <-> Zoom Level map for this profile. 399 virtual HostZoomMap* GetHostZoomMap() = 0; 400 401 // Returns the geolocation settings map for this profile. 402 virtual GeolocationContentSettingsMap* GetGeolocationContentSettingsMap() = 0; 403 404 // Returns the geolocation permission context for this profile. 405 virtual GeolocationPermissionContext* GetGeolocationPermissionContext() = 0; 406 407 // Returns the user style sheet watcher. 408 virtual UserStyleSheetWatcher* GetUserStyleSheetWatcher() = 0; 409 410 // Returns the find bar state for this profile. The find bar state is lazily 411 // created the first time that this method is called. 412 virtual FindBarState* GetFindBarState() = 0; 413 414 // Returns the session service for this profile. This may return NULL. If 415 // this profile supports a session service (it isn't incognito), and 416 // the session service hasn't yet been created, this forces creation of 417 // the session service. 418 // 419 // This returns NULL in two situations: the profile is incognito, or the 420 // session service has been explicitly shutdown (browser is exiting). Callers 421 // should always check the return value for NULL. 422 virtual SessionService* GetSessionService() = 0; 423 424 // If this profile has a session service, it is shut down. To properly record 425 // the current state this forces creation of the session service, then shuts 426 // it down. 427 virtual void ShutdownSessionService() = 0; 428 429 // Returns true if this profile has a session service. 430 virtual bool HasSessionService() const = 0; 431 432 // Returns true if this profile has a profile sync service. 433 virtual bool HasProfileSyncService() const = 0; 434 435 // Returns true if the last time this profile was open it was exited cleanly. 436 virtual bool DidLastSessionExitCleanly() = 0; 437 438 // Returns the BookmarkModel, creating if not yet created. 439 virtual BookmarkModel* GetBookmarkModel() = 0; 440 441 // Returns the ProtocolHandlerRegistry, creating if not yet created. 442 virtual ProtocolHandlerRegistry* GetProtocolHandlerRegistry() = 0; 443 444 // Returns the Gaia Token Service, creating if not yet created. 445 virtual TokenService* GetTokenService() = 0; 446 447 // Returns the ProfileSyncService, creating if not yet created. 448 virtual ProfileSyncService* GetProfileSyncService() = 0; 449 450 // Returns the ProfileSyncService, creating if not yet created, with 451 // the specified CrOS username. 452 virtual ProfileSyncService* GetProfileSyncService( 453 const std::string& cros_user) = 0; 454 455 // Returns the CloudPrintProxyService, creating if not yet created. 456 virtual CloudPrintProxyService* GetCloudPrintProxyService() = 0; 457 458 // Return whether 2 profiles are the same. 2 profiles are the same if they 459 // represent the same profile. This can happen if there is pointer equality 460 // or if one profile is the incognito version of another profile (or vice 461 // versa). 462 virtual bool IsSameProfile(Profile* profile) = 0; 463 464 // Returns the time the profile was started. This is not the time the profile 465 // was created, rather it is the time the user started chrome and logged into 466 // this profile. For the single profile case, this corresponds to the time 467 // the user started chrome. 468 virtual base::Time GetStartTime() const = 0; 469 470 // Returns the TabRestoreService. This returns NULL when incognito. 471 virtual TabRestoreService* GetTabRestoreService() = 0; 472 473 virtual void ResetTabRestoreService() = 0; 474 475 // May return NULL. 476 virtual SpellCheckHost* GetSpellCheckHost() = 0; 477 478 // If |force| is false, and the spellchecker is already initialized (or is in 479 // the process of initializing), then do nothing. Otherwise clobber the 480 // current spellchecker and replace it with a new one. 481 virtual void ReinitializeSpellCheckHost(bool force) = 0; 482 483 // Returns the WebKitContext assigned to this profile. 484 virtual WebKitContext* GetWebKitContext() = 0; 485 486 // Returns the StatusTray, which provides an API for displaying status icons 487 // in the system status tray. Returns NULL if status icons are not supported 488 // on this platform (or this is a unit test). 489 virtual StatusTray* GetStatusTray() = 0; 490 491 // Marks the profile as cleanly shutdown. 492 // 493 // NOTE: this is invoked internally on a normal shutdown, but is public so 494 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION). 495 virtual void MarkAsCleanShutdown() = 0; 496 497 // Initializes extensions machinery. 498 // Component extensions are always enabled, external and user extensions 499 // are controlled by |extensions_enabled|. 500 virtual void InitExtensions(bool extensions_enabled) = 0; 501 502 // Start up service that gathers data from a promo resource feed. 503 virtual void InitPromoResources() = 0; 504 505 // Register URLRequestFactories for protocols registered with 506 // registerProtocolHandler. 507 virtual void InitRegisteredProtocolHandlers() = 0; 508 509 // Returns the new tab page resource cache. 510 virtual NTPResourceCache* GetNTPResourceCache() = 0; 511 512 // Returns the last directory that was chosen for uploading or opening a file. 513 virtual FilePath last_selected_directory() = 0; 514 virtual void set_last_selected_directory(const FilePath& path) = 0; 515 516 // Returns a pointer to the ChromeBlobStorageContext instance for this 517 // profile. 518 virtual ChromeBlobStorageContext* GetBlobStorageContext() = 0; 519 520 // Returns the IO-thread-accessible profile data for this profile. 521 virtual ExtensionInfoMap* GetExtensionInfoMap() = 0; 522 523 // Returns the PromoCounter for Instant, or NULL if not applicable. 524 virtual PromoCounter* GetInstantPromoCounter() = 0; 525 526 // Gets the policy connector associated with this profile. 527 virtual policy::ProfilePolicyConnector* GetPolicyConnector() = 0; 528 529 // Returns the ChromeURLDataManager for this profile. 530 virtual ChromeURLDataManager* GetChromeURLDataManager() = 0; 531 532 #if defined(OS_CHROMEOS) 533 enum AppLocaleChangedVia { 534 // Caused by chrome://settings change. 535 APP_LOCALE_CHANGED_VIA_SETTINGS, 536 // Locale has been reverted via LocaleChangeGuard. 537 APP_LOCALE_CHANGED_VIA_REVERT, 538 // From login screen. 539 APP_LOCALE_CHANGED_VIA_LOGIN, 540 // Source unknown. 541 APP_LOCALE_CHANGED_VIA_UNKNOWN 542 }; 543 544 // Changes application locale for a profile. 545 virtual void ChangeAppLocale( 546 const std::string& locale, AppLocaleChangedVia via) = 0; 547 548 // Called after login. 549 virtual void OnLogin() = 0; 550 551 // Creates ChromeOS's EnterpriseExtensionListener. 552 virtual void SetupChromeOSEnterpriseExtensionObserver() = 0; 553 554 // Initializes Chrome OS's preferences. 555 virtual void InitChromeOSPreferences() = 0; 556 #endif // defined(OS_CHROMEOS) 557 558 // Returns the helper object that provides the proxy configuration service 559 // access to the the proxy configuration possibly defined by preferences. 560 virtual PrefProxyConfigTracker* GetProxyConfigTracker() = 0; 561 562 // Returns the PrerenderManager used to prerender entire webpages for this 563 // profile. 564 virtual prerender::PrerenderManager* GetPrerenderManager() = 0; 565 566 // Returns whether it is a guest session. 567 static bool IsGuestSession(); 568 569 #ifdef UNIT_TEST 570 // Use with caution. GetDefaultRequestContext may be called on any thread! 571 static void set_default_request_context(net::URLRequestContextGetter* c) { 572 default_request_context_ = c; 573 } 574 #endif 575 576 // Did the user restore the last session? This is set by SessionRestore. 577 void set_restored_last_session(bool restored_last_session) { 578 restored_last_session_ = restored_last_session; 579 } 580 bool restored_last_session() const { 581 return restored_last_session_; 582 } 583 584 // Stop sending accessibility events until ResumeAccessibilityEvents(). 585 // Calls to Pause nest; no events will be sent until the number of 586 // Resume calls matches the number of Pause calls received. 587 void PauseAccessibilityEvents() { 588 accessibility_pause_level_++; 589 } 590 591 void ResumeAccessibilityEvents() { 592 DCHECK(accessibility_pause_level_ > 0); 593 accessibility_pause_level_--; 594 } 595 596 bool ShouldSendAccessibilityEvents() { 597 return 0 == accessibility_pause_level_; 598 } 599 600 // Checks whether sync is configurable by the user. Returns false if sync is 601 // disabled or controlled by configuration management. 602 bool IsSyncAccessible(); 603 604 // Creates an OffTheRecordProfile which points to this Profile. 605 Profile* CreateOffTheRecordProfile(); 606 607 protected: 608 friend class OffTheRecordProfileImpl; 609 610 static net::URLRequestContextGetter* default_request_context_; 611 612 private: 613 bool restored_last_session_; 614 615 // Accessibility events will only be propagated when the pause 616 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents 617 // increment and decrement the level, respectively, rather than set it to 618 // true or false, so that calls can be nested. 619 int accessibility_pause_level_; 620 }; 621 622 #endif // CHROME_BROWSER_PROFILES_PROFILE_H_ 623