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/io_thread.h"
22 #include "chrome/browser/profiles/profile.h"
23 #include "chrome/browser/profiles/storage_partition_descriptor.h"
24 #include "components/content_settings/core/common/content_settings_types.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_interceptor.h"
32 #include "net/url_request/url_request_job_factory.h"
34 class ChromeHttpUserAgentSettings
;
35 class ChromeNetworkDelegate
;
36 class ChromeURLRequestContextGetter
;
37 class DevToolsNetworkController
;
38 class HostContentSettingsMap
;
39 class MediaDeviceIDSalt
;
40 class ProtocolHandlerRegistry
;
41 class SupervisedUserURLFilter
;
43 namespace chrome_browser_net
{
44 class ResourcePrefetchPredictorObserver
;
47 namespace content_settings
{
51 namespace data_reduction_proxy
{
52 class DataReductionProxyIOData
;
55 namespace extensions
{
56 class ExtensionThrottleManager
;
61 class CertificateReportSender
;
63 class ChannelIDService
;
65 class FraudulentCertificateReporter
;
66 class FtpTransactionFactory
;
67 class HttpServerProperties
;
68 class HttpTransactionFactory
;
69 class ProxyConfigService
;
71 class SSLConfigService
;
72 class TransportSecurityPersister
;
73 class TransportSecurityState
;
74 class URLRequestJobFactoryImpl
;
78 class PolicyCertVerifier
;
79 class PolicyHeaderIOHelper
;
80 class URLBlacklistManager
;
83 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
84 // thread that is owned by a Profile, such as, but not limited to, network
85 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
86 // ProfileIOData, but will make sure to delete it on the IO thread (except
87 // possibly in unit tests where there is no IO thread).
90 typedef std::vector
<scoped_refptr
<ChromeURLRequestContextGetter
>>
91 ChromeURLRequestContextGetterVector
;
93 virtual ~ProfileIOData();
95 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
97 // Returns true if |scheme| is handled in Chrome, or by default handlers in
99 static bool IsHandledProtocol(const std::string
& scheme
);
101 // Returns true if |url| is handled in Chrome, or by default handlers in
103 static bool IsHandledURL(const GURL
& url
);
105 // Utility to install additional WebUI handlers into the |job_factory|.
106 // Ownership of the handlers is transfered from |protocol_handlers|
107 // to the |job_factory|.
108 static void InstallProtocolHandlers(
109 net::URLRequestJobFactoryImpl
* job_factory
,
110 content::ProtocolHandlerMap
* protocol_handlers
);
112 // Sets a global CertVerifier to use when initializing all profiles.
113 static void SetCertVerifierForTesting(net::CertVerifier
* cert_verifier
);
115 // Called by Profile.
116 content::ResourceContext
* GetResourceContext() const;
118 // Initializes the ProfileIOData object and primes the RequestContext
119 // generation. Must be called prior to any of the Get*() methods other than
120 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
122 content::ProtocolHandlerMap
* protocol_handlers
,
123 content::URLRequestInterceptorScopedVector request_interceptors
) const;
125 net::URLRequestContext
* GetMainRequestContext() const;
126 net::URLRequestContext
* GetMediaRequestContext() const;
127 net::URLRequestContext
* GetExtensionsRequestContext() const;
128 net::URLRequestContext
* GetIsolatedAppRequestContext(
129 net::URLRequestContext
* main_context
,
130 const StoragePartitionDescriptor
& partition_descriptor
,
131 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
132 protocol_handler_interceptor
,
133 content::ProtocolHandlerMap
* protocol_handlers
,
134 content::URLRequestInterceptorScopedVector request_interceptors
) const;
135 net::URLRequestContext
* GetIsolatedMediaRequestContext(
136 net::URLRequestContext
* app_context
,
137 const StoragePartitionDescriptor
& partition_descriptor
) const;
139 // These are useful when the Chrome layer is called from the content layer
140 // with a content::ResourceContext, and they want access to Chrome data for
142 extensions::InfoMap
* GetExtensionInfoMap() const;
143 extensions::ExtensionThrottleManager
* GetExtensionThrottleManager() const;
144 content_settings::CookieSettings
* GetCookieSettings() const;
145 HostContentSettingsMap
* GetHostContentSettingsMap() const;
147 IntegerPrefMember
* session_startup_pref() const {
148 return &session_startup_pref_
;
151 StringPrefMember
* google_services_account_id() const {
152 return &google_services_user_account_id_
;
155 net::URLRequestContext
* extensions_request_context() const {
156 return extensions_request_context_
.get();
159 BooleanPrefMember
* safe_browsing_enabled() const {
160 return &safe_browsing_enabled_
;
163 BooleanPrefMember
* sync_disabled() const {
164 return &sync_disabled_
;
167 BooleanPrefMember
* signin_allowed() const {
168 return &signin_allowed_
;
171 IntegerPrefMember
* network_prediction_options() const {
172 return &network_prediction_options_
;
175 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
177 DevToolsNetworkController
* network_controller() const {
178 return network_controller_
.get();
181 net::TransportSecurityState
* transport_security_state() const {
182 return transport_security_state_
.get();
185 #if defined(OS_CHROMEOS)
186 std::string
username_hash() const {
187 return username_hash_
;
190 bool use_system_key_slot() const { return use_system_key_slot_
; }
193 Profile::ProfileType
profile_type() const {
194 return profile_type_
;
197 bool IsOffTheRecord() const;
199 IntegerPrefMember
* incognito_availibility() const {
200 return &incognito_availibility_pref_
;
203 chrome_browser_net::ResourcePrefetchPredictorObserver
*
204 resource_prefetch_predictor_observer() const {
205 return resource_prefetch_predictor_observer_
.get();
208 #if defined(ENABLE_CONFIGURATION_POLICY)
209 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
210 return policy_header_helper_
.get();
214 #if defined(ENABLE_SUPERVISED_USERS)
215 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
216 return supervised_user_url_filter_
.get();
220 // Initialize the member needed to track the metrics enabled state. This is
221 // only to be called on the UI thread.
222 void InitializeMetricsEnabledStateOnUIThread();
224 // Returns whether or not metrics reporting is enabled in the browser instance
225 // on which this profile resides. This is safe for use from the IO thread, and
226 // should only be called from there.
227 bool GetMetricsEnabledStateOnIOThread() const;
229 void set_client_cert_store_factory_for_testing(
230 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
231 client_cert_store_factory_
= factory
;
234 bool IsDataReductionProxyEnabled() const;
236 data_reduction_proxy::DataReductionProxyIOData
*
237 data_reduction_proxy_io_data() const {
238 return data_reduction_proxy_io_data_
.get();
242 // A URLRequestContext for media that owns its HTTP factory, to ensure
244 class MediaRequestContext
: public net::URLRequestContext
{
246 MediaRequestContext();
248 void SetHttpTransactionFactory(
249 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
252 ~MediaRequestContext() override
;
254 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
257 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
258 // to ensure they are deleted.
259 class AppRequestContext
: public net::URLRequestContext
{
263 void SetCookieStore(net::CookieStore
* cookie_store
);
264 void SetHttpTransactionFactory(
265 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
266 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
269 ~AppRequestContext() override
;
271 scoped_refptr
<net::CookieStore
> cookie_store_
;
272 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
273 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
276 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
278 struct ProfileParams
{
284 scoped_refptr
<content_settings::CookieSettings
> cookie_settings
;
285 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
286 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
287 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
288 #if defined(ENABLE_EXTENSIONS)
289 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
291 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
292 resource_prefetch_predictor_observer_
;
294 // This pointer exists only as a means of conveying a url job factory
295 // pointer from the protocol handler registry on the UI thread to the
296 // the URLRequestContext on the IO thread. The consumer MUST take
297 // ownership of the object by calling release() on this pointer.
298 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
299 protocol_handler_interceptor
;
301 // Holds the URLRequestInterceptor pointer that is created on the UI thread
302 // and then passed to the list of request_interceptors on the IO thread.
303 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
305 // We need to initialize the ProxyConfigService from the UI thread
306 // because on linux it relies on initializing things through gconf,
307 // and needs to be on the main thread.
308 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
310 #if defined(ENABLE_SUPERVISED_USERS)
311 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
314 #if defined(OS_CHROMEOS)
315 std::string username_hash
;
316 bool use_system_key_slot
;
319 // The profile this struct was populated from. It's passed as a void* to
320 // ensure it's not accidently used on the IO thread. Before using it on the
321 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
325 explicit ProfileIOData(Profile::ProfileType profile_type
);
327 static std::string
GetSSLSessionCacheShard();
329 void InitializeOnUIThread(Profile
* profile
);
330 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
332 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
333 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
334 content::URLRequestInterceptorScopedVector request_interceptors
,
335 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
336 protocol_handler_interceptor
,
337 net::NetworkDelegate
* network_delegate
,
338 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
340 // Called when the Profile is destroyed. |context_getters| must include all
341 // URLRequestContextGetters that refer to the ProfileIOData's
342 // URLRequestContexts. Triggers destruction of the ProfileIOData and shuts
343 // down |context_getters| safely on the IO thread.
344 // TODO(mmenke): Passing all those URLRequestContextGetters around like this
345 // is really silly. Can we do something cleaner?
346 void ShutdownOnUIThread(
347 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
349 // A ChannelIDService object is created by a derived class of
350 // ProfileIOData, and the derived class calls this method to set the
351 // channel_id_service_ member and transfers ownership to the base
353 void set_channel_id_service(
354 net::ChannelIDService
* channel_id_service
) const;
356 void set_data_reduction_proxy_io_data(
357 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
358 data_reduction_proxy_io_data
) const;
360 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
361 return fraudulent_certificate_reporter_
.get();
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::FraudulentCertificateReporter
>
548 fraudulent_certificate_reporter_
;
549 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
550 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
551 mutable scoped_ptr
<net::HttpServerProperties
>
552 http_server_properties_
;
553 #if defined(OS_CHROMEOS)
554 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
555 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
556 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
557 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
558 mutable std::string username_hash_
;
559 mutable bool use_system_key_slot_
;
562 mutable scoped_ptr
<net::TransportSecurityPersister
>
563 transport_security_persister_
;
564 mutable scoped_ptr
<net::CertificateReportSender
> certificate_report_sender_
;
566 // These are only valid in between LazyInitialize() and their accessor being
568 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
569 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
570 // One URLRequestContext per isolated app for main and media requests.
571 mutable URLRequestContextMap app_request_context_map_
;
572 mutable URLRequestContextMap isolated_media_request_context_map_
;
574 mutable scoped_ptr
<ResourceContext
> resource_context_
;
576 mutable scoped_refptr
<content_settings::CookieSettings
> cookie_settings_
;
578 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
580 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
581 resource_prefetch_predictor_observer_
;
583 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
584 chrome_http_user_agent_settings_
;
586 #if defined(ENABLE_SUPERVISED_USERS)
587 mutable scoped_refptr
<const SupervisedUserURLFilter
>
588 supervised_user_url_filter_
;
591 #if defined(ENABLE_EXTENSIONS)
592 // Is NULL if switches::kDisableExtensionsHttpThrottling is on.
593 mutable scoped_ptr
<extensions::ExtensionThrottleManager
>
594 extension_throttle_manager_
;
597 mutable scoped_ptr
<DevToolsNetworkController
> network_controller_
;
599 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
600 bool initialized_on_UI_thread_
;
602 const Profile::ProfileType profile_type_
;
604 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
607 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_