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
;
80 class PrerenderTracker
;
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 // Called by Profile.
113 content::ResourceContext
* GetResourceContext() const;
115 // Initializes the ProfileIOData object and primes the RequestContext
116 // generation. Must be called prior to any of the Get*() methods other than
117 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
119 content::ProtocolHandlerMap
* protocol_handlers
,
120 content::URLRequestInterceptorScopedVector request_interceptors
) const;
122 net::URLRequestContext
* GetMainRequestContext() const;
123 net::URLRequestContext
* GetMediaRequestContext() const;
124 net::URLRequestContext
* GetExtensionsRequestContext() const;
125 net::URLRequestContext
* GetIsolatedAppRequestContext(
126 net::URLRequestContext
* main_context
,
127 const StoragePartitionDescriptor
& partition_descriptor
,
128 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
129 protocol_handler_interceptor
,
130 content::ProtocolHandlerMap
* protocol_handlers
,
131 content::URLRequestInterceptorScopedVector request_interceptors
) const;
132 net::URLRequestContext
* GetIsolatedMediaRequestContext(
133 net::URLRequestContext
* app_context
,
134 const StoragePartitionDescriptor
& partition_descriptor
) const;
136 // These are useful when the Chrome layer is called from the content layer
137 // with a content::ResourceContext, and they want access to Chrome data for
139 extensions::InfoMap
* GetExtensionInfoMap() const;
140 CookieSettings
* GetCookieSettings() const;
141 HostContentSettingsMap
* GetHostContentSettingsMap() const;
143 IntegerPrefMember
* session_startup_pref() const {
144 return &session_startup_pref_
;
147 SigninNamesOnIOThread
* signin_names() const {
148 return signin_names_
.get();
151 StringPrefMember
* google_services_account_id() const {
152 return &google_services_user_account_id_
;
155 StringPrefMember
* google_services_username() const {
156 return &google_services_username_
;
159 StringPrefMember
* google_services_username_pattern() const {
160 return &google_services_username_pattern_
;
163 BooleanPrefMember
* reverse_autologin_enabled() const {
164 return &reverse_autologin_enabled_
;
167 const std::string
& reverse_autologin_pending_email() const {
168 return reverse_autologin_pending_email_
;
171 void set_reverse_autologin_pending_email(const std::string
& email
) {
172 reverse_autologin_pending_email_
= email
;
175 StringListPrefMember
* one_click_signin_rejected_email_list() const {
176 return &one_click_signin_rejected_email_list_
;
179 net::URLRequestContext
* extensions_request_context() const {
180 return extensions_request_context_
.get();
183 BooleanPrefMember
* safe_browsing_enabled() const {
184 return &safe_browsing_enabled_
;
187 BooleanPrefMember
* sync_disabled() const {
188 return &sync_disabled_
;
191 BooleanPrefMember
* signin_allowed() const {
192 return &signin_allowed_
;
195 IntegerPrefMember
* network_prediction_options() const {
196 return &network_prediction_options_
;
199 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
201 DevToolsNetworkController
* network_controller() const {
202 return network_controller_
.get();
205 net::TransportSecurityState
* transport_security_state() const {
206 return transport_security_state_
.get();
209 #if defined(OS_CHROMEOS)
210 std::string
username_hash() const {
211 return username_hash_
;
214 bool use_system_key_slot() const { return use_system_key_slot_
; }
217 Profile::ProfileType
profile_type() const {
218 return profile_type_
;
221 bool IsOffTheRecord() const;
223 IntegerPrefMember
* incognito_availibility() const {
224 return &incognito_availibility_pref_
;
227 chrome_browser_net::ResourcePrefetchPredictorObserver
*
228 resource_prefetch_predictor_observer() const {
229 return resource_prefetch_predictor_observer_
.get();
232 #if defined(ENABLE_CONFIGURATION_POLICY)
233 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
234 return policy_header_helper_
.get();
238 #if defined(ENABLE_SUPERVISED_USERS)
239 const SupervisedUserURLFilter
* supervised_user_url_filter() const {
240 return supervised_user_url_filter_
.get();
244 // Initialize the member needed to track the metrics enabled state. This is
245 // only to be called on the UI thread.
246 void InitializeMetricsEnabledStateOnUIThread();
248 // Returns whether or not metrics reporting is enabled in the browser instance
249 // on which this profile resides. This is safe for use from the IO thread, and
250 // should only be called from there.
251 bool GetMetricsEnabledStateOnIOThread() const;
253 void set_client_cert_store_factory_for_testing(
254 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
255 client_cert_store_factory_
= factory
;
258 bool IsDataReductionProxyEnabled() const;
260 data_reduction_proxy::DataReductionProxyIOData
*
261 data_reduction_proxy_io_data() const {
262 return data_reduction_proxy_io_data_
.get();
266 // A URLRequestContext for media that owns its HTTP factory, to ensure
268 class MediaRequestContext
: public net::URLRequestContext
{
270 MediaRequestContext();
272 void SetHttpTransactionFactory(
273 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
276 ~MediaRequestContext() override
;
278 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
281 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
282 // to ensure they are deleted.
283 class AppRequestContext
: public net::URLRequestContext
{
287 void SetCookieStore(net::CookieStore
* cookie_store
);
288 void SetHttpTransactionFactory(
289 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
290 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
293 ~AppRequestContext() override
;
295 scoped_refptr
<net::CookieStore
> cookie_store_
;
296 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
297 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
300 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
302 struct ProfileParams
{
308 scoped_refptr
<CookieSettings
> cookie_settings
;
309 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
310 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
311 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
312 #if defined(ENABLE_EXTENSIONS)
313 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
315 scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
316 resource_prefetch_predictor_observer_
;
318 // This pointer exists only as a means of conveying a url job factory
319 // pointer from the protocol handler registry on the UI thread to the
320 // the URLRequestContext on the IO thread. The consumer MUST take
321 // ownership of the object by calling release() on this pointer.
322 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
323 protocol_handler_interceptor
;
325 // Holds the URLRequestInterceptor pointer that is created on the UI thread
326 // and then passed to the list of request_interceptors on the IO thread.
327 scoped_ptr
<net::URLRequestInterceptor
> new_tab_page_interceptor
;
329 // We need to initialize the ProxyConfigService from the UI thread
330 // because on linux it relies on initializing things through gconf,
331 // and needs to be on the main thread.
332 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
334 #if defined(ENABLE_SUPERVISED_USERS)
335 scoped_refptr
<const SupervisedUserURLFilter
> supervised_user_url_filter
;
338 #if defined(OS_CHROMEOS)
339 std::string username_hash
;
340 bool use_system_key_slot
;
343 // The profile this struct was populated from. It's passed as a void* to
344 // ensure it's not accidently used on the IO thread. Before using it on the
345 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
348 prerender::PrerenderTracker
* prerender_tracker
;
351 explicit ProfileIOData(Profile::ProfileType profile_type
);
353 static std::string
GetSSLSessionCacheShard();
355 void InitializeOnUIThread(Profile
* profile
);
356 void ApplyProfileParamsToContext(net::URLRequestContext
* context
) const;
358 scoped_ptr
<net::URLRequestJobFactory
> SetUpJobFactoryDefaults(
359 scoped_ptr
<net::URLRequestJobFactoryImpl
> job_factory
,
360 content::URLRequestInterceptorScopedVector request_interceptors
,
361 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
362 protocol_handler_interceptor
,
363 net::NetworkDelegate
* network_delegate
,
364 net::FtpTransactionFactory
* ftp_transaction_factory
) const;
366 // Called when the profile is destroyed.
367 void ShutdownOnUIThread(
368 scoped_ptr
<ChromeURLRequestContextGetterVector
> context_getters
);
370 // A ChannelIDService object is created by a derived class of
371 // ProfileIOData, and the derived class calls this method to set the
372 // channel_id_service_ member and transfers ownership to the base
374 void set_channel_id_service(
375 net::ChannelIDService
* channel_id_service
) const;
377 void set_data_reduction_proxy_io_data(
378 scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
379 data_reduction_proxy_io_data
) const;
381 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
382 return fraudulent_certificate_reporter_
.get();
385 net::ProxyService
* proxy_service() const {
386 return proxy_service_
.get();
389 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
391 void set_http_server_properties(
392 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
394 net::URLRequestContext
* main_request_context() const {
395 return main_request_context_
.get();
398 bool initialized() const {
402 // Destroys the ResourceContext first, to cancel any URLRequests that are
403 // using it still, before we destroy the member variables that those
404 // URLRequests may be accessing.
405 void DestroyResourceContext();
407 // Creates network session and main network transaction factory.
408 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
409 const ProfileParams
* profile_params
,
410 net::HttpCache::BackendFactory
* main_backend
) const;
412 // Creates network transaction factory.
413 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
414 net::HttpNetworkSession
* shared_session
,
415 net::HttpCache::BackendFactory
* backend
) const;
417 void SetCookieSettingsForTesting(CookieSettings
* cookie_settings
);
419 void set_signin_names_for_testing(SigninNamesOnIOThread
* signin_names
);
422 class ResourceContext
: public content::ResourceContext
{
424 explicit ResourceContext(ProfileIOData
* io_data
);
425 ~ResourceContext() override
;
427 // ResourceContext implementation:
428 net::HostResolver
* GetHostResolver() override
;
429 net::URLRequestContext
* GetRequestContext() override
;
430 scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() override
;
431 void CreateKeygenHandler(
432 uint32 key_size_in_bits
,
433 const std::string
& challenge_string
,
435 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
437 SaltCallback
GetMediaDeviceIDSalt() override
;
440 friend class ProfileIOData
;
442 ProfileIOData
* const io_data_
;
444 net::HostResolver
* host_resolver_
;
445 net::URLRequestContext
* request_context_
;
448 typedef std::map
<StoragePartitionDescriptor
,
449 net::URLRequestContext
*,
450 StoragePartitionDescriptorLess
>
451 URLRequestContextMap
;
453 // --------------------------------------------
454 // Virtual interface for subtypes to implement:
455 // --------------------------------------------
457 // Does the actual initialization of the ProfileIOData subtype. Subtypes
458 // should use the static helper functions above to implement this.
459 virtual void InitializeInternal(
460 scoped_ptr
<ChromeNetworkDelegate
> chrome_network_delegate
,
461 ProfileParams
* profile_params
,
462 content::ProtocolHandlerMap
* protocol_handlers
,
463 content::URLRequestInterceptorScopedVector
464 request_interceptors
) const = 0;
466 // Initializes the RequestContext for extensions.
467 virtual void InitializeExtensionsRequestContext(
468 ProfileParams
* profile_params
) const = 0;
469 // Does an on-demand initialization of a RequestContext for the given
471 virtual net::URLRequestContext
* InitializeAppRequestContext(
472 net::URLRequestContext
* main_context
,
473 const StoragePartitionDescriptor
& details
,
474 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
475 protocol_handler_interceptor
,
476 content::ProtocolHandlerMap
* protocol_handlers
,
477 content::URLRequestInterceptorScopedVector
478 request_interceptors
) const = 0;
480 // Does an on-demand initialization of a media RequestContext for the given
482 virtual net::URLRequestContext
* InitializeMediaRequestContext(
483 net::URLRequestContext
* original_context
,
484 const StoragePartitionDescriptor
& details
) const = 0;
486 // These functions are used to transfer ownership of the lazily initialized
487 // context from ProfileIOData to the URLRequestContextGetter.
488 virtual net::URLRequestContext
*
489 AcquireMediaRequestContext() const = 0;
490 virtual net::URLRequestContext
* AcquireIsolatedAppRequestContext(
491 net::URLRequestContext
* main_context
,
492 const StoragePartitionDescriptor
& partition_descriptor
,
493 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
494 protocol_handler_interceptor
,
495 content::ProtocolHandlerMap
* protocol_handlers
,
496 content::URLRequestInterceptorScopedVector
497 request_interceptors
) const = 0;
498 virtual net::URLRequestContext
*
499 AcquireIsolatedMediaRequestContext(
500 net::URLRequestContext
* app_context
,
501 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
503 // The order *DOES* matter for the majority of these member variables, so
504 // don't move them around unless you know what you're doing!
506 // * ResourceContext references the URLRequestContexts, so
507 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
508 // should be destroyed first.
509 // * URLRequestContexts reference a whole bunch of members, so
510 // URLRequestContext needs to be destroyed before them.
511 // * Therefore, ResourceContext should be listed last, and then the
512 // URLRequestContexts, and then the URLRequestContext members.
513 // * Note that URLRequestContext members have a directed dependency graph
514 // too, so they must themselves be ordered correctly.
516 // Tracks whether or not we've been lazily initialized.
517 mutable bool initialized_
;
519 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
520 // Deleted after lazy initialization.
521 mutable scoped_ptr
<ProfileParams
> profile_params_
;
523 // Provides access to the email addresses of all signed in profiles.
524 mutable scoped_ptr
<SigninNamesOnIOThread
> signin_names_
;
527 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
528 client_cert_store_factory_
;
530 mutable StringPrefMember google_services_user_account_id_
;
531 mutable StringPrefMember google_services_username_
;
532 mutable StringPrefMember google_services_username_pattern_
;
533 mutable BooleanPrefMember reverse_autologin_enabled_
;
535 // During the reverse autologin request chain processing, this member saves
536 // the email of the google account that is being signed into.
537 std::string reverse_autologin_pending_email_
;
539 mutable StringListPrefMember one_click_signin_rejected_email_list_
;
541 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
543 // Member variables which are pointed to by the various context objects.
544 mutable BooleanPrefMember enable_referrers_
;
545 mutable BooleanPrefMember enable_do_not_track_
;
546 mutable BooleanPrefMember force_safesearch_
;
547 mutable BooleanPrefMember force_google_safesearch_
;
548 mutable BooleanPrefMember force_youtube_safety_mode_
;
549 mutable BooleanPrefMember safe_browsing_enabled_
;
550 mutable BooleanPrefMember sync_disabled_
;
551 mutable BooleanPrefMember signin_allowed_
;
552 mutable IntegerPrefMember network_prediction_options_
;
553 // TODO(marja): Remove session_startup_pref_ if no longer needed.
554 mutable IntegerPrefMember session_startup_pref_
;
555 mutable BooleanPrefMember quick_check_enabled_
;
556 mutable IntegerPrefMember incognito_availibility_pref_
;
558 // The state of metrics reporting in the browser that this profile runs on.
559 // Unfortunately, since ChromeOS has a separate representation of this state,
560 // we need to make one available based on the platform.
561 #if defined(OS_CHROMEOS)
562 bool enable_metrics_
;
564 BooleanPrefMember enable_metrics_
;
567 #if defined(ENABLE_CONFIGURATION_POLICY)
568 // Pointed to by NetworkDelegate.
569 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
570 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
573 // Pointed to by URLRequestContext.
574 #if defined(ENABLE_EXTENSIONS)
575 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
577 mutable scoped_ptr
<net::ChannelIDService
> channel_id_service_
;
579 mutable scoped_ptr
<data_reduction_proxy::DataReductionProxyIOData
>
580 data_reduction_proxy_io_data_
;
582 mutable scoped_ptr
<net::FraudulentCertificateReporter
>
583 fraudulent_certificate_reporter_
;
584 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
585 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
586 mutable scoped_ptr
<net::HttpServerProperties
>
587 http_server_properties_
;
588 #if defined(OS_CHROMEOS)
589 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
590 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
591 mutable policy::PolicyCertVerifier
* policy_cert_verifier_
;
592 mutable scoped_ptr
<net::CertVerifier
> cert_verifier_
;
593 mutable std::string username_hash_
;
594 mutable bool use_system_key_slot_
;
597 mutable scoped_ptr
<net::TransportSecurityPersister
>
598 transport_security_persister_
;
600 // These are only valid in between LazyInitialize() and their accessor being
602 mutable scoped_ptr
<net::URLRequestContext
> main_request_context_
;
603 mutable scoped_ptr
<net::URLRequestContext
> extensions_request_context_
;
604 // One URLRequestContext per isolated app for main and media requests.
605 mutable URLRequestContextMap app_request_context_map_
;
606 mutable URLRequestContextMap isolated_media_request_context_map_
;
608 mutable scoped_ptr
<ResourceContext
> resource_context_
;
610 mutable scoped_refptr
<CookieSettings
> cookie_settings_
;
612 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
614 mutable scoped_ptr
<chrome_browser_net::ResourcePrefetchPredictorObserver
>
615 resource_prefetch_predictor_observer_
;
617 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
618 chrome_http_user_agent_settings_
;
620 #if defined(ENABLE_SUPERVISED_USERS)
621 mutable scoped_refptr
<const SupervisedUserURLFilter
>
622 supervised_user_url_filter_
;
625 mutable scoped_ptr
<DevToolsNetworkController
> network_controller_
;
627 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
628 bool initialized_on_UI_thread_
;
630 const Profile::ProfileType profile_type_
;
632 DISALLOW_COPY_AND_ASSIGN(ProfileIOData
);
635 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_