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_EVENT_ROUTER_H_ | 5 #ifndef EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
21 #include "content/public/browser/notification_observer.h" | 21 #include "content/public/browser/notification_observer.h" |
22 #include "content/public/browser/notification_registrar.h" | 22 #include "content/public/browser/notification_registrar.h" |
23 #include "content/public/browser/render_process_host_observer.h" | 23 #include "content/public/browser/render_process_host_observer.h" |
24 #include "extensions/browser/event_listener_map.h" | 24 #include "extensions/browser/event_listener_map.h" |
25 #include "extensions/browser/extension_event_histogram_value.h" | 25 #include "extensions/browser/extension_event_histogram_value.h" |
26 #include "extensions/browser/extension_registry_observer.h" | 26 #include "extensions/browser/extension_registry_observer.h" |
27 #include "extensions/common/constants.h" | |
27 #include "extensions/common/event_filtering_info.h" | 28 #include "extensions/common/event_filtering_info.h" |
28 #include "ipc/ipc_sender.h" | 29 #include "ipc/ipc_sender.h" |
29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
30 | 31 |
31 class GURL; | 32 class GURL; |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 class BrowserContext; | 35 class BrowserContext; |
35 class RenderProcessHost; | 36 class RenderProcessHost; |
36 } | 37 } |
(...skipping 16 matching lines...) Expand all Loading... | |
53 // These constants convey the state of our knowledge of whether we're in | 54 // These constants convey the state of our knowledge of whether we're in |
54 // a user-caused gesture as part of DispatchEvent. | 55 // a user-caused gesture as part of DispatchEvent. |
55 enum UserGestureState { | 56 enum UserGestureState { |
56 USER_GESTURE_UNKNOWN = 0, | 57 USER_GESTURE_UNKNOWN = 0, |
57 USER_GESTURE_ENABLED = 1, | 58 USER_GESTURE_ENABLED = 1, |
58 USER_GESTURE_NOT_ENABLED = 2, | 59 USER_GESTURE_NOT_ENABLED = 2, |
59 }; | 60 }; |
60 | 61 |
61 // The pref key for the list of event names for which an extension has | 62 // The pref key for the list of event names for which an extension has |
62 // registered from its lazy background page. | 63 // registered from its lazy background page. |
63 static const char kRegisteredEvents[]; | 64 static const char kRegisteredLazyEvents[]; |
65 // The pref key for the list of event names for which an extension has | |
66 // registered from its service worker. | |
67 static const char kRegisteredServiceWorkerEvents[]; | |
64 | 68 |
65 // Observers register interest in events with a particular name and are | 69 // Observers register interest in events with a particular name and are |
66 // notified when a listener is added or removed. Observers are matched by | 70 // notified when a listener is added or removed. Observers are matched by |
67 // the base name of the event (e.g. adding an event listener for event name | 71 // the base name of the event (e.g. adding an event listener for event name |
68 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). | 72 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). |
69 class Observer { | 73 class Observer { |
70 public: | 74 public: |
71 // Called when a listener is added. | 75 // Called when a listener is added. |
72 virtual void OnListenerAdded(const EventListenerInfo& details) {} | 76 virtual void OnListenerAdded(const EventListenerInfo& details) {} |
73 // Called when a listener is removed. | 77 // Called when a listener is removed. |
(...skipping 27 matching lines...) Expand all Loading... | |
101 ExtensionPrefs* extension_prefs); | 105 ExtensionPrefs* extension_prefs); |
102 ~EventRouter() override; | 106 ~EventRouter() override; |
103 | 107 |
104 // Add or remove an extension as an event listener for |event_name|. | 108 // Add or remove an extension as an event listener for |event_name|. |
105 // | 109 // |
106 // Note that multiple extensions can share a process due to process | 110 // Note that multiple extensions can share a process due to process |
107 // collapsing. Also, a single extension can have 2 processes if it is a split | 111 // collapsing. Also, a single extension can have 2 processes if it is a split |
108 // mode extension. | 112 // mode extension. |
109 void AddEventListener(const std::string& event_name, | 113 void AddEventListener(const std::string& event_name, |
110 content::RenderProcessHost* process, | 114 content::RenderProcessHost* process, |
111 const std::string& extension_id); | 115 const ExtensionId& extension_id); |
116 void AddServiceWorkerEventListener(const std::string& event_name, | |
117 content::RenderProcessHost* process, | |
118 const ExtensionId& extension_id, | |
119 int worker_thread_id); | |
112 void RemoveEventListener(const std::string& event_name, | 120 void RemoveEventListener(const std::string& event_name, |
113 content::RenderProcessHost* process, | 121 content::RenderProcessHost* process, |
114 const std::string& extension_id); | 122 const ExtensionId& extension_id); |
123 void RemoveServiceWorkerEventListener(const std::string& event_name, | |
124 content::RenderProcessHost* process, | |
125 const ExtensionId& extension_id, | |
126 int worker_thread_id); | |
115 | 127 |
116 // Add or remove a URL as an event listener for |event_name|. | 128 // Add or remove a URL as an event listener for |event_name|. |
117 void AddEventListenerForURL(const std::string& event_name, | 129 void AddEventListenerForURL(const std::string& event_name, |
118 content::RenderProcessHost* process, | 130 content::RenderProcessHost* process, |
119 const GURL& listener_url); | 131 const GURL& listener_url); |
120 void RemoveEventListenerForURL(const std::string& event_name, | 132 void RemoveEventListenerForURL(const std::string& event_name, |
121 content::RenderProcessHost* process, | 133 content::RenderProcessHost* process, |
122 const GURL& listener_url); | 134 const GURL& listener_url); |
123 | 135 |
124 EventListenerMap& listeners() { return listeners_; } | 136 EventListenerMap& listeners() { return listeners_; } |
125 | 137 |
126 // Registers an observer to be notified when an event listener for | 138 // Registers an observer to be notified when an event listener for |
127 // |event_name| is added or removed. There can currently be only one observer | 139 // |event_name| is added or removed. There can currently be only one observer |
128 // for each distinct |event_name|. | 140 // for each distinct |event_name|. |
129 void RegisterObserver(Observer* observer, | 141 void RegisterObserver(Observer* observer, |
130 const std::string& event_name); | 142 const std::string& event_name); |
131 | 143 |
132 // Unregisters an observer from all events. | 144 // Unregisters an observer from all events. |
133 void UnregisterObserver(Observer* observer); | 145 void UnregisterObserver(Observer* observer); |
134 | 146 |
135 // Add or remove the extension as having a lazy background page that listens | 147 // Add or remove the extension as having a lazy background page that listens |
136 // to the event. The difference from the above methods is that these will be | 148 // to the event. The difference from the above methods is that these will be |
137 // remembered even after the process goes away. We use this list to decide | 149 // remembered even after the process goes away. We use this list to decide |
138 // which extension pages to load when dispatching an event. | 150 // which extension pages to load when dispatching an event. |
139 void AddLazyEventListener(const std::string& event_name, | 151 void AddLazyEventListener(const std::string& event_name, |
140 const std::string& extension_id); | 152 const ExtensionId& extension_id); |
141 void RemoveLazyEventListener(const std::string& event_name, | 153 void RemoveLazyEventListener(const std::string& event_name, |
142 const std::string& extension_id); | 154 const ExtensionId& extension_id); |
155 // Similar to Add/RemoveLazyEventListener, but applies to extension service | |
156 // workers. | |
157 void AddLazyServiceWorkerEventListener(const std::string& event_name, | |
158 const ExtensionId& extension_id, | |
159 int worker_thread_id); | |
160 void RemoveLazyServiceWorkerEventListener(const std::string& event_name, | |
161 const ExtensionId& extension_id, | |
162 int worker_thread_id); | |
143 | 163 |
144 // If |add_lazy_listener| is true also add the lazy version of this listener. | 164 // If |add_lazy_listener| is true also add the lazy version of this listener. |
145 void AddFilteredEventListener(const std::string& event_name, | 165 void AddFilteredEventListener(const std::string& event_name, |
146 content::RenderProcessHost* process, | 166 content::RenderProcessHost* process, |
147 const std::string& extension_id, | 167 const std::string& extension_id, |
148 const base::DictionaryValue& filter, | 168 const base::DictionaryValue& filter, |
149 bool add_lazy_listener); | 169 bool add_lazy_listener); |
150 | 170 |
151 // If |remove_lazy_listener| is true also remove the lazy version of this | 171 // If |remove_lazy_listener| is true also remove the lazy version of this |
152 // listener. | 172 // listener. |
(...skipping 23 matching lines...) Expand all Loading... | |
176 // newly installed extensions. | 196 // newly installed extensions. |
177 void DispatchEventWithLazyListener(const std::string& extension_id, | 197 void DispatchEventWithLazyListener(const std::string& extension_id, |
178 std::unique_ptr<Event> event); | 198 std::unique_ptr<Event> event); |
179 | 199 |
180 // Record the Event Ack from the renderer. (One less event in-flight.) | 200 // Record the Event Ack from the renderer. (One less event in-flight.) |
181 void OnEventAck(content::BrowserContext* context, | 201 void OnEventAck(content::BrowserContext* context, |
182 const std::string& extension_id); | 202 const std::string& extension_id); |
183 | 203 |
184 // Returns whether or not the given extension has any registered events. | 204 // Returns whether or not the given extension has any registered events. |
185 bool HasRegisteredEvents(const ExtensionId& extension_id) const { | 205 bool HasRegisteredEvents(const ExtensionId& extension_id) const { |
186 return !GetRegisteredEvents(extension_id).empty(); | 206 return !GetRegisteredEvents(extension_id, kRegisteredLazyEvents).empty(); |
187 } | 207 } |
188 | 208 |
189 // Clears registered events for testing purposes. | 209 // Clears registered events for testing purposes. |
190 void ClearRegisteredEventsForTest(const ExtensionId& extension_id) { | 210 void ClearRegisteredEventsForTest(const ExtensionId& extension_id); |
191 SetRegisteredEvents(extension_id, std::set<std::string>()); | |
192 } | |
193 | 211 |
194 // Reports UMA for an event dispatched to |extension| with histogram value | 212 // Reports UMA for an event dispatched to |extension| with histogram value |
195 // |histogram_value|. Must be called on the UI thread. | 213 // |histogram_value|. Must be called on the UI thread. |
196 // | 214 // |
197 // |did_enqueue| should be true if the event was queued waiting for a process | 215 // |did_enqueue| should be true if the event was queued waiting for a process |
198 // to start, like an event page. | 216 // to start, like an event page. |
199 void ReportEvent(events::HistogramValue histogram_value, | 217 void ReportEvent(events::HistogramValue histogram_value, |
200 const Extension* extension, | 218 const Extension* extension, |
201 bool did_enqueue); | 219 bool did_enqueue); |
202 | 220 |
203 private: | 221 private: |
204 friend class EventRouterFilterTest; | 222 friend class EventRouterFilterTest; |
205 friend class EventRouterTest; | 223 friend class EventRouterTest; |
206 | 224 |
207 // An identifier for an event dispatch that is used to prevent double dispatch | 225 // An identifier for an event dispatch that is used to prevent double dispatch |
208 // due to race conditions between the direct and lazy dispatch paths. | 226 // due to race conditions between the direct and lazy dispatch paths. |
209 typedef std::pair<const content::BrowserContext*, std::string> | 227 typedef std::tuple<const content::BrowserContext*, std::string, int> |
210 EventDispatchIdentifier; | 228 EventDispatchIdentifier; |
211 | 229 |
212 // TODO(gdk): Document this. | 230 // TODO(gdk): Document this. |
213 static void DispatchExtensionMessage( | 231 static void DispatchExtensionMessage( |
214 IPC::Sender* ipc_sender, | 232 IPC::Sender* ipc_sender, |
233 int worker_thread_id, | |
215 void* browser_context_id, | 234 void* browser_context_id, |
216 const std::string& extension_id, | 235 const std::string& extension_id, |
217 int event_id, | 236 int event_id, |
218 const std::string& event_name, | 237 const std::string& event_name, |
219 base::ListValue* event_args, | 238 base::ListValue* event_args, |
220 UserGestureState user_gesture, | 239 UserGestureState user_gesture, |
221 const extensions::EventFilteringInfo& info); | 240 const extensions::EventFilteringInfo& info); |
222 | 241 |
223 // Returns or sets the list of events for which the given extension has | 242 // Returns or sets the list of events for which the given extension has |
224 // registered. | 243 // registered. |
225 std::set<std::string> GetRegisteredEvents( | 244 std::set<std::string> GetRegisteredEvents(const std::string& extension_id, |
226 const std::string& extension_id) const; | 245 const char* pref_key) const; |
Devlin
2017/05/24 17:58:25
nit: can we pass an enum rather than a const char
lazyboy
2017/05/25 01:33:43
Done.
| |
227 void SetRegisteredEvents(const std::string& extension_id, | 246 void SetRegisteredEvents(const std::string& extension_id, |
228 const std::set<std::string>& events); | 247 const std::set<std::string>& events, |
248 const char* pref_key); | |
229 | 249 |
230 void Observe(int type, | 250 void Observe(int type, |
231 const content::NotificationSource& source, | 251 const content::NotificationSource& source, |
232 const content::NotificationDetails& details) override; | 252 const content::NotificationDetails& details) override; |
233 // ExtensionRegistryObserver implementation. | 253 // ExtensionRegistryObserver implementation. |
234 void OnExtensionLoaded(content::BrowserContext* browser_context, | 254 void OnExtensionLoaded(content::BrowserContext* browser_context, |
235 const Extension* extension) override; | 255 const Extension* extension) override; |
236 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 256 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
237 const Extension* extension, | 257 const Extension* extension, |
238 UnloadedExtensionReason reason) override; | 258 UnloadedExtensionReason reason) override; |
239 | 259 |
260 void AddLazyEventListenerImpl(const std::string& event_name, | |
261 const ExtensionId& extension_id, | |
262 int worker_thread_id); | |
263 void RemoveLazyEventListenerImpl(const std::string& event_name, | |
264 const ExtensionId& extension_id, | |
265 int worker_thread_id); | |
266 | |
240 // Shared by all event dispatch methods. If |restrict_to_extension_id| is | 267 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
241 // empty, the event is broadcast. An event that just came off the pending | 268 // empty, the event is broadcast. An event that just came off the pending |
242 // list may not be delayed again. | 269 // list may not be delayed again. |
243 void DispatchEventImpl(const std::string& restrict_to_extension_id, | 270 void DispatchEventImpl(const std::string& restrict_to_extension_id, |
244 const linked_ptr<Event>& event); | 271 const linked_ptr<Event>& event); |
245 | 272 |
246 // Ensures that all lazy background pages that are interested in the given | 273 // Ensures that all lazy background pages that are interested in the given |
247 // event are loaded, and queues the event if the page is not ready yet. | 274 // event are loaded, and queues the event if the page is not ready yet. |
248 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy | 275 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy |
249 // event dispatch that is queued. | 276 // event dispatch that is queued. |
250 void DispatchLazyEvent(const std::string& extension_id, | 277 void DispatchLazyEvent(const std::string& extension_id, |
251 const linked_ptr<Event>& event, | 278 const linked_ptr<Event>& event, |
252 std::set<EventDispatchIdentifier>* already_dispatched, | 279 std::set<EventDispatchIdentifier>* already_dispatched, |
253 const base::DictionaryValue* listener_filter); | 280 const base::DictionaryValue* listener_filter); |
254 | 281 |
255 // Dispatches the event to the specified extension or URL running in | 282 // Dispatches the event to the specified extension or URL running in |
256 // |process|. | 283 // |process|. |
257 void DispatchEventToProcess(const std::string& extension_id, | 284 void DispatchEventToProcess(const std::string& extension_id, |
258 const GURL& listener_url, | 285 const GURL& listener_url, |
259 content::RenderProcessHost* process, | 286 content::RenderProcessHost* process, |
287 int worker_thread_id, | |
260 const linked_ptr<Event>& event, | 288 const linked_ptr<Event>& event, |
261 const base::DictionaryValue* listener_filter, | 289 const base::DictionaryValue* listener_filter, |
262 bool did_enqueue); | 290 bool did_enqueue); |
263 | 291 |
264 // Returns false when the event is scoped to a context and the listening | 292 // Returns false when the event is scoped to a context and the listening |
265 // extension does not have access to events from that context. Also fills | 293 // extension does not have access to events from that context. Also fills |
266 // |event_args| with the proper arguments to send, which may differ if | 294 // |event_args| with the proper arguments to send, which may differ if |
267 // the event crosses the incognito boundary. | 295 // the event crosses the incognito boundary. |
268 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, | 296 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, |
269 const Extension* extension, | 297 const Extension* extension, |
(...skipping 14 matching lines...) Expand all Loading... | |
284 const base::DictionaryValue* filter); | 312 const base::DictionaryValue* filter); |
285 | 313 |
286 // Removes a filter from an event. | 314 // Removes a filter from an event. |
287 void RemoveFilterFromEvent(const std::string& event_name, | 315 void RemoveFilterFromEvent(const std::string& event_name, |
288 const std::string& extension_id, | 316 const std::string& extension_id, |
289 const base::DictionaryValue* filter); | 317 const base::DictionaryValue* filter); |
290 | 318 |
291 // Returns the dictionary of event filters that the given extension has | 319 // Returns the dictionary of event filters that the given extension has |
292 // registered. | 320 // registered. |
293 const base::DictionaryValue* GetFilteredEvents( | 321 const base::DictionaryValue* GetFilteredEvents( |
294 const std::string& extension_id); | 322 const std::string& extension_id, |
323 const char* pref_key); | |
295 | 324 |
296 // Track the dispatched events that have not yet sent an ACK from the | 325 // Track the dispatched events that have not yet sent an ACK from the |
297 // renderer. | 326 // renderer. |
298 void IncrementInFlightEvents(content::BrowserContext* context, | 327 void IncrementInFlightEvents(content::BrowserContext* context, |
299 const Extension* extension, | 328 const Extension* extension, |
300 int event_id, | 329 int event_id, |
301 const std::string& event_name); | 330 const std::string& event_name); |
302 | 331 |
303 // static | 332 // static |
304 static void DoDispatchEventToSenderBookkeepingOnUI( | 333 static void DoDispatchEventToSenderBookkeepingOnUI( |
(...skipping 117 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
422 const std::string event_name; | 451 const std::string event_name; |
423 | 452 |
424 const std::string extension_id; | 453 const std::string extension_id; |
425 const GURL listener_url; | 454 const GURL listener_url; |
426 content::BrowserContext* browser_context; | 455 content::BrowserContext* browser_context; |
427 }; | 456 }; |
428 | 457 |
429 } // namespace extensions | 458 } // namespace extensions |
430 | 459 |
431 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 460 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |