Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 1 | // Copyright (c) 2011 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. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 4 | #ifndef NET_URL_REQUEST_URL_REQUEST_FILTER_H_ |
| 5 | #define NET_URL_REQUEST_URL_REQUEST_FILTER_H_ |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 10 | #include "base/callback.h" |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 11 | #include "base/containers/hash_tables.h" |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 12 | #include "base/memory/scoped_ptr.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 13 | #include "net/base/net_export.h" |
| 14 | #include "net/url_request/url_request.h" |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 15 | #include "net/url_request/url_request_interceptor.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 16 | |
| 17 | class GURL; |
| 18 | |
| 19 | namespace net { |
| 20 | class URLRequestJob; |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 21 | class URLRequestInterceptor; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 22 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 23 | // A class to help filter URLRequest jobs based on the URL of the request |
| 24 | // rather than just the scheme. Example usage: |
| 25 | // |
| 26 | // // Intercept "scheme://host/" requests. |
| 27 | // URLRequestFilter::GetInstance()->AddHostnameInterceptor("scheme", "host", |
| 28 | // interceptor.Pass()); |
| 29 | // // Add special handling for the URL http://foo.com/ |
| 30 | // URLRequestFilter::GetInstance()->AddUrlInterceptor(GURL("http://foo.com/"), |
| 31 | // interceptor.Pass()); |
| 32 | // |
| 33 | // If the URLRequestFilter::MaybeInterceptRequest can't find a handler for a |
| 34 | // request, it returns NULL and lets the configured ProtocolHandler handle the |
| 35 | // request. |
| 36 | class NET_EXPORT URLRequestFilter : public URLRequestInterceptor { |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 37 | public: |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 38 | static URLRequest::ProtocolFactory Factory; |
| 39 | |
| 40 | // Singleton instance for use. |
| 41 | static URLRequestFilter* GetInstance(); |
| 42 | |
| 43 | void AddHostnameHandler(const std::string& scheme, |
| 44 | const std::string& hostname, |
| 45 | URLRequest::ProtocolFactory* factory); |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 46 | void AddHostnameInterceptor( |
Torne (Richard Coles) | 2a99a7e | 2013-03-28 15:31:22 +0000 | [diff] [blame] | 47 | const std::string& scheme, |
| 48 | const std::string& hostname, |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 49 | scoped_ptr<URLRequestInterceptor> interceptor); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 50 | void RemoveHostnameHandler(const std::string& scheme, |
| 51 | const std::string& hostname); |
| 52 | |
| 53 | // Returns true if we successfully added the URL handler. This will replace |
| 54 | // old handlers for the URL if one existed. |
| 55 | bool AddUrlHandler(const GURL& url, |
| 56 | URLRequest::ProtocolFactory* factory); |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 57 | bool AddUrlInterceptor(const GURL& url, |
| 58 | scoped_ptr<URLRequestInterceptor> interceptor); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 59 | |
| 60 | void RemoveUrlHandler(const GURL& url); |
| 61 | |
| 62 | // Clear all the existing URL handlers and unregister with the |
| 63 | // ProtocolFactory. Resets the hit count. |
| 64 | void ClearHandlers(); |
| 65 | |
| 66 | // Returns the number of times a handler was used to service a request. |
| 67 | int hit_count() const { return hit_count_; } |
| 68 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 69 | // URLRequestInterceptor implementation: |
| 70 | virtual URLRequestJob* MaybeInterceptRequest( |
| 71 | URLRequest* request, |
| 72 | NetworkDelegate* network_delegate) const OVERRIDE; |
| 73 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 74 | private: |
| 75 | // scheme,hostname -> URLRequestInterceptor |
| 76 | typedef std::map<std::pair<std::string, std::string>, |
| 77 | URLRequestInterceptor* > HostnameInterceptorMap; |
| 78 | // URL -> URLRequestInterceptor |
| 79 | typedef base::hash_map<std::string, URLRequestInterceptor*> URLInterceptorMap; |
| 80 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 81 | URLRequestFilter(); |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 82 | virtual ~URLRequestFilter(); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 83 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 84 | // Maps hostnames to interceptors. Hostnames take priority over URLs. |
| 85 | HostnameInterceptorMap hostname_interceptor_map_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 86 | |
Torne (Richard Coles) | 46d4c2b | 2014-06-09 12:00:27 +0100 | [diff] [blame] | 87 | // Maps URLs to interceptors. |
| 88 | URLInterceptorMap url_interceptor_map_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 89 | |
Torne (Richard Coles) | f8ee788 | 2014-06-20 14:52:04 +0100 | [diff] [blame] | 90 | mutable int hit_count_; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 91 | |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 92 | // Singleton instance. |
| 93 | static URLRequestFilter* shared_instance_; |
| 94 | |
| 95 | DISALLOW_COPY_AND_ASSIGN(URLRequestFilter); |
| 96 | }; |
| 97 | |
| 98 | } // namespace net |
| 99 | |
| 100 | #endif // NET_URL_REQUEST_URL_REQUEST_FILTER_H_ |