Move Webstore URL concepts to //extensions and out
[chromium-blink-merge.git] / chrome / browser / profiles / profile_io_data.h
blob34fcb6451f40d5ef92382fb79f7861332491d0ab
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 CertVerifier;
51 class ChannelIDService;
52 class CookieStore;
53 class FraudulentCertificateReporter;
54 class FtpTransactionFactory;
55 class HttpServerProperties;
56 class HttpTransactionFactory;
57 class ProxyConfigService;
58 class ProxyService;
59 class SSLConfigService;
60 class TransportSecurityPersister;
61 class TransportSecurityState;
62 class URLRequestJobFactoryImpl;
63 } // namespace net
65 namespace policy {
66 class PolicyCertVerifier;
67 class PolicyHeaderIOHelper;
68 class URLBlacklistManager;
69 } // namespace policy
71 namespace prerender {
72 class PrerenderTracker;
75 // Conceptually speaking, the ProfileIOData represents data that lives on the IO
76 // thread that is owned by a Profile, such as, but not limited to, network
77 // objects like CookieMonster, HttpTransactionFactory, etc. Profile owns
78 // ProfileIOData, but will make sure to delete it on the IO thread (except
79 // possibly in unit tests where there is no IO thread).
80 class ProfileIOData {
81 public:
82 typedef std::vector<scoped_refptr<ChromeURLRequestContextGetter>>
83 ChromeURLRequestContextGetterVector;
85 virtual ~ProfileIOData();
87 static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
89 // Returns true if |scheme| is handled in Chrome, or by default handlers in
90 // net::URLRequest.
91 static bool IsHandledProtocol(const std::string& scheme);
93 // Returns true if |url| is handled in Chrome, or by default handlers in
94 // net::URLRequest.
95 static bool IsHandledURL(const GURL& url);
97 // Utility to install additional WebUI handlers into the |job_factory|.
98 // Ownership of the handlers is transfered from |protocol_handlers|
99 // to the |job_factory|.
100 static void InstallProtocolHandlers(
101 net::URLRequestJobFactoryImpl* job_factory,
102 content::ProtocolHandlerMap* protocol_handlers);
104 // Called by Profile.
105 content::ResourceContext* GetResourceContext() const;
107 // Initializes the ProfileIOData object and primes the RequestContext
108 // generation. Must be called prior to any of the Get*() methods other than
109 // GetResouceContext or GetMetricsEnabledStateOnIOThread.
110 void Init(
111 content::ProtocolHandlerMap* protocol_handlers,
112 content::URLRequestInterceptorScopedVector request_interceptors) const;
114 net::URLRequestContext* GetMainRequestContext() const;
115 net::URLRequestContext* GetMediaRequestContext() const;
116 net::URLRequestContext* GetExtensionsRequestContext() const;
117 net::URLRequestContext* GetIsolatedAppRequestContext(
118 net::URLRequestContext* main_context,
119 const StoragePartitionDescriptor& partition_descriptor,
120 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
121 protocol_handler_interceptor,
122 content::ProtocolHandlerMap* protocol_handlers,
123 content::URLRequestInterceptorScopedVector request_interceptors) const;
124 net::URLRequestContext* GetIsolatedMediaRequestContext(
125 net::URLRequestContext* app_context,
126 const StoragePartitionDescriptor& partition_descriptor) const;
128 // These are useful when the Chrome layer is called from the content layer
129 // with a content::ResourceContext, and they want access to Chrome data for
130 // that profile.
131 extensions::InfoMap* GetExtensionInfoMap() const;
132 CookieSettings* GetCookieSettings() const;
133 HostContentSettingsMap* GetHostContentSettingsMap() const;
135 IntegerPrefMember* session_startup_pref() const {
136 return &session_startup_pref_;
139 SigninNamesOnIOThread* signin_names() const {
140 return signin_names_.get();
143 StringPrefMember* google_services_account_id() const {
144 return &google_services_user_account_id_;
147 StringPrefMember* google_services_username() const {
148 return &google_services_username_;
151 StringPrefMember* google_services_username_pattern() const {
152 return &google_services_username_pattern_;
155 BooleanPrefMember* reverse_autologin_enabled() const {
156 return &reverse_autologin_enabled_;
159 const std::string& reverse_autologin_pending_email() const {
160 return reverse_autologin_pending_email_;
163 void set_reverse_autologin_pending_email(const std::string& email) {
164 reverse_autologin_pending_email_ = email;
167 StringListPrefMember* one_click_signin_rejected_email_list() const {
168 return &one_click_signin_rejected_email_list_;
171 net::URLRequestContext* extensions_request_context() const {
172 return extensions_request_context_.get();
175 BooleanPrefMember* safe_browsing_enabled() const {
176 return &safe_browsing_enabled_;
179 #if defined(SPDY_PROXY_AUTH_ORIGIN)
180 // TODO(feng): move the function to protected area.
181 // IsDataReductionProxyEnabled() should be used as public API.
182 BooleanPrefMember* data_reduction_proxy_enabled() const {
183 return &data_reduction_proxy_enabled_;
185 #endif
187 BooleanPrefMember* printing_enabled() const {
188 return &printing_enabled_;
191 BooleanPrefMember* sync_disabled() const {
192 return &sync_disabled_;
195 BooleanPrefMember* signin_allowed() const {
196 return &signin_allowed_;
199 IntegerPrefMember* network_prediction_options() const {
200 return &network_prediction_options_;
203 content::ResourceContext::SaltCallback GetMediaDeviceIDSalt() const;
205 DevToolsNetworkController* network_controller() const {
206 return network_controller_.get();
209 net::TransportSecurityState* transport_security_state() const {
210 return transport_security_state_.get();
213 #if defined(OS_CHROMEOS)
214 std::string username_hash() const {
215 return username_hash_;
218 bool use_system_key_slot() const { return use_system_key_slot_; }
219 #endif
221 Profile::ProfileType profile_type() const {
222 return profile_type_;
225 bool IsOffTheRecord() const;
227 IntegerPrefMember* incognito_availibility() const {
228 return &incognito_availibility_pref_;
231 #if defined(ENABLE_CONFIGURATION_POLICY)
232 policy::PolicyHeaderIOHelper* policy_header_helper() const {
233 return policy_header_helper_.get();
235 #endif
237 #if defined(ENABLE_MANAGED_USERS)
238 const SupervisedUserURLFilter* supervised_user_url_filter() const {
239 return supervised_user_url_filter_.get();
241 #endif
243 // Initialize the member needed to track the metrics enabled state. This is
244 // only to be called on the UI thread.
245 void InitializeMetricsEnabledStateOnUIThread();
247 // Returns whether or not metrics reporting is enabled in the browser instance
248 // on which this profile resides. This is safe for use from the IO thread, and
249 // should only be called from there.
250 bool GetMetricsEnabledStateOnIOThread() const;
252 #if defined(OS_ANDROID)
253 // Returns whether or not data reduction proxy is enabled in the browser
254 // instance on which this profile resides.
255 bool IsDataReductionProxyEnabled() const;
256 #endif
258 void set_client_cert_store_factory_for_testing(
259 const base::Callback<scoped_ptr<net::ClientCertStore>()>& factory) {
260 client_cert_store_factory_ = factory;
263 protected:
264 // A URLRequestContext for media that owns its HTTP factory, to ensure
265 // it is deleted.
266 class MediaRequestContext : public net::URLRequestContext {
267 public:
268 MediaRequestContext();
270 void SetHttpTransactionFactory(
271 scoped_ptr<net::HttpTransactionFactory> http_factory);
273 private:
274 virtual ~MediaRequestContext();
276 scoped_ptr<net::HttpTransactionFactory> http_factory_;
279 // A URLRequestContext for apps that owns its cookie store and HTTP factory,
280 // to ensure they are deleted.
281 class AppRequestContext : public net::URLRequestContext {
282 public:
283 AppRequestContext();
285 void SetCookieStore(net::CookieStore* cookie_store);
286 void SetHttpTransactionFactory(
287 scoped_ptr<net::HttpTransactionFactory> http_factory);
288 void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
290 private:
291 virtual ~AppRequestContext();
293 scoped_refptr<net::CookieStore> cookie_store_;
294 scoped_ptr<net::HttpTransactionFactory> http_factory_;
295 scoped_ptr<net::URLRequestJobFactory> job_factory_;
298 // Created on the UI thread, read on the IO thread during ProfileIOData lazy
299 // initialization.
300 struct ProfileParams {
301 ProfileParams();
302 ~ProfileParams();
304 base::FilePath path;
305 IOThread* io_thread;
306 scoped_refptr<CookieSettings> cookie_settings;
307 scoped_refptr<HostContentSettingsMap> host_content_settings_map;
308 scoped_refptr<net::SSLConfigService> ssl_config_service;
309 scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
310 #if defined(ENABLE_EXTENSIONS)
311 scoped_refptr<extensions::InfoMap> extension_info_map;
312 #endif
314 // This pointer exists only as a means of conveying a url job factory
315 // pointer from the protocol handler registry on the UI thread to the
316 // the URLRequestContext on the IO thread. The consumer MUST take
317 // ownership of the object by calling release() on this pointer.
318 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
319 protocol_handler_interceptor;
321 // We need to initialize the ProxyConfigService from the UI thread
322 // because on linux it relies on initializing things through gconf,
323 // and needs to be on the main thread.
324 scoped_ptr<net::ProxyConfigService> proxy_config_service;
326 #if defined(ENABLE_MANAGED_USERS)
327 scoped_refptr<const SupervisedUserURLFilter> supervised_user_url_filter;
328 #endif
330 #if defined(OS_CHROMEOS)
331 std::string username_hash;
332 bool use_system_key_slot;
333 #endif
335 // The profile this struct was populated from. It's passed as a void* to
336 // ensure it's not accidently used on the IO thread. Before using it on the
337 // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
338 void* profile;
340 prerender::PrerenderTracker* prerender_tracker;
343 explicit ProfileIOData(Profile::ProfileType profile_type);
345 static std::string GetSSLSessionCacheShard();
347 void InitializeOnUIThread(Profile* profile);
348 void ApplyProfileParamsToContext(net::URLRequestContext* context) const;
350 scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults(
351 scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
352 content::URLRequestInterceptorScopedVector request_interceptors,
353 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
354 protocol_handler_interceptor,
355 net::NetworkDelegate* network_delegate,
356 net::FtpTransactionFactory* ftp_transaction_factory) const;
358 // Called when the profile is destroyed.
359 void ShutdownOnUIThread(
360 scoped_ptr<ChromeURLRequestContextGetterVector> context_getters);
362 // A ChannelIDService object is created by a derived class of
363 // ProfileIOData, and the derived class calls this method to set the
364 // channel_id_service_ member and transfers ownership to the base
365 // class.
366 void set_channel_id_service(
367 net::ChannelIDService* channel_id_service) const;
369 ChromeNetworkDelegate* network_delegate() const {
370 return network_delegate_.get();
373 net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
374 return fraudulent_certificate_reporter_.get();
377 net::ProxyService* proxy_service() const {
378 return proxy_service_.get();
381 base::WeakPtr<net::HttpServerProperties> http_server_properties() const;
383 void set_http_server_properties(
384 scoped_ptr<net::HttpServerProperties> http_server_properties) const;
386 net::URLRequestContext* main_request_context() const {
387 return main_request_context_.get();
390 bool initialized() const {
391 return initialized_;
394 // Destroys the ResourceContext first, to cancel any URLRequests that are
395 // using it still, before we destroy the member variables that those
396 // URLRequests may be accessing.
397 void DestroyResourceContext();
399 // Creates network session and main network transaction factory.
400 scoped_ptr<net::HttpCache> CreateMainHttpFactory(
401 const ProfileParams* profile_params,
402 net::HttpCache::BackendFactory* main_backend) const;
404 // Creates network transaction factory.
405 scoped_ptr<net::HttpCache> CreateHttpFactory(
406 net::HttpNetworkSession* shared_session,
407 net::HttpCache::BackendFactory* backend) const;
409 void SetCookieSettingsForTesting(CookieSettings* cookie_settings);
411 void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names);
413 private:
414 class ResourceContext : public content::ResourceContext {
415 public:
416 explicit ResourceContext(ProfileIOData* io_data);
417 virtual ~ResourceContext();
419 // ResourceContext implementation:
420 virtual net::HostResolver* GetHostResolver() OVERRIDE;
421 virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
422 virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE;
423 virtual void CreateKeygenHandler(
424 uint32 key_size_in_bits,
425 const std::string& challenge_string,
426 const GURL& url,
427 const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback)
428 OVERRIDE;
429 virtual SaltCallback GetMediaDeviceIDSalt() OVERRIDE;
431 private:
432 friend class ProfileIOData;
434 ProfileIOData* const io_data_;
436 net::HostResolver* host_resolver_;
437 net::URLRequestContext* request_context_;
440 typedef std::map<StoragePartitionDescriptor,
441 net::URLRequestContext*,
442 StoragePartitionDescriptorLess>
443 URLRequestContextMap;
445 // --------------------------------------------
446 // Virtual interface for subtypes to implement:
447 // --------------------------------------------
449 // Does the actual initialization of the ProfileIOData subtype. Subtypes
450 // should use the static helper functions above to implement this.
451 virtual void InitializeInternal(
452 ProfileParams* profile_params,
453 content::ProtocolHandlerMap* protocol_handlers,
454 content::URLRequestInterceptorScopedVector
455 request_interceptors) const = 0;
457 // Initializes the RequestContext for extensions.
458 virtual void InitializeExtensionsRequestContext(
459 ProfileParams* profile_params) const = 0;
460 // Does an on-demand initialization of a RequestContext for the given
461 // isolated app.
462 virtual net::URLRequestContext* InitializeAppRequestContext(
463 net::URLRequestContext* main_context,
464 const StoragePartitionDescriptor& details,
465 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
466 protocol_handler_interceptor,
467 content::ProtocolHandlerMap* protocol_handlers,
468 content::URLRequestInterceptorScopedVector
469 request_interceptors) const = 0;
471 // Does an on-demand initialization of a media RequestContext for the given
472 // isolated app.
473 virtual net::URLRequestContext* InitializeMediaRequestContext(
474 net::URLRequestContext* original_context,
475 const StoragePartitionDescriptor& details) const = 0;
477 // These functions are used to transfer ownership of the lazily initialized
478 // context from ProfileIOData to the URLRequestContextGetter.
479 virtual net::URLRequestContext*
480 AcquireMediaRequestContext() const = 0;
481 virtual net::URLRequestContext* AcquireIsolatedAppRequestContext(
482 net::URLRequestContext* main_context,
483 const StoragePartitionDescriptor& partition_descriptor,
484 scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
485 protocol_handler_interceptor,
486 content::ProtocolHandlerMap* protocol_handlers,
487 content::URLRequestInterceptorScopedVector
488 request_interceptors) const = 0;
489 virtual net::URLRequestContext*
490 AcquireIsolatedMediaRequestContext(
491 net::URLRequestContext* app_context,
492 const StoragePartitionDescriptor& partition_descriptor) const = 0;
494 // The order *DOES* matter for the majority of these member variables, so
495 // don't move them around unless you know what you're doing!
496 // General rules:
497 // * ResourceContext references the URLRequestContexts, so
498 // URLRequestContexts must outlive ResourceContext, hence ResourceContext
499 // should be destroyed first.
500 // * URLRequestContexts reference a whole bunch of members, so
501 // URLRequestContext needs to be destroyed before them.
502 // * Therefore, ResourceContext should be listed last, and then the
503 // URLRequestContexts, and then the URLRequestContext members.
504 // * Note that URLRequestContext members have a directed dependency graph
505 // too, so they must themselves be ordered correctly.
507 // Tracks whether or not we've been lazily initialized.
508 mutable bool initialized_;
510 // Data from the UI thread from the Profile, used to initialize ProfileIOData.
511 // Deleted after lazy initialization.
512 mutable scoped_ptr<ProfileParams> profile_params_;
514 // Provides access to the email addresses of all signed in profiles.
515 mutable scoped_ptr<SigninNamesOnIOThread> signin_names_;
517 // Used for testing.
518 mutable base::Callback<scoped_ptr<net::ClientCertStore>()>
519 client_cert_store_factory_;
521 mutable StringPrefMember google_services_user_account_id_;
522 mutable StringPrefMember google_services_username_;
523 mutable StringPrefMember google_services_username_pattern_;
524 mutable BooleanPrefMember reverse_autologin_enabled_;
526 // During the reverse autologin request chain processing, this member saves
527 // the email of the google account that is being signed into.
528 std::string reverse_autologin_pending_email_;
530 mutable StringListPrefMember one_click_signin_rejected_email_list_;
532 mutable scoped_refptr<MediaDeviceIDSalt> media_device_id_salt_;
534 // Member variables which are pointed to by the various context objects.
535 mutable BooleanPrefMember enable_referrers_;
536 mutable BooleanPrefMember enable_do_not_track_;
537 mutable BooleanPrefMember force_safesearch_;
538 mutable BooleanPrefMember safe_browsing_enabled_;
539 #if defined(SPDY_PROXY_AUTH_ORIGIN)
540 mutable BooleanPrefMember data_reduction_proxy_enabled_;
541 #endif
542 mutable BooleanPrefMember printing_enabled_;
543 mutable BooleanPrefMember sync_disabled_;
544 mutable BooleanPrefMember signin_allowed_;
545 mutable IntegerPrefMember network_prediction_options_;
546 // TODO(marja): Remove session_startup_pref_ if no longer needed.
547 mutable IntegerPrefMember session_startup_pref_;
548 mutable BooleanPrefMember quick_check_enabled_;
549 mutable IntegerPrefMember incognito_availibility_pref_;
551 // The state of metrics reporting in the browser that this profile runs on.
552 // Unfortunately, since ChromeOS has a separate representation of this state,
553 // we need to make one available based on the platform.
554 #if defined(OS_CHROMEOS)
555 bool enable_metrics_;
556 #else
557 BooleanPrefMember enable_metrics_;
558 #endif
560 #if defined(ENABLE_CONFIGURATION_POLICY)
561 // Pointed to by NetworkDelegate.
562 mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
563 mutable scoped_ptr<policy::PolicyHeaderIOHelper> policy_header_helper_;
564 #endif
566 // Pointed to by URLRequestContext.
567 #if defined(ENABLE_EXTENSIONS)
568 mutable scoped_refptr<extensions::InfoMap> extension_info_map_;
569 #endif
570 mutable scoped_ptr<net::ChannelIDService> channel_id_service_;
571 mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_;
572 mutable scoped_ptr<net::FraudulentCertificateReporter>
573 fraudulent_certificate_reporter_;
574 mutable scoped_ptr<net::ProxyService> proxy_service_;
575 mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
576 mutable scoped_ptr<net::HttpServerProperties>
577 http_server_properties_;
578 #if defined(OS_CHROMEOS)
579 // Set to |cert_verifier_| if it references a PolicyCertVerifier. In that
580 // case, the verifier is owned by |cert_verifier_|. Otherwise, set to NULL.
581 mutable policy::PolicyCertVerifier* policy_cert_verifier_;
582 mutable scoped_ptr<net::CertVerifier> cert_verifier_;
583 mutable std::string username_hash_;
584 mutable bool use_system_key_slot_;
585 #endif
587 mutable scoped_ptr<net::TransportSecurityPersister>
588 transport_security_persister_;
590 // These are only valid in between LazyInitialize() and their accessor being
591 // called.
592 mutable scoped_ptr<net::URLRequestContext> main_request_context_;
593 mutable scoped_ptr<net::URLRequestContext> extensions_request_context_;
594 // One URLRequestContext per isolated app for main and media requests.
595 mutable URLRequestContextMap app_request_context_map_;
596 mutable URLRequestContextMap isolated_media_request_context_map_;
598 mutable scoped_ptr<ResourceContext> resource_context_;
600 mutable scoped_refptr<CookieSettings> cookie_settings_;
602 mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
604 mutable scoped_ptr<ChromeHttpUserAgentSettings>
605 chrome_http_user_agent_settings_;
607 #if defined(ENABLE_MANAGED_USERS)
608 mutable scoped_refptr<const SupervisedUserURLFilter>
609 supervised_user_url_filter_;
610 #endif
612 mutable scoped_ptr<DevToolsNetworkController> network_controller_;
614 // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
615 bool initialized_on_UI_thread_;
617 const Profile::ProfileType profile_type_;
619 DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
622 #endif // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_