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
;
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 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 // Sets a global CertVerifier to use when initializing all profiles.
112 static void SetCertVerifierForTesting(net::CertVerifier
* cert_verifier
);
114 // Called by Profile.
115 content::ResourceContext
* GetResourceContext() const;
117 // Initializes the ProfileIOData object and primes the RequestContext
118 // generation. Must be called prior to any of the Get*() methods other than
119 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
121 content::ProtocolHandlerMap
* protocol_handlers
,
122 content::URLRequestInterceptorScopedVector request_interceptors
) const;
124 net::URLRequestContext
* GetMainRequestContext() const;
125 net::URLRequestContext
* GetMediaRequestContext() const;
126 net::URLRequestContext
* GetExtensionsRequestContext() const;
127 net::URLRequestContext
* GetIsolatedAppRequestContext(
128 net::URLRequestContext
* main_context
,
129 const StoragePartitionDescriptor
& partition_descriptor
,
130 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
131 protocol_handler_interceptor
,
132 content::ProtocolHandlerMap
* protocol_handlers
,
133 content::URLRequestInterceptorScopedVector request_interceptors
) const;
134 net::URLRequestContext
* GetIsolatedMediaRequestContext(
135 net::URLRequestContext
* app_context
,
136 const StoragePartitionDescriptor
& partition_descriptor
) const;
138 // These are useful when the Chrome layer is called from the content layer
139 // with a content::ResourceContext, and they want access to Chrome data for
141 extensions::InfoMap
* GetExtensionInfoMap() const;
142 extensions::ExtensionThrottleManager
* GetExtensionThrottleManager() const;
143 content_settings::CookieSettings
* GetCookieSettings() const;
144 HostContentSettingsMap
* GetHostContentSettingsMap() const;
146 IntegerPrefMember
* session_startup_pref() const {
147 return &session_startup_pref_
;
150 StringPrefMember
* google_services_account_id() const {
151 return &google_services_user_account_id_
;
154 net::URLRequestContext
* extensions_request_context() const {
155 return extensions_request_context_
.get();
158 BooleanPrefMember
* safe_browsing_enabled() const {
159 return &safe_browsing_enabled_
;
162 BooleanPrefMember
* sync_disabled() const {
163 return &sync_disabled_
;
166 BooleanPrefMember
* signin_allowed() const {
167 return &signin_allowed_
;
170 IntegerPrefMember
* network_prediction_options() const {
171 return &network_prediction_options_
;
174 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
176 DevToolsNetworkControllerHandle
* network_controller_handle() const {
177 return &network_controller_handle_
;
180 net::TransportSecurityState
* transport_security_state() const {
181 return transport_security_state_
.get();
184 #if defined(OS_CHROMEOS)
185 std::string
username_hash() const {
186 return username_hash_
;
189 bool use_system_key_slot() const { return use_system_key_slot_
; }
192 Profile::ProfileType
profile_type() const {
193 return profile_type_
;
196 bool IsOffTheRecord() const;
198 IntegerPrefMember
* incognito_availibility() const {
199 return &incognito_availibility_pref_
;
202 chrome_browser_net::ResourcePrefetchPredictorObserver
*
203 resource_prefetch_predictor_observer() const {
204 return resource_prefetch_predictor_observer_
.get();
207 #if defined(ENABLE_CONFIGURATION_POLICY)
208 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
209 return policy_header_helper_
.get();
213 #if defined(ENABLE_SUPERVISED_USERS)
214 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
215 return supervised_user_url_filter_
.get();
219 // Initialize the member needed to track the metrics enabled state. This is
220 // only to be called on the UI thread.
221 void InitializeMetricsEnabledStateOnUIThread();
223 // Returns whether or not metrics reporting is enabled in the browser instance
224 // on which this profile resides. This is safe for use from the IO thread, and
225 // should only be called from there.
226 bool GetMetricsEnabledStateOnIOThread() const;
228 void set_client_cert_store_factory_for_testing(
229 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
230 client_cert_store_factory_
= factory
;
233 bool IsDataReductionProxyEnabled() const;
235 data_reduction_proxy::DataReductionProxyIOData
*
236 data_reduction_proxy_io_data() const {
237 return data_reduction_proxy_io_data_
.get();
241 // A URLRequestContext for media that owns its HTTP factory, to ensure
243 class MediaRequestContext
: public net::URLRequestContext
{
245 MediaRequestContext();
247 void SetHttpTransactionFactory(
248 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
251 ~MediaRequestContext() override
;
253 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
256 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
257 // to ensure they are deleted.
258 class AppRequestContext
: public net::URLRequestContext
{
262 void SetCookieStore(net::CookieStore
* cookie_store
);
263 void SetHttpTransactionFactory(
264 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
265 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
268 ~AppRequestContext() override
;
270 scoped_refptr
<net::CookieStore
> cookie_store_
;
271 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
272 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
275 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
277 struct ProfileParams
{
283 scoped_refptr
<content_settings::CookieSettings
> cookie_settings
;
284 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
285 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
286 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
287 #if defined(ENABLE_EXTENSIONS)
288 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
290 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
291 resource_prefetch_predictor_observer_
;
293 // This pointer exists only as a means of conveying a url job factory
294 // pointer from the protocol handler registry on the UI thread to the
295 // the URLRequestContext on the IO thread. The consumer MUST take
296 // ownership of the object by calling release() on this pointer.
297 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
298 protocol_handler_interceptor
;
300 // Holds the URLRequestInterceptor pointer that is created on the UI thread
301 // and then passed to the list of request_interceptors on the IO thread.
302 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
304 // We need to initialize the ProxyConfigService from the UI thread
305 // because on linux it relies on initializing things through gconf,
306 // and needs to be on the main thread.
307 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
309 #if defined(ENABLE_SUPERVISED_USERS)
310 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
313 #if defined(OS_CHROMEOS)
314 std::string username_hash
;
315 bool use_system_key_slot
;
318 // The profile this struct was populated from. It's passed as a void* to
319 // ensure it's not accidently used on the IO thread. Before using it on the
320 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
324 explicit ProfileIOData(Profile::ProfileType profile_type
);
326 static std::string
GetSSLSessionCacheShard();
328 void InitializeOnUIThread(Profile
* profile
);
329 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
331 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
332 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
333 content::URLRequestInterceptorScopedVector request_interceptors
,
334 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
335 protocol_handler_interceptor
,
336 net::NetworkDelegate
* network_delegate
,
337 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
339 // Called when the Profile is destroyed. |context_getters| must include all
340 // URLRequestContextGetters that refer to the ProfileIOData's
341 // URLRequestContexts. Triggers destruction of the ProfileIOData and shuts
342 // down |context_getters| safely on the IO thread.
343 // TODO(mmenke): Passing all those URLRequestContextGetters around like this
344 // is really silly. Can we do something cleaner?
345 void ShutdownOnUIThread(
346 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
348 // A ChannelIDService object is created by a derived class of
349 // ProfileIOData, and the derived class calls this method to set the
350 // channel_id_service_ member and transfers ownership to the base
352 void set_channel_id_service(
353 net::ChannelIDService
* channel_id_service
) const;
355 void set_data_reduction_proxy_io_data(
356 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
357 data_reduction_proxy_io_data
) const;
359 net::ProxyService
* proxy_service() const {
360 return proxy_service_
.get();
363 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
365 void set_http_server_properties(
366 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
368 net::URLRequestContext
* main_request_context() const {
369 return main_request_context_
.get();
372 bool initialized() const {
376 // Destroys the ResourceContext first, to cancel any URLRequests that are
377 // using it still, before we destroy the member variables that those
378 // URLRequests may be accessing.
379 void DestroyResourceContext();
381 // Creates network session and main network transaction factory.
382 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
383 const ProfileParams
* profile_params
,
384 net::HttpCache::BackendFactory
* main_backend
) const;
386 // Creates network transaction factory.
387 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
388 net::HttpNetworkSession
* shared_session
,
389 net::HttpCache::BackendFactory
* backend
) const;
391 void SetCookieSettingsForTesting(
392 content_settings::CookieSettings
* cookie_settings
);
395 class ResourceContext
: public content::ResourceContext
{
397 explicit ResourceContext(ProfileIOData
* io_data
);
398 ~ResourceContext() override
;
400 // ResourceContext implementation:
401 net::HostResolver
* GetHostResolver() override
;
402 net::URLRequestContext
* GetRequestContext() override
;
403 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
404 void CreateKeygenHandler(
405 uint32 key_size_in_bits
,
406 const std::string
& challenge_string
,
408 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
410 SaltCallback
GetMediaDeviceIDSalt() override
;
413 friend class ProfileIOData
;
415 ProfileIOData
* const io_data_
;
417 net::HostResolver
* host_resolver_
;
418 net::URLRequestContext
* request_context_
;
421 typedef std::map
<StoragePartitionDescriptor
,
422 net::URLRequestContext
*,
423 StoragePartitionDescriptorLess
>
424 URLRequestContextMap
;
426 // --------------------------------------------
427 // Virtual interface for subtypes to implement:
428 // --------------------------------------------
430 // Does the actual initialization of the ProfileIOData subtype. Subtypes
431 // should use the static helper functions above to implement this.
432 virtual void InitializeInternal(
433 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
434 ProfileParams
* profile_params
,
435 content::ProtocolHandlerMap
* protocol_handlers
,
436 content::URLRequestInterceptorScopedVector
437 request_interceptors
) const = 0;
439 // Initializes the RequestContext for extensions.
440 virtual void InitializeExtensionsRequestContext(
441 ProfileParams
* profile_params
) const = 0;
442 // Does an on-demand initialization of a RequestContext for the given
444 virtual net::URLRequestContext
* InitializeAppRequestContext(
445 net::URLRequestContext
* main_context
,
446 const StoragePartitionDescriptor
& details
,
447 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
448 protocol_handler_interceptor
,
449 content::ProtocolHandlerMap
* protocol_handlers
,
450 content::URLRequestInterceptorScopedVector
451 request_interceptors
) const = 0;
453 // Does an on-demand initialization of a media RequestContext for the given
455 virtual net::URLRequestContext
* InitializeMediaRequestContext(
456 net::URLRequestContext
* original_context
,
457 const StoragePartitionDescriptor
& details
) const = 0;
459 // These functions are used to transfer ownership of the lazily initialized
460 // context from ProfileIOData to the URLRequestContextGetter.
461 virtual net::URLRequestContext
*
462 AcquireMediaRequestContext() const = 0;
463 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
464 net::URLRequestContext
* main_context
,
465 const StoragePartitionDescriptor
& partition_descriptor
,
466 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
467 protocol_handler_interceptor
,
468 content::ProtocolHandlerMap
* protocol_handlers
,
469 content::URLRequestInterceptorScopedVector
470 request_interceptors
) const = 0;
471 virtual net::URLRequestContext
*
472 AcquireIsolatedMediaRequestContext(
473 net::URLRequestContext
* app_context
,
474 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
476 // The order *DOES* matter for the majority of these member variables, so
477 // don't move them around unless you know what you're doing!
479 // * ResourceContext references the URLRequestContexts, so
480 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
481 // should be destroyed first.
482 // * URLRequestContexts reference a whole bunch of members, so
483 // URLRequestContext needs to be destroyed before them.
484 // * Therefore, ResourceContext should be listed last, and then the
485 // URLRequestContexts, and then the URLRequestContext members.
486 // * Note that URLRequestContext members have a directed dependency graph
487 // too, so they must themselves be ordered correctly.
489 // Tracks whether or not we've been lazily initialized.
490 mutable bool initialized_
;
492 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
493 // Deleted after lazy initialization.
494 mutable scoped_ptr
<ProfileParams
> profile_params_
;
497 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
498 client_cert_store_factory_
;
500 mutable StringPrefMember google_services_user_account_id_
;
502 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
504 // Member variables which are pointed to by the various context objects.
505 mutable BooleanPrefMember enable_referrers_
;
506 mutable BooleanPrefMember enable_do_not_track_
;
507 mutable BooleanPrefMember force_google_safesearch_
;
508 mutable BooleanPrefMember force_youtube_safety_mode_
;
509 mutable BooleanPrefMember safe_browsing_enabled_
;
510 mutable BooleanPrefMember sync_disabled_
;
511 mutable BooleanPrefMember signin_allowed_
;
512 mutable IntegerPrefMember network_prediction_options_
;
513 // TODO(marja): Remove session_startup_pref_ if no longer needed.
514 mutable IntegerPrefMember session_startup_pref_
;
515 mutable BooleanPrefMember quick_check_enabled_
;
516 mutable IntegerPrefMember incognito_availibility_pref_
;
518 // The state of metrics reporting in the browser that this profile runs on.
519 // Unfortunately, since ChromeOS has a separate representation of this state,
520 // we need to make one available based on the platform.
521 #if defined(OS_CHROMEOS)
522 bool enable_metrics_
;
524 BooleanPrefMember enable_metrics_
;
527 #if defined(ENABLE_CONFIGURATION_POLICY)
528 // Pointed to by NetworkDelegate.
529 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
530 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
533 // Pointed to by URLRequestContext.
534 #if defined(ENABLE_EXTENSIONS)
535 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
537 mutable scoped_ptr
<net::ChannelIDService
> channel_id_service_
;
539 mutable scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
540 data_reduction_proxy_io_data_
;
542 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
543 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
544 mutable scoped_ptr
<net::HttpServerProperties
>
545 http_server_properties_
;
546 #if defined(OS_CHROMEOS)
547 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
548 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
549 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
550 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
551 mutable std::string username_hash_
;
552 mutable bool use_system_key_slot_
;
555 mutable scoped_ptr
<net::TransportSecurityPersister
>
556 transport_security_persister_
;
557 mutable scoped_ptr
<net::CertificateReportSender
> certificate_report_sender_
;
559 // These are only valid in between LazyInitialize() and their accessor being
561 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
562 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
563 // One URLRequestContext per isolated app for main and media requests.
564 mutable URLRequestContextMap app_request_context_map_
;
565 mutable URLRequestContextMap isolated_media_request_context_map_
;
567 mutable scoped_ptr
<ResourceContext
> resource_context_
;
569 mutable scoped_refptr
<content_settings::CookieSettings
> cookie_settings_
;
571 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
573 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
574 resource_prefetch_predictor_observer_
;
576 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
577 chrome_http_user_agent_settings_
;
579 #if defined(ENABLE_SUPERVISED_USERS)
580 mutable scoped_refptr
<const SupervisedUserURLFilter
>
581 supervised_user_url_filter_
;
584 #if defined(ENABLE_EXTENSIONS)
585 // Is NULL if switches::kDisableExtensionsHttpThrottling is on.
586 mutable scoped_ptr
<extensions::ExtensionThrottleManager
>
587 extension_throttle_manager_
;
590 mutable DevToolsNetworkControllerHandle network_controller_handle_
;
592 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
593 bool initialized_on_UI_thread_
;
595 const Profile::ProfileType profile_type_
;
597 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
600 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_