chrome_url_request_context.h revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2011 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_NET_CHROME_URL_REQUEST_CONTEXT_H_
6#define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
7#pragma once
8
9#include <string>
10#include <vector>
11
12#include "base/file_path.h"
13#include "chrome/browser/appcache/chrome_appcache_service.h"
14#include "chrome/browser/chrome_blob_storage_context.h"
15#include "chrome/browser/content_settings/host_content_settings_map.h"
16#include "chrome/browser/extensions/extension_info_map.h"
17#include "chrome/browser/extensions/extension_io_event_router.h"
18#include "chrome/browser/extensions/extension_webrequest_api.h"
19#include "chrome/browser/host_zoom_map.h"
20#include "chrome/browser/io_thread.h"
21#include "chrome/browser/net/chrome_cookie_policy.h"
22#include "chrome/browser/prefs/pref_change_registrar.h"
23#include "chrome/browser/prefs/pref_service.h"
24#include "chrome/browser/prerender/prerender_manager.h"
25#include "chrome/common/extensions/extension_icon_set.h"
26#include "chrome/common/net/url_request_context_getter.h"
27#include "net/base/cookie_monster.h"
28#include "net/base/cookie_policy.h"
29#include "net/url_request/url_request_context.h"
30#include "webkit/database/database_tracker.h"
31#include "webkit/fileapi/file_system_context.h"
32
33class CommandLine;
34class PrefService;
35class Profile;
36class ProfileIOData;
37
38namespace net {
39class DnsCertProvenanceChecker;
40class NetworkDelegate;
41}
42
43class ChromeURLDataManagerBackend;
44class ChromeURLRequestContext;
45class ChromeURLRequestContextFactory;
46
47// Subclass of net::URLRequestContext which can be used to store extra
48// information for requests.
49//
50// All methods of this class must be called from the IO thread,
51// including the constructor and destructor.
52class ChromeURLRequestContext : public net::URLRequestContext {
53 public:
54  ChromeURLRequestContext();
55
56  // Gets the path to the directory user scripts are stored in.
57  FilePath user_script_dir_path() const {
58    return user_script_dir_path_;
59  }
60
61  // Gets the appcache service to be used for requests in this context.
62  // May be NULL if requests for this context aren't subject to appcaching.
63  ChromeAppCacheService* appcache_service() const {
64    return appcache_service_.get();
65  }
66
67  // Gets the database tracker associated with this context's profile.
68  webkit_database::DatabaseTracker* database_tracker() const {
69    return database_tracker_.get();
70  }
71
72  // Gets the blob storage context associated with this context's profile.
73  ChromeBlobStorageContext* blob_storage_context() const {
74    return blob_storage_context_.get();
75  }
76
77  // Gets the file system host context with this context's profile.
78  fileapi::FileSystemContext* file_system_context() const {
79    return file_system_context_.get();
80  }
81
82  bool is_off_the_record() const {
83    return is_off_the_record_;
84  }
85
86  virtual const std::string& GetUserAgent(const GURL& url) const;
87
88  HostContentSettingsMap* host_content_settings_map() {
89    return host_content_settings_map_;
90  }
91
92  const HostZoomMap* host_zoom_map() const { return host_zoom_map_; }
93
94  const ExtensionInfoMap* extension_info_map() const {
95    return extension_info_map_;
96  }
97
98  const ExtensionIOEventRouter* extension_io_event_router() const {
99    return extension_io_event_router_;
100  }
101
102  PrerenderManager* prerender_manager() {
103    return prerender_manager_.get();
104  }
105
106  ChromeURLDataManagerBackend* GetChromeURLDataManagerBackend();
107
108 protected:
109  virtual ~ChromeURLRequestContext();
110
111 public:
112  // Setters to simplify initializing from factory objects.
113
114  void set_accept_language(const std::string& accept_language) {
115    accept_language_ = accept_language;
116  }
117  void set_accept_charset(const std::string& accept_charset) {
118    accept_charset_ = accept_charset;
119  }
120  void set_referrer_charset(const std::string& referrer_charset) {
121    referrer_charset_ = referrer_charset;
122  }
123  void set_transport_security_state(
124      net::TransportSecurityState* state) {
125    transport_security_state_ = state;
126  }
127  void set_ssl_config_service(net::SSLConfigService* service) {
128    ssl_config_service_ = service;
129  }
130  void set_dns_cert_checker(net::DnsCertProvenanceChecker* ctx) {
131    dns_cert_checker_.reset(ctx);
132  }
133  void set_ftp_transaction_factory(net::FtpTransactionFactory* factory) {
134    ftp_transaction_factory_ = factory;
135  }
136  void set_cookie_policy(ChromeCookiePolicy* cookie_policy) {
137    chrome_cookie_policy_ = cookie_policy;  // Take a strong reference.
138    cookie_policy_ = cookie_policy;
139  }
140  void set_user_script_dir_path(const FilePath& path) {
141    user_script_dir_path_ = path;
142  }
143  void set_is_off_the_record(bool is_off_the_record) {
144    is_off_the_record_ = is_off_the_record;
145  }
146  void set_host_content_settings_map(
147      HostContentSettingsMap* host_content_settings_map) {
148    host_content_settings_map_ = host_content_settings_map;
149  }
150  void set_host_zoom_map(HostZoomMap* host_zoom_map) {
151    host_zoom_map_ = host_zoom_map;
152  }
153  void set_appcache_service(ChromeAppCacheService* service) {
154    appcache_service_ = service;
155  }
156  void set_database_tracker(webkit_database::DatabaseTracker* tracker) {
157    database_tracker_ = tracker;
158  }
159  void set_blob_storage_context(ChromeBlobStorageContext* context) {
160    blob_storage_context_ = context;
161  }
162  void set_file_system_context(fileapi::FileSystemContext* context) {
163    file_system_context_ = context;
164  }
165  void set_extension_info_map(ExtensionInfoMap* map) {
166    extension_info_map_ = map;
167  }
168  void set_extension_io_event_router(ExtensionIOEventRouter* router) {
169    extension_io_event_router_ = router;
170  }
171  void set_prerender_manager(PrerenderManager* prerender_manager) {
172    prerender_manager_ = prerender_manager;
173  }
174
175  // Callback for when the accept language changes.
176  void OnAcceptLanguageChange(const std::string& accept_language);
177
178  // Callback for when the default charset changes.
179  void OnDefaultCharsetChange(const std::string& default_charset);
180
181 protected:
182  // Path to the directory user scripts are stored in.
183  FilePath user_script_dir_path_;
184
185  // TODO(willchan): Make these non-refcounted.
186  scoped_refptr<ChromeAppCacheService> appcache_service_;
187  scoped_refptr<webkit_database::DatabaseTracker> database_tracker_;
188  scoped_refptr<ChromeCookiePolicy> chrome_cookie_policy_;
189  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
190  scoped_refptr<HostZoomMap> host_zoom_map_;
191  scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
192  scoped_refptr<fileapi::FileSystemContext> file_system_context_;
193  // TODO(aa): This should use chrome/common/extensions/extension_set.h.
194  scoped_refptr<ExtensionInfoMap> extension_info_map_;
195  scoped_refptr<ExtensionIOEventRouter> extension_io_event_router_;
196  scoped_refptr<PrerenderManager> prerender_manager_;
197  scoped_ptr<ChromeURLDataManagerBackend> chrome_url_data_manager_backend_;
198
199  bool is_off_the_record_;
200
201 private:
202  DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext);
203};
204
205// A URLRequestContextGetter subclass used by the browser. This returns a
206// subclass of net::URLRequestContext which can be used to store extra
207// information about requests.
208//
209// Most methods are expected to be called on the UI thread, except for
210// the destructor and GetURLRequestContext().
211class ChromeURLRequestContextGetter : public URLRequestContextGetter,
212                                      public NotificationObserver {
213 public:
214  // Constructs a ChromeURLRequestContextGetter that will use |factory| to
215  // create the ChromeURLRequestContext. If |profile| is non-NULL, then the
216  // ChromeURLRequestContextGetter will additionally watch the preferences for
217  // changes to charset/language and CleanupOnUIThread() will need to be
218  // called to unregister.
219  ChromeURLRequestContextGetter(Profile* profile,
220                                ChromeURLRequestContextFactory* factory);
221
222  // Note that GetURLRequestContext() can only be called from the IO
223  // thread (it will assert otherwise). GetCookieStore() and
224  // GetIOMessageLoopProxy however can be called from any thread.
225  //
226  // URLRequestContextGetter implementation.
227  virtual net::URLRequestContext* GetURLRequestContext();
228  virtual net::CookieStore* GetCookieStore();
229  virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const;
230
231  // Releases |url_request_context_|.  It's invalid to call
232  // GetURLRequestContext() after this point.
233  void ReleaseURLRequestContext();
234
235  // Convenience overload of GetURLRequestContext() that returns a
236  // ChromeURLRequestContext* rather than a net::URLRequestContext*.
237  ChromeURLRequestContext* GetIOContext() {
238    return reinterpret_cast<ChromeURLRequestContext*>(GetURLRequestContext());
239  }
240
241  // Create an instance for use with an 'original' (non-OTR) profile. This is
242  // expected to get called on the UI thread.
243  static ChromeURLRequestContextGetter* CreateOriginal(
244      Profile* profile, const ProfileIOData* profile_io_data);
245
246  // Create an instance for an original profile for media. This is expected to
247  // get called on UI thread. This method takes a profile and reuses the
248  // 'original' net::URLRequestContext for common files.
249  static ChromeURLRequestContextGetter* CreateOriginalForMedia(
250      Profile* profile, const ProfileIOData* profile_io_data);
251
252  // Create an instance for an original profile for extensions. This is expected
253  // to get called on UI thread.
254  static ChromeURLRequestContextGetter* CreateOriginalForExtensions(
255      Profile* profile, const ProfileIOData* profile_io_data);
256
257  // Create an instance for use with an OTR profile. This is expected to get
258  // called on the UI thread.
259  static ChromeURLRequestContextGetter* CreateOffTheRecord(Profile* profile);
260
261  // Create an instance for an OTR profile for extensions. This is expected
262  // to get called on UI thread.
263  static ChromeURLRequestContextGetter* CreateOffTheRecordForExtensions(
264      Profile* profile);
265
266  // Clean up UI thread resources. This is expected to get called on the UI
267  // thread before the instance is deleted on the IO thread.
268  void CleanupOnUIThread();
269
270  // NotificationObserver implementation.
271  virtual void Observe(NotificationType type,
272                       const NotificationSource& source,
273                       const NotificationDetails& details);
274
275 private:
276  // Must be called on the IO thread.
277  virtual ~ChromeURLRequestContextGetter();
278
279  // Registers an observer on |profile|'s preferences which will be used
280  // to update the context when the default language and charset change.
281  void RegisterPrefsObserver(Profile* profile);
282
283  // These methods simply forward to the corresponding method on
284  // ChromeURLRequestContext.
285  void OnAcceptLanguageChange(const std::string& accept_language);
286  void OnDefaultCharsetChange(const std::string& default_charset);
287  void OnClearSiteDataOnExitChange(bool clear_site_data);
288
289  // Saves the cookie store to |result| and signals |completion|.
290  void GetCookieStoreAsyncHelper(base::WaitableEvent* completion,
291                                 net::CookieStore** result);
292
293  PrefChangeRegistrar registrar_;
294
295  // |io_thread_| is always valid during the lifetime of |this| since |this| is
296  // deleted on the IO thread.
297  IOThread* const io_thread_;
298
299  // Deferred logic for creating a ChromeURLRequestContext.
300  // Access only from the IO thread.
301  scoped_ptr<ChromeURLRequestContextFactory> factory_;
302
303  // NULL if not yet initialized. Otherwise, it is the net::URLRequestContext
304  // instance that was lazilly created by GetURLRequestContext.
305  // Access only from the IO thread.
306  scoped_refptr<net::URLRequestContext> url_request_context_;
307
308  DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter);
309};
310
311// Base class for a ChromeURLRequestContext factory. This includes
312// the shared functionality like extracting the default language/charset
313// from a profile.
314//
315// Except for the constructor, all methods of this class must be called from
316// the IO thread.
317class ChromeURLRequestContextFactory {
318 public:
319  // Extract properties of interested from |profile|, for setting later into
320  // a ChromeURLRequestContext using ApplyProfileParametersToContext().
321  explicit ChromeURLRequestContextFactory(Profile* profile);
322
323  virtual ~ChromeURLRequestContextFactory();
324
325  // Called to create a new instance (will only be called once).
326  virtual scoped_refptr<ChromeURLRequestContext> Create() = 0;
327
328 protected:
329  IOThread* io_thread() { return io_thread_; }
330
331  // Assigns this factory's properties to |context|.
332  void ApplyProfileParametersToContext(ChromeURLRequestContext* context);
333
334  // Values extracted from the Profile.
335  //
336  // NOTE: If you add any parameters here, keep it in sync with
337  // ApplyProfileParametersToContext().
338  bool is_off_the_record_;
339  bool clear_local_state_on_exit_;
340  std::string accept_language_;
341  std::string accept_charset_;
342  std::string referrer_charset_;
343
344  // TODO(aa): I think this can go away now as we no longer support standalone
345  // user scripts.
346  // TODO(willchan): Make these non-refcounted.
347  FilePath user_script_dir_path_;
348  scoped_refptr<HostContentSettingsMap> host_content_settings_map_;
349  scoped_refptr<ChromeAppCacheService> appcache_service_;
350  scoped_refptr<webkit_database::DatabaseTracker> database_tracker_;
351  scoped_refptr<HostZoomMap> host_zoom_map_;
352  scoped_refptr<net::TransportSecurityState> transport_security_state_;
353  scoped_refptr<net::SSLConfigService> ssl_config_service_;
354  scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate_;
355  scoped_refptr<ChromeBlobStorageContext> blob_storage_context_;
356  scoped_refptr<fileapi::FileSystemContext> file_system_context_;
357  scoped_refptr<ExtensionInfoMap> extension_info_map_;
358  scoped_refptr<ExtensionIOEventRouter> extension_io_event_router_;
359  scoped_refptr<PrerenderManager> prerender_manager_;
360
361  FilePath profile_dir_path_;
362
363 private:
364  IOThread* const io_thread_;
365
366  DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextFactory);
367};
368
369#endif  // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_
370