Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
| 7 #pragma once | 7 #pragma once |
| 8 | 8 |
| 9 #include <map> | 9 #include <map> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/singleton.h" | 14 #include "base/singleton.h" |
| 15 #include "chrome/browser/extensions/extension_function.h" | 15 #include "chrome/browser/extensions/extension_function.h" |
| 16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
| 17 #include "ipc/ipc_message.h" | 17 #include "ipc/ipc_message.h" |
| 18 #include "net/base/completion_callback.h" | 18 #include "net/base/completion_callback.h" |
| 19 #include "webkit/glue/resource_type.h" | 19 #include "webkit/glue/resource_type.h" |
| 20 | 20 |
| 21 class ExtensionEventRouterForwarder; | 21 class ExtensionEventRouterForwarder; |
| 22 class GURL; | 22 class GURL; |
| 23 | 23 |
| 24 namespace net { | 24 namespace net { |
| 25 class HttpRequestHeaders; | |
| 25 class URLRequest; | 26 class URLRequest; |
| 26 } | 27 } |
| 27 | 28 |
| 28 // This class observes network events and routes them to the appropriate | 29 // This class observes network events and routes them to the appropriate |
| 29 // extensions listening to those events. All methods must be called on the IO | 30 // extensions listening to those events. All methods must be called on the IO |
| 30 // thread unless otherwise specified. | 31 // thread unless otherwise specified. |
| 31 class ExtensionWebRequestEventRouter { | 32 class ExtensionWebRequestEventRouter { |
| 32 public: | 33 public: |
| 33 struct RequestFilter; | 34 struct RequestFilter; |
| 34 struct ExtraInfoSpec; | 35 struct ExtraInfoSpec; |
| 35 | 36 |
| 36 static ExtensionWebRequestEventRouter* GetInstance(); | 37 static ExtensionWebRequestEventRouter* GetInstance(); |
| 37 | 38 |
| 38 // Dispatches the OnBeforeRequest event to any extensions whose filters match | 39 // Dispatches the OnBeforeRequest event to any extensions whose filters match |
| 39 // the given request. Returns true if an extension wants to pause the request. | 40 // the given request. Returns net::ERR_IO_PENDING if an extension is |
| 40 bool OnBeforeRequest(ProfileId profile_id, | 41 // intercepting the request, OK otherwise. |
| 41 ExtensionEventRouterForwarder* event_router, | 42 int OnBeforeRequest(ProfileId profile_id, |
| 42 net::URLRequest* request, | 43 ExtensionEventRouterForwarder* event_router, |
| 43 net::CompletionCallback* callback); | 44 net::URLRequest* request, |
| 45 net::CompletionCallback* callback); | |
| 46 | |
| 47 // Dispatches the onBeforeSendHeaders event. This is fired for HTTP(s) | |
| 48 // requests only, and allows modification of the outgoing request headers. | |
| 49 // Returns net::ERR_IO_PENDING if an extension is intercepting the request, OK | |
| 50 // otherwise. | |
| 51 int OnBeforeSendHeaders(ProfileId profile_id, | |
| 52 ExtensionEventRouterForwarder* event_router, | |
| 53 uint64 request_id, | |
| 54 net::HttpRequestHeaders* headers, | |
| 55 net::CompletionCallback* callback); | |
| 56 | |
| 57 void OnURLRequestDestroyed(ProfileId profile_id, net::URLRequest* request); | |
| 44 | 58 |
| 45 // Called when an event listener handles a blocking event and responds. | 59 // Called when an event listener handles a blocking event and responds. |
| 46 // TODO(mpcomplete): modify request | 60 // TODO(mpcomplete): modify request |
| 47 void OnEventHandled( | 61 void OnEventHandled( |
| 48 ProfileId profile_id, | 62 ProfileId profile_id, |
| 49 const std::string& extension_id, | 63 const std::string& extension_id, |
| 50 const std::string& event_name, | 64 const std::string& event_name, |
| 51 const std::string& sub_event_name, | 65 const std::string& sub_event_name, |
| 52 uint64 request_id, | 66 uint64 request_id, |
| 53 bool cancel); | 67 bool cancel); |
| (...skipping 16 matching lines...) Expand all Loading... | |
| 70 const std::string& extension_id, | 84 const std::string& extension_id, |
| 71 const std::string& sub_event_name); | 85 const std::string& sub_event_name); |
| 72 | 86 |
| 73 private: | 87 private: |
| 74 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 88 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
| 75 struct EventListener; | 89 struct EventListener; |
| 76 struct BlockedRequest; | 90 struct BlockedRequest; |
| 77 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; | 91 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; |
| 78 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; | 92 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; |
| 79 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; | 93 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; |
| 94 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; | |
| 80 | 95 |
| 81 ExtensionWebRequestEventRouter(); | 96 ExtensionWebRequestEventRouter(); |
| 82 ~ExtensionWebRequestEventRouter(); | 97 ~ExtensionWebRequestEventRouter(); |
| 83 | 98 |
| 99 bool DispatchEvent( | |
| 100 ProfileId profile_id, | |
| 101 ExtensionEventRouterForwarder* event_router, | |
| 102 net::URLRequest* request, | |
| 103 net::CompletionCallback* callback, | |
| 104 const std::vector<const EventListener*>& listeners, | |
| 105 const ListValue& args); | |
| 106 | |
| 84 // Returns a list of event listeners that care about the given event, based | 107 // Returns a list of event listeners that care about the given event, based |
| 85 // on their filter parameters. | 108 // on their filter parameters. |
| 86 std::vector<const EventListener*> GetMatchingListeners( | 109 std::vector<const EventListener*> GetMatchingListeners( |
| 87 ProfileId profile_id, | 110 ProfileId profile_id, |
| 88 const std::string& event_name, | 111 const std::string& event_name, |
| 89 const GURL& url, | 112 const GURL& url, |
| 90 int tab_id, | 113 int tab_id, |
| 91 int window_id, | 114 int window_id, |
| 92 ResourceType::Type resource_type); | 115 ResourceType::Type resource_type); |
| 93 | 116 |
| 117 // Same as above, but retrieves the filter parameters from the request. | |
| 118 std::vector<const EventListener*> GetMatchingListeners( | |
|
willchan no longer on Chromium
2011/03/26 01:46:49
Nit: Make sure you follow the rules on overloading
Matt Perry
2011/03/28 22:51:01
The guide has changed slightly since I last looked
| |
| 119 ProfileId profile_id, | |
| 120 const std::string& event_name, | |
| 121 net::URLRequest* request); | |
| 94 // Decrements the count of event handlers blocking the given request. When the | 122 // Decrements the count of event handlers blocking the given request. When the |
| 95 // count reaches 0 (or immediately if the request is being cancelled), we | 123 // count reaches 0 (or immediately if the request is being cancelled), we |
| 96 // stop blocking the request and either resume or cancel it. | 124 // stop blocking the request and either resume or cancel it. |
| 97 void DecrementBlockCount(uint64 request_id, bool cancel); | 125 void DecrementBlockCount(uint64 request_id, bool cancel); |
| 98 | 126 |
| 127 void OnRequestDeleted(net::URLRequest* request); | |
| 128 | |
| 99 // A map for each profile that maps an event name to a set of extensions that | 129 // A map for each profile that maps an event name to a set of extensions that |
| 100 // are listening to that event. | 130 // are listening to that event. |
| 101 ListenerMap listeners_; | 131 ListenerMap listeners_; |
| 102 | 132 |
| 103 // A map of network requests that are waiting for at least one event handler | 133 // A map of network requests that are waiting for at least one event handler |
| 104 // to respond. | 134 // to respond. |
| 105 BlockedRequestMap blocked_requests_; | 135 BlockedRequestMap blocked_requests_; |
| 106 | 136 |
| 137 // A map of HTTP(s) network requests. We don't dispatch onBeforeRequest for | |
|
willchan no longer on Chromium
2011/03/26 01:46:49
Is this comment still true? Aren't we going to dis
Matt Perry
2011/03/28 22:51:01
Oops, fixed.
| |
| 138 // these requests until headers are available. | |
| 139 HttpRequestMap http_requests_; | |
| 140 | |
| 107 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); | 141 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); |
| 108 }; | 142 }; |
| 109 | 143 |
| 110 class WebRequestAddEventListener : public SyncExtensionFunction { | 144 class WebRequestAddEventListener : public SyncExtensionFunction { |
| 111 public: | 145 public: |
| 112 virtual bool RunImpl(); | 146 virtual bool RunImpl(); |
| 113 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); | 147 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); |
| 114 }; | 148 }; |
| 115 | 149 |
| 116 class WebRequestEventHandled : public SyncExtensionFunction { | 150 class WebRequestEventHandled : public SyncExtensionFunction { |
| 117 public: | 151 public: |
| 118 virtual bool RunImpl(); | 152 virtual bool RunImpl(); |
| 119 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); | 153 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); |
| 120 }; | 154 }; |
| 121 | 155 |
| 122 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 156 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
| OLD | NEW |