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