blob: ae4ac7c5711425783ace77ec7bbb375af7b78466 [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]4243f9a2014-08-04 18:53:1218#include "base/scoped_observer.h"
[email protected]8a16a032012-06-18 19:37:3119#include "base/values.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]a0e26d42013-11-20 07:04:0422#include "extensions/browser/event_listener_map.h"
[email protected]4243f9a2014-08-04 18:53:1223#include "extensions/browser/extension_registry_observer.h"
[email protected]4b3e1922013-02-12 04:45:5824#include "extensions/common/event_filtering_info.h"
[email protected]b44f8ad2012-06-15 20:52:5825#include "ipc/ipc_sender.h"
[email protected]2c699652010-10-15 18:22:4126
27class GURL;
[email protected]45fd94172013-11-13 03:29:5228class PrefService;
[email protected]f3b1a082011-11-18 00:34:3029
30namespace content {
[email protected]7061be92013-02-18 15:44:0231class BrowserContext;
[email protected]2c699652010-10-15 18:22:4132class RenderProcessHost;
[email protected]f3b1a082011-11-18 00:34:3033}
[email protected]2c699652010-10-15 18:22:4134
[email protected]1c321ee2012-05-21 03:02:3435namespace extensions {
[email protected]c2ed3f692013-01-31 20:37:3636class ActivityLog;
[email protected]1c321ee2012-05-21 03:02:3437class Extension;
[email protected]3a1dc572012-07-31 22:25:1338class ExtensionHost;
[email protected]79cb81bb2012-09-20 02:23:3139class ExtensionPrefs;
[email protected]4243f9a2014-08-04 18:53:1240class ExtensionRegistry;
[email protected]3a1dc572012-07-31 22:25:1341
[email protected]5a38dfd2012-07-23 23:22:1042struct Event;
[email protected]72e280f92013-11-20 02:02:3443struct EventDispatchInfo;
[email protected]954e13492012-11-15 03:18:2344struct EventListenerInfo;
[email protected]1c321ee2012-05-21 03:02:3445
[email protected]5a38dfd2012-07-23 23:22:1046class EventRouter : public content::NotificationObserver,
[email protected]4243f9a2014-08-04 18:53:1247 public ExtensionRegistryObserver,
[email protected]5a38dfd2012-07-23 23:22:1048 public EventListenerMap::Delegate {
[email protected]2c699652010-10-15 18:22:4149 public:
[email protected]b085856f2012-03-02 04:37:2550 // These constants convey the state of our knowledge of whether we're in
51 // a user-caused gesture as part of DispatchEvent.
52 enum UserGestureState {
53 USER_GESTURE_UNKNOWN = 0,
54 USER_GESTURE_ENABLED = 1,
55 USER_GESTURE_NOT_ENABLED = 2,
56 };
57
[email protected]e74d43c72013-05-17 19:01:4158 // The pref key for the list of event names for which an extension has
59 // registered from its lazy background page.
60 static const char kRegisteredEvents[];
61
[email protected]c4dc5cc2012-11-09 08:48:3962 // Observers register interest in events with a particular name and are
[email protected]c761a962013-11-20 04:19:4163 // notified when a listener is added or removed. Observers are matched by
64 // the base name of the event (e.g. adding an event listener for event name
65 // "foo.onBar/123" will trigger observers registered for "foo.onBar").
[email protected]c4dc5cc2012-11-09 08:48:3966 class Observer {
67 public:
68 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2369 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3970 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2371 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3972 };
73
[email protected]3a368a22014-03-26 19:29:1974 // Gets the EventRouter for |browser_context|.
75 // Shorthand for ExtensionSystem::Get(browser_context)->event_router(); it's
76 // a very common operation.
77 static EventRouter* Get(content::BrowserContext* browser_context);
78
[email protected]c761a962013-11-20 04:19:4179 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names
80 // without a "/" are returned unchanged.
81 static std::string GetBaseEventName(const std::string& full_event_name);
82
[email protected]c9bd90f2012-08-07 23:58:1583 // Sends an event via ipc_sender to the given extension. Can be called on any
84 // thread.
[email protected]b44f8ad2012-06-15 20:52:5885 static void DispatchEvent(IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:4986 void* browser_context_id,
[email protected]c357acb42011-06-09 20:52:4287 const std::string& extension_id,
88 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1589 scoped_ptr<base::ListValue> event_args,
[email protected]d9e559d2012-07-05 01:04:5790 UserGestureState user_gesture,
[email protected]5a38dfd2012-07-23 23:22:1091 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:1992
[email protected]45fd94172013-11-13 03:29:5293 // An EventRouter is shared between |browser_context| and its associated
94 // incognito context. |extension_prefs| may be NULL in tests.
95 EventRouter(content::BrowserContext* browser_context,
96 ExtensionPrefs* extension_prefs);
dcheng9168b2f2014-10-21 12:38:2497 ~EventRouter() override;
[email protected]2c699652010-10-15 18:22:4198
[email protected]c1abb3232014-07-30 18:28:3999 // Add or remove an extension as an event listener for |event_name|.
100 //
[email protected]a7ab1b782010-10-21 23:24:16101 // Note that multiple extensions can share a process due to process
102 // collapsing. Also, a single extension can have 2 processes if it is a split
103 // mode extension.
[email protected]2c699652010-10-15 18:22:41104 void AddEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30105 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:16106 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:41107 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:30108 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:16109 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:41110
[email protected]c1abb3232014-07-30 18:28:39111 // Add or remove a URL as an event listener for |event_name|.
112 void AddEventListenerForURL(const std::string& event_name,
113 content::RenderProcessHost* process,
114 const GURL& listener_url);
115 void RemoveEventListenerForURL(const std::string& event_name,
116 content::RenderProcessHost* process,
117 const GURL& listener_url);
118
[email protected]f34706be2012-09-04 07:32:09119 EventListenerMap& listeners() { return listeners_; }
120
[email protected]c4dc5cc2012-11-09 08:48:39121 // Registers an observer to be notified when an event listener for
122 // |event_name| is added or removed. There can currently be only one observer
123 // for each distinct |event_name|.
124 void RegisterObserver(Observer* observer,
125 const std::string& event_name);
126
127 // Unregisters an observer from all events.
128 void UnregisterObserver(Observer* observer);
129
[email protected]36531222012-02-07 19:41:27130 // Add or remove the extension as having a lazy background page that listens
131 // to the event. The difference from the above methods is that these will be
132 // remembered even after the process goes away. We use this list to decide
133 // which extension pages to load when dispatching an event.
134 void AddLazyEventListener(const std::string& event_name,
135 const std::string& extension_id);
136 void RemoveLazyEventListener(const std::string& event_name,
137 const std::string& extension_id);
138
[email protected]d9e559d2012-07-05 01:04:57139 // If |add_lazy_listener| is true also add the lazy version of this listener.
140 void AddFilteredEventListener(const std::string& event_name,
141 content::RenderProcessHost* process,
142 const std::string& extension_id,
143 const base::DictionaryValue& filter,
144 bool add_lazy_listener);
145
146 // If |remove_lazy_listener| is true also remove the lazy version of this
147 // listener.
148 void RemoveFilteredEventListener(const std::string& event_name,
149 content::RenderProcessHost* process,
150 const std::string& extension_id,
151 const base::DictionaryValue& filter,
152 bool remove_lazy_listener);
153
[email protected]2c699652010-10-15 18:22:41154 // Returns true if there is at least one listener for the given event.
155 bool HasEventListener(const std::string& event_name);
156
[email protected]a7ab1b782010-10-21 23:24:16157 // Returns true if the extension is listening to the given event.
158 bool ExtensionHasEventListener(const std::string& extension_id,
159 const std::string& event_name);
160
[email protected]e74d43c72013-05-17 19:01:41161 // Return or set the list of events for which the given extension has
162 // registered.
163 std::set<std::string> GetRegisteredEvents(const std::string& extension_id);
164 void SetRegisteredEvents(const std::string& extension_id,
165 const std::set<std::string>& events);
166
[email protected]6e850922012-12-05 03:22:48167 // Broadcasts an event to every listener registered for that event.
168 virtual void BroadcastEvent(scoped_ptr<Event> event);
169
170 // Dispatches an event to the given extension.
171 virtual void DispatchEventToExtension(const std::string& extension_id,
172 scoped_ptr<Event> event);
173
[email protected]42d24742013-07-23 05:25:55174 // Dispatches |event| to the given extension as if the extension has a lazy
175 // listener for it. NOTE: This should be used rarely, for dispatching events
176 // to extensions that haven't had a chance to add their own listeners yet, eg:
177 // newly installed extensions.
178 void DispatchEventWithLazyListener(const std::string& extension_id,
179 scoped_ptr<Event> event);
180
[email protected]89102012011-11-01 21:23:56181 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]45fd94172013-11-13 03:29:52182 void OnEventAck(content::BrowserContext* context,
183 const std::string& extension_id);
[email protected]89102012011-11-01 21:23:56184
[email protected]fb6ff23b2012-03-13 23:13:42185 private:
[email protected]c1abb3232014-07-30 18:28:39186 friend class EventRouterTest;
[email protected]c761a962013-11-20 04:19:41187
[email protected]61f5fc82012-02-15 20:10:45188 // The extension and process that contains the event listener for a given
189 // event.
190 struct ListenerProcess;
191
192 // A map between an event name and a set of extensions that are listening
193 // to that event.
194 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
[email protected]a7ab1b782010-10-21 23:24:16195
[email protected]f3270a02012-10-26 15:54:05196 // An identifier for an event dispatch that is used to prevent double dispatch
197 // due to race conditions between the direct and lazy dispatch paths.
198 typedef std::pair<const content::BrowserContext*, std::string>
199 EventDispatchIdentifier;
200
[email protected]c9bd90f2012-08-07 23:58:15201 // TODO(gdk): Document this.
202 static void DispatchExtensionMessage(
203 IPC::Sender* ipc_sender,
[email protected]513b8032013-11-18 07:47:49204 void* browser_context_id,
[email protected]c9bd90f2012-08-07 23:58:15205 const std::string& extension_id,
206 const std::string& event_name,
207 base::ListValue* event_args,
[email protected]c9bd90f2012-08-07 23:58:15208 UserGestureState user_gesture,
209 const extensions::EventFilteringInfo& info);
210
dcheng9168b2f2014-10-21 12:38:24211 void Observe(int type,
212 const content::NotificationSource& source,
213 const content::NotificationDetails& details) override;
[email protected]4243f9a2014-08-04 18:53:12214 // ExtensionRegistryObserver implementation.
dcheng9168b2f2014-10-21 12:38:24215 void OnExtensionLoaded(content::BrowserContext* browser_context,
216 const Extension* extension) override;
217 void OnExtensionUnloaded(content::BrowserContext* browser_context,
218 const Extension* extension,
219 UnloadedExtensionInfo::Reason reason) override;
[email protected]2c699652010-10-15 18:22:41220
[email protected]61f5fc82012-02-15 20:10:45221 // Returns true if the given listener map contains a event listeners for
222 // the given event. If |extension_id| is non-empty, we also check that that
223 // extension is one of the listeners.
224 bool HasEventListenerImpl(const ListenerMap& listeners,
225 const std::string& extension_id,
226 const std::string& event_name);
227
[email protected]d9e559d2012-07-05 01:04:57228 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
229 // event is broadcast.
[email protected]fb6ff23b2012-03-13 23:13:42230 // An event that just came off the pending list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57231 void DispatchEventImpl(const std::string& restrict_to_extension_id,
[email protected]5a38dfd2012-07-23 23:22:10232 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42233
[email protected]d9e559d2012-07-05 01:04:57234 // Ensures that all lazy background pages that are interested in the given
235 // event are loaded, and queues the event if the page is not ready yet.
[email protected]f3270a02012-10-26 15:54:05236 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy
237 // event dispatch that is queued.
238 void DispatchLazyEvent(const std::string& extension_id,
239 const linked_ptr<Event>& event,
240 std::set<EventDispatchIdentifier>* already_dispatched);
[email protected]d9e559d2012-07-05 01:04:57241
[email protected]c1abb3232014-07-30 18:28:39242 // Dispatches the event to the specified extension or URL running in
243 // |process|.
[email protected]d9e559d2012-07-05 01:04:57244 void DispatchEventToProcess(const std::string& extension_id,
[email protected]c1abb3232014-07-30 18:28:39245 const GURL& listener_url,
[email protected]d9e559d2012-07-05 01:04:57246 content::RenderProcessHost* process,
[email protected]5a38dfd2012-07-23 23:22:10247 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42248
[email protected]45fd94172013-11-13 03:29:52249 // Returns false when the event is scoped to a context and the listening
250 // extension does not have access to events from that context. Also fills
[email protected]fb6ff23b2012-03-13 23:13:42251 // |event_args| with the proper arguments to send, which may differ if
252 // the event crosses the incognito boundary.
[email protected]45fd94172013-11-13 03:29:52253 bool CanDispatchEventToBrowserContext(content::BrowserContext* context,
254 const Extension* extension,
255 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42256
[email protected]fb6ff23b2012-03-13 23:13:42257 // Possibly loads given extension's background page in preparation to
[email protected]6b527572012-10-12 18:00:43258 // dispatch an event. Returns true if the event was queued for subsequent
259 // dispatch, false otherwise.
260 bool MaybeLoadLazyBackgroundPageToDispatchEvent(
[email protected]45fd94172013-11-13 03:29:52261 content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10262 const Extension* extension,
263 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42264
[email protected]e74d43c72013-05-17 19:01:41265 // Adds a filter to an event.
266 void AddFilterToEvent(const std::string& event_name,
267 const std::string& extension_id,
268 const base::DictionaryValue* filter);
269
270 // Removes a filter from an event.
271 void RemoveFilterFromEvent(const std::string& event_name,
272 const std::string& extension_id,
273 const base::DictionaryValue* filter);
274
275 // Returns the dictionary of event filters that the given extension has
276 // registered.
277 const base::DictionaryValue* GetFilteredEvents(
278 const std::string& extension_id);
279
[email protected]fb6ff23b2012-03-13 23:13:42280 // Track of the number of dispatched events that have not yet sent an
281 // ACK from the renderer.
[email protected]45fd94172013-11-13 03:29:52282 void IncrementInFlightEvents(content::BrowserContext* context,
[email protected]5a38dfd2012-07-23 23:22:10283 const Extension* extension);
[email protected]fb6ff23b2012-03-13 23:13:42284
[email protected]db9f2142013-05-27 22:56:16285 // static
286 static void IncrementInFlightEventsOnUI(
[email protected]513b8032013-11-18 07:47:49287 void* browser_context_id,
[email protected]db9f2142013-05-27 22:56:16288 const std::string& extension_id);
289
[email protected]5a38dfd2012-07-23 23:22:10290 void DispatchPendingEvent(const linked_ptr<Event>& event,
[email protected]b6536df2012-03-16 18:55:23291 ExtensionHost* host);
[email protected]fb6ff23b2012-03-13 23:13:42292
[email protected]5a38dfd2012-07-23 23:22:10293 // Implementation of EventListenerMap::Delegate.
dcheng9168b2f2014-10-21 12:38:24294 void OnListenerAdded(const EventListener* listener) override;
295 void OnListenerRemoved(const EventListener* listener) override;
[email protected]d9e559d2012-07-05 01:04:57296
[email protected]45fd94172013-11-13 03:29:52297 content::BrowserContext* browser_context_;
298
[email protected]513b8032013-11-18 07:47:49299 // The ExtensionPrefs associated with |browser_context_|. May be NULL in
300 // tests.
[email protected]45fd94172013-11-13 03:29:52301 ExtensionPrefs* extension_prefs_;
[email protected]2c699652010-10-15 18:22:41302
[email protected]6c2381d2011-10-19 02:52:53303 content::NotificationRegistrar registrar_;
[email protected]2c699652010-10-15 18:22:41304
[email protected]4243f9a2014-08-04 18:53:12305 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver>
306 extension_registry_observer_;
307
[email protected]d81d3a192012-11-09 00:32:50308 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30309
[email protected]c761a962013-11-20 04:19:41310 // Map from base event name to observer.
[email protected]cf118b962012-12-11 03:08:22311 typedef base::hash_map<std::string, Observer*> ObserverMap;
[email protected]c4dc5cc2012-11-09 08:48:39312 ObserverMap observers_;
313
[email protected]5a38dfd2012-07-23 23:22:10314 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41315};
316
[email protected]5a38dfd2012-07-23 23:22:10317struct Event {
[email protected]45fd94172013-11-13 03:29:52318 typedef base::Callback<void(content::BrowserContext*,
319 const Extension*,
320 base::ListValue*)> WillDispatchCallback;
[email protected]6e850922012-12-05 03:22:48321
322 // The event to dispatch.
[email protected]d9e559d2012-07-05 01:04:57323 std::string event_name;
[email protected]6e850922012-12-05 03:22:48324
325 // Arguments to send to the event listener.
[email protected]c9bd90f2012-08-07 23:58:15326 scoped_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48327
[email protected]45fd94172013-11-13 03:29:52328 // If non-NULL, then the event will not be sent to other BrowserContexts
329 // unless the extension has permission (e.g. incognito tab update -> normal
330 // tab only works if extension is allowed incognito access).
331 content::BrowserContext* restrict_to_browser_context;
[email protected]6e850922012-12-05 03:22:48332
333 // If not empty, the event is only sent to extensions with host permissions
334 // for this url.
335 GURL event_url;
336
337 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10338 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48339
340 // Extra information used to filter which events are sent to the listener.
341 EventFilteringInfo filter_info;
342
343 // If specified, this is called before dispatching an event to each
344 // extension. The third argument is a mutable reference to event_args,
345 // allowing the caller to provide different arguments depending on the
346 // extension and profile. This is guaranteed to be called synchronously with
347 // DispatchEvent, so callers don't need to worry about lifetime.
[email protected]2c6e3b04c2014-07-24 12:48:09348 //
349 // NOTE: the Extension argument to this may be NULL because it's possible for
350 // this event to be dispatched to non-extension processes, like WebUI.
[email protected]6e850922012-12-05 03:22:48351 WillDispatchCallback will_dispatch_callback;
352
[email protected]6e850922012-12-05 03:22:48353 Event(const std::string& event_name,
354 scoped_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57355
[email protected]5a38dfd2012-07-23 23:22:10356 Event(const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:15357 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52358 content::BrowserContext* restrict_to_browser_context);
[email protected]f0eb58a2012-12-17 22:10:49359
360 Event(const std::string& event_name,
361 scoped_ptr<base::ListValue> event_args,
[email protected]45fd94172013-11-13 03:29:52362 content::BrowserContext* restrict_to_browser_context,
[email protected]6e850922012-12-05 03:22:48363 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10364 EventRouter::UserGestureState user_gesture,
365 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57366
[email protected]5a38dfd2012-07-23 23:22:10367 ~Event();
[email protected]6e850922012-12-05 03:22:48368
369 // Makes a deep copy of this instance. Ownership is transferred to the
370 // caller.
371 Event* DeepCopy();
[email protected]d9e559d2012-07-05 01:04:57372};
373
[email protected]954e13492012-11-15 03:18:23374struct EventListenerInfo {
375 EventListenerInfo(const std::string& event_name,
[email protected]c761a962013-11-20 04:19:41376 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);
379 // The event name including any sub-event, e.g. "runtime.onStartup" or
380 // "webRequest.onCompleted/123".
[email protected]954e13492012-11-15 03:18:23381 const std::string event_name;
[email protected]c761a962013-11-20 04:19:41382
[email protected]954e13492012-11-15 03:18:23383 const std::string extension_id;
[email protected]c1abb3232014-07-30 18:28:39384 const GURL listener_url;
[email protected]c761a962013-11-20 04:19:41385 content::BrowserContext* browser_context;
[email protected]954e13492012-11-15 03:18:23386};
387
[email protected]5a38dfd2012-07-23 23:22:10388} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57389
[email protected]34423532013-11-21 18:13:10390#endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_