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.
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
12 #include "base/basictypes.h"
13 #include "base/callback_forward.h"
14 #include "base/files/file_path.h"
15 #include "base/memory/ref_counted.h"
16 #include "base/memory/scoped_ptr.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/prefs/pref_member.h"
19 #include "base/synchronization/lock.h"
20 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
21 #include "chrome/browser/devtools/devtools_network_controller_handle.h"
22 #include "chrome/browser/io_thread.h"
23 #include "chrome/browser/profiles/profile.h"
24 #include "chrome/browser/profiles/storage_partition_descriptor.h"
25 #include "components/content_settings/core/common/content_settings_types.h"
26 #include "content/public/browser/content_browser_client.h"
27 #include "content/public/browser/resource_context.h"
28 #include "net/cookies/cookie_monster.h"
29 #include "net/http/http_cache.h"
30 #include "net/http/http_network_session.h"
31 #include "net/url_request/url_request_context.h"
32 #include "net/url_request/url_request_interceptor.h"
33 #include "net/url_request/url_request_job_factory.h"
35 class ChromeHttpUserAgentSettings
;
36 class ChromeNetworkDelegate
;
37 class ChromeURLRequestContextGetter
;
38 class HostContentSettingsMap
;
39 class MediaDeviceIDSalt
;
40 class ProtocolHandlerRegistry
;
41 class SupervisedUserURLFilter
;
44 class CertificateProvider
;
47 namespace chrome_browser_net
{
48 class ResourcePrefetchPredictorObserver
;
51 namespace content_settings
{
55 namespace data_reduction_proxy
{
56 class DataReductionProxyIOData
;
59 namespace extensions
{
60 class ExtensionThrottleManager
;
65 class CertificateReportSender
;
67 class ChannelIDService
;
69 class FtpTransactionFactory
;
70 class HttpServerProperties
;
71 class HttpTransactionFactory
;
72 class ProxyConfigService
;
74 class SSLConfigService
;
75 class TransportSecurityPersister
;
76 class TransportSecurityState
;
77 class URLRequestJobFactoryImpl
;
81 class PolicyCertVerifier
;
82 class PolicyHeaderIOHelper
;
83 class URLBlacklistManager
;
86 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
87 // thread that is owned by a Profile, such as, but not limited to, network
88 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
89 // ProfileIOData, but will make sure to delete it on the IO thread (except
90 // possibly in unit tests where there is no IO thread).
93 typedef std::vector
<scoped_refptr
<ChromeURLRequestContextGetter
>>
94 ChromeURLRequestContextGetterVector
;
96 virtual ~ProfileIOData();
98 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
100 // Returns true if |scheme| is handled in Chrome, or by default handlers in
102 static bool IsHandledProtocol(const std::string
& scheme
);
104 // Returns true if |url| is handled in Chrome, or by default handlers in
106 static bool IsHandledURL(const GURL
& url
);
108 // Utility to install additional WebUI handlers into the |job_factory|.
109 // Ownership of the handlers is transfered from |protocol_handlers|
110 // to the |job_factory|.
111 static void InstallProtocolHandlers(
112 net::URLRequestJobFactoryImpl
* job_factory
,
113 content::ProtocolHandlerMap
* protocol_handlers
);
115 // Sets a global CertVerifier to use when initializing all profiles.
116 static void SetCertVerifierForTesting(net::CertVerifier
* cert_verifier
);
118 // Called by Profile.
119 content::ResourceContext
* GetResourceContext() const;
121 // Initializes the ProfileIOData object and primes the RequestContext
122 // generation. Must be called prior to any of the Get*() methods other than
123 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
125 content::ProtocolHandlerMap
* protocol_handlers
,
126 content::URLRequestInterceptorScopedVector request_interceptors
) const;
128 net::URLRequestContext
* GetMainRequestContext() const;
129 net::URLRequestContext
* GetMediaRequestContext() const;
130 net::URLRequestContext
* GetExtensionsRequestContext() const;
131 net::URLRequestContext
* GetIsolatedAppRequestContext(
132 net::URLRequestContext
* main_context
,
133 const StoragePartitionDescriptor
& partition_descriptor
,
134 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
135 protocol_handler_interceptor
,
136 content::ProtocolHandlerMap
* protocol_handlers
,
137 content::URLRequestInterceptorScopedVector request_interceptors
) const;
138 net::URLRequestContext
* GetIsolatedMediaRequestContext(
139 net::URLRequestContext
* app_context
,
140 const StoragePartitionDescriptor
& partition_descriptor
) const;
142 // These are useful when the Chrome layer is called from the content layer
143 // with a content::ResourceContext, and they want access to Chrome data for
145 extensions::InfoMap
* GetExtensionInfoMap() const;
146 extensions::ExtensionThrottleManager
* GetExtensionThrottleManager() const;
147 content_settings::CookieSettings
* GetCookieSettings() const;
148 HostContentSettingsMap
* GetHostContentSettingsMap() const;
150 IntegerPrefMember
* session_startup_pref() const {
151 return &session_startup_pref_
;
154 StringPrefMember
* google_services_account_id() const {
155 return &google_services_user_account_id_
;
158 net::URLRequestContext
* extensions_request_context() const {
159 return extensions_request_context_
.get();
162 BooleanPrefMember
* safe_browsing_enabled() const {
163 return &safe_browsing_enabled_
;
166 BooleanPrefMember
* sync_disabled() const {
167 return &sync_disabled_
;
170 BooleanPrefMember
* signin_allowed() const {
171 return &signin_allowed_
;
174 IntegerPrefMember
* network_prediction_options() const {
175 return &network_prediction_options_
;
178 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
180 DevToolsNetworkControllerHandle
* network_controller_handle() const {
181 return &network_controller_handle_
;
184 net::TransportSecurityState
* transport_security_state() const {
185 return transport_security_state_
.get();
188 #if defined(OS_CHROMEOS)
189 std::string
username_hash() const {
190 return username_hash_
;
193 bool use_system_key_slot() const { return use_system_key_slot_
; }
196 Profile::ProfileType
profile_type() const {
197 return profile_type_
;
200 bool IsOffTheRecord() const;
202 IntegerPrefMember
* incognito_availibility() const {
203 return &incognito_availibility_pref_
;
206 chrome_browser_net::ResourcePrefetchPredictorObserver
*
207 resource_prefetch_predictor_observer() const {
208 return resource_prefetch_predictor_observer_
.get();
211 #if defined(ENABLE_CONFIGURATION_POLICY)
212 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
213 return policy_header_helper_
.get();
217 #if defined(ENABLE_SUPERVISED_USERS)
218 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
219 return supervised_user_url_filter_
.get();
223 // Initialize the member needed to track the metrics enabled state. This is
224 // only to be called on the UI thread.
225 void InitializeMetricsEnabledStateOnUIThread();
227 // Returns whether or not metrics reporting is enabled in the browser instance
228 // on which this profile resides. This is safe for use from the IO thread, and
229 // should only be called from there.
230 bool GetMetricsEnabledStateOnIOThread() const;
232 void set_client_cert_store_factory_for_testing(
233 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
234 client_cert_store_factory_
= factory
;
237 bool IsDataReductionProxyEnabled() const;
239 data_reduction_proxy::DataReductionProxyIOData
*
240 data_reduction_proxy_io_data() const {
241 return data_reduction_proxy_io_data_
.get();
245 // A URLRequestContext for media that owns its HTTP factory, to ensure
247 class MediaRequestContext
: public net::URLRequestContext
{
249 MediaRequestContext();
251 void SetHttpTransactionFactory(
252 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
255 ~MediaRequestContext() override
;
257 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
260 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
261 // to ensure they are deleted.
262 class AppRequestContext
: public net::URLRequestContext
{
266 void SetCookieStore(net::CookieStore
* cookie_store
);
267 void SetHttpTransactionFactory(
268 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
269 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
272 ~AppRequestContext() override
;
274 scoped_refptr
<net::CookieStore
> cookie_store_
;
275 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
276 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
279 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
281 struct ProfileParams
{
287 scoped_refptr
<content_settings::CookieSettings
> cookie_settings
;
288 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
289 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
290 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
291 #if defined(ENABLE_EXTENSIONS)
292 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
294 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
295 resource_prefetch_predictor_observer_
;
297 // This pointer exists only as a means of conveying a url job factory
298 // pointer from the protocol handler registry on the UI thread to the
299 // the URLRequestContext on the IO thread. The consumer MUST take
300 // ownership of the object by calling release() on this pointer.
301 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
302 protocol_handler_interceptor
;
304 // Holds the URLRequestInterceptor pointer that is created on the UI thread
305 // and then passed to the list of request_interceptors on the IO thread.
306 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
308 // We need to initialize the ProxyConfigService from the UI thread
309 // because on linux it relies on initializing things through gconf,
310 // and needs to be on the main thread.
311 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
313 #if defined(ENABLE_SUPERVISED_USERS)
314 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
317 #if defined(OS_CHROMEOS)
318 std::string username_hash
;
319 bool use_system_key_slot
;
320 scoped_ptr
<chromeos::CertificateProvider
> certificate_provider
;
323 // The profile this struct was populated from. It's passed as a void* to
324 // ensure it's not accidently used on the IO thread. Before using it on the
325 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
329 explicit ProfileIOData(Profile::ProfileType profile_type
);
331 static std::string
GetSSLSessionCacheShard();
333 void InitializeOnUIThread(Profile
* profile
);
334 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
336 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
337 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
338 content::URLRequestInterceptorScopedVector request_interceptors
,
339 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
340 protocol_handler_interceptor
,
341 net::NetworkDelegate
* network_delegate
,
342 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
344 // Called when the Profile is destroyed. |context_getters| must include all
345 // URLRequestContextGetters that refer to the ProfileIOData's
346 // URLRequestContexts. Triggers destruction of the ProfileIOData and shuts
347 // down |context_getters| safely on the IO thread.
348 // TODO(mmenke): Passing all those URLRequestContextGetters around like this
349 // is really silly. Can we do something cleaner?
350 void ShutdownOnUIThread(
351 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
353 // A ChannelIDService object is created by a derived class of
354 // ProfileIOData, and the derived class calls this method to set the
355 // channel_id_service_ member and transfers ownership to the base
357 void set_channel_id_service(
358 net::ChannelIDService
* channel_id_service
) const;
360 void set_data_reduction_proxy_io_data(
361 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
362 data_reduction_proxy_io_data
) const;
364 net::ProxyService
* proxy_service() const {
365 return proxy_service_
.get();
368 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
370 void set_http_server_properties(
371 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
373 net::URLRequestContext
* main_request_context() const {
374 return main_request_context_
.get();
377 bool initialized() const {
381 // Destroys the ResourceContext first, to cancel any URLRequests that are
382 // using it still, before we destroy the member variables that those
383 // URLRequests may be accessing.
384 void DestroyResourceContext();
386 // Creates network session and main network transaction factory.
387 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
388 const ProfileParams
* profile_params
,
389 net::HttpCache::BackendFactory
* main_backend
) const;
391 // Creates network transaction factory.
392 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
393 net::HttpNetworkSession
* shared_session
,
394 net::HttpCache::BackendFactory
* backend
) const;
396 void SetCookieSettingsForTesting(
397 content_settings::CookieSettings
* cookie_settings
);
400 class ResourceContext
: public content::ResourceContext
{
402 explicit ResourceContext(ProfileIOData
* io_data
);
403 ~ResourceContext() override
;
405 // ResourceContext implementation:
406 net::HostResolver
* GetHostResolver() override
;
407 net::URLRequestContext
* GetRequestContext() override
;
408 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
409 void CreateKeygenHandler(
410 uint32 key_size_in_bits
,
411 const std::string
& challenge_string
,
413 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
415 SaltCallback
GetMediaDeviceIDSalt() override
;
418 friend class ProfileIOData
;
420 ProfileIOData
* const io_data_
;
422 net::HostResolver
* host_resolver_
;
423 net::URLRequestContext
* request_context_
;
426 typedef std::map
<StoragePartitionDescriptor
,
427 net::URLRequestContext
*,
428 StoragePartitionDescriptorLess
>
429 URLRequestContextMap
;
431 // --------------------------------------------
432 // Virtual interface for subtypes to implement:
433 // --------------------------------------------
435 // Does the actual initialization of the ProfileIOData subtype. Subtypes
436 // should use the static helper functions above to implement this.
437 virtual void InitializeInternal(
438 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
439 ProfileParams
* profile_params
,
440 content::ProtocolHandlerMap
* protocol_handlers
,
441 content::URLRequestInterceptorScopedVector
442 request_interceptors
) const = 0;
444 // Initializes the RequestContext for extensions.
445 virtual void InitializeExtensionsRequestContext(
446 ProfileParams
* profile_params
) const = 0;
447 // Does an on-demand initialization of a RequestContext for the given
449 virtual net::URLRequestContext
* InitializeAppRequestContext(
450 net::URLRequestContext
* main_context
,
451 const StoragePartitionDescriptor
& details
,
452 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
453 protocol_handler_interceptor
,
454 content::ProtocolHandlerMap
* protocol_handlers
,
455 content::URLRequestInterceptorScopedVector
456 request_interceptors
) const = 0;
458 // Does an on-demand initialization of a media RequestContext for the given
460 virtual net::URLRequestContext
* InitializeMediaRequestContext(
461 net::URLRequestContext
* original_context
,
462 const StoragePartitionDescriptor
& details
) const = 0;
464 // These functions are used to transfer ownership of the lazily initialized
465 // context from ProfileIOData to the URLRequestContextGetter.
466 virtual net::URLRequestContext
*
467 AcquireMediaRequestContext() const = 0;
468 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
469 net::URLRequestContext
* main_context
,
470 const StoragePartitionDescriptor
& partition_descriptor
,
471 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
472 protocol_handler_interceptor
,
473 content::ProtocolHandlerMap
* protocol_handlers
,
474 content::URLRequestInterceptorScopedVector
475 request_interceptors
) const = 0;
476 virtual net::URLRequestContext
*
477 AcquireIsolatedMediaRequestContext(
478 net::URLRequestContext
* app_context
,
479 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
481 // The order *DOES* matter for the majority of these member variables, so
482 // don't move them around unless you know what you're doing!
484 // * ResourceContext references the URLRequestContexts, so
485 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
486 // should be destroyed first.
487 // * URLRequestContexts reference a whole bunch of members, so
488 // URLRequestContext needs to be destroyed before them.
489 // * Therefore, ResourceContext should be listed last, and then the
490 // URLRequestContexts, and then the URLRequestContext members.
491 // * Note that URLRequestContext members have a directed dependency graph
492 // too, so they must themselves be ordered correctly.
494 // Tracks whether or not we've been lazily initialized.
495 mutable bool initialized_
;
497 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
498 // Deleted after lazy initialization.
499 mutable scoped_ptr
<ProfileParams
> profile_params_
;
502 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
503 client_cert_store_factory_
;
505 mutable StringPrefMember google_services_user_account_id_
;
507 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
509 // Member variables which are pointed to by the various context objects.
510 mutable BooleanPrefMember enable_referrers_
;
511 mutable BooleanPrefMember enable_do_not_track_
;
512 mutable BooleanPrefMember force_google_safesearch_
;
513 mutable BooleanPrefMember force_youtube_safety_mode_
;
514 mutable BooleanPrefMember safe_browsing_enabled_
;
515 mutable BooleanPrefMember sync_disabled_
;
516 mutable BooleanPrefMember signin_allowed_
;
517 mutable IntegerPrefMember network_prediction_options_
;
518 // TODO(marja): Remove session_startup_pref_ if no longer needed.
519 mutable IntegerPrefMember session_startup_pref_
;
520 mutable BooleanPrefMember quick_check_enabled_
;
521 mutable IntegerPrefMember incognito_availibility_pref_
;
523 // The state of metrics reporting in the browser that this profile runs on.
524 // Unfortunately, since ChromeOS has a separate representation of this state,
525 // we need to make one available based on the platform.
526 #if defined(OS_CHROMEOS)
527 bool enable_metrics_
;
529 BooleanPrefMember enable_metrics_
;
532 #if defined(ENABLE_CONFIGURATION_POLICY)
533 // Pointed to by NetworkDelegate.
534 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
535 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
538 // Pointed to by URLRequestContext.
539 #if defined(ENABLE_EXTENSIONS)
540 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
542 mutable scoped_ptr
<net::ChannelIDService
> channel_id_service_
;
544 mutable scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
545 data_reduction_proxy_io_data_
;
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 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
553 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
554 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
555 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
556 mutable std::string username_hash_
;
557 mutable bool use_system_key_slot_
;
558 mutable scoped_ptr
<chromeos::CertificateProvider
> certificate_provider_
;
561 mutable scoped_ptr
<net::TransportSecurityPersister
>
562 transport_security_persister_
;
563 mutable scoped_ptr
<net::CertificateReportSender
> certificate_report_sender_
;
565 // These are only valid in between LazyInitialize() and their accessor being
567 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
568 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
569 // One URLRequestContext per isolated app for main and media requests.
570 mutable URLRequestContextMap app_request_context_map_
;
571 mutable URLRequestContextMap isolated_media_request_context_map_
;
573 mutable scoped_ptr
<ResourceContext
> resource_context_
;
575 mutable scoped_refptr
<content_settings::CookieSettings
> cookie_settings_
;
577 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
579 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
580 resource_prefetch_predictor_observer_
;
582 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
583 chrome_http_user_agent_settings_
;
585 #if defined(ENABLE_SUPERVISED_USERS)
586 mutable scoped_refptr
<const SupervisedUserURLFilter
>
587 supervised_user_url_filter_
;
590 #if defined(ENABLE_EXTENSIONS)
591 // Is NULL if switches::kDisableExtensionsHttpThrottling is on.
592 mutable scoped_ptr
<extensions::ExtensionThrottleManager
>
593 extension_throttle_manager_
;
596 mutable DevToolsNetworkControllerHandle network_controller_handle_
;
598 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
599 bool initialized_on_UI_thread_
;
601 const Profile::ProfileType profile_type_
;
603 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
606 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_