blob: ad8b72be311e85a5e8d9450a101f9d898ab3044b [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]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]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]954e13492012-11-15 03:18:2340struct EventListenerInfo;
[email protected]1c321ee2012-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]e74d43c72013-05-17 19:01:4153 // The pref key for the list of event names for which an extension has
54 // registered from its lazy background page.
55 static const char kRegisteredEvents[];
56
[email protected]c4dc5cc2012-11-09 08:48:3957 // Observers register interest in events with a particular name and are
58 // notified when a listener is added or removed for that |event_name|.
59 class Observer {
60 public:
61 // Called when a listener is added.
[email protected]954e13492012-11-15 03:18:2362 virtual void OnListenerAdded(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3963 // Called when a listener is removed.
[email protected]954e13492012-11-15 03:18:2364 virtual void OnListenerRemoved(const EventListenerInfo& details) {}
[email protected]c4dc5cc2012-11-09 08:48:3965 };
66
[email protected]c9bd90f2012-08-07 23:58:1567 // Sends an event via ipc_sender to the given extension. Can be called on any
68 // thread.
[email protected]b44f8ad2012-06-15 20:52:5869 static void DispatchEvent(IPC::Sender* ipc_sender,
[email protected]c2ed3f692013-01-31 20:37:3670 void* profile_id,
[email protected]c357acb42011-06-09 20:52:4271 const std::string& extension_id,
72 const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:1573 scoped_ptr<base::ListValue> event_args,
[email protected]d9e559d2012-07-05 01:04:5774 UserGestureState user_gesture,
[email protected]5a38dfd2012-07-23 23:22:1075 const EventFilteringInfo& info);
[email protected]5a7b5eaf2010-11-02 20:52:1976
[email protected]79cb81bb2012-09-20 02:23:3177 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs);
[email protected]5a38dfd2012-07-23 23:22:1078 virtual ~EventRouter();
[email protected]2c699652010-10-15 18:22:4179
[email protected]a7ab1b782010-10-21 23:24:1680 // Add or remove the process/extension pair as a listener for |event_name|.
81 // Note that multiple extensions can share a process due to process
82 // collapsing. Also, a single extension can have 2 processes if it is a split
83 // mode extension.
[email protected]2c699652010-10-15 18:22:4184 void AddEventListener(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 void RemoveEventListener(const std::string& event_name,
[email protected]f3b1a082011-11-18 00:34:3088 content::RenderProcessHost* process,
[email protected]a7ab1b782010-10-21 23:24:1689 const std::string& extension_id);
[email protected]2c699652010-10-15 18:22:4190
[email protected]f34706be2012-09-04 07:32:0991 EventListenerMap& listeners() { return listeners_; }
92
[email protected]c4dc5cc2012-11-09 08:48:3993 // Registers an observer to be notified when an event listener for
94 // |event_name| is added or removed. There can currently be only one observer
95 // for each distinct |event_name|.
96 void RegisterObserver(Observer* observer,
97 const std::string& event_name);
98
99 // Unregisters an observer from all events.
100 void UnregisterObserver(Observer* observer);
101
[email protected]36531222012-02-07 19:41:27102 // Add or remove the extension as having a lazy background page that listens
103 // to the event. The difference from the above methods is that these will be
104 // remembered even after the process goes away. We use this list to decide
105 // which extension pages to load when dispatching an event.
106 void AddLazyEventListener(const std::string& event_name,
107 const std::string& extension_id);
108 void RemoveLazyEventListener(const std::string& event_name,
109 const std::string& extension_id);
110
[email protected]d9e559d2012-07-05 01:04:57111 // If |add_lazy_listener| is true also add the lazy version of this listener.
112 void AddFilteredEventListener(const std::string& event_name,
113 content::RenderProcessHost* process,
114 const std::string& extension_id,
115 const base::DictionaryValue& filter,
116 bool add_lazy_listener);
117
118 // If |remove_lazy_listener| is true also remove the lazy version of this
119 // listener.
120 void RemoveFilteredEventListener(const std::string& event_name,
121 content::RenderProcessHost* process,
122 const std::string& extension_id,
123 const base::DictionaryValue& filter,
124 bool remove_lazy_listener);
125
[email protected]2c699652010-10-15 18:22:41126 // Returns true if there is at least one listener for the given event.
127 bool HasEventListener(const std::string& event_name);
128
[email protected]a7ab1b782010-10-21 23:24:16129 // Returns true if the extension is listening to the given event.
130 bool ExtensionHasEventListener(const std::string& extension_id,
131 const std::string& event_name);
132
[email protected]e74d43c72013-05-17 19:01:41133 // Return or set the list of events for which the given extension has
134 // registered.
135 std::set<std::string> GetRegisteredEvents(const std::string& extension_id);
136 void SetRegisteredEvents(const std::string& extension_id,
137 const std::set<std::string>& events);
138
[email protected]6e850922012-12-05 03:22:48139 // Broadcasts an event to every listener registered for that event.
140 virtual void BroadcastEvent(scoped_ptr<Event> event);
141
142 // Dispatches an event to the given extension.
143 virtual void DispatchEventToExtension(const std::string& extension_id,
144 scoped_ptr<Event> event);
145
[email protected]89102012011-11-01 21:23:56146 // Record the Event Ack from the renderer. (One less event in-flight.)
[email protected]7042b682012-04-19 22:57:51147 void OnEventAck(Profile* profile, const std::string& extension_id);
[email protected]89102012011-11-01 21:23:56148
[email protected]fb6ff23b2012-03-13 23:13:42149 private:
[email protected]61f5fc82012-02-15 20:10:45150 // The extension and process that contains the event listener for a given
151 // event.
152 struct ListenerProcess;
153
154 // A map between an event name and a set of extensions that are listening
155 // to that event.
156 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap;
[email protected]a7ab1b782010-10-21 23:24:16157
[email protected]f3270a02012-10-26 15:54:05158 // An identifier for an event dispatch that is used to prevent double dispatch
159 // due to race conditions between the direct and lazy dispatch paths.
160 typedef std::pair<const content::BrowserContext*, std::string>
161 EventDispatchIdentifier;
162
[email protected]c2ed3f692013-01-31 20:37:36163 // Records an event notification in the extension activity log. Can be
164 // called from any thread.
165 static void LogExtensionEventMessage(void* profile_id,
166 const std::string& extension_id,
167 const std::string& event_name,
168 scoped_ptr<ListValue> event_args);
169
[email protected]c9bd90f2012-08-07 23:58:15170 // TODO(gdk): Document this.
171 static void DispatchExtensionMessage(
172 IPC::Sender* ipc_sender,
[email protected]c2ed3f692013-01-31 20:37:36173 void* profile_id,
[email protected]c9bd90f2012-08-07 23:58:15174 const std::string& extension_id,
175 const std::string& event_name,
176 base::ListValue* event_args,
[email protected]c9bd90f2012-08-07 23:58:15177 UserGestureState user_gesture,
178 const extensions::EventFilteringInfo& info);
179
[email protected]432115822011-07-10 15:52:27180 virtual void Observe(int type,
[email protected]6c2381d2011-10-19 02:52:53181 const content::NotificationSource& source,
182 const content::NotificationDetails& details) OVERRIDE;
[email protected]2c699652010-10-15 18:22:41183
[email protected]61f5fc82012-02-15 20:10:45184 // Returns true if the given listener map contains a event listeners for
185 // the given event. If |extension_id| is non-empty, we also check that that
186 // extension is one of the listeners.
187 bool HasEventListenerImpl(const ListenerMap& listeners,
188 const std::string& extension_id,
189 const std::string& event_name);
190
[email protected]d9e559d2012-07-05 01:04:57191 // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the
192 // event is broadcast.
[email protected]fb6ff23b2012-03-13 23:13:42193 // An event that just came off the pending list may not be delayed again.
[email protected]d9e559d2012-07-05 01:04:57194 void DispatchEventImpl(const std::string& restrict_to_extension_id,
[email protected]5a38dfd2012-07-23 23:22:10195 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42196
[email protected]d9e559d2012-07-05 01:04:57197 // Ensures that all lazy background pages that are interested in the given
198 // event are loaded, and queues the event if the page is not ready yet.
[email protected]f3270a02012-10-26 15:54:05199 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy
200 // event dispatch that is queued.
201 void DispatchLazyEvent(const std::string& extension_id,
202 const linked_ptr<Event>& event,
203 std::set<EventDispatchIdentifier>* already_dispatched);
[email protected]d9e559d2012-07-05 01:04:57204
205 // Dispatches the event to the specified extension running in |process|.
206 void DispatchEventToProcess(const std::string& extension_id,
207 content::RenderProcessHost* process,
[email protected]5a38dfd2012-07-23 23:22:10208 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42209
210 // Returns false when the event is scoped to a profile and the listening
211 // extension does not have access to events from that profile. Also fills
212 // |event_args| with the proper arguments to send, which may differ if
213 // the event crosses the incognito boundary.
[email protected]5a38dfd2012-07-23 23:22:10214 bool CanDispatchEventToProfile(Profile* profile,
215 const Extension* extension,
[email protected]c43ff6a2012-12-10 19:36:24216 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42217
[email protected]fb6ff23b2012-03-13 23:13:42218 // Possibly loads given extension's background page in preparation to
[email protected]6b527572012-10-12 18:00:43219 // dispatch an event. Returns true if the event was queued for subsequent
220 // dispatch, false otherwise.
221 bool MaybeLoadLazyBackgroundPageToDispatchEvent(
[email protected]fb6ff23b2012-03-13 23:13:42222 Profile* profile,
[email protected]5a38dfd2012-07-23 23:22:10223 const Extension* extension,
224 const linked_ptr<Event>& event);
[email protected]fb6ff23b2012-03-13 23:13:42225
[email protected]e74d43c72013-05-17 19:01:41226 // Returns true if registered events are from this version of Chrome. Else,
227 // clear them, and return false.
228 bool CheckRegisteredEventsUpToDate();
229
230 // Adds a filter to an event.
231 void AddFilterToEvent(const std::string& event_name,
232 const std::string& extension_id,
233 const base::DictionaryValue* filter);
234
235 // Removes a filter from an event.
236 void RemoveFilterFromEvent(const std::string& event_name,
237 const std::string& extension_id,
238 const base::DictionaryValue* filter);
239
240 // Returns the dictionary of event filters that the given extension has
241 // registered.
242 const base::DictionaryValue* GetFilteredEvents(
243 const std::string& extension_id);
244
[email protected]fb6ff23b2012-03-13 23:13:42245 // Track of the number of dispatched events that have not yet sent an
246 // ACK from the renderer.
[email protected]1c321ee2012-05-21 03:02:34247 void IncrementInFlightEvents(Profile* profile,
[email protected]5a38dfd2012-07-23 23:22:10248 const Extension* extension);
[email protected]fb6ff23b2012-03-13 23:13:42249
[email protected]db9f2142013-05-27 22:56:16250 // static
251 static void IncrementInFlightEventsOnUI(
252 void* profile_id,
253 const std::string& extension_id);
254
[email protected]5a38dfd2012-07-23 23:22:10255 void DispatchPendingEvent(const linked_ptr<Event>& event,
[email protected]b6536df2012-03-16 18:55:23256 ExtensionHost* host);
[email protected]fb6ff23b2012-03-13 23:13:42257
[email protected]5a38dfd2012-07-23 23:22:10258 // Implementation of EventListenerMap::Delegate.
[email protected]d9e559d2012-07-05 01:04:57259 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE;
260 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE;
261
[email protected]2c699652010-10-15 18:22:41262 Profile* profile_;
263
[email protected]6c2381d2011-10-19 02:52:53264 content::NotificationRegistrar registrar_;
[email protected]2c699652010-10-15 18:22:41265
[email protected]d81d3a192012-11-09 00:32:50266 EventListenerMap listeners_;
[email protected]58c90952012-11-09 00:03:30267
[email protected]cf118b962012-12-11 03:08:22268 typedef base::hash_map<std::string, Observer*> ObserverMap;
[email protected]c4dc5cc2012-11-09 08:48:39269 ObserverMap observers_;
270
[email protected]c2ed3f692013-01-31 20:37:36271 ActivityLog* activity_log_;
272
[email protected]79cb81bb2012-09-20 02:23:31273 // True if we should dispatch the event signalling that Chrome was updated
274 // upon loading an extension.
275 bool dispatch_chrome_updated_event_;
276
[email protected]5a38dfd2012-07-23 23:22:10277 DISALLOW_COPY_AND_ASSIGN(EventRouter);
[email protected]2c699652010-10-15 18:22:41278};
279
[email protected]5a38dfd2012-07-23 23:22:10280struct Event {
[email protected]6e850922012-12-05 03:22:48281 typedef base::Callback<
282 void(Profile*, const Extension*, base::ListValue*)> WillDispatchCallback;
283
284 // The event to dispatch.
[email protected]d9e559d2012-07-05 01:04:57285 std::string event_name;
[email protected]6e850922012-12-05 03:22:48286
287 // Arguments to send to the event listener.
[email protected]c9bd90f2012-08-07 23:58:15288 scoped_ptr<base::ListValue> event_args;
[email protected]6e850922012-12-05 03:22:48289
[email protected]6e850922012-12-05 03:22:48290 // If non-NULL, then the event will not be sent to other profiles unless the
291 // extension has permission (e.g. incognito tab update -> normal profile only
292 // works if extension is allowed incognito access).
293 Profile* restrict_to_profile;
294
295 // If not empty, the event is only sent to extensions with host permissions
296 // for this url.
297 GURL event_url;
298
299 // Whether a user gesture triggered the event.
[email protected]5a38dfd2012-07-23 23:22:10300 EventRouter::UserGestureState user_gesture;
[email protected]6e850922012-12-05 03:22:48301
302 // Extra information used to filter which events are sent to the listener.
303 EventFilteringInfo filter_info;
304
305 // If specified, this is called before dispatching an event to each
306 // extension. The third argument is a mutable reference to event_args,
307 // allowing the caller to provide different arguments depending on the
308 // extension and profile. This is guaranteed to be called synchronously with
309 // DispatchEvent, so callers don't need to worry about lifetime.
310 WillDispatchCallback will_dispatch_callback;
311
312 Event(const std::string& event_name,
313 scoped_ptr<base::ListValue> event_args);
[email protected]d9e559d2012-07-05 01:04:57314
[email protected]5a38dfd2012-07-23 23:22:10315 Event(const std::string& event_name,
[email protected]c9bd90f2012-08-07 23:58:15316 scoped_ptr<base::ListValue> event_args,
[email protected]f0eb58a2012-12-17 22:10:49317 Profile* restrict_to_profile);
318
319 Event(const std::string& event_name,
320 scoped_ptr<base::ListValue> event_args,
[email protected]5a38dfd2012-07-23 23:22:10321 Profile* restrict_to_profile,
[email protected]6e850922012-12-05 03:22:48322 const GURL& event_url,
[email protected]5a38dfd2012-07-23 23:22:10323 EventRouter::UserGestureState user_gesture,
324 const EventFilteringInfo& info);
[email protected]d9e559d2012-07-05 01:04:57325
[email protected]5a38dfd2012-07-23 23:22:10326 ~Event();
[email protected]6e850922012-12-05 03:22:48327
328 // Makes a deep copy of this instance. Ownership is transferred to the
329 // caller.
330 Event* DeepCopy();
[email protected]d9e559d2012-07-05 01:04:57331};
332
[email protected]954e13492012-11-15 03:18:23333struct EventListenerInfo {
334 EventListenerInfo(const std::string& event_name,
335 const std::string& extension_id);
336
337 const std::string event_name;
338 const std::string extension_id;
339};
340
[email protected]5a38dfd2012-07-23 23:22:10341} // namespace extensions
[email protected]d9e559d2012-07-05 01:04:57342
[email protected]5a38dfd2012-07-23 23:22:10343#endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_