| Index: extensions/browser/event_router.h
|
| diff --git a/extensions/browser/event_router.h b/extensions/browser/event_router.h
|
| index fc8d3efa3bbdc68c9d076844c5f1bb2ba96bccfe..8cc397ced1fe22895e19d5a55c15e0f6152eca27 100644
|
| --- a/extensions/browser/event_router.h
|
| +++ b/extensions/browser/event_router.h
|
| @@ -23,6 +23,7 @@
|
| #include "extensions/browser/events/lazy_event_dispatch_util.h"
|
| #include "extensions/browser/extension_event_histogram_value.h"
|
| #include "extensions/browser/extension_registry_observer.h"
|
| +#include "extensions/common/constants.h"
|
| #include "extensions/common/event_filtering_info.h"
|
| #include "ipc/ipc_sender.h"
|
| #include "url/gurl.h"
|
| @@ -43,6 +44,8 @@ class ExtensionRegistry;
|
| struct Event;
|
| struct EventListenerInfo;
|
|
|
| +// TODO(lazyboy): Document how extension events work, including how listeners
|
| +// are registered and how listeners are tracked in renderer and browser process.
|
| class EventRouter : public KeyedService,
|
| public ExtensionRegistryObserver,
|
| public EventListenerMap::Delegate,
|
| @@ -58,7 +61,10 @@ class EventRouter : public KeyedService,
|
|
|
| // The pref key for the list of event names for which an extension has
|
| // registered from its lazy background page.
|
| - static const char kRegisteredEvents[];
|
| + static const char kRegisteredLazyEvents[];
|
| + // The pref key for the list of event names for which an extension has
|
| + // registered from its service worker.
|
| + static const char kRegisteredServiceWorkerEvents[];
|
|
|
| // Observers register interest in events with a particular name and are
|
| // notified when a listener is added or removed. Observers are matched by
|
| @@ -109,10 +115,18 @@ class EventRouter : public KeyedService,
|
| // mode extension.
|
| void AddEventListener(const std::string& event_name,
|
| content::RenderProcessHost* process,
|
| - const std::string& extension_id);
|
| + const ExtensionId& extension_id);
|
| + void AddServiceWorkerEventListener(const std::string& event_name,
|
| + content::RenderProcessHost* process,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
| void RemoveEventListener(const std::string& event_name,
|
| content::RenderProcessHost* process,
|
| - const std::string& extension_id);
|
| + const ExtensionId& extension_id);
|
| + void RemoveServiceWorkerEventListener(const std::string& event_name,
|
| + content::RenderProcessHost* process,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
|
|
| // Add or remove a URL as an event listener for |event_name|.
|
| void AddEventListenerForURL(const std::string& event_name,
|
| @@ -137,9 +151,17 @@ class EventRouter : public KeyedService,
|
| // remembered even after the process goes away. We use this list to decide
|
| // which extension pages to load when dispatching an event.
|
| void AddLazyEventListener(const std::string& event_name,
|
| - const std::string& extension_id);
|
| + const ExtensionId& extension_id);
|
| void RemoveLazyEventListener(const std::string& event_name,
|
| - const std::string& extension_id);
|
| + const ExtensionId& extension_id);
|
| + // Similar to Add/RemoveLazyEventListener, but applies to extension service
|
| + // workers.
|
| + void AddLazyServiceWorkerEventListener(const std::string& event_name,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
| + void RemoveLazyServiceWorkerEventListener(const std::string& event_name,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
|
|
| // If |add_lazy_listener| is true also add the lazy version of this listener.
|
| void AddFilteredEventListener(const std::string& event_name,
|
| @@ -184,13 +206,12 @@ class EventRouter : public KeyedService,
|
|
|
| // Returns whether or not the given extension has any registered events.
|
| bool HasRegisteredEvents(const ExtensionId& extension_id) const {
|
| - return !GetRegisteredEvents(extension_id).empty();
|
| + return !GetRegisteredEvents(extension_id, RegisteredEventType::kLazy)
|
| + .empty();
|
| }
|
|
|
| // Clears registered events for testing purposes.
|
| - void ClearRegisteredEventsForTest(const ExtensionId& extension_id) {
|
| - SetRegisteredEvents(extension_id, std::set<std::string>());
|
| - }
|
| + void ClearRegisteredEventsForTest(const ExtensionId& extension_id);
|
|
|
| // Reports UMA for an event dispatched to |extension| with histogram value
|
| // |histogram_value|. Must be called on the UI thread.
|
| @@ -209,14 +230,20 @@ class EventRouter : public KeyedService,
|
| friend class EventRouterFilterTest;
|
| friend class EventRouterTest;
|
|
|
| + enum class RegisteredEventType {
|
| + kLazy,
|
| + kServiceWorker,
|
| + };
|
| +
|
| // An identifier for an event dispatch that is used to prevent double dispatch
|
| // due to race conditions between the direct and lazy dispatch paths.
|
| - typedef std::pair<const content::BrowserContext*, std::string>
|
| + typedef std::tuple<const content::BrowserContext*, std::string, int>
|
| EventDispatchIdentifier;
|
|
|
| // TODO(gdk): Document this.
|
| static void DispatchExtensionMessage(
|
| IPC::Sender* ipc_sender,
|
| + int worker_thread_id,
|
| void* browser_context_id,
|
| const std::string& extension_id,
|
| int event_id,
|
| @@ -227,10 +254,11 @@ class EventRouter : public KeyedService,
|
|
|
| // Returns or sets the list of events for which the given extension has
|
| // registered.
|
| - std::set<std::string> GetRegisteredEvents(
|
| - const std::string& extension_id) const;
|
| + std::set<std::string> GetRegisteredEvents(const std::string& extension_id,
|
| + RegisteredEventType type) const;
|
| void SetRegisteredEvents(const std::string& extension_id,
|
| - const std::set<std::string>& events);
|
| + const std::set<std::string>& events,
|
| + RegisteredEventType type);
|
|
|
| // ExtensionRegistryObserver implementation.
|
| void OnExtensionLoaded(content::BrowserContext* browser_context,
|
| @@ -239,6 +267,13 @@ class EventRouter : public KeyedService,
|
| const Extension* extension,
|
| UnloadedExtensionReason reason) override;
|
|
|
| + void AddLazyEventListenerImpl(const std::string& event_name,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
| + void RemoveLazyEventListenerImpl(const std::string& event_name,
|
| + const ExtensionId& extension_id,
|
| + int worker_thread_id);
|
| +
|
| // Shared by all event dispatch methods. If |restrict_to_extension_id| is
|
| // empty, the event is broadcast. An event that just came off the pending
|
| // list may not be delayed again.
|
| @@ -259,6 +294,7 @@ class EventRouter : public KeyedService,
|
| void DispatchEventToProcess(const std::string& extension_id,
|
| const GURL& listener_url,
|
| content::RenderProcessHost* process,
|
| + int worker_thread_id,
|
| const linked_ptr<Event>& event,
|
| const base::DictionaryValue* listener_filter,
|
| bool did_enqueue);
|
|
|