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