blob: 1de4c1a03eea29eaaff8cb2e164de9e6f11928b8 [file] [log] [blame]
[email protected]9e6720a2012-01-24 02:30:561// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]2c699652010-10-15 18:22:412// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]34423532013-11-21 18:13:105#ifndef EXTENSIONS_BROWSER_EVENT_ROUTER_H_
6#define EXTENSIONS_BROWSER_EVENT_ROUTER_H_
[email protected]2c699652010-10-15 18:22:417
[email protected]2c699652010-10-15 18:22:418#include <set>
9#include <string>
thestig7ade5b52017-05-23 23:13:3610#include <unordered_map>
[email protected]2c699652010-10-15 18:22:4111
[email protected]6e850922012-12-05 03:22:4812#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5413#include "base/compiler_specific.h"
avic9cec102015-12-23 00:39:2614#include "base/macros.h"
[email protected]3b63f8f42011-03-28 01:54:1515#include "base/memory/ref_counted.h"
lazyboye464732f2017-06-15 21:17:2716#include "base/memory/weak_ptr.h"
Devlin Croninffbd2fe22018-07-20 17:20:1717#include "base/observer_list.h"
[email protected]4243f9a2014-08-04 18:53:1218#include "base/scoped_observer.h"
[email protected]8a16a032012-06-18 19:37:3119#include "base/values.h"
juncaicf523332015-06-04 00:14:0420#include "components/keyed_service/core/keyed_service.h"
amistry69e9ee422015-05-22 07:40:2521#include "content/public/browser/render_process_host_observer.h"
[email protected]a0e26d42013-11-20 07:04:0422#include "extensions/browser/event_listener_map.h"
Istiaque Ahmeda14ec482018-08-25 01:02:1823#include "extensions/browser/events/event_ack_data.h"
lazyboy75b9def2017-06-06 18:56:5924#include "extensions/browser/events/lazy_event_dispatch_util.h"
kalmanf1b4d782015-06-24 21:14:0525#include "extensions/browser/extension_event_histogram_value.h"
[email protected]4243f9a2014-08-04 18:53:1226#include "extensions/browser/extension_registry_observer.h"
lazyboy63b994a2017-06-30 21:20:2327#include "extensions/browser/lazy_context_task_queue.h"
lazyboye7847242017-06-07 23:29:1828#include "extensions/common/constants.h"
[email protected]4b3e1922013-02-12 04:45:5829#include "extensions/common/event_filtering_info.h"
[email protected]b44f8ad2012-06-15 20:52:5830#include "ipc/ipc_sender.h"
kalmana9f6e672015-08-11 00:22:5031#include "url/gurl.h"
[email protected]2c699652010-10-15 18:22:4132
33class GURL;
Istiaque Ahmed9d1666182017-09-21 23:58:1834struct ServiceWorkerIdentifier;
[email protected]f3b1a082011-11-18 00:34:3035
36namespace content {
[email protected]7061be92013-02-18 15:44:0237class BrowserContext;
[email protected]2c699652010-10-15 18:22:4138class RenderProcessHost;
[email protected]f3b1a082011-11-18 00:34:3039}
[email protected]2c699652010-10-15 18:22:4140
[email protected]1c321ee2012-05-21 03:02:3441namespace extensions {
42class Extension;
[email protected]79cb81bb2012-09-20 02:23:3143class ExtensionPrefs;
[email protected]4243f9a2014-08-04 18:53:1244class ExtensionRegistry;
[email protected]3a1dc572012-07-31 22:25:1345
[email protected]5a38dfd2012-07-23 23:22:1046struct Event;
[email protected]954e13492012-11-15 03:18:2347struct EventListenerInfo;
[email protected]1c321ee2012-05-21 03:02:3448
lazyboye7847242017-06-07 23:29:1849// TODO(lazyboy): Document how extension events work, including how listeners
50// are registered and how listeners are tracked in renderer and browser process.
juncaicf523332015-06-04 00:14:0451class EventRouter : public KeyedService,
[email protected]4243f9a2014-08-04 18:53:1252 public ExtensionRegistryObserver,
amistry69e9ee422015-05-22 07:40:2553 public EventListenerMap::Delegate,
54 public content::RenderProcessHostObserver {
[email protected]2c699652010-10-15 18:22:4155 public:
[email protected]b085856f2012-03-02 04:37:2556 // These constants convey the state of our knowledge of whether we're in
57 // a user-caused gesture as part of DispatchEvent.
58 enum UserGestureState {
59 USER_GESTURE_UNKNOWN = 0,
60 USER_GESTURE_ENABLED = 1,
61 USER_GESTURE_NOT_ENABLED = 2,
62 };
63
[email protected]e74d43c72013-05-17 19:01:4164 // The pref key for the list of event names for which an extension has
65 // registered from its lazy background page.
lazyboye7847242017-06-07 23:29:1866 static const char kRegisteredLazyEvents[];
67 // The pref key for the list of event names for which an extension has
68 // registered from its service worker.
69 static const char kRegisteredServiceWorkerEvents[];
[email protected]e74d43c72013-05-17 19:01:4170
[email protected]c4dc5cc2012-11-09 08:48:3971 // Observers register interest in events with a particular name and are
[email protected]c761a962013-11-20 04:19:4172 // notified when a listener is added or removed. Observers are matched by
73 // the base name of the event (e.g. adding an event listener for event name
74 // "foo.onBar/123" will trigger observers registered for "foo.onBar").
[email protected]c4dc5cc2012-11-09 08:48:3975 class Observer {
76 public:
77 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2378 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3979 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2380 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
thestig7ade5b52017-05-23 23:13:3681
82 protected:
83 virtual ~Observer() {}
[email protected]c4dc5cc2012-11-09 08:48:3984 };
85
Devlin Croninffbd2fe22018-07-20 17:20:1786 // A test observer to monitor event dispatching.
87 class TestObserver {
88 public:
89 virtual ~TestObserver() = default;
90 virtual void OnWillDispatchEvent(const Event& event) = 0;
Ramin Halavatia4870222018-07-31 05:41:0491 virtual void OnDidDispatchEventToProcess(const Event& event) = 0;
Devlin Croninffbd2fe22018-07-20 17:20:1792 };
93
[email protected]3a368a22014-03-26 19:29:1994 // Gets the EventRouter for |browser_context|.
[email protected]3a368a22014-03-26 19:29:1995 static EventRouter* Get(content::BrowserContext* browser_context);
96
[email protected]c761a962013-11-20 04:19:4197 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names
98 // without a "/" are returned unchanged.
99 static std::string GetBaseEventName(const std::string& full_event_name);
100
[email protected]c9bd90f2012-08-07 23:58:15101 // Sends an event via ipc_sender to the given extension. Can be called on any
102 // thread.
kalmana9f6e672015-08-11 00:22:50103 //
104 // It is very rare to call this function directly. Instead use the instance
105 // methods BroadcastEvent or DispatchEventToExtension.
106 static void DispatchEventToSender(IPC::Sender* ipc_sender,
107 void* browser_context_id,
108 const std::string& extension_id,
109 events::HistogramValue histogram_value,
110 const std::string& event_name,
dchengf5d241082016-04-21 03:43:11111 std::unique_ptr<base::ListValue> event_args,
kalmana9f6e672015-08-11 00:22:50112 UserGestureState user_gesture,
113 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:19114
lazyboye464732f2017-06-15 21:17:27115 // Returns false when the event is scoped to a context and the listening
116 // extension does not have access to events from that context.
117 static bool CanDispatchEventToBrowserContext(content::BrowserContext* context,
118 const Extension* extension,
119 const Event& event);
120
[email protected]45fd94172013-11-13 03:29:52121 // An EventRouter is shared between |browser_context| and its associated
122 // incognito context. |extension_prefs| may be NULL in tests.
123 EventRouter(content::BrowserContext* browser_context,
124 ExtensionPrefs* extension_prefs);
dcheng9168b2f2014-10-21 12:38:24125 ~EventRouter() override;
[email protected]2c699652010-10-15 18:22:41126
[email protected]c1abb3232014-07-30 18:28:39127 // Add or remove an extension as an event listener for |event_name|.
128 //
[email protected]a7ab1b782010-10-21 23:24:16129 // Note that multiple extensions can share a process due to process
130 // collapsing. Also, a single extension can have 2 processes if it is a split
131 // mode extension.
[email protected]2c699652010-10-15 18:22:41132 void AddEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30133 content::RenderProcessHost* process,
lazyboye7847242017-06-07 23:29:18134 const ExtensionId& extension_id);
135 void AddServiceWorkerEventListener(const std::string& event_name,
136 content::RenderProcessHost* process,
137 const ExtensionId& extension_id,
lazyboy63b994a2017-06-30 21:20:23138 const GURL& service_worker_scope,
Istiaque Ahmeda14ec482018-08-25 01:02:18139 int64_t service_worker_version_id,
lazyboye7847242017-06-07 23:29:18140 int worker_thread_id);
[email protected]2c699652010-10-15 18:22:41141 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30142 content::RenderProcessHost* process,
lazyboye7847242017-06-07 23:29:18143 const ExtensionId& extension_id);
144 void RemoveServiceWorkerEventListener(const std::string& event_name,
145 content::RenderProcessHost* process,
146 const ExtensionId& extension_id,
lazyboy63b994a2017-06-30 21:20:23147 const GURL& service_worker_scope,
Istiaque Ahmeda14ec482018-08-25 01:02:18148 int64_t service_worker_version_id,
lazyboye7847242017-06-07 23:29:18149 int worker_thread_id);
[email protected]2c699652010-10-15 18:22:41150
[email protected]c1abb3232014-07-30 18:28:39151 // Add or remove a URL as an event listener for |event_name|.
152 void AddEventListenerForURL(const std::string& event_name,
153 content::RenderProcessHost* process,
154 const GURL& listener_url);
155 void RemoveEventListenerForURL(const std::string& event_name,
156 content::RenderProcessHost* process,
157 const GURL& listener_url);
158
[email protected]f34706b2012-09-04 07:32:09159 EventListenerMap& listeners() { return listeners_; }
160
[email protected]c4dc5cc2012-11-09 08:48:39161 // Registers an observer to be notified when an event listener for
162 // |event_name| is added or removed. There can currently be only one observer
163 // for each distinct |event_name|.
thestig7ade5b52017-05-23 23:13:36164 void RegisterObserver(Observer* observer, const std::string& event_name);
[email protected]c4dc5cc2012-11-09 08:48:39165
166 // Unregisters an observer from all events.
167 void UnregisterObserver(Observer* observer);
168
Devlin Croninffbd2fe22018-07-20 17:20:17169 // Adds/removes test observers.
170 void AddObserverForTesting(TestObserver* observer);
171 void RemoveObserverForTesting(TestObserver* observer);
172
[email protected]36531222012-02-07 19:41:27173 // Add or remove the extension as having a lazy background page that listens
174 // to the event. The difference from the above methods is that these will be
175 // remembered even after the process goes away. We use this list to decide
176 // which extension pages to load when dispatching an event.
177 void AddLazyEventListener(const std::string& event_name,
lazyboye7847242017-06-07 23:29:18178 const ExtensionId& extension_id);
[email protected]36531222012-02-07 19:41:27179 void RemoveLazyEventListener(const std::string& event_name,
lazyboye7847242017-06-07 23:29:18180 const ExtensionId& extension_id);
181 // Similar to Add/RemoveLazyEventListener, but applies to extension service
182 // workers.
183 void AddLazyServiceWorkerEventListener(const std::string& event_name,
184 const ExtensionId& extension_id,
lazyboy63b994a2017-06-30 21:20:23185 const GURL& service_worker_scope);
lazyboye7847242017-06-07 23:29:18186 void RemoveLazyServiceWorkerEventListener(const std::string& event_name,
187 const ExtensionId& extension_id,
lazyboy63b994a2017-06-30 21:20:23188 const GURL& service_worker_scope);
[email protected]36531222012-02-07 19:41:27189
[email protected]d9e559d2012-07-05 01:04:57190 // If |add_lazy_listener| is true also add the lazy version of this listener.
Istiaque Ahmed9d1666182017-09-21 23:58:18191 void AddFilteredEventListener(
192 const std::string& event_name,
193 content::RenderProcessHost* process,
194 const std::string& extension_id,
195 base::Optional<ServiceWorkerIdentifier> sw_identifier,
196 const base::DictionaryValue& filter,
197 bool add_lazy_listener);
[email protected]d9e559d2012-07-05 01:04:57198
199 // If |remove_lazy_listener| is true also remove the lazy version of this
200 // listener.
Istiaque Ahmed9d1666182017-09-21 23:58:18201 void RemoveFilteredEventListener(
202 const std::string& event_name,
203 content::RenderProcessHost* process,
204 const std::string& extension_id,
205 base::Optional<ServiceWorkerIdentifier> sw_identifier,
206 const base::DictionaryValue& filter,
207 bool remove_lazy_listener);
[email protected]d9e559d2012-07-05 01:04:57208
[email protected]2c699652010-10-15 18:22:41209 // Returns true if there is at least one listener for the given event.
thestig7ade5b52017-05-23 23:13:36210 bool HasEventListener(const std::string& event_name) const;
[email protected]2c699652010-10-15 18:22:41211
[email protected]a7ab1b782010-10-21 23:24:16212 // Returns true if the extension is listening to the given event.
thestig7ade5b52017-05-23 23:13:36213 // (virtual for testing only.)
asargentd50b18c2016-04-21 01:17:16214 virtual bool ExtensionHasEventListener(const std::string& extension_id,
thestig7ade5b52017-05-23 23:13:36215 const std::string& event_name) const;
[email protected]a7ab1b782010-10-21 23:24:16216
[email protected]6e850922012-12-05 03:22:48217 // Broadcasts an event to every listener registered for that event.
dchengf5d241082016-04-21 03:43:11218 virtual void BroadcastEvent(std::unique_ptr<Event> event);
[email protected]6e850922012-12-05 03:22:48219
220 // Dispatches an event to the given extension.
221 virtual void DispatchEventToExtension(const std::string& extension_id,
dchengf5d241082016-04-21 03:43:11222 std::unique_ptr<Event> event);
[email protected]6e850922012-12-05 03:22:48223
[email protected]42d24742013-07-23 05:25:55224 // Dispatches |event| to the given extension as if the extension has a lazy
225 // listener for it. NOTE: This should be used rarely, for dispatching events
226 // to extensions that haven't had a chance to add their own listeners yet, eg:
227 // newly installed extensions.
228 void DispatchEventWithLazyListener(const std::string& extension_id,
dchengf5d241082016-04-21 03:43:11229 std::unique_ptr<Event> event);
[email protected]42d24742013-07-23 05:25:55230
[email protected]89102012011-11-01 21:23:56231 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]45fd94172013-11-13 03:29:52232 void OnEventAck(content::BrowserContext* context,
David Bertoni3e1e9fa2018-08-29 20:39:30233 const std::string& extension_id,
234 const std::string& event_name);
[email protected]89102012011-11-01 21:23:56235
lazyboyac968912017-05-16 17:50:09236 // Returns whether or not the given extension has any registered events.
Istiaque Ahmed805f6a83b2017-10-05 01:23:26237 bool HasRegisteredEvents(const ExtensionId& extension_id) const;
lazyboyac968912017-05-16 17:50:09238
239 // Clears registered events for testing purposes.
lazyboye7847242017-06-07 23:29:18240 void ClearRegisteredEventsForTest(const ExtensionId& extension_id);
lazyboyac968912017-05-16 17:50:09241
kalmana9f6e672015-08-11 00:22:50242 // Reports UMA for an event dispatched to |extension| with histogram value
243 // |histogram_value|. Must be called on the UI thread.
244 //
245 // |did_enqueue| should be true if the event was queued waiting for a process
246 // to start, like an event page.
247 void ReportEvent(events::HistogramValue histogram_value,
248 const Extension* extension,
249 bool did_enqueue);
250
lazyboy75b9def2017-06-06 18:56:59251 LazyEventDispatchUtil* lazy_event_dispatch_util() {
252 return &lazy_event_dispatch_util_;
253 }
254
Istiaque Ahmeda14ec482018-08-25 01:02:18255 EventAckData* event_ack_data() { return &event_ack_data_; }
256
Devlin Cronin6e1ee262017-10-05 01:38:34257 // Returns true if there is a registered lazy/non-lazy listener for the given
rdevlin.cronin91f0c8a32017-07-19 21:26:33258 // |event_name|.
259 bool HasLazyEventListenerForTesting(const std::string& event_name);
Devlin Cronin6e1ee262017-10-05 01:38:34260 bool HasNonLazyEventListenerForTesting(const std::string& event_name);
rdevlin.cronin91f0c8a32017-07-19 21:26:33261
[email protected]fb6ff23b2012-03-13 23:13:42262 private:
lazyboy348e5ca2016-12-05 21:43:29263 friend class EventRouterFilterTest;
[email protected]c1abb3232014-07-30 18:28:39264 friend class EventRouterTest;
[email protected]c761a962013-11-20 04:19:41265
lazyboye7847242017-06-07 23:29:18266 enum class RegisteredEventType {
267 kLazy,
268 kServiceWorker,
269 };
270
[email protected]c9bd90f2012-08-07 23:58:15271 // TODO(gdk): Document this.
272 static void DispatchExtensionMessage(
273 IPC::Sender* ipc_sender,
lazyboye7847242017-06-07 23:29:18274 int worker_thread_id,
[email protected]513b8032013-11-18 07:47:49275 void* browser_context_id,
[email protected]c9bd90f2012-08-07 23:58:15276 const std::string& extension_id,
chirantan669993c2015-03-05 23:38:33277 int event_id,
[email protected]c9bd90f2012-08-07 23:58:15278 const std::string& event_name,
279 base::ListValue* event_args,
[email protected]c9bd90f2012-08-07 23:58:15280 UserGestureState user_gesture,
281 const extensions::EventFilteringInfo& info);
282
lazyboyac968912017-05-16 17:50:09283 // Returns or sets the list of events for which the given extension has
284 // registered.
lazyboye7847242017-06-07 23:29:18285 std::set<std::string> GetRegisteredEvents(const std::string& extension_id,
286 RegisteredEventType type) const;
lazyboyac968912017-05-16 17:50:09287 void SetRegisteredEvents(const std::string& extension_id,
lazyboye7847242017-06-07 23:29:18288 const std::set<std::string>& events,
289 RegisteredEventType type);
lazyboyac968912017-05-16 17:50:09290
[email protected]4243f9a2014-08-04 18:53:12291 // ExtensionRegistryObserver implementation.
dcheng9168b2f2014-10-21 12:38:24292 void OnExtensionLoaded(content::BrowserContext* browser_context,
293 const Extension* extension) override;
294 void OnExtensionUnloaded(content::BrowserContext* browser_context,
295 const Extension* extension,
limasdf0deef2042017-05-03 19:17:17296 UnloadedExtensionReason reason) override;
[email protected]2c699652010-10-15 18:22:41297
lazyboy63b994a2017-06-30 21:20:23298 void AddLazyEventListenerImpl(std::unique_ptr<EventListener> listener,
299 RegisteredEventType type);
300 void RemoveLazyEventListenerImpl(std::unique_ptr<EventListener> listener,
301 RegisteredEventType type);
lazyboye7847242017-06-07 23:29:18302
kalmana9f6e672015-08-11 00:22:50303 // Shared by all event dispatch methods. If |restrict_to_extension_id| is
304 // empty, the event is broadcast. An event that just came off the pending
305 // list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57306 void DispatchEventImpl(const std::string& restrict_to_extension_id,
Devlin Cronin6db70572018-12-21 23:23:24307 std::unique_ptr<Event> event);
[email protected]fb6ff23b2012-03-13 23:13:42308
[email protected]c1abb3232014-07-30 18:28:39309 // Dispatches the event to the specified extension or URL running in
310 // |process|.
[email protected]d9e559d2012-07-05 01:04:57311 void DispatchEventToProcess(const std::string& extension_id,
[email protected]c1abb3232014-07-30 18:28:39312 const GURL& listener_url,
[email protected]d9e559d2012-07-05 01:04:57313 content::RenderProcessHost* process,
Istiaque Ahmeda14ec482018-08-25 01:02:18314 int64_t service_worker_version_id,
lazyboye7847242017-06-07 23:29:18315 int worker_thread_id,
Devlin Cronin6db70572018-12-21 23:23:24316 Event* event,
kalmana9f6e672015-08-11 00:22:50317 const base::DictionaryValue* listener_filter,
318 bool did_enqueue);
[email protected]fb6ff23b2012-03-13 23:13:42319
[email protected]e74d43c72013-05-17 19:01:41320 // Adds a filter to an event.
321 void AddFilterToEvent(const std::string& event_name,
322 const std::string& extension_id,
Istiaque Ahmed9ce21b32017-10-10 20:43:18323 bool is_for_service_worker,
[email protected]e74d43c72013-05-17 19:01:41324 const base::DictionaryValue* filter);
325
326 // Removes a filter from an event.
327 void RemoveFilterFromEvent(const std::string& event_name,
328 const std::string& extension_id,
Istiaque Ahmed9ce21b32017-10-10 20:43:18329 bool is_for_service_worker,
[email protected]e74d43c72013-05-17 19:01:41330 const base::DictionaryValue* filter);
331
332 // Returns the dictionary of event filters that the given extension has
333 // registered.
334 const base::DictionaryValue* GetFilteredEvents(
Istiaque Ahmed9ce21b32017-10-10 20:43:18335 const std::string& extension_id,
336 RegisteredEventType type);
[email protected]e74d43c72013-05-17 19:01:41337
chirantan669993c2015-03-05 23:38:33338 // Track the dispatched events that have not yet sent an ACK from the
339 // renderer.
[email protected]45fd94172013-11-13 03:29:52340 void IncrementInFlightEvents(content::BrowserContext* context,
chirantan669993c2015-03-05 23:38:33341 const Extension* extension,
342 int event_id,
343 const std::string& event_name);
[email protected]fb6ff23b2012-03-13 23:13:42344
[email protected]db9f2142013-05-27 22:56:16345 // static
kalmana9f6e672015-08-11 00:22:50346 static void DoDispatchEventToSenderBookkeepingOnUI(
347 void* browser_context_id,
348 const std::string& extension_id,
349 int event_id,
350 events::HistogramValue histogram_value,
351 const std::string& event_name);
[email protected]db9f2142013-05-27 22:56:16352
lazyboy63b994a2017-06-30 21:20:23353 void DispatchPendingEvent(
Devlin Cronin6db70572018-12-21 23:23:24354 std::unique_ptr<Event> event,
lazyboy63b994a2017-06-30 21:20:23355 std::unique_ptr<LazyContextTaskQueue::ContextInfo> params);
[email protected]fb6ff23b2012-03-13 23:13:42356
[email protected]5a38dfd2012-07-23 23:22:10357 // Implementation of EventListenerMap::Delegate.
dcheng9168b2f2014-10-21 12:38:24358 void OnListenerAdded(const EventListener* listener) override;
359 void OnListenerRemoved(const EventListener* listener) override;
[email protected]d9e559d2012-07-05 01:04:57360
amistry69e9ee422015-05-22 07:40:25361 // RenderProcessHostObserver implementation.
Bo Liu2a489402018-04-24 23:41:27362 void RenderProcessExited(
363 content::RenderProcessHost* host,
364 const content::ChildProcessTerminationInfo& info) override;
amistry69e9ee422015-05-22 07:40:25365 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override;
366
thestig7ade5b52017-05-23 23:13:36367 content::BrowserContext* const browser_context_;
[email protected]45fd94172013-11-13 03:29:52368
[email protected]513b8032013-11-18 07:47:49369 // The ExtensionPrefs associated with |browser_context_|. May be NULL in
370 // tests.
thestig7ade5b52017-05-23 23:13:36371 ExtensionPrefs* const extension_prefs_;
[email protected]2c699652010-10-15 18:22:41372
[email protected]4243f9a2014-08-04 18:53:12373 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
374 extension_registry_observer_;
375
[email protected]d81d3a192012-11-09 00:32:50376 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30377
[email protected]c761a962013-11-20 04:19:41378 // Map from base event name to observer.
thestig7ade5b52017-05-23 23:13:36379 using ObserverMap = std::unordered_map<std::string, Observer*>;
[email protected]c4dc5cc2012-11-09 08:48:39380 ObserverMap observers_;
381
Trent Apteda250ec3ab2018-08-19 08:52:19382 base::ObserverList<TestObserver>::Unchecked test_observers_;
Devlin Croninffbd2fe22018-07-20 17:20:17383
amistry69e9ee422015-05-22 07:40:25384 std::set<content::RenderProcessHost*> observed_process_set_;
385
lazyboy75b9def2017-06-06 18:56:59386 LazyEventDispatchUtil lazy_event_dispatch_util_;
387
Istiaque Ahmeda14ec482018-08-25 01:02:18388 EventAckData event_ack_data_;
389
lazyboye464732f2017-06-15 21:17:27390 base::WeakPtrFactory<EventRouter> weak_factory_;
391
[email protected]5a38dfd2012-07-23 23:22:10392 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41393};
394
[email protected]5a38dfd2012-07-23 23:22:10395struct Event {
reillyg5464e7e2014-12-11 00:35:08396 // This callback should return true if the event should be dispatched to the
397 // given context and extension, and false otherwise.
thestig7ade5b52017-05-23 23:13:36398 using WillDispatchCallback =
Devlin Cronin66bfdb82018-12-27 23:54:42399 base::RepeatingCallback<bool(content::BrowserContext*,
400 const Extension*,
401 Event*,
402 const base::DictionaryValue*)>;
[email protected]6e850922012-12-05 03:22:48403
kalmanf1b4d782015-06-24 21:14:05404 // The identifier for the event, for histograms. In most cases this
405 // correlates 1:1 with |event_name|, in some cases events will generate
406 // their own names, but they cannot generate their own identifier.
thestig7ade5b52017-05-23 23:13:36407 const events::HistogramValue histogram_value;
kalmanf1b4d782015-06-24 21:14:05408
[email protected]6e850922012-12-05 03:22:48409 // The event to dispatch.
thestig7ade5b52017-05-23 23:13:36410 const std::string event_name;
[email protected]6e850922012-12-05 03:22:48411
412 // Arguments to send to the event listener.
dchengf5d241082016-04-21 03:43:11413 std::unique_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48414
lazyboy59155a42017-05-24 22:23:35415 // If non-null, then the event will not be sent to other BrowserContexts
[email protected]45fd94172013-11-13 03:29:52416 // unless the extension has permission (e.g. incognito tab update -> normal
417 // tab only works if extension is allowed incognito access).
lazyboy59155a42017-05-24 22:23:35418 content::BrowserContext* const restrict_to_browser_context;
[email protected]6e850922012-12-05 03:22:48419
420 // If not empty, the event is only sent to extensions with host permissions
421 // for this url.
422 GURL event_url;
423
424 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10425 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48426
427 // Extra information used to filter which events are sent to the listener.
428 EventFilteringInfo filter_info;
429
430 // If specified, this is called before dispatching an event to each
431 // extension. The third argument is a mutable reference to event_args,
432 // allowing the caller to provide different arguments depending on the
433 // extension and profile. This is guaranteed to be called synchronously with
434 // DispatchEvent, so callers don't need to worry about lifetime.
[email protected]2c6e3b04c2014-07-24 12:48:09435 //
436 // NOTE: the Extension argument to this may be NULL because it's possible for
437 // this event to be dispatched to non-extension processes, like WebUI.
[email protected]6e850922012-12-05 03:22:48438 WillDispatchCallback will_dispatch_callback;
439
lazyboy59155a42017-05-24 22:23:35440 // TODO(lazyboy): This sets |restrict_to_browser_context| to nullptr, this
441 // will dispatch the event to unrelated profiles, not just incognito. Audit
442 // and limit usages of this constructor and introduce "include incognito"
443 // option to a constructor version for clients that need to disptach events to
444 // related browser_contexts. See https://crbug.com/726022.
kalmanf1b4d782015-06-24 21:14:05445 Event(events::HistogramValue histogram_value,
446 const std::string& event_name,
dchengf5d241082016-04-21 03:43:11447 std::unique_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57448
kalmanf1b4d782015-06-24 21:14:05449 Event(events::HistogramValue histogram_value,
450 const std::string& event_name,
dchengf5d241082016-04-21 03:43:11451 std::unique_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52452 content::BrowserContext* restrict_to_browser_context);
[email protected]f0eb58a2012-12-17 22:10:49453
kalmanf1b4d782015-06-24 21:14:05454 Event(events::HistogramValue histogram_value,
455 const std::string& event_name,
dchengf5d241082016-04-21 03:43:11456 std::unique_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52457 content::BrowserContext* restrict_to_browser_context,
[email protected]6e850922012-12-05 03:22:48458 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10459 EventRouter::UserGestureState user_gesture,
460 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57461
[email protected]5a38dfd2012-07-23 23:22:10462 ~Event();
[email protected]6e850922012-12-05 03:22:48463
Devlin Cronin614b0142018-12-10 22:08:53464 // Makes a deep copy of this instance.
465 std::unique_ptr<Event> DeepCopy() const;
[email protected]d9e559d2012-07-05 01:04:57466};
467
[email protected]954e13492012-11-15 03:18:23468struct EventListenerInfo {
469 EventListenerInfo(const std::string& event_name,
[email protected]c761a962013-11-20 04:19:41470 const std::string& extension_id,
[email protected]c1abb3232014-07-30 18:28:39471 const GURL& listener_url,
[email protected]c761a962013-11-20 04:19:41472 content::BrowserContext* browser_context);
473 // The event name including any sub-event, e.g. "runtime.onStartup" or
474 // "webRequest.onCompleted/123".
[email protected]954e13492012-11-15 03:18:23475 const std::string event_name;
[email protected]c761a962013-11-20 04:19:41476
[email protected]954e13492012-11-15 03:18:23477 const std::string extension_id;
[email protected]c1abb3232014-07-30 18:28:39478 const GURL listener_url;
thestig7ade5b52017-05-23 23:13:36479 content::BrowserContext* const browser_context;
[email protected]954e13492012-11-15 03:18:23480};
481
[email protected]5a38dfd2012-07-23 23:22:10482} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57483
[email protected]34423532013-11-21 18:13:10484#endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_