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