blob: 1b89bfebb238978b1196c887720370960a27f881 [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]5a38dfd2012-07-23 23:22:105#ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_
6#define CHROME_BROWSER_EXTENSIONS_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]d9e559d2012-07-05 01:04:5719#include "chrome/browser/extensions/event_listener_map.h"
[email protected]6c2381d2011-10-19 02:52:5320#include "content/public/browser/notification_observer.h"
21#include "content/public/browser/notification_registrar.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]1c321ee2012-05-21 03:02:3433namespace extensions {
[email protected]c2ed3f692013-01-31 20:37:3634class ActivityLog;
[email protected]1c321ee2012-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]1c321ee2012-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
59 // notified when a listener is added or removed for that |event_name|.
60 class Observer {
61 public:
62 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2363 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3964 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2365 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3966 };
67
[email protected]72e280f92013-11-20 02:02:3468 // The EventDispatchObserver is notified on the UI thread whenever
69 // an event is dispatched. There can be only one EventDispatchObserver.
70 class EventDispatchObserver {
71 public:
72 virtual void OnWillDispatchEvent(scoped_ptr<EventDispatchInfo> details) = 0;
73 };
74
[email protected]c9bd90f2012-08-07 23:58:1575 // Sends an event via ipc_sender to the given extension. Can be called on any
76 // thread.
[email protected]b44f8ad2012-06-15 20:52:5877 static void DispatchEvent(IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:4978 void* browser_context_id,
[email protected]c357acb42011-06-09 20:52:4279 const std::string& extension_id,
80 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1581 scoped_ptr<base::ListValue> event_args,
[email protected]d9e559d2012-07-05 01:04:5782 UserGestureState user_gesture,
[email protected]5a38dfd2012-07-23 23:22:1083 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:1984
[email protected]45fd94172013-11-13 03:29:5285 // An EventRouter is shared between |browser_context| and its associated
86 // incognito context. |extension_prefs| may be NULL in tests.
87 EventRouter(content::BrowserContext* browser_context,
88 ExtensionPrefs* extension_prefs);
[email protected]5a38dfd2012-07-23 23:22:1089 virtual ~EventRouter();
[email protected]2c699652010-10-15 18:22:4190
[email protected]a7ab1b782010-10-21 23:24:1691 // Add or remove the process/extension pair as a listener for |event_name|.
92 // Note that multiple extensions can share a process due to process
93 // collapsing. Also, a single extension can have 2 processes if it is a split
94 // mode extension.
[email protected]2c699652010-10-15 18:22:4195 void AddEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:3096 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:1697 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:4198 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:3099 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:16100 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:41101
[email protected]f34706be2012-09-04 07:32:09102 EventListenerMap& listeners() { return listeners_; }
103
[email protected]c4dc5cc2012-11-09 08:48:39104 // Registers an observer to be notified when an event listener for
105 // |event_name| is added or removed. There can currently be only one observer
106 // for each distinct |event_name|.
107 void RegisterObserver(Observer* observer,
108 const std::string& event_name);
109
110 // Unregisters an observer from all events.
111 void UnregisterObserver(Observer* observer);
112
[email protected]72e280f92013-11-20 02:02:34113 // Sets the observer to be notified whenever an event is dispatched to an
114 // extension.
115 void SetEventDispatchObserver(EventDispatchObserver* observer);
116
[email protected]36531222012-02-07 19:41:27117 // Add or remove the extension as having a lazy background page that listens
118 // to the event. The difference from the above methods is that these will be
119 // remembered even after the process goes away. We use this list to decide
120 // which extension pages to load when dispatching an event.
121 void AddLazyEventListener(const std::string& event_name,
122 const std::string& extension_id);
123 void RemoveLazyEventListener(const std::string& event_name,
124 const std::string& extension_id);
125
[email protected]d9e559d2012-07-05 01:04:57126 // If |add_lazy_listener| is true also add the lazy version of this listener.
127 void AddFilteredEventListener(const std::string& event_name,
128 content::RenderProcessHost* process,
129 const std::string& extension_id,
130 const base::DictionaryValue& filter,
131 bool add_lazy_listener);
132
133 // If |remove_lazy_listener| is true also remove the lazy version of this
134 // listener.
135 void RemoveFilteredEventListener(const std::string& event_name,
136 content::RenderProcessHost* process,
137 const std::string& extension_id,
138 const base::DictionaryValue& filter,
139 bool remove_lazy_listener);
140
[email protected]2c699652010-10-15 18:22:41141 // Returns true if there is at least one listener for the given event.
142 bool HasEventListener(const std::string& event_name);
143
[email protected]a7ab1b782010-10-21 23:24:16144 // Returns true if the extension is listening to the given event.
145 bool ExtensionHasEventListener(const std::string& extension_id,
146 const std::string& event_name);
147
[email protected]e74d43c72013-05-17 19:01:41148 // Return or set the list of events for which the given extension has
149 // registered.
150 std::set<std::string> GetRegisteredEvents(const std::string& extension_id);
151 void SetRegisteredEvents(const std::string& extension_id,
152 const std::set<std::string>& events);
153
[email protected]6e850922012-12-05 03:22:48154 // Broadcasts an event to every listener registered for that event.
155 virtual void BroadcastEvent(scoped_ptr<Event> event);
156
157 // Dispatches an event to the given extension.
158 virtual void DispatchEventToExtension(const std::string& extension_id,
159 scoped_ptr<Event> event);
160
[email protected]42d24742013-07-23 05:25:55161 // Dispatches |event| to the given extension as if the extension has a lazy
162 // listener for it. NOTE: This should be used rarely, for dispatching events
163 // to extensions that haven't had a chance to add their own listeners yet, eg:
164 // newly installed extensions.
165 void DispatchEventWithLazyListener(const std::string& extension_id,
166 scoped_ptr<Event> event);
167
[email protected]89102012011-11-01 21:23:56168 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]45fd94172013-11-13 03:29:52169 void OnEventAck(content::BrowserContext* context,
170 const std::string& extension_id);
[email protected]89102012011-11-01 21:23:56171
[email protected]fb6ff23b2012-03-13 23:13:42172 private:
[email protected]61f5fc82012-02-15 20:10:45173 // The extension and process that contains the event listener for a given
174 // event.
175 struct ListenerProcess;
176
177 // A map between an event name and a set of extensions that are listening
178 // to that event.
179 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
[email protected]a7ab1b782010-10-21 23:24:16180
[email protected]f3270a02012-10-26 15:54:05181 // An identifier for an event dispatch that is used to prevent double dispatch
182 // due to race conditions between the direct and lazy dispatch paths.
183 typedef std::pair<const content::BrowserContext*, std::string>
184 EventDispatchIdentifier;
185
[email protected]72e280f92013-11-20 02:02:34186 // Sends a notification about an event to the event dispatch observer on the
187 // UI thread. Can be called from any thread.
188 static void NotifyExtensionDispatchObserverOnUIThread(
189 void* browser_context_id,
190 scoped_ptr<EventDispatchInfo> details);
[email protected]c2ed3f692013-01-31 20:37:36191
[email protected]c9bd90f2012-08-07 23:58:15192 // TODO(gdk): Document this.
193 static void DispatchExtensionMessage(
194 IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:49195 void* browser_context_id,
[email protected]c9bd90f2012-08-07 23:58:15196 const std::string& extension_id,
197 const std::string& event_name,
198 base::ListValue* event_args,
[email protected]c9bd90f2012-08-07 23:58:15199 UserGestureState user_gesture,
200 const extensions::EventFilteringInfo& info);
201
[email protected]432115822011-07-10 15:52:27202 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53203 const content::NotificationSource& source,
204 const content::NotificationDetails& details) OVERRIDE;
[email protected]2c699652010-10-15 18:22:41205
[email protected]61f5fc82012-02-15 20:10:45206 // Returns true if the given listener map contains a event listeners for
207 // the given event. If |extension_id| is non-empty, we also check that that
208 // extension is one of the listeners.
209 bool HasEventListenerImpl(const ListenerMap& listeners,
210 const std::string& extension_id,
211 const std::string& event_name);
212
[email protected]d9e559d2012-07-05 01:04:57213 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
214 // event is broadcast.
[email protected]fb6ff23b2012-03-13 23:13:42215 // An event that just came off the pending list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57216 void DispatchEventImpl(const std::string& restrict_to_extension_id,
[email protected]5a38dfd2012-07-23 23:22:10217 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42218
[email protected]d9e559d2012-07-05 01:04:57219 // Ensures that all lazy background pages that are interested in the given
220 // event are loaded, and queues the event if the page is not ready yet.
[email protected]f3270a02012-10-26 15:54:05221 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy
222 // event dispatch that is queued.
223 void DispatchLazyEvent(const std::string& extension_id,
224 const linked_ptr<Event>& event,
225 std::set<EventDispatchIdentifier>* already_dispatched);
[email protected]d9e559d2012-07-05 01:04:57226
227 // Dispatches the event to the specified extension running in |process|.
228 void DispatchEventToProcess(const std::string& extension_id,
229 content::RenderProcessHost* process,
[email protected]5a38dfd2012-07-23 23:22:10230 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42231
[email protected]45fd94172013-11-13 03:29:52232 // Returns false when the event is scoped to a context and the listening
233 // extension does not have access to events from that context. Also fills
[email protected]fb6ff23b2012-03-13 23:13:42234 // |event_args| with the proper arguments to send, which may differ if
235 // the event crosses the incognito boundary.
[email protected]45fd94172013-11-13 03:29:52236 bool CanDispatchEventToBrowserContext(content::BrowserContext* context,
237 const Extension* extension,
238 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42239
[email protected]fb6ff23b2012-03-13 23:13:42240 // Possibly loads given extension's background page in preparation to
[email protected]6b527572012-10-12 18:00:43241 // dispatch an event. Returns true if the event was queued for subsequent
242 // dispatch, false otherwise.
243 bool MaybeLoadLazyBackgroundPageToDispatchEvent(
[email protected]45fd94172013-11-13 03:29:52244 content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10245 const Extension* extension,
246 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42247
[email protected]e74d43c72013-05-17 19:01:41248 // Adds a filter to an event.
249 void AddFilterToEvent(const std::string& event_name,
250 const std::string& extension_id,
251 const base::DictionaryValue* filter);
252
253 // Removes a filter from an event.
254 void RemoveFilterFromEvent(const std::string& event_name,
255 const std::string& extension_id,
256 const base::DictionaryValue* filter);
257
258 // Returns the dictionary of event filters that the given extension has
259 // registered.
260 const base::DictionaryValue* GetFilteredEvents(
261 const std::string& extension_id);
262
[email protected]fb6ff23b2012-03-13 23:13:42263 // Track of the number of dispatched events that have not yet sent an
264 // ACK from the renderer.
[email protected]45fd94172013-11-13 03:29:52265 void IncrementInFlightEvents(content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10266 const Extension* extension);
[email protected]fb6ff23b2012-03-13 23:13:42267
[email protected]db9f2142013-05-27 22:56:16268 // static
269 static void IncrementInFlightEventsOnUI(
[email protected]513b8032013-11-18 07:47:49270 void* browser_context_id,
[email protected]db9f2142013-05-27 22:56:16271 const std::string& extension_id);
272
[email protected]5a38dfd2012-07-23 23:22:10273 void DispatchPendingEvent(const linked_ptr<Event>& event,
[email protected]b6536df2012-03-16 18:55:23274 ExtensionHost* host);
[email protected]fb6ff23b2012-03-13 23:13:42275
[email protected]5a38dfd2012-07-23 23:22:10276 // Implementation of EventListenerMap::Delegate.
[email protected]d9e559d2012-07-05 01:04:57277 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE;
278 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE;
279
[email protected]45fd94172013-11-13 03:29:52280 content::BrowserContext* browser_context_;
281
[email protected]513b8032013-11-18 07:47:49282 // The ExtensionPrefs associated with |browser_context_|. May be NULL in
283 // tests.
[email protected]45fd94172013-11-13 03:29:52284 ExtensionPrefs* extension_prefs_;
[email protected]2c699652010-10-15 18:22:41285
[email protected]6c2381d2011-10-19 02:52:53286 content::NotificationRegistrar registrar_;
[email protected]2c699652010-10-15 18:22:41287
[email protected]d81d3a192012-11-09 00:32:50288 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30289
[email protected]cf118b962012-12-11 03:08:22290 typedef base::hash_map<std::string, Observer*> ObserverMap;
[email protected]c4dc5cc2012-11-09 08:48:39291 ObserverMap observers_;
292
[email protected]72e280f92013-11-20 02:02:34293 EventDispatchObserver* event_dispatch_observer_;
294
[email protected]5a38dfd2012-07-23 23:22:10295 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41296};
297
[email protected]5a38dfd2012-07-23 23:22:10298struct Event {
[email protected]45fd94172013-11-13 03:29:52299 typedef base::Callback<void(content::BrowserContext*,
300 const Extension*,
301 base::ListValue*)> WillDispatchCallback;
[email protected]6e850922012-12-05 03:22:48302
303 // The event to dispatch.
[email protected]d9e559d2012-07-05 01:04:57304 std::string event_name;
[email protected]6e850922012-12-05 03:22:48305
306 // Arguments to send to the event listener.
[email protected]c9bd90f2012-08-07 23:58:15307 scoped_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48308
[email protected]45fd94172013-11-13 03:29:52309 // If non-NULL, then the event will not be sent to other BrowserContexts
310 // unless the extension has permission (e.g. incognito tab update -> normal
311 // tab only works if extension is allowed incognito access).
312 content::BrowserContext* restrict_to_browser_context;
[email protected]6e850922012-12-05 03:22:48313
314 // If not empty, the event is only sent to extensions with host permissions
315 // for this url.
316 GURL event_url;
317
318 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10319 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48320
321 // Extra information used to filter which events are sent to the listener.
322 EventFilteringInfo filter_info;
323
324 // If specified, this is called before dispatching an event to each
325 // extension. The third argument is a mutable reference to event_args,
326 // allowing the caller to provide different arguments depending on the
327 // extension and profile. This is guaranteed to be called synchronously with
328 // DispatchEvent, so callers don't need to worry about lifetime.
329 WillDispatchCallback will_dispatch_callback;
330
331 Event(const std::string& event_name,
332 scoped_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57333
[email protected]5a38dfd2012-07-23 23:22:10334 Event(const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:15335 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52336 content::BrowserContext* restrict_to_browser_context);
[email protected]f0eb58a2012-12-17 22:10:49337
338 Event(const std::string& event_name,
339 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52340 content::BrowserContext* restrict_to_browser_context,
[email protected]6e850922012-12-05 03:22:48341 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10342 EventRouter::UserGestureState user_gesture,
343 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57344
[email protected]5a38dfd2012-07-23 23:22:10345 ~Event();
[email protected]6e850922012-12-05 03:22:48346
347 // Makes a deep copy of this instance. Ownership is transferred to the
348 // caller.
349 Event* DeepCopy();
[email protected]d9e559d2012-07-05 01:04:57350};
351
[email protected]954e13492012-11-15 03:18:23352struct EventListenerInfo {
353 EventListenerInfo(const std::string& event_name,
354 const std::string& extension_id);
355
356 const std::string event_name;
357 const std::string extension_id;
358};
359
[email protected]72e280f92013-11-20 02:02:34360struct EventDispatchInfo {
361 EventDispatchInfo(const std::string& extension_id,
362 const std::string& event_name,
363 scoped_ptr<ListValue> event_args);
364 ~EventDispatchInfo();
365
366 const std::string extension_id;
367 const std::string event_name;
368 scoped_ptr<ListValue> event_args;
369};
370
[email protected]5a38dfd2012-07-23 23:22:10371} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57372
[email protected]5a38dfd2012-07-23 23:22:10373#endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_