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
;
62 class ChannelIDService
;
64 class FraudulentCertificateReporter
;
65 class FtpTransactionFactory
;
66 class HttpServerProperties
;
67 class HttpTransactionFactory
;
68 class ProxyConfigService
;
70 class SSLConfigService
;
71 class TransportSecurityPersister
;
72 class TransportSecurityState
;
73 class URLRequestJobFactoryImpl
;
77 class PolicyCertVerifier
;
78 class PolicyHeaderIOHelper
;
79 class URLBlacklistManager
;
82 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
83 // thread that is owned by a Profile, such as, but not limited to, network
84 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
85 // ProfileIOData, but will make sure to delete it on the IO thread (except
86 // possibly in unit tests where there is no IO thread).
89 typedef std::vector
<scoped_refptr
<ChromeURLRequestContextGetter
>>
90 ChromeURLRequestContextGetterVector
;
92 virtual ~ProfileIOData();
94 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
96 // Returns true if |scheme| is handled in Chrome, or by default handlers in
98 static bool IsHandledProtocol(const std::string
& scheme
);
100 // Returns true if |url| is handled in Chrome, or by default handlers in
102 static bool IsHandledURL(const GURL
& url
);
104 // Utility to install additional WebUI handlers into the |job_factory|.
105 // Ownership of the handlers is transfered from |protocol_handlers|
106 // to the |job_factory|.
107 static void InstallProtocolHandlers(
108 net::URLRequestJobFactoryImpl
* job_factory
,
109 content::ProtocolHandlerMap
* protocol_handlers
);
111 // Called by Profile.
112 content::ResourceContext
* GetResourceContext() const;
114 // Initializes the ProfileIOData object and primes the RequestContext
115 // generation. Must be called prior to any of the Get*() methods other than
116 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
118 content::ProtocolHandlerMap
* protocol_handlers
,
119 content::URLRequestInterceptorScopedVector request_interceptors
) const;
121 net::URLRequestContext
* GetMainRequestContext() const;
122 net::URLRequestContext
* GetMediaRequestContext() const;
123 net::URLRequestContext
* GetExtensionsRequestContext() const;
124 net::URLRequestContext
* GetIsolatedAppRequestContext(
125 net::URLRequestContext
* main_context
,
126 const StoragePartitionDescriptor
& partition_descriptor
,
127 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
128 protocol_handler_interceptor
,
129 content::ProtocolHandlerMap
* protocol_handlers
,
130 content::URLRequestInterceptorScopedVector request_interceptors
) const;
131 net::URLRequestContext
* GetIsolatedMediaRequestContext(
132 net::URLRequestContext
* app_context
,
133 const StoragePartitionDescriptor
& partition_descriptor
) const;
135 // These are useful when the Chrome layer is called from the content layer
136 // with a content::ResourceContext, and they want access to Chrome data for
138 extensions::InfoMap
* GetExtensionInfoMap() const;
139 extensions::ExtensionThrottleManager
* GetExtensionThrottleManager() const;
140 content_settings::CookieSettings
* GetCookieSettings() const;
141 HostContentSettingsMap
* GetHostContentSettingsMap() const;
143 IntegerPrefMember
* session_startup_pref() const {
144 return &session_startup_pref_
;
147 StringPrefMember
* google_services_account_id() const {
148 return &google_services_user_account_id_
;
151 net::URLRequestContext
* extensions_request_context() const {
152 return extensions_request_context_
.get();
155 BooleanPrefMember
* safe_browsing_enabled() const {
156 return &safe_browsing_enabled_
;
159 BooleanPrefMember
* sync_disabled() const {
160 return &sync_disabled_
;
163 BooleanPrefMember
* signin_allowed() const {
164 return &signin_allowed_
;
167 IntegerPrefMember
* network_prediction_options() const {
168 return &network_prediction_options_
;
171 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
173 DevToolsNetworkController
* network_controller() const {
174 return network_controller_
.get();
177 net::TransportSecurityState
* transport_security_state() const {
178 return transport_security_state_
.get();
181 #if defined(OS_CHROMEOS)
182 std::string
username_hash() const {
183 return username_hash_
;
186 bool use_system_key_slot() const { return use_system_key_slot_
; }
189 Profile::ProfileType
profile_type() const {
190 return profile_type_
;
193 bool IsOffTheRecord() const;
195 IntegerPrefMember
* incognito_availibility() const {
196 return &incognito_availibility_pref_
;
199 chrome_browser_net::ResourcePrefetchPredictorObserver
*
200 resource_prefetch_predictor_observer() const {
201 return resource_prefetch_predictor_observer_
.get();
204 #if defined(ENABLE_CONFIGURATION_POLICY)
205 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
206 return policy_header_helper_
.get();
210 #if defined(ENABLE_SUPERVISED_USERS)
211 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
212 return supervised_user_url_filter_
.get();
216 // Initialize the member needed to track the metrics enabled state. This is
217 // only to be called on the UI thread.
218 void InitializeMetricsEnabledStateOnUIThread();
220 // Returns whether or not metrics reporting is enabled in the browser instance
221 // on which this profile resides. This is safe for use from the IO thread, and
222 // should only be called from there.
223 bool GetMetricsEnabledStateOnIOThread() const;
225 void set_client_cert_store_factory_for_testing(
226 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
227 client_cert_store_factory_
= factory
;
230 bool IsDataReductionProxyEnabled() const;
232 data_reduction_proxy::DataReductionProxyIOData
*
233 data_reduction_proxy_io_data() const {
234 return data_reduction_proxy_io_data_
.get();
238 // A URLRequestContext for media that owns its HTTP factory, to ensure
240 class MediaRequestContext
: public net::URLRequestContext
{
242 MediaRequestContext();
244 void SetHttpTransactionFactory(
245 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
248 ~MediaRequestContext() override
;
250 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
253 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
254 // to ensure they are deleted.
255 class AppRequestContext
: public net::URLRequestContext
{
259 void SetCookieStore(net::CookieStore
* cookie_store
);
260 void SetHttpTransactionFactory(
261 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
262 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
265 ~AppRequestContext() override
;
267 scoped_refptr
<net::CookieStore
> cookie_store_
;
268 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
269 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
272 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
274 struct ProfileParams
{
280 scoped_refptr
<content_settings::CookieSettings
> cookie_settings
;
281 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
282 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
283 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
284 #if defined(ENABLE_EXTENSIONS)
285 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
287 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
288 resource_prefetch_predictor_observer_
;
290 // This pointer exists only as a means of conveying a url job factory
291 // pointer from the protocol handler registry on the UI thread to the
292 // the URLRequestContext on the IO thread. The consumer MUST take
293 // ownership of the object by calling release() on this pointer.
294 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
295 protocol_handler_interceptor
;
297 // Holds the URLRequestInterceptor pointer that is created on the UI thread
298 // and then passed to the list of request_interceptors on the IO thread.
299 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
301 // We need to initialize the ProxyConfigService from the UI thread
302 // because on linux it relies on initializing things through gconf,
303 // and needs to be on the main thread.
304 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
306 #if defined(ENABLE_SUPERVISED_USERS)
307 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
310 #if defined(OS_CHROMEOS)
311 std::string username_hash
;
312 bool use_system_key_slot
;
315 // The profile this struct was populated from. It's passed as a void* to
316 // ensure it's not accidently used on the IO thread. Before using it on the
317 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
321 explicit ProfileIOData(Profile::ProfileType profile_type
);
323 static std::string
GetSSLSessionCacheShard();
325 void InitializeOnUIThread(Profile
* profile
);
326 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
328 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
329 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
330 content::URLRequestInterceptorScopedVector request_interceptors
,
331 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
332 protocol_handler_interceptor
,
333 net::NetworkDelegate
* network_delegate
,
334 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
336 // Called when the Profile is destroyed. |context_getters| must include all
337 // URLRequestContextGetters that refer to the ProfileIOData's
338 // URLRequestContexts. Triggers destruction of the ProfileIOData and shuts
339 // down |context_getters| safely on the IO thread.
340 // TODO(mmenke): Passing all those URLRequestContextGetters around like this
341 // is really silly. Can we do something cleaner?
342 void ShutdownOnUIThread(
343 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
345 // A ChannelIDService object is created by a derived class of
346 // ProfileIOData, and the derived class calls this method to set the
347 // channel_id_service_ member and transfers ownership to the base
349 void set_channel_id_service(
350 net::ChannelIDService
* channel_id_service
) const;
352 void set_data_reduction_proxy_io_data(
353 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
354 data_reduction_proxy_io_data
) const;
356 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
357 return fraudulent_certificate_reporter_
.get();
360 net::ProxyService
* proxy_service() const {
361 return proxy_service_
.get();
364 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
366 void set_http_server_properties(
367 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
369 net::URLRequestContext
* main_request_context() const {
370 return main_request_context_
.get();
373 bool initialized() const {
377 // Destroys the ResourceContext first, to cancel any URLRequests that are
378 // using it still, before we destroy the member variables that those
379 // URLRequests may be accessing.
380 void DestroyResourceContext();
382 // Creates network session and main network transaction factory.
383 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
384 const ProfileParams
* profile_params
,
385 net::HttpCache::BackendFactory
* main_backend
) const;
387 // Creates network transaction factory.
388 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
389 net::HttpNetworkSession
* shared_session
,
390 net::HttpCache::BackendFactory
* backend
) const;
392 void SetCookieSettingsForTesting(
393 content_settings::CookieSettings
* cookie_settings
);
396 class ResourceContext
: public content::ResourceContext
{
398 explicit ResourceContext(ProfileIOData
* io_data
);
399 ~ResourceContext() override
;
401 // ResourceContext implementation:
402 net::HostResolver
* GetHostResolver() override
;
403 net::URLRequestContext
* GetRequestContext() override
;
404 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
405 void CreateKeygenHandler(
406 uint32 key_size_in_bits
,
407 const std::string
& challenge_string
,
409 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
411 SaltCallback
GetMediaDeviceIDSalt() override
;
414 friend class ProfileIOData
;
416 ProfileIOData
* const io_data_
;
418 net::HostResolver
* host_resolver_
;
419 net::URLRequestContext
* request_context_
;
422 typedef std::map
<StoragePartitionDescriptor
,
423 net::URLRequestContext
*,
424 StoragePartitionDescriptorLess
>
425 URLRequestContextMap
;
427 // --------------------------------------------
428 // Virtual interface for subtypes to implement:
429 // --------------------------------------------
431 // Does the actual initialization of the ProfileIOData subtype. Subtypes
432 // should use the static helper functions above to implement this.
433 virtual void InitializeInternal(
434 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
435 ProfileParams
* profile_params
,
436 content::ProtocolHandlerMap
* protocol_handlers
,
437 content::URLRequestInterceptorScopedVector
438 request_interceptors
) const = 0;
440 // Initializes the RequestContext for extensions.
441 virtual void InitializeExtensionsRequestContext(
442 ProfileParams
* profile_params
) const = 0;
443 // Does an on-demand initialization of a RequestContext for the given
445 virtual net::URLRequestContext
* InitializeAppRequestContext(
446 net::URLRequestContext
* main_context
,
447 const StoragePartitionDescriptor
& details
,
448 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
449 protocol_handler_interceptor
,
450 content::ProtocolHandlerMap
* protocol_handlers
,
451 content::URLRequestInterceptorScopedVector
452 request_interceptors
) const = 0;
454 // Does an on-demand initialization of a media RequestContext for the given
456 virtual net::URLRequestContext
* InitializeMediaRequestContext(
457 net::URLRequestContext
* original_context
,
458 const StoragePartitionDescriptor
& details
) const = 0;
460 // These functions are used to transfer ownership of the lazily initialized
461 // context from ProfileIOData to the URLRequestContextGetter.
462 virtual net::URLRequestContext
*
463 AcquireMediaRequestContext() const = 0;
464 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
465 net::URLRequestContext
* main_context
,
466 const StoragePartitionDescriptor
& partition_descriptor
,
467 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
468 protocol_handler_interceptor
,
469 content::ProtocolHandlerMap
* protocol_handlers
,
470 content::URLRequestInterceptorScopedVector
471 request_interceptors
) const = 0;
472 virtual net::URLRequestContext
*
473 AcquireIsolatedMediaRequestContext(
474 net::URLRequestContext
* app_context
,
475 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
477 // The order *DOES* matter for the majority of these member variables, so
478 // don't move them around unless you know what you're doing!
480 // * ResourceContext references the URLRequestContexts, so
481 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
482 // should be destroyed first.
483 // * URLRequestContexts reference a whole bunch of members, so
484 // URLRequestContext needs to be destroyed before them.
485 // * Therefore, ResourceContext should be listed last, and then the
486 // URLRequestContexts, and then the URLRequestContext members.
487 // * Note that URLRequestContext members have a directed dependency graph
488 // too, so they must themselves be ordered correctly.
490 // Tracks whether or not we've been lazily initialized.
491 mutable bool initialized_
;
493 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
494 // Deleted after lazy initialization.
495 mutable scoped_ptr
<ProfileParams
> profile_params_
;
498 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
499 client_cert_store_factory_
;
501 mutable StringPrefMember google_services_user_account_id_
;
503 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
505 // Member variables which are pointed to by the various context objects.
506 mutable BooleanPrefMember enable_referrers_
;
507 mutable BooleanPrefMember enable_do_not_track_
;
508 mutable BooleanPrefMember force_google_safesearch_
;
509 mutable BooleanPrefMember force_youtube_safety_mode_
;
510 mutable BooleanPrefMember safe_browsing_enabled_
;
511 mutable BooleanPrefMember sync_disabled_
;
512 mutable BooleanPrefMember signin_allowed_
;
513 mutable IntegerPrefMember network_prediction_options_
;
514 // TODO(marja): Remove session_startup_pref_ if no longer needed.
515 mutable IntegerPrefMember session_startup_pref_
;
516 mutable BooleanPrefMember quick_check_enabled_
;
517 mutable IntegerPrefMember incognito_availibility_pref_
;
519 // The state of metrics reporting in the browser that this profile runs on.
520 // Unfortunately, since ChromeOS has a separate representation of this state,
521 // we need to make one available based on the platform.
522 #if defined(OS_CHROMEOS)
523 bool enable_metrics_
;
525 BooleanPrefMember enable_metrics_
;
528 #if defined(ENABLE_CONFIGURATION_POLICY)
529 // Pointed to by NetworkDelegate.
530 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
531 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
534 // Pointed to by URLRequestContext.
535 #if defined(ENABLE_EXTENSIONS)
536 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
538 mutable scoped_ptr
<net::ChannelIDService
> channel_id_service_
;
540 mutable scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
541 data_reduction_proxy_io_data_
;
543 mutable scoped_ptr
<net::FraudulentCertificateReporter
>
544 fraudulent_certificate_reporter_
;
545 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
546 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
547 mutable scoped_ptr
<net::HttpServerProperties
>
548 http_server_properties_
;
549 #if defined(OS_CHROMEOS)
550 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
551 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
552 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
553 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
554 mutable std::string username_hash_
;
555 mutable bool use_system_key_slot_
;
558 mutable scoped_ptr
<net::TransportSecurityPersister
>
559 transport_security_persister_
;
561 // These are only valid in between LazyInitialize() and their accessor being
563 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
564 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
565 // One URLRequestContext per isolated app for main and media requests.
566 mutable URLRequestContextMap app_request_context_map_
;
567 mutable URLRequestContextMap isolated_media_request_context_map_
;
569 mutable scoped_ptr
<ResourceContext
> resource_context_
;
571 mutable scoped_refptr
<content_settings::CookieSettings
> cookie_settings_
;
573 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
575 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
576 resource_prefetch_predictor_observer_
;
578 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
579 chrome_http_user_agent_settings_
;
581 #if defined(ENABLE_SUPERVISED_USERS)
582 mutable scoped_refptr
<const SupervisedUserURLFilter
>
583 supervised_user_url_filter_
;
586 #if defined(ENABLE_EXTENSIONS)
587 // Is NULL if switches::kDisableExtensionsHttpThrottling is on.
588 mutable scoped_ptr
<extensions::ExtensionThrottleManager
>
589 extension_throttle_manager_
;
592 mutable scoped_ptr
<DevToolsNetworkController
> network_controller_
;
594 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
595 bool initialized_on_UI_thread_
;
597 const Profile::ProfileType profile_type_
;
599 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
602 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_