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