Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(112)

Side by Side Diff: chrome/browser/extensions/api/webrequest/webrequest_api.h

Issue 9701039: Refactor folders in chrome/browser/extensions/api (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: Small change to includes in extension_event_router.cc Created 8 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_EXTENSIONS_API_WEBREQUEST_WEBREQUEST_API_H_
6 #define CHROME_BROWSER_EXTENSIONS_API_WEBREQUEST_WEBREQUEST_API_H_
7 #pragma once
8
9 #include <list>
10 #include <map>
11 #include <set>
12 #include <string>
13 #include <vector>
14
15 #include "base/memory/singleton.h"
16 #include "base/time.h"
17 #include "chrome/browser/extensions/api/webrequest/webrequest_api_helpers.h"
18 #include "chrome/browser/extensions/extension_function.h"
19 #include "chrome/browser/profiles/profile.h"
20 #include "chrome/common/extensions/url_pattern_set.h"
21 #include "ipc/ipc_message.h"
22 #include "net/base/completion_callback.h"
23 #include "net/base/network_delegate.h"
24 #include "net/http/http_request_headers.h"
25 #include "webkit/glue/resource_type.h"
26
27 class ExtensionInfoMap;
28 class ExtensionWebRequestTimeTracker;
29 class GURL;
30
31 namespace base {
32 class DictionaryValue;
33 class ListValue;
34 class StringValue;
35 }
36
37 namespace content {
38 class RenderProcessHost;
39 }
40
41 namespace net {
42 class AuthCredentials;
43 class AuthChallengeInfo;
44 class HttpRequestHeaders;
45 class HttpResponseHeaders;
46 class URLRequest;
47 }
48
49 // This class observes network events and routes them to the appropriate
50 // extensions listening to those events. All methods must be called on the IO
51 // thread unless otherwise specified.
52 class ExtensionWebRequestEventRouter {
53 public:
54 struct BlockedRequest;
55
56 enum EventTypes {
57 kInvalidEvent = 0,
58 kOnBeforeRequest = 1 << 0,
59 kOnBeforeSendHeaders = 1 << 1,
60 kOnSendHeaders = 1 << 2,
61 kOnHeadersReceived = 1 << 3,
62 kOnBeforeRedirect = 1 << 4,
63 kOnAuthRequired = 1 << 5,
64 kOnResponseStarted = 1 << 6,
65 kOnErrorOccurred = 1 << 7,
66 kOnCompleted = 1 << 8,
67 };
68
69 // Internal representation of the webRequest.RequestFilter type, used to
70 // filter what network events an extension cares about.
71 struct RequestFilter {
72 URLPatternSet urls;
73 std::vector<ResourceType::Type> types;
74 int tab_id;
75 int window_id;
76
77 RequestFilter();
78 ~RequestFilter();
79
80 // Returns false if there was an error initializing. If it is a user error,
81 // an error message is provided, otherwise the error is internal (and
82 // unexpected).
83 bool InitFromValue(const base::DictionaryValue& value, std::string* error);
84 };
85
86 // Internal representation of the extraInfoSpec parameter on webRequest
87 // events, used to specify extra information to be included with network
88 // events.
89 struct ExtraInfoSpec {
90 enum Flags {
91 REQUEST_HEADERS = 1<<0,
92 RESPONSE_HEADERS = 1<<1,
93 BLOCKING = 1<<2,
94 ASYNC_BLOCKING = 1<<3,
95 };
96
97 static bool InitFromValue(const base::ListValue& value,
98 int* extra_info_spec);
99 };
100
101 // Contains an extension's response to a blocking event.
102 struct EventResponse {
103 // ID of the extension that sent this response.
104 std::string extension_id;
105
106 // The time that the extension was installed. Used for deciding order of
107 // precedence in case multiple extensions respond with conflicting
108 // decisions.
109 base::Time extension_install_time;
110
111 // Response values. These are mutually exclusive.
112 bool cancel;
113 GURL new_url;
114 scoped_ptr<net::HttpRequestHeaders> request_headers;
115 scoped_ptr<extension_webrequest_api_helpers::ResponseHeaders>
116 response_headers;
117
118 scoped_ptr<net::AuthCredentials> auth_credentials;
119
120 EventResponse(const std::string& extension_id,
121 const base::Time& extension_install_time);
122 ~EventResponse();
123
124 DISALLOW_COPY_AND_ASSIGN(EventResponse);
125 };
126
127 static ExtensionWebRequestEventRouter* GetInstance();
128
129 // Dispatches the OnBeforeRequest event to any extensions whose filters match
130 // the given request. Returns net::ERR_IO_PENDING if an extension is
131 // intercepting the request, OK otherwise.
132 int OnBeforeRequest(void* profile,
133 ExtensionInfoMap* extension_info_map,
134 net::URLRequest* request,
135 const net::CompletionCallback& callback,
136 GURL* new_url);
137
138 // Dispatches the onBeforeSendHeaders event. This is fired for HTTP(s)
139 // requests only, and allows modification of the outgoing request headers.
140 // Returns net::ERR_IO_PENDING if an extension is intercepting the request, OK
141 // otherwise.
142 int OnBeforeSendHeaders(void* profile,
143 ExtensionInfoMap* extension_info_map,
144 net::URLRequest* request,
145 const net::CompletionCallback& callback,
146 net::HttpRequestHeaders* headers);
147
148 // Dispatches the onSendHeaders event. This is fired for HTTP(s) requests
149 // only.
150 void OnSendHeaders(void* profile,
151 ExtensionInfoMap* extension_info_map,
152 net::URLRequest* request,
153 const net::HttpRequestHeaders& headers);
154
155 // Dispatches the onHeadersReceived event. This is fired for HTTP(s)
156 // requests only, and allows modification of incoming response headers.
157 // Returns net::ERR_IO_PENDING if an extension is intercepting the request,
158 // OK otherwise. |original_response_headers| is reference counted. |callback|
159 // and |override_response_headers| are owned by a URLRequestJob. They are
160 // guaranteed to be valid until |callback| is called or OnURLRequestDestroyed
161 // is called (whatever comes first).
162 // Do not modify |original_response_headers| directly but write new ones
163 // into |override_response_headers|.
164 int OnHeadersReceived(
165 void* profile,
166 ExtensionInfoMap* extension_info_map,
167 net::URLRequest* request,
168 const net::CompletionCallback& callback,
169 net::HttpResponseHeaders* original_response_headers,
170 scoped_refptr<net::HttpResponseHeaders>* override_response_headers);
171
172 // Dispatches the OnAuthRequired event to any extensions whose filters match
173 // the given request. If the listener is not registered as "blocking", then
174 // AUTH_REQUIRED_RESPONSE_OK is returned. Otherwise,
175 // AUTH_REQUIRED_RESPONSE_IO_PENDING is returned and |callback| will be
176 // invoked later.
177 net::NetworkDelegate::AuthRequiredResponse OnAuthRequired(
178 void* profile,
179 ExtensionInfoMap* extension_info_map,
180 net::URLRequest* request,
181 const net::AuthChallengeInfo& auth_info,
182 const net::NetworkDelegate::AuthCallback& callback,
183 net::AuthCredentials* credentials);
184
185 // Dispatches the onBeforeRedirect event. This is fired for HTTP(s) requests
186 // only.
187 void OnBeforeRedirect(void* profile,
188 ExtensionInfoMap* extension_info_map,
189 net::URLRequest* request,
190 const GURL& new_location);
191
192 // Dispatches the onResponseStarted event indicating that the first bytes of
193 // the response have arrived.
194 void OnResponseStarted(void* profile,
195 ExtensionInfoMap* extension_info_map,
196 net::URLRequest* request);
197
198 // Dispatches the onComplete event.
199 void OnCompleted(void* profile,
200 ExtensionInfoMap* extension_info_map,
201 net::URLRequest* request);
202
203 // Dispatches an onErrorOccurred event.
204 void OnErrorOccurred(void* profile,
205 ExtensionInfoMap* extension_info_map,
206 net::URLRequest* request,
207 bool started);
208
209 // Notifications when objects are going away.
210 void OnURLRequestDestroyed(void* profile, net::URLRequest* request);
211
212 // Called when an event listener handles a blocking event and responds.
213 void OnEventHandled(
214 void* profile,
215 const std::string& extension_id,
216 const std::string& event_name,
217 const std::string& sub_event_name,
218 uint64 request_id,
219 EventResponse* response);
220
221 // Adds a listener to the given event. |event_name| specifies the event being
222 // listened to. |sub_event_name| is an internal event uniquely generated in
223 // the extension process to correspond to the given filter and
224 // extra_info_spec.
225 void AddEventListener(
226 void* profile,
227 const std::string& extension_id,
228 const std::string& extension_name,
229 const std::string& event_name,
230 const std::string& sub_event_name,
231 const RequestFilter& filter,
232 int extra_info_spec,
233 base::WeakPtr<IPC::Message::Sender> ipc_sender);
234
235 // Removes the listener for the given sub-event.
236 void RemoveEventListener(
237 void* profile,
238 const std::string& extension_id,
239 const std::string& sub_event_name);
240
241 // Called when an incognito profile is created or destroyed.
242 void OnOTRProfileCreated(void* original_profile,
243 void* otr_profile);
244 void OnOTRProfileDestroyed(void* original_profile,
245 void* otr_profile);
246
247 // Registers a |callback| that is executed when the next page load happens.
248 // The callback is then deleted.
249 void AddCallbackForPageLoad(const base::Closure& callback);
250
251 private:
252 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>;
253 struct EventListener;
254 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile;
255 typedef std::map<void*, ListenerMapForProfile> ListenerMap;
256 typedef std::map<uint64, BlockedRequest> BlockedRequestMap;
257 // Map of request_id -> bit vector of EventTypes already signaled
258 typedef std::map<uint64, int> SignaledRequestMap;
259 typedef std::map<void*, void*> CrossProfileMap;
260 typedef std::list<base::Closure> CallbacksForPageLoad;
261
262 ExtensionWebRequestEventRouter();
263 ~ExtensionWebRequestEventRouter();
264
265 // Ensures that future callbacks for |request| are ignored so that it can be
266 // destroyed safely.
267 void ClearPendingCallbacks(net::URLRequest* request);
268
269 bool DispatchEvent(
270 void* profile,
271 net::URLRequest* request,
272 const std::vector<const EventListener*>& listeners,
273 const base::ListValue& args);
274
275 // Returns a list of event listeners that care about the given event, based
276 // on their filter parameters. |extra_info_spec| will contain the combined
277 // set of extra_info_spec flags that every matching listener asked for.
278 std::vector<const EventListener*> GetMatchingListeners(
279 void* profile,
280 ExtensionInfoMap* extension_info_map,
281 const std::string& event_name,
282 net::URLRequest* request,
283 int* extra_info_spec);
284
285 // Helper for the above functions. This is called twice: once for the profile
286 // of the event, the next time for the "cross" profile (i.e. the incognito
287 // profile if the event is originally for the normal profile, or vice versa).
288 void GetMatchingListenersImpl(
289 void* profile,
290 ExtensionInfoMap* extension_info_map,
291 bool crosses_incognito,
292 const std::string& event_name,
293 const GURL& url,
294 int tab_id,
295 int window_id,
296 ResourceType::Type resource_type,
297 bool is_request_from_extension,
298 int* extra_info_spec,
299 std::vector<const ExtensionWebRequestEventRouter::EventListener*>*
300 matching_listeners);
301
302 // Decrements the count of event handlers blocking the given request. When the
303 // count reaches 0, we stop blocking the request and proceed it using the
304 // method requested by the extension with the highest precedence. Precedence
305 // is decided by extension install time. If |response| is non-NULL, this
306 // method assumes ownership.
307 void DecrementBlockCount(
308 void* profile,
309 const std::string& extension_id,
310 const std::string& event_name,
311 uint64 request_id,
312 EventResponse* response);
313
314 // Sets the flag that |event_type| has been signaled for |request_id|.
315 // Returns the value of the flag before setting it.
316 bool GetAndSetSignaled(uint64 request_id, EventTypes event_type);
317
318 // Clears the flag that |event_type| has been signaled for |request_id|.
319 void ClearSignaled(uint64 request_id, EventTypes event_type);
320
321 // Returns whether |request| represents a top level window navigation.
322 bool IsPageLoad(net::URLRequest* request) const;
323
324 // Called on a page load to process all registered callbacks.
325 void NotifyPageLoad();
326
327 // A map for each profile that maps an event name to a set of extensions that
328 // are listening to that event.
329 ListenerMap listeners_;
330
331 // A map of network requests that are waiting for at least one event handler
332 // to respond.
333 BlockedRequestMap blocked_requests_;
334
335 // A map of request ids to a bitvector indicating which events have been
336 // signaled and should not be sent again.
337 SignaledRequestMap signaled_requests_;
338
339 // A map of original profile -> corresponding incognito profile (and vice
340 // versa).
341 CrossProfileMap cross_profile_map_;
342
343 // Keeps track of time spent waiting on extensions using the blocking
344 // webRequest API.
345 scoped_ptr<ExtensionWebRequestTimeTracker> request_time_tracker_;
346
347 CallbacksForPageLoad callbacks_for_page_load_;
348
349 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter);
350 };
351
352 class WebRequestAddEventListener : public SyncIOThreadExtensionFunction {
353 public:
354 virtual bool RunImpl() OVERRIDE;
355 DECLARE_EXTENSION_FUNCTION_NAME("webRequest.addEventListener");
356 };
357
358 class WebRequestEventHandled : public SyncIOThreadExtensionFunction {
359 public:
360 virtual bool RunImpl() OVERRIDE;
361 DECLARE_EXTENSION_FUNCTION_NAME("webRequest.eventHandled");
362 };
363
364 class WebRequestHandlerBehaviorChanged : public SyncIOThreadExtensionFunction {
365 public:
366 virtual bool RunImpl() OVERRIDE;
367 DECLARE_EXTENSION_FUNCTION_NAME(
368 "webRequest.handlerBehaviorChanged");
369
370 private:
371 virtual void GetQuotaLimitHeuristics(
372 QuotaLimitHeuristics* heuristics) const OVERRIDE;
373 // Handle quota exceeded gracefully: Only warn the user but still execute the
374 // function.
375 virtual void OnQuotaExceeded() OVERRIDE;
376 };
377
378 // Send updates to |host| with information about what webRequest-related
379 // extensions are installed.
380 // TODO(mpcomplete): remove. http://crbug.com/100411
381 void SendExtensionWebRequestStatusToHost(content::RenderProcessHost* host);
382
383 #endif // CHROME_BROWSER_EXTENSIONS_API_WEBREQUEST_WEBREQUEST_API_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698