3f6e2a2ce394f917421e68fb1b1327e504f27a7f
[platform/framework/web/crosswalk.git] / src / chrome / browser / profiles / profile_io_data.h
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.
4
5 #ifndef CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
6 #define CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_
7
8 #include <string>
9
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/storage_partition_descriptor.h"
22 #include "chrome/common/content_settings_types.h"
23 #include "content/public/browser/content_browser_client.h"
24 #include "content/public/browser/resource_context.h"
25 #include "net/cookies/cookie_monster.h"
26 #include "net/http/http_network_session.h"
27 #include "net/url_request/url_request_job_factory.h"
28
29 class ChromeHttpUserAgentSettings;
30 class ChromeNetworkDelegate;
31 class CookieSettings;
32 class HostContentSettingsMap;
33 class ManagedModeURLFilter;
34 class MediaDeviceIDSalt;
35 class Profile;
36 class ProtocolHandlerRegistry;
37 class SigninNamesOnIOThread;
38
39 namespace extensions {
40 class InfoMap;
41 }
42
43 namespace net {
44 class CookieStore;
45 class FraudulentCertificateReporter;
46 class FtpTransactionFactory;
47 class HttpServerProperties;
48 class HttpTransactionFactory;
49 class ServerBoundCertService;
50 class ProxyConfigService;
51 class ProxyService;
52 class SSLConfigService;
53 class TransportSecurityPersister;
54 class TransportSecurityState;
55 class URLRequestJobFactoryImpl;
56 }  // namespace net
57
58 namespace policy {
59 class PolicyCertVerifier;
60 class PolicyHeaderIOHelper;
61 class URLBlacklistManager;
62 }  // namespace policy
63
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).
69 class ProfileIOData {
70  public:
71   virtual ~ProfileIOData();
72
73   static ProfileIOData* FromResourceContext(content::ResourceContext* rc);
74
75   // Returns true if |scheme| is handled in Chrome, or by default handlers in
76   // net::URLRequest.
77   static bool IsHandledProtocol(const std::string& scheme);
78
79   // Returns true if |url| is handled in Chrome, or by default handlers in
80   // net::URLRequest.
81   static bool IsHandledURL(const GURL& url);
82
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);
89
90   // Called by Profile.
91   content::ResourceContext* GetResourceContext() const;
92
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) const;
97
98   ChromeURLRequestContext* GetMainRequestContext() const;
99   ChromeURLRequestContext* GetMediaRequestContext() const;
100   ChromeURLRequestContext* GetExtensionsRequestContext() const;
101   ChromeURLRequestContext* GetIsolatedAppRequestContext(
102       ChromeURLRequestContext* main_context,
103       const StoragePartitionDescriptor& partition_descriptor,
104       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
105           protocol_handler_interceptor,
106       content::ProtocolHandlerMap* protocol_handlers) const;
107   ChromeURLRequestContext* GetIsolatedMediaRequestContext(
108       ChromeURLRequestContext* app_context,
109       const StoragePartitionDescriptor& partition_descriptor) const;
110
111   // These are useful when the Chrome layer is called from the content layer
112   // with a content::ResourceContext, and they want access to Chrome data for
113   // that profile.
114   extensions::InfoMap* GetExtensionInfoMap() const;
115   CookieSettings* GetCookieSettings() const;
116   HostContentSettingsMap* GetHostContentSettingsMap() const;
117
118   IntegerPrefMember* session_startup_pref() const {
119     return &session_startup_pref_;
120   }
121
122   SigninNamesOnIOThread* signin_names() const {
123     return signin_names_.get();
124   }
125
126   StringPrefMember* google_services_account_id() const {
127     return &google_services_user_account_id_;
128   }
129
130   StringPrefMember* google_services_username() const {
131     return &google_services_username_;
132   }
133
134   StringPrefMember* google_services_username_pattern() const {
135     return &google_services_username_pattern_;
136   }
137
138   BooleanPrefMember* reverse_autologin_enabled() const {
139     return &reverse_autologin_enabled_;
140   }
141
142   const std::string& reverse_autologin_pending_email() const {
143     return reverse_autologin_pending_email_;
144   }
145
146   void set_reverse_autologin_pending_email(const std::string& email) {
147     reverse_autologin_pending_email_ = email;
148   }
149
150   StringListPrefMember* one_click_signin_rejected_email_list() const {
151     return &one_click_signin_rejected_email_list_;
152   }
153
154   ChromeURLRequestContext* extensions_request_context() const {
155     return extensions_request_context_.get();
156   }
157
158   BooleanPrefMember* safe_browsing_enabled() const {
159     return &safe_browsing_enabled_;
160   }
161
162   BooleanPrefMember* printing_enabled() const {
163     return &printing_enabled_;
164   }
165
166   BooleanPrefMember* sync_disabled() const {
167     return &sync_disabled_;
168   }
169
170   BooleanPrefMember* signin_allowed() const {
171     return &signin_allowed_;
172   }
173
174   std::string GetMediaDeviceIDSalt() const;
175
176   net::TransportSecurityState* transport_security_state() const {
177     return transport_security_state_.get();
178   }
179
180 #if defined(OS_CHROMEOS)
181   std::string username_hash() const {
182     return username_hash_;
183   }
184 #endif
185
186   bool is_incognito() const {
187     return is_incognito_;
188   }
189
190 #if defined(ENABLE_CONFIGURATION_POLICY)
191   policy::PolicyHeaderIOHelper* policy_header_helper() const {
192     return policy_header_helper_.get();
193   }
194 #endif
195
196 #if defined(ENABLE_MANAGED_USERS)
197   const ManagedModeURLFilter* managed_mode_url_filter() const {
198     return managed_mode_url_filter_.get();
199   }
200 #endif
201
202   // Initialize the member needed to track the metrics enabled state. This is
203   // only to be called on the UI thread.
204   void InitializeMetricsEnabledStateOnUIThread();
205
206   // Returns whether or not metrics reporting is enabled in the browser instance
207   // on which this profile resides. This is safe for use from the IO thread, and
208   // should only be called from there.
209   bool GetMetricsEnabledStateOnIOThread() const;
210
211   void set_client_cert_store_factory_for_testing(
212     const base::Callback<scoped_ptr<net::ClientCertStore>()>& factory) {
213       client_cert_store_factory_ = factory;
214   }
215
216  protected:
217   // A URLRequestContext for media that owns its HTTP factory, to ensure
218   // it is deleted.
219   class MediaRequestContext : public ChromeURLRequestContext {
220    public:
221     MediaRequestContext();
222
223     void SetHttpTransactionFactory(
224         scoped_ptr<net::HttpTransactionFactory> http_factory);
225
226    private:
227     virtual ~MediaRequestContext();
228
229     scoped_ptr<net::HttpTransactionFactory> http_factory_;
230   };
231
232   // A URLRequestContext for apps that owns its cookie store and HTTP factory,
233   // to ensure they are deleted.
234   class AppRequestContext : public ChromeURLRequestContext {
235    public:
236     AppRequestContext();
237
238     void SetCookieStore(net::CookieStore* cookie_store);
239     void SetHttpTransactionFactory(
240         scoped_ptr<net::HttpTransactionFactory> http_factory);
241     void SetJobFactory(scoped_ptr<net::URLRequestJobFactory> job_factory);
242
243    private:
244     virtual ~AppRequestContext();
245
246     scoped_refptr<net::CookieStore> cookie_store_;
247     scoped_ptr<net::HttpTransactionFactory> http_factory_;
248     scoped_ptr<net::URLRequestJobFactory> job_factory_;
249   };
250
251   // Created on the UI thread, read on the IO thread during ProfileIOData lazy
252   // initialization.
253   struct ProfileParams {
254     ProfileParams();
255     ~ProfileParams();
256
257     base::FilePath path;
258     IOThread* io_thread;
259     scoped_refptr<CookieSettings> cookie_settings;
260     scoped_refptr<HostContentSettingsMap> host_content_settings_map;
261     scoped_refptr<net::SSLConfigService> ssl_config_service;
262     scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate;
263     scoped_refptr<extensions::InfoMap> extension_info_map;
264
265     // This pointer exists only as a means of conveying a url job factory
266     // pointer from the protocol handler registry on the UI thread to the
267     // the URLRequestContext on the IO thread. The consumer MUST take
268     // ownership of the object by calling release() on this pointer.
269     scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
270         protocol_handler_interceptor;
271
272     // We need to initialize the ProxyConfigService from the UI thread
273     // because on linux it relies on initializing things through gconf,
274     // and needs to be on the main thread.
275     scoped_ptr<net::ProxyConfigService> proxy_config_service;
276
277 #if defined(ENABLE_MANAGED_USERS)
278     scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter;
279 #endif
280
281 #if defined(OS_CHROMEOS)
282     std::string username_hash;
283 #endif
284
285     // The profile this struct was populated from. It's passed as a void* to
286     // ensure it's not accidently used on the IO thread. Before using it on the
287     // UI thread, call ProfileManager::IsValidProfile to ensure it's alive.
288     void* profile;
289   };
290
291   explicit ProfileIOData(bool is_incognito);
292
293   static std::string GetSSLSessionCacheShard();
294
295   void InitializeOnUIThread(Profile* profile);
296   void ApplyProfileParamsToContext(ChromeURLRequestContext* context) const;
297
298   scoped_ptr<net::URLRequestJobFactory> SetUpJobFactoryDefaults(
299       scoped_ptr<net::URLRequestJobFactoryImpl> job_factory,
300       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
301           protocol_handler_interceptor,
302       net::NetworkDelegate* network_delegate,
303       net::FtpTransactionFactory* ftp_transaction_factory) const;
304
305   // Called when the profile is destroyed.
306   void ShutdownOnUIThread();
307
308   // A ServerBoundCertService object is created by a derived class of
309   // ProfileIOData, and the derived class calls this method to set the
310   // server_bound_cert_service_ member and transfers ownership to the base
311   // class.
312   void set_server_bound_cert_service(
313       net::ServerBoundCertService* server_bound_cert_service) const;
314
315   ChromeNetworkDelegate* network_delegate() const {
316     return network_delegate_.get();
317   }
318
319   net::FraudulentCertificateReporter* fraudulent_certificate_reporter() const {
320     return fraudulent_certificate_reporter_.get();
321   }
322
323   net::ProxyService* proxy_service() const {
324     return proxy_service_.get();
325   }
326
327   base::WeakPtr<net::HttpServerProperties> http_server_properties() const;
328
329   void set_http_server_properties(
330       scoped_ptr<net::HttpServerProperties> http_server_properties) const;
331
332   ChromeURLRequestContext* main_request_context() const {
333     return main_request_context_.get();
334   }
335
336   bool initialized() const {
337     return initialized_;
338   }
339
340   // Destroys the ResourceContext first, to cancel any URLRequests that are
341   // using it still, before we destroy the member variables that those
342   // URLRequests may be accessing.
343   void DestroyResourceContext();
344
345   // Fills in fields of params using values from main_request_context_ and the
346   // IOThread associated with profile_params.
347   void PopulateNetworkSessionParams(
348       const ProfileParams* profile_params,
349       net::HttpNetworkSession::Params* params) const;
350
351   void SetCookieSettingsForTesting(CookieSettings* cookie_settings);
352
353   void set_signin_names_for_testing(SigninNamesOnIOThread* signin_names);
354
355  private:
356   class ResourceContext : public content::ResourceContext {
357    public:
358     explicit ResourceContext(ProfileIOData* io_data);
359     virtual ~ResourceContext();
360
361     // ResourceContext implementation:
362     virtual net::HostResolver* GetHostResolver() OVERRIDE;
363     virtual net::URLRequestContext* GetRequestContext() OVERRIDE;
364     virtual scoped_ptr<net::ClientCertStore> CreateClientCertStore() OVERRIDE;
365     virtual void CreateKeygenHandler(
366         uint32 key_size_in_bits,
367         const std::string& challenge_string,
368         const GURL& url,
369         const base::Callback<void(scoped_ptr<net::KeygenHandler>)>& callback)
370         OVERRIDE;
371     virtual bool AllowMicAccess(const GURL& origin) OVERRIDE;
372     virtual bool AllowCameraAccess(const GURL& origin) OVERRIDE;
373     virtual std::string GetMediaDeviceIDSalt() OVERRIDE;
374
375    private:
376     friend class ProfileIOData;
377
378     // Helper method that returns true if |type| is allowed for |origin|, false
379     // otherwise.
380     bool AllowContentAccess(const GURL& origin, ContentSettingsType type);
381
382     ProfileIOData* const io_data_;
383
384     net::HostResolver* host_resolver_;
385     net::URLRequestContext* request_context_;
386   };
387
388   typedef std::map<StoragePartitionDescriptor,
389                    ChromeURLRequestContext*,
390                    StoragePartitionDescriptorLess>
391       URLRequestContextMap;
392
393   // --------------------------------------------
394   // Virtual interface for subtypes to implement:
395   // --------------------------------------------
396
397   // Does the actual initialization of the ProfileIOData subtype. Subtypes
398   // should use the static helper functions above to implement this.
399   virtual void InitializeInternal(
400       ProfileParams* profile_params,
401       content::ProtocolHandlerMap* protocol_handlers) const = 0;
402
403   // Initializes the RequestContext for extensions.
404   virtual void InitializeExtensionsRequestContext(
405       ProfileParams* profile_params) const = 0;
406   // Does an on-demand initialization of a RequestContext for the given
407   // isolated app.
408   virtual ChromeURLRequestContext* InitializeAppRequestContext(
409       ChromeURLRequestContext* main_context,
410       const StoragePartitionDescriptor& details,
411       scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
412           protocol_handler_interceptor,
413       content::ProtocolHandlerMap* protocol_handlers) const = 0;
414
415   // Does an on-demand initialization of a media RequestContext for the given
416   // isolated app.
417   virtual ChromeURLRequestContext* InitializeMediaRequestContext(
418       ChromeURLRequestContext* original_context,
419       const StoragePartitionDescriptor& details) const = 0;
420
421   // These functions are used to transfer ownership of the lazily initialized
422   // context from ProfileIOData to the URLRequestContextGetter.
423   virtual ChromeURLRequestContext*
424       AcquireMediaRequestContext() const = 0;
425   virtual ChromeURLRequestContext*
426       AcquireIsolatedAppRequestContext(
427           ChromeURLRequestContext* main_context,
428           const StoragePartitionDescriptor& partition_descriptor,
429           scoped_ptr<ProtocolHandlerRegistry::JobInterceptorFactory>
430               protocol_handler_interceptor,
431           content::ProtocolHandlerMap* protocol_handlers) const = 0;
432   virtual ChromeURLRequestContext*
433       AcquireIsolatedMediaRequestContext(
434           ChromeURLRequestContext* app_context,
435           const StoragePartitionDescriptor& partition_descriptor) const = 0;
436
437   // The order *DOES* matter for the majority of these member variables, so
438   // don't move them around unless you know what you're doing!
439   // General rules:
440   //   * ResourceContext references the URLRequestContexts, so
441   //   URLRequestContexts must outlive ResourceContext, hence ResourceContext
442   //   should be destroyed first.
443   //   * URLRequestContexts reference a whole bunch of members, so
444   //   URLRequestContext needs to be destroyed before them.
445   //   * Therefore, ResourceContext should be listed last, and then the
446   //   URLRequestContexts, and then the URLRequestContext members.
447   //   * Note that URLRequestContext members have a directed dependency graph
448   //   too, so they must themselves be ordered correctly.
449
450   // Tracks whether or not we've been lazily initialized.
451   mutable bool initialized_;
452
453   // Data from the UI thread from the Profile, used to initialize ProfileIOData.
454   // Deleted after lazy initialization.
455   mutable scoped_ptr<ProfileParams> profile_params_;
456
457   // Provides access to the email addresses of all signed in profiles.
458   mutable scoped_ptr<SigninNamesOnIOThread> signin_names_;
459
460   // Used for testing.
461   mutable base::Callback<scoped_ptr<net::ClientCertStore>()>
462       client_cert_store_factory_;
463
464   mutable StringPrefMember google_services_user_account_id_;
465   mutable StringPrefMember google_services_username_;
466   mutable StringPrefMember google_services_username_pattern_;
467   mutable BooleanPrefMember reverse_autologin_enabled_;
468
469   // During the reverse autologin request chain processing, this member saves
470   // the email of the google account that is being signed into.
471   std::string reverse_autologin_pending_email_;
472
473   mutable StringListPrefMember one_click_signin_rejected_email_list_;
474
475   mutable scoped_ptr<MediaDeviceIDSalt> media_device_id_salt_;
476
477   // Member variables which are pointed to by the various context objects.
478   mutable BooleanPrefMember enable_referrers_;
479   mutable BooleanPrefMember enable_do_not_track_;
480   mutable BooleanPrefMember force_safesearch_;
481   mutable BooleanPrefMember safe_browsing_enabled_;
482   mutable BooleanPrefMember printing_enabled_;
483   mutable BooleanPrefMember sync_disabled_;
484   mutable BooleanPrefMember signin_allowed_;
485   // TODO(marja): Remove session_startup_pref_ if no longer needed.
486   mutable IntegerPrefMember session_startup_pref_;
487   mutable BooleanPrefMember quick_check_enabled_;
488
489   // The state of metrics reporting in the browser that this profile runs on.
490   // Unfortunately, since ChromeOS has a separate representation of this state,
491   // we need to make one available based on the platform.
492 #if defined(OS_CHROMEOS)
493   bool enable_metrics_;
494 #else
495   BooleanPrefMember enable_metrics_;
496 #endif
497
498 #if defined(ENABLE_CONFIGURATION_POLICY)
499   // Pointed to by NetworkDelegate.
500   mutable scoped_ptr<policy::URLBlacklistManager> url_blacklist_manager_;
501   mutable scoped_ptr<policy::PolicyHeaderIOHelper> policy_header_helper_;
502 #endif
503
504   // Pointed to by URLRequestContext.
505   mutable scoped_refptr<extensions::InfoMap> extension_info_map_;
506   mutable scoped_ptr<net::ServerBoundCertService> server_bound_cert_service_;
507   mutable scoped_ptr<ChromeNetworkDelegate> network_delegate_;
508   mutable scoped_ptr<net::FraudulentCertificateReporter>
509       fraudulent_certificate_reporter_;
510   mutable scoped_ptr<net::ProxyService> proxy_service_;
511   mutable scoped_ptr<net::TransportSecurityState> transport_security_state_;
512   mutable scoped_ptr<net::HttpServerProperties>
513       http_server_properties_;
514 #if defined(OS_CHROMEOS)
515   mutable scoped_ptr<policy::PolicyCertVerifier> cert_verifier_;
516   mutable std::string username_hash_;
517 #endif
518
519   mutable scoped_ptr<net::TransportSecurityPersister>
520       transport_security_persister_;
521
522   // These are only valid in between LazyInitialize() and their accessor being
523   // called.
524   mutable scoped_ptr<ChromeURLRequestContext> main_request_context_;
525   mutable scoped_ptr<ChromeURLRequestContext> extensions_request_context_;
526   // One URLRequestContext per isolated app for main and media requests.
527   mutable URLRequestContextMap app_request_context_map_;
528   mutable URLRequestContextMap isolated_media_request_context_map_;
529
530   mutable scoped_ptr<ResourceContext> resource_context_;
531
532   mutable scoped_refptr<CookieSettings> cookie_settings_;
533
534   mutable scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
535
536   mutable scoped_ptr<ChromeHttpUserAgentSettings>
537       chrome_http_user_agent_settings_;
538
539 #if defined(ENABLE_MANAGED_USERS)
540   mutable scoped_refptr<const ManagedModeURLFilter> managed_mode_url_filter_;
541 #endif
542
543   // TODO(jhawkins): Remove once crbug.com/102004 is fixed.
544   bool initialized_on_UI_thread_;
545
546   bool is_incognito_;
547
548   DISALLOW_COPY_AND_ASSIGN(ProfileIOData);
549 };
550
551 #endif  // CHROME_BROWSER_PROFILES_PROFILE_IO_DATA_H_