blob: ec432e72b38f935aff0e08c61189a5f00cd3b80d [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]cf118b962012-12-11 03:08:2215#include "base/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]2c699652010-10-15 18:22:4126class Profile;
[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]954e13492012-11-15 03:18:2340struct EventListenerInfo;
[email protected]1c321ee52012-05-21 03:02:3441
[email protected]5a38dfd2012-07-23 23:22:1042class EventRouter : public content::NotificationObserver,
43 public EventListenerMap::Delegate {
[email protected]2c699652010-10-15 18:22:4144 public:
[email protected]b085856f2012-03-02 04:37:2545 // These constants convey the state of our knowledge of whether we're in
46 // a user-caused gesture as part of DispatchEvent.
47 enum UserGestureState {
48 USER_GESTURE_UNKNOWN = 0,
49 USER_GESTURE_ENABLED = 1,
50 USER_GESTURE_NOT_ENABLED = 2,
51 };
52
[email protected]c4dc5cc2012-11-09 08:48:3953 // Observers register interest in events with a particular name and are
54 // notified when a listener is added or removed for that |event_name|.
55 class Observer {
56 public:
57 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2358 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3959 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2360 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3961 };
62
[email protected]c9bd90f2012-08-07 23:58:1563 // Sends an event via ipc_sender to the given extension. Can be called on any
64 // thread.
[email protected]b44f8ad2012-06-15 20:52:5865 static void DispatchEvent(IPC::Sender* ipc_sender,
[email protected]c2ed3f692013-01-31 20:37:3666 void* profile_id,
[email protected]c357acb42011-06-09 20:52:4267 const std::string& extension_id,
68 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1569 scoped_ptr<base::ListValue> event_args,
[email protected]b085856f2012-03-02 04:37:2570 const GURL& event_url,
[email protected]d9e559d2012-07-05 01:04:5771 UserGestureState user_gesture,
[email protected]5a38dfd2012-07-23 23:22:1072 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:1973
[email protected]79cb81bb2012-09-20 02:23:3174 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs);
[email protected]5a38dfd2012-07-23 23:22:1075 virtual ~EventRouter();
[email protected]2c699652010-10-15 18:22:4176
[email protected]a7ab1b782010-10-21 23:24:1677 // Add or remove the process/extension pair as a listener for |event_name|.
78 // Note that multiple extensions can share a process due to process
79 // collapsing. Also, a single extension can have 2 processes if it is a split
80 // mode extension.
[email protected]2c699652010-10-15 18:22:4181 void AddEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:3082 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:1683 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:4184 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:3085 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:1686 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:4187
[email protected]f34706be2012-09-04 07:32:0988 EventListenerMap& listeners() { return listeners_; }
89
[email protected]c4dc5cc2012-11-09 08:48:3990 // Registers an observer to be notified when an event listener for
91 // |event_name| is added or removed. There can currently be only one observer
92 // for each distinct |event_name|.
93 void RegisterObserver(Observer* observer,
94 const std::string& event_name);
95
96 // Unregisters an observer from all events.
97 void UnregisterObserver(Observer* observer);
98
[email protected]36531222012-02-07 19:41:2799 // Add or remove the extension as having a lazy background page that listens
100 // to the event. The difference from the above methods is that these will be
101 // remembered even after the process goes away. We use this list to decide
102 // which extension pages to load when dispatching an event.
103 void AddLazyEventListener(const std::string& event_name,
104 const std::string& extension_id);
105 void RemoveLazyEventListener(const std::string& event_name,
106 const std::string& extension_id);
107
[email protected]d9e559d2012-07-05 01:04:57108 // If |add_lazy_listener| is true also add the lazy version of this listener.
109 void AddFilteredEventListener(const std::string& event_name,
110 content::RenderProcessHost* process,
111 const std::string& extension_id,
112 const base::DictionaryValue& filter,
113 bool add_lazy_listener);
114
115 // If |remove_lazy_listener| is true also remove the lazy version of this
116 // listener.
117 void RemoveFilteredEventListener(const std::string& event_name,
118 content::RenderProcessHost* process,
119 const std::string& extension_id,
120 const base::DictionaryValue& filter,
121 bool remove_lazy_listener);
122
[email protected]2c699652010-10-15 18:22:41123 // Returns true if there is at least one listener for the given event.
124 bool HasEventListener(const std::string& event_name);
125
[email protected]a7ab1b782010-10-21 23:24:16126 // Returns true if the extension is listening to the given event.
127 bool ExtensionHasEventListener(const std::string& extension_id,
128 const std::string& event_name);
129
[email protected]6e850922012-12-05 03:22:48130 // Broadcasts an event to every listener registered for that event.
131 virtual void BroadcastEvent(scoped_ptr<Event> event);
132
133 // Dispatches an event to the given extension.
134 virtual void DispatchEventToExtension(const std::string& extension_id,
135 scoped_ptr<Event> event);
136
[email protected]89102012011-11-01 21:23:56137 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]7042b682012-04-19 22:57:51138 void OnEventAck(Profile* profile, const std::string& extension_id);
[email protected]89102012011-11-01 21:23:56139
[email protected]fb6ff23b2012-03-13 23:13:42140 private:
[email protected]61f5fc82012-02-15 20:10:45141 // The extension and process that contains the event listener for a given
142 // event.
143 struct ListenerProcess;
144
145 // A map between an event name and a set of extensions that are listening
146 // to that event.
147 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
[email protected]a7ab1b782010-10-21 23:24:16148
[email protected]f3270a02012-10-26 15:54:05149 // An identifier for an event dispatch that is used to prevent double dispatch
150 // due to race conditions between the direct and lazy dispatch paths.
151 typedef std::pair<const content::BrowserContext*, std::string>
152 EventDispatchIdentifier;
153
[email protected]c2ed3f692013-01-31 20:37:36154 // Records an event notification in the extension activity log. Can be
155 // called from any thread.
156 static void LogExtensionEventMessage(void* profile_id,
157 const std::string& extension_id,
158 const std::string& event_name,
159 scoped_ptr<ListValue> event_args);
160
[email protected]c9bd90f2012-08-07 23:58:15161 // TODO(gdk): Document this.
162 static void DispatchExtensionMessage(
163 IPC::Sender* ipc_sender,
[email protected]c2ed3f692013-01-31 20:37:36164 void* profile_id,
[email protected]c9bd90f2012-08-07 23:58:15165 const std::string& extension_id,
166 const std::string& event_name,
167 base::ListValue* event_args,
168 const GURL& event_url,
169 UserGestureState user_gesture,
170 const extensions::EventFilteringInfo& info);
171
[email protected]432115822011-07-10 15:52:27172 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53173 const content::NotificationSource& source,
174 const content::NotificationDetails& details) OVERRIDE;
[email protected]2c699652010-10-15 18:22:41175
[email protected]61f5fc82012-02-15 20:10:45176 // Returns true if the given listener map contains a event listeners for
177 // the given event. If |extension_id| is non-empty, we also check that that
178 // extension is one of the listeners.
179 bool HasEventListenerImpl(const ListenerMap& listeners,
180 const std::string& extension_id,
181 const std::string& event_name);
182
[email protected]d9e559d2012-07-05 01:04:57183 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
184 // event is broadcast.
[email protected]fb6ff23b2012-03-13 23:13:42185 // An event that just came off the pending list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57186 void DispatchEventImpl(const std::string& restrict_to_extension_id,
[email protected]5a38dfd2012-07-23 23:22:10187 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42188
[email protected]d9e559d2012-07-05 01:04:57189 // Ensures that all lazy background pages that are interested in the given
190 // event are loaded, and queues the event if the page is not ready yet.
[email protected]f3270a02012-10-26 15:54:05191 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy
192 // event dispatch that is queued.
193 void DispatchLazyEvent(const std::string& extension_id,
194 const linked_ptr<Event>& event,
195 std::set<EventDispatchIdentifier>* already_dispatched);
[email protected]d9e559d2012-07-05 01:04:57196
197 // Dispatches the event to the specified extension running in |process|.
198 void DispatchEventToProcess(const std::string& extension_id,
199 content::RenderProcessHost* process,
[email protected]5a38dfd2012-07-23 23:22:10200 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42201
202 // Returns false when the event is scoped to a profile and the listening
203 // extension does not have access to events from that profile. Also fills
204 // |event_args| with the proper arguments to send, which may differ if
205 // the event crosses the incognito boundary.
[email protected]5a38dfd2012-07-23 23:22:10206 bool CanDispatchEventToProfile(Profile* profile,
207 const Extension* extension,
[email protected]c43ff6a2012-12-10 19:36:24208 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42209
[email protected]fb6ff23b2012-03-13 23:13:42210 // Possibly loads given extension's background page in preparation to
[email protected]6b527572012-10-12 18:00:43211 // dispatch an event. Returns true if the event was queued for subsequent
212 // dispatch, false otherwise.
213 bool MaybeLoadLazyBackgroundPageToDispatchEvent(
[email protected]fb6ff23b2012-03-13 23:13:42214 Profile* profile,
[email protected]5a38dfd2012-07-23 23:22:10215 const Extension* extension,
216 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42217
[email protected]fb6ff23b2012-03-13 23:13:42218 // Track of the number of dispatched events that have not yet sent an
219 // ACK from the renderer.
[email protected]1c321ee52012-05-21 03:02:34220 void IncrementInFlightEvents(Profile* profile,
[email protected]5a38dfd2012-07-23 23:22:10221 const Extension* extension);
[email protected]fb6ff23b2012-03-13 23:13:42222
[email protected]5a38dfd2012-07-23 23:22:10223 void DispatchPendingEvent(const linked_ptr<Event>& event,
[email protected]b6536df2012-03-16 18:55:23224 ExtensionHost* host);
[email protected]fb6ff23b2012-03-13 23:13:42225
[email protected]5a38dfd2012-07-23 23:22:10226 // Implementation of EventListenerMap::Delegate.
[email protected]d9e559d2012-07-05 01:04:57227 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE;
228 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE;
229
[email protected]2c699652010-10-15 18:22:41230 Profile* profile_;
231
[email protected]6c2381d2011-10-19 02:52:53232 content::NotificationRegistrar registrar_;
[email protected]2c699652010-10-15 18:22:41233
[email protected]d81d3a192012-11-09 00:32:50234 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30235
[email protected]cf118b962012-12-11 03:08:22236 typedef base::hash_map<std::string, Observer*> ObserverMap;
[email protected]c4dc5cc2012-11-09 08:48:39237 ObserverMap observers_;
238
[email protected]c2ed3f692013-01-31 20:37:36239 ActivityLog* activity_log_;
240
[email protected]79cb81bb2012-09-20 02:23:31241 // True if we should dispatch the event signalling that Chrome was updated
242 // upon loading an extension.
243 bool dispatch_chrome_updated_event_;
244
[email protected]5a38dfd2012-07-23 23:22:10245 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41246};
247
[email protected]5a38dfd2012-07-23 23:22:10248struct Event {
[email protected]6e850922012-12-05 03:22:48249 typedef base::Callback<
250 void(Profile*, const Extension*, base::ListValue*)> WillDispatchCallback;
251
252 // The event to dispatch.
[email protected]d9e559d2012-07-05 01:04:57253 std::string event_name;
[email protected]6e850922012-12-05 03:22:48254
255 // Arguments to send to the event listener.
[email protected]c9bd90f2012-08-07 23:58:15256 scoped_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48257
[email protected]6e850922012-12-05 03:22:48258 // If non-NULL, then the event will not be sent to other profiles unless the
259 // extension has permission (e.g. incognito tab update -> normal profile only
260 // works if extension is allowed incognito access).
261 Profile* restrict_to_profile;
262
263 // If not empty, the event is only sent to extensions with host permissions
264 // for this url.
265 GURL event_url;
266
267 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10268 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48269
270 // Extra information used to filter which events are sent to the listener.
271 EventFilteringInfo filter_info;
272
273 // If specified, this is called before dispatching an event to each
274 // extension. The third argument is a mutable reference to event_args,
275 // allowing the caller to provide different arguments depending on the
276 // extension and profile. This is guaranteed to be called synchronously with
277 // DispatchEvent, so callers don't need to worry about lifetime.
278 WillDispatchCallback will_dispatch_callback;
279
280 Event(const std::string& event_name,
281 scoped_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57282
[email protected]5a38dfd2012-07-23 23:22:10283 Event(const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:15284 scoped_ptr<base::ListValue> event_args,
[email protected]f0eb58a2012-12-17 22:10:49285 Profile* restrict_to_profile);
286
287 Event(const std::string& event_name,
288 scoped_ptr<base::ListValue> event_args,
[email protected]5a38dfd2012-07-23 23:22:10289 Profile* restrict_to_profile,
[email protected]6e850922012-12-05 03:22:48290 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10291 EventRouter::UserGestureState user_gesture,
292 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57293
[email protected]5a38dfd2012-07-23 23:22:10294 ~Event();
[email protected]6e850922012-12-05 03:22:48295
296 // Makes a deep copy of this instance. Ownership is transferred to the
297 // caller.
298 Event* DeepCopy();
[email protected]d9e559d2012-07-05 01:04:57299};
300
[email protected]954e13492012-11-15 03:18:23301struct EventListenerInfo {
302 EventListenerInfo(const std::string& event_name,
303 const std::string& extension_id);
304
305 const std::string event_name;
306 const std::string extension_id;
307};
308
[email protected]5a38dfd2012-07-23 23:22:10309} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57310
[email protected]5a38dfd2012-07-23 23:22:10311#endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_