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.
4 #ifndef NET_URL_REQUEST_URL_REQUEST_FILTER_H_
5 #define NET_URL_REQUEST_URL_REQUEST_FILTER_H_
10 #include "base/callback.h"
11 #include "base/containers/hash_tables.h"
12 #include "base/memory/scoped_ptr.h"
13 #include "net/base/net_export.h"
14 #include "net/url_request/url_request.h"
15 #include "net/url_request/url_request_interceptor.h"
21 class URLRequestInterceptor;
23 // A class to help filter URLRequest jobs based on the URL of the request
24 // rather than just the scheme. Example usage:
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());
33 // If the URLRequestFilter::MaybeInterceptRequest can't find a handler for a
34 // request, it returns NULL and lets the configured ProtocolHandler handle the
36 class NET_EXPORT URLRequestFilter : public URLRequestInterceptor {
38 static URLRequest::ProtocolFactory Factory;
40 // Singleton instance for use.
41 static URLRequestFilter* GetInstance();
43 void AddHostnameHandler(const std::string& scheme,
44 const std::string& hostname,
45 URLRequest::ProtocolFactory* factory);
46 void AddHostnameInterceptor(
47 const std::string& scheme,
48 const std::string& hostname,
49 scoped_ptr<URLRequestInterceptor> interceptor);
50 void RemoveHostnameHandler(const std::string& scheme,
51 const std::string& hostname);
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);
57 bool AddUrlInterceptor(const GURL& url,
58 scoped_ptr<URLRequestInterceptor> interceptor);
60 void RemoveUrlHandler(const GURL& url);
62 // Clear all the existing URL handlers and unregister with the
63 // ProtocolFactory. Resets the hit count.
66 // Returns the number of times a handler was used to service a request.
67 int hit_count() const { return hit_count_; }
69 // URLRequestInterceptor implementation:
70 URLRequestJob* MaybeInterceptRequest(
72 NetworkDelegate* network_delegate) const override;
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;
82 ~URLRequestFilter() override;
84 // Maps hostnames to interceptors. Hostnames take priority over URLs.
85 HostnameInterceptorMap hostname_interceptor_map_;
87 // Maps URLs to interceptors.
88 URLInterceptorMap url_interceptor_map_;
90 mutable int hit_count_;
92 // Singleton instance.
93 static URLRequestFilter* shared_instance_;
95 DISALLOW_COPY_AND_ASSIGN(URLRequestFilter);
100 #endif // NET_URL_REQUEST_URL_REQUEST_FILTER_H_