Update mojo surfaces bindings and mojo/cc/ glue
[chromium-blink-merge.git] / chrome / browser / profiles / profile_io_data.h
blobeed446ea6c26e2d4d35af22c122920602d4e755f
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_
8 #include <map>
9 #include <string>
10 #include <vector>
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 "components/data_reduction_proxy/browser/data_reduction_proxy_usage_stats.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_job_factory.h"
34 class ChromeHttpUserAgentSettings;
35 class ChromeNetworkDelegate;
36 class ChromeURLRequestContextGetter;
37 class CookieSettings;
38 class DevToolsNetworkController;
39 class HostContentSettingsMap;
40 class MediaDeviceIDSalt;
41 class ProtocolHandlerRegistry;
42 class SigninNamesOnIOThread;
43 class SupervisedUserURLFilter;
45 namespace extensions {
46 class InfoMap;
49 namespace net {
50 class ChannelIDService;
51 class CookieStore;
52 class FraudulentCertificateReporter;
53 class FtpTransactionFactory;
54 class HttpServerProperties;
55 class HttpTransactionFactory;
56 class ProxyConfigService;
57 class ProxyService;
58 class SSLConfigService;
59 class TransportSecurityPersister;
60 class TransportSecurityState;
61 class URLRequestJobFactoryImpl;
62 } // namespace net
64 namespace policy {
65 class PolicyCertVerifier;
66 class PolicyHeaderIOHelper;
67 class URLBlacklistManager;
68 } // namespace policy
70 namespace prerender {
71 class PrerenderTracker;
74 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
75 // thread that is owned by a Profile, such as, but not limited to, network
76 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
77 // ProfileIOData, but will make sure to delete it on the IO thread (except
78 // possibly in unit tests where there is no IO thread).
79 class ProfileIOData {
80 public:
81 typedef std::vector<scoped_refptr<ChromeURLRequestContextGetter>>
82 ChromeURLRequestContextGetterVector;
84 virtual ~ProfileIOData();
86 static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
88 // Returns true if |scheme| is handled in Chrome, or by default handlers in
89 // net::URLRequest.
90 static bool IsHandledProtocol(const std::string& scheme);
92 // Returns true if |url| is handled in Chrome, or by default handlers in
93 // net::URLRequest.
94 static bool IsHandledURL(const GURL& url);
96 // Utility to install additional WebUI handlers into the |job_factory|.
97 // Ownership of the handlers is transfered from |protocol_handlers|
98 // to the |job_factory|.
99 static void InstallProtocolHandlers(
100 net::URLRequestJobFactoryImpl* job_factory,
101 content::ProtocolHandlerMap* protocol_handlers);
103 // Called by Profile.
104 content::ResourceContext* GetResourceContext() const;
106 // Initializes the ProfileIOData object and primes the RequestContext
107 // generation. Must be called prior to any of the Get*() methods other than
108 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
109 void Init(
110 content::ProtocolHandlerMap* protocol_handlers,
111 content::URLRequestInterceptorScopedVector request_interceptors) const;
113 net::URLRequestContext* GetMainRequestContext() const;
114 net::URLRequestContext* GetMediaRequestContext() const;
115 net::URLRequestContext* GetExtensionsRequestContext() const;
116 net::URLRequestContext* GetIsolatedAppRequestContext(
117 net::URLRequestContext* main_context,
118 const StoragePartitionDescriptor& partition_descriptor,
119 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
120 protocol_handler_interceptor,
121 content::ProtocolHandlerMap* protocol_handlers,
122 content::URLRequestInterceptorScopedVector request_interceptors) const;
123 net::URLRequestContext* GetIsolatedMediaRequestContext(
124 net::URLRequestContext* app_context,
125 const StoragePartitionDescriptor& partition_descriptor) const;
127 // These are useful when the Chrome layer is called from the content layer
128 // with a content::ResourceContext, and they want access to Chrome data for
129 // that profile.
130 extensions::InfoMap* GetExtensionInfoMap() const;
131 CookieSettings* GetCookieSettings() const;
132 HostContentSettingsMap* GetHostContentSettingsMap() const;
134 IntegerPrefMember* session_startup_pref() const {
135 return &session_startup_pref_;
138 SigninNamesOnIOThread* signin_names() const {
139 return signin_names_.get();
142 StringPrefMember* google_services_account_id() const {
143 return &google_services_user_account_id_;
146 StringPrefMember* google_services_username() const {
147 return &google_services_username_;
150 StringPrefMember* google_services_username_pattern() const {
151 return &google_services_username_pattern_;
154 BooleanPrefMember* reverse_autologin_enabled() const {
155 return &reverse_autologin_enabled_;
158 const std::string& reverse_autologin_pending_email() const {
159 return reverse_autologin_pending_email_;
162 void set_reverse_autologin_pending_email(const std::string& email) {
163 reverse_autologin_pending_email_ = email;
166 StringListPrefMember* one_click_signin_rejected_email_list() const {
167 return &one_click_signin_rejected_email_list_;
170 net::URLRequestContext* extensions_request_context() const {
171 return extensions_request_context_.get();
174 BooleanPrefMember* safe_browsing_enabled() const {
175 return &safe_browsing_enabled_;
178 #if defined(SPDY_PROXY_AUTH_ORIGIN)
179 // TODO(feng): move the function to protected area.
180 // IsDataReductionProxyEnabled() should be used as public API.
181 BooleanPrefMember* data_reduction_proxy_enabled() const {
182 return &data_reduction_proxy_enabled_;
184 #endif
186 BooleanPrefMember* printing_enabled() const {
187 return &printing_enabled_;
190 BooleanPrefMember* sync_disabled() const {
191 return &sync_disabled_;
194 BooleanPrefMember* signin_allowed() const {
195 return &signin_allowed_;
198 // TODO(bnc): remove per https://crbug.com/334602.
199 BooleanPrefMember* network_prediction_enabled() const {
200 return &network_prediction_enabled_;
203 IntegerPrefMember* network_prediction_options() const {
204 return &network_prediction_options_;
207 content::ResourceContext::SaltCallback GetMediaDeviceIDSalt() const;
209 DevToolsNetworkController* network_controller() const {
210 return network_controller_.get();
213 net::TransportSecurityState* transport_security_state() const {
214 return transport_security_state_.get();
217 #if defined(OS_CHROMEOS)
218 std::string username_hash() const {
219 return username_hash_;
222 bool use_system_key_slot() const { return use_system_key_slot_; }
223 #endif
225 Profile::ProfileType profile_type() const {
226 return profile_type_;
229 bool IsOffTheRecord() const;
231 IntegerPrefMember* incognito_availibility() const {
232 return &incognito_availibility_pref_;
235 #if defined(ENABLE_CONFIGURATION_POLICY)
236 policy::PolicyHeaderIOHelper* policy_header_helper() const {
237 return policy_header_helper_.get();
239 #endif
241 #if defined(ENABLE_MANAGED_USERS)
242 const SupervisedUserURLFilter* supervised_user_url_filter() const {
243 return supervised_user_url_filter_.get();
245 #endif
247 // Initialize the member needed to track the metrics enabled state. This is
248 // only to be called on the UI thread.
249 void InitializeMetricsEnabledStateOnUIThread();
251 // Returns whether or not metrics reporting is enabled in the browser instance
252 // on which this profile resides. This is safe for use from the IO thread, and
253 // should only be called from there.
254 bool GetMetricsEnabledStateOnIOThread() const;
256 #if defined(OS_ANDROID)
257 // Returns whether or not data reduction proxy is enabled in the browser
258 // instance on which this profile resides.
259 bool IsDataReductionProxyEnabled() const;
260 #endif
262 void set_client_cert_store_factory_for_testing(
263 const base::Callback<scoped_ptr<net::ClientCertStore>()>& factory) {
264 client_cert_store_factory_ = factory;
267 protected:
268 // A URLRequestContext for media that owns its HTTP factory, to ensure
269 // it is deleted.
270 class MediaRequestContext : public net::URLRequestContext {
271 public:
272 MediaRequestContext();
274 void SetHttpTransactionFactory(
275 scoped_ptr<net::HttpTransactionFactory> http_factory);
277 private:
278 virtual ~MediaRequestContext();
280 scoped_ptr<net::HttpTransactionFactory> http_factory_;
283 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
284 // to ensure they are deleted.
285 class AppRequestContext : public net::URLRequestContext {
286 public:
287 AppRequestContext();
289 void SetCookieStore(net::CookieStore* cookie_store);
290 void SetHttpTransactionFactory(
291 scoped_ptr<net::HttpTransactionFactory> http_factory);
292 void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
294 private:
295 virtual ~AppRequestContext();
297 scoped_refptr<net::CookieStore> cookie_store_;
298 scoped_ptr<net::HttpTransactionFactory> http_factory_;
299 scoped_ptr<net::URLRequestJobFactory> job_factory_;
302 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
303 // initialization.
304 struct ProfileParams {
305 ProfileParams();
306 ~ProfileParams();
308 base::FilePath path;
309 IOThread* io_thread;
310 scoped_refptr<CookieSettings> cookie_settings;
311 scoped_refptr<HostContentSettingsMap> host_content_settings_map;
312 scoped_refptr<net::SSLConfigService> ssl_config_service;
313 scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
314 #if defined(ENABLE_EXTENSIONS)
315 scoped_refptr<extensions::InfoMap> extension_info_map;
316 #endif
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 // We need to initialize the ProxyConfigService from the UI thread
326 // because on linux it relies on initializing things through gconf,
327 // and needs to be on the main thread.
328 scoped_ptr<net::ProxyConfigService> proxy_config_service;
330 #if defined(ENABLE_MANAGED_USERS)
331 scoped_refptr<const SupervisedUserURLFilter> supervised_user_url_filter;
332 #endif
334 #if defined(OS_CHROMEOS)
335 std::string username_hash;
336 bool use_system_key_slot;
337 #endif
339 // The profile this struct was populated from. It's passed as a void* to
340 // ensure it's not accidently used on the IO thread. Before using it on the
341 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
342 void* profile;
344 prerender::PrerenderTracker* prerender_tracker;
347 explicit ProfileIOData(Profile::ProfileType profile_type);
349 static std::string GetSSLSessionCacheShard();
351 void InitializeOnUIThread(Profile* profile);
352 void ApplyProfileParamsToContext(net::URLRequestContext* context) const;
354 scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults(
355 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
356 content::URLRequestInterceptorScopedVector request_interceptors,
357 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
358 protocol_handler_interceptor,
359 net::NetworkDelegate* network_delegate,
360 net::FtpTransactionFactory* ftp_transaction_factory) const;
362 // Called when the profile is destroyed.
363 void ShutdownOnUIThread(
364 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters);
366 // A ChannelIDService object is created by a derived class of
367 // ProfileIOData, and the derived class calls this method to set the
368 // channel_id_service_ member and transfers ownership to the base
369 // class.
370 void set_channel_id_service(
371 net::ChannelIDService* channel_id_service) const;
373 ChromeNetworkDelegate* network_delegate() const {
374 return network_delegate_.get();
377 net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
378 return fraudulent_certificate_reporter_.get();
381 net::ProxyService* proxy_service() const {
382 return proxy_service_.get();
385 base::WeakPtr<net::HttpServerProperties> http_server_properties() const;
387 void set_http_server_properties(
388 scoped_ptr<net::HttpServerProperties> http_server_properties) const;
390 net::URLRequestContext* main_request_context() const {
391 return main_request_context_.get();
394 bool initialized() const {
395 return initialized_;
398 // Destroys the ResourceContext first, to cancel any URLRequests that are
399 // using it still, before we destroy the member variables that those
400 // URLRequests may be accessing.
401 void DestroyResourceContext();
403 // Creates network session and main network transaction factory.
404 scoped_ptr<net::HttpCache> CreateMainHttpFactory(
405 const ProfileParams* profile_params,
406 net::HttpCache::BackendFactory* main_backend) const;
408 // Creates network transaction factory.
409 scoped_ptr<net::HttpCache> CreateHttpFactory(
410 net::HttpNetworkSession* shared_session,
411 net::HttpCache::BackendFactory* backend) const;
413 void SetCookieSettingsForTesting(CookieSettings* cookie_settings);
415 void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names);
417 private:
418 class ResourceContext : public content::ResourceContext {
419 public:
420 explicit ResourceContext(ProfileIOData* io_data);
421 virtual ~ResourceContext();
423 // ResourceContext implementation:
424 virtual net::HostResolver* GetHostResolver() OVERRIDE;
425 virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
426 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE;
427 virtual void CreateKeygenHandler(
428 uint32 key_size_in_bits,
429 const std::string& challenge_string,
430 const GURL& url,
431 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback)
432 OVERRIDE;
433 virtual bool AllowMicAccess(const GURL& origin) OVERRIDE;
434 virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE;
435 virtual SaltCallback GetMediaDeviceIDSalt() OVERRIDE;
437 private:
438 friend class ProfileIOData;
440 // Helper method that returns true if |type| is allowed for |origin|, false
441 // otherwise.
442 bool AllowContentAccess(const GURL& origin, ContentSettingsType type);
444 ProfileIOData* const io_data_;
446 net::HostResolver* host_resolver_;
447 net::URLRequestContext* request_context_;
450 typedef std::map<StoragePartitionDescriptor,
451 net::URLRequestContext*,
452 StoragePartitionDescriptorLess>
453 URLRequestContextMap;
455 // --------------------------------------------
456 // Virtual interface for subtypes to implement:
457 // --------------------------------------------
459 // Does the actual initialization of the ProfileIOData subtype. Subtypes
460 // should use the static helper functions above to implement this.
461 virtual void InitializeInternal(
462 ProfileParams* profile_params,
463 content::ProtocolHandlerMap* protocol_handlers,
464 content::URLRequestInterceptorScopedVector
465 request_interceptors) const = 0;
467 // Initializes the RequestContext for extensions.
468 virtual void InitializeExtensionsRequestContext(
469 ProfileParams* profile_params) const = 0;
470 // Does an on-demand initialization of a RequestContext for the given
471 // isolated app.
472 virtual net::URLRequestContext* InitializeAppRequestContext(
473 net::URLRequestContext* main_context,
474 const StoragePartitionDescriptor& details,
475 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
476 protocol_handler_interceptor,
477 content::ProtocolHandlerMap* protocol_handlers,
478 content::URLRequestInterceptorScopedVector
479 request_interceptors) const = 0;
481 // Does an on-demand initialization of a media RequestContext for the given
482 // isolated app.
483 virtual net::URLRequestContext* InitializeMediaRequestContext(
484 net::URLRequestContext* original_context,
485 const StoragePartitionDescriptor& details) const = 0;
487 // These functions are used to transfer ownership of the lazily initialized
488 // context from ProfileIOData to the URLRequestContextGetter.
489 virtual net::URLRequestContext*
490 AcquireMediaRequestContext() const = 0;
491 virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
492 net::URLRequestContext* main_context,
493 const StoragePartitionDescriptor& partition_descriptor,
494 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
495 protocol_handler_interceptor,
496 content::ProtocolHandlerMap* protocol_handlers,
497 content::URLRequestInterceptorScopedVector
498 request_interceptors) const = 0;
499 virtual net::URLRequestContext*
500 AcquireIsolatedMediaRequestContext(
501 net::URLRequestContext* app_context,
502 const StoragePartitionDescriptor& partition_descriptor) const = 0;
504 // The order *DOES* matter for the majority of these member variables, so
505 // don't move them around unless you know what you're doing!
506 // General rules:
507 // * ResourceContext references the URLRequestContexts, so
508 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
509 // should be destroyed first.
510 // * URLRequestContexts reference a whole bunch of members, so
511 // URLRequestContext needs to be destroyed before them.
512 // * Therefore, ResourceContext should be listed last, and then the
513 // URLRequestContexts, and then the URLRequestContext members.
514 // * Note that URLRequestContext members have a directed dependency graph
515 // too, so they must themselves be ordered correctly.
517 // Tracks whether or not we've been lazily initialized.
518 mutable bool initialized_;
520 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
521 // Deleted after lazy initialization.
522 mutable scoped_ptr<ProfileParams> profile_params_;
524 // Provides access to the email addresses of all signed in profiles.
525 mutable scoped_ptr<SigninNamesOnIOThread> signin_names_;
527 // Used for testing.
528 mutable base::Callback<scoped_ptr<net::ClientCertStore>()>
529 client_cert_store_factory_;
531 mutable StringPrefMember google_services_user_account_id_;
532 mutable StringPrefMember google_services_username_;
533 mutable StringPrefMember google_services_username_pattern_;
534 mutable BooleanPrefMember reverse_autologin_enabled_;
536 // During the reverse autologin request chain processing, this member saves
537 // the email of the google account that is being signed into.
538 std::string reverse_autologin_pending_email_;
540 mutable StringListPrefMember one_click_signin_rejected_email_list_;
542 mutable scoped_refptr<MediaDeviceIDSalt> media_device_id_salt_;
544 // Member variables which are pointed to by the various context objects.
545 mutable BooleanPrefMember enable_referrers_;
546 mutable BooleanPrefMember enable_do_not_track_;
547 mutable BooleanPrefMember force_safesearch_;
548 mutable BooleanPrefMember safe_browsing_enabled_;
549 #if defined(SPDY_PROXY_AUTH_ORIGIN)
550 mutable BooleanPrefMember data_reduction_proxy_enabled_;
551 #endif
552 mutable BooleanPrefMember printing_enabled_;
553 mutable BooleanPrefMember sync_disabled_;
554 mutable BooleanPrefMember signin_allowed_;
555 mutable BooleanPrefMember network_prediction_enabled_;
556 mutable IntegerPrefMember network_prediction_options_;
557 // TODO(marja): Remove session_startup_pref_ if no longer needed.
558 mutable IntegerPrefMember session_startup_pref_;
559 mutable BooleanPrefMember quick_check_enabled_;
560 mutable IntegerPrefMember incognito_availibility_pref_;
562 // The state of metrics reporting in the browser that this profile runs on.
563 // Unfortunately, since ChromeOS has a separate representation of this state,
564 // we need to make one available based on the platform.
565 #if defined(OS_CHROMEOS)
566 bool enable_metrics_;
567 #else
568 BooleanPrefMember enable_metrics_;
569 #endif
571 #if defined(ENABLE_CONFIGURATION_POLICY)
572 // Pointed to by NetworkDelegate.
573 mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
574 mutable scoped_ptr<policy::PolicyHeaderIOHelper> policy_header_helper_;
575 #endif
577 // Pointed to by URLRequestContext.
578 #if defined(ENABLE_EXTENSIONS)
579 mutable scoped_refptr<extensions::InfoMap> extension_info_map_;
580 #endif
581 mutable scoped_ptr<net::ChannelIDService> channel_id_service_;
582 mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_;
583 mutable scoped_ptr<net::FraudulentCertificateReporter>
584 fraudulent_certificate_reporter_;
585 mutable scoped_ptr<net::ProxyService> proxy_service_;
586 mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
587 mutable scoped_ptr<net::HttpServerProperties>
588 http_server_properties_;
589 #if defined(OS_CHROMEOS)
590 mutable scoped_ptr<policy::PolicyCertVerifier> cert_verifier_;
591 mutable std::string username_hash_;
592 mutable bool use_system_key_slot_;
593 #endif
595 mutable scoped_ptr<net::TransportSecurityPersister>
596 transport_security_persister_;
598 // These are only valid in between LazyInitialize() and their accessor being
599 // called.
600 mutable scoped_ptr<net::URLRequestContext> main_request_context_;
601 mutable scoped_ptr<net::URLRequestContext> extensions_request_context_;
602 // One URLRequestContext per isolated app for main and media requests.
603 mutable URLRequestContextMap app_request_context_map_;
604 mutable URLRequestContextMap isolated_media_request_context_map_;
606 mutable scoped_ptr<ResourceContext> resource_context_;
608 mutable scoped_refptr<CookieSettings> cookie_settings_;
610 mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
612 mutable scoped_ptr<ChromeHttpUserAgentSettings>
613 chrome_http_user_agent_settings_;
615 #if defined(ENABLE_MANAGED_USERS)
616 mutable scoped_refptr<const SupervisedUserURLFilter>
617 supervised_user_url_filter_;
618 #endif
620 mutable scoped_ptr<DevToolsNetworkController> network_controller_;
622 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
623 bool initialized_on_UI_thread_;
625 const Profile::ProfileType profile_type_;
627 DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
630 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_