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_network_session.h"
28 #include "net/url_request/url_request_job_factory.h"
30 class ChromeHttpUserAgentSettings;
31 class ChromeNetworkDelegate;
33 class HostContentSettingsMap;
34 class ManagedModeURLFilter;
35 class MediaDeviceIDSalt;
36 class ProtocolHandlerRegistry;
37 class SigninNamesOnIOThread;
39 namespace extensions {
45 class FraudulentCertificateReporter;
46 class FtpTransactionFactory;
47 class HttpServerProperties;
48 class HttpTransactionFactory;
49 class ServerBoundCertService;
50 class ProxyConfigService;
52 class SSLConfigService;
53 class TransportSecurityPersister;
54 class TransportSecurityState;
55 class URLRequestJobFactoryImpl;
59 class PolicyCertVerifier;
60 class PolicyHeaderIOHelper;
61 class URLBlacklistManager;
64 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
65 // thread that is owned by a Profile, such as, but not limited to, network
66 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
67 // ProfileIOData, but will make sure to delete it on the IO thread (except
68 // possibly in unit tests where there is no IO thread).
71 virtual ~ProfileIOData();
73 static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
75 // Returns true if |scheme| is handled in Chrome, or by default handlers in
77 static bool IsHandledProtocol(const std::string& scheme);
79 // Returns true if |url| is handled in Chrome, or by default handlers in
81 static bool IsHandledURL(const GURL& url);
83 // Utility to install additional WebUI handlers into the |job_factory|.
84 // Ownership of the handlers is transfered from |protocol_handlers|
85 // to the |job_factory|.
86 static void InstallProtocolHandlers(
87 net::URLRequestJobFactoryImpl* job_factory,
88 content::ProtocolHandlerMap* protocol_handlers);
91 content::ResourceContext* GetResourceContext() const;
93 // Initializes the ProfileIOData object and primes the RequestContext
94 // generation. Must be called prior to any of the Get*() methods other than
95 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
96 void Init(content::ProtocolHandlerMap* protocol_handlers,
97 content::ProtocolHandlerScopedVector protocol_interceptors) const;
99 ChromeURLRequestContext* GetMainRequestContext() const;
100 ChromeURLRequestContext* GetMediaRequestContext() const;
101 ChromeURLRequestContext* GetExtensionsRequestContext() const;
102 ChromeURLRequestContext* GetIsolatedAppRequestContext(
103 ChromeURLRequestContext* main_context,
104 const StoragePartitionDescriptor& partition_descriptor,
105 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
106 protocol_handler_interceptor,
107 content::ProtocolHandlerMap* protocol_handlers,
108 content::ProtocolHandlerScopedVector protocol_interceptors) const;
109 ChromeURLRequestContext* GetIsolatedMediaRequestContext(
110 ChromeURLRequestContext* app_context,
111 const StoragePartitionDescriptor& partition_descriptor) const;
113 // These are useful when the Chrome layer is called from the content layer
114 // with a content::ResourceContext, and they want access to Chrome data for
116 extensions::InfoMap* GetExtensionInfoMap() const;
117 CookieSettings* GetCookieSettings() const;
118 HostContentSettingsMap* GetHostContentSettingsMap() const;
120 IntegerPrefMember* session_startup_pref() const {
121 return &session_startup_pref_;
124 SigninNamesOnIOThread* signin_names() const {
125 return signin_names_.get();
128 StringPrefMember* google_services_account_id() const {
129 return &google_services_user_account_id_;
132 StringPrefMember* google_services_username() const {
133 return &google_services_username_;
136 StringPrefMember* google_services_username_pattern() const {
137 return &google_services_username_pattern_;
140 BooleanPrefMember* reverse_autologin_enabled() const {
141 return &reverse_autologin_enabled_;
144 const std::string& reverse_autologin_pending_email() const {
145 return reverse_autologin_pending_email_;
148 void set_reverse_autologin_pending_email(const std::string& email) {
149 reverse_autologin_pending_email_ = email;
152 StringListPrefMember* one_click_signin_rejected_email_list() const {
153 return &one_click_signin_rejected_email_list_;
156 ChromeURLRequestContext* extensions_request_context() const {
157 return extensions_request_context_.get();
160 BooleanPrefMember* safe_browsing_enabled() const {
161 return &safe_browsing_enabled_;
164 BooleanPrefMember* printing_enabled() const {
165 return &printing_enabled_;
168 BooleanPrefMember* sync_disabled() const {
169 return &sync_disabled_;
172 BooleanPrefMember* signin_allowed() const {
173 return &signin_allowed_;
176 BooleanPrefMember* network_prediction_enabled() const {
177 return &network_prediction_enabled_;
180 content::ResourceContext::SaltCallback GetMediaDeviceIDSalt() const;
182 net::TransportSecurityState* transport_security_state() const {
183 return transport_security_state_.get();
186 #if defined(OS_CHROMEOS)
187 std::string username_hash() const {
188 return username_hash_;
192 Profile::ProfileType profile_type() const {
193 return profile_type_;
196 bool IsOffTheRecord() const;
198 #if defined(ENABLE_CONFIGURATION_POLICY)
199 policy::PolicyHeaderIOHelper* policy_header_helper() const {
200 return policy_header_helper_.get();
204 #if defined(ENABLE_MANAGED_USERS)
205 const ManagedModeURLFilter* managed_mode_url_filter() const {
206 return managed_mode_url_filter_.get();
210 // Initialize the member needed to track the metrics enabled state. This is
211 // only to be called on the UI thread.
212 void InitializeMetricsEnabledStateOnUIThread();
214 // Returns whether or not metrics reporting is enabled in the browser instance
215 // on which this profile resides. This is safe for use from the IO thread, and
216 // should only be called from there.
217 bool GetMetricsEnabledStateOnIOThread() const;
219 void set_client_cert_store_factory_for_testing(
220 const base::Callback<scoped_ptr<net::ClientCertStore>()>& factory) {
221 client_cert_store_factory_ = factory;
225 // A URLRequestContext for media that owns its HTTP factory, to ensure
227 class MediaRequestContext : public ChromeURLRequestContext {
229 MediaRequestContext();
231 void SetHttpTransactionFactory(
232 scoped_ptr<net::HttpTransactionFactory> http_factory);
235 virtual ~MediaRequestContext();
237 scoped_ptr<net::HttpTransactionFactory> http_factory_;
240 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
241 // to ensure they are deleted.
242 class AppRequestContext : public ChromeURLRequestContext {
246 void SetCookieStore(net::CookieStore* cookie_store);
247 void SetHttpTransactionFactory(
248 scoped_ptr<net::HttpTransactionFactory> http_factory);
249 void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
252 virtual ~AppRequestContext();
254 scoped_refptr<net::CookieStore> cookie_store_;
255 scoped_ptr<net::HttpTransactionFactory> http_factory_;
256 scoped_ptr<net::URLRequestJobFactory> job_factory_;
259 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
261 struct ProfileParams {
267 scoped_refptr<CookieSettings> cookie_settings;
268 scoped_refptr<HostContentSettingsMap> host_content_settings_map;
269 scoped_refptr<net::SSLConfigService> ssl_config_service;
270 scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
271 scoped_refptr<extensions::InfoMap> extension_info_map;
273 // This pointer exists only as a means of conveying a url job factory
274 // pointer from the protocol handler registry on the UI thread to the
275 // the URLRequestContext on the IO thread. The consumer MUST take
276 // ownership of the object by calling release() on this pointer.
277 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
278 protocol_handler_interceptor;
280 // We need to initialize the ProxyConfigService from the UI thread
281 // because on linux it relies on initializing things through gconf,
282 // and needs to be on the main thread.
283 scoped_ptr<net::ProxyConfigService> proxy_config_service;
285 #if defined(ENABLE_MANAGED_USERS)
286 scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter;
289 #if defined(OS_CHROMEOS)
290 std::string username_hash;
293 // The profile this struct was populated from. It's passed as a void* to
294 // ensure it's not accidently used on the IO thread. Before using it on the
295 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
299 explicit ProfileIOData(Profile::ProfileType profile_type);
301 static std::string GetSSLSessionCacheShard();
303 void InitializeOnUIThread(Profile* profile);
304 void ApplyProfileParamsToContext(ChromeURLRequestContext* context) const;
306 scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults(
307 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
308 content::ProtocolHandlerScopedVector protocol_interceptors,
309 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
310 protocol_handler_interceptor,
311 net::NetworkDelegate* network_delegate,
312 net::FtpTransactionFactory* ftp_transaction_factory) const;
314 // Called when the profile is destroyed.
315 void ShutdownOnUIThread();
317 // A ServerBoundCertService object is created by a derived class of
318 // ProfileIOData, and the derived class calls this method to set the
319 // server_bound_cert_service_ member and transfers ownership to the base
321 void set_server_bound_cert_service(
322 net::ServerBoundCertService* server_bound_cert_service) const;
324 ChromeNetworkDelegate* network_delegate() const {
325 return network_delegate_.get();
328 net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
329 return fraudulent_certificate_reporter_.get();
332 net::ProxyService* proxy_service() const {
333 return proxy_service_.get();
336 base::WeakPtr<net::HttpServerProperties> http_server_properties() const;
338 void set_http_server_properties(
339 scoped_ptr<net::HttpServerProperties> http_server_properties) const;
341 ChromeURLRequestContext* main_request_context() const {
342 return main_request_context_.get();
345 bool initialized() const {
349 // Destroys the ResourceContext first, to cancel any URLRequests that are
350 // using it still, before we destroy the member variables that those
351 // URLRequests may be accessing.
352 void DestroyResourceContext();
354 // Fills in fields of params using values from main_request_context_ and the
355 // IOThread associated with profile_params.
356 void PopulateNetworkSessionParams(
357 const ProfileParams* profile_params,
358 net::HttpNetworkSession::Params* params) const;
360 void SetCookieSettingsForTesting(CookieSettings* cookie_settings);
362 void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names);
365 class ResourceContext : public content::ResourceContext {
367 explicit ResourceContext(ProfileIOData* io_data);
368 virtual ~ResourceContext();
370 // ResourceContext implementation:
371 virtual net::HostResolver* GetHostResolver() OVERRIDE;
372 virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
373 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE;
374 virtual void CreateKeygenHandler(
375 uint32 key_size_in_bits,
376 const std::string& challenge_string,
378 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback)
380 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE;
381 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE;
382 virtual SaltCallback GetMediaDeviceIDSalt() OVERRIDE;
385 friend class ProfileIOData;
387 // Helper method that returns true if |type| is allowed for |origin|, false
389 bool AllowContentAccess(const GURL& origin, ContentSettingsType type);
391 ProfileIOData* const io_data_;
393 net::HostResolver* host_resolver_;
394 net::URLRequestContext* request_context_;
397 typedef std::map<StoragePartitionDescriptor,
398 ChromeURLRequestContext*,
399 StoragePartitionDescriptorLess>
400 URLRequestContextMap;
402 // --------------------------------------------
403 // Virtual interface for subtypes to implement:
404 // --------------------------------------------
406 // Does the actual initialization of the ProfileIOData subtype. Subtypes
407 // should use the static helper functions above to implement this.
408 virtual void InitializeInternal(
409 ProfileParams* profile_params,
410 content::ProtocolHandlerMap* protocol_handlers,
411 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0;
413 // Initializes the RequestContext for extensions.
414 virtual void InitializeExtensionsRequestContext(
415 ProfileParams* profile_params) const = 0;
416 // Does an on-demand initialization of a RequestContext for the given
418 virtual ChromeURLRequestContext* InitializeAppRequestContext(
419 ChromeURLRequestContext* main_context,
420 const StoragePartitionDescriptor& details,
421 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
422 protocol_handler_interceptor,
423 content::ProtocolHandlerMap* protocol_handlers,
424 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0;
426 // Does an on-demand initialization of a media RequestContext for the given
428 virtual ChromeURLRequestContext* InitializeMediaRequestContext(
429 ChromeURLRequestContext* original_context,
430 const StoragePartitionDescriptor& details) const = 0;
432 // These functions are used to transfer ownership of the lazily initialized
433 // context from ProfileIOData to the URLRequestContextGetter.
434 virtual ChromeURLRequestContext*
435 AcquireMediaRequestContext() const = 0;
436 virtual ChromeURLRequestContext* AcquireIsolatedAppRequestContext(
437 ChromeURLRequestContext* main_context,
438 const StoragePartitionDescriptor& partition_descriptor,
439 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
440 protocol_handler_interceptor,
441 content::ProtocolHandlerMap* protocol_handlers,
442 content::ProtocolHandlerScopedVector protocol_interceptors) const = 0;
443 virtual ChromeURLRequestContext*
444 AcquireIsolatedMediaRequestContext(
445 ChromeURLRequestContext* app_context,
446 const StoragePartitionDescriptor& partition_descriptor) const = 0;
448 // The order *DOES* matter for the majority of these member variables, so
449 // don't move them around unless you know what you're doing!
451 // * ResourceContext references the URLRequestContexts, so
452 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
453 // should be destroyed first.
454 // * URLRequestContexts reference a whole bunch of members, so
455 // URLRequestContext needs to be destroyed before them.
456 // * Therefore, ResourceContext should be listed last, and then the
457 // URLRequestContexts, and then the URLRequestContext members.
458 // * Note that URLRequestContext members have a directed dependency graph
459 // too, so they must themselves be ordered correctly.
461 // Tracks whether or not we've been lazily initialized.
462 mutable bool initialized_;
464 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
465 // Deleted after lazy initialization.
466 mutable scoped_ptr<ProfileParams> profile_params_;
468 // Provides access to the email addresses of all signed in profiles.
469 mutable scoped_ptr<SigninNamesOnIOThread> signin_names_;
472 mutable base::Callback<scoped_ptr<net::ClientCertStore>()>
473 client_cert_store_factory_;
475 mutable StringPrefMember google_services_user_account_id_;
476 mutable StringPrefMember google_services_username_;
477 mutable StringPrefMember google_services_username_pattern_;
478 mutable BooleanPrefMember reverse_autologin_enabled_;
480 // During the reverse autologin request chain processing, this member saves
481 // the email of the google account that is being signed into.
482 std::string reverse_autologin_pending_email_;
484 mutable StringListPrefMember one_click_signin_rejected_email_list_;
486 mutable scoped_refptr<MediaDeviceIDSalt> media_device_id_salt_;
488 // Member variables which are pointed to by the various context objects.
489 mutable BooleanPrefMember enable_referrers_;
490 mutable BooleanPrefMember enable_do_not_track_;
491 mutable BooleanPrefMember force_safesearch_;
492 mutable BooleanPrefMember safe_browsing_enabled_;
493 mutable BooleanPrefMember printing_enabled_;
494 mutable BooleanPrefMember sync_disabled_;
495 mutable BooleanPrefMember signin_allowed_;
496 mutable BooleanPrefMember network_prediction_enabled_;
497 // TODO(marja): Remove session_startup_pref_ if no longer needed.
498 mutable IntegerPrefMember session_startup_pref_;
499 mutable BooleanPrefMember quick_check_enabled_;
501 // The state of metrics reporting in the browser that this profile runs on.
502 // Unfortunately, since ChromeOS has a separate representation of this state,
503 // we need to make one available based on the platform.
504 #if defined(OS_CHROMEOS)
505 bool enable_metrics_;
507 BooleanPrefMember enable_metrics_;
510 #if defined(ENABLE_CONFIGURATION_POLICY)
511 // Pointed to by NetworkDelegate.
512 mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
513 mutable scoped_ptr<policy::PolicyHeaderIOHelper> policy_header_helper_;
516 // Pointed to by URLRequestContext.
517 mutable scoped_refptr<extensions::InfoMap> extension_info_map_;
518 mutable scoped_ptr<net::ServerBoundCertService> server_bound_cert_service_;
519 mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_;
520 mutable scoped_ptr<net::FraudulentCertificateReporter>
521 fraudulent_certificate_reporter_;
522 mutable scoped_ptr<net::ProxyService> proxy_service_;
523 mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
524 mutable scoped_ptr<net::HttpServerProperties>
525 http_server_properties_;
526 #if defined(OS_CHROMEOS)
527 mutable scoped_ptr<policy::PolicyCertVerifier> cert_verifier_;
528 mutable std::string username_hash_;
531 mutable scoped_ptr<net::TransportSecurityPersister>
532 transport_security_persister_;
534 // These are only valid in between LazyInitialize() and their accessor being
536 mutable scoped_ptr<ChromeURLRequestContext> main_request_context_;
537 mutable scoped_ptr<ChromeURLRequestContext> extensions_request_context_;
538 // One URLRequestContext per isolated app for main and media requests.
539 mutable URLRequestContextMap app_request_context_map_;
540 mutable URLRequestContextMap isolated_media_request_context_map_;
542 mutable scoped_ptr<ResourceContext> resource_context_;
544 mutable scoped_refptr<CookieSettings> cookie_settings_;
546 mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
548 mutable scoped_ptr<ChromeHttpUserAgentSettings>
549 chrome_http_user_agent_settings_;
551 #if defined(ENABLE_MANAGED_USERS)
552 mutable scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter_;
555 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
556 bool initialized_on_UI_thread_;
558 const Profile::ProfileType profile_type_;
560 DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
563 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_