blob: 93baedba9ac27322e1d6b9852da73c24edeeac19 [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// 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)58218062012-11-14 11:43:16 +00004#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)2a99a7e2013-03-28 15:31:22 +000010#include "base/callback.h"
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010011#include "base/containers/hash_tables.h"
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000012#include "base/memory/scoped_ptr.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000013#include "net/base/net_export.h"
14#include "net/url_request/url_request.h"
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010015#include "net/url_request/url_request_interceptor.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000016
17class GURL;
18
19namespace net {
20class URLRequestJob;
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010021class URLRequestInterceptor;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000022
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010023// 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.
36class NET_EXPORT URLRequestFilter : public URLRequestInterceptor {
Torne (Richard Coles)58218062012-11-14 11:43:16 +000037 public:
Torne (Richard Coles)58218062012-11-14 11:43:16 +000038 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)46d4c2b2014-06-09 12:00:27 +010046 void AddHostnameInterceptor(
Torne (Richard Coles)2a99a7e2013-03-28 15:31:22 +000047 const std::string& scheme,
48 const std::string& hostname,
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010049 scoped_ptr<URLRequestInterceptor> interceptor);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000050 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)46d4c2b2014-06-09 12:00:27 +010057 bool AddUrlInterceptor(const GURL& url,
58 scoped_ptr<URLRequestInterceptor> interceptor);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000059
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)f8ee7882014-06-20 14:52:04 +010069 // URLRequestInterceptor implementation:
70 virtual URLRequestJob* MaybeInterceptRequest(
71 URLRequest* request,
72 NetworkDelegate* network_delegate) const OVERRIDE;
73
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010074 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)58218062012-11-14 11:43:16 +000081 URLRequestFilter();
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010082 virtual ~URLRequestFilter();
Torne (Richard Coles)58218062012-11-14 11:43:16 +000083
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010084 // Maps hostnames to interceptors. Hostnames take priority over URLs.
85 HostnameInterceptorMap hostname_interceptor_map_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000086
Torne (Richard Coles)46d4c2b2014-06-09 12:00:27 +010087 // Maps URLs to interceptors.
88 URLInterceptorMap url_interceptor_map_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000089
Torne (Richard Coles)f8ee7882014-06-20 14:52:04 +010090 mutable int hit_count_;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000091
Torne (Richard Coles)58218062012-11-14 11:43:16 +000092 // 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_