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 19 class ChromeAppCacheService; 20 class DevToolsNetworkController; 21 class ExtensionService; 22 class ExtensionSpecialStoragePolicy; 23 class FaviconService; 24 class HostContentSettingsMap; 25 class PrefProxyConfigTracker; 26 class PrefService; 27 class PromoCounter; 28 class ProtocolHandlerRegistry; 29 class TestingProfile; 30 class WebDataService; 31 32 namespace android { 33 class TabContentsProvider; 34 } 35 36 namespace base { 37 class SequencedTaskRunner; 38 class Time; 39 } 40 41 namespace chrome_browser_net { 42 class Predictor; 43 } 44 45 namespace chromeos { 46 class LibCrosServiceLibraryImpl; 47 class ResetDefaultProxyConfigServiceTask; 48 } 49 50 namespace content { 51 class WebUI; 52 } 53 54 namespace fileapi { 55 class FileSystemContext; 56 } 57 58 namespace history { 59 class TopSites; 60 } 61 62 namespace net { 63 class SSLConfigService; 64 } 65 66 namespace user_prefs { 67 class PrefRegistrySyncable; 68 } 69 70 // Instead of adding more members to Profile, consider creating a 71 // KeyedService. See 72 // http://dev.chromium.org/developers/design-documents/profile-architecture 73 class 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 enum ProfileType { 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 136 }; 137 138 class Delegate { 139 public: 140 virtual ~Delegate(); 141 142 // Called when creation of the profile is finished. 143 virtual void OnProfileCreated(Profile* profile, 144 bool success, 145 bool is_new_profile) = 0; 146 }; 147 148 // Key used to bind profile to the widget with which it is associated. 149 static const char kProfileKey[]; 150 151 Profile(); 152 virtual ~Profile(); 153 154 // Profile prefs are registered as soon as the prefs are loaded for the first 155 // time. 156 static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 157 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, 161 Delegate* delegate, 162 CreateMode create_mode); 163 164 // Returns the profile corresponding to the given browser context. 165 static Profile* FromBrowserContext(content::BrowserContext* browser_context); 166 167 // Returns the profile corresponding to the given WebUI. 168 static Profile* FromWebUI(content::WebUI* web_ui); 169 170 // content::BrowserContext implementation ------------------------------------ 171 172 // Typesafe upcast. 173 virtual TestingProfile* AsTestingProfile(); 174 175 // Returns sequenced task runner where browser context dependent I/O 176 // operations should be performed. 177 virtual scoped_refptr<base::SequencedTaskRunner> GetIOTaskRunner() = 0; 178 179 // Returns the name associated with this profile. This name is displayed in 180 // the browser frame. 181 virtual std::string GetProfileName() = 0; 182 183 // Returns the profile type. 184 virtual ProfileType GetProfileType() const = 0; 185 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. 189 // 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; 194 195 // Destroys the incognito profile. 196 virtual void DestroyOffTheRecordProfile() = 0; 197 198 // True if an incognito profile exists. 199 virtual bool HasOffTheRecordProfile() = 0; 200 201 // Return the original "recording" profile. This method returns this if the 202 // profile is not incognito. 203 virtual Profile* GetOriginalProfile() = 0; 204 205 // Returns whether the profile is supervised (see ManagedUserService). 206 virtual bool IsSupervised() = 0; 207 208 // Returns a pointer to the TopSites (thumbnail manager) instance 209 // for this profile. 210 virtual history::TopSites* GetTopSites() = 0; 211 212 // Variant of GetTopSites that doesn't force creation. 213 virtual history::TopSites* GetTopSitesWithoutCreating() = 0; 214 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; 220 221 // Accessor. The instance is created upon first access. 222 virtual ExtensionSpecialStoragePolicy* 223 GetExtensionSpecialStoragePolicy() = 0; 224 225 // Retrieves a pointer to the PrefService that manages the 226 // preferences for this user profile. 227 virtual PrefService* GetPrefs() = 0; 228 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; 233 234 // Returns the main request context. 235 virtual net::URLRequestContextGetter* GetRequestContext() = 0; 236 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; 240 241 // Returns the SSLConfigService for this profile. 242 virtual net::SSLConfigService* GetSSLConfigService() = 0; 243 244 // Returns the Hostname <-> Content settings map for this profile. 245 virtual HostContentSettingsMap* GetHostContentSettingsMap() = 0; 246 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 250 // versa). 251 virtual bool IsSameProfile(Profile* profile) = 0; 252 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; 258 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; 269 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, 278 bool in_memory, 279 content::ProtocolHandlerMap* protocol_handlers, 280 content::URLRequestInterceptorScopedVector request_interceptors) = 0; 281 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; 285 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, 294 // Source unknown. 295 APP_LOCALE_CHANGED_VIA_UNKNOWN 296 }; 297 298 // Changes application locale for a profile. 299 virtual void ChangeAppLocale( 300 const std::string& locale, AppLocaleChangedVia via) = 0; 301 302 // Called after login. 303 virtual void OnLogin() = 0; 304 305 // Initializes Chrome OS's preferences. 306 virtual void InitChromeOSPreferences() = 0; 307 #endif // defined(OS_CHROMEOS) 308 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; 312 313 // Returns the Predictor object used for dns prefetch. 314 virtual chrome_browser_net::Predictor* GetNetworkPredictor() = 0; 315 316 // Returns the DevToolsNetworkController for this profile. 317 virtual DevToolsNetworkController* GetDevToolsNetworkController() = 0; 318 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; 327 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; 333 334 // Returns the home page for this profile. 335 virtual GURL GetHomePage() = 0; 336 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; 340 341 std::string GetDebugName(); 342 343 // Returns whether it is a guest session. 344 virtual bool IsGuestSession() const; 345 346 // Did the user restore the last session? This is set by SessionRestore. set_restored_last_session(bool restored_last_session)347 void set_restored_last_session(bool restored_last_session) { 348 restored_last_session_ = restored_last_session; 349 } restored_last_session()350 bool restored_last_session() const { 351 return restored_last_session_; 352 } 353 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. 358 // 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; 363 364 // Returns how the last session was shutdown. 365 virtual ExitType GetLastSessionExitType() = 0; 366 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. PauseAccessibilityEvents()370 void PauseAccessibilityEvents() { 371 accessibility_pause_level_++; 372 } 373 ResumeAccessibilityEvents()374 void ResumeAccessibilityEvents() { 375 DCHECK_GT(accessibility_pause_level_, 0); 376 accessibility_pause_level_--; 377 } 378 ShouldSendAccessibilityEvents()379 bool ShouldSendAccessibilityEvents() { 380 return 0 == accessibility_pause_level_; 381 } 382 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. 385 bool IsNewProfile(); 386 387 // Checks whether sync is configurable by the user. Returns false if sync is 388 // disabled or controlled by configuration management. 389 bool IsSyncAccessible(); 390 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(); 395 396 // Creates an OffTheRecordProfile which points to this Profile. 397 Profile* CreateOffTheRecordProfile(); 398 399 private: 400 bool restored_last_session_; 401 402 // Used to prevent the notification that this Profile is destroyed from 403 // being sent twice. 404 bool sent_destroyed_notification_; 405 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_; 411 412 DISALLOW_COPY_AND_ASSIGN(Profile); 413 }; 414 415 // The comparator for profile pointers as key in a map. 416 struct ProfileCompare { 417 bool operator()(Profile* a, Profile* b) const; 418 }; 419 420 #if defined(COMPILER_GCC) 421 namespace BASE_HASH_NAMESPACE { 422 423 template<> 424 struct hash<Profile*> { 425 std::size_t operator()(Profile* const& p) const { 426 return reinterpret_cast<std::size_t>(p); 427 } 428 }; 429 430 } // namespace BASE_HASH_NAMESPACE 431 #endif 432 433 #endif // CHROME_BROWSER_PROFILES_PROFILE_H_ 434