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 <unordered_map> | 10 #include <unordered_map> |
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/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/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
19 #include "base/scoped_observer.h" | 19 #include "base/scoped_observer.h" |
20 #include "base/values.h" | 20 #include "base/values.h" |
21 #include "components/keyed_service/core/keyed_service.h" | 21 #include "components/keyed_service/core/keyed_service.h" |
22 #include "content/public/browser/render_process_host_observer.h" | 22 #include "content/public/browser/render_process_host_observer.h" |
23 #include "extensions/browser/event_listener_map.h" | 23 #include "extensions/browser/event_listener_map.h" |
24 #include "extensions/browser/events/lazy_event_dispatch_util.h" | 24 #include "extensions/browser/events/lazy_event_dispatch_util.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/browser/lazy_context_task_queue.h" | |
27 #include "extensions/common/constants.h" | 28 #include "extensions/common/constants.h" |
28 #include "extensions/common/event_filtering_info.h" | 29 #include "extensions/common/event_filtering_info.h" |
29 #include "ipc/ipc_sender.h" | 30 #include "ipc/ipc_sender.h" |
30 #include "url/gurl.h" | 31 #include "url/gurl.h" |
31 | 32 |
32 class GURL; | 33 class GURL; |
33 | 34 |
34 namespace content { | 35 namespace content { |
35 class BrowserContext; | 36 class BrowserContext; |
36 class RenderProcessHost; | 37 class RenderProcessHost; |
37 } | 38 } |
38 | 39 |
39 namespace extensions { | 40 namespace extensions { |
40 class Extension; | 41 class Extension; |
41 class ExtensionHost; | |
42 class ExtensionPrefs; | 42 class ExtensionPrefs; |
43 class ExtensionRegistry; | 43 class ExtensionRegistry; |
44 | 44 |
45 struct Event; | 45 struct Event; |
46 struct EventListenerInfo; | 46 struct EventListenerInfo; |
47 | 47 |
48 // TODO(lazyboy): Document how extension events work, including how listeners | 48 // TODO(lazyboy): Document how extension events work, including how listeners |
49 // are registered and how listeners are tracked in renderer and browser process. | 49 // are registered and how listeners are tracked in renderer and browser process. |
50 class EventRouter : public KeyedService, | 50 class EventRouter : public KeyedService, |
51 public ExtensionRegistryObserver, | 51 public ExtensionRegistryObserver, |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 // | 119 // |
120 // Note that multiple extensions can share a process due to process | 120 // Note that multiple extensions can share a process due to process |
121 // collapsing. Also, a single extension can have 2 processes if it is a split | 121 // collapsing. Also, a single extension can have 2 processes if it is a split |
122 // mode extension. | 122 // mode extension. |
123 void AddEventListener(const std::string& event_name, | 123 void AddEventListener(const std::string& event_name, |
124 content::RenderProcessHost* process, | 124 content::RenderProcessHost* process, |
125 const ExtensionId& extension_id); | 125 const ExtensionId& extension_id); |
126 void AddServiceWorkerEventListener(const std::string& event_name, | 126 void AddServiceWorkerEventListener(const std::string& event_name, |
127 content::RenderProcessHost* process, | 127 content::RenderProcessHost* process, |
128 const ExtensionId& extension_id, | 128 const ExtensionId& extension_id, |
129 const GURL& service_worker_scope, | |
129 int worker_thread_id); | 130 int worker_thread_id); |
130 void RemoveEventListener(const std::string& event_name, | 131 void RemoveEventListener(const std::string& event_name, |
131 content::RenderProcessHost* process, | 132 content::RenderProcessHost* process, |
132 const ExtensionId& extension_id); | 133 const ExtensionId& extension_id); |
133 void RemoveServiceWorkerEventListener(const std::string& event_name, | 134 void RemoveServiceWorkerEventListener(const std::string& event_name, |
134 content::RenderProcessHost* process, | 135 content::RenderProcessHost* process, |
135 const ExtensionId& extension_id, | 136 const ExtensionId& extension_id, |
137 const GURL& service_worker_scope, | |
136 int worker_thread_id); | 138 int worker_thread_id); |
137 | 139 |
138 // Add or remove a URL as an event listener for |event_name|. | 140 // Add or remove a URL as an event listener for |event_name|. |
139 void AddEventListenerForURL(const std::string& event_name, | 141 void AddEventListenerForURL(const std::string& event_name, |
140 content::RenderProcessHost* process, | 142 content::RenderProcessHost* process, |
141 const GURL& listener_url); | 143 const GURL& listener_url); |
142 void RemoveEventListenerForURL(const std::string& event_name, | 144 void RemoveEventListenerForURL(const std::string& event_name, |
143 content::RenderProcessHost* process, | 145 content::RenderProcessHost* process, |
144 const GURL& listener_url); | 146 const GURL& listener_url); |
145 | 147 |
(...skipping 12 matching lines...) Expand all Loading... | |
158 // remembered even after the process goes away. We use this list to decide | 160 // remembered even after the process goes away. We use this list to decide |
159 // which extension pages to load when dispatching an event. | 161 // which extension pages to load when dispatching an event. |
160 void AddLazyEventListener(const std::string& event_name, | 162 void AddLazyEventListener(const std::string& event_name, |
161 const ExtensionId& extension_id); | 163 const ExtensionId& extension_id); |
162 void RemoveLazyEventListener(const std::string& event_name, | 164 void RemoveLazyEventListener(const std::string& event_name, |
163 const ExtensionId& extension_id); | 165 const ExtensionId& extension_id); |
164 // Similar to Add/RemoveLazyEventListener, but applies to extension service | 166 // Similar to Add/RemoveLazyEventListener, but applies to extension service |
165 // workers. | 167 // workers. |
166 void AddLazyServiceWorkerEventListener(const std::string& event_name, | 168 void AddLazyServiceWorkerEventListener(const std::string& event_name, |
167 const ExtensionId& extension_id, | 169 const ExtensionId& extension_id, |
168 int worker_thread_id); | 170 const GURL& service_worker_scope); |
169 void RemoveLazyServiceWorkerEventListener(const std::string& event_name, | 171 void RemoveLazyServiceWorkerEventListener(const std::string& event_name, |
170 const ExtensionId& extension_id, | 172 const ExtensionId& extension_id, |
171 int worker_thread_id); | 173 const GURL& service_worker_scope); |
172 | 174 |
173 // If |add_lazy_listener| is true also add the lazy version of this listener. | 175 // If |add_lazy_listener| is true also add the lazy version of this listener. |
174 void AddFilteredEventListener(const std::string& event_name, | 176 void AddFilteredEventListener(const std::string& event_name, |
175 content::RenderProcessHost* process, | 177 content::RenderProcessHost* process, |
176 const std::string& extension_id, | 178 const std::string& extension_id, |
177 const base::DictionaryValue& filter, | 179 const base::DictionaryValue& filter, |
178 bool add_lazy_listener); | 180 bool add_lazy_listener); |
179 | 181 |
180 // If |remove_lazy_listener| is true also remove the lazy version of this | 182 // If |remove_lazy_listener| is true also remove the lazy version of this |
181 // listener. | 183 // listener. |
(...skipping 85 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
267 const std::set<std::string>& events, | 269 const std::set<std::string>& events, |
268 RegisteredEventType type); | 270 RegisteredEventType type); |
269 | 271 |
270 // ExtensionRegistryObserver implementation. | 272 // ExtensionRegistryObserver implementation. |
271 void OnExtensionLoaded(content::BrowserContext* browser_context, | 273 void OnExtensionLoaded(content::BrowserContext* browser_context, |
272 const Extension* extension) override; | 274 const Extension* extension) override; |
273 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 275 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
274 const Extension* extension, | 276 const Extension* extension, |
275 UnloadedExtensionReason reason) override; | 277 UnloadedExtensionReason reason) override; |
276 | 278 |
277 void AddLazyEventListenerImpl(const std::string& event_name, | 279 void AddLazyEventListenerImpl(std::unique_ptr<EventListener> listener, |
278 const ExtensionId& extension_id, | 280 RegisteredEventType type); |
279 int worker_thread_id); | 281 void RemoveLazyEventListenerImpl(std::unique_ptr<EventListener> listener, |
280 void RemoveLazyEventListenerImpl(const std::string& event_name, | 282 RegisteredEventType type); |
281 const ExtensionId& extension_id, | |
282 int worker_thread_id); | |
283 | 283 |
284 // Shared by all event dispatch methods. If |restrict_to_extension_id| is | 284 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
285 // empty, the event is broadcast. An event that just came off the pending | 285 // empty, the event is broadcast. An event that just came off the pending |
286 // list may not be delayed again. | 286 // list may not be delayed again. |
287 void DispatchEventImpl(const std::string& restrict_to_extension_id, | 287 void DispatchEventImpl(const std::string& restrict_to_extension_id, |
288 const linked_ptr<Event>& event); | 288 const linked_ptr<Event>& event); |
289 | 289 |
290 // Ensures that all lazy background pages that are interested in the given | |
Devlin
2017/06/16 02:34:42
These are gone now, right? Can you rebase?
lazyboy
2017/06/16 18:48:05
Yes, Done. (The CQ was taking long time to submit
| |
291 // event are loaded, and queues the event if the page is not ready yet. | |
292 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy | |
293 // event dispatch that is queued. | |
294 void DispatchLazyEvent(const std::string& extension_id, | |
295 const linked_ptr<Event>& event, | |
296 std::set<EventDispatchIdentifier>* already_dispatched, | |
297 const base::DictionaryValue* listener_filter); | |
298 | |
299 // Dispatches the event to the specified extension or URL running in | 290 // Dispatches the event to the specified extension or URL running in |
300 // |process|. | 291 // |process|. |
301 void DispatchEventToProcess(const std::string& extension_id, | 292 void DispatchEventToProcess(const std::string& extension_id, |
302 const GURL& listener_url, | 293 const GURL& listener_url, |
303 content::RenderProcessHost* process, | 294 content::RenderProcessHost* process, |
304 int worker_thread_id, | 295 int worker_thread_id, |
305 const linked_ptr<Event>& event, | 296 const linked_ptr<Event>& event, |
306 const base::DictionaryValue* listener_filter, | 297 const base::DictionaryValue* listener_filter, |
307 bool did_enqueue); | 298 bool did_enqueue); |
308 | 299 |
309 // Possibly loads given extension's background page in preparation to | |
310 // dispatch an event. Returns true if the event was queued for subsequent | |
311 // dispatch, false otherwise. | |
312 bool MaybeLoadLazyBackgroundPageToDispatchEvent( | |
313 content::BrowserContext* context, | |
314 const Extension* extension, | |
315 const linked_ptr<Event>& event, | |
316 const base::DictionaryValue* listener_filter); | |
317 | |
318 // Adds a filter to an event. | 300 // Adds a filter to an event. |
319 void AddFilterToEvent(const std::string& event_name, | 301 void AddFilterToEvent(const std::string& event_name, |
320 const std::string& extension_id, | 302 const std::string& extension_id, |
321 const base::DictionaryValue* filter); | 303 const base::DictionaryValue* filter); |
322 | 304 |
323 // Removes a filter from an event. | 305 // Removes a filter from an event. |
324 void RemoveFilterFromEvent(const std::string& event_name, | 306 void RemoveFilterFromEvent(const std::string& event_name, |
325 const std::string& extension_id, | 307 const std::string& extension_id, |
326 const base::DictionaryValue* filter); | 308 const base::DictionaryValue* filter); |
327 | 309 |
(...skipping 10 matching lines...) Expand all Loading... | |
338 const std::string& event_name); | 320 const std::string& event_name); |
339 | 321 |
340 // static | 322 // static |
341 static void DoDispatchEventToSenderBookkeepingOnUI( | 323 static void DoDispatchEventToSenderBookkeepingOnUI( |
342 void* browser_context_id, | 324 void* browser_context_id, |
343 const std::string& extension_id, | 325 const std::string& extension_id, |
344 int event_id, | 326 int event_id, |
345 events::HistogramValue histogram_value, | 327 events::HistogramValue histogram_value, |
346 const std::string& event_name); | 328 const std::string& event_name); |
347 | 329 |
348 void DispatchPendingEvent(const linked_ptr<Event>& event, | 330 void DispatchPendingEvent( |
349 ExtensionHost* host); | 331 const linked_ptr<Event>& event, |
332 std::unique_ptr<LazyContextTaskQueue::ContextInfo> params); | |
350 | 333 |
351 // Implementation of EventListenerMap::Delegate. | 334 // Implementation of EventListenerMap::Delegate. |
352 void OnListenerAdded(const EventListener* listener) override; | 335 void OnListenerAdded(const EventListener* listener) override; |
353 void OnListenerRemoved(const EventListener* listener) override; | 336 void OnListenerRemoved(const EventListener* listener) override; |
354 | 337 |
355 // RenderProcessHostObserver implementation. | 338 // RenderProcessHostObserver implementation. |
356 void RenderProcessExited(content::RenderProcessHost* host, | 339 void RenderProcessExited(content::RenderProcessHost* host, |
357 base::TerminationStatus status, | 340 base::TerminationStatus status, |
358 int exit_code) override; | 341 int exit_code) override; |
359 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 342 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
466 const std::string event_name; | 449 const std::string event_name; |
467 | 450 |
468 const std::string extension_id; | 451 const std::string extension_id; |
469 const GURL listener_url; | 452 const GURL listener_url; |
470 content::BrowserContext* const browser_context; | 453 content::BrowserContext* const browser_context; |
471 }; | 454 }; |
472 | 455 |
473 } // namespace extensions | 456 } // namespace extensions |
474 | 457 |
475 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 458 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |