• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_
6 
7 #include <map>
8 #include <string>
9 
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"
16 
17 class GURL;
18 
19 namespace net {
20 class URLRequestJob;
21 class URLRequestInterceptor;
22 
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 {
37  public:
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);
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);
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);
57   bool AddUrlInterceptor(const GURL& url,
58                          scoped_ptr<URLRequestInterceptor> interceptor);
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.
hit_count()67   int hit_count() const { return hit_count_; }
68 
69   // URLRequestInterceptor implementation:
70   virtual URLRequestJob* MaybeInterceptRequest(
71       URLRequest* request,
72       NetworkDelegate* network_delegate) const OVERRIDE;
73 
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 
81   URLRequestFilter();
82   virtual ~URLRequestFilter();
83 
84   // Maps hostnames to interceptors.  Hostnames take priority over URLs.
85   HostnameInterceptorMap hostname_interceptor_map_;
86 
87   // Maps URLs to interceptors.
88   URLInterceptorMap url_interceptor_map_;
89 
90   mutable int hit_count_;
91 
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_
101