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