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.
5 // This class gathers state related to a single user profile.
7 #ifndef CHROME_BROWSER_PROFILES_PROFILE_H_
8 #define CHROME_BROWSER_PROFILES_PROFILE_H_
12 #include "base/basictypes.h"
13 #include "base/containers/hash_tables.h"
14 #include "base/logging.h"
15 #include "components/domain_reliability/clear_mode.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/content_browser_client.h"
19 class ChromeAppCacheService
;
20 class DevToolsNetworkController
;
21 class ExtensionService
;
22 class ExtensionSpecialStoragePolicy
;
24 class HostContentSettingsMap
;
25 class PrefProxyConfigTracker
;
28 class ProtocolHandlerRegistry
;
33 class TabContentsProvider
;
37 class SequencedTaskRunner
;
41 namespace chrome_browser_net
{
46 class LibCrosServiceLibraryImpl
;
47 class ResetDefaultProxyConfigServiceTask
;
55 class FileSystemContext
;
63 class SSLConfigService
;
66 namespace user_prefs
{
67 class PrefRegistrySyncable
;
70 // Instead of adding more members to Profile, consider creating a
72 // http://dev.chromium.org/developers/design-documents/profile-architecture
73 class Profile
: public content::BrowserContext
{
75 // Profile services are accessed with the following parameter. This parameter
76 // defines what the caller plans to do with the service.
77 // The caller is responsible for not performing any operation that would
78 // result in persistent implicit records while using an OffTheRecord profile.
79 // This flag allows the profile to perform an additional check.
81 // It also gives us an opportunity to perform further checks in the future. We
82 // could, for example, return an history service that only allow some specific
84 enum ServiceAccessType
{
85 // The caller plans to perform a read or write that takes place as a result
86 // of the user input. Use this flag when the operation you are doing can be
87 // performed while incognito. (ex: creating a bookmark)
89 // Since EXPLICIT_ACCESS means "as a result of a user action", this request
93 // The caller plans to call a method that will permanently change some data
94 // in the profile, as part of Chrome's implicit data logging. Use this flag
95 // when you are about to perform an operation which is incompatible with the
101 // Profile services were not created due to a local error (e.g., disk full).
102 CREATE_STATUS_LOCAL_FAIL
,
103 // Profile services were not created due to a remote error (e.g., network
104 // down during limited-user registration).
105 CREATE_STATUS_REMOTE_FAIL
,
106 // Profile created but before initializing extensions and promo resources.
107 CREATE_STATUS_CREATED
,
108 // Profile is created, extensions and promo resources are initialized.
109 CREATE_STATUS_INITIALIZED
,
110 // Profile creation (managed-user registration, generally) was canceled
112 CREATE_STATUS_CANCELED
,
113 MAX_CREATE_STATUS
// For histogram display.
117 CREATE_MODE_SYNCHRONOUS
,
118 CREATE_MODE_ASYNCHRONOUS
122 // A normal shutdown. The user clicked exit/closed last window of the
126 // The exit was the result of the system shutting down.
133 REGULAR_PROFILE
, // Login user's normal profile
134 INCOGNITO_PROFILE
, // Login user's off-the-record profile
135 GUEST_PROFILE
, // Guest session's profile
142 // Called when creation of the profile is finished.
143 virtual void OnProfileCreated(Profile
* profile
,
145 bool is_new_profile
) = 0;
148 // Key used to bind profile to the widget with which it is associated.
149 static const char kProfileKey
[];
154 // Profile prefs are registered as soon as the prefs are loaded for the first
156 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable
* registry
);
158 // Create a new profile given a path. If |create_mode| is
159 // CREATE_MODE_ASYNCHRONOUS then the profile is initialized asynchronously.
160 static Profile
* CreateProfile(const base::FilePath
& path
,
162 CreateMode create_mode
);
164 // Returns the profile corresponding to the given browser context.
165 static Profile
* FromBrowserContext(content::BrowserContext
* browser_context
);
167 // Returns the profile corresponding to the given WebUI.
168 static Profile
* FromWebUI(content::WebUI
* web_ui
);
170 // content::BrowserContext implementation ------------------------------------
173 virtual TestingProfile
* AsTestingProfile();
175 // Returns sequenced task runner where browser context dependent I/O
176 // operations should be performed.
177 virtual scoped_refptr
<base::SequencedTaskRunner
> GetIOTaskRunner() = 0;
179 // Returns the name associated with this profile. This name is displayed in
180 // the browser frame.
181 virtual std::string
GetProfileName() = 0;
183 // Returns the profile type.
184 virtual ProfileType
GetProfileType() const = 0;
186 // Return the incognito version of this profile. The returned pointer
187 // is owned by the receiving profile. If the receiving profile is off the
188 // record, the same profile is returned.
190 // WARNING: This will create the OffTheRecord profile if it doesn't already
191 // exist. If this isn't what you want, you need to check
192 // HasOffTheRecordProfile() first.
193 virtual Profile
* GetOffTheRecordProfile() = 0;
195 // Destroys the incognito profile.
196 virtual void DestroyOffTheRecordProfile() = 0;
198 // True if an incognito profile exists.
199 virtual bool HasOffTheRecordProfile() = 0;
201 // Return the original "recording" profile. This method returns this if the
202 // profile is not incognito.
203 virtual Profile
* GetOriginalProfile() = 0;
205 // Returns whether the profile is managed (see ManagedUserService).
206 virtual bool IsManaged() = 0;
208 // Returns a pointer to the TopSites (thumbnail manager) instance
210 virtual history::TopSites
* GetTopSites() = 0;
212 // Variant of GetTopSites that doesn't force creation.
213 virtual history::TopSites
* GetTopSitesWithoutCreating() = 0;
215 // DEPRECATED. Instead, use ExtensionSystem::extension_service().
216 // Retrieves a pointer to the ExtensionService associated with this
217 // profile. The ExtensionService is created at startup.
218 // TODO(yoz): remove this accessor (bug 104095).
219 virtual ExtensionService
* GetExtensionService() = 0;
221 // Accessor. The instance is created upon first access.
222 virtual ExtensionSpecialStoragePolicy
*
223 GetExtensionSpecialStoragePolicy() = 0;
225 // Retrieves a pointer to the PrefService that manages the
226 // preferences for this user profile.
227 virtual PrefService
* GetPrefs() = 0;
229 // Retrieves a pointer to the PrefService that manages the preferences
230 // for OffTheRecord Profiles. This PrefService is lazily created the first
231 // time that this method is called.
232 virtual PrefService
* GetOffTheRecordPrefs() = 0;
234 // Returns the main request context.
235 virtual net::URLRequestContextGetter
* GetRequestContext() = 0;
237 // Returns the request context used for extension-related requests. This
238 // is only used for a separate cookie store currently.
239 virtual net::URLRequestContextGetter
* GetRequestContextForExtensions() = 0;
241 // Returns the SSLConfigService for this profile.
242 virtual net::SSLConfigService
* GetSSLConfigService() = 0;
244 // Returns the Hostname <-> Content settings map for this profile.
245 virtual HostContentSettingsMap
* GetHostContentSettingsMap() = 0;
247 // Return whether 2 profiles are the same. 2 profiles are the same if they
248 // represent the same profile. This can happen if there is pointer equality
249 // or if one profile is the incognito version of another profile (or vice
251 virtual bool IsSameProfile(Profile
* profile
) = 0;
253 // Returns the time the profile was started. This is not the time the profile
254 // was created, rather it is the time the user started chrome and logged into
255 // this profile. For the single profile case, this corresponds to the time
256 // the user started chrome.
257 virtual base::Time
GetStartTime() const = 0;
259 // Creates the main net::URLRequestContextGetter that will be returned by
260 // GetRequestContext(). Should only be called once per ContentBrowserClient
261 // object. This function is exposed because of the circular dependency where
262 // GetStoragePartition() is used to retrieve the request context, but creation
263 // still has to happen in the Profile so the StoragePartition calls
264 // ContextBrowserClient to call this function.
265 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
266 virtual net::URLRequestContextGetter
* CreateRequestContext(
267 content::ProtocolHandlerMap
* protocol_handlers
,
268 content::URLRequestInterceptorScopedVector request_interceptors
) = 0;
270 // Creates the net::URLRequestContextGetter for a StoragePartition. Should
271 // only be called once per partition_path per ContentBrowserClient object.
272 // This function is exposed because the request context is retrieved from the
273 // StoragePartition, but creation still has to happen in the Profile so the
274 // StoragePartition calls ContextBrowserClient to call this function.
275 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
276 virtual net::URLRequestContextGetter
* CreateRequestContextForStoragePartition(
277 const base::FilePath
& partition_path
,
279 content::ProtocolHandlerMap
* protocol_handlers
,
280 content::URLRequestInterceptorScopedVector request_interceptors
) = 0;
282 // Returns the last directory that was chosen for uploading or opening a file.
283 virtual base::FilePath
last_selected_directory() = 0;
284 virtual void set_last_selected_directory(const base::FilePath
& path
) = 0;
286 #if defined(OS_CHROMEOS)
287 enum AppLocaleChangedVia
{
288 // Caused by chrome://settings change.
289 APP_LOCALE_CHANGED_VIA_SETTINGS
,
290 // Locale has been reverted via LocaleChangeGuard.
291 APP_LOCALE_CHANGED_VIA_REVERT
,
292 // From login screen.
293 APP_LOCALE_CHANGED_VIA_LOGIN
,
295 APP_LOCALE_CHANGED_VIA_UNKNOWN
298 // Changes application locale for a profile.
299 virtual void ChangeAppLocale(
300 const std::string
& locale
, AppLocaleChangedVia via
) = 0;
302 // Called after login.
303 virtual void OnLogin() = 0;
305 // Initializes Chrome OS's preferences.
306 virtual void InitChromeOSPreferences() = 0;
307 #endif // defined(OS_CHROMEOS)
309 // Returns the helper object that provides the proxy configuration service
310 // access to the the proxy configuration possibly defined by preferences.
311 virtual PrefProxyConfigTracker
* GetProxyConfigTracker() = 0;
313 // Returns the Predictor object used for dns prefetch.
314 virtual chrome_browser_net::Predictor
* GetNetworkPredictor() = 0;
316 // Returns the DevToolsNetworkController for this profile.
317 virtual DevToolsNetworkController
* GetDevToolsNetworkController() = 0;
319 // Deletes all network related data since |time|. It deletes transport
320 // security state since |time| and it also deletes HttpServerProperties data.
321 // Works asynchronously, however if the |completion| callback is non-null, it
322 // will be posted on the UI thread once the removal process completes.
323 // Be aware that theoretically it is possible that |completion| will be
324 // invoked after the Profile instance has been destroyed.
325 virtual void ClearNetworkingHistorySince(base::Time time
,
326 const base::Closure
& completion
) = 0;
328 // Clears browsing data stored in the Domain Reliability Monitor. (See
329 // profile_impl_io_data.h for details.)
330 virtual void ClearDomainReliabilityMonitor(
331 domain_reliability::DomainReliabilityClearMode mode
,
332 const base::Closure
& competion
) = 0;
334 // Returns the home page for this profile.
335 virtual GURL
GetHomePage() = 0;
337 // Returns whether or not the profile was created by a version of Chrome
338 // more recent (or equal to) the one specified.
339 virtual bool WasCreatedByVersionOrLater(const std::string
& version
) = 0;
341 std::string
GetDebugName();
343 // Returns whether it is a guest session.
344 virtual bool IsGuestSession() const;
346 // Did the user restore the last session? This is set by SessionRestore.
347 void set_restored_last_session(bool restored_last_session
) {
348 restored_last_session_
= restored_last_session
;
350 bool restored_last_session() const {
351 return restored_last_session_
;
354 // Sets the ExitType for the profile. This may be invoked multiple times
355 // during shutdown; only the first such change (the transition from
356 // EXIT_CRASHED to one of the other values) is written to prefs, any
357 // later calls are ignored.
359 // NOTE: this is invoked internally on a normal shutdown, but is public so
360 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION),
361 // or to handle backgrounding/foregrounding on mobile.
362 virtual void SetExitType(ExitType exit_type
) = 0;
364 // Returns how the last session was shutdown.
365 virtual ExitType
GetLastSessionExitType() = 0;
367 // Stop sending accessibility events until ResumeAccessibilityEvents().
368 // Calls to Pause nest; no events will be sent until the number of
369 // Resume calls matches the number of Pause calls received.
370 void PauseAccessibilityEvents() {
371 accessibility_pause_level_
++;
374 void ResumeAccessibilityEvents() {
375 DCHECK_GT(accessibility_pause_level_
, 0);
376 accessibility_pause_level_
--;
379 bool ShouldSendAccessibilityEvents() {
380 return 0 == accessibility_pause_level_
;
383 // Returns whether the profile is new. A profile is new if the browser has
384 // not been shut down since the profile was created.
387 // Checks whether sync is configurable by the user. Returns false if sync is
388 // disabled or controlled by configuration management.
389 bool IsSyncAccessible();
391 // Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not
392 // already been sent. It is necessary because most Profiles are destroyed by
393 // ProfileDestroyer, but in tests, some are not.
394 void MaybeSendDestroyedNotification();
396 // Creates an OffTheRecordProfile which points to this Profile.
397 Profile
* CreateOffTheRecordProfile();
400 bool restored_last_session_
;
402 // Used to prevent the notification that this Profile is destroyed from
404 bool sent_destroyed_notification_
;
406 // Accessibility events will only be propagated when the pause
407 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents
408 // increment and decrement the level, respectively, rather than set it to
409 // true or false, so that calls can be nested.
410 int accessibility_pause_level_
;
412 DISALLOW_COPY_AND_ASSIGN(Profile
);
415 // The comparator for profile pointers as key in a map.
416 struct ProfileCompare
{
417 bool operator()(Profile
* a
, Profile
* b
) const;
420 #if defined(COMPILER_GCC)
421 namespace BASE_HASH_NAMESPACE
{
424 struct hash
<Profile
*> {
425 std::size_t operator()(Profile
* const& p
) const {
426 return reinterpret_cast<std::size_t>(p
);
430 } // namespace BASE_HASH_NAMESPACE
433 #endif // CHROME_BROWSER_PROFILES_PROFILE_H_