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 SigninNamesOnIOThread
;
43 class SupervisedUserURLFilter
;
45 namespace chrome_browser_net
{
46 class ResourcePrefetchPredictorObserver
;
49 namespace data_reduction_proxy
{
50 class DataReductionProxyIOData
;
53 namespace extensions
{
59 class ChannelIDService
;
61 class FraudulentCertificateReporter
;
62 class FtpTransactionFactory
;
63 class HttpServerProperties
;
64 class HttpTransactionFactory
;
65 class ProxyConfigService
;
67 class SSLConfigService
;
68 class TransportSecurityPersister
;
69 class TransportSecurityState
;
70 class URLRequestJobFactoryImpl
;
74 class PolicyCertVerifier
;
75 class PolicyHeaderIOHelper
;
76 class URLBlacklistManager
;
79 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
80 // thread that is owned by a Profile, such as, but not limited to, network
81 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
82 // ProfileIOData, but will make sure to delete it on the IO thread (except
83 // possibly in unit tests where there is no IO thread).
86 typedef std::vector
<scoped_refptr
<ChromeURLRequestContextGetter
>>
87 ChromeURLRequestContextGetterVector
;
89 virtual ~ProfileIOData();
91 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
93 // Returns true if |scheme| is handled in Chrome, or by default handlers in
95 static bool IsHandledProtocol(const std::string
& scheme
);
97 // Returns true if |url| is handled in Chrome, or by default handlers in
99 static bool IsHandledURL(const GURL
& url
);
101 // Utility to install additional WebUI handlers into the |job_factory|.
102 // Ownership of the handlers is transfered from |protocol_handlers|
103 // to the |job_factory|.
104 static void InstallProtocolHandlers(
105 net::URLRequestJobFactoryImpl
* job_factory
,
106 content::ProtocolHandlerMap
* protocol_handlers
);
108 // Called by Profile.
109 content::ResourceContext
* GetResourceContext() const;
111 // Initializes the ProfileIOData object and primes the RequestContext
112 // generation. Must be called prior to any of the Get*() methods other than
113 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
115 content::ProtocolHandlerMap
* protocol_handlers
,
116 content::URLRequestInterceptorScopedVector request_interceptors
) const;
118 net::URLRequestContext
* GetMainRequestContext() const;
119 net::URLRequestContext
* GetMediaRequestContext() const;
120 net::URLRequestContext
* GetExtensionsRequestContext() const;
121 net::URLRequestContext
* GetIsolatedAppRequestContext(
122 net::URLRequestContext
* main_context
,
123 const StoragePartitionDescriptor
& partition_descriptor
,
124 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
125 protocol_handler_interceptor
,
126 content::ProtocolHandlerMap
* protocol_handlers
,
127 content::URLRequestInterceptorScopedVector request_interceptors
) const;
128 net::URLRequestContext
* GetIsolatedMediaRequestContext(
129 net::URLRequestContext
* app_context
,
130 const StoragePartitionDescriptor
& partition_descriptor
) const;
132 // These are useful when the Chrome layer is called from the content layer
133 // with a content::ResourceContext, and they want access to Chrome data for
135 extensions::InfoMap
* GetExtensionInfoMap() const;
136 CookieSettings
* GetCookieSettings() const;
137 HostContentSettingsMap
* GetHostContentSettingsMap() const;
139 IntegerPrefMember
* session_startup_pref() const {
140 return &session_startup_pref_
;
143 SigninNamesOnIOThread
* signin_names() const {
144 return signin_names_
.get();
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
<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.
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
);
389 void set_signin_names_for_testing(SigninNamesOnIOThread
* signin_names
);
392 class ResourceContext
: public content::ResourceContext
{
394 explicit ResourceContext(ProfileIOData
* io_data
);
395 ~ResourceContext() override
;
397 // ResourceContext implementation:
398 net::HostResolver
* GetHostResolver() override
;
399 net::URLRequestContext
* GetRequestContext() override
;
400 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
401 void CreateKeygenHandler(
402 uint32 key_size_in_bits
,
403 const std::string
& challenge_string
,
405 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
407 SaltCallback
GetMediaDeviceIDSalt() override
;
410 friend class ProfileIOData
;
412 ProfileIOData
* const io_data_
;
414 net::HostResolver
* host_resolver_
;
415 net::URLRequestContext
* request_context_
;
418 typedef std::map
<StoragePartitionDescriptor
,
419 net::URLRequestContext
*,
420 StoragePartitionDescriptorLess
>
421 URLRequestContextMap
;
423 // --------------------------------------------
424 // Virtual interface for subtypes to implement:
425 // --------------------------------------------
427 // Does the actual initialization of the ProfileIOData subtype. Subtypes
428 // should use the static helper functions above to implement this.
429 virtual void InitializeInternal(
430 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
431 ProfileParams
* profile_params
,
432 content::ProtocolHandlerMap
* protocol_handlers
,
433 content::URLRequestInterceptorScopedVector
434 request_interceptors
) const = 0;
436 // Initializes the RequestContext for extensions.
437 virtual void InitializeExtensionsRequestContext(
438 ProfileParams
* profile_params
) const = 0;
439 // Does an on-demand initialization of a RequestContext for the given
441 virtual net::URLRequestContext
* InitializeAppRequestContext(
442 net::URLRequestContext
* main_context
,
443 const StoragePartitionDescriptor
& details
,
444 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
445 protocol_handler_interceptor
,
446 content::ProtocolHandlerMap
* protocol_handlers
,
447 content::URLRequestInterceptorScopedVector
448 request_interceptors
) const = 0;
450 // Does an on-demand initialization of a media RequestContext for the given
452 virtual net::URLRequestContext
* InitializeMediaRequestContext(
453 net::URLRequestContext
* original_context
,
454 const StoragePartitionDescriptor
& details
) const = 0;
456 // These functions are used to transfer ownership of the lazily initialized
457 // context from ProfileIOData to the URLRequestContextGetter.
458 virtual net::URLRequestContext
*
459 AcquireMediaRequestContext() const = 0;
460 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
461 net::URLRequestContext
* main_context
,
462 const StoragePartitionDescriptor
& partition_descriptor
,
463 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
464 protocol_handler_interceptor
,
465 content::ProtocolHandlerMap
* protocol_handlers
,
466 content::URLRequestInterceptorScopedVector
467 request_interceptors
) const = 0;
468 virtual net::URLRequestContext
*
469 AcquireIsolatedMediaRequestContext(
470 net::URLRequestContext
* app_context
,
471 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
473 // The order *DOES* matter for the majority of these member variables, so
474 // don't move them around unless you know what you're doing!
476 // * ResourceContext references the URLRequestContexts, so
477 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
478 // should be destroyed first.
479 // * URLRequestContexts reference a whole bunch of members, so
480 // URLRequestContext needs to be destroyed before them.
481 // * Therefore, ResourceContext should be listed last, and then the
482 // URLRequestContexts, and then the URLRequestContext members.
483 // * Note that URLRequestContext members have a directed dependency graph
484 // too, so they must themselves be ordered correctly.
486 // Tracks whether or not we've been lazily initialized.
487 mutable bool initialized_
;
489 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
490 // Deleted after lazy initialization.
491 mutable scoped_ptr
<ProfileParams
> profile_params_
;
493 // Provides access to the email addresses of all signed in profiles.
494 mutable scoped_ptr
<SigninNamesOnIOThread
> signin_names_
;
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_safesearch_
;
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
<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 mutable scoped_ptr
<DevToolsNetworkController
> network_controller_
;
588 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
589 bool initialized_on_UI_thread_
;
591 const Profile::ProfileType profile_type_
;
593 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
596 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_