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 <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
15 #include "base/containers/hash_tables.h" | 15 #include "base/containers/hash_tables.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/event_filtering_info.h" | 27 #include "extensions/common/event_filtering_info.h" |
28 #include "ipc/ipc_sender.h" | 28 #include "ipc/ipc_sender.h" |
29 #include "url/gurl.h" | |
29 | 30 |
30 class GURL; | 31 class GURL; |
31 class PrefService; | 32 class PrefService; |
32 | 33 |
33 namespace content { | 34 namespace content { |
34 class BrowserContext; | 35 class BrowserContext; |
35 class RenderProcessHost; | 36 class RenderProcessHost; |
36 } | 37 } |
37 | 38 |
38 namespace extensions { | 39 namespace extensions { |
(...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
78 | 79 |
79 // Gets the EventRouter for |browser_context|. | 80 // Gets the EventRouter for |browser_context|. |
80 static EventRouter* Get(content::BrowserContext* browser_context); | 81 static EventRouter* Get(content::BrowserContext* browser_context); |
81 | 82 |
82 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names | 83 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names |
83 // without a "/" are returned unchanged. | 84 // without a "/" are returned unchanged. |
84 static std::string GetBaseEventName(const std::string& full_event_name); | 85 static std::string GetBaseEventName(const std::string& full_event_name); |
85 | 86 |
86 // Sends an event via ipc_sender to the given extension. Can be called on any | 87 // Sends an event via ipc_sender to the given extension. Can be called on any |
87 // thread. | 88 // thread. |
88 static void DispatchEvent(IPC::Sender* ipc_sender, | 89 // |
89 void* browser_context_id, | 90 // It is very rare to call this function directly. Instead use the instance |
90 const std::string& extension_id, | 91 // methods BroadcastEvent or DispatchEventToExtension. |
91 const std::string& event_name, | 92 static void DispatchEventToSender(IPC::Sender* ipc_sender, |
92 scoped_ptr<base::ListValue> event_args, | 93 void* browser_context_id, |
93 UserGestureState user_gesture, | 94 const GURL& url, |
Devlin
2015/08/07 21:25:47
We can kill the urls, now, right?
not at google - send to devlin
2015/08/10 20:48:33
Good point.
| |
94 const EventFilteringInfo& info); | 95 const std::string& extension_id, |
96 events::HistogramValue histogram_value, | |
97 const std::string& event_name, | |
98 scoped_ptr<base::ListValue> event_args, | |
99 UserGestureState user_gesture, | |
100 const EventFilteringInfo& info); | |
95 | 101 |
96 // An EventRouter is shared between |browser_context| and its associated | 102 // An EventRouter is shared between |browser_context| and its associated |
97 // incognito context. |extension_prefs| may be NULL in tests. | 103 // incognito context. |extension_prefs| may be NULL in tests. |
98 EventRouter(content::BrowserContext* browser_context, | 104 EventRouter(content::BrowserContext* browser_context, |
99 ExtensionPrefs* extension_prefs); | 105 ExtensionPrefs* extension_prefs); |
100 ~EventRouter() override; | 106 ~EventRouter() override; |
101 | 107 |
102 // 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|. |
103 // | 109 // |
104 // Note that multiple extensions can share a process due to process | 110 // Note that multiple extensions can share a process due to process |
(...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
178 // listener for it. NOTE: This should be used rarely, for dispatching events | 184 // listener for it. NOTE: This should be used rarely, for dispatching events |
179 // to extensions that haven't had a chance to add their own listeners yet, eg: | 185 // to extensions that haven't had a chance to add their own listeners yet, eg: |
180 // newly installed extensions. | 186 // newly installed extensions. |
181 void DispatchEventWithLazyListener(const std::string& extension_id, | 187 void DispatchEventWithLazyListener(const std::string& extension_id, |
182 scoped_ptr<Event> event); | 188 scoped_ptr<Event> event); |
183 | 189 |
184 // Record the Event Ack from the renderer. (One less event in-flight.) | 190 // Record the Event Ack from the renderer. (One less event in-flight.) |
185 void OnEventAck(content::BrowserContext* context, | 191 void OnEventAck(content::BrowserContext* context, |
186 const std::string& extension_id); | 192 const std::string& extension_id); |
187 | 193 |
194 // Reports UMA for an event dispatched to |extension| with histogram value | |
195 // |histogram_value|. Must be called on the UI thread. | |
196 // | |
197 // |did_enqueue| should be true if the event was queued waiting for a process | |
198 // to start, like an event page. | |
199 void ReportEvent(events::HistogramValue histogram_value, | |
200 const Extension* extension, | |
201 bool did_enqueue); | |
202 | |
188 private: | 203 private: |
189 friend class EventRouterTest; | 204 friend class EventRouterTest; |
190 | 205 |
191 // The extension and process that contains the event listener for a given | 206 // The extension and process that contains the event listener for a given |
192 // event. | 207 // event. |
193 struct ListenerProcess; | 208 struct ListenerProcess; |
194 | 209 |
195 // A map between an event name and a set of extensions that are listening | 210 // A map between an event name and a set of extensions that are listening |
196 // to that event. | 211 // to that event. |
197 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; | 212 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
(...skipping 24 matching lines...) Expand all Loading... | |
222 const Extension* extension, | 237 const Extension* extension, |
223 UnloadedExtensionInfo::Reason reason) override; | 238 UnloadedExtensionInfo::Reason reason) override; |
224 | 239 |
225 // Returns true if the given listener map contains a event listeners for | 240 // Returns true if the given listener map contains a event listeners for |
226 // the given event. If |extension_id| is non-empty, we also check that that | 241 // the given event. If |extension_id| is non-empty, we also check that that |
227 // extension is one of the listeners. | 242 // extension is one of the listeners. |
228 bool HasEventListenerImpl(const ListenerMap& listeners, | 243 bool HasEventListenerImpl(const ListenerMap& listeners, |
229 const std::string& extension_id, | 244 const std::string& extension_id, |
230 const std::string& event_name); | 245 const std::string& event_name); |
231 | 246 |
232 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the | 247 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
233 // event is broadcast. | 248 // empty, the event is broadcast. An event that just came off the pending |
234 // An event that just came off the pending list may not be delayed again. | 249 // list may not be delayed again. |
235 void DispatchEventImpl(const std::string& restrict_to_extension_id, | 250 void DispatchEventImpl(const std::string& restrict_to_extension_id, |
236 const linked_ptr<Event>& event); | 251 const linked_ptr<Event>& event); |
237 | 252 |
238 // Ensures that all lazy background pages that are interested in the given | 253 // Ensures that all lazy background pages that are interested in the given |
239 // event are loaded, and queues the event if the page is not ready yet. | 254 // event are loaded, and queues the event if the page is not ready yet. |
240 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy | 255 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy |
241 // event dispatch that is queued. | 256 // event dispatch that is queued. |
242 void DispatchLazyEvent(const std::string& extension_id, | 257 void DispatchLazyEvent(const std::string& extension_id, |
243 const linked_ptr<Event>& event, | 258 const linked_ptr<Event>& event, |
244 std::set<EventDispatchIdentifier>* already_dispatched, | 259 std::set<EventDispatchIdentifier>* already_dispatched, |
245 const base::DictionaryValue* listener_filter); | 260 const base::DictionaryValue* listener_filter); |
246 | 261 |
247 // Dispatches the event to the specified extension or URL running in | 262 // Dispatches the event to the specified extension or URL running in |
248 // |process|. | 263 // |process|. |
249 void DispatchEventToProcess(const std::string& extension_id, | 264 void DispatchEventToProcess(const std::string& extension_id, |
250 const GURL& listener_url, | 265 const GURL& listener_url, |
251 content::RenderProcessHost* process, | 266 content::RenderProcessHost* process, |
252 const linked_ptr<Event>& event, | 267 const linked_ptr<Event>& event, |
253 const base::DictionaryValue* listener_filter); | 268 const base::DictionaryValue* listener_filter, |
269 bool did_enqueue); | |
254 | 270 |
255 // Returns false when the event is scoped to a context and the listening | 271 // Returns false when the event is scoped to a context and the listening |
256 // extension does not have access to events from that context. Also fills | 272 // extension does not have access to events from that context. Also fills |
257 // |event_args| with the proper arguments to send, which may differ if | 273 // |event_args| with the proper arguments to send, which may differ if |
258 // the event crosses the incognito boundary. | 274 // the event crosses the incognito boundary. |
259 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, | 275 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, |
260 const Extension* extension, | 276 const Extension* extension, |
261 const linked_ptr<Event>& event); | 277 const linked_ptr<Event>& event); |
262 | 278 |
263 // Possibly loads given extension's background page in preparation to | 279 // Possibly loads given extension's background page in preparation to |
(...skipping 21 matching lines...) Expand all Loading... | |
285 const std::string& extension_id); | 301 const std::string& extension_id); |
286 | 302 |
287 // Track the dispatched events that have not yet sent an ACK from the | 303 // Track the dispatched events that have not yet sent an ACK from the |
288 // renderer. | 304 // renderer. |
289 void IncrementInFlightEvents(content::BrowserContext* context, | 305 void IncrementInFlightEvents(content::BrowserContext* context, |
290 const Extension* extension, | 306 const Extension* extension, |
291 int event_id, | 307 int event_id, |
292 const std::string& event_name); | 308 const std::string& event_name); |
293 | 309 |
294 // static | 310 // static |
295 static void IncrementInFlightEventsOnUI(void* browser_context_id, | 311 static void DoDispatchEventToSenderBookkeepingOnUI( |
296 const std::string& extension_id, | 312 void* browser_context_id, |
297 int event_id, | 313 const std::string& extension_id, |
298 const std::string& event_name); | 314 const GURL& url, |
315 int event_id, | |
316 events::HistogramValue histogram_value, | |
317 const std::string& event_name); | |
299 | 318 |
300 void DispatchPendingEvent(const linked_ptr<Event>& event, | 319 void DispatchPendingEvent(const linked_ptr<Event>& event, |
301 ExtensionHost* host); | 320 ExtensionHost* host); |
302 | 321 |
303 // Implementation of EventListenerMap::Delegate. | 322 // Implementation of EventListenerMap::Delegate. |
304 void OnListenerAdded(const EventListener* listener) override; | 323 void OnListenerAdded(const EventListener* listener) override; |
305 void OnListenerRemoved(const EventListener* listener) override; | 324 void OnListenerRemoved(const EventListener* listener) override; |
306 | 325 |
307 // RenderProcessHostObserver implementation. | 326 // RenderProcessHostObserver implementation. |
308 void RenderProcessExited(content::RenderProcessHost* host, | 327 void RenderProcessExited(content::RenderProcessHost* host, |
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
411 const std::string event_name; | 430 const std::string event_name; |
412 | 431 |
413 const std::string extension_id; | 432 const std::string extension_id; |
414 const GURL listener_url; | 433 const GURL listener_url; |
415 content::BrowserContext* browser_context; | 434 content::BrowserContext* browser_context; |
416 }; | 435 }; |
417 | 436 |
418 } // namespace extensions | 437 } // namespace extensions |
419 | 438 |
420 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 439 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |