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