chrome_url_request_context.h revision ac1e49eb6695f711d72215fcdf9388548942a00d
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 // Note that GetURLRequestContext() can only be called from the IO 239 // thread (it will assert otherwise). GetCookieStore() and 240 // GetIOMessageLoopProxy however can be called from any thread. 241 // 242 // URLRequestContextGetter implementation. 243 virtual URLRequestContext* GetURLRequestContext(); 244 virtual net::CookieStore* GetCookieStore(); 245 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy() const; 246 247 // Releases |url_request_context_|. It's invalid to call 248 // GetURLRequestContext() after this point. 249 void ReleaseURLRequestContext(); 250 251 // Convenience overload of GetURLRequestContext() that returns a 252 // ChromeURLRequestContext* rather than a URLRequestContext*. 253 ChromeURLRequestContext* GetIOContext() { 254 return reinterpret_cast<ChromeURLRequestContext*>(GetURLRequestContext()); 255 } 256 257 // Create an instance for use with an 'original' (non-OTR) profile. This is 258 // expected to get called on the UI thread. 259 static ChromeURLRequestContextGetter* CreateOriginal( 260 Profile* profile, const FilePath& cookie_store_path, 261 const FilePath& disk_cache_path, int cache_size); 262 263 // Create an instance for an original profile for media. This is expected to 264 // get called on UI thread. This method takes a profile and reuses the 265 // 'original' URLRequestContext for common files. 266 static ChromeURLRequestContextGetter* CreateOriginalForMedia( 267 Profile* profile, const FilePath& disk_cache_path, int cache_size); 268 269 // Create an instance for an original profile for extensions. This is expected 270 // to get called on UI thread. 271 static ChromeURLRequestContextGetter* CreateOriginalForExtensions( 272 Profile* profile, const FilePath& cookie_store_path); 273 274 // Create an instance for use with an OTR profile. This is expected to get 275 // called on the UI thread. 276 static ChromeURLRequestContextGetter* CreateOffTheRecord(Profile* profile); 277 278 // Create an instance for an OTR profile for extensions. This is expected 279 // to get called on UI thread. 280 static ChromeURLRequestContextGetter* CreateOffTheRecordForExtensions( 281 Profile* profile); 282 283 // Clean up UI thread resources. This is expected to get called on the UI 284 // thread before the instance is deleted on the IO thread. 285 void CleanupOnUIThread(); 286 287 // NotificationObserver implementation. 288 virtual void Observe(NotificationType type, 289 const NotificationSource& source, 290 const NotificationDetails& details); 291 292 private: 293 // Must be called on the IO thread. 294 virtual ~ChromeURLRequestContextGetter(); 295 296 // Registers an observer on |profile|'s preferences which will be used 297 // to update the context when the default language and charset change. 298 void RegisterPrefsObserver(Profile* profile); 299 300 // Creates a request context for media resources from a regular request 301 // context. This helper method is called from CreateOriginalForMedia and 302 // CreateOffTheRecordForMedia. 303 static ChromeURLRequestContextGetter* CreateRequestContextForMedia( 304 Profile* profile, const FilePath& disk_cache_path, int cache_size, 305 bool off_the_record); 306 307 // These methods simply forward to the corresponding method on 308 // ChromeURLRequestContext. 309 void OnAcceptLanguageChange(const std::string& accept_language); 310 void OnDefaultCharsetChange(const std::string& default_charset); 311 312 // Saves the cookie store to |result| and signals |completion|. 313 void GetCookieStoreAsyncHelper(base::WaitableEvent* completion, 314 net::CookieStore** result); 315 316 PrefChangeRegistrar registrar_; 317 318 // |io_thread_| is always valid during the lifetime of |this| since |this| is 319 // deleted on the IO thread. 320 IOThread* const io_thread_; 321 322 // Deferred logic for creating a ChromeURLRequestContext. 323 // Access only from the IO thread. 324 scoped_ptr<ChromeURLRequestContextFactory> factory_; 325 326 // NULL if not yet initialized. Otherwise, it is the URLRequestContext 327 // instance that was lazilly created by GetURLRequestContext. 328 // Access only from the IO thread. 329 scoped_refptr<URLRequestContext> url_request_context_; 330 331 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter); 332}; 333 334// Base class for a ChromeURLRequestContext factory. This includes 335// the shared functionality like extracting the default language/charset 336// from a profile. 337// 338// Except for the constructor, all methods of this class must be called from 339// the IO thread. 340class ChromeURLRequestContextFactory { 341 public: 342 // Extract properties of interested from |profile|, for setting later into 343 // a ChromeURLRequestContext using ApplyProfileParametersToContext(). 344 explicit ChromeURLRequestContextFactory(Profile* profile); 345 346 virtual ~ChromeURLRequestContextFactory(); 347 348 // Called to create a new instance (will only be called once). 349 virtual ChromeURLRequestContext* Create() = 0; 350 351 protected: 352 IOThread* io_thread() { return io_thread_; } 353 354 // Assigns this factory's properties to |context|. 355 void ApplyProfileParametersToContext(ChromeURLRequestContext* context); 356 357 // Values extracted from the Profile. 358 // 359 // NOTE: If you add any parameters here, keep it in sync with 360 // ApplyProfileParametersToContext(). 361 bool is_media_; 362 bool is_off_the_record_; 363 std::string accept_language_; 364 std::string accept_charset_; 365 std::string referrer_charset_; 366 367 // TODO(aa): I think this can go away now as we no longer support standalone 368 // user scripts. 369 FilePath user_script_dir_path_; 370 scoped_refptr<HostContentSettingsMap> host_content_settings_map_; 371 scoped_refptr<ChromeAppCacheService> appcache_service_; 372 scoped_refptr<webkit_database::DatabaseTracker> database_tracker_; 373 scoped_refptr<HostZoomMap> host_zoom_map_; 374 scoped_refptr<net::TransportSecurityState> transport_security_state_; 375 scoped_refptr<net::SSLConfigService> ssl_config_service_; 376 scoped_refptr<net::CookieMonster::Delegate> cookie_monster_delegate_; 377 scoped_refptr<ChromeBlobStorageContext> blob_storage_context_; 378 scoped_refptr<BrowserFileSystemContext> browser_file_system_context_; 379 scoped_refptr<ExtensionInfoMap> extension_info_map_; 380 381 FilePath profile_dir_path_; 382 383 private: 384 IOThread* const io_thread_; 385 386 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextFactory); 387}; 388 389#endif // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ 390