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/hash_tables.h"
14 #include "base/logging.h"
15 #include "chrome/browser/net/pref_proxy_config_tracker.h"
16 #include "content/public/browser/browser_context.h"
17 #include "content/public/browser/content_browser_client.h"
18 #include "net/url_request/url_request_job_factory.h"
20 class ChromeAppCacheService
;
21 class ExtensionService
;
22 class ExtensionSpecialStoragePolicy
;
24 class HostContentSettingsMap
;
26 class PrefRegistrySyncable
;
28 class ProtocolHandlerRegistry
;
33 class TabContentsProvider
;
37 class SequencedTaskRunner
;
41 namespace chrome_browser_net
{
46 class LibCrosServiceLibraryImpl
;
47 class ResetDefaultProxyConfigServiceTask
;
55 class FileSystemContext
;
59 class ShortcutsBackend
;
64 class SSLConfigService
;
68 class ManagedModePolicyProvider
;
72 class Profile
: public content::BrowserContext
{
74 // Profile services are accessed with the following parameter. This parameter
75 // defines what the caller plans to do with the service.
76 // The caller is responsible for not performing any operation that would
77 // result in persistent implicit records while using an OffTheRecord profile.
78 // This flag allows the profile to perform an additional check.
80 // It also gives us an opportunity to perform further checks in the future. We
81 // could, for example, return an history service that only allow some specific
83 enum ServiceAccessType
{
84 // The caller plans to perform a read or write that takes place as a result
85 // of the user input. Use this flag when the operation you are doing can be
86 // performed while incognito. (ex: creating a bookmark)
88 // Since EXPLICIT_ACCESS means "as a result of a user action", this request
92 // The caller plans to call a method that will permanently change some data
93 // in the profile, as part of Chrome's implicit data logging. Use this flag
94 // when you are about to perform an operation which is incompatible with the
100 // Profile services were not created.
102 // Profile created but before initializing extensions and promo resources.
103 CREATE_STATUS_CREATED
,
104 // Profile is created, extensions and promo resources are initialized.
105 CREATE_STATUS_INITIALIZED
,
109 CREATE_MODE_SYNCHRONOUS
,
110 CREATE_MODE_ASYNCHRONOUS
114 // A normal shutdown. The user clicked exit/closed last window of the
118 // The exit was the result of the system shutting down.
128 // Called when creation of the profile is finished.
129 virtual void OnProfileCreated(Profile
* profile
,
131 bool is_new_profile
) = 0;
134 // Key used to bind profile to the widget with which it is associated.
135 static const char kProfileKey
[];
140 // Profile prefs are registered as soon as the prefs are loaded for the first
142 static void RegisterUserPrefs(PrefRegistrySyncable
* registry
);
144 // Gets task runner for I/O operations associated with |profile|.
145 static scoped_refptr
<base::SequencedTaskRunner
> GetTaskRunnerForProfile(
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 base::FilePath
& path
,
152 CreateMode create_mode
);
154 // Returns the profile corresponding to the given browser context.
155 static Profile
* FromBrowserContext(content::BrowserContext
* browser_context
);
157 // Returns the profile corresponding to the given WebUI.
158 static Profile
* FromWebUI(content::WebUI
* web_ui
);
160 // content::BrowserContext implementation ------------------------------------
163 virtual TestingProfile
* AsTestingProfile();
165 // Returns sequenced task runner where browser context dependent I/O
166 // operations should be performed.
167 virtual scoped_refptr
<base::SequencedTaskRunner
> GetIOTaskRunner() = 0;
169 // Returns the name associated with this profile. This name is displayed in
170 // the browser frame.
171 virtual std::string
GetProfileName() = 0;
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.
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;
182 // Destroys the incognito profile.
183 virtual void DestroyOffTheRecordProfile() = 0;
185 // True if an incognito profile exists.
186 virtual bool HasOffTheRecordProfile() = 0;
188 // Return the original "recording" profile. This method returns this if the
189 // profile is not incognito.
190 virtual Profile
* GetOriginalProfile() = 0;
192 // Returns a pointer to the TopSites (thumbnail manager) instance
194 virtual history::TopSites
* GetTopSites() = 0;
196 // Variant of GetTopSites that doesn't force creation.
197 virtual history::TopSites
* GetTopSitesWithoutCreating() = 0;
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;
205 // Accessor. The instance is created upon first access.
206 virtual ExtensionSpecialStoragePolicy
*
207 GetExtensionSpecialStoragePolicy() = 0;
209 // Returns the ManagedModePolicyProvider for this profile, if it exists.
210 virtual policy::ManagedModePolicyProvider
* GetManagedModePolicyProvider() = 0;
212 // Returns the PolicyService that provides policies for this profile.
213 virtual policy::PolicyService
* GetPolicyService() = 0;
215 // Retrieves a pointer to the PrefService that manages the
216 // preferences for this user profile.
217 virtual PrefService
* GetPrefs() = 0;
219 // Retrieves a pointer to the PrefService that manages the preferences
220 // for OffTheRecord Profiles. This PrefService is lazily created the first
221 // time that this method is called.
222 virtual PrefService
* GetOffTheRecordPrefs() = 0;
224 // Returns the main request context.
225 virtual net::URLRequestContextGetter
* GetRequestContext() = 0;
227 // Returns the request context used for extension-related requests. This
228 // is only used for a separate cookie store currently.
229 virtual net::URLRequestContextGetter
* GetRequestContextForExtensions() = 0;
231 // Returns the SSLConfigService for this profile.
232 virtual net::SSLConfigService
* GetSSLConfigService() = 0;
234 // Returns the Hostname <-> Content settings map for this profile.
235 virtual HostContentSettingsMap
* GetHostContentSettingsMap() = 0;
237 // Return whether 2 profiles are the same. 2 profiles are the same if they
238 // represent the same profile. This can happen if there is pointer equality
239 // or if one profile is the incognito version of another profile (or vice
241 virtual bool IsSameProfile(Profile
* profile
) = 0;
243 // Returns the time the profile was started. This is not the time the profile
244 // was created, rather it is the time the user started chrome and logged into
245 // this profile. For the single profile case, this corresponds to the time
246 // the user started chrome.
247 virtual base::Time
GetStartTime() const = 0;
249 // Creates the main net::URLRequestContextGetter that will be returned by
250 // GetRequestContext(). Should only be called once per ContentBrowserClient
251 // object. This function is exposed because of the circular dependency where
252 // GetStoragePartition() is used to retrieve the request context, but creation
253 // still has to happen in the Profile so the StoragePartition calls
254 // ContextBrowserClient to call this function.
255 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
256 virtual net::URLRequestContextGetter
* CreateRequestContext(
257 content::ProtocolHandlerMap
* protocol_handlers
) = 0;
259 // Creates the net::URLRequestContextGetter for a StoragePartition. Should
260 // only be called once per partition_path per ContentBrowserClient object.
261 // This function is exposed because the request context is retrieved from the
262 // StoragePartition, but creation still has to happen in the Profile so the
263 // StoragePartition calls ContextBrowserClient to call this function.
264 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
265 virtual net::URLRequestContextGetter
* CreateRequestContextForStoragePartition(
266 const base::FilePath
& partition_path
,
268 content::ProtocolHandlerMap
* protocol_handlers
) = 0;
270 // Returns the last directory that was chosen for uploading or opening a file.
271 virtual base::FilePath
last_selected_directory() = 0;
272 virtual void set_last_selected_directory(const base::FilePath
& path
) = 0;
274 #if defined(OS_CHROMEOS)
275 enum AppLocaleChangedVia
{
276 // Caused by chrome://settings change.
277 APP_LOCALE_CHANGED_VIA_SETTINGS
,
278 // Locale has been reverted via LocaleChangeGuard.
279 APP_LOCALE_CHANGED_VIA_REVERT
,
280 // From login screen.
281 APP_LOCALE_CHANGED_VIA_LOGIN
,
283 APP_LOCALE_CHANGED_VIA_UNKNOWN
286 // Changes application locale for a profile.
287 virtual void ChangeAppLocale(
288 const std::string
& locale
, AppLocaleChangedVia via
) = 0;
290 // Called after login.
291 virtual void OnLogin() = 0;
293 // Creates ChromeOS's EnterpriseExtensionListener.
294 virtual void SetupChromeOSEnterpriseExtensionObserver() = 0;
296 // Initializes Chrome OS's preferences.
297 virtual void InitChromeOSPreferences() = 0;
298 #endif // defined(OS_CHROMEOS)
300 // Returns the helper object that provides the proxy configuration service
301 // access to the the proxy configuration possibly defined by preferences.
302 virtual PrefProxyConfigTracker
* GetProxyConfigTracker() = 0;
304 // Returns the Predictor object used for dns prefetch.
305 virtual chrome_browser_net::Predictor
* GetNetworkPredictor() = 0;
307 // Deletes all network related data since |time|. It deletes transport
308 // security state since |time| and it also deletes HttpServerProperties data.
309 // Works asynchronously, however if the |completion| callback is non-null, it
310 // will be posted on the UI thread once the removal process completes.
311 // Be aware that theoretically it is possible that |completion| will be
312 // invoked after the Profile instance has been destroyed.
313 virtual void ClearNetworkingHistorySince(base::Time time
,
314 const base::Closure
& completion
) = 0;
316 // Returns the home page for this profile.
317 virtual GURL
GetHomePage() = 0;
319 // Returns whether or not the profile was created by a version of Chrome
320 // more recent (or equal to) the one specified.
321 virtual bool WasCreatedByVersionOrLater(const std::string
& version
) = 0;
323 std::string
GetDebugName();
325 // Returns whether it is a guest session.
326 bool IsGuestSession() const;
328 // Did the user restore the last session? This is set by SessionRestore.
329 void set_restored_last_session(bool restored_last_session
) {
330 restored_last_session_
= restored_last_session
;
332 bool restored_last_session() const {
333 return restored_last_session_
;
336 // Sets the ExitType for the profile. This may be invoked multiple times
337 // during shutdown; only the first such change (the transition from
338 // EXIT_CRASHED to one of the other values) is written to prefs, any
339 // later calls are ignored.
341 // NOTE: this is invoked internally on a normal shutdown, but is public so
342 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION),
343 // or to handle backgrounding/foregrounding on mobile.
344 virtual void SetExitType(ExitType exit_type
) = 0;
346 // Returns how the last session was shutdown.
347 virtual ExitType
GetLastSessionExitType() = 0;
349 // Stop sending accessibility events until ResumeAccessibilityEvents().
350 // Calls to Pause nest; no events will be sent until the number of
351 // Resume calls matches the number of Pause calls received.
352 void PauseAccessibilityEvents() {
353 accessibility_pause_level_
++;
356 void ResumeAccessibilityEvents() {
357 DCHECK_GT(accessibility_pause_level_
, 0);
358 accessibility_pause_level_
--;
361 bool ShouldSendAccessibilityEvents() {
362 return 0 == accessibility_pause_level_
;
365 // Returns whether the profile is new. A profile is new if the browser has
366 // not been shut down since the profile was created.
369 // Checks whether sync is configurable by the user. Returns false if sync is
370 // disabled or controlled by configuration management.
371 bool IsSyncAccessible();
373 // Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not
374 // already been sent. It is necessary because most Profiles are destroyed by
375 // ProfileDestroyer, but in tests, some are not.
376 void MaybeSendDestroyedNotification();
378 // Creates an OffTheRecordProfile which points to this Profile.
379 Profile
* CreateOffTheRecordProfile();
382 bool restored_last_session_
;
384 // Used to prevent the notification that this Profile is destroyed from
386 bool sent_destroyed_notification_
;
388 // Accessibility events will only be propagated when the pause
389 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents
390 // increment and decrement the level, respectively, rather than set it to
391 // true or false, so that calls can be nested.
392 int accessibility_pause_level_
;
394 DISALLOW_COPY_AND_ASSIGN(Profile
);
397 #if defined(COMPILER_GCC)
398 namespace BASE_HASH_NAMESPACE
{
401 struct hash
<Profile
*> {
402 std::size_t operator()(Profile
* const& p
) const {
403 return reinterpret_cast<std::size_t>(p
);
407 } // namespace BASE_HASH_NAMESPACE
410 #endif // CHROME_BROWSER_PROFILES_PROFILE_H_