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_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/files/file_path.h"
13 #include "base/memory/ref_counted.h"
14 #include "base/memory/scoped_ptr.h"
15 #include "base/memory/weak_ptr.h"
16 #include "base/prefs/pref_member.h"
17 #include "base/synchronization/lock.h"
18 #include "chrome/browser/custom_handlers/protocol_handler_registry.h"
19 #include "chrome/browser/io_thread.h"
20 #include "chrome/browser/net/chrome_url_request_context.h"
21 #include "chrome/browser/profiles/profile.h"
22 #include "chrome/browser/profiles/storage_partition_descriptor.h"
23 #include "chrome/common/content_settings_types.h"
24 #include "content/public/browser/content_browser_client.h"
25 #include "content/public/browser/resource_context.h"
26 #include "net/cookies/cookie_monster.h"
27 #include "net/http/http_cache.h"
28 #include "net/http/http_network_session.h"
29 #include "net/url_request/url_request_job_factory.h"
31 class ChromeHttpUserAgentSettings
;
32 class ChromeNetworkDelegate
;
34 class DevToolsNetworkController
;
35 class HostContentSettingsMap
;
36 class ManagedModeURLFilter
;
37 class MediaDeviceIDSalt
;
38 class ProtocolHandlerRegistry
;
39 class SigninNamesOnIOThread
;
41 namespace extensions
{
47 class FraudulentCertificateReporter
;
48 class FtpTransactionFactory
;
49 class HttpServerProperties
;
50 class HttpTransactionFactory
;
51 class ServerBoundCertService
;
52 class ProxyConfigService
;
54 class SSLConfigService
;
55 class TransportSecurityPersister
;
56 class TransportSecurityState
;
57 class URLRequestJobFactoryImpl
;
61 class PolicyCertVerifier
;
62 class PolicyHeaderIOHelper
;
63 class URLBlacklistManager
;
67 class PrerenderTracker
;
70 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
71 // thread that is owned by a Profile, such as, but not limited to, network
72 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
73 // ProfileIOData, but will make sure to delete it on the IO thread (except
74 // possibly in unit tests where there is no IO thread).
77 virtual ~ProfileIOData();
79 static ProfileIOData
* FromResourceContext(content::ResourceContext
* rc
);
81 // Returns true if |scheme| is handled in Chrome, or by default handlers in
83 static bool IsHandledProtocol(const std::string
& scheme
);
85 // Returns true if |url| is handled in Chrome, or by default handlers in
87 static bool IsHandledURL(const GURL
& url
);
89 // Utility to install additional WebUI handlers into the |job_factory|.
90 // Ownership of the handlers is transfered from |protocol_handlers|
91 // to the |job_factory|.
92 static void InstallProtocolHandlers(
93 net::URLRequestJobFactoryImpl
* job_factory
,
94 content::ProtocolHandlerMap
* protocol_handlers
);
97 content::ResourceContext
* GetResourceContext() const;
99 // Initializes the ProfileIOData object and primes the RequestContext
100 // generation. Must be called prior to any of the Get*() methods other than
101 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
103 content::ProtocolHandlerMap
* protocol_handlers
,
104 content::URLRequestInterceptorScopedVector request_interceptors
) const;
106 ChromeURLRequestContext
* GetMainRequestContext() const;
107 ChromeURLRequestContext
* GetMediaRequestContext() const;
108 ChromeURLRequestContext
* GetExtensionsRequestContext() const;
109 ChromeURLRequestContext
* GetIsolatedAppRequestContext(
110 ChromeURLRequestContext
* main_context
,
111 const StoragePartitionDescriptor
& partition_descriptor
,
112 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
113 protocol_handler_interceptor
,
114 content::ProtocolHandlerMap
* protocol_handlers
,
115 content::URLRequestInterceptorScopedVector request_interceptors
) const;
116 ChromeURLRequestContext
* GetIsolatedMediaRequestContext(
117 ChromeURLRequestContext
* app_context
,
118 const StoragePartitionDescriptor
& partition_descriptor
) const;
120 // These are useful when the Chrome layer is called from the content layer
121 // with a content::ResourceContext, and they want access to Chrome data for
123 extensions::InfoMap
* GetExtensionInfoMap() const;
124 CookieSettings
* GetCookieSettings() const;
125 HostContentSettingsMap
* GetHostContentSettingsMap() const;
127 IntegerPrefMember
* session_startup_pref() const {
128 return &session_startup_pref_
;
131 SigninNamesOnIOThread
* signin_names() const {
132 return signin_names_
.get();
135 StringPrefMember
* google_services_account_id() const {
136 return &google_services_user_account_id_
;
139 StringPrefMember
* google_services_username() const {
140 return &google_services_username_
;
143 StringPrefMember
* google_services_username_pattern() const {
144 return &google_services_username_pattern_
;
147 BooleanPrefMember
* reverse_autologin_enabled() const {
148 return &reverse_autologin_enabled_
;
151 const std::string
& reverse_autologin_pending_email() const {
152 return reverse_autologin_pending_email_
;
155 void set_reverse_autologin_pending_email(const std::string
& email
) {
156 reverse_autologin_pending_email_
= email
;
159 StringListPrefMember
* one_click_signin_rejected_email_list() const {
160 return &one_click_signin_rejected_email_list_
;
163 ChromeURLRequestContext
* extensions_request_context() const {
164 return extensions_request_context_
.get();
167 BooleanPrefMember
* safe_browsing_enabled() const {
168 return &safe_browsing_enabled_
;
171 BooleanPrefMember
* printing_enabled() const {
172 return &printing_enabled_
;
175 BooleanPrefMember
* sync_disabled() const {
176 return &sync_disabled_
;
179 BooleanPrefMember
* signin_allowed() const {
180 return &signin_allowed_
;
183 BooleanPrefMember
* network_prediction_enabled() const {
184 return &network_prediction_enabled_
;
187 content::ResourceContext::SaltCallback
GetMediaDeviceIDSalt() const;
189 DevToolsNetworkController
* network_controller() const {
190 return network_controller_
.get();
193 net::TransportSecurityState
* transport_security_state() const {
194 return transport_security_state_
.get();
197 #if defined(OS_CHROMEOS)
198 std::string
username_hash() const {
199 return username_hash_
;
203 Profile::ProfileType
profile_type() const {
204 return profile_type_
;
207 bool IsOffTheRecord() const;
209 IntegerPrefMember
* incognito_availibility() const {
210 return &incognito_availibility_pref_
;
213 #if defined(ENABLE_CONFIGURATION_POLICY)
214 policy::PolicyHeaderIOHelper
* policy_header_helper() const {
215 return policy_header_helper_
.get();
219 #if defined(ENABLE_MANAGED_USERS)
220 const ManagedModeURLFilter
* managed_mode_url_filter() const {
221 return managed_mode_url_filter_
.get();
225 // Initialize the member needed to track the metrics enabled state. This is
226 // only to be called on the UI thread.
227 void InitializeMetricsEnabledStateOnUIThread();
229 // Returns whether or not metrics reporting is enabled in the browser instance
230 // on which this profile resides. This is safe for use from the IO thread, and
231 // should only be called from there.
232 bool GetMetricsEnabledStateOnIOThread() const;
234 void set_client_cert_store_factory_for_testing(
235 const base::Callback
<scoped_ptr
<net::ClientCertStore
>()>& factory
) {
236 client_cert_store_factory_
= factory
;
240 // A URLRequestContext for media that owns its HTTP factory, to ensure
242 class MediaRequestContext
: public ChromeURLRequestContext
{
244 MediaRequestContext();
246 void SetHttpTransactionFactory(
247 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
250 virtual ~MediaRequestContext();
252 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
255 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
256 // to ensure they are deleted.
257 class AppRequestContext
: public ChromeURLRequestContext
{
261 void SetCookieStore(net::CookieStore
* cookie_store
);
262 void SetHttpTransactionFactory(
263 scoped_ptr
<net::HttpTransactionFactory
> http_factory
);
264 void SetJobFactory(scoped_ptr
<net::URLRequestJobFactory
> job_factory
);
267 virtual ~AppRequestContext();
269 scoped_refptr
<net::CookieStore
> cookie_store_
;
270 scoped_ptr
<net::HttpTransactionFactory
> http_factory_
;
271 scoped_ptr
<net::URLRequestJobFactory
> job_factory_
;
274 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
276 struct ProfileParams
{
282 scoped_refptr
<CookieSettings
> cookie_settings
;
283 scoped_refptr
<HostContentSettingsMap
> host_content_settings_map
;
284 scoped_refptr
<net::SSLConfigService
> ssl_config_service
;
285 scoped_refptr
<net::CookieMonster::Delegate
> cookie_monster_delegate
;
286 scoped_refptr
<extensions::InfoMap
> extension_info_map
;
288 // This pointer exists only as a means of conveying a url job factory
289 // pointer from the protocol handler registry on the UI thread to the
290 // the URLRequestContext on the IO thread. The consumer MUST take
291 // ownership of the object by calling release() on this pointer.
292 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
293 protocol_handler_interceptor
;
295 // We need to initialize the ProxyConfigService from the UI thread
296 // because on linux it relies on initializing things through gconf,
297 // and needs to be on the main thread.
298 scoped_ptr
<net::ProxyConfigService
> proxy_config_service
;
300 #if defined(ENABLE_MANAGED_USERS)
301 scoped_refptr
<const ManagedModeURLFilter
> managed_mode_url_filter
;
304 #if defined(OS_CHROMEOS)
305 std::string username_hash
;
308 // The profile this struct was populated from. It's passed as a void* to
309 // ensure it's not accidently used on the IO thread. Before using it on the
310 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
313 prerender::PrerenderTracker
* prerender_tracker
;
316 explicit ProfileIOData(Profile::ProfileType profile_type
);
318 static std::string
GetSSLSessionCacheShard();
320 void InitializeOnUIThread(Profile
* profile
);
321 void ApplyProfileParamsToContext(ChromeURLRequestContext
* 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.
332 void ShutdownOnUIThread();
334 // A ServerBoundCertService object is created by a derived class of
335 // ProfileIOData, and the derived class calls this method to set the
336 // server_bound_cert_service_ member and transfers ownership to the base
338 void set_server_bound_cert_service(
339 net::ServerBoundCertService
* server_bound_cert_service
) const;
341 ChromeNetworkDelegate
* network_delegate() const {
342 return network_delegate_
.get();
345 net::FraudulentCertificateReporter
* fraudulent_certificate_reporter() const {
346 return fraudulent_certificate_reporter_
.get();
349 net::ProxyService
* proxy_service() const {
350 return proxy_service_
.get();
353 base::WeakPtr
<net::HttpServerProperties
> http_server_properties() const;
355 void set_http_server_properties(
356 scoped_ptr
<net::HttpServerProperties
> http_server_properties
) const;
358 ChromeURLRequestContext
* main_request_context() const {
359 return main_request_context_
.get();
362 bool initialized() const {
366 // Destroys the ResourceContext first, to cancel any URLRequests that are
367 // using it still, before we destroy the member variables that those
368 // URLRequests may be accessing.
369 void DestroyResourceContext();
371 // Creates network session and main network transaction factory.
372 scoped_ptr
<net::HttpCache
> CreateMainHttpFactory(
373 const ProfileParams
* profile_params
,
374 net::HttpCache::BackendFactory
* main_backend
) const;
376 // Creates network transaction factory.
377 scoped_ptr
<net::HttpCache
> CreateHttpFactory(
378 net::HttpNetworkSession
* shared_session
,
379 net::HttpCache::BackendFactory
* backend
) const;
381 void SetCookieSettingsForTesting(CookieSettings
* cookie_settings
);
383 void set_signin_names_for_testing(SigninNamesOnIOThread
* signin_names
);
386 class ResourceContext
: public content::ResourceContext
{
388 explicit ResourceContext(ProfileIOData
* io_data
);
389 virtual ~ResourceContext();
391 // ResourceContext implementation:
392 virtual net::HostResolver
* GetHostResolver() OVERRIDE
;
393 virtual net::URLRequestContext
* GetRequestContext() OVERRIDE
;
394 virtual scoped_ptr
<net::ClientCertStore
> CreateClientCertStore() OVERRIDE
;
395 virtual void CreateKeygenHandler(
396 uint32 key_size_in_bits
,
397 const std::string
& challenge_string
,
399 const base::Callback
<void(scoped_ptr
<net::KeygenHandler
>)>& callback
)
401 virtual bool AllowMicAccess(const GURL
& origin
) OVERRIDE
;
402 virtual bool AllowCameraAccess(const GURL
& origin
) OVERRIDE
;
403 virtual SaltCallback
GetMediaDeviceIDSalt() OVERRIDE
;
406 friend class ProfileIOData
;
408 // Helper method that returns true if |type| is allowed for |origin|, false
410 bool AllowContentAccess(const GURL
& origin
, ContentSettingsType type
);
412 ProfileIOData
* const io_data_
;
414 net::HostResolver
* host_resolver_
;
415 net::URLRequestContext
* request_context_
;
418 typedef std::map
<StoragePartitionDescriptor
,
419 ChromeURLRequestContext
*,
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 ProfileParams
* profile_params
,
431 content::ProtocolHandlerMap
* protocol_handlers
,
432 content::URLRequestInterceptorScopedVector
433 request_interceptors
) const = 0;
435 // Initializes the RequestContext for extensions.
436 virtual void InitializeExtensionsRequestContext(
437 ProfileParams
* profile_params
) const = 0;
438 // Does an on-demand initialization of a RequestContext for the given
440 virtual ChromeURLRequestContext
* InitializeAppRequestContext(
441 ChromeURLRequestContext
* main_context
,
442 const StoragePartitionDescriptor
& details
,
443 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
444 protocol_handler_interceptor
,
445 content::ProtocolHandlerMap
* protocol_handlers
,
446 content::URLRequestInterceptorScopedVector
447 request_interceptors
) const = 0;
449 // Does an on-demand initialization of a media RequestContext for the given
451 virtual ChromeURLRequestContext
* InitializeMediaRequestContext(
452 ChromeURLRequestContext
* original_context
,
453 const StoragePartitionDescriptor
& details
) const = 0;
455 // These functions are used to transfer ownership of the lazily initialized
456 // context from ProfileIOData to the URLRequestContextGetter.
457 virtual ChromeURLRequestContext
*
458 AcquireMediaRequestContext() const = 0;
459 virtual ChromeURLRequestContext
* AcquireIsolatedAppRequestContext(
460 ChromeURLRequestContext
* main_context
,
461 const StoragePartitionDescriptor
& partition_descriptor
,
462 scoped_ptr
<ProtocolHandlerRegistry::JobInterceptorFactory
>
463 protocol_handler_interceptor
,
464 content::ProtocolHandlerMap
* protocol_handlers
,
465 content::URLRequestInterceptorScopedVector
466 request_interceptors
) const = 0;
467 virtual ChromeURLRequestContext
*
468 AcquireIsolatedMediaRequestContext(
469 ChromeURLRequestContext
* app_context
,
470 const StoragePartitionDescriptor
& partition_descriptor
) const = 0;
472 // The order *DOES* matter for the majority of these member variables, so
473 // don't move them around unless you know what you're doing!
475 // * ResourceContext references the URLRequestContexts, so
476 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
477 // should be destroyed first.
478 // * URLRequestContexts reference a whole bunch of members, so
479 // URLRequestContext needs to be destroyed before them.
480 // * Therefore, ResourceContext should be listed last, and then the
481 // URLRequestContexts, and then the URLRequestContext members.
482 // * Note that URLRequestContext members have a directed dependency graph
483 // too, so they must themselves be ordered correctly.
485 // Tracks whether or not we've been lazily initialized.
486 mutable bool initialized_
;
488 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
489 // Deleted after lazy initialization.
490 mutable scoped_ptr
<ProfileParams
> profile_params_
;
492 // Provides access to the email addresses of all signed in profiles.
493 mutable scoped_ptr
<SigninNamesOnIOThread
> signin_names_
;
496 mutable base::Callback
<scoped_ptr
<net::ClientCertStore
>()>
497 client_cert_store_factory_
;
499 mutable StringPrefMember google_services_user_account_id_
;
500 mutable StringPrefMember google_services_username_
;
501 mutable StringPrefMember google_services_username_pattern_
;
502 mutable BooleanPrefMember reverse_autologin_enabled_
;
504 // During the reverse autologin request chain processing, this member saves
505 // the email of the google account that is being signed into.
506 std::string reverse_autologin_pending_email_
;
508 mutable StringListPrefMember one_click_signin_rejected_email_list_
;
510 mutable scoped_refptr
<MediaDeviceIDSalt
> media_device_id_salt_
;
512 // Member variables which are pointed to by the various context objects.
513 mutable BooleanPrefMember enable_referrers_
;
514 mutable BooleanPrefMember enable_do_not_track_
;
515 mutable BooleanPrefMember force_safesearch_
;
516 mutable BooleanPrefMember safe_browsing_enabled_
;
517 mutable BooleanPrefMember printing_enabled_
;
518 mutable BooleanPrefMember sync_disabled_
;
519 mutable BooleanPrefMember signin_allowed_
;
520 mutable BooleanPrefMember network_prediction_enabled_
;
521 // TODO(marja): Remove session_startup_pref_ if no longer needed.
522 mutable IntegerPrefMember session_startup_pref_
;
523 mutable BooleanPrefMember quick_check_enabled_
;
524 mutable IntegerPrefMember incognito_availibility_pref_
;
526 // The state of metrics reporting in the browser that this profile runs on.
527 // Unfortunately, since ChromeOS has a separate representation of this state,
528 // we need to make one available based on the platform.
529 #if defined(OS_CHROMEOS)
530 bool enable_metrics_
;
532 BooleanPrefMember enable_metrics_
;
535 #if defined(ENABLE_CONFIGURATION_POLICY)
536 // Pointed to by NetworkDelegate.
537 mutable scoped_ptr
<policy::URLBlacklistManager
> url_blacklist_manager_
;
538 mutable scoped_ptr
<policy::PolicyHeaderIOHelper
> policy_header_helper_
;
541 // Pointed to by URLRequestContext.
542 mutable scoped_refptr
<extensions::InfoMap
> extension_info_map_
;
543 mutable scoped_ptr
<net::ServerBoundCertService
> server_bound_cert_service_
;
544 mutable scoped_ptr
<ChromeNetworkDelegate
> network_delegate_
;
545 mutable scoped_ptr
<net::FraudulentCertificateReporter
>
546 fraudulent_certificate_reporter_
;
547 mutable scoped_ptr
<net::ProxyService
> proxy_service_
;
548 mutable scoped_ptr
<net::TransportSecurityState
> transport_security_state_
;
549 mutable scoped_ptr
<net::HttpServerProperties
>
550 http_server_properties_
;
551 #if defined(OS_CHROMEOS)
552 mutable scoped_ptr
<policy::PolicyCertVerifier
> cert_verifier_
;
553 mutable std::string username_hash_
;
556 mutable scoped_ptr
<net::TransportSecurityPersister
>
557 transport_security_persister_
;
559 // These are only valid in between LazyInitialize() and their accessor being
561 mutable scoped_ptr
<ChromeURLRequestContext
> main_request_context_
;
562 mutable scoped_ptr
<ChromeURLRequestContext
> 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
<CookieSettings
> cookie_settings_
;
571 mutable scoped_refptr
<HostContentSettingsMap
> host_content_settings_map_
;
573 mutable scoped_ptr
<ChromeHttpUserAgentSettings
>
574 chrome_http_user_agent_settings_
;
576 #if defined(ENABLE_MANAGED_USERS)
577 mutable scoped_refptr
<const ManagedModeURLFilter
> managed_mode_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_