blob: c8c5226b71a0fa6bc39121e578e4bc07050d3100 [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
8#include <map>
9#include <set>
10#include <string>
[email protected]f3270a02012-10-26 15:54:0511#include <utility>
[email protected]2c699652010-10-15 18:22:4112
[email protected]6e850922012-12-05 03:22:4813#include "base/callback.h"
[email protected]17902752011-08-31 22:52:5414#include "base/compiler_specific.h"
[email protected]14c1c232013-06-11 17:52:4415#include "base/containers/hash_tables.h"
[email protected]f72d0c62011-08-31 16:27:4416#include "base/memory/linked_ptr.h"
[email protected]3b63f8f42011-03-28 01:54:1517#include "base/memory/ref_counted.h"
[email protected]8a16a032012-06-18 19:37:3118#include "base/values.h"
[email protected]6c2381d2011-10-19 02:52:5319#include "content/public/browser/notification_observer.h"
20#include "content/public/browser/notification_registrar.h"
[email protected]a0e26d42013-11-20 07:04:0421#include "extensions/browser/event_listener_map.h"
[email protected]4b3e1922013-02-12 04:45:5822#include "extensions/common/event_filtering_info.h"
[email protected]b44f8ad2012-06-15 20:52:5823#include "ipc/ipc_sender.h"
[email protected]2c699652010-10-15 18:22:4124
25class GURL;
[email protected]45fd94172013-11-13 03:29:5226class PrefService;
[email protected]f3b1a082011-11-18 00:34:3027
28namespace content {
[email protected]7061be92013-02-18 15:44:0229class BrowserContext;
[email protected]2c699652010-10-15 18:22:4130class RenderProcessHost;
[email protected]f3b1a082011-11-18 00:34:3031}
[email protected]2c699652010-10-15 18:22:4132
[email protected]1c321ee52012-05-21 03:02:3433namespace extensions {
[email protected]c2ed3f692013-01-31 20:37:3634class ActivityLog;
[email protected]1c321ee52012-05-21 03:02:3435class Extension;
[email protected]3a1dc572012-07-31 22:25:1336class ExtensionHost;
[email protected]79cb81bb2012-09-20 02:23:3137class ExtensionPrefs;
[email protected]3a1dc572012-07-31 22:25:1338
[email protected]5a38dfd2012-07-23 23:22:1039struct Event;
[email protected]72e280f92013-11-20 02:02:3440struct EventDispatchInfo;
[email protected]954e13492012-11-15 03:18:2341struct EventListenerInfo;
[email protected]1c321ee52012-05-21 03:02:3442
[email protected]5a38dfd2012-07-23 23:22:1043class EventRouter : public content::NotificationObserver,
44 public EventListenerMap::Delegate {
[email protected]2c699652010-10-15 18:22:4145 public:
[email protected]b085856f2012-03-02 04:37:2546 // These constants convey the state of our knowledge of whether we're in
47 // a user-caused gesture as part of DispatchEvent.
48 enum UserGestureState {
49 USER_GESTURE_UNKNOWN = 0,
50 USER_GESTURE_ENABLED = 1,
51 USER_GESTURE_NOT_ENABLED = 2,
52 };
53
[email protected]e74d43c72013-05-17 19:01:4154 // The pref key for the list of event names for which an extension has
55 // registered from its lazy background page.
56 static const char kRegisteredEvents[];
57
[email protected]c4dc5cc2012-11-09 08:48:3958 // Observers register interest in events with a particular name and are
[email protected]c761a962013-11-20 04:19:4159 // notified when a listener is added or removed. Observers are matched by
60 // the base name of the event (e.g. adding an event listener for event name
61 // "foo.onBar/123" will trigger observers registered for "foo.onBar").
[email protected]c4dc5cc2012-11-09 08:48:3962 class Observer {
63 public:
64 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2365 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3966 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2367 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3968 };
69
[email protected]3a368a22014-03-26 19:29:1970 // Gets the EventRouter for |browser_context|.
71 // Shorthand for ExtensionSystem::Get(browser_context)->event_router(); it's
72 // a very common operation.
73 static EventRouter* Get(content::BrowserContext* browser_context);
74
[email protected]c761a962013-11-20 04:19:4175 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names
76 // without a "/" are returned unchanged.
77 static std::string GetBaseEventName(const std::string& full_event_name);
78
[email protected]c9bd90f2012-08-07 23:58:1579 // Sends an event via ipc_sender to the given extension. Can be called on any
80 // thread.
[email protected]b44f8ad2012-06-15 20:52:5881 static void DispatchEvent(IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:4982 void* browser_context_id,
[email protected]c357acb42011-06-09 20:52:4283 const std::string& extension_id,
84 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1585 scoped_ptr<base::ListValue> event_args,
[email protected]d9e559d2012-07-05 01:04:5786 UserGestureState user_gesture,
[email protected]5a38dfd2012-07-23 23:22:1087 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:1988
[email protected]45fd94172013-11-13 03:29:5289 // An EventRouter is shared between |browser_context| and its associated
90 // incognito context. |extension_prefs| may be NULL in tests.
91 EventRouter(content::BrowserContext* browser_context,
92 ExtensionPrefs* extension_prefs);
[email protected]5a38dfd2012-07-23 23:22:1093 virtual ~EventRouter();
[email protected]2c699652010-10-15 18:22:4194
[email protected]c1abb3232014-07-30 18:28:3995 // Add or remove an extension as an event listener for |event_name|.
96 //
[email protected]a7ab1b782010-10-21 23:24:1697 // Note that multiple extensions can share a process due to process
98 // collapsing. Also, a single extension can have 2 processes if it is a split
99 // mode extension.
[email protected]2c699652010-10-15 18:22:41100 void AddEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30101 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:16102 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:41103 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30104 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:16105 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:41106
[email protected]c1abb3232014-07-30 18:28:39107 // Add or remove a URL as an event listener for |event_name|.
108 void AddEventListenerForURL(const std::string& event_name,
109 content::RenderProcessHost* process,
110 const GURL& listener_url);
111 void RemoveEventListenerForURL(const std::string& event_name,
112 content::RenderProcessHost* process,
113 const GURL& listener_url);
114
[email protected]f34706be2012-09-04 07:32:09115 EventListenerMap& listeners() { return listeners_; }
116
[email protected]c4dc5cc2012-11-09 08:48:39117 // Registers an observer to be notified when an event listener for
118 // |event_name| is added or removed. There can currently be only one observer
119 // for each distinct |event_name|.
120 void RegisterObserver(Observer* observer,
121 const std::string& event_name);
122
123 // Unregisters an observer from all events.
124 void UnregisterObserver(Observer* observer);
125
[email protected]36531222012-02-07 19:41:27126 // Add or remove the extension as having a lazy background page that listens
127 // to the event. The difference from the above methods is that these will be
128 // remembered even after the process goes away. We use this list to decide
129 // which extension pages to load when dispatching an event.
130 void AddLazyEventListener(const std::string& event_name,
131 const std::string& extension_id);
132 void RemoveLazyEventListener(const std::string& event_name,
133 const std::string& extension_id);
134
[email protected]d9e559d2012-07-05 01:04:57135 // If |add_lazy_listener| is true also add the lazy version of this listener.
136 void AddFilteredEventListener(const std::string& event_name,
137 content::RenderProcessHost* process,
138 const std::string& extension_id,
139 const base::DictionaryValue& filter,
140 bool add_lazy_listener);
141
142 // If |remove_lazy_listener| is true also remove the lazy version of this
143 // listener.
144 void RemoveFilteredEventListener(const std::string& event_name,
145 content::RenderProcessHost* process,
146 const std::string& extension_id,
147 const base::DictionaryValue& filter,
148 bool remove_lazy_listener);
149
[email protected]2c699652010-10-15 18:22:41150 // Returns true if there is at least one listener for the given event.
151 bool HasEventListener(const std::string& event_name);
152
[email protected]a7ab1b782010-10-21 23:24:16153 // Returns true if the extension is listening to the given event.
154 bool ExtensionHasEventListener(const std::string& extension_id,
155 const std::string& event_name);
156
[email protected]e74d43c72013-05-17 19:01:41157 // Return or set the list of events for which the given extension has
158 // registered.
159 std::set<std::string> GetRegisteredEvents(const std::string& extension_id);
160 void SetRegisteredEvents(const std::string& extension_id,
161 const std::set<std::string>& events);
162
[email protected]6e850922012-12-05 03:22:48163 // Broadcasts an event to every listener registered for that event.
164 virtual void BroadcastEvent(scoped_ptr<Event> event);
165
166 // Dispatches an event to the given extension.
167 virtual void DispatchEventToExtension(const std::string& extension_id,
168 scoped_ptr<Event> event);
169
[email protected]42d24742013-07-23 05:25:55170 // Dispatches |event| to the given extension as if the extension has a lazy
171 // listener for it. NOTE: This should be used rarely, for dispatching events
172 // to extensions that haven't had a chance to add their own listeners yet, eg:
173 // newly installed extensions.
174 void DispatchEventWithLazyListener(const std::string& extension_id,
175 scoped_ptr<Event> event);
176
[email protected]89102012011-11-01 21:23:56177 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]45fd94172013-11-13 03:29:52178 void OnEventAck(content::BrowserContext* context,
179 const std::string& extension_id);
[email protected]89102012011-11-01 21:23:56180
[email protected]fb6ff23b2012-03-13 23:13:42181 private:
[email protected]c1abb3232014-07-30 18:28:39182 friend class EventRouterTest;
[email protected]c761a962013-11-20 04:19:41183
[email protected]61f5fc82012-02-15 20:10:45184 // The extension and process that contains the event listener for a given
185 // event.
186 struct ListenerProcess;
187
188 // A map between an event name and a set of extensions that are listening
189 // to that event.
190 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
[email protected]a7ab1b782010-10-21 23:24:16191
[email protected]f3270a02012-10-26 15:54:05192 // An identifier for an event dispatch that is used to prevent double dispatch
193 // due to race conditions between the direct and lazy dispatch paths.
194 typedef std::pair<const content::BrowserContext*, std::string>
195 EventDispatchIdentifier;
196
[email protected]c9bd90f2012-08-07 23:58:15197 // TODO(gdk): Document this.
198 static void DispatchExtensionMessage(
199 IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:49200 void* browser_context_id,
[email protected]c9bd90f2012-08-07 23:58:15201 const std::string& extension_id,
202 const std::string& event_name,
203 base::ListValue* event_args,
[email protected]c9bd90f2012-08-07 23:58:15204 UserGestureState user_gesture,
205 const extensions::EventFilteringInfo& info);
206
[email protected]432115822011-07-10 15:52:27207 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53208 const content::NotificationSource& source,
209 const content::NotificationDetails& details) OVERRIDE;
[email protected]2c699652010-10-15 18:22:41210
[email protected]61f5fc82012-02-15 20:10:45211 // Returns true if the given listener map contains a event listeners for
212 // the given event. If |extension_id| is non-empty, we also check that that
213 // extension is one of the listeners.
214 bool HasEventListenerImpl(const ListenerMap& listeners,
215 const std::string& extension_id,
216 const std::string& event_name);
217
[email protected]d9e559d2012-07-05 01:04:57218 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
219 // event is broadcast.
[email protected]fb6ff23b2012-03-13 23:13:42220 // An event that just came off the pending list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57221 void DispatchEventImpl(const std::string& restrict_to_extension_id,
[email protected]5a38dfd2012-07-23 23:22:10222 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42223
[email protected]d9e559d2012-07-05 01:04:57224 // Ensures that all lazy background pages that are interested in the given
225 // event are loaded, and queues the event if the page is not ready yet.
[email protected]f3270a02012-10-26 15:54:05226 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy
227 // event dispatch that is queued.
228 void DispatchLazyEvent(const std::string& extension_id,
229 const linked_ptr<Event>& event,
230 std::set<EventDispatchIdentifier>* already_dispatched);
[email protected]d9e559d2012-07-05 01:04:57231
[email protected]c1abb3232014-07-30 18:28:39232 // Dispatches the event to the specified extension or URL running in
233 // |process|.
[email protected]d9e559d2012-07-05 01:04:57234 void DispatchEventToProcess(const std::string& extension_id,
[email protected]c1abb3232014-07-30 18:28:39235 const GURL& listener_url,
[email protected]d9e559d2012-07-05 01:04:57236 content::RenderProcessHost* process,
[email protected]5a38dfd2012-07-23 23:22:10237 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42238
[email protected]45fd94172013-11-13 03:29:52239 // Returns false when the event is scoped to a context and the listening
240 // extension does not have access to events from that context. Also fills
[email protected]fb6ff23b2012-03-13 23:13:42241 // |event_args| with the proper arguments to send, which may differ if
242 // the event crosses the incognito boundary.
[email protected]45fd94172013-11-13 03:29:52243 bool CanDispatchEventToBrowserContext(content::BrowserContext* context,
244 const Extension* extension,
245 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42246
[email protected]fb6ff23b2012-03-13 23:13:42247 // Possibly loads given extension's background page in preparation to
[email protected]6b527572012-10-12 18:00:43248 // dispatch an event. Returns true if the event was queued for subsequent
249 // dispatch, false otherwise.
250 bool MaybeLoadLazyBackgroundPageToDispatchEvent(
[email protected]45fd94172013-11-13 03:29:52251 content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10252 const Extension* extension,
253 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42254
[email protected]e74d43c72013-05-17 19:01:41255 // Adds a filter to an event.
256 void AddFilterToEvent(const std::string& event_name,
257 const std::string& extension_id,
258 const base::DictionaryValue* filter);
259
260 // Removes a filter from an event.
261 void RemoveFilterFromEvent(const std::string& event_name,
262 const std::string& extension_id,
263 const base::DictionaryValue* filter);
264
265 // Returns the dictionary of event filters that the given extension has
266 // registered.
267 const base::DictionaryValue* GetFilteredEvents(
268 const std::string& extension_id);
269
[email protected]fb6ff23b2012-03-13 23:13:42270 // Track of the number of dispatched events that have not yet sent an
271 // ACK from the renderer.
[email protected]45fd94172013-11-13 03:29:52272 void IncrementInFlightEvents(content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10273 const Extension* extension);
[email protected]fb6ff23b2012-03-13 23:13:42274
[email protected]db9f2142013-05-27 22:56:16275 // static
276 static void IncrementInFlightEventsOnUI(
[email protected]513b8032013-11-18 07:47:49277 void* browser_context_id,
[email protected]db9f2142013-05-27 22:56:16278 const std::string& extension_id);
279
[email protected]5a38dfd2012-07-23 23:22:10280 void DispatchPendingEvent(const linked_ptr<Event>& event,
[email protected]b6536df2012-03-16 18:55:23281 ExtensionHost* host);
[email protected]fb6ff23b2012-03-13 23:13:42282
[email protected]5a38dfd2012-07-23 23:22:10283 // Implementation of EventListenerMap::Delegate.
[email protected]d9e559d2012-07-05 01:04:57284 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE;
285 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE;
286
[email protected]45fd94172013-11-13 03:29:52287 content::BrowserContext* browser_context_;
288
[email protected]513b8032013-11-18 07:47:49289 // The ExtensionPrefs associated with |browser_context_|. May be NULL in
290 // tests.
[email protected]45fd94172013-11-13 03:29:52291 ExtensionPrefs* extension_prefs_;
[email protected]2c699652010-10-15 18:22:41292
[email protected]6c2381d2011-10-19 02:52:53293 content::NotificationRegistrar registrar_;
[email protected]2c699652010-10-15 18:22:41294
[email protected]d81d3a192012-11-09 00:32:50295 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30296
[email protected]c761a962013-11-20 04:19:41297 // Map from base event name to observer.
[email protected]cf118b962012-12-11 03:08:22298 typedef base::hash_map<std::string, Observer*> ObserverMap;
[email protected]c4dc5cc2012-11-09 08:48:39299 ObserverMap observers_;
300
[email protected]5a38dfd2012-07-23 23:22:10301 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41302};
303
[email protected]5a38dfd2012-07-23 23:22:10304struct Event {
[email protected]45fd94172013-11-13 03:29:52305 typedef base::Callback<void(content::BrowserContext*,
306 const Extension*,
307 base::ListValue*)> WillDispatchCallback;
[email protected]6e850922012-12-05 03:22:48308
309 // The event to dispatch.
[email protected]d9e559d2012-07-05 01:04:57310 std::string event_name;
[email protected]6e850922012-12-05 03:22:48311
312 // Arguments to send to the event listener.
[email protected]c9bd90f2012-08-07 23:58:15313 scoped_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48314
[email protected]45fd94172013-11-13 03:29:52315 // If non-NULL, then the event will not be sent to other BrowserContexts
316 // unless the extension has permission (e.g. incognito tab update -> normal
317 // tab only works if extension is allowed incognito access).
318 content::BrowserContext* restrict_to_browser_context;
[email protected]6e850922012-12-05 03:22:48319
320 // If not empty, the event is only sent to extensions with host permissions
321 // for this url.
322 GURL event_url;
323
324 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10325 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48326
327 // Extra information used to filter which events are sent to the listener.
328 EventFilteringInfo filter_info;
329
330 // If specified, this is called before dispatching an event to each
331 // extension. The third argument is a mutable reference to event_args,
332 // allowing the caller to provide different arguments depending on the
333 // extension and profile. This is guaranteed to be called synchronously with
334 // DispatchEvent, so callers don't need to worry about lifetime.
[email protected]2c6e3b04c2014-07-24 12:48:09335 //
336 // NOTE: the Extension argument to this may be NULL because it's possible for
337 // this event to be dispatched to non-extension processes, like WebUI.
[email protected]6e850922012-12-05 03:22:48338 WillDispatchCallback will_dispatch_callback;
339
[email protected]8b4405012014-01-08 03:02:13340 // If true, this event will always be dispatched to ephemeral apps, regardless
341 // of whether they are running or inactive. Defaults to false.
342 // Most events can only be dispatched to ephemeral apps that are already
343 // running. Cached ephemeral apps are inactive until launched by the user.
344 bool can_load_ephemeral_apps;
345
[email protected]6e850922012-12-05 03:22:48346 Event(const std::string& event_name,
347 scoped_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57348
[email protected]5a38dfd2012-07-23 23:22:10349 Event(const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:15350 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52351 content::BrowserContext* restrict_to_browser_context);
[email protected]f0eb58a2012-12-17 22:10:49352
353 Event(const std::string& event_name,
354 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52355 content::BrowserContext* restrict_to_browser_context,
[email protected]6e850922012-12-05 03:22:48356 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10357 EventRouter::UserGestureState user_gesture,
358 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57359
[email protected]5a38dfd2012-07-23 23:22:10360 ~Event();
[email protected]6e850922012-12-05 03:22:48361
362 // Makes a deep copy of this instance. Ownership is transferred to the
363 // caller.
364 Event* DeepCopy();
[email protected]d9e559d2012-07-05 01:04:57365};
366
[email protected]954e13492012-11-15 03:18:23367struct EventListenerInfo {
368 EventListenerInfo(const std::string& event_name,
[email protected]c761a962013-11-20 04:19:41369 const std::string& extension_id,
[email protected]c1abb3232014-07-30 18:28:39370 const GURL& listener_url,
[email protected]c761a962013-11-20 04:19:41371 content::BrowserContext* browser_context);
372 // The event name including any sub-event, e.g. "runtime.onStartup" or
373 // "webRequest.onCompleted/123".
[email protected]954e13492012-11-15 03:18:23374 const std::string event_name;
[email protected]c761a962013-11-20 04:19:41375
[email protected]954e13492012-11-15 03:18:23376 const std::string extension_id;
[email protected]c1abb3232014-07-30 18:28:39377 const GURL listener_url;
[email protected]c761a962013-11-20 04:19:41378 content::BrowserContext* browser_context;
[email protected]954e13492012-11-15 03:18:23379};
380
[email protected]5a38dfd2012-07-23 23:22:10381} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57382
[email protected]34423532013-11-21 18:13:10383#endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_