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
;
38 class DevToolsNetworkController
;
39 class HostContentSettingsMap
;
40 class MediaDeviceIDSalt
;
41 class ProtocolHandlerRegistry
;
42 class SupervisedUserURLFilter
;
44 namespace chrome_browser_net
{
45 class ResourcePrefetchPredictorObserver
;
48 namespace data_reduction_proxy
{
49 class DataReductionProxyIOData
;
52 namespace extensions
{
58 class ChannelIDService
;
60 class FraudulentCertificateReporter
;
61 class FtpTransactionFactory
;
62 class HttpServerProperties
;
63 class HttpTransactionFactory
;
64 class ProxyConfigService
;
66 class SSLConfigService
;
67 class TransportSecurityPersister
;
68 class TransportSecurityState
;
69 class URLRequestJobFactoryImpl
;
73 class PolicyCertVerifier
;
74 class PolicyHeaderIOHelper
;
75 class URLBlacklistManager
;
78 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
79 // thread that is owned by a Profile, such as, but not limited to, network
80 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
81 // ProfileIOData, but will make sure to delete it on the IO thread (except
82 // possibly in unit tests where there is no IO thread).
85 typedef std::vector
<scoped_refptr
<ChromeURLRequestContextGetter
>>
86 ChromeURLRequestContextGetterVector
;
88 virtual ~ProfileIOData();
90 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
92 // Returns true if |scheme| is handled in Chrome, or by default handlers in
94 static bool IsHandledProtocol(const std::string
& scheme
);
96 // Returns true if |url| is handled in Chrome, or by default handlers in
98 static bool IsHandledURL(const GURL
& url
);
100 // Utility to install additional WebUI handlers into the |job_factory|.
101 // Ownership of the handlers is transfered from |protocol_handlers|
102 // to the |job_factory|.
103 static void InstallProtocolHandlers(
104 net::URLRequestJobFactoryImpl
* job_factory
,
105 content::ProtocolHandlerMap
* protocol_handlers
);
107 // Called by Profile.
108 content::ResourceContext
* GetResourceContext() const;
110 // Initializes the ProfileIOData object and primes the RequestContext
111 // generation. Must be called prior to any of the Get*() methods other than
112 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
114 content::ProtocolHandlerMap
* protocol_handlers
,
115 content::URLRequestInterceptorScopedVector request_interceptors
) const;
117 net::URLRequestContext
* GetMainRequestContext() const;
118 net::URLRequestContext
* GetMediaRequestContext() const;
119 net::URLRequestContext
* GetExtensionsRequestContext() const;
120 net::URLRequestContext
* GetIsolatedAppRequestContext(
121 net::URLRequestContext
* main_context
,
122 const StoragePartitionDescriptor
& partition_descriptor
,
123 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
124 protocol_handler_interceptor
,
125 content::ProtocolHandlerMap
* protocol_handlers
,
126 content::URLRequestInterceptorScopedVector request_interceptors
) const;
127 net::URLRequestContext
* GetIsolatedMediaRequestContext(
128 net::URLRequestContext
* app_context
,
129 const StoragePartitionDescriptor
& partition_descriptor
) const;
131 // These are useful when the Chrome layer is called from the content layer
132 // with a content::ResourceContext, and they want access to Chrome data for
134 extensions::InfoMap
* GetExtensionInfoMap() const;
135 CookieSettings
* GetCookieSettings() const;
136 HostContentSettingsMap
* GetHostContentSettingsMap() const;
138 IntegerPrefMember
* session_startup_pref() const {
139 return &session_startup_pref_
;
142 StringPrefMember
* google_services_account_id() const {
143 return &google_services_user_account_id_
;
146 net::URLRequestContext
* extensions_request_context() const {
147 return extensions_request_context_
.get();
150 BooleanPrefMember
* safe_browsing_enabled() const {
151 return &safe_browsing_enabled_
;
154 BooleanPrefMember
* sync_disabled() const {
155 return &sync_disabled_
;
158 BooleanPrefMember
* signin_allowed() const {
159 return &signin_allowed_
;
162 IntegerPrefMember
* network_prediction_options() const {
163 return &network_prediction_options_
;
166 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
168 DevToolsNetworkController
* network_controller() const {
169 return network_controller_
.get();
172 net::TransportSecurityState
* transport_security_state() const {
173 return transport_security_state_
.get();
176 #if defined(OS_CHROMEOS)
177 std::string
username_hash() const {
178 return username_hash_
;
181 bool use_system_key_slot() const { return use_system_key_slot_
; }
184 Profile::ProfileType
profile_type() const {
185 return profile_type_
;
188 bool IsOffTheRecord() const;
190 IntegerPrefMember
* incognito_availibility() const {
191 return &incognito_availibility_pref_
;
194 chrome_browser_net::ResourcePrefetchPredictorObserver
*
195 resource_prefetch_predictor_observer() const {
196 return resource_prefetch_predictor_observer_
.get();
199 #if defined(ENABLE_CONFIGURATION_POLICY)
200 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
201 return policy_header_helper_
.get();
205 #if defined(ENABLE_SUPERVISED_USERS)
206 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
207 return supervised_user_url_filter_
.get();
211 // Initialize the member needed to track the metrics enabled state. This is
212 // only to be called on the UI thread.
213 void InitializeMetricsEnabledStateOnUIThread();
215 // Returns whether or not metrics reporting is enabled in the browser instance
216 // on which this profile resides. This is safe for use from the IO thread, and
217 // should only be called from there.
218 bool GetMetricsEnabledStateOnIOThread() const;
220 void set_client_cert_store_factory_for_testing(
221 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
222 client_cert_store_factory_
= factory
;
225 bool IsDataReductionProxyEnabled() const;
227 data_reduction_proxy::DataReductionProxyIOData
*
228 data_reduction_proxy_io_data() const {
229 return data_reduction_proxy_io_data_
.get();
233 // A URLRequestContext for media that owns its HTTP factory, to ensure
235 class MediaRequestContext
: public net::URLRequestContext
{
237 MediaRequestContext();
239 void SetHttpTransactionFactory(
240 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
243 ~MediaRequestContext() override
;
245 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
248 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
249 // to ensure they are deleted.
250 class AppRequestContext
: public net::URLRequestContext
{
254 void SetCookieStore(net::CookieStore
* cookie_store
);
255 void SetHttpTransactionFactory(
256 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
257 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
260 ~AppRequestContext() override
;
262 scoped_refptr
<net::CookieStore
> cookie_store_
;
263 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
264 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
267 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
269 struct ProfileParams
{
275 scoped_refptr
<CookieSettings
> cookie_settings
;
276 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
277 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
278 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
279 #if defined(ENABLE_EXTENSIONS)
280 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
282 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
283 resource_prefetch_predictor_observer_
;
285 // This pointer exists only as a means of conveying a url job factory
286 // pointer from the protocol handler registry on the UI thread to the
287 // the URLRequestContext on the IO thread. The consumer MUST take
288 // ownership of the object by calling release() on this pointer.
289 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
290 protocol_handler_interceptor
;
292 // Holds the URLRequestInterceptor pointer that is created on the UI thread
293 // and then passed to the list of request_interceptors on the IO thread.
294 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
296 // We need to initialize the ProxyConfigService from the UI thread
297 // because on linux it relies on initializing things through gconf,
298 // and needs to be on the main thread.
299 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
301 #if defined(ENABLE_SUPERVISED_USERS)
302 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
305 #if defined(OS_CHROMEOS)
306 std::string username_hash
;
307 bool use_system_key_slot
;
310 // The profile this struct was populated from. It's passed as a void* to
311 // ensure it's not accidently used on the IO thread. Before using it on the
312 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
316 explicit ProfileIOData(Profile::ProfileType profile_type
);
318 static std::string
GetSSLSessionCacheShard();
320 void InitializeOnUIThread(Profile
* profile
);
321 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
323 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
324 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
325 content::URLRequestInterceptorScopedVector request_interceptors
,
326 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
327 protocol_handler_interceptor
,
328 net::NetworkDelegate
* network_delegate
,
329 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
331 // Called when the Profile is destroyed. |context_getters| must include all
332 // URLRequestContextGetters that refer to the ProfileIOData's
333 // URLRequestContexts. Triggers destruction of the ProfileIOData and shuts
334 // down |context_getters| safely on the IO thread.
335 // TODO(mmenke): Passing all those URLRequestContextGetters around like this
336 // is really silly. Can we do something cleaner?
337 void ShutdownOnUIThread(
338 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
340 // A ChannelIDService object is created by a derived class of
341 // ProfileIOData, and the derived class calls this method to set the
342 // channel_id_service_ member and transfers ownership to the base
344 void set_channel_id_service(
345 net::ChannelIDService
* channel_id_service
) const;
347 void set_data_reduction_proxy_io_data(
348 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
349 data_reduction_proxy_io_data
) const;
351 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
352 return fraudulent_certificate_reporter_
.get();
355 net::ProxyService
* proxy_service() const {
356 return proxy_service_
.get();
359 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
361 void set_http_server_properties(
362 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
364 net::URLRequestContext
* main_request_context() const {
365 return main_request_context_
.get();
368 bool initialized() const {
372 // Destroys the ResourceContext first, to cancel any URLRequests that are
373 // using it still, before we destroy the member variables that those
374 // URLRequests may be accessing.
375 void DestroyResourceContext();
377 // Creates network session and main network transaction factory.
378 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
379 const ProfileParams
* profile_params
,
380 net::HttpCache::BackendFactory
* main_backend
) const;
382 // Creates network transaction factory.
383 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
384 net::HttpNetworkSession
* shared_session
,
385 net::HttpCache::BackendFactory
* backend
) const;
387 void SetCookieSettingsForTesting(CookieSettings
* cookie_settings
);
390 class ResourceContext
: public content::ResourceContext
{
392 explicit ResourceContext(ProfileIOData
* io_data
);
393 ~ResourceContext() override
;
395 // ResourceContext implementation:
396 net::HostResolver
* GetHostResolver() override
;
397 net::URLRequestContext
* GetRequestContext() override
;
398 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
399 void CreateKeygenHandler(
400 uint32 key_size_in_bits
,
401 const std::string
& challenge_string
,
403 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
405 SaltCallback
GetMediaDeviceIDSalt() override
;
408 friend class ProfileIOData
;
410 ProfileIOData
* const io_data_
;
412 net::HostResolver
* host_resolver_
;
413 net::URLRequestContext
* request_context_
;
416 typedef std::map
<StoragePartitionDescriptor
,
417 net::URLRequestContext
*,
418 StoragePartitionDescriptorLess
>
419 URLRequestContextMap
;
421 // --------------------------------------------
422 // Virtual interface for subtypes to implement:
423 // --------------------------------------------
425 // Does the actual initialization of the ProfileIOData subtype. Subtypes
426 // should use the static helper functions above to implement this.
427 virtual void InitializeInternal(
428 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
429 ProfileParams
* profile_params
,
430 content::ProtocolHandlerMap
* protocol_handlers
,
431 content::URLRequestInterceptorScopedVector
432 request_interceptors
) const = 0;
434 // Initializes the RequestContext for extensions.
435 virtual void InitializeExtensionsRequestContext(
436 ProfileParams
* profile_params
) const = 0;
437 // Does an on-demand initialization of a RequestContext for the given
439 virtual net::URLRequestContext
* InitializeAppRequestContext(
440 net::URLRequestContext
* main_context
,
441 const StoragePartitionDescriptor
& details
,
442 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
443 protocol_handler_interceptor
,
444 content::ProtocolHandlerMap
* protocol_handlers
,
445 content::URLRequestInterceptorScopedVector
446 request_interceptors
) const = 0;
448 // Does an on-demand initialization of a media RequestContext for the given
450 virtual net::URLRequestContext
* InitializeMediaRequestContext(
451 net::URLRequestContext
* original_context
,
452 const StoragePartitionDescriptor
& details
) const = 0;
454 // These functions are used to transfer ownership of the lazily initialized
455 // context from ProfileIOData to the URLRequestContextGetter.
456 virtual net::URLRequestContext
*
457 AcquireMediaRequestContext() const = 0;
458 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
459 net::URLRequestContext
* main_context
,
460 const StoragePartitionDescriptor
& partition_descriptor
,
461 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
462 protocol_handler_interceptor
,
463 content::ProtocolHandlerMap
* protocol_handlers
,
464 content::URLRequestInterceptorScopedVector
465 request_interceptors
) const = 0;
466 virtual net::URLRequestContext
*
467 AcquireIsolatedMediaRequestContext(
468 net::URLRequestContext
* app_context
,
469 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
471 // The order *DOES* matter for the majority of these member variables, so
472 // don't move them around unless you know what you're doing!
474 // * ResourceContext references the URLRequestContexts, so
475 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
476 // should be destroyed first.
477 // * URLRequestContexts reference a whole bunch of members, so
478 // URLRequestContext needs to be destroyed before them.
479 // * Therefore, ResourceContext should be listed last, and then the
480 // URLRequestContexts, and then the URLRequestContext members.
481 // * Note that URLRequestContext members have a directed dependency graph
482 // too, so they must themselves be ordered correctly.
484 // Tracks whether or not we've been lazily initialized.
485 mutable bool initialized_
;
487 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
488 // Deleted after lazy initialization.
489 mutable scoped_ptr
<ProfileParams
> profile_params_
;
492 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
493 client_cert_store_factory_
;
495 mutable StringPrefMember google_services_user_account_id_
;
497 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
499 // Member variables which are pointed to by the various context objects.
500 mutable BooleanPrefMember enable_referrers_
;
501 mutable BooleanPrefMember enable_do_not_track_
;
502 mutable BooleanPrefMember force_google_safesearch_
;
503 mutable BooleanPrefMember force_youtube_safety_mode_
;
504 mutable BooleanPrefMember safe_browsing_enabled_
;
505 mutable BooleanPrefMember sync_disabled_
;
506 mutable BooleanPrefMember signin_allowed_
;
507 mutable IntegerPrefMember network_prediction_options_
;
508 // TODO(marja): Remove session_startup_pref_ if no longer needed.
509 mutable IntegerPrefMember session_startup_pref_
;
510 mutable BooleanPrefMember quick_check_enabled_
;
511 mutable IntegerPrefMember incognito_availibility_pref_
;
513 // The state of metrics reporting in the browser that this profile runs on.
514 // Unfortunately, since ChromeOS has a separate representation of this state,
515 // we need to make one available based on the platform.
516 #if defined(OS_CHROMEOS)
517 bool enable_metrics_
;
519 BooleanPrefMember enable_metrics_
;
522 #if defined(ENABLE_CONFIGURATION_POLICY)
523 // Pointed to by NetworkDelegate.
524 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
525 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
528 // Pointed to by URLRequestContext.
529 #if defined(ENABLE_EXTENSIONS)
530 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
532 mutable scoped_ptr
<net::ChannelIDService
> channel_id_service_
;
534 mutable scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
535 data_reduction_proxy_io_data_
;
537 mutable scoped_ptr
<net::FraudulentCertificateReporter
>
538 fraudulent_certificate_reporter_
;
539 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
540 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
541 mutable scoped_ptr
<net::HttpServerProperties
>
542 http_server_properties_
;
543 #if defined(OS_CHROMEOS)
544 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
545 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
546 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
547 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
548 mutable std::string username_hash_
;
549 mutable bool use_system_key_slot_
;
552 mutable scoped_ptr
<net::TransportSecurityPersister
>
553 transport_security_persister_
;
555 // These are only valid in between LazyInitialize() and their accessor being
557 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
558 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
559 // One URLRequestContext per isolated app for main and media requests.
560 mutable URLRequestContextMap app_request_context_map_
;
561 mutable URLRequestContextMap isolated_media_request_context_map_
;
563 mutable scoped_ptr
<ResourceContext
> resource_context_
;
565 mutable scoped_refptr
<CookieSettings
> cookie_settings_
;
567 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
569 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
570 resource_prefetch_predictor_observer_
;
572 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
573 chrome_http_user_agent_settings_
;
575 #if defined(ENABLE_SUPERVISED_USERS)
576 mutable scoped_refptr
<const SupervisedUserURLFilter
>
577 supervised_user_url_filter_
;
580 mutable scoped_ptr
<DevToolsNetworkController
> network_controller_
;
582 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
583 bool initialized_on_UI_thread_
;
585 const Profile::ProfileType profile_type_
;
587 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
590 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_