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