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