blob: d8110218c09974e54c289b7c8511e362a8cd22c9 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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.
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
10#include <string>
11
12#include "base/basictypes.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010013#include "base/containers/hash_tables.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000014#include "base/logging.h"
15#include "chrome/browser/net/pref_proxy_config_tracker.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016#include "content/public/browser/browser_context.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000017#include "content/public/browser/content_browser_client.h"
18#include "net/url_request/url_request_job_factory.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000019
20class ChromeAppCacheService;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021class ExtensionService;
22class ExtensionSpecialStoragePolicy;
23class FaviconService;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000024class HostContentSettingsMap;
25class PasswordStore;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000026class PromoCounter;
27class ProtocolHandlerRegistry;
28class TestingProfile;
29class WebDataService;
30
31namespace android {
32class TabContentsProvider;
33}
34
35namespace base {
36class SequencedTaskRunner;
37class Time;
38}
39
40namespace chrome_browser_net {
41class Predictor;
42}
43
44namespace chromeos {
45class LibCrosServiceLibraryImpl;
46class ResetDefaultProxyConfigServiceTask;
47}
48
49namespace content {
50class WebUI;
51}
52
53namespace fileapi {
54class FileSystemContext;
55}
56
57namespace history {
58class ShortcutsBackend;
59class TopSites;
60}
61
62namespace net {
63class SSLConfigService;
64}
65
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +010066namespace user_prefs {
67class PrefRegistrySyncable;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000068}
69
70class Profile : public content::BrowserContext {
71 public:
72 // Profile services are accessed with the following parameter. This parameter
73 // defines what the caller plans to do with the service.
74 // The caller is responsible for not performing any operation that would
75 // result in persistent implicit records while using an OffTheRecord profile.
76 // This flag allows the profile to perform an additional check.
77 //
78 // It also gives us an opportunity to perform further checks in the future. We
79 // could, for example, return an history service that only allow some specific
80 // methods.
81 enum ServiceAccessType {
82 // The caller plans to perform a read or write that takes place as a result
83 // of the user input. Use this flag when the operation you are doing can be
84 // performed while incognito. (ex: creating a bookmark)
85 //
86 // Since EXPLICIT_ACCESS means "as a result of a user action", this request
87 // always succeeds.
88 EXPLICIT_ACCESS,
89
90 // The caller plans to call a method that will permanently change some data
91 // in the profile, as part of Chrome's implicit data logging. Use this flag
92 // when you are about to perform an operation which is incompatible with the
93 // incognito mode.
94 IMPLICIT_ACCESS
95 };
96
97 enum CreateStatus {
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010098 // Profile services were not created due to a local error (e.g., disk full).
99 CREATE_STATUS_LOCAL_FAIL,
100 // Profile services were not created due to a remote error (e.g., network
101 // down during limited-user registration).
102 CREATE_STATUS_REMOTE_FAIL,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000103 // Profile created but before initializing extensions and promo resources.
104 CREATE_STATUS_CREATED,
105 // Profile is created, extensions and promo resources are initialized.
106 CREATE_STATUS_INITIALIZED,
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +0100107 // Profile creation (managed-user registration, generally) was canceled
108 // by the user.
109 CREATE_STATUS_CANCELED,
110 MAX_CREATE_STATUS // For histogram display.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000111 };
112
113 enum CreateMode {
114 CREATE_MODE_SYNCHRONOUS,
115 CREATE_MODE_ASYNCHRONOUS
116 };
117
118 enum ExitType {
119 // A normal shutdown. The user clicked exit/closed last window of the
120 // profile.
121 EXIT_NORMAL,
122
123 // The exit was the result of the system shutting down.
124 EXIT_SESSION_ENDED,
125
126 EXIT_CRASHED,
127 };
128
129 class Delegate {
130 public:
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100131 virtual ~Delegate();
132
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000133 // Called when creation of the profile is finished.
134 virtual void OnProfileCreated(Profile* profile,
135 bool success,
136 bool is_new_profile) = 0;
137 };
138
139 // Key used to bind profile to the widget with which it is associated.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100140 static const char kProfileKey[];
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000141
142 Profile();
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100143 virtual ~Profile();
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000144
145 // Profile prefs are registered as soon as the prefs are loaded for the first
146 // time.
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100147 static void RegisterUserPrefs(user_prefs::PrefRegistrySyncable* registry);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000148
149 // Gets task runner for I/O operations associated with |profile|.
150 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForProfile(
151 Profile* profile);
152
153 // Create a new profile given a path. If |create_mode| is
154 // CREATE_MODE_ASYNCHRONOUS then the profile is initialized asynchronously.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000155 static Profile* CreateProfile(const base::FilePath& path,
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000156 Delegate* delegate,
157 CreateMode create_mode);
158
159 // Returns the profile corresponding to the given browser context.
160 static Profile* FromBrowserContext(content::BrowserContext* browser_context);
161
162 // Returns the profile corresponding to the given WebUI.
163 static Profile* FromWebUI(content::WebUI* web_ui);
164
165 // content::BrowserContext implementation ------------------------------------
166
167 // Typesafe upcast.
168 virtual TestingProfile* AsTestingProfile();
169
170 // Returns sequenced task runner where browser context dependent I/O
171 // operations should be performed.
172 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() = 0;
173
174 // Returns the name associated with this profile. This name is displayed in
175 // the browser frame.
176 virtual std::string GetProfileName() = 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 //
182 // WARNING: This will create the OffTheRecord profile if it doesn't already
183 // exist. If this isn't what you want, you need to check
184 // HasOffTheRecordProfile() first.
185 virtual Profile* GetOffTheRecordProfile() = 0;
186
187 // Destroys the incognito profile.
188 virtual void DestroyOffTheRecordProfile() = 0;
189
190 // True if an incognito profile exists.
191 virtual bool HasOffTheRecordProfile() = 0;
192
193 // Return the original "recording" profile. This method returns this if the
194 // profile is not incognito.
195 virtual Profile* GetOriginalProfile() = 0;
196
197 // Returns a pointer to the TopSites (thumbnail manager) instance
198 // for this profile.
199 virtual history::TopSites* GetTopSites() = 0;
200
201 // Variant of GetTopSites that doesn't force creation.
202 virtual history::TopSites* GetTopSitesWithoutCreating() = 0;
203
204 // DEPRECATED. Instead, use ExtensionSystem::extension_service().
205 // Retrieves a pointer to the ExtensionService associated with this
206 // profile. The ExtensionService is created at startup.
207 // TODO(yoz): remove this accessor (bug 104095).
208 virtual ExtensionService* GetExtensionService() = 0;
209
210 // Accessor. The instance is created upon first access.
211 virtual ExtensionSpecialStoragePolicy*
212 GetExtensionSpecialStoragePolicy() = 0;
213
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000214 // Retrieves a pointer to the PrefService that manages the
215 // preferences for this user profile.
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000216 virtual PrefService* GetPrefs() = 0;
217
218 // Retrieves a pointer to the PrefService that manages the preferences
219 // for OffTheRecord Profiles. This PrefService is lazily created the first
220 // time that this method is called.
221 virtual PrefService* GetOffTheRecordPrefs() = 0;
222
223 // Returns the main request context.
224 virtual net::URLRequestContextGetter* GetRequestContext() = 0;
225
226 // Returns the request context used for extension-related requests. This
227 // is only used for a separate cookie store currently.
228 virtual net::URLRequestContextGetter* GetRequestContextForExtensions() = 0;
229
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000230 // Returns the SSLConfigService for this profile.
231 virtual net::SSLConfigService* GetSSLConfigService() = 0;
232
233 // Returns the Hostname <-> Content settings map for this profile.
234 virtual HostContentSettingsMap* GetHostContentSettingsMap() = 0;
235
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000236 // Return whether 2 profiles are the same. 2 profiles are the same if they
237 // represent the same profile. This can happen if there is pointer equality
238 // or if one profile is the incognito version of another profile (or vice
239 // versa).
240 virtual bool IsSameProfile(Profile* profile) = 0;
241
242 // Returns the time the profile was started. This is not the time the profile
243 // was created, rather it is the time the user started chrome and logged into
244 // this profile. For the single profile case, this corresponds to the time
245 // the user started chrome.
246 virtual base::Time GetStartTime() const = 0;
247
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000248 // Creates the main net::URLRequestContextGetter that will be returned by
249 // GetRequestContext(). Should only be called once per ContentBrowserClient
250 // object. This function is exposed because of the circular dependency where
251 // GetStoragePartition() is used to retrieve the request context, but creation
252 // still has to happen in the Profile so the StoragePartition calls
253 // ContextBrowserClient to call this function.
254 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
255 virtual net::URLRequestContextGetter* CreateRequestContext(
256 content::ProtocolHandlerMap* protocol_handlers) = 0;
257
258 // Creates the net::URLRequestContextGetter for a StoragePartition. Should
259 // only be called once per partition_path per ContentBrowserClient object.
260 // This function is exposed because the request context is retrieved from the
261 // StoragePartition, but creation still has to happen in the Profile so the
262 // StoragePartition calls ContextBrowserClient to call this function.
263 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved.
264 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition(
265 const base::FilePath& partition_path,
266 bool in_memory,
267 content::ProtocolHandlerMap* protocol_handlers) = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000268
269 // Returns the last directory that was chosen for uploading or opening a file.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000270 virtual base::FilePath last_selected_directory() = 0;
271 virtual void set_last_selected_directory(const base::FilePath& path) = 0;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000272
273#if defined(OS_CHROMEOS)
274 enum AppLocaleChangedVia {
275 // Caused by chrome://settings change.
276 APP_LOCALE_CHANGED_VIA_SETTINGS,
277 // Locale has been reverted via LocaleChangeGuard.
278 APP_LOCALE_CHANGED_VIA_REVERT,
279 // From login screen.
280 APP_LOCALE_CHANGED_VIA_LOGIN,
281 // Source unknown.
282 APP_LOCALE_CHANGED_VIA_UNKNOWN
283 };
284
285 // Changes application locale for a profile.
286 virtual void ChangeAppLocale(
287 const std::string& locale, AppLocaleChangedVia via) = 0;
288
289 // Called after login.
290 virtual void OnLogin() = 0;
291
292 // Creates ChromeOS's EnterpriseExtensionListener.
293 virtual void SetupChromeOSEnterpriseExtensionObserver() = 0;
294
295 // Initializes Chrome OS's preferences.
296 virtual void InitChromeOSPreferences() = 0;
297#endif // defined(OS_CHROMEOS)
298
299 // Returns the helper object that provides the proxy configuration service
300 // access to the the proxy configuration possibly defined by preferences.
301 virtual PrefProxyConfigTracker* GetProxyConfigTracker() = 0;
302
303 // Returns the Predictor object used for dns prefetch.
304 virtual chrome_browser_net::Predictor* GetNetworkPredictor() = 0;
305
306 // Deletes all network related data since |time|. It deletes transport
307 // security state since |time| and it also deletes HttpServerProperties data.
308 // Works asynchronously, however if the |completion| callback is non-null, it
309 // will be posted on the UI thread once the removal process completes.
310 // Be aware that theoretically it is possible that |completion| will be
311 // invoked after the Profile instance has been destroyed.
312 virtual void ClearNetworkingHistorySince(base::Time time,
313 const base::Closure& completion) = 0;
314
315 // Returns the home page for this profile.
316 virtual GURL GetHomePage() = 0;
317
318 // Returns whether or not the profile was created by a version of Chrome
319 // more recent (or equal to) the one specified.
320 virtual bool WasCreatedByVersionOrLater(const std::string& version) = 0;
321
322 std::string GetDebugName();
323
324 // Returns whether it is a guest session.
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +0000325 bool IsGuestSession() const;
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000326
327 // Did the user restore the last session? This is set by SessionRestore.
328 void set_restored_last_session(bool restored_last_session) {
329 restored_last_session_ = restored_last_session;
330 }
331 bool restored_last_session() const {
332 return restored_last_session_;
333 }
334
335 // Sets the ExitType for the profile. This may be invoked multiple times
336 // during shutdown; only the first such change (the transition from
337 // EXIT_CRASHED to one of the other values) is written to prefs, any
338 // later calls are ignored.
339 //
340 // NOTE: this is invoked internally on a normal shutdown, but is public so
341 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION),
342 // or to handle backgrounding/foregrounding on mobile.
343 virtual void SetExitType(ExitType exit_type) = 0;
344
345 // Returns how the last session was shutdown.
346 virtual ExitType GetLastSessionExitType() = 0;
347
348 // Stop sending accessibility events until ResumeAccessibilityEvents().
349 // Calls to Pause nest; no events will be sent until the number of
350 // Resume calls matches the number of Pause calls received.
351 void PauseAccessibilityEvents() {
352 accessibility_pause_level_++;
353 }
354
355 void ResumeAccessibilityEvents() {
356 DCHECK_GT(accessibility_pause_level_, 0);
357 accessibility_pause_level_--;
358 }
359
360 bool ShouldSendAccessibilityEvents() {
361 return 0 == accessibility_pause_level_;
362 }
363
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100364 // Returns whether the profile is new. A profile is new if the browser has
365 // not been shut down since the profile was created.
366 bool IsNewProfile();
367
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000368 // Checks whether sync is configurable by the user. Returns false if sync is
369 // disabled or controlled by configuration management.
370 bool IsSyncAccessible();
371
372 // Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not
373 // already been sent. It is necessary because most Profiles are destroyed by
374 // ProfileDestroyer, but in tests, some are not.
375 void MaybeSendDestroyedNotification();
376
377 // Creates an OffTheRecordProfile which points to this Profile.
378 Profile* CreateOffTheRecordProfile();
379
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000380 private:
381 bool restored_last_session_;
382
383 // Used to prevent the notification that this Profile is destroyed from
384 // being sent twice.
385 bool sent_destroyed_notification_;
386
387 // Accessibility events will only be propagated when the pause
388 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents
389 // increment and decrement the level, respectively, rather than set it to
390 // true or false, so that calls can be nested.
391 int accessibility_pause_level_;
Torne (Richard Coles)c2e0dbd2013-05-09 18:35:53 +0100392
393 DISALLOW_COPY_AND_ASSIGN(Profile);
Torne (Richard Coles)58218062012-11-14 11:43:16 +0000394};
395
396#if defined(COMPILER_GCC)
397namespace BASE_HASH_NAMESPACE {
398
399template<>
400struct hash<Profile*> {
401 std::size_t operator()(Profile* const& p) const {
402 return reinterpret_cast<std::size_t>(p);
403 }
404};
405
406} // namespace BASE_HASH_NAMESPACE
407#endif
408
409#endif // CHROME_BROWSER_PROFILES_PROFILE_H_