[email protected] | 9e6720a | 2012-01-24 02:30:56 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 5 | #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
| 6 | #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <set> |
| 10 | #include <string> |
[email protected] | f3270a0 | 2012-10-26 15:54:05 | [diff] [blame] | 11 | #include <utility> |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 12 | |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 13 | #include "base/callback.h" |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 14 | #include "base/compiler_specific.h" |
[email protected] | 14c1c23 | 2013-06-11 17:52:44 | [diff] [blame] | 15 | #include "base/containers/hash_tables.h" |
[email protected] | f72d0c6 | 2011-08-31 16:27:44 | [diff] [blame] | 16 | #include "base/memory/linked_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 17 | #include "base/memory/ref_counted.h" |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 18 | #include "base/values.h" |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 19 | #include "chrome/browser/extensions/event_listener_map.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 20 | #include "content/public/browser/notification_observer.h" |
| 21 | #include "content/public/browser/notification_registrar.h" |
[email protected] | 4b3e192 | 2013-02-12 04:45:58 | [diff] [blame] | 22 | #include "extensions/common/event_filtering_info.h" |
[email protected] | b44f8ad | 2012-06-15 20:52:58 | [diff] [blame] | 23 | #include "ipc/ipc_sender.h" |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 24 | |
| 25 | class GURL; |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 26 | class PrefService; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 27 | |
| 28 | namespace content { |
[email protected] | 7061be9 | 2013-02-18 15:44:02 | [diff] [blame] | 29 | class BrowserContext; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 30 | class RenderProcessHost; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 31 | } |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 32 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 33 | namespace extensions { |
[email protected] | c2ed3f69 | 2013-01-31 20:37:36 | [diff] [blame] | 34 | class ActivityLog; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 35 | class Extension; |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 36 | class ExtensionHost; |
[email protected] | 79cb81bb | 2012-09-20 02:23:31 | [diff] [blame] | 37 | class ExtensionPrefs; |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 38 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 39 | struct Event; |
[email protected] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 40 | struct EventDispatchInfo; |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 41 | struct EventListenerInfo; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 42 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 43 | class EventRouter : public content::NotificationObserver, |
| 44 | public EventListenerMap::Delegate { |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 45 | public: |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 46 | // 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] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 54 | // 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] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 58 | // 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] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 63 | virtual void OnListenerAdded(const EventListenerInfo& details) {} |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 64 | // Called when a listener is removed. |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 65 | virtual void OnListenerRemoved(const EventListenerInfo& details) {} |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 66 | }; |
| 67 | |
[email protected] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 68 | // 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] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 75 | // Sends an event via ipc_sender to the given extension. Can be called on any |
| 76 | // thread. |
[email protected] | b44f8ad | 2012-06-15 20:52:58 | [diff] [blame] | 77 | static void DispatchEvent(IPC::Sender* ipc_sender, |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 78 | void* browser_context_id, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 79 | const std::string& extension_id, |
| 80 | const std::string& event_name, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 81 | scoped_ptr<base::ListValue> event_args, |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 82 | UserGestureState user_gesture, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 83 | const EventFilteringInfo& info); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 84 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 85 | // 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] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 89 | virtual ~EventRouter(); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 90 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 91 | // 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] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 95 | void AddEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 96 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 97 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 98 | void RemoveEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 99 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 100 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 101 | |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 102 | EventListenerMap& listeners() { return listeners_; } |
| 103 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 104 | // 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] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 113 | // Sets the observer to be notified whenever an event is dispatched to an |
| 114 | // extension. |
| 115 | void SetEventDispatchObserver(EventDispatchObserver* observer); |
| 116 | |
[email protected] | 3653122 | 2012-02-07 19:41:27 | [diff] [blame] | 117 | // 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] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 126 | // 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] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 141 | // Returns true if there is at least one listener for the given event. |
| 142 | bool HasEventListener(const std::string& event_name); |
| 143 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 144 | // 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] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 148 | // 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] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 154 | // 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] | 42d2474 | 2013-07-23 05:25:55 | [diff] [blame] | 161 | // 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] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 168 | // Record the Event Ack from the renderer. (One less event in-flight.) |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 169 | void OnEventAck(content::BrowserContext* context, |
| 170 | const std::string& extension_id); |
[email protected] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 171 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 172 | private: |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 173 | // 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] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 180 | |
[email protected] | f3270a0 | 2012-10-26 15:54:05 | [diff] [blame] | 181 | // 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] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 186 | // 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] | c2ed3f69 | 2013-01-31 20:37:36 | [diff] [blame] | 191 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 192 | // TODO(gdk): Document this. |
| 193 | static void DispatchExtensionMessage( |
| 194 | IPC::Sender* ipc_sender, |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 195 | void* browser_context_id, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 196 | const std::string& extension_id, |
| 197 | const std::string& event_name, |
| 198 | base::ListValue* event_args, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 199 | UserGestureState user_gesture, |
| 200 | const extensions::EventFilteringInfo& info); |
| 201 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 202 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 203 | const content::NotificationSource& source, |
| 204 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 205 | |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 206 | // 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] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 213 | // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the |
| 214 | // event is broadcast. |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 215 | // An event that just came off the pending list may not be delayed again. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 216 | void DispatchEventImpl(const std::string& restrict_to_extension_id, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 217 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 218 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 219 | // 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] | f3270a0 | 2012-10-26 15:54:05 | [diff] [blame] | 221 | // 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] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 226 | |
| 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] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 230 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 231 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 232 | // 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] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 234 | // |event_args| with the proper arguments to send, which may differ if |
| 235 | // the event crosses the incognito boundary. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 236 | bool CanDispatchEventToBrowserContext(content::BrowserContext* context, |
| 237 | const Extension* extension, |
| 238 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 239 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 240 | // Possibly loads given extension's background page in preparation to |
[email protected] | 6b52757 | 2012-10-12 18:00:43 | [diff] [blame] | 241 | // dispatch an event. Returns true if the event was queued for subsequent |
| 242 | // dispatch, false otherwise. |
| 243 | bool MaybeLoadLazyBackgroundPageToDispatchEvent( |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 244 | content::BrowserContext* context, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 245 | const Extension* extension, |
| 246 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 247 | |
[email protected] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 248 | // 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] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 263 | // Track of the number of dispatched events that have not yet sent an |
| 264 | // ACK from the renderer. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 265 | void IncrementInFlightEvents(content::BrowserContext* context, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 266 | const Extension* extension); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 267 | |
[email protected] | db9f214 | 2013-05-27 22:56:16 | [diff] [blame] | 268 | // static |
| 269 | static void IncrementInFlightEventsOnUI( |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 270 | void* browser_context_id, |
[email protected] | db9f214 | 2013-05-27 22:56:16 | [diff] [blame] | 271 | const std::string& extension_id); |
| 272 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 273 | void DispatchPendingEvent(const linked_ptr<Event>& event, |
[email protected] | b6536df | 2012-03-16 18:55:23 | [diff] [blame] | 274 | ExtensionHost* host); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 275 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 276 | // Implementation of EventListenerMap::Delegate. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 277 | virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; |
| 278 | virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
| 279 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 280 | content::BrowserContext* browser_context_; |
| 281 | |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 282 | // The ExtensionPrefs associated with |browser_context_|. May be NULL in |
| 283 | // tests. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 284 | ExtensionPrefs* extension_prefs_; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 285 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 286 | content::NotificationRegistrar registrar_; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 287 | |
[email protected] | d81d3a19 | 2012-11-09 00:32:50 | [diff] [blame] | 288 | EventListenerMap listeners_; |
[email protected] | 58c9095 | 2012-11-09 00:03:30 | [diff] [blame] | 289 | |
[email protected] | cf118b96 | 2012-12-11 03:08:22 | [diff] [blame] | 290 | typedef base::hash_map<std::string, Observer*> ObserverMap; |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 291 | ObserverMap observers_; |
| 292 | |
[email protected] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 293 | EventDispatchObserver* event_dispatch_observer_; |
| 294 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 295 | DISALLOW_COPY_AND_ASSIGN(EventRouter); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 296 | }; |
| 297 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 298 | struct Event { |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 299 | typedef base::Callback<void(content::BrowserContext*, |
| 300 | const Extension*, |
| 301 | base::ListValue*)> WillDispatchCallback; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 302 | |
| 303 | // The event to dispatch. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 304 | std::string event_name; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 305 | |
| 306 | // Arguments to send to the event listener. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 307 | scoped_ptr<base::ListValue> event_args; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 308 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 309 | // 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] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 313 | |
| 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] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 319 | EventRouter::UserGestureState user_gesture; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 320 | |
| 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] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 333 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 334 | Event(const std::string& event_name, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 335 | scoped_ptr<base::ListValue> event_args, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 336 | content::BrowserContext* restrict_to_browser_context); |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 337 | |
| 338 | Event(const std::string& event_name, |
| 339 | scoped_ptr<base::ListValue> event_args, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 340 | content::BrowserContext* restrict_to_browser_context, |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 341 | const GURL& event_url, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 342 | EventRouter::UserGestureState user_gesture, |
| 343 | const EventFilteringInfo& info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 344 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 345 | ~Event(); |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 346 | |
| 347 | // Makes a deep copy of this instance. Ownership is transferred to the |
| 348 | // caller. |
| 349 | Event* DeepCopy(); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 350 | }; |
| 351 | |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 352 | struct 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] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame^] | 360 | struct 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] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 371 | } // namespace extensions |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 372 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 373 | #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |