| OLD | NEW |
| 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ |
| 6 #define CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ |
| 7 |
| 5 #include "base/file_path.h" | 8 #include "base/file_path.h" |
| 9 #include "chrome/browser/net/url_request_context_getter.h" |
| 6 #include "chrome/common/appcache/chrome_appcache_service.h" | 10 #include "chrome/common/appcache/chrome_appcache_service.h" |
| 7 #include "chrome/common/notification_registrar.h" | 11 #include "chrome/common/notification_registrar.h" |
| 8 #include "chrome/common/pref_service.h" | 12 #include "chrome/common/pref_service.h" |
| 9 #include "net/url_request/url_request_context.h" | 13 #include "net/url_request/url_request_context.h" |
| 10 | 14 |
| 11 class Blacklist; | 15 class Blacklist; |
| 12 class CommandLine; | 16 class CommandLine; |
| 13 class Profile; | 17 class Profile; |
| 14 | 18 |
| 15 namespace net { | 19 namespace net { |
| 16 class ProxyConfig; | 20 class ProxyConfig; |
| 17 } | 21 } |
| 18 | 22 |
| 19 // A URLRequestContext subclass used by the browser. This can be used to store | 23 class ChromeURLRequestContext; |
| 20 // extra information about requests, beyond what is supported by the base | 24 class ChromeURLRequestContextFactory; |
| 21 // URLRequestContext class. | 25 |
| 26 // TODO(eroman): Cleanup the declaration order in this file -- it is all |
| 27 // wonky to try and minimize awkward deltas. |
| 28 |
| 29 // A URLRequestContextGetter subclass used by the browser. This returns a |
| 30 // subclass of URLRequestContext which can be used to store extra information |
| 31 // about requests. |
| 22 // | 32 // |
| 23 // All methods are expected to be called on the IO thread except the | 33 // Most methods are expected to be called on the UI thread, except for |
| 24 // constructor and factories (CreateOriginal, CreateOffTheRecord), which are | 34 // the destructor and GetURLRequestContext(). |
| 25 // expected to be called on the UI thread. | 35 class ChromeURLRequestContextGetter : public URLRequestContextGetter, |
| 26 class ChromeURLRequestContext : public URLRequestContext, | 36 public NotificationObserver { |
| 27 public NotificationObserver { | |
| 28 public: | 37 public: |
| 29 typedef std::map<std::string, FilePath> ExtensionPaths; | 38 // Constructs a ChromeURLRequestContextGetter that will use |factory| to |
| 39 // create the ChromeURLRequestContext. If |profile| is non-NULL, then the |
| 40 // ChromeURLRequestContextGetter will additionally watch the preferences for |
| 41 // changes to charset/language and CleanupOnUIThread() will need to be |
| 42 // called to unregister. |
| 43 ChromeURLRequestContextGetter(Profile* profile, |
| 44 ChromeURLRequestContextFactory* factory); |
| 45 |
| 46 // Must be called on the IO thread. |
| 47 virtual ~ChromeURLRequestContextGetter(); |
| 48 |
| 49 // Note that GetURLRequestContext() can only be called from the IO |
| 50 // thread (it will assert otherwise). GetCookieStore() however can |
| 51 // be called from any thread. |
| 52 // |
| 53 // URLRequestContextGetter implementation. |
| 54 virtual URLRequestContext* GetURLRequestContext(); |
| 55 virtual net::CookieStore* GetCookieStore(); |
| 56 |
| 57 // Convenience overload of GetURLRequestContext() that returns a |
| 58 // ChromeURLRequestContext* rather than a URLRequestContext*. |
| 59 ChromeURLRequestContext* GetIOContext() { |
| 60 return reinterpret_cast<ChromeURLRequestContext*>(GetURLRequestContext()); |
| 61 } |
| 30 | 62 |
| 31 // Create an instance for use with an 'original' (non-OTR) profile. This is | 63 // Create an instance for use with an 'original' (non-OTR) profile. This is |
| 32 // expected to get called on the UI thread. | 64 // expected to get called on the UI thread. |
| 33 static ChromeURLRequestContext* CreateOriginal( | 65 static ChromeURLRequestContextGetter* CreateOriginal( |
| 34 Profile* profile, const FilePath& cookie_store_path, | 66 Profile* profile, const FilePath& cookie_store_path, |
| 35 const FilePath& disk_cache_path, int cache_size, | 67 const FilePath& disk_cache_path, int cache_size); |
| 36 ChromeAppCacheService* appcache_service); | |
| 37 | 68 |
| 38 // Create an instance for an original profile for media. This is expected to | 69 // Create an instance for an original profile for media. This is expected to |
| 39 // get called on UI thread. This method takes a profile and reuses the | 70 // get called on UI thread. This method takes a profile and reuses the |
| 40 // 'original' URLRequestContext for common files. | 71 // 'original' URLRequestContext for common files. |
| 41 static ChromeURLRequestContext* CreateOriginalForMedia(Profile *profile, | 72 static ChromeURLRequestContextGetter* CreateOriginalForMedia( |
| 42 const FilePath& disk_cache_path, int cache_size, | 73 Profile* profile, const FilePath& disk_cache_path, int cache_size); |
| 43 ChromeAppCacheService* appcache_service); | |
| 44 | 74 |
| 45 // Create an instance for an original profile for extensions. This is expected | 75 // Create an instance for an original profile for extensions. This is expected |
| 46 // to get called on UI thread. | 76 // to get called on UI thread. |
| 47 static ChromeURLRequestContext* CreateOriginalForExtensions(Profile *profile, | 77 static ChromeURLRequestContextGetter* CreateOriginalForExtensions( |
| 48 const FilePath& cookie_store_path); | 78 Profile* profile, const FilePath& cookie_store_path); |
| 49 | 79 |
| 50 // Create an instance for use with an OTR profile. This is expected to get | 80 // Create an instance for use with an OTR profile. This is expected to get |
| 51 // called on the UI thread. | 81 // called on the UI thread. |
| 52 static ChromeURLRequestContext* CreateOffTheRecord(Profile* profile, | 82 static ChromeURLRequestContextGetter* CreateOffTheRecord(Profile* profile); |
| 53 ChromeAppCacheService* appcache_service); | |
| 54 | 83 |
| 55 // Create an instance of request context for OTR profile for extensions. | 84 // Create an instance of request context for OTR profile for extensions. |
| 56 static ChromeURLRequestContext* CreateOffTheRecordForExtensions( | 85 static ChromeURLRequestContextGetter* CreateOffTheRecordForExtensions( |
| 57 Profile* profile); | 86 Profile* profile); |
| 58 | 87 |
| 59 // Clean up UI thread resources. This is expected to get called on the UI | 88 // Clean up UI thread resources. This is expected to get called on the UI |
| 60 // thread before the instance is deleted on the IO thread. | 89 // thread before the instance is deleted on the IO thread. |
| 61 void CleanupOnUIThread(); | 90 void CleanupOnUIThread(); |
| 62 | 91 |
| 92 // These methods simply forward to the corresponding method on |
| 93 // ChromeURLRequestContext. |
| 94 void OnNewExtensions(const std::string& id, const FilePath& path); |
| 95 void OnUnloadedExtension(const std::string& id); |
| 96 |
| 97 // NotificationObserver implementation. |
| 98 virtual void Observe(NotificationType type, |
| 99 const NotificationSource& source, |
| 100 const NotificationDetails& details); |
| 101 |
| 102 private: |
| 103 // Registers an observer on |profile|'s preferences which will be used |
| 104 // to update the context when the default language and charset change. |
| 105 void RegisterPrefsObserver(Profile* profile); |
| 106 |
| 107 // Creates a request context for media resources from a regular request |
| 108 // context. This helper method is called from CreateOriginalForMedia and |
| 109 // CreateOffTheRecordForMedia. |
| 110 static ChromeURLRequestContextGetter* CreateRequestContextForMedia( |
| 111 Profile* profile, const FilePath& disk_cache_path, int cache_size, |
| 112 bool off_the_record); |
| 113 |
| 114 // These methods simply forward to the corresponding method on |
| 115 // ChromeURLRequestContext. |
| 116 void OnAcceptLanguageChange(const std::string& accept_language); |
| 117 void OnCookiePolicyChange(net::CookiePolicy::Type type); |
| 118 void OnDefaultCharsetChange(const std::string& default_charset); |
| 119 |
| 120 // Saves the cookie store to |result| and signals |completion|. |
| 121 void GetCookieStoreAsyncHelper(base::WaitableEvent* completion, |
| 122 net::CookieStore** result); |
| 123 |
| 124 // Access only from the UI thread. |
| 125 PrefService* prefs_; |
| 126 NotificationRegistrar registrar_; |
| 127 |
| 128 // Deferred logic for creating a ChromeURLRequestContext. |
| 129 // Access only from the IO thread. |
| 130 scoped_ptr<ChromeURLRequestContextFactory> factory_; |
| 131 |
| 132 // NULL if not yet initialized. Otherwise, it is the URLRequestContext |
| 133 // instance that was lazilly created by GetURLRequestContext. |
| 134 // Access only from the IO thread. |
| 135 scoped_refptr<URLRequestContext> url_request_context_; |
| 136 |
| 137 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextGetter); |
| 138 }; |
| 139 |
| 140 // Subclass of URLRequestContext which can be used to store extra information |
| 141 // for requests. |
| 142 // |
| 143 // All methods of this class must be called from the IO thread, |
| 144 // including the constructor and destructor. |
| 145 class ChromeURLRequestContext : public URLRequestContext { |
| 146 public: |
| 147 typedef std::map<std::string, FilePath> ExtensionPaths; |
| 148 |
| 149 ChromeURLRequestContext(); |
| 150 virtual ~ChromeURLRequestContext(); |
| 151 |
| 63 // Gets the path to the directory for the specified extension. | 152 // Gets the path to the directory for the specified extension. |
| 64 FilePath GetPathForExtension(const std::string& id); | 153 FilePath GetPathForExtension(const std::string& id); |
| 65 | 154 |
| 66 // Gets the path to the directory user scripts are stored in. | 155 // Gets the path to the directory user scripts are stored in. |
| 67 FilePath user_script_dir_path() const { | 156 FilePath user_script_dir_path() const { |
| 68 return user_script_dir_path_; | 157 return user_script_dir_path_; |
| 69 } | 158 } |
| 70 | 159 |
| 71 // Gets the appcache service to be used for requests in this context. | 160 // Gets the appcache service to be used for requests in this context. |
| 72 // May be NULL if requests for this context aren't subject to appcaching. | 161 // May be NULL if requests for this context aren't subject to appcaching. |
| 73 ChromeAppCacheService* appcache_service() const { | 162 ChromeAppCacheService* appcache_service() const { |
| 74 return appcache_service_.get(); | 163 return appcache_service_.get(); |
| 75 } | 164 } |
| 76 | 165 |
| 166 bool is_off_the_record() const { |
| 167 return is_off_the_record_; |
| 168 } |
| 169 bool is_media() const { |
| 170 return is_media_; |
| 171 } |
| 172 const ExtensionPaths& extension_paths() const { |
| 173 return extension_paths_; |
| 174 } |
| 175 |
| 77 virtual const std::string& GetUserAgent(const GURL& url) const; | 176 virtual const std::string& GetUserAgent(const GURL& url) const; |
| 78 | 177 |
| 79 virtual bool InterceptCookie(const URLRequest* request, std::string* cookie); | 178 virtual bool InterceptCookie(const URLRequest* request, std::string* cookie); |
| 80 | 179 |
| 81 virtual bool AllowSendingCookies(const URLRequest* request) const; | 180 virtual bool AllowSendingCookies(const URLRequest* request) const; |
| 82 | 181 |
| 83 // Gets the Privacy Blacklist, if any for this context. | 182 // Gets the Privacy Blacklist, if any for this context. |
| 84 const Blacklist* blacklist() const { return blacklist_; } | 183 const Blacklist* blacklist() const { return blacklist_; } |
| 85 | 184 |
| 86 // Callback for when new extensions are loaded. | 185 // Callback for when new extensions are loaded. |
| 87 void OnNewExtensions(const std::string& id, const FilePath& path); | 186 void OnNewExtensions(const std::string& id, const FilePath& path); |
| 88 | 187 |
| 89 // Callback for when an extension is unloaded. | 188 // Callback for when an extension is unloaded. |
| 90 void OnUnloadedExtension(const std::string& id); | 189 void OnUnloadedExtension(const std::string& id); |
| 91 | 190 |
| 92 protected: | 191 protected: |
| 93 // Private constructors, use the static factory methods instead. This is | 192 // Copies the dependencies from |other| into |this|. If you use this |
| 94 // expected to be called on the UI thread. | 193 // constructor, then you should hold a reference to |other|, as we |
| 95 ChromeURLRequestContext( | 194 // depend on |other| being alive. |
| 96 Profile* profile, ChromeAppCacheService* appcache_service); | |
| 97 ChromeURLRequestContext(ChromeURLRequestContext* other); | 195 ChromeURLRequestContext(ChromeURLRequestContext* other); |
| 98 | 196 |
| 99 // Create a request context for media resources from a regular request | 197 public: |
| 100 // context. This helper method is called from CreateOriginalForMedia and | 198 // Setters to simplify initializing from factory objects. |
| 101 // CreateOffTheRecordForMedia. | |
| 102 static ChromeURLRequestContext* CreateRequestContextForMedia(Profile* profile, | |
| 103 const FilePath& disk_cache_path, int cache_size, bool off_the_record, | |
| 104 ChromeAppCacheService* appache_service); | |
| 105 | 199 |
| 106 // NotificationObserver implementation. | 200 void set_accept_language(const std::string& accept_language) { |
| 107 virtual void Observe(NotificationType type, | 201 accept_language_ = accept_language; |
| 108 const NotificationSource& source, | 202 } |
| 109 const NotificationDetails& details); | 203 void set_accept_charset(const std::string& accept_charset) { |
| 204 accept_charset_ = accept_charset; |
| 205 } |
| 206 void set_referrer_charset(const std::string& referrer_charset) { |
| 207 referrer_charset_ = referrer_charset; |
| 208 } |
| 209 void set_cookie_policy_type(net::CookiePolicy::Type type) { |
| 210 cookie_policy_.set_type(type); |
| 211 } |
| 212 void set_strict_transport_security_state( |
| 213 net::StrictTransportSecurityState* state) { |
| 214 strict_transport_security_state_ = state; |
| 215 } |
| 216 void set_ssl_config_service(net::SSLConfigService* service) { |
| 217 ssl_config_service_ = service; |
| 218 } |
| 219 void set_host_resolver(net::HostResolver* resolver) { |
| 220 host_resolver_ = resolver; |
| 221 } |
| 222 void set_http_transaction_factory(net::HttpTransactionFactory* factory) { |
| 223 http_transaction_factory_ = factory; |
| 224 } |
| 225 void set_ftp_transaction_factory(net::FtpTransactionFactory* factory) { |
| 226 ftp_transaction_factory_ = factory; |
| 227 } |
| 228 void set_cookie_store(net::CookieStore* cookie_store) { |
| 229 cookie_store_ = cookie_store; |
| 230 } |
| 231 void set_proxy_service(net::ProxyService* service) { |
| 232 proxy_service_ = service; |
| 233 } |
| 234 void set_user_script_dir_path(const FilePath& path) { |
| 235 user_script_dir_path_ = path; |
| 236 } |
| 237 void set_is_off_the_record(bool is_off_the_record) { |
| 238 is_off_the_record_ = is_off_the_record; |
| 239 } |
| 240 void set_is_media(bool is_media) { |
| 241 is_media_ = is_media; |
| 242 } |
| 243 void set_extension_paths(const ExtensionPaths& paths) { |
| 244 extension_paths_ = paths; |
| 245 } |
| 246 void set_blacklist(const Blacklist* blacklist) { |
| 247 blacklist_ = blacklist; |
| 248 } |
| 249 void set_appcache_service(ChromeAppCacheService* service) { |
| 250 appcache_service_ = service; |
| 251 } |
| 110 | 252 |
| 111 // Callback for when the accept language changes. | 253 // Callback for when the accept language changes. |
| 112 void OnAcceptLanguageChange(const std::string& accept_language); | 254 void OnAcceptLanguageChange(const std::string& accept_language); |
| 113 | 255 |
| 114 // Callback for when the cookie policy changes. | 256 // Callback for when the cookie policy changes. |
| 115 void OnCookiePolicyChange(net::CookiePolicy::Type type); | 257 void OnCookiePolicyChange(net::CookiePolicy::Type type); |
| 116 | 258 |
| 117 // Callback for when the default charset changes. | 259 // Callback for when the default charset changes. |
| 118 void OnDefaultCharsetChange(const std::string& default_charset); | 260 void OnDefaultCharsetChange(const std::string& default_charset); |
| 119 | 261 |
| 120 // Destructor. | 262 protected: |
| 121 virtual ~ChromeURLRequestContext(); | |
| 122 | |
| 123 NotificationRegistrar registrar_; | |
| 124 | |
| 125 // Maps extension IDs to paths on disk. This is initialized in the | 263 // Maps extension IDs to paths on disk. This is initialized in the |
| 126 // construtor and updated when extensions changed. | 264 // construtor and updated when extensions changed. |
| 127 ExtensionPaths extension_paths_; | 265 ExtensionPaths extension_paths_; |
| 128 | 266 |
| 129 // Path to the directory user scripts are stored in. | 267 // Path to the directory user scripts are stored in. |
| 130 FilePath user_script_dir_path_; | 268 FilePath user_script_dir_path_; |
| 131 | 269 |
| 132 scoped_refptr<ChromeAppCacheService> appcache_service_; | 270 scoped_refptr<ChromeAppCacheService> appcache_service_; |
| 133 | 271 |
| 134 PrefService* prefs_; | |
| 135 const Blacklist* blacklist_; | 272 const Blacklist* blacklist_; |
| 136 bool is_media_; | 273 bool is_media_; |
| 137 bool is_off_the_record_; | 274 bool is_off_the_record_; |
| 275 |
| 276 private: |
| 277 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContext); |
| 278 }; |
| 279 |
| 280 // Base class for a ChromeURLRequestContext factory. This includes |
| 281 // the shared functionality like extracting the default language/charset |
| 282 // from a profile. |
| 283 // |
| 284 // Except for the constructor, all methods of this class must be called from |
| 285 // the IO thread. |
| 286 class ChromeURLRequestContextFactory { |
| 287 public: |
| 288 // Extract properties of interested from |profile|, for setting later into |
| 289 // a ChromeURLRequestContext using ApplyProfileParametersToContext(). |
| 290 ChromeURLRequestContextFactory(Profile* profile); |
| 291 |
| 292 virtual ~ChromeURLRequestContextFactory(); |
| 293 |
| 294 // Called to create a new instance (will only be called once). |
| 295 virtual ChromeURLRequestContext* Create() = 0; |
| 296 |
| 297 protected: |
| 298 // Assigns this factory's properties to |context|. |
| 299 void ApplyProfileParametersToContext(ChromeURLRequestContext* context); |
| 300 |
| 301 // Values extracted from the Profile. |
| 302 // |
| 303 // NOTE: If you add any parameters here, keep it in sync with |
| 304 // ApplyProfileParametersToContext(). |
| 305 bool is_media_; |
| 306 bool is_off_the_record_; |
| 307 std::string accept_language_; |
| 308 std::string accept_charset_; |
| 309 std::string referrer_charset_; |
| 310 net::CookiePolicy::Type cookie_policy_type_; |
| 311 ChromeURLRequestContext::ExtensionPaths extension_paths_; |
| 312 FilePath user_script_dir_path_; |
| 313 Blacklist* blacklist_; |
| 314 net::StrictTransportSecurityState* strict_transport_security_state_; |
| 315 scoped_refptr<net::SSLConfigService> ssl_config_service_; |
| 316 |
| 317 FilePath profile_dir_path_; |
| 318 |
| 319 // Values extracted from the browser process. |
| 320 MessageLoop* db_loop_; |
| 321 |
| 322 DISALLOW_COPY_AND_ASSIGN(ChromeURLRequestContextFactory); |
| 138 }; | 323 }; |
| 139 | 324 |
| 140 // Creates a proxy configuration using the overrides specified on the command | 325 // Creates a proxy configuration using the overrides specified on the command |
| 141 // line. Returns NULL if the system defaults should be used instead. | 326 // line. Returns NULL if the system defaults should be used instead. |
| 142 net::ProxyConfig* CreateProxyConfig(const CommandLine& command_line); | 327 net::ProxyConfig* CreateProxyConfig(const CommandLine& command_line); |
| 328 |
| 329 #endif // CHROME_BROWSER_NET_CHROME_URL_REQUEST_CONTEXT_H_ |
| OLD | NEW |