profile_io_data.h revision 5c02ac1a9c1b504631c0a3d2b6e737b5d738bae1
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#ifndef CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_ 6#define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/callback_forward.h" 12#include "base/files/file_path.h" 13#include "base/memory/ref_counted.h" 14#include "base/memory/scoped_ptr.h" 15#include "base/memory/weak_ptr.h" 16#include "base/prefs/pref_member.h" 17#include "base/synchronization/lock.h" 18#include "chrome/browser/custom_handlers/protocol_handler_registry.h" 19#include "chrome/browser/io_thread.h" 20#include "chrome/browser/net/chrome_url_request_context.h" 21#include "chrome/browser/profiles/profile.h" 22#include "chrome/browser/profiles/storage_partition_descriptor.h" 23#include "chrome/common/content_settings_types.h" 24#include "content/public/browser/content_browser_client.h" 25#include "content/public/browser/resource_context.h" 26#include "net/cookies/cookie_monster.h" 27#include "net/http/http_network_session.h" 28#include "net/url_request/url_request_job_factory.h" 29 30class ChromeHttpUserAgentSettings; 31class ChromeNetworkDelegate; 32class CookieSettings; 33class HostContentSettingsMap; 34class ManagedModeURLFilter; 35class MediaDeviceIDSalt; 36class ProtocolHandlerRegistry; 37class SigninNamesOnIOThread; 38 39namespace extensions { 40class InfoMap; 41} 42 43namespace net { 44class CookieStore; 45class FraudulentCertificateReporter; 46class FtpTransactionFactory; 47class HttpServerProperties; 48class HttpTransactionFactory; 49class ServerBoundCertService; 50class ProxyConfigService; 51class ProxyService; 52class SSLConfigService; 53class TransportSecurityPersister; 54class TransportSecurityState; 55class URLRequestJobFactoryImpl; 56} // namespace net 57 58namespace policy { 59class PolicyCertVerifier; 60class PolicyHeaderIOHelper; 61class URLBlacklistManager; 62} // namespace policy 63 64// Conceptually speaking, the ProfileIOData represents data that lives on the IO 65// thread that is owned by a Profile, such as, but not limited to, network 66// objects like CookieMonster, HttpTransactionFactory, etc. Profile owns 67// ProfileIOData, but will make sure to delete it on the IO thread (except 68// possibly in unit tests where there is no IO thread). 69class ProfileIOData { 70 public: 71 virtual ~ProfileIOData(); 72 73 static ProfileIOData* FromResourceContext(content::ResourceContext* rc); 74 75 // Returns true if |scheme| is handled in Chrome, or by default handlers in 76 // net::URLRequest. 77 static bool IsHandledProtocol(const std::string& scheme); 78 79 // Returns true if |url| is handled in Chrome, or by default handlers in 80 // net::URLRequest. 81 static bool IsHandledURL(const GURL& url); 82 83 // Utility to install additional WebUI handlers into the |job_factory|. 84 // Ownership of the handlers is transfered from |protocol_handlers| 85 // to the |job_factory|. 86 static void InstallProtocolHandlers( 87 net::URLRequestJobFactoryImpl* job_factory, 88 content::ProtocolHandlerMap* protocol_handlers); 89 90 // Called by Profile. 91 content::ResourceContext* GetResourceContext() const; 92 93 // Initializes the ProfileIOData object and primes the RequestContext 94 // generation. Must be called prior to any of the Get*() methods other than 95 // GetResouceContext or GetMetricsEnabledStateOnIOThread. 96 void Init(content::ProtocolHandlerMap* protocol_handlers, 97 content::ProtocolHandlerScopedVector protocol_interceptors) const; 98 99 ChromeURLRequestContext* GetMainRequestContext() const; 100 ChromeURLRequestContext* GetMediaRequestContext() const; 101 ChromeURLRequestContext* GetExtensionsRequestContext() const; 102 ChromeURLRequestContext* GetIsolatedAppRequestContext( 103 ChromeURLRequestContext* main_context, 104 const StoragePartitionDescriptor& partition_descriptor, 105 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 106 protocol_handler_interceptor, 107 content::ProtocolHandlerMap* protocol_handlers, 108 content::ProtocolHandlerScopedVector protocol_interceptors) const; 109 ChromeURLRequestContext* GetIsolatedMediaRequestContext( 110 ChromeURLRequestContext* app_context, 111 const StoragePartitionDescriptor& partition_descriptor) const; 112 113 // These are useful when the Chrome layer is called from the content layer 114 // with a content::ResourceContext, and they want access to Chrome data for 115 // that profile. 116 extensions::InfoMap* GetExtensionInfoMap() const; 117 CookieSettings* GetCookieSettings() const; 118 HostContentSettingsMap* GetHostContentSettingsMap() const; 119 120 IntegerPrefMember* session_startup_pref() const { 121 return &session_startup_pref_; 122 } 123 124 SigninNamesOnIOThread* signin_names() const { 125 return signin_names_.get(); 126 } 127 128 StringPrefMember* google_services_account_id() const { 129 return &google_services_user_account_id_; 130 } 131 132 StringPrefMember* google_services_username() const { 133 return &google_services_username_; 134 } 135 136 StringPrefMember* google_services_username_pattern() const { 137 return &google_services_username_pattern_; 138 } 139 140 BooleanPrefMember* reverse_autologin_enabled() const { 141 return &reverse_autologin_enabled_; 142 } 143 144 const std::string& reverse_autologin_pending_email() const { 145 return reverse_autologin_pending_email_; 146 } 147 148 void set_reverse_autologin_pending_email(const std::string& email) { 149 reverse_autologin_pending_email_ = email; 150 } 151 152 StringListPrefMember* one_click_signin_rejected_email_list() const { 153 return &one_click_signin_rejected_email_list_; 154 } 155 156 ChromeURLRequestContext* extensions_request_context() const { 157 return extensions_request_context_.get(); 158 } 159 160 BooleanPrefMember* safe_browsing_enabled() const { 161 return &safe_browsing_enabled_; 162 } 163 164 BooleanPrefMember* printing_enabled() const { 165 return &printing_enabled_; 166 } 167 168 BooleanPrefMember* sync_disabled() const { 169 return &sync_disabled_; 170 } 171 172 BooleanPrefMember* signin_allowed() const { 173 return &signin_allowed_; 174 } 175 176 BooleanPrefMember* network_prediction_enabled() const { 177 return &network_prediction_enabled_; 178 } 179 180 content::ResourceContext::SaltCallback GetMediaDeviceIDSalt() const; 181 182 net::TransportSecurityState* transport_security_state() const { 183 return transport_security_state_.get(); 184 } 185 186#if defined(OS_CHROMEOS) 187 std::string username_hash() const { 188 return username_hash_; 189 } 190#endif 191 192 Profile::ProfileType profile_type() const { 193 return profile_type_; 194 } 195 196 bool IsOffTheRecord() const; 197 198 IntegerPrefMember* incognito_availibility() const { 199 return &incognito_availibility_pref_; 200 } 201 202#if defined(ENABLE_CONFIGURATION_POLICY) 203 policy::PolicyHeaderIOHelper* policy_header_helper() const { 204 return policy_header_helper_.get(); 205 } 206#endif 207 208#if defined(ENABLE_MANAGED_USERS) 209 const ManagedModeURLFilter* managed_mode_url_filter() const { 210 return managed_mode_url_filter_.get(); 211 } 212#endif 213 214 // Initialize the member needed to track the metrics enabled state. This is 215 // only to be called on the UI thread. 216 void InitializeMetricsEnabledStateOnUIThread(); 217 218 // Returns whether or not metrics reporting is enabled in the browser instance 219 // on which this profile resides. This is safe for use from the IO thread, and 220 // should only be called from there. 221 bool GetMetricsEnabledStateOnIOThread() const; 222 223 void set_client_cert_store_factory_for_testing( 224 const base::Callback<scoped_ptr<net::ClientCertStore>()>& factory) { 225 client_cert_store_factory_ = factory; 226 } 227 228 protected: 229 // A URLRequestContext for media that owns its HTTP factory, to ensure 230 // it is deleted. 231 class MediaRequestContext : public ChromeURLRequestContext { 232 public: 233 MediaRequestContext(); 234 235 void SetHttpTransactionFactory( 236 scoped_ptr<net::HttpTransactionFactory> http_factory); 237 238 private: 239 virtual ~MediaRequestContext(); 240 241 scoped_ptr<net::HttpTransactionFactory> http_factory_; 242 }; 243 244 // A URLRequestContext for apps that owns its cookie store and HTTP factory, 245 // to ensure they are deleted. 246 class AppRequestContext : public ChromeURLRequestContext { 247 public: 248 AppRequestContext(); 249 250 void SetCookieStore(net::CookieStore* cookie_store); 251 void SetHttpTransactionFactory( 252 scoped_ptr<net::HttpTransactionFactory> http_factory); 253 void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory); 254 255 private: 256 virtual ~AppRequestContext(); 257 258 scoped_refptr<net::CookieStore> cookie_store_; 259 scoped_ptr<net::HttpTransactionFactory> http_factory_; 260 scoped_ptr<net::URLRequestJobFactory> job_factory_; 261 }; 262 263 // Created on the UI thread, read on the IO thread during ProfileIOData lazy 264 // initialization. 265 struct ProfileParams { 266 ProfileParams(); 267 ~ProfileParams(); 268 269 base::FilePath path; 270 IOThread* io_thread; 271 scoped_refptr<CookieSettings> cookie_settings; 272 scoped_refptr<HostContentSettingsMap> host_content_settings_map; 273 scoped_refptr<net::SSLConfigService> ssl_config_service; 274 scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate; 275 scoped_refptr<extensions::InfoMap> extension_info_map; 276 277 // This pointer exists only as a means of conveying a url job factory 278 // pointer from the protocol handler registry on the UI thread to the 279 // the URLRequestContext on the IO thread. The consumer MUST take 280 // ownership of the object by calling release() on this pointer. 281 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 282 protocol_handler_interceptor; 283 284 // We need to initialize the ProxyConfigService from the UI thread 285 // because on linux it relies on initializing things through gconf, 286 // and needs to be on the main thread. 287 scoped_ptr<net::ProxyConfigService> proxy_config_service; 288 289#if defined(ENABLE_MANAGED_USERS) 290 scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter; 291#endif 292 293#if defined(OS_CHROMEOS) 294 std::string username_hash; 295#endif 296 297 // The profile this struct was populated from. It's passed as a void* to 298 // ensure it's not accidently used on the IO thread. Before using it on the 299 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive. 300 void* profile; 301 }; 302 303 explicit ProfileIOData(Profile::ProfileType profile_type); 304 305 static std::string GetSSLSessionCacheShard(); 306 307 void InitializeOnUIThread(Profile* profile); 308 void ApplyProfileParamsToContext(ChromeURLRequestContext* context) const; 309 310 scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults( 311 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory, 312 content::ProtocolHandlerScopedVector protocol_interceptors, 313 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 314 protocol_handler_interceptor, 315 net::NetworkDelegate* network_delegate, 316 net::FtpTransactionFactory* ftp_transaction_factory) const; 317 318 // Called when the profile is destroyed. 319 void ShutdownOnUIThread(); 320 321 // A ServerBoundCertService object is created by a derived class of 322 // ProfileIOData, and the derived class calls this method to set the 323 // server_bound_cert_service_ member and transfers ownership to the base 324 // class. 325 void set_server_bound_cert_service( 326 net::ServerBoundCertService* server_bound_cert_service) const; 327 328 ChromeNetworkDelegate* network_delegate() const { 329 return network_delegate_.get(); 330 } 331 332 net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const { 333 return fraudulent_certificate_reporter_.get(); 334 } 335 336 net::ProxyService* proxy_service() const { 337 return proxy_service_.get(); 338 } 339 340 base::WeakPtr<net::HttpServerProperties> http_server_properties() const; 341 342 void set_http_server_properties( 343 scoped_ptr<net::HttpServerProperties> http_server_properties) const; 344 345 ChromeURLRequestContext* main_request_context() const { 346 return main_request_context_.get(); 347 } 348 349 bool initialized() const { 350 return initialized_; 351 } 352 353 // Destroys the ResourceContext first, to cancel any URLRequests that are 354 // using it still, before we destroy the member variables that those 355 // URLRequests may be accessing. 356 void DestroyResourceContext(); 357 358 // Fills in fields of params using values from main_request_context_ and the 359 // IOThread associated with profile_params. 360 void PopulateNetworkSessionParams( 361 const ProfileParams* profile_params, 362 net::HttpNetworkSession::Params* params) const; 363 364 void SetCookieSettingsForTesting(CookieSettings* cookie_settings); 365 366 void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names); 367 368 private: 369 class ResourceContext : public content::ResourceContext { 370 public: 371 explicit ResourceContext(ProfileIOData* io_data); 372 virtual ~ResourceContext(); 373 374 // ResourceContext implementation: 375 virtual net::HostResolver* GetHostResolver() OVERRIDE; 376 virtual net::URLRequestContext* GetRequestContext() OVERRIDE; 377 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE; 378 virtual void CreateKeygenHandler( 379 uint32 key_size_in_bits, 380 const std::string& challenge_string, 381 const GURL& url, 382 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback) 383 OVERRIDE; 384 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE; 385 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE; 386 virtual SaltCallback GetMediaDeviceIDSalt() OVERRIDE; 387 388 private: 389 friend class ProfileIOData; 390 391 // Helper method that returns true if |type| is allowed for |origin|, false 392 // otherwise. 393 bool AllowContentAccess(const GURL& origin, ContentSettingsType type); 394 395 ProfileIOData* const io_data_; 396 397 net::HostResolver* host_resolver_; 398 net::URLRequestContext* request_context_; 399 }; 400 401 typedef std::map<StoragePartitionDescriptor, 402 ChromeURLRequestContext*, 403 StoragePartitionDescriptorLess> 404 URLRequestContextMap; 405 406 // -------------------------------------------- 407 // Virtual interface for subtypes to implement: 408 // -------------------------------------------- 409 410 // Does the actual initialization of the ProfileIOData subtype. Subtypes 411 // should use the static helper functions above to implement this. 412 virtual void InitializeInternal( 413 ProfileParams* profile_params, 414 content::ProtocolHandlerMap* protocol_handlers, 415 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0; 416 417 // Initializes the RequestContext for extensions. 418 virtual void InitializeExtensionsRequestContext( 419 ProfileParams* profile_params) const = 0; 420 // Does an on-demand initialization of a RequestContext for the given 421 // isolated app. 422 virtual ChromeURLRequestContext* InitializeAppRequestContext( 423 ChromeURLRequestContext* main_context, 424 const StoragePartitionDescriptor& details, 425 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 426 protocol_handler_interceptor, 427 content::ProtocolHandlerMap* protocol_handlers, 428 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0; 429 430 // Does an on-demand initialization of a media RequestContext for the given 431 // isolated app. 432 virtual ChromeURLRequestContext* InitializeMediaRequestContext( 433 ChromeURLRequestContext* original_context, 434 const StoragePartitionDescriptor& details) const = 0; 435 436 // These functions are used to transfer ownership of the lazily initialized 437 // context from ProfileIOData to the URLRequestContextGetter. 438 virtual ChromeURLRequestContext* 439 AcquireMediaRequestContext() const = 0; 440 virtual ChromeURLRequestContext* AcquireIsolatedAppRequestContext( 441 ChromeURLRequestContext* main_context, 442 const StoragePartitionDescriptor& partition_descriptor, 443 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory> 444 protocol_handler_interceptor, 445 content::ProtocolHandlerMap* protocol_handlers, 446 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0; 447 virtual ChromeURLRequestContext* 448 AcquireIsolatedMediaRequestContext( 449 ChromeURLRequestContext* app_context, 450 const StoragePartitionDescriptor& partition_descriptor) const = 0; 451 452 // The order *DOES* matter for the majority of these member variables, so 453 // don't move them around unless you know what you're doing! 454 // General rules: 455 // * ResourceContext references the URLRequestContexts, so 456 // URLRequestContexts must outlive ResourceContext, hence ResourceContext 457 // should be destroyed first. 458 // * URLRequestContexts reference a whole bunch of members, so 459 // URLRequestContext needs to be destroyed before them. 460 // * Therefore, ResourceContext should be listed last, and then the 461 // URLRequestContexts, and then the URLRequestContext members. 462 // * Note that URLRequestContext members have a directed dependency graph 463 // too, so they must themselves be ordered correctly. 464 465 // Tracks whether or not we've been lazily initialized. 466 mutable bool initialized_; 467 468 // Data from the UI thread from the Profile, used to initialize ProfileIOData. 469 // Deleted after lazy initialization. 470 mutable scoped_ptr<ProfileParams> profile_params_; 471 472 // Provides access to the email addresses of all signed in profiles. 473 mutable scoped_ptr<SigninNamesOnIOThread> signin_names_; 474 475 // Used for testing. 476 mutable base::Callback<scoped_ptr<net::ClientCertStore>()> 477 client_cert_store_factory_; 478 479 mutable StringPrefMember google_services_user_account_id_; 480 mutable StringPrefMember google_services_username_; 481 mutable StringPrefMember google_services_username_pattern_; 482 mutable BooleanPrefMember reverse_autologin_enabled_; 483 484 // During the reverse autologin request chain processing, this member saves 485 // the email of the google account that is being signed into. 486 std::string reverse_autologin_pending_email_; 487 488 mutable StringListPrefMember one_click_signin_rejected_email_list_; 489 490 mutable scoped_refptr<MediaDeviceIDSalt> media_device_id_salt_; 491 492 // Member variables which are pointed to by the various context objects. 493 mutable BooleanPrefMember enable_referrers_; 494 mutable BooleanPrefMember enable_do_not_track_; 495 mutable BooleanPrefMember force_safesearch_; 496 mutable BooleanPrefMember safe_browsing_enabled_; 497 mutable BooleanPrefMember printing_enabled_; 498 mutable BooleanPrefMember sync_disabled_; 499 mutable BooleanPrefMember signin_allowed_; 500 mutable BooleanPrefMember network_prediction_enabled_; 501 // TODO(marja): Remove session_startup_pref_ if no longer needed. 502 mutable IntegerPrefMember session_startup_pref_; 503 mutable BooleanPrefMember quick_check_enabled_; 504 mutable IntegerPrefMember incognito_availibility_pref_; 505 506 // The state of metrics reporting in the browser that this profile runs on. 507 // Unfortunately, since ChromeOS has a separate representation of this state, 508 // we need to make one available based on the platform. 509#if defined(OS_CHROMEOS) 510 bool enable_metrics_; 511#else 512 BooleanPrefMember enable_metrics_; 513#endif 514 515#if defined(ENABLE_CONFIGURATION_POLICY) 516 // Pointed to by NetworkDelegate. 517 mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_; 518 mutable scoped_ptr<policy::PolicyHeaderIOHelper> policy_header_helper_; 519#endif 520 521 // Pointed to by URLRequestContext. 522 mutable scoped_refptr<extensions::InfoMap> extension_info_map_; 523 mutable scoped_ptr<net::ServerBoundCertService> server_bound_cert_service_; 524 mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_; 525 mutable scoped_ptr<net::FraudulentCertificateReporter> 526 fraudulent_certificate_reporter_; 527 mutable scoped_ptr<net::ProxyService> proxy_service_; 528 mutable scoped_ptr<net::TransportSecurityState> transport_security_state_; 529 mutable scoped_ptr<net::HttpServerProperties> 530 http_server_properties_; 531#if defined(OS_CHROMEOS) 532 mutable scoped_ptr<policy::PolicyCertVerifier> cert_verifier_; 533 mutable std::string username_hash_; 534#endif 535 536 mutable scoped_ptr<net::TransportSecurityPersister> 537 transport_security_persister_; 538 539 // These are only valid in between LazyInitialize() and their accessor being 540 // called. 541 mutable scoped_ptr<ChromeURLRequestContext> main_request_context_; 542 mutable scoped_ptr<ChromeURLRequestContext> extensions_request_context_; 543 // One URLRequestContext per isolated app for main and media requests. 544 mutable URLRequestContextMap app_request_context_map_; 545 mutable URLRequestContextMap isolated_media_request_context_map_; 546 547 mutable scoped_ptr<ResourceContext> resource_context_; 548 549 mutable scoped_refptr<CookieSettings> cookie_settings_; 550 551 mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 552 553 mutable scoped_ptr<ChromeHttpUserAgentSettings> 554 chrome_http_user_agent_settings_; 555 556#if defined(ENABLE_MANAGED_USERS) 557 mutable scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter_; 558#endif 559 560 // TODO(jhawkins): Remove once crbug.com/102004 is fixed. 561 bool initialized_on_UI_thread_; 562 563 const Profile::ProfileType profile_type_; 564 565 DISALLOW_COPY_AND_ASSIGN(ProfileIOData); 566}; 567 568#endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_ 569