OLD | NEW |
---|---|
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 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 EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ | 5 #ifndef EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ |
6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ | 6 #define EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <list> | 10 #include <list> |
11 #include <map> | 11 #include <map> |
12 #include <set> | 12 #include <set> |
13 #include <string> | 13 #include <string> |
14 #include <utility> | 14 #include <utility> |
15 #include <vector> | 15 #include <vector> |
16 | 16 |
17 #include "base/gtest_prod_util.h" | |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
18 #include "base/memory/singleton.h" | 19 #include "base/memory/singleton.h" |
19 #include "base/memory/weak_ptr.h" | 20 #include "base/memory/weak_ptr.h" |
20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
21 #include "base/time/time.h" | 22 #include "base/time/time.h" |
22 #include "content/public/common/resource_type.h" | 23 #include "content/public/common/resource_type.h" |
23 #include "extensions/browser/api/declarative/rules_registry.h" | 24 #include "extensions/browser/api/declarative/rules_registry.h" |
24 #include "extensions/browser/api/declarative_webrequest/request_stage.h" | 25 #include "extensions/browser/api/declarative_webrequest/request_stage.h" |
25 #include "extensions/browser/api/web_request/web_request_api_helpers.h" | 26 #include "extensions/browser/api/web_request/web_request_api_helpers.h" |
26 #include "extensions/browser/api/web_request/web_request_permissions.h" | 27 #include "extensions/browser/api/web_request/web_request_permissions.h" |
(...skipping 259 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
286 const std::string& extension_name, | 287 const std::string& extension_name, |
287 events::HistogramValue histogram_value, | 288 events::HistogramValue histogram_value, |
288 const std::string& event_name, | 289 const std::string& event_name, |
289 const std::string& sub_event_name, | 290 const std::string& sub_event_name, |
290 const RequestFilter& filter, | 291 const RequestFilter& filter, |
291 int extra_info_spec, | 292 int extra_info_spec, |
292 int embedder_process_id, | 293 int embedder_process_id, |
293 int web_view_instance_id, | 294 int web_view_instance_id, |
294 base::WeakPtr<IPC::Sender> ipc_sender); | 295 base::WeakPtr<IPC::Sender> ipc_sender); |
295 | 296 |
296 // Removes the listener for the given sub-event. | |
297 void RemoveEventListener( | |
298 void* browser_context, | |
299 const std::string& extension_id, | |
300 const std::string& sub_event_name, | |
301 int embedder_process_id, | |
302 int web_view_instance_id); | |
303 | |
304 // Removes the listeners for a given <webview>. | 297 // Removes the listeners for a given <webview>. |
305 void RemoveWebViewEventListeners( | 298 void RemoveWebViewEventListeners( |
306 void* browser_context, | 299 void* browser_context, |
307 int embedder_process_id, | 300 int embedder_process_id, |
308 int web_view_instance_id); | 301 int web_view_instance_id); |
309 | 302 |
310 // Called when an incognito browser_context is created or destroyed. | 303 // Called when an incognito browser_context is created or destroyed. |
311 void OnOTRBrowserContextCreated(void* original_browser_context, | 304 void OnOTRBrowserContextCreated(void* original_browser_context, |
312 void* otr_browser_context); | 305 void* otr_browser_context); |
313 void OnOTRBrowserContextDestroyed(void* original_browser_context, | 306 void OnOTRBrowserContextDestroyed(void* original_browser_context, |
314 void* otr_browser_context); | 307 void* otr_browser_context); |
315 | 308 |
316 // Registers a |callback| that is executed when the next page load happens. | 309 // Registers a |callback| that is executed when the next page load happens. |
317 // The callback is then deleted. | 310 // The callback is then deleted. |
318 void AddCallbackForPageLoad(const base::Closure& callback); | 311 void AddCallbackForPageLoad(const base::Closure& callback); |
319 | 312 |
320 private: | 313 private: |
314 friend class WebRequestAPI; | |
315 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, | |
316 BlockingEventPrecedenceRedirect); | |
317 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, | |
318 BlockingEventPrecedenceCancel); | |
319 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, | |
320 SimulateChancelWhileBlocked); | |
321 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, AccessRequestBodyData); | |
322 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, | |
323 MinimalAccessRequestBodyData); | |
324 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, NoAccessRequestBodyData); | |
325 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, AddAndRemoveListeners); | |
326 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, BlockedRequestsAreRemoved); | |
327 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestHeaderModificationTest, | |
328 TestModifications); | |
329 | |
330 struct EventListener { | |
331 // An EventListener is uniquely defined by five properties. | |
332 struct Identifier { | |
Devlin
2016/09/08 17:33:12
nit: s/Identifier/ID?
erikchen
2016/09/08 18:34:42
Done.
| |
333 Identifier(void* browser_context, | |
334 const std::string& extension_id, | |
335 const std::string& sub_event_name, | |
336 int embedder_process_id, | |
337 int web_view_instance_id); | |
338 | |
339 // If web_view_instance_id is 0, then ignore embedder_process_id. | |
340 bool LooselyMatches(const Identifier& that) const; | |
341 | |
342 bool operator==(const Identifier& that) const; | |
343 void* browser_context; | |
344 std::string extension_id; | |
345 std::string sub_event_name; | |
346 int embedder_process_id; | |
347 int web_view_instance_id; | |
348 }; | |
349 | |
350 EventListener(Identifier id); | |
351 ~EventListener(); | |
352 | |
353 const Identifier id; | |
354 std::string extension_name; | |
355 events::HistogramValue histogram_value = events::UNKNOWN; | |
356 RequestFilter filter; | |
357 int extra_info_spec = 0; | |
358 base::WeakPtr<IPC::Sender> ipc_sender; | |
359 std::unordered_set<uint64_t> blocked_requests; | |
360 | |
361 private: | |
362 DISALLOW_COPY_AND_ASSIGN(EventListener); | |
363 }; | |
364 | |
321 friend struct base::DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 365 friend struct base::DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
322 | 366 |
323 struct EventListener; | 367 using ListenerPointers = std::vector<EventListener*>; |
Devlin
2016/09/08 17:33:12
s/ListenerPointers/WeakListeners?
erikchen
2016/09/08 18:34:42
I used RawListeners, since weak pointers have diff
| |
324 using EventListeners = std::vector<const EventListener*>; | 368 using ListenerIdentifiers = std::vector<EventListener::Identifier>; |
325 using ListenerMapForBrowserContext = | 369 using Listeners = std::vector<std::unique_ptr<EventListener>>; |
326 std::map<std::string, std::set<EventListener>>; | 370 using ListenerMapForBrowserContext = std::map<std::string, Listeners>; |
327 using ListenerMap = std::map<void*, ListenerMapForBrowserContext>; | 371 using ListenerMap = std::map<void*, ListenerMapForBrowserContext>; |
328 using BlockedRequestMap = std::map<uint64_t, BlockedRequest>; | 372 using BlockedRequestMap = std::map<uint64_t, BlockedRequest>; |
329 // Map of request_id -> bit vector of EventTypes already signaled | 373 // Map of request_id -> bit vector of EventTypes already signaled |
330 using SignaledRequestMap = std::map<uint64_t, int>; | 374 using SignaledRequestMap = std::map<uint64_t, int>; |
331 // For each browser_context: a bool indicating whether it is an incognito | 375 // For each browser_context: a bool indicating whether it is an incognito |
332 // browser_context, and a pointer to the corresponding (non-)incognito | 376 // browser_context, and a pointer to the corresponding (non-)incognito |
333 // browser_context. | 377 // browser_context. |
334 using CrossBrowserContextMap = std::map<void*, std::pair<bool, void*>>; | 378 using CrossBrowserContextMap = std::map<void*, std::pair<bool, void*>>; |
335 using CallbacksForPageLoad = std::list<base::Closure>; | 379 using CallbacksForPageLoad = std::list<base::Closure>; |
336 | 380 |
337 ExtensionWebRequestEventRouter(); | 381 ExtensionWebRequestEventRouter(); |
338 ~ExtensionWebRequestEventRouter(); | 382 ~ExtensionWebRequestEventRouter(); |
339 | 383 |
384 // Returns the EventListener with the given |id|, or nullptr. Must be called | |
385 // from the IO thread. | |
386 EventListener* FindEventListener(const EventListener::Identifier& id); | |
387 | |
388 // Returns the EventListener with the given |id| from |listeners|. | |
389 EventListener* FindEventListenerInContainer( | |
390 const EventListener::Identifier& id, | |
391 Listeners& listeners); | |
392 | |
393 // Removes the listener for the given sub-event. Must be called from the IO | |
394 // thread. | |
395 void RemoveEventListener(const EventListener::Identifier& id); | |
396 | |
340 // Ensures that future callbacks for |request| are ignored so that it can be | 397 // Ensures that future callbacks for |request| are ignored so that it can be |
341 // destroyed safely. | 398 // destroyed safely. |
342 void ClearPendingCallbacks(const net::URLRequest* request); | 399 void ClearPendingCallbacks(const net::URLRequest* request); |
343 | 400 |
344 bool DispatchEvent(void* browser_context, | 401 bool DispatchEvent(void* browser_context, |
345 net::URLRequest* request, | 402 net::URLRequest* request, |
346 const std::vector<const EventListener*>& listeners, | 403 const ListenerPointers& listener_ids, |
347 std::unique_ptr<WebRequestEventDetails> event_details); | 404 std::unique_ptr<WebRequestEventDetails> event_details); |
348 | 405 |
349 void DispatchEventToListeners( | 406 void DispatchEventToListeners( |
350 void* browser_context, | 407 void* browser_context, |
351 std::unique_ptr<std::vector<EventListener>> listeners, | 408 std::unique_ptr<ListenerIdentifiers> listener_ids, |
352 std::unique_ptr<WebRequestEventDetails> event_details); | 409 std::unique_ptr<WebRequestEventDetails> event_details); |
353 | 410 |
354 // Returns a list of event listeners that care about the given event, based | 411 // Returns a list of event listeners that care about the given event, based |
355 // on their filter parameters. |extra_info_spec| will contain the combined | 412 // on their filter parameters. |extra_info_spec| will contain the combined |
356 // set of extra_info_spec flags that every matching listener asked for. | 413 // set of extra_info_spec flags that every matching listener asked for. |
357 std::vector<const EventListener*> GetMatchingListeners( | 414 ListenerPointers GetMatchingListeners( |
358 void* browser_context, | 415 void* browser_context, |
359 const extensions::InfoMap* extension_info_map, | 416 const extensions::InfoMap* extension_info_map, |
360 const std::string& event_name, | 417 const std::string& event_name, |
361 const net::URLRequest* request, | 418 const net::URLRequest* request, |
362 int* extra_info_spec); | 419 int* extra_info_spec); |
363 | 420 |
364 // Helper for the above functions. This is called twice: once for the | 421 // Helper for the above functions. This is called twice: once for the |
365 // browser_context of the event, the next time for the "cross" browser_context | 422 // browser_context of the event, the next time for the "cross" browser_context |
366 // (i.e. the incognito browser_context if the event is originally for the | 423 // (i.e. the incognito browser_context if the event is originally for the |
367 // normal browser_context, or vice versa). | 424 // normal browser_context, or vice versa). |
368 void GetMatchingListenersImpl( | 425 void GetMatchingListenersImpl(void* browser_context, |
369 void* browser_context, | 426 const net::URLRequest* request, |
370 const net::URLRequest* request, | 427 const extensions::InfoMap* extension_info_map, |
371 const extensions::InfoMap* extension_info_map, | 428 bool crosses_incognito, |
372 bool crosses_incognito, | 429 const std::string& event_name, |
373 const std::string& event_name, | 430 const GURL& url, |
374 const GURL& url, | 431 int render_process_host_id, |
375 int render_process_host_id, | 432 int routing_id, |
376 int routing_id, | 433 content::ResourceType resource_type, |
377 content::ResourceType resource_type, | 434 bool is_async_request, |
378 bool is_async_request, | 435 bool is_request_from_extension, |
379 bool is_request_from_extension, | 436 int* extra_info_spec, |
380 int* extra_info_spec, | 437 ListenerPointers* matching_listeners); |
381 std::vector<const EventListener*>* matching_listeners); | |
382 | 438 |
383 // Decrements the count of event handlers blocking the given request. When the | 439 // Decrements the count of event handlers blocking the given request. When the |
384 // count reaches 0, we stop blocking the request and proceed it using the | 440 // count reaches 0, we stop blocking the request and proceed it using the |
385 // method requested by the extension with the highest precedence. Precedence | 441 // method requested by the extension with the highest precedence. Precedence |
386 // is decided by extension install time. If |response| is non-NULL, this | 442 // is decided by extension install time. If |response| is non-NULL, this |
387 // method assumes ownership. | 443 // method assumes ownership. |
388 void DecrementBlockCount(void* browser_context, | 444 void DecrementBlockCount(void* browser_context, |
389 const std::string& extension_id, | 445 const std::string& extension_id, |
390 const std::string& event_name, | 446 const std::string& event_name, |
391 uint64_t request_id, | 447 uint64_t request_id, |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
450 void* GetCrossBrowserContext(void* browser_context) const; | 506 void* GetCrossBrowserContext(void* browser_context) const; |
451 | 507 |
452 // Determines whether the specified browser_context is an incognito | 508 // Determines whether the specified browser_context is an incognito |
453 // browser_context (based on the contents of the cross-browser_context table | 509 // browser_context (based on the contents of the cross-browser_context table |
454 // and without dereferencing the browser_context pointer). | 510 // and without dereferencing the browser_context pointer). |
455 bool IsIncognitoBrowserContext(void* browser_context) const; | 511 bool IsIncognitoBrowserContext(void* browser_context) const; |
456 | 512 |
457 // Returns true if |request| was already signaled to some event handlers. | 513 // Returns true if |request| was already signaled to some event handlers. |
458 bool WasSignaled(const net::URLRequest& request) const; | 514 bool WasSignaled(const net::URLRequest& request) const; |
459 | 515 |
516 // Get the number of listeners - for testing only. | |
517 size_t GetListenerCountForTesting(void* browser_context, | |
518 const std::string& event_name); | |
519 | |
460 // A map for each browser_context that maps an event name to a set of | 520 // A map for each browser_context that maps an event name to a set of |
461 // extensions that are listening to that event. | 521 // extensions that are listening to that event. |
462 ListenerMap listeners_; | 522 ListenerMap listeners_; |
463 | 523 |
464 // A map of network requests that are waiting for at least one event handler | 524 // A map of network requests that are waiting for at least one event handler |
465 // to respond. | 525 // to respond. |
466 BlockedRequestMap blocked_requests_; | 526 BlockedRequestMap blocked_requests_; |
467 | 527 |
468 // A map of request ids to a bitvector indicating which events have been | 528 // A map of request ids to a bitvector indicating which events have been |
469 // signaled and should not be sent again. | 529 // signaled and should not be sent again. |
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
553 extensions::QuotaLimitHeuristics* heuristics) const override; | 613 extensions::QuotaLimitHeuristics* heuristics) const override; |
554 // Handle quota exceeded gracefully: Only warn the user but still execute the | 614 // Handle quota exceeded gracefully: Only warn the user but still execute the |
555 // function. | 615 // function. |
556 void OnQuotaExceeded(const std::string& error) override; | 616 void OnQuotaExceeded(const std::string& error) override; |
557 ResponseAction Run() override; | 617 ResponseAction Run() override; |
558 }; | 618 }; |
559 | 619 |
560 } // namespace extensions | 620 } // namespace extensions |
561 | 621 |
562 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ | 622 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ |
OLD | NEW |