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