- add sources.
[platform/framework/web/crosswalk.git] / src / net / url_request / url_request_filter.h
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 //
5 // A class to help filter URLRequest jobs based on the URL of the request
6 // rather than just the scheme.  Example usage:
7 //
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);
14 //
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
17 // handle it.
18
19 #ifndef NET_URL_REQUEST_URL_REQUEST_FILTER_H_
20 #define NET_URL_REQUEST_URL_REQUEST_FILTER_H_
21
22 #include <map>
23 #include <string>
24
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"
31
32 class GURL;
33
34 namespace net {
35 class URLRequestJob;
36
37 class NET_EXPORT URLRequestFilter {
38  public:
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*>
44       UrlHandlerMap;
45
46   ~URLRequestFilter();
47
48   static URLRequest::ProtocolFactory Factory;
49
50   // Singleton instance for use.
51   static URLRequestFilter* GetInstance();
52
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);
62
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(
68       const GURL& url,
69       scoped_ptr<URLRequestJobFactory::ProtocolHandler> protocol_handler);
70
71   void RemoveUrlHandler(const GURL& url);
72
73   // Clear all the existing URL handlers and unregister with the
74   // ProtocolFactory.  Resets the hit count.
75   void ClearHandlers();
76
77   // Returns the number of times a handler was used to service a request.
78   int hit_count() const { return hit_count_; }
79
80  protected:
81   URLRequestFilter();
82
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);
87
88   // Maps hostnames to factories.  Hostnames take priority over URLs.
89   HostnameHandlerMap hostname_handler_map_;
90
91   // Maps URLs to factories.
92   UrlHandlerMap url_handler_map_;
93
94   int hit_count_;
95
96  private:
97   // Singleton instance.
98   static URLRequestFilter* shared_instance_;
99
100   DISALLOW_COPY_AND_ASSIGN(URLRequestFilter);
101 };
102
103 }  // namespace net
104
105 #endif  // NET_URL_REQUEST_URL_REQUEST_FILTER_H_