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 CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 | 11 |
12 #include "base/compiler_specific.h" | 12 #include "base/compiler_specific.h" |
13 #include "base/memory/linked_ptr.h" | 13 #include "base/memory/linked_ptr.h" |
14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
15 #include "base/values.h" | 15 #include "base/values.h" |
16 #include "chrome/browser/extensions/event_listener_map.h" | 16 #include "chrome/browser/extensions/event_listener_map.h" |
17 #include "chrome/common/extensions/event_filtering_info.h" | 17 #include "chrome/common/extensions/event_filtering_info.h" |
18 #include "content/public/browser/notification_observer.h" | 18 #include "content/public/browser/notification_observer.h" |
19 #include "content/public/browser/notification_registrar.h" | 19 #include "content/public/browser/notification_registrar.h" |
20 #include "ipc/ipc_sender.h" | 20 #include "ipc/ipc_sender.h" |
21 | 21 |
22 class GURL; | 22 class GURL; |
23 class ExtensionHost; | 23 class ExtensionHost; |
24 class ExtensionDevToolsManager; | 24 class ExtensionDevToolsManager; |
25 class Profile; | 25 class Profile; |
26 | 26 |
27 namespace content { | 27 namespace content { |
28 class RenderProcessHost; | 28 class RenderProcessHost; |
29 } | 29 } |
30 | 30 |
31 namespace extensions { | 31 namespace extensions { |
32 class Extension; | 32 class Extension; |
33 } | 33 struct Event; |
34 | 34 |
35 struct ExtensionEvent; | 35 class EventRouter : public content::NotificationObserver, |
36 | 36 public EventListenerMap::Delegate { |
37 using extensions::EventFilteringInfo; | |
38 using extensions::EventListener; | |
39 using extensions::EventListenerMap; | |
40 | |
41 class ExtensionEventRouter : public content::NotificationObserver, | |
42 public extensions::EventListenerMap::Delegate { | |
43 public: | 37 public: |
44 // These constants convey the state of our knowledge of whether we're in | 38 // These constants convey the state of our knowledge of whether we're in |
45 // a user-caused gesture as part of DispatchEvent. | 39 // a user-caused gesture as part of DispatchEvent. |
46 enum UserGestureState { | 40 enum UserGestureState { |
47 USER_GESTURE_UNKNOWN = 0, | 41 USER_GESTURE_UNKNOWN = 0, |
48 USER_GESTURE_ENABLED = 1, | 42 USER_GESTURE_ENABLED = 1, |
49 USER_GESTURE_NOT_ENABLED = 2, | 43 USER_GESTURE_NOT_ENABLED = 2, |
50 }; | 44 }; |
51 | 45 |
52 // Sends an event via ipc_sender to the given extension. Can be called on | 46 // Sends an event via ipc_sender to the given extension. Can be called on |
53 // any thread. | 47 // any thread. |
54 static void DispatchEvent(IPC::Sender* ipc_sender, | 48 static void DispatchEvent(IPC::Sender* ipc_sender, |
55 const std::string& extension_id, | 49 const std::string& extension_id, |
56 const std::string& event_name, | 50 const std::string& event_name, |
57 const base::Value& event_args, | 51 const base::Value& event_args, |
58 const GURL& event_url, | 52 const GURL& event_url, |
59 UserGestureState user_gesture, | 53 UserGestureState user_gesture, |
60 const extensions::EventFilteringInfo& info); | 54 const EventFilteringInfo& info); |
61 | 55 |
62 // This invocation is deprecated. All future consumers of this API should be | 56 // This invocation is deprecated. All future consumers of this API should be |
63 // sending Values as event arguments, using the above version. | 57 // sending Values as event arguments, using the above version. |
64 static void DispatchEvent(IPC::Sender* ipc_sender, | 58 static void DispatchEvent(IPC::Sender* ipc_sender, |
65 const std::string& extension_id, | 59 const std::string& extension_id, |
66 const std::string& event_name, | 60 const std::string& event_name, |
67 const std::string& event_args, | 61 const std::string& event_args, |
68 const GURL& event_url, | 62 const GURL& event_url, |
69 UserGestureState user_gesture, | 63 UserGestureState user_gesture, |
70 const extensions::EventFilteringInfo& info); | 64 const EventFilteringInfo& info); |
71 | 65 |
72 explicit ExtensionEventRouter(Profile* profile); | 66 explicit EventRouter(Profile* profile); |
73 virtual ~ExtensionEventRouter(); | 67 virtual ~EventRouter(); |
74 | 68 |
75 // Add or remove the process/extension pair as a listener for |event_name|. | 69 // Add or remove the process/extension pair as a listener for |event_name|. |
76 // Note that multiple extensions can share a process due to process | 70 // Note that multiple extensions can share a process due to process |
77 // collapsing. Also, a single extension can have 2 processes if it is a split | 71 // collapsing. Also, a single extension can have 2 processes if it is a split |
78 // mode extension. | 72 // mode extension. |
79 void AddEventListener(const std::string& event_name, | 73 void AddEventListener(const std::string& event_name, |
80 content::RenderProcessHost* process, | 74 content::RenderProcessHost* process, |
81 const std::string& extension_id); | 75 const std::string& extension_id); |
82 void RemoveEventListener(const std::string& event_name, | 76 void RemoveEventListener(const std::string& event_name, |
83 content::RenderProcessHost* process, | 77 content::RenderProcessHost* process, |
(...skipping 29 matching lines...) Expand all Loading... |
113 // Returns true if the extension is listening to the given event. | 107 // Returns true if the extension is listening to the given event. |
114 bool ExtensionHasEventListener(const std::string& extension_id, | 108 bool ExtensionHasEventListener(const std::string& extension_id, |
115 const std::string& event_name); | 109 const std::string& event_name); |
116 | 110 |
117 // Send an event to every registered extension renderer. If | 111 // Send an event to every registered extension renderer. If |
118 // |restrict_to_profile| is non-NULL, then the event will not be sent to other | 112 // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
119 // profiles unless the extension has permission (e.g. incognito tab update -> | 113 // profiles unless the extension has permission (e.g. incognito tab update -> |
120 // normal profile only works if extension is allowed incognito access). If | 114 // normal profile only works if extension is allowed incognito access). If |
121 // |event_url| is not empty, the event is only sent to extension with host | 115 // |event_url| is not empty, the event is only sent to extension with host |
122 // permissions for this url. | 116 // permissions for this url. |
123 void DispatchEventToRenderers( | 117 void DispatchEventToRenderers(const std::string& event_name, |
124 const std::string& event_name, | 118 const std::string& event_args, |
125 const std::string& event_args, | 119 Profile* restrict_to_profile, |
126 Profile* restrict_to_profile, | 120 const GURL& event_url, |
127 const GURL& event_url, | 121 EventFilteringInfo info); |
128 extensions::EventFilteringInfo info); | |
129 | 122 |
130 // As above, but defaults |info| to EventFilteringInfo(). | 123 // As above, but defaults |info| to EventFilteringInfo(). |
131 void DispatchEventToRenderers( | 124 void DispatchEventToRenderers(const std::string& event_name, |
132 const std::string& event_name, | 125 const std::string& event_args, |
133 const std::string& event_args, | 126 Profile* restrict_to_profile, |
134 Profile* restrict_to_profile, | 127 const GURL& event_url); |
135 const GURL& event_url); | |
136 | 128 |
137 // Same as above, except only send the event to the given extension. | 129 // Same as above, except only send the event to the given extension. |
138 virtual void DispatchEventToExtension( | 130 virtual void DispatchEventToExtension(const std::string& extension_id, |
139 const std::string& extension_id, | 131 const std::string& event_name, |
140 const std::string& event_name, | 132 const base::Value& event_args, |
141 const base::Value& event_args, | 133 Profile* restrict_to_profile, |
142 Profile* restrict_to_profile, | 134 const GURL& event_url); |
143 const GURL& event_url); | |
144 | 135 |
145 // This invocation is deprecated. The above variant which uses a Value for | 136 // This invocation is deprecated. The above variant which uses a Value for |
146 // event_args is to be used instead. | 137 // event_args is to be used instead. |
147 virtual void DispatchEventToExtension( | 138 virtual void DispatchEventToExtension(const std::string& extension_id, |
148 const std::string& extension_id, | 139 const std::string& event_name, |
149 const std::string& event_name, | 140 const std::string& event_args, |
150 const std::string& event_args, | 141 Profile* restrict_to_profile, |
151 Profile* restrict_to_profile, | 142 const GURL& event_url); |
152 const GURL& event_url); | |
153 | 143 |
154 // Dispatch an event to particular extension. Also include an | 144 // Dispatch an event to particular extension. Also include an |
155 // explicit user gesture indicator. | 145 // explicit user gesture indicator. |
156 virtual void DispatchEventToExtension( | 146 virtual void DispatchEventToExtension(const std::string& extension_id, |
157 const std::string& extension_id, | 147 const std::string& event_name, |
158 const std::string& event_name, | 148 const std::string& event_args, |
159 const std::string& event_args, | 149 Profile* restrict_to_profile, |
160 Profile* restrict_to_profile, | 150 const GURL& event_url, |
161 const GURL& event_url, | 151 UserGestureState user_gesture); |
162 UserGestureState user_gesture); | |
163 | 152 |
164 // Send different versions of an event to extensions in different profiles. | 153 // Send different versions of an event to extensions in different profiles. |
165 // This is used in the case of sending one event to extensions that have | 154 // This is used in the case of sending one event to extensions that have |
166 // incognito access, and another event to extensions that don't (here), | 155 // incognito access, and another event to extensions that don't (here), |
167 // in order to avoid sending 2 events to "spanning" extensions. | 156 // in order to avoid sending 2 events to "spanning" extensions. |
168 // If |cross_incognito_profile| is non-NULL and different from | 157 // If |cross_incognito_profile| is non-NULL and different from |
169 // restrict_to_profile, send the event with cross_incognito_args to the | 158 // restrict_to_profile, send the event with cross_incognito_args to the |
170 // extensions in that profile that can't cross incognito. | 159 // extensions in that profile that can't cross incognito. |
171 void DispatchEventsToRenderersAcrossIncognito( | 160 void DispatchEventsToRenderersAcrossIncognito( |
172 const std::string& event_name, | 161 const std::string& event_name, |
(...skipping 22 matching lines...) Expand all Loading... |
195 // the given event. If |extension_id| is non-empty, we also check that that | 184 // the given event. If |extension_id| is non-empty, we also check that that |
196 // extension is one of the listeners. | 185 // extension is one of the listeners. |
197 bool HasEventListenerImpl(const ListenerMap& listeners, | 186 bool HasEventListenerImpl(const ListenerMap& listeners, |
198 const std::string& extension_id, | 187 const std::string& extension_id, |
199 const std::string& event_name); | 188 const std::string& event_name); |
200 | 189 |
201 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the | 190 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the |
202 // event is broadcast. | 191 // event is broadcast. |
203 // An event that just came off the pending list may not be delayed again. | 192 // An event that just came off the pending list may not be delayed again. |
204 void DispatchEventImpl(const std::string& restrict_to_extension_id, | 193 void DispatchEventImpl(const std::string& restrict_to_extension_id, |
205 const linked_ptr<ExtensionEvent>& event); | 194 const linked_ptr<Event>& event); |
206 | 195 |
207 // Ensures that all lazy background pages that are interested in the given | 196 // Ensures that all lazy background pages that are interested in the given |
208 // event are loaded, and queues the event if the page is not ready yet. | 197 // event are loaded, and queues the event if the page is not ready yet. |
209 void DispatchLazyEvent(const std::string& extension_id, | 198 void DispatchLazyEvent(const std::string& extension_id, |
210 const linked_ptr<ExtensionEvent>& event); | 199 const linked_ptr<Event>& event); |
211 | 200 |
212 // Dispatches the event to the specified extension running in |process|. | 201 // Dispatches the event to the specified extension running in |process|. |
213 void DispatchEventToProcess(const std::string& extension_id, | 202 void DispatchEventToProcess(const std::string& extension_id, |
214 content::RenderProcessHost* process, | 203 content::RenderProcessHost* process, |
215 const linked_ptr<ExtensionEvent>& event); | 204 const linked_ptr<Event>& event); |
216 | 205 |
217 // Returns false when the event is scoped to a profile and the listening | 206 // Returns false when the event is scoped to a profile and the listening |
218 // extension does not have access to events from that profile. Also fills | 207 // extension does not have access to events from that profile. Also fills |
219 // |event_args| with the proper arguments to send, which may differ if | 208 // |event_args| with the proper arguments to send, which may differ if |
220 // the event crosses the incognito boundary. | 209 // the event crosses the incognito boundary. |
221 bool CanDispatchEventToProfile( | 210 bool CanDispatchEventToProfile(Profile* profile, |
222 Profile* profile, | 211 const Extension* extension, |
223 const extensions::Extension* extension, | 212 const linked_ptr<Event>& event, |
224 const linked_ptr<ExtensionEvent>& event, | 213 const base::Value** event_args); |
225 const base::Value** event_args); | |
226 | 214 |
227 // Possibly loads given extension's background page in preparation to | 215 // Possibly loads given extension's background page in preparation to |
228 // dispatch an event. | 216 // dispatch an event. |
229 void MaybeLoadLazyBackgroundPageToDispatchEvent( | 217 void MaybeLoadLazyBackgroundPageToDispatchEvent( |
230 Profile* profile, | 218 Profile* profile, |
231 const extensions::Extension* extension, | 219 const Extension* extension, |
232 const linked_ptr<ExtensionEvent>& event); | 220 const linked_ptr<Event>& event); |
233 | 221 |
234 // Track of the number of dispatched events that have not yet sent an | 222 // Track of the number of dispatched events that have not yet sent an |
235 // ACK from the renderer. | 223 // ACK from the renderer. |
236 void IncrementInFlightEvents(Profile* profile, | 224 void IncrementInFlightEvents(Profile* profile, |
237 const extensions::Extension* extension); | 225 const Extension* extension); |
238 | 226 |
239 void DispatchPendingEvent(const linked_ptr<ExtensionEvent>& event, | 227 void DispatchPendingEvent(const linked_ptr<Event>& event, |
240 ExtensionHost* host); | 228 ExtensionHost* host); |
241 | 229 |
242 // Implementation of extensions::EventListenerMap::Delegate. | 230 // Implementation of EventListenerMap::Delegate. |
243 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; | 231 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; |
244 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; | 232 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
245 | 233 |
246 Profile* profile_; | 234 Profile* profile_; |
247 | 235 |
248 content::NotificationRegistrar registrar_; | 236 content::NotificationRegistrar registrar_; |
249 | 237 |
250 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 238 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
251 | 239 |
252 EventListenerMap listeners_; | 240 EventListenerMap listeners_; |
253 | 241 |
254 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); | 242 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
255 }; | 243 }; |
256 | 244 |
257 struct ExtensionEvent { | 245 struct Event { |
258 std::string event_name; | 246 std::string event_name; |
259 scoped_ptr<Value> event_args; | 247 scoped_ptr<Value> event_args; |
260 GURL event_url; | 248 GURL event_url; |
261 Profile* restrict_to_profile; | 249 Profile* restrict_to_profile; |
262 scoped_ptr<Value> cross_incognito_args; | 250 scoped_ptr<Value> cross_incognito_args; |
263 ExtensionEventRouter::UserGestureState user_gesture; | 251 EventRouter::UserGestureState user_gesture; |
264 extensions::EventFilteringInfo info; | 252 EventFilteringInfo info; |
265 | 253 |
266 ExtensionEvent(const std::string& event_name, | 254 Event(const std::string& event_name, |
267 const Value& event_args, | 255 const Value& event_args, |
268 const GURL& event_url, | 256 const GURL& event_url, |
269 Profile* restrict_to_profile, | 257 Profile* restrict_to_profile, |
270 const Value& cross_incognito_args, | 258 const Value& cross_incognito_args, |
271 ExtensionEventRouter::UserGestureState user_gesture, | 259 EventRouter::UserGestureState user_gesture, |
272 const extensions::EventFilteringInfo& info); | 260 const EventFilteringInfo& info); |
273 | 261 |
274 // TODO(gdk): This variant should be retired once the callers are switched to | 262 // TODO(gdk): This variant should be retired once the callers are switched to |
275 // providing Values instead of just strings. | 263 // providing Values instead of just strings. |
276 ExtensionEvent(const std::string& event_name, | 264 Event(const std::string& event_name, |
277 const std::string& event_args, | 265 const std::string& event_args, |
278 const GURL& event_url, | 266 const GURL& event_url, |
279 Profile* restrict_to_profile, | 267 Profile* restrict_to_profile, |
280 const std::string& cross_incognito_args, | 268 const std::string& cross_incognito_args, |
281 ExtensionEventRouter::UserGestureState user_gesture, | 269 EventRouter::UserGestureState user_gesture, |
282 const extensions::EventFilteringInfo& info); | 270 const EventFilteringInfo& info); |
283 | 271 |
284 ExtensionEvent(const std::string& event_name, | 272 Event(const std::string& event_name, |
285 const Value& event_args, | 273 const Value& event_args, |
286 const GURL& event_url, | 274 const GURL& event_url, |
287 Profile* restrict_to_profile, | 275 Profile* restrict_to_profile, |
288 ExtensionEventRouter::UserGestureState user_gesture, | 276 EventRouter::UserGestureState user_gesture, |
289 const extensions::EventFilteringInfo& info); | 277 const EventFilteringInfo& info); |
290 | 278 |
291 ~ExtensionEvent(); | 279 ~Event(); |
292 }; | 280 }; |
293 | 281 |
| 282 } // namespace extensions |
294 | 283 |
295 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 284 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
OLD | NEW |