profile.h revision 7dbb3d5cf0c15f500944d211057644d6a2f37371
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. 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" 13#include "base/containers/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" 19 20class ChromeAppCacheService; 21class ExtensionService; 22class ExtensionSpecialStoragePolicy; 23class FaviconService; 24class HostContentSettingsMap; 25class PasswordStore; 26class 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 66namespace user_prefs { 67class PrefRegistrySyncable; 68} 69 70// Instead of adding more members to Profile, consider creating a 71// BrowserContextKeyedService. See 72// http://dev.chromium.org/developers/design-documents/profile-architecture 73class Profile : public content::BrowserContext { 74 public: 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. 80 // 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 83 // methods. 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) 88 // 89 // Since EXPLICIT_ACCESS means "as a result of a user action", this request 90 // always succeeds. 91 EXPLICIT_ACCESS, 92 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 96 // incognito mode. 97 IMPLICIT_ACCESS 98 }; 99 100 enum CreateStatus { 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 111 // by the user. 112 CREATE_STATUS_CANCELED, 113 MAX_CREATE_STATUS // For histogram display. 114 }; 115 116 enum CreateMode { 117 CREATE_MODE_SYNCHRONOUS, 118 CREATE_MODE_ASYNCHRONOUS 119 }; 120 121 enum ExitType { 122 // A normal shutdown. The user clicked exit/closed last window of the 123 // profile. 124 EXIT_NORMAL, 125 126 // The exit was the result of the system shutting down. 127 EXIT_SESSION_ENDED, 128 129 EXIT_CRASHED, 130 }; 131 132 class Delegate { 133 public: 134 virtual ~Delegate(); 135 136 // Called when creation of the profile is finished. 137 virtual void OnProfileCreated(Profile* profile, 138 bool success, 139 bool is_new_profile) = 0; 140 }; 141 142 // Key used to bind profile to the widget with which it is associated. 143 static const char kProfileKey[]; 144 145 Profile(); 146 virtual ~Profile(); 147 148 // Profile prefs are registered as soon as the prefs are loaded for the first 149 // time. 150 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 151 152 // Gets task runner for I/O operations associated with |profile|. 153 static scoped_refptr<base::SequencedTaskRunner> GetTaskRunnerForProfile( 154 Profile* profile); 155 156 // Create a new profile given a path. If |create_mode| is 157 // CREATE_MODE_ASYNCHRONOUS then the profile is initialized asynchronously. 158 static Profile* CreateProfile(const base::FilePath& path, 159 Delegate* delegate, 160 CreateMode create_mode); 161 162 // Returns the profile corresponding to the given browser context. 163 static Profile* FromBrowserContext(content::BrowserContext* browser_context); 164 165 // Returns the profile corresponding to the given WebUI. 166 static Profile* FromWebUI(content::WebUI* web_ui); 167 168 // content::BrowserContext implementation ------------------------------------ 169 170 // Typesafe upcast. 171 virtual TestingProfile* AsTestingProfile(); 172 173 // Returns sequenced task runner where browser context dependent I/O 174 // operations should be performed. 175 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() = 0; 176 177 // Returns the name associated with this profile. This name is displayed in 178 // the browser frame. 179 virtual std::string GetProfileName() = 0; 180 181 // Return the incognito version of this profile. The returned pointer 182 // is owned by the receiving profile. If the receiving profile is off the 183 // record, the same profile is returned. 184 // 185 // WARNING: This will create the OffTheRecord profile if it doesn't already 186 // exist. If this isn't what you want, you need to check 187 // HasOffTheRecordProfile() first. 188 virtual Profile* GetOffTheRecordProfile() = 0; 189 190 // Destroys the incognito profile. 191 virtual void DestroyOffTheRecordProfile() = 0; 192 193 // True if an incognito profile exists. 194 virtual bool HasOffTheRecordProfile() = 0; 195 196 // Return the original "recording" profile. This method returns this if the 197 // profile is not incognito. 198 virtual Profile* GetOriginalProfile() = 0; 199 200 // Returns a pointer to the TopSites (thumbnail manager) instance 201 // for this profile. 202 virtual history::TopSites* GetTopSites() = 0; 203 204 // Variant of GetTopSites that doesn't force creation. 205 virtual history::TopSites* GetTopSitesWithoutCreating() = 0; 206 207 // DEPRECATED. Instead, use ExtensionSystem::extension_service(). 208 // Retrieves a pointer to the ExtensionService associated with this 209 // profile. The ExtensionService is created at startup. 210 // TODO(yoz): remove this accessor (bug 104095). 211 virtual ExtensionService* GetExtensionService() = 0; 212 213 // Accessor. The instance is created upon first access. 214 virtual ExtensionSpecialStoragePolicy* 215 GetExtensionSpecialStoragePolicy() = 0; 216 217 // Retrieves a pointer to the PrefService that manages the 218 // preferences for this user profile. 219 virtual PrefService* GetPrefs() = 0; 220 221 // Retrieves a pointer to the PrefService that manages the preferences 222 // for OffTheRecord Profiles. This PrefService is lazily created the first 223 // time that this method is called. 224 virtual PrefService* GetOffTheRecordPrefs() = 0; 225 226 // Returns the main request context. 227 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 228 229 // Returns the request context used for extension-related requests. This 230 // is only used for a separate cookie store currently. 231 virtual net::URLRequestContextGetter* GetRequestContextForExtensions() = 0; 232 233 // Returns the SSLConfigService for this profile. 234 virtual net::SSLConfigService* GetSSLConfigService() = 0; 235 236 // Returns the Hostname <-> Content settings map for this profile. 237 virtual HostContentSettingsMap* GetHostContentSettingsMap() = 0; 238 239 // Return whether 2 profiles are the same. 2 profiles are the same if they 240 // represent the same profile. This can happen if there is pointer equality 241 // or if one profile is the incognito version of another profile (or vice 242 // versa). 243 virtual bool IsSameProfile(Profile* profile) = 0; 244 245 // Returns the time the profile was started. This is not the time the profile 246 // was created, rather it is the time the user started chrome and logged into 247 // this profile. For the single profile case, this corresponds to the time 248 // the user started chrome. 249 virtual base::Time GetStartTime() const = 0; 250 251 // Creates the main net::URLRequestContextGetter that will be returned by 252 // GetRequestContext(). Should only be called once per ContentBrowserClient 253 // object. This function is exposed because of the circular dependency where 254 // GetStoragePartition() is used to retrieve the request context, but creation 255 // still has to happen in the Profile so the StoragePartition calls 256 // ContextBrowserClient to call this function. 257 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved. 258 virtual net::URLRequestContextGetter* CreateRequestContext( 259 content::ProtocolHandlerMap* protocol_handlers) = 0; 260 261 // Creates the net::URLRequestContextGetter for a StoragePartition. Should 262 // only be called once per partition_path per ContentBrowserClient object. 263 // This function is exposed because the request context is retrieved from the 264 // StoragePartition, but creation still has to happen in the Profile so the 265 // StoragePartition calls ContextBrowserClient to call this function. 266 // TODO(ajwong): Remove once http://crbug.com/159193 is resolved. 267 virtual net::URLRequestContextGetter* CreateRequestContextForStoragePartition( 268 const base::FilePath& partition_path, 269 bool in_memory, 270 content::ProtocolHandlerMap* protocol_handlers) = 0; 271 272 // Returns the last directory that was chosen for uploading or opening a file. 273 virtual base::FilePath last_selected_directory() = 0; 274 virtual void set_last_selected_directory(const base::FilePath& path) = 0; 275 276#if defined(OS_CHROMEOS) 277 enum AppLocaleChangedVia { 278 // Caused by chrome://settings change. 279 APP_LOCALE_CHANGED_VIA_SETTINGS, 280 // Locale has been reverted via LocaleChangeGuard. 281 APP_LOCALE_CHANGED_VIA_REVERT, 282 // From login screen. 283 APP_LOCALE_CHANGED_VIA_LOGIN, 284 // Source unknown. 285 APP_LOCALE_CHANGED_VIA_UNKNOWN 286 }; 287 288 // Changes application locale for a profile. 289 virtual void ChangeAppLocale( 290 const std::string& locale, AppLocaleChangedVia via) = 0; 291 292 // Called after login. 293 virtual void OnLogin() = 0; 294 295 // Creates ChromeOS's EnterpriseExtensionListener. 296 virtual void SetupChromeOSEnterpriseExtensionObserver() = 0; 297 298 // Initializes Chrome OS's preferences. 299 virtual void InitChromeOSPreferences() = 0; 300#endif // defined(OS_CHROMEOS) 301 302 // Returns the helper object that provides the proxy configuration service 303 // access to the the proxy configuration possibly defined by preferences. 304 virtual PrefProxyConfigTracker* GetProxyConfigTracker() = 0; 305 306 // Returns the Predictor object used for dns prefetch. 307 virtual chrome_browser_net::Predictor* GetNetworkPredictor() = 0; 308 309 // Deletes all network related data since |time|. It deletes transport 310 // security state since |time| and it also deletes HttpServerProperties data. 311 // Works asynchronously, however if the |completion| callback is non-null, it 312 // will be posted on the UI thread once the removal process completes. 313 // Be aware that theoretically it is possible that |completion| will be 314 // invoked after the Profile instance has been destroyed. 315 virtual void ClearNetworkingHistorySince(base::Time time, 316 const base::Closure& completion) = 0; 317 318 // Returns the home page for this profile. 319 virtual GURL GetHomePage() = 0; 320 321 // Returns whether or not the profile was created by a version of Chrome 322 // more recent (or equal to) the one specified. 323 virtual bool WasCreatedByVersionOrLater(const std::string& version) = 0; 324 325 std::string GetDebugName(); 326 327 // Returns whether it is a guest session. 328 virtual bool IsGuestSession() const; 329 330 // Did the user restore the last session? This is set by SessionRestore. 331 void set_restored_last_session(bool restored_last_session) { 332 restored_last_session_ = restored_last_session; 333 } 334 bool restored_last_session() const { 335 return restored_last_session_; 336 } 337 338 // Sets the ExitType for the profile. This may be invoked multiple times 339 // during shutdown; only the first such change (the transition from 340 // EXIT_CRASHED to one of the other values) is written to prefs, any 341 // later calls are ignored. 342 // 343 // NOTE: this is invoked internally on a normal shutdown, but is public so 344 // that it can be invoked when the user logs out/powers down (WM_ENDSESSION), 345 // or to handle backgrounding/foregrounding on mobile. 346 virtual void SetExitType(ExitType exit_type) = 0; 347 348 // Returns how the last session was shutdown. 349 virtual ExitType GetLastSessionExitType() = 0; 350 351 // Stop sending accessibility events until ResumeAccessibilityEvents(). 352 // Calls to Pause nest; no events will be sent until the number of 353 // Resume calls matches the number of Pause calls received. 354 void PauseAccessibilityEvents() { 355 accessibility_pause_level_++; 356 } 357 358 void ResumeAccessibilityEvents() { 359 DCHECK_GT(accessibility_pause_level_, 0); 360 accessibility_pause_level_--; 361 } 362 363 bool ShouldSendAccessibilityEvents() { 364 return 0 == accessibility_pause_level_; 365 } 366 367 // Returns whether the profile is new. A profile is new if the browser has 368 // not been shut down since the profile was created. 369 bool IsNewProfile(); 370 371 // Checks whether sync is configurable by the user. Returns false if sync is 372 // disabled or controlled by configuration management. 373 bool IsSyncAccessible(); 374 375 // Send NOTIFICATION_PROFILE_DESTROYED for this Profile, if it has not 376 // already been sent. It is necessary because most Profiles are destroyed by 377 // ProfileDestroyer, but in tests, some are not. 378 void MaybeSendDestroyedNotification(); 379 380 // Creates an OffTheRecordProfile which points to this Profile. 381 Profile* CreateOffTheRecordProfile(); 382 383 private: 384 bool restored_last_session_; 385 386 // Used to prevent the notification that this Profile is destroyed from 387 // being sent twice. 388 bool sent_destroyed_notification_; 389 390 // Accessibility events will only be propagated when the pause 391 // level is zero. PauseAccessibilityEvents and ResumeAccessibilityEvents 392 // increment and decrement the level, respectively, rather than set it to 393 // true or false, so that calls can be nested. 394 int accessibility_pause_level_; 395 396 DISALLOW_COPY_AND_ASSIGN(Profile); 397}; 398 399#if defined(COMPILER_GCC) 400namespace BASE_HASH_NAMESPACE { 401 402template<> 403struct hash<Profile*> { 404 std::size_t operator()(Profile* const& p) const { 405 return reinterpret_cast<std::size_t>(p); 406 } 407}; 408 409} // namespace BASE_HASH_NAMESPACE 410#endif 411 412#endif // CHROME_BROWSER_PROFILES_PROFILE_H_ 413