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