| 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 true if an extension wants to pause the request. |
| 40 bool OnBeforeRequest(ProfileId profile_id, | 41 bool OnBeforeRequest(ProfileId profile_id, |
| 41 ExtensionEventRouterForwarder* event_router, | 42 ExtensionEventRouterForwarder* event_router, |
| 42 net::URLRequest* request, | 43 net::URLRequest* request, |
| 43 net::CompletionCallback* callback); | 44 net::CompletionCallback* callback); |
| 44 | 45 |
| 46 // Same as above, but for HTTP(s) requests. This version includes the request |
| 47 // headers about to be sent. |
| 48 bool OnBeforeSendHeaders(ProfileId profile_id, |
| 49 ExtensionEventRouterForwarder* event_router, |
| 50 uint64 request_id, |
| 51 net::HttpRequestHeaders* headers, |
| 52 net::CompletionCallback* callback); |
| 53 |
| 54 void OnURLRequestDestroyed(ProfileId profile_id, net::URLRequest* request); |
| 55 |
| 45 // Called when an event listener handles a blocking event and responds. | 56 // Called when an event listener handles a blocking event and responds. |
| 46 // TODO(mpcomplete): modify request | 57 // TODO(mpcomplete): modify request |
| 47 void OnEventHandled( | 58 void OnEventHandled( |
| 48 ProfileId profile_id, | 59 ProfileId profile_id, |
| 49 const std::string& extension_id, | 60 const std::string& extension_id, |
| 50 const std::string& event_name, | 61 const std::string& event_name, |
| 51 const std::string& sub_event_name, | 62 const std::string& sub_event_name, |
| 52 uint64 request_id, | 63 uint64 request_id, |
| 53 bool cancel); | 64 bool cancel); |
| 54 | 65 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 70 const std::string& extension_id, | 81 const std::string& extension_id, |
| 71 const std::string& sub_event_name); | 82 const std::string& sub_event_name); |
| 72 | 83 |
| 73 private: | 84 private: |
| 74 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 85 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
| 75 struct EventListener; | 86 struct EventListener; |
| 76 struct BlockedRequest; | 87 struct BlockedRequest; |
| 77 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; | 88 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; |
| 78 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; | 89 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; |
| 79 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; | 90 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; |
| 91 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; |
| 80 | 92 |
| 81 ExtensionWebRequestEventRouter(); | 93 ExtensionWebRequestEventRouter(); |
| 82 ~ExtensionWebRequestEventRouter(); | 94 ~ExtensionWebRequestEventRouter(); |
| 83 | 95 |
| 96 bool DispatchEvent( |
| 97 ProfileId profile_id, |
| 98 ExtensionEventRouterForwarder* event_router, |
| 99 net::URLRequest* request, |
| 100 net::CompletionCallback* callback, |
| 101 const std::vector<const EventListener*>& listeners, |
| 102 const ListValue& args); |
| 103 |
| 84 // Returns a list of event listeners that care about the given event, based | 104 // Returns a list of event listeners that care about the given event, based |
| 85 // on their filter parameters. | 105 // on their filter parameters. |
| 86 std::vector<const EventListener*> GetMatchingListeners( | 106 std::vector<const EventListener*> GetMatchingListeners( |
| 87 ProfileId profile_id, | 107 ProfileId profile_id, |
| 88 const std::string& event_name, | 108 const std::string& event_name, |
| 89 const GURL& url, | 109 const GURL& url, |
| 90 int tab_id, | 110 int tab_id, |
| 91 int window_id, | 111 int window_id, |
| 92 ResourceType::Type resource_type); | 112 ResourceType::Type resource_type); |
| 93 | 113 |
| 114 // Same as above, but retrieves the filter parameters from the request. |
| 115 std::vector<const EventListener*> GetMatchingListeners( |
| 116 ProfileId profile_id, |
| 117 const std::string& event_name, |
| 118 net::URLRequest* request); |
| 94 // Decrements the count of event handlers blocking the given request. When the | 119 // 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 | 120 // count reaches 0 (or immediately if the request is being cancelled), we |
| 96 // stop blocking the request and either resume or cancel it. | 121 // stop blocking the request and either resume or cancel it. |
| 97 void DecrementBlockCount(uint64 request_id, bool cancel); | 122 void DecrementBlockCount(uint64 request_id, bool cancel); |
| 98 | 123 |
| 124 void OnRequestDeleted(net::URLRequest* request); |
| 125 |
| 99 // A map for each profile that maps an event name to a set of extensions that | 126 // A map for each profile that maps an event name to a set of extensions that |
| 100 // are listening to that event. | 127 // are listening to that event. |
| 101 ListenerMap listeners_; | 128 ListenerMap listeners_; |
| 102 | 129 |
| 103 // A map of network requests that are waiting for at least one event handler | 130 // A map of network requests that are waiting for at least one event handler |
| 104 // to respond. | 131 // to respond. |
| 105 BlockedRequestMap blocked_requests_; | 132 BlockedRequestMap blocked_requests_; |
| 106 | 133 |
| 134 // A map of HTTP(s) network requests. We don't dispatch onBeforeRequest for |
| 135 // these requests until headers are available. |
| 136 HttpRequestMap http_requests_; |
| 137 |
| 107 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); | 138 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); |
| 108 }; | 139 }; |
| 109 | 140 |
| 110 class WebRequestAddEventListener : public SyncExtensionFunction { | 141 class WebRequestAddEventListener : public SyncExtensionFunction { |
| 111 public: | 142 public: |
| 112 virtual bool RunImpl(); | 143 virtual bool RunImpl(); |
| 113 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); | 144 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); |
| 114 }; | 145 }; |
| 115 | 146 |
| 116 class WebRequestEventHandled : public SyncExtensionFunction { | 147 class WebRequestEventHandled : public SyncExtensionFunction { |
| 117 public: | 148 public: |
| 118 virtual bool RunImpl(); | 149 virtual bool RunImpl(); |
| 119 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); | 150 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); |
| 120 }; | 151 }; |
| 121 | 152 |
| 122 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 153 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
| OLD | NEW |