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/memory/singleton.h" | 14 #include "base/memory/singleton.h" |
15 #include "base/time.h" | 15 #include "base/time.h" |
16 #include "chrome/browser/extensions/extension_function.h" | 16 #include "chrome/browser/extensions/extension_function.h" |
17 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
18 #include "chrome/common/extensions/url_pattern_set.h" | 18 #include "chrome/common/extensions/url_pattern_set.h" |
19 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
20 #include "net/base/completion_callback.h" | 20 #include "net/base/completion_callback.h" |
21 #include "webkit/glue/resource_type.h" | 21 #include "webkit/glue/resource_type.h" |
22 | 22 |
23 class DictionaryValue; | 23 class DictionaryValue; |
24 class ExtensionEventRouterForwarder; | 24 class ExtensionInfoMap; |
25 class GURL; | 25 class GURL; |
26 class ListValue; | 26 class ListValue; |
27 class StringValue; | 27 class StringValue; |
28 | 28 |
29 namespace net { | 29 namespace net { |
30 class HostPortPair; | 30 class HostPortPair; |
31 class HttpRequestHeaders; | 31 class HttpRequestHeaders; |
32 class HttpResponseHeaders; | 32 class HttpResponseHeaders; |
33 class URLRequest; | 33 class URLRequest; |
34 } | 34 } |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
102 | 102 |
103 DISALLOW_COPY_AND_ASSIGN(EventResponse); | 103 DISALLOW_COPY_AND_ASSIGN(EventResponse); |
104 }; | 104 }; |
105 | 105 |
106 static ExtensionWebRequestEventRouter* GetInstance(); | 106 static ExtensionWebRequestEventRouter* GetInstance(); |
107 | 107 |
108 // Dispatches the OnBeforeRequest event to any extensions whose filters match | 108 // Dispatches the OnBeforeRequest event to any extensions whose filters match |
109 // the given request. Returns net::ERR_IO_PENDING if an extension is | 109 // the given request. Returns net::ERR_IO_PENDING if an extension is |
110 // intercepting the request, OK otherwise. | 110 // intercepting the request, OK otherwise. |
111 int OnBeforeRequest(ProfileId profile_id, | 111 int OnBeforeRequest(ProfileId profile_id, |
112 ExtensionEventRouterForwarder* event_router, | 112 ExtensionInfoMap* extension_info_map, |
113 net::URLRequest* request, | 113 net::URLRequest* request, |
114 net::CompletionCallback* callback, | 114 net::CompletionCallback* callback, |
115 GURL* new_url); | 115 GURL* new_url); |
116 | 116 |
117 // Dispatches the onBeforeSendHeaders event. This is fired for HTTP(s) | 117 // Dispatches the onBeforeSendHeaders event. This is fired for HTTP(s) |
118 // requests only, and allows modification of the outgoing request headers. | 118 // requests only, and allows modification of the outgoing request headers. |
119 // Returns net::ERR_IO_PENDING if an extension is intercepting the request, OK | 119 // Returns net::ERR_IO_PENDING if an extension is intercepting the request, OK |
120 // otherwise. | 120 // otherwise. |
121 int OnBeforeSendHeaders(ProfileId profile_id, | 121 int OnBeforeSendHeaders(ProfileId profile_id, |
122 ExtensionEventRouterForwarder* event_router, | 122 ExtensionInfoMap* extension_info_map, |
123 uint64 request_id, | 123 uint64 request_id, |
124 net::CompletionCallback* callback, | 124 net::CompletionCallback* callback, |
125 net::HttpRequestHeaders* headers); | 125 net::HttpRequestHeaders* headers); |
126 | 126 |
127 // Dispatches the onRequestSent event. This is fired for HTTP(s) requests | 127 // Dispatches the onRequestSent event. This is fired for HTTP(s) requests |
128 // only. | 128 // only. |
129 void OnRequestSent(ProfileId profile_id, | 129 void OnRequestSent(ProfileId profile_id, |
130 ExtensionEventRouterForwarder* event_router, | 130 ExtensionInfoMap* extension_info_map, |
131 uint64 request_id, | 131 uint64 request_id, |
132 const net::HostPortPair& socket_address, | 132 const net::HostPortPair& socket_address, |
133 const net::HttpRequestHeaders& headers); | 133 const net::HttpRequestHeaders& headers); |
134 | 134 |
135 // Dispatches the onBeforeRedirect event. This is fired for HTTP(s) requests | 135 // Dispatches the onBeforeRedirect event. This is fired for HTTP(s) requests |
136 // only. | 136 // only. |
137 void OnBeforeRedirect(ProfileId profile_id, | 137 void OnBeforeRedirect(ProfileId profile_id, |
138 ExtensionEventRouterForwarder* event_router, | 138 ExtensionInfoMap* extension_info_map, |
139 net::URLRequest* request, | 139 net::URLRequest* request, |
140 const GURL& new_location); | 140 const GURL& new_location); |
141 | 141 |
142 // Dispatches the onResponseStarted event indicating that the first bytes of | 142 // Dispatches the onResponseStarted event indicating that the first bytes of |
143 // the response have arrived. | 143 // the response have arrived. |
144 void OnResponseStarted(ProfileId profile_id, | 144 void OnResponseStarted(ProfileId profile_id, |
145 ExtensionEventRouterForwarder* event_router, | 145 ExtensionInfoMap* extension_info_map, |
146 net::URLRequest* request); | 146 net::URLRequest* request); |
147 | 147 |
148 // Dispatches the onComplete event. | 148 // Dispatches the onComplete event. |
149 void OnCompleted(ProfileId profile_id, | 149 void OnCompleted(ProfileId profile_id, |
150 ExtensionEventRouterForwarder* event_router, | 150 ExtensionInfoMap* extension_info_map, |
151 net::URLRequest* request); | 151 net::URLRequest* request); |
152 | 152 |
153 // Dispatches an onErrorOccurred event. | 153 // Dispatches an onErrorOccurred event. |
154 void OnErrorOccurred(ProfileId profile_id, | 154 void OnErrorOccurred(ProfileId profile_id, |
155 ExtensionEventRouterForwarder* event_router, | 155 ExtensionInfoMap* extension_info_map, |
156 net::URLRequest* request); | 156 net::URLRequest* request); |
157 | 157 |
158 // Notifications when objects are going away. | 158 // Notifications when objects are going away. |
159 void OnURLRequestDestroyed(ProfileId profile_id, net::URLRequest* request); | 159 void OnURLRequestDestroyed(ProfileId profile_id, net::URLRequest* request); |
160 void OnHttpTransactionDestroyed(ProfileId profile_id, uint64 request_id); | 160 void OnHttpTransactionDestroyed(ProfileId profile_id, uint64 request_id); |
161 | 161 |
162 // Called when an event listener handles a blocking event and responds. | 162 // Called when an event listener handles a blocking event and responds. |
163 void OnEventHandled( | 163 void OnEventHandled( |
164 ProfileId profile_id, | 164 ProfileId profile_id, |
165 const std::string& extension_id, | 165 const std::string& extension_id, |
166 const std::string& event_name, | 166 const std::string& event_name, |
167 const std::string& sub_event_name, | 167 const std::string& sub_event_name, |
168 uint64 request_id, | 168 uint64 request_id, |
169 EventResponse* response); | 169 EventResponse* response); |
170 | 170 |
171 // Adds a listener to the given event. |event_name| specifies the event being | 171 // Adds a listener to the given event. |event_name| specifies the event being |
172 // listened to. |sub_event_name| is an internal event uniquely generated in | 172 // listened to. |sub_event_name| is an internal event uniquely generated in |
173 // the extension process to correspond to the given filter and | 173 // the extension process to correspond to the given filter and |
174 // extra_info_spec. | 174 // extra_info_spec. |
175 void AddEventListener( | 175 void AddEventListener( |
176 ProfileId profile_id, | 176 ProfileId profile_id, |
177 const std::string& extension_id, | 177 const std::string& extension_id, |
178 const std::string& event_name, | 178 const std::string& event_name, |
179 const std::string& sub_event_name, | 179 const std::string& sub_event_name, |
180 const RequestFilter& filter, | 180 const RequestFilter& filter, |
181 int extra_info_spec); | 181 int extra_info_spec, |
| 182 base::WeakPtr<IPC::Message::Sender> ipc_sender); |
182 | 183 |
183 // Removes the listener for the given sub-event. | 184 // Removes the listener for the given sub-event. |
184 void RemoveEventListener( | 185 void RemoveEventListener( |
185 ProfileId profile_id, | 186 ProfileId profile_id, |
186 const std::string& extension_id, | 187 const std::string& extension_id, |
187 const std::string& sub_event_name); | 188 const std::string& sub_event_name); |
188 | 189 |
| 190 // Called when an incognito profile is created or destroyed. |
| 191 void OnOTRProfileCreated(ProfileId original_profile_id, |
| 192 ProfileId otr_profile_id); |
| 193 void OnOTRProfileDestroyed(ProfileId original_profile_id, |
| 194 ProfileId otr_profile_id); |
| 195 |
189 private: | 196 private: |
190 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 197 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
191 struct EventListener; | 198 struct EventListener; |
192 struct BlockedRequest; | 199 struct BlockedRequest; |
193 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; | 200 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; |
194 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; | 201 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; |
195 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; | 202 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; |
196 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; | 203 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; |
197 // Map of request_id -> bit vector of EventTypes already signaled | 204 // Map of request_id -> bit vector of EventTypes already signaled |
198 typedef std::map<uint64, int> SignaledRequestMap; | 205 typedef std::map<uint64, int> SignaledRequestMap; |
| 206 typedef std::map<ProfileId, ProfileId> CrossProfileMap; |
199 | 207 |
200 ExtensionWebRequestEventRouter(); | 208 ExtensionWebRequestEventRouter(); |
201 ~ExtensionWebRequestEventRouter(); | 209 ~ExtensionWebRequestEventRouter(); |
202 | 210 |
203 bool DispatchEvent( | 211 bool DispatchEvent( |
204 ProfileId profile_id, | 212 ProfileId profile_id, |
205 ExtensionEventRouterForwarder* event_router, | |
206 net::URLRequest* request, | 213 net::URLRequest* request, |
207 const std::vector<const EventListener*>& listeners, | 214 const std::vector<const EventListener*>& listeners, |
208 const ListValue& args); | 215 const ListValue& args); |
209 | 216 |
210 // Returns a list of event listeners that care about the given event, based | 217 // Returns a list of event listeners that care about the given event, based |
211 // on their filter parameters. |extra_info_spec| will contain the combined | 218 // on their filter parameters. |extra_info_spec| will contain the combined |
212 // set of extra_info_spec flags that every matching listener asked for. | 219 // set of extra_info_spec flags that every matching listener asked for. |
213 std::vector<const EventListener*> GetMatchingListeners( | 220 std::vector<const EventListener*> GetMatchingListeners( |
214 ProfileId profile_id, | 221 ProfileId profile_id, |
| 222 ExtensionInfoMap* extension_info_map, |
215 const std::string& event_name, | 223 const std::string& event_name, |
216 const GURL& url, | 224 const GURL& url, |
217 int tab_id, | 225 int tab_id, |
218 int window_id, | 226 int window_id, |
219 ResourceType::Type resource_type, | 227 ResourceType::Type resource_type, |
220 int* extra_info_spec); | 228 int* extra_info_spec); |
221 | 229 |
222 // Same as above, but retrieves the filter parameters from the request. | 230 // Same as above, but retrieves the filter parameters from the request. |
223 std::vector<const EventListener*> GetMatchingListeners( | 231 std::vector<const EventListener*> GetMatchingListeners( |
224 ProfileId profile_id, | 232 ProfileId profile_id, |
| 233 ExtensionInfoMap* extension_info_map, |
225 const std::string& event_name, | 234 const std::string& event_name, |
226 net::URLRequest* request, | 235 net::URLRequest* request, |
227 int* extra_info_spec); | 236 int* extra_info_spec); |
228 | 237 |
| 238 // Helper for the above functions. This is called twice: once for the profile |
| 239 // of the event, the next time for the "cross" profile (i.e. the incognito |
| 240 // profile if the event is originally for the normal profile, or vice versa). |
| 241 void GetMatchingListenersImpl( |
| 242 ProfileId profile_id, |
| 243 ExtensionInfoMap* extension_info_map, |
| 244 bool crosses_incognito, |
| 245 const std::string& event_name, |
| 246 const GURL& url, |
| 247 int tab_id, |
| 248 int window_id, |
| 249 ResourceType::Type resource_type, |
| 250 int* extra_info_spec, |
| 251 std::vector<const ExtensionWebRequestEventRouter::EventListener*>* |
| 252 matching_listeners); |
| 253 |
229 // Decrements the count of event handlers blocking the given request. When the | 254 // Decrements the count of event handlers blocking the given request. When the |
230 // count reaches 0, we stop blocking the request and proceed it using the | 255 // count reaches 0, we stop blocking the request and proceed it using the |
231 // method requested by the extension with the highest precedence. Precedence | 256 // method requested by the extension with the highest precedence. Precedence |
232 // is decided by extension install time. If |response| is non-NULL, this | 257 // is decided by extension install time. If |response| is non-NULL, this |
233 // method assumes ownership. | 258 // method assumes ownership. |
234 void DecrementBlockCount(uint64 request_id, EventResponse* response); | 259 void DecrementBlockCount(uint64 request_id, EventResponse* response); |
235 | 260 |
236 void OnRequestDeleted(net::URLRequest* request); | |
237 | |
238 // Sets the flag that |event_type| has been signaled for |request_id|. | 261 // Sets the flag that |event_type| has been signaled for |request_id|. |
239 // Returns the value of the flag before setting it. | 262 // Returns the value of the flag before setting it. |
240 bool GetAndSetSignaled(uint64 request_id, EventTypes event_type); | 263 bool GetAndSetSignaled(uint64 request_id, EventTypes event_type); |
241 | 264 |
242 // Clears the flag that |event_type| has been signaled for |request_id|. | 265 // Clears the flag that |event_type| has been signaled for |request_id|. |
243 void ClearSignaled(uint64 request_id, EventTypes event_type); | 266 void ClearSignaled(uint64 request_id, EventTypes event_type); |
244 | 267 |
245 // A map for each profile that maps an event name to a set of extensions that | 268 // A map for each profile that maps an event name to a set of extensions that |
246 // are listening to that event. | 269 // are listening to that event. |
247 ListenerMap listeners_; | 270 ListenerMap listeners_; |
248 | 271 |
249 // A map of network requests that are waiting for at least one event handler | 272 // A map of network requests that are waiting for at least one event handler |
250 // to respond. | 273 // to respond. |
251 BlockedRequestMap blocked_requests_; | 274 BlockedRequestMap blocked_requests_; |
252 | 275 |
253 // A map of HTTP(s) network requests. We use this to look up the URLRequest | 276 // A map of HTTP(s) network requests. We use this to look up the URLRequest |
254 // from the request ID given to us for HTTP-specific events. | 277 // from the request ID given to us for HTTP-specific events. |
255 HttpRequestMap http_requests_; | 278 HttpRequestMap http_requests_; |
256 | 279 |
257 // A map of request ids to a bitvector indicating which events have been | 280 // A map of request ids to a bitvector indicating which events have been |
258 // signaled and should not be sent again. | 281 // signaled and should not be sent again. |
259 SignaledRequestMap signaled_requests_; | 282 SignaledRequestMap signaled_requests_; |
260 | 283 |
| 284 // A map of original profile -> corresponding incognito profile (and vice |
| 285 // versa). |
| 286 CrossProfileMap cross_profile_map_; |
| 287 |
261 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); | 288 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); |
262 }; | 289 }; |
263 | 290 |
264 class WebRequestAddEventListener : public SyncExtensionFunction { | 291 class WebRequestAddEventListener : public SyncIOThreadExtensionFunction { |
265 public: | 292 public: |
266 virtual bool RunImpl(); | 293 virtual bool RunImpl(); |
267 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); | 294 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); |
268 }; | 295 }; |
269 | 296 |
270 class WebRequestEventHandled : public SyncExtensionFunction { | 297 class WebRequestEventHandled : public SyncIOThreadExtensionFunction { |
271 public: | 298 public: |
272 virtual bool RunImpl(); | 299 virtual bool RunImpl(); |
273 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); | 300 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); |
274 }; | 301 }; |
275 | 302 |
276 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 303 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
OLD | NEW |