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.
5 // A class to help filter URLRequest jobs based on the URL of the request
6 // rather than just the scheme. Example usage:
8 // // Use as an "http" handler.
9 // URLRequest::RegisterProtocolFactory("http", &URLRequestFilter::Factory);
10 // // Add special handling for the URL http://foo.com/
11 // URLRequestFilter::GetInstance()->AddUrlHandler(
12 // GURL("http://foo.com/"),
13 // &URLRequestCustomJob::Factory);
15 // If URLRequestFilter::Factory can't find a handle for the request, it passes
16 // it through to URLRequestInetJob::Factory and lets the default network stack
19 #ifndef NET_URL_REQUEST_URL_REQUEST_FILTER_H_
20 #define NET_URL_REQUEST_URL_REQUEST_FILTER_H_
25 #include "base/callback.h"
26 #include "base/containers/hash_tables.h"
27 #include "base/memory/scoped_ptr.h"
28 #include "net/base/net_export.h"
29 #include "net/url_request/url_request.h"
30 #include "net/url_request/url_request_job_factory.h"
37 class NET_EXPORT URLRequestFilter {
39 // scheme,hostname -> ProtocolHandler
40 typedef std::map<std::pair<std::string, std::string>,
41 URLRequestJobFactory::ProtocolHandler* > HostnameHandlerMap;
42 // URL -> ProtocolHandler
43 typedef base::hash_map<std::string, URLRequestJobFactory::ProtocolHandler*>
48 static URLRequest::ProtocolFactory Factory;
50 // Singleton instance for use.
51 static URLRequestFilter* GetInstance();
53 void AddHostnameHandler(const std::string& scheme,
54 const std::string& hostname,
55 URLRequest::ProtocolFactory* factory);
56 void AddHostnameProtocolHandler(
57 const std::string& scheme,
58 const std::string& hostname,
59 scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler);
60 void RemoveHostnameHandler(const std::string& scheme,
61 const std::string& hostname);
63 // Returns true if we successfully added the URL handler. This will replace
64 // old handlers for the URL if one existed.
65 bool AddUrlHandler(const GURL& url,
66 URLRequest::ProtocolFactory* factory);
67 bool AddUrlProtocolHandler(
69 scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler);
71 void RemoveUrlHandler(const GURL& url);
73 // Clear all the existing URL handlers and unregister with the
74 // ProtocolFactory. Resets the hit count.
77 // Returns the number of times a handler was used to service a request.
78 int hit_count() const { return hit_count_; }
83 // Helper method that looks up the request in the url_handler_map_.
84 URLRequestJob* FindRequestHandler(URLRequest* request,
85 NetworkDelegate* network_delegate,
86 const std::string& scheme);
88 // Maps hostnames to factories. Hostnames take priority over URLs.
89 HostnameHandlerMap hostname_handler_map_;
91 // Maps URLs to factories.
92 UrlHandlerMap url_handler_map_;
97 // Singleton instance.
98 static URLRequestFilter* shared_instance_;
100 DISALLOW_COPY_AND_ASSIGN(URLRequestFilter);
105 #endif // NET_URL_REQUEST_URL_REQUEST_FILTER_H_