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 | 19 |
20 class ExtensionEventRouterForwarder; | 20 class ExtensionEventRouterForwarder; |
21 class GURL; | 21 class GURL; |
22 | 22 |
23 namespace net { | 23 namespace net { |
| 24 class HttpRequestHeaders; |
24 class URLRequest; | 25 class URLRequest; |
25 } | 26 } |
26 | 27 |
27 // This class observes network events and routes them to the appropriate | 28 // This class observes network events and routes them to the appropriate |
28 // extensions listening to those events. All methods must be called on the IO | 29 // extensions listening to those events. All methods must be called on the IO |
29 // thread unless otherwise specified. | 30 // thread unless otherwise specified. |
30 class ExtensionWebRequestEventRouter { | 31 class ExtensionWebRequestEventRouter { |
31 public: | 32 public: |
32 struct RequestFilter; | 33 struct RequestFilter; |
33 struct ExtraInfoSpec; | 34 struct ExtraInfoSpec; |
34 | 35 |
35 static ExtensionWebRequestEventRouter* GetInstance(); | 36 static ExtensionWebRequestEventRouter* GetInstance(); |
36 | 37 |
37 // Dispatches the OnBeforeRequest event to any extensions whose filters match | 38 // Dispatches the OnBeforeRequest event to any extensions whose filters match |
38 // the given request. Returns true if an extension wants to pause the request. | 39 // the given request. Returns true if an extension wants to pause the request. |
39 bool OnBeforeRequest(ProfileId profile_id, | 40 bool OnBeforeRequest(ProfileId profile_id, |
40 ExtensionEventRouterForwarder* event_router, | 41 ExtensionEventRouterForwarder* event_router, |
41 net::URLRequest* request, | 42 net::URLRequest* request, |
42 net::CompletionCallback* callback); | 43 net::CompletionCallback* callback); |
43 | 44 |
| 45 // Same as above, but for HTTP(s) requests. This version includes the request |
| 46 // headers about to be sent. |
| 47 bool OnBeforeHttpRequest(ProfileId profile_id, |
| 48 ExtensionEventRouterForwarder* event_router, |
| 49 uint64 request_id, |
| 50 net::HttpRequestHeaders* headers, |
| 51 net::CompletionCallback* callback); |
| 52 |
44 // Called when an event listener handles a blocking event and responds. | 53 // Called when an event listener handles a blocking event and responds. |
45 // TODO(mpcomplete): modify request | 54 // TODO(mpcomplete): modify request |
46 void OnEventHandled( | 55 void OnEventHandled( |
47 ProfileId profile_id, | 56 ProfileId profile_id, |
48 const std::string& extension_id, | 57 const std::string& extension_id, |
49 const std::string& event_name, | 58 const std::string& event_name, |
50 const std::string& sub_event_name, | 59 const std::string& sub_event_name, |
51 uint64 request_id, | 60 uint64 request_id, |
52 bool cancel); | 61 bool cancel); |
53 | 62 |
(...skipping 15 matching lines...) Expand all Loading... |
69 const std::string& extension_id, | 78 const std::string& extension_id, |
70 const std::string& sub_event_name); | 79 const std::string& sub_event_name); |
71 | 80 |
72 private: | 81 private: |
73 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; | 82 friend struct DefaultSingletonTraits<ExtensionWebRequestEventRouter>; |
74 struct EventListener; | 83 struct EventListener; |
75 struct BlockedRequest; | 84 struct BlockedRequest; |
76 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; | 85 typedef std::map<std::string, std::set<EventListener> > ListenerMapForProfile; |
77 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; | 86 typedef std::map<ProfileId, ListenerMapForProfile> ListenerMap; |
78 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; | 87 typedef std::map<uint64, BlockedRequest> BlockedRequestMap; |
| 88 typedef std::map<uint64, net::URLRequest*> HttpRequestMap; |
79 | 89 |
80 ExtensionWebRequestEventRouter(); | 90 ExtensionWebRequestEventRouter(); |
81 ~ExtensionWebRequestEventRouter(); | 91 ~ExtensionWebRequestEventRouter(); |
82 | 92 |
| 93 bool OnBeforeRequestCommon( |
| 94 ProfileId profile_id, |
| 95 ExtensionEventRouterForwarder* event_router, |
| 96 net::URLRequest* request, |
| 97 net::CompletionCallback* callback, |
| 98 int tab_id, |
| 99 int window_id, |
| 100 ResourceType::Type resource_type, |
| 101 const std::vector<const EventListener*>& listeners); |
| 102 |
83 // Returns a list of event listeners that care about the given event, based | 103 // Returns a list of event listeners that care about the given event, based |
84 // on their filter parameters. | 104 // on their filter parameters. |
85 std::vector<const EventListener*> GetMatchingListeners( | 105 std::vector<const EventListener*> GetMatchingListeners( |
86 ProfileId profile_id, const std::string& event_name, const GURL& url); | 106 ProfileId profile_id, const std::string& event_name, const GURL& url); |
87 | 107 |
88 // Decrements the count of event handlers blocking the given request. When the | 108 // Decrements the count of event handlers blocking the given request. When the |
89 // count reaches 0 (or immediately if the request is being cancelled), we | 109 // count reaches 0 (or immediately if the request is being cancelled), we |
90 // stop blocking the request and either resume or cancel it. | 110 // stop blocking the request and either resume or cancel it. |
91 void DecrementBlockCount(uint64 request_id, bool cancel); | 111 void DecrementBlockCount(uint64 request_id, bool cancel); |
92 | 112 |
93 // A map for each profile that maps an event name to a set of extensions that | 113 // A map for each profile that maps an event name to a set of extensions that |
94 // are listening to that event. | 114 // are listening to that event. |
95 ListenerMap listeners_; | 115 ListenerMap listeners_; |
96 | 116 |
97 // A map of network requests that are waiting for at least one event handler | 117 // A map of network requests that are waiting for at least one event handler |
98 // to respond. | 118 // to respond. |
99 BlockedRequestMap blocked_requests_; | 119 BlockedRequestMap blocked_requests_; |
100 | 120 |
| 121 // A map of HTTP(s) network requests. We don't dispatch onBeforeRequest for |
| 122 // these requests until headers are available. |
| 123 HttpRequestMap http_requests_; |
| 124 |
101 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); | 125 DISALLOW_COPY_AND_ASSIGN(ExtensionWebRequestEventRouter); |
102 }; | 126 }; |
103 | 127 |
104 class WebRequestAddEventListener : public SyncExtensionFunction { | 128 class WebRequestAddEventListener : public SyncExtensionFunction { |
105 public: | 129 public: |
106 virtual bool RunImpl(); | 130 virtual bool RunImpl(); |
107 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); | 131 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.addEventListener"); |
108 }; | 132 }; |
109 | 133 |
110 class WebRequestEventHandled : public SyncExtensionFunction { | 134 class WebRequestEventHandled : public SyncExtensionFunction { |
111 public: | 135 public: |
112 virtual bool RunImpl(); | 136 virtual bool RunImpl(); |
113 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); | 137 DECLARE_EXTENSION_FUNCTION_NAME("experimental.webRequest.eventHandled"); |
114 }; | 138 }; |
115 | 139 |
116 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ | 140 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_WEBREQUEST_API_H_ |
OLD | NEW |