| 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 240 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 267 const std::string& extension_name, | 268 const std::string& extension_name, |
| 268 events::HistogramValue histogram_value, | 269 events::HistogramValue histogram_value, |
| 269 const std::string& event_name, | 270 const std::string& event_name, |
| 270 const std::string& sub_event_name, | 271 const std::string& sub_event_name, |
| 271 const RequestFilter& filter, | 272 const RequestFilter& filter, |
| 272 int extra_info_spec, | 273 int extra_info_spec, |
| 273 int embedder_process_id, | 274 int embedder_process_id, |
| 274 int web_view_instance_id, | 275 int web_view_instance_id, |
| 275 base::WeakPtr<IPC::Sender> ipc_sender); | 276 base::WeakPtr<IPC::Sender> ipc_sender); |
| 276 | 277 |
| 277 // Removes the listener for the given sub-event. | |
| 278 void RemoveEventListener( | |
| 279 void* browser_context, | |
| 280 const std::string& extension_id, | |
| 281 const std::string& sub_event_name, | |
| 282 int embedder_process_id, | |
| 283 int web_view_instance_id); | |
| 284 | |
| 285 // Removes the listeners for a given <webview>. | 278 // Removes the listeners for a given <webview>. |
| 286 void RemoveWebViewEventListeners( | 279 void RemoveWebViewEventListeners( |
| 287 void* browser_context, | 280 void* browser_context, |
| 288 int embedder_process_id, | 281 int embedder_process_id, |
| 289 int web_view_instance_id); | 282 int web_view_instance_id); |
| 290 | 283 |
| 291 // Called when an incognito browser_context is created or destroyed. | 284 // Called when an incognito browser_context is created or destroyed. |
| 292 void OnOTRBrowserContextCreated(void* original_browser_context, | 285 void OnOTRBrowserContextCreated(void* original_browser_context, |
| 293 void* otr_browser_context); | 286 void* otr_browser_context); |
| 294 void OnOTRBrowserContextDestroyed(void* original_browser_context, | 287 void OnOTRBrowserContextDestroyed(void* original_browser_context, |
| 295 void* otr_browser_context); | 288 void* otr_browser_context); |
| 296 | 289 |
| 297 // Registers a |callback| that is executed when the next page load happens. | 290 // Registers a |callback| that is executed when the next page load happens. |
| 298 // The callback is then deleted. | 291 // The callback is then deleted. |
| 299 void AddCallbackForPageLoad(const base::Closure& callback); | 292 void AddCallbackForPageLoad(const base::Closure& callback); |
| 300 | 293 |
| 301 private: | 294 private: |
| 295 friend class WebRequestAPI; |
| 296 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, |
| 297 BlockingEventPrecedenceRedirect); |
| 298 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, |
| 299 BlockingEventPrecedenceCancel); |
| 300 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, |
| 301 SimulateChancelWhileBlocked); |
| 302 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, AccessRequestBodyData); |
| 303 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, |
| 304 MinimalAccessRequestBodyData); |
| 305 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, NoAccessRequestBodyData); |
| 306 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, AddAndRemoveListeners); |
| 307 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestTest, BlockedRequestsAreRemoved); |
| 308 FRIEND_TEST_ALL_PREFIXES(ExtensionWebRequestHeaderModificationTest, |
| 309 TestModifications); |
| 310 |
| 311 struct EventListener { |
| 312 // An EventListener is uniquely defined by five properties. |
| 313 // TODO(rdevlin.cronin): There are two types of EventListeners - those |
| 314 // associated with WebViews and those that are not. The ones associated with |
| 315 // WebViews are always identified by all five properties. The other ones |
| 316 // will always have web_view_instance_id = 0. Unfortunately, the |
| 317 // callbacks/interfaces for these ones don't specify embedder_process_id. |
| 318 // This is why we need the LooselyMatches method, and the need for a |
| 319 // |strict| argument on RemoveEventListener. |
| 320 struct ID { |
| 321 ID(void* browser_context, |
| 322 const std::string& extension_id, |
| 323 const std::string& sub_event_name, |
| 324 int embedder_process_id, |
| 325 int web_view_instance_id); |
| 326 |
| 327 // If web_view_instance_id is 0, then ignore embedder_process_id. |
| 328 // TODO(rdevlin.cronin): In a more sane world, LooselyMatches wouldn't be |
| 329 // necessary. |
| 330 bool LooselyMatches(const ID& that) const; |
| 331 |
| 332 bool operator==(const ID& that) const; |
| 333 void* browser_context; |
| 334 std::string extension_id; |
| 335 std::string sub_event_name; |
| 336 int embedder_process_id; |
| 337 int web_view_instance_id; |
| 338 }; |
| 339 |
| 340 EventListener(ID id); |
| 341 ~EventListener(); |
| 342 |
| 343 const ID id; |
| 344 std::string extension_name; |
| 345 events::HistogramValue histogram_value = events::UNKNOWN; |
| 346 RequestFilter filter; |
| 347 int extra_info_spec = 0; |
| 348 base::WeakPtr<IPC::Sender> ipc_sender; |
| 349 std::unordered_set<uint64_t> blocked_requests; |
| 350 |
| 351 private: |
| 352 DISALLOW_COPY_AND_ASSIGN(EventListener); |
| 353 }; |
| 354 |
| 302 friend struct base::DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 355 friend struct base::DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
| 303 | 356 |
| 304 struct EventListener; | 357 using RawListeners = std::vector<EventListener*>; |
| 305 using EventListeners = std::vector<const EventListener*>; | 358 using ListenerIDs = std::vector<EventListener::ID>; |
| 306 using ListenerMapForBrowserContext = | 359 using Listeners = std::vector<std::unique_ptr<EventListener>>; |
| 307 std::map<std::string, std::set<EventListener>>; | 360 using ListenerMapForBrowserContext = std::map<std::string, Listeners>; |
| 308 using ListenerMap = std::map<void*, ListenerMapForBrowserContext>; | 361 using ListenerMap = std::map<void*, ListenerMapForBrowserContext>; |
| 309 using BlockedRequestMap = std::map<uint64_t, BlockedRequest>; | 362 using BlockedRequestMap = std::map<uint64_t, BlockedRequest>; |
| 310 // Map of request_id -> bit vector of EventTypes already signaled | 363 // Map of request_id -> bit vector of EventTypes already signaled |
| 311 using SignaledRequestMap = std::map<uint64_t, int>; | 364 using SignaledRequestMap = std::map<uint64_t, int>; |
| 312 // For each browser_context: a bool indicating whether it is an incognito | 365 // For each browser_context: a bool indicating whether it is an incognito |
| 313 // browser_context, and a pointer to the corresponding (non-)incognito | 366 // browser_context, and a pointer to the corresponding (non-)incognito |
| 314 // browser_context. | 367 // browser_context. |
| 315 using CrossBrowserContextMap = std::map<void*, std::pair<bool, void*>>; | 368 using CrossBrowserContextMap = std::map<void*, std::pair<bool, void*>>; |
| 316 using CallbacksForPageLoad = std::list<base::Closure>; | 369 using CallbacksForPageLoad = std::list<base::Closure>; |
| 317 | 370 |
| 318 ExtensionWebRequestEventRouter(); | 371 ExtensionWebRequestEventRouter(); |
| 319 ~ExtensionWebRequestEventRouter(); | 372 ~ExtensionWebRequestEventRouter(); |
| 320 | 373 |
| 374 // Returns the EventListener with the given |id|, or nullptr. Must be called |
| 375 // from the IO thread. |
| 376 EventListener* FindEventListener(const EventListener::ID& id); |
| 377 |
| 378 // Returns the EventListener with the given |id| from |listeners|. |
| 379 EventListener* FindEventListenerInContainer(const EventListener::ID& id, |
| 380 Listeners& listeners); |
| 381 |
| 382 // Removes the listener for the given sub-event. Must be called from the IO |
| 383 // thread. |
| 384 void RemoveEventListener(const EventListener::ID& id, bool strict); |
| 385 |
| 321 // Ensures that future callbacks for |request| are ignored so that it can be | 386 // Ensures that future callbacks for |request| are ignored so that it can be |
| 322 // destroyed safely. | 387 // destroyed safely. |
| 323 void ClearPendingCallbacks(const net::URLRequest* request); | 388 void ClearPendingCallbacks(const net::URLRequest* request); |
| 324 | 389 |
| 325 bool DispatchEvent(void* browser_context, | 390 bool DispatchEvent(void* browser_context, |
| 326 net::URLRequest* request, | 391 net::URLRequest* request, |
| 327 const std::vector<const EventListener*>& listeners, | 392 const RawListeners& listener_ids, |
| 328 std::unique_ptr<WebRequestEventDetails> event_details); | 393 std::unique_ptr<WebRequestEventDetails> event_details); |
| 329 | 394 |
| 330 void DispatchEventToListeners( | 395 void DispatchEventToListeners( |
| 331 void* browser_context, | 396 void* browser_context, |
| 332 std::unique_ptr<std::vector<EventListener>> listeners, | 397 std::unique_ptr<ListenerIDs> listener_ids, |
| 333 std::unique_ptr<WebRequestEventDetails> event_details); | 398 std::unique_ptr<WebRequestEventDetails> event_details); |
| 334 | 399 |
| 335 // Returns a list of event listeners that care about the given event, based | 400 // Returns a list of event listeners that care about the given event, based |
| 336 // on their filter parameters. |extra_info_spec| will contain the combined | 401 // on their filter parameters. |extra_info_spec| will contain the combined |
| 337 // set of extra_info_spec flags that every matching listener asked for. | 402 // set of extra_info_spec flags that every matching listener asked for. |
| 338 std::vector<const EventListener*> GetMatchingListeners( | 403 RawListeners GetMatchingListeners( |
| 339 void* browser_context, | 404 void* browser_context, |
| 340 const extensions::InfoMap* extension_info_map, | 405 const extensions::InfoMap* extension_info_map, |
| 341 const std::string& event_name, | 406 const std::string& event_name, |
| 342 const net::URLRequest* request, | 407 const net::URLRequest* request, |
| 343 int* extra_info_spec); | 408 int* extra_info_spec); |
| 344 | 409 |
| 345 // Helper for the above functions. This is called twice: once for the | 410 // Helper for the above functions. This is called twice: once for the |
| 346 // browser_context of the event, the next time for the "cross" browser_context | 411 // browser_context of the event, the next time for the "cross" browser_context |
| 347 // (i.e. the incognito browser_context if the event is originally for the | 412 // (i.e. the incognito browser_context if the event is originally for the |
| 348 // normal browser_context, or vice versa). | 413 // normal browser_context, or vice versa). |
| 349 void GetMatchingListenersImpl( | 414 void GetMatchingListenersImpl(void* browser_context, |
| 350 void* browser_context, | 415 const net::URLRequest* request, |
| 351 const net::URLRequest* request, | 416 const extensions::InfoMap* extension_info_map, |
| 352 const extensions::InfoMap* extension_info_map, | 417 bool crosses_incognito, |
| 353 bool crosses_incognito, | 418 const std::string& event_name, |
| 354 const std::string& event_name, | 419 const GURL& url, |
| 355 const GURL& url, | 420 int render_process_host_id, |
| 356 int render_process_host_id, | 421 int routing_id, |
| 357 int routing_id, | 422 content::ResourceType resource_type, |
| 358 content::ResourceType resource_type, | 423 bool is_async_request, |
| 359 bool is_async_request, | 424 bool is_request_from_extension, |
| 360 bool is_request_from_extension, | 425 int* extra_info_spec, |
| 361 int* extra_info_spec, | 426 RawListeners* matching_listeners); |
| 362 std::vector<const EventListener*>* matching_listeners); | |
| 363 | 427 |
| 364 // Decrements the count of event handlers blocking the given request. When the | 428 // Decrements the count of event handlers blocking the given request. When the |
| 365 // count reaches 0, we stop blocking the request and proceed it using the | 429 // count reaches 0, we stop blocking the request and proceed it using the |
| 366 // method requested by the extension with the highest precedence. Precedence | 430 // method requested by the extension with the highest precedence. Precedence |
| 367 // is decided by extension install time. If |response| is non-NULL, this | 431 // is decided by extension install time. If |response| is non-NULL, this |
| 368 // method assumes ownership. | 432 // method assumes ownership. |
| 369 void DecrementBlockCount(void* browser_context, | 433 void DecrementBlockCount(void* browser_context, |
| 370 const std::string& extension_id, | 434 const std::string& extension_id, |
| 371 const std::string& event_name, | 435 const std::string& event_name, |
| 372 uint64_t request_id, | 436 uint64_t request_id, |
| (...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 431 void* GetCrossBrowserContext(void* browser_context) const; | 495 void* GetCrossBrowserContext(void* browser_context) const; |
| 432 | 496 |
| 433 // Determines whether the specified browser_context is an incognito | 497 // Determines whether the specified browser_context is an incognito |
| 434 // browser_context (based on the contents of the cross-browser_context table | 498 // browser_context (based on the contents of the cross-browser_context table |
| 435 // and without dereferencing the browser_context pointer). | 499 // and without dereferencing the browser_context pointer). |
| 436 bool IsIncognitoBrowserContext(void* browser_context) const; | 500 bool IsIncognitoBrowserContext(void* browser_context) const; |
| 437 | 501 |
| 438 // Returns true if |request| was already signaled to some event handlers. | 502 // Returns true if |request| was already signaled to some event handlers. |
| 439 bool WasSignaled(const net::URLRequest& request) const; | 503 bool WasSignaled(const net::URLRequest& request) const; |
| 440 | 504 |
| 505 // Get the number of listeners - for testing only. |
| 506 size_t GetListenerCountForTesting(void* browser_context, |
| 507 const std::string& event_name); |
| 508 |
| 441 // A map for each browser_context that maps an event name to a set of | 509 // A map for each browser_context that maps an event name to a set of |
| 442 // extensions that are listening to that event. | 510 // extensions that are listening to that event. |
| 443 ListenerMap listeners_; | 511 ListenerMap listeners_; |
| 444 | 512 |
| 445 // A map of network requests that are waiting for at least one event handler | 513 // A map of network requests that are waiting for at least one event handler |
| 446 // to respond. | 514 // to respond. |
| 447 BlockedRequestMap blocked_requests_; | 515 BlockedRequestMap blocked_requests_; |
| 448 | 516 |
| 449 // A map of request ids to a bitvector indicating which events have been | 517 // A map of request ids to a bitvector indicating which events have been |
| 450 // signaled and should not be sent again. | 518 // signaled and should not be sent again. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 534 extensions::QuotaLimitHeuristics* heuristics) const override; | 602 extensions::QuotaLimitHeuristics* heuristics) const override; |
| 535 // Handle quota exceeded gracefully: Only warn the user but still execute the | 603 // Handle quota exceeded gracefully: Only warn the user but still execute the |
| 536 // function. | 604 // function. |
| 537 void OnQuotaExceeded(const std::string& error) override; | 605 void OnQuotaExceeded(const std::string& error) override; |
| 538 ResponseAction Run() override; | 606 ResponseAction Run() override; |
| 539 }; | 607 }; |
| 540 | 608 |
| 541 } // namespace extensions | 609 } // namespace extensions |
| 542 | 610 |
| 543 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ | 611 #endif // EXTENSIONS_BROWSER_API_WEB_REQUEST_WEB_REQUEST_API_H_ |
| OLD | NEW |