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