[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] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 5 | #ifndef EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
| 6 | #define EXTENSIONS_BROWSER_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" |
avi | c9cec10 | 2015-12-23 00:39:26 | [diff] [blame] | 16 | #include "base/macros.h" |
[email protected] | f72d0c6 | 2011-08-31 16:27:44 | [diff] [blame] | 17 | #include "base/memory/linked_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 18 | #include "base/memory/ref_counted.h" |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 19 | #include "base/scoped_observer.h" |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 20 | #include "base/values.h" |
juncai | cf52333 | 2015-06-04 00:14:04 | [diff] [blame] | 21 | #include "components/keyed_service/core/keyed_service.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 22 | #include "content/public/browser/notification_observer.h" |
| 23 | #include "content/public/browser/notification_registrar.h" |
amistry | 69e9ee42 | 2015-05-22 07:40:25 | [diff] [blame] | 24 | #include "content/public/browser/render_process_host_observer.h" |
[email protected] | a0e26d4 | 2013-11-20 07:04:04 | [diff] [blame] | 25 | #include "extensions/browser/event_listener_map.h" |
kalman | f1b4d78 | 2015-06-24 21:14:05 | [diff] [blame] | 26 | #include "extensions/browser/extension_event_histogram_value.h" |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 27 | #include "extensions/browser/extension_registry_observer.h" |
[email protected] | 4b3e192 | 2013-02-12 04:45:58 | [diff] [blame] | 28 | #include "extensions/common/event_filtering_info.h" |
[email protected] | b44f8ad | 2012-06-15 20:52:58 | [diff] [blame] | 29 | #include "ipc/ipc_sender.h" |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 30 | #include "url/gurl.h" |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 31 | |
| 32 | class GURL; |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 33 | class PrefService; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 34 | |
| 35 | namespace content { |
[email protected] | 7061be9 | 2013-02-18 15:44:02 | [diff] [blame] | 36 | class BrowserContext; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 37 | class RenderProcessHost; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 38 | } |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 39 | |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 40 | namespace extensions { |
[email protected] | c2ed3f69 | 2013-01-31 20:37:36 | [diff] [blame] | 41 | class ActivityLog; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 42 | class Extension; |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 43 | class ExtensionHost; |
[email protected] | 79cb81bb | 2012-09-20 02:23:31 | [diff] [blame] | 44 | class ExtensionPrefs; |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 45 | class ExtensionRegistry; |
[email protected] | 3a1dc57 | 2012-07-31 22:25:13 | [diff] [blame] | 46 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 47 | struct Event; |
[email protected] | 72e280f9 | 2013-11-20 02:02:34 | [diff] [blame] | 48 | struct EventDispatchInfo; |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 49 | struct EventListenerInfo; |
[email protected] | 1c321ee5 | 2012-05-21 03:02:34 | [diff] [blame] | 50 | |
juncai | cf52333 | 2015-06-04 00:14:04 | [diff] [blame] | 51 | class EventRouter : public KeyedService, |
| 52 | public content::NotificationObserver, |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 53 | public ExtensionRegistryObserver, |
amistry | 69e9ee42 | 2015-05-22 07:40:25 | [diff] [blame] | 54 | public EventListenerMap::Delegate, |
| 55 | public content::RenderProcessHostObserver { |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 56 | public: |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 57 | // These constants convey the state of our knowledge of whether we're in |
| 58 | // a user-caused gesture as part of DispatchEvent. |
| 59 | enum UserGestureState { |
| 60 | USER_GESTURE_UNKNOWN = 0, |
| 61 | USER_GESTURE_ENABLED = 1, |
| 62 | USER_GESTURE_NOT_ENABLED = 2, |
| 63 | }; |
| 64 | |
[email protected] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 65 | // The pref key for the list of event names for which an extension has |
| 66 | // registered from its lazy background page. |
| 67 | static const char kRegisteredEvents[]; |
| 68 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 69 | // Observers register interest in events with a particular name and are |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 70 | // notified when a listener is added or removed. Observers are matched by |
| 71 | // the base name of the event (e.g. adding an event listener for event name |
| 72 | // "foo.onBar/123" will trigger observers registered for "foo.onBar"). |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 73 | class Observer { |
| 74 | public: |
| 75 | // Called when a listener is added. |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 76 | virtual void OnListenerAdded(const EventListenerInfo& details) {} |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 77 | // Called when a listener is removed. |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 78 | virtual void OnListenerRemoved(const EventListenerInfo& details) {} |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 79 | }; |
| 80 | |
[email protected] | 3a368a2 | 2014-03-26 19:29:19 | [diff] [blame] | 81 | // Gets the EventRouter for |browser_context|. |
[email protected] | 3a368a2 | 2014-03-26 19:29:19 | [diff] [blame] | 82 | static EventRouter* Get(content::BrowserContext* browser_context); |
| 83 | |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 84 | // Converts event names like "foo.onBar/123" into "foo.onBar". Event names |
| 85 | // without a "/" are returned unchanged. |
| 86 | static std::string GetBaseEventName(const std::string& full_event_name); |
| 87 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 88 | // Sends an event via ipc_sender to the given extension. Can be called on any |
| 89 | // thread. |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 90 | // |
| 91 | // It is very rare to call this function directly. Instead use the instance |
| 92 | // methods BroadcastEvent or DispatchEventToExtension. |
| 93 | static void DispatchEventToSender(IPC::Sender* ipc_sender, |
| 94 | void* browser_context_id, |
| 95 | const std::string& extension_id, |
| 96 | events::HistogramValue histogram_value, |
| 97 | const std::string& event_name, |
| 98 | scoped_ptr<base::ListValue> event_args, |
| 99 | UserGestureState user_gesture, |
| 100 | const EventFilteringInfo& info); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 101 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 102 | // An EventRouter is shared between |browser_context| and its associated |
| 103 | // incognito context. |extension_prefs| may be NULL in tests. |
| 104 | EventRouter(content::BrowserContext* browser_context, |
| 105 | ExtensionPrefs* extension_prefs); |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 106 | ~EventRouter() override; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 107 | |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 108 | // Add or remove an extension as an event listener for |event_name|. |
| 109 | // |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 110 | // Note that multiple extensions can share a process due to process |
| 111 | // collapsing. Also, a single extension can have 2 processes if it is a split |
| 112 | // mode extension. |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 113 | void AddEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 114 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 115 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 116 | void RemoveEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 117 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 118 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 119 | |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 120 | // Add or remove a URL as an event listener for |event_name|. |
| 121 | void AddEventListenerForURL(const std::string& event_name, |
| 122 | content::RenderProcessHost* process, |
| 123 | const GURL& listener_url); |
| 124 | void RemoveEventListenerForURL(const std::string& event_name, |
| 125 | content::RenderProcessHost* process, |
| 126 | const GURL& listener_url); |
| 127 | |
[email protected] | f34706be | 2012-09-04 07:32:09 | [diff] [blame] | 128 | EventListenerMap& listeners() { return listeners_; } |
| 129 | |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 130 | // Registers an observer to be notified when an event listener for |
| 131 | // |event_name| is added or removed. There can currently be only one observer |
| 132 | // for each distinct |event_name|. |
| 133 | void RegisterObserver(Observer* observer, |
| 134 | const std::string& event_name); |
| 135 | |
| 136 | // Unregisters an observer from all events. |
| 137 | void UnregisterObserver(Observer* observer); |
| 138 | |
[email protected] | 3653122 | 2012-02-07 19:41:27 | [diff] [blame] | 139 | // Add or remove the extension as having a lazy background page that listens |
| 140 | // to the event. The difference from the above methods is that these will be |
| 141 | // remembered even after the process goes away. We use this list to decide |
| 142 | // which extension pages to load when dispatching an event. |
| 143 | void AddLazyEventListener(const std::string& event_name, |
| 144 | const std::string& extension_id); |
| 145 | void RemoveLazyEventListener(const std::string& event_name, |
| 146 | const std::string& extension_id); |
| 147 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 148 | // If |add_lazy_listener| is true also add the lazy version of this listener. |
| 149 | void AddFilteredEventListener(const std::string& event_name, |
| 150 | content::RenderProcessHost* process, |
| 151 | const std::string& extension_id, |
| 152 | const base::DictionaryValue& filter, |
| 153 | bool add_lazy_listener); |
| 154 | |
| 155 | // If |remove_lazy_listener| is true also remove the lazy version of this |
| 156 | // listener. |
| 157 | void RemoveFilteredEventListener(const std::string& event_name, |
| 158 | content::RenderProcessHost* process, |
| 159 | const std::string& extension_id, |
| 160 | const base::DictionaryValue& filter, |
| 161 | bool remove_lazy_listener); |
| 162 | |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 163 | // Returns true if there is at least one listener for the given event. |
| 164 | bool HasEventListener(const std::string& event_name); |
| 165 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 166 | // Returns true if the extension is listening to the given event. |
asargent | d50b18c | 2016-04-21 01:17:16 | [diff] [blame^] | 167 | virtual bool ExtensionHasEventListener(const std::string& extension_id, |
| 168 | const std::string& event_name); |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 169 | |
[email protected] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 170 | // Return or set the list of events for which the given extension has |
| 171 | // registered. |
| 172 | std::set<std::string> GetRegisteredEvents(const std::string& extension_id); |
| 173 | void SetRegisteredEvents(const std::string& extension_id, |
| 174 | const std::set<std::string>& events); |
| 175 | |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 176 | // Broadcasts an event to every listener registered for that event. |
| 177 | virtual void BroadcastEvent(scoped_ptr<Event> event); |
| 178 | |
| 179 | // Dispatches an event to the given extension. |
| 180 | virtual void DispatchEventToExtension(const std::string& extension_id, |
| 181 | scoped_ptr<Event> event); |
| 182 | |
[email protected] | 42d2474 | 2013-07-23 05:25:55 | [diff] [blame] | 183 | // Dispatches |event| to the given extension as if the extension has a lazy |
| 184 | // listener for it. NOTE: This should be used rarely, for dispatching events |
| 185 | // to extensions that haven't had a chance to add their own listeners yet, eg: |
| 186 | // newly installed extensions. |
| 187 | void DispatchEventWithLazyListener(const std::string& extension_id, |
| 188 | scoped_ptr<Event> event); |
| 189 | |
[email protected] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 190 | // Record the Event Ack from the renderer. (One less event in-flight.) |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 191 | void OnEventAck(content::BrowserContext* context, |
| 192 | const std::string& extension_id); |
[email protected] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 193 | |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 194 | // Reports UMA for an event dispatched to |extension| with histogram value |
| 195 | // |histogram_value|. Must be called on the UI thread. |
| 196 | // |
| 197 | // |did_enqueue| should be true if the event was queued waiting for a process |
| 198 | // to start, like an event page. |
| 199 | void ReportEvent(events::HistogramValue histogram_value, |
| 200 | const Extension* extension, |
| 201 | bool did_enqueue); |
| 202 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 203 | private: |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 204 | friend class EventRouterTest; |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 205 | |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 206 | // The extension and process that contains the event listener for a given |
| 207 | // event. |
| 208 | struct ListenerProcess; |
| 209 | |
| 210 | // A map between an event name and a set of extensions that are listening |
| 211 | // to that event. |
| 212 | typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 213 | |
[email protected] | f3270a0 | 2012-10-26 15:54:05 | [diff] [blame] | 214 | // An identifier for an event dispatch that is used to prevent double dispatch |
| 215 | // due to race conditions between the direct and lazy dispatch paths. |
| 216 | typedef std::pair<const content::BrowserContext*, std::string> |
| 217 | EventDispatchIdentifier; |
| 218 | |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 219 | // TODO(gdk): Document this. |
| 220 | static void DispatchExtensionMessage( |
| 221 | IPC::Sender* ipc_sender, |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 222 | void* browser_context_id, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 223 | const std::string& extension_id, |
chirantan | 669993c | 2015-03-05 23:38:33 | [diff] [blame] | 224 | int event_id, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 225 | const std::string& event_name, |
| 226 | base::ListValue* event_args, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 227 | UserGestureState user_gesture, |
| 228 | const extensions::EventFilteringInfo& info); |
| 229 | |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 230 | void Observe(int type, |
| 231 | const content::NotificationSource& source, |
| 232 | const content::NotificationDetails& details) override; |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 233 | // ExtensionRegistryObserver implementation. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 234 | void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 235 | const Extension* extension) override; |
| 236 | void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 237 | const Extension* extension, |
| 238 | UnloadedExtensionInfo::Reason reason) override; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 239 | |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 240 | // Returns true if the given listener map contains a event listeners for |
| 241 | // the given event. If |extension_id| is non-empty, we also check that that |
| 242 | // extension is one of the listeners. |
| 243 | bool HasEventListenerImpl(const ListenerMap& listeners, |
| 244 | const std::string& extension_id, |
| 245 | const std::string& event_name); |
| 246 | |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 247 | // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
| 248 | // empty, the event is broadcast. An event that just came off the pending |
| 249 | // list may not be delayed again. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 250 | void DispatchEventImpl(const std::string& restrict_to_extension_id, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 251 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 252 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 253 | // Ensures that all lazy background pages that are interested in the given |
| 254 | // 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] | 255 | // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy |
| 256 | // event dispatch that is queued. |
| 257 | void DispatchLazyEvent(const std::string& extension_id, |
| 258 | const linked_ptr<Event>& event, |
lionel.g.landwerlin | b87f1c9c | 2015-07-27 16:53:14 | [diff] [blame] | 259 | std::set<EventDispatchIdentifier>* already_dispatched, |
| 260 | const base::DictionaryValue* listener_filter); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 261 | |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 262 | // Dispatches the event to the specified extension or URL running in |
| 263 | // |process|. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 264 | void DispatchEventToProcess(const std::string& extension_id, |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 265 | const GURL& listener_url, |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 266 | content::RenderProcessHost* process, |
lionel.g.landwerlin | b87f1c9c | 2015-07-27 16:53:14 | [diff] [blame] | 267 | const linked_ptr<Event>& event, |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 268 | const base::DictionaryValue* listener_filter, |
| 269 | bool did_enqueue); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 270 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 271 | // Returns false when the event is scoped to a context and the listening |
| 272 | // extension does not have access to events from that context. Also fills |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 273 | // |event_args| with the proper arguments to send, which may differ if |
| 274 | // the event crosses the incognito boundary. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 275 | bool CanDispatchEventToBrowserContext(content::BrowserContext* context, |
| 276 | const Extension* extension, |
| 277 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 278 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 279 | // Possibly loads given extension's background page in preparation to |
[email protected] | 6b52757 | 2012-10-12 18:00:43 | [diff] [blame] | 280 | // dispatch an event. Returns true if the event was queued for subsequent |
| 281 | // dispatch, false otherwise. |
| 282 | bool MaybeLoadLazyBackgroundPageToDispatchEvent( |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 283 | content::BrowserContext* context, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 284 | const Extension* extension, |
lionel.g.landwerlin | b87f1c9c | 2015-07-27 16:53:14 | [diff] [blame] | 285 | const linked_ptr<Event>& event, |
| 286 | const base::DictionaryValue* listener_filter); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 287 | |
[email protected] | e74d43c7 | 2013-05-17 19:01:41 | [diff] [blame] | 288 | // Adds a filter to an event. |
| 289 | void AddFilterToEvent(const std::string& event_name, |
| 290 | const std::string& extension_id, |
| 291 | const base::DictionaryValue* filter); |
| 292 | |
| 293 | // Removes a filter from an event. |
| 294 | void RemoveFilterFromEvent(const std::string& event_name, |
| 295 | const std::string& extension_id, |
| 296 | const base::DictionaryValue* filter); |
| 297 | |
| 298 | // Returns the dictionary of event filters that the given extension has |
| 299 | // registered. |
| 300 | const base::DictionaryValue* GetFilteredEvents( |
| 301 | const std::string& extension_id); |
| 302 | |
chirantan | 669993c | 2015-03-05 23:38:33 | [diff] [blame] | 303 | // Track the dispatched events that have not yet sent an ACK from the |
| 304 | // renderer. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 305 | void IncrementInFlightEvents(content::BrowserContext* context, |
chirantan | 669993c | 2015-03-05 23:38:33 | [diff] [blame] | 306 | const Extension* extension, |
| 307 | int event_id, |
| 308 | const std::string& event_name); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 309 | |
[email protected] | db9f214 | 2013-05-27 22:56:16 | [diff] [blame] | 310 | // static |
kalman | a9f6e67a | 2015-08-11 00:22:50 | [diff] [blame] | 311 | static void DoDispatchEventToSenderBookkeepingOnUI( |
| 312 | void* browser_context_id, |
| 313 | const std::string& extension_id, |
| 314 | int event_id, |
| 315 | events::HistogramValue histogram_value, |
| 316 | const std::string& event_name); |
[email protected] | db9f214 | 2013-05-27 22:56:16 | [diff] [blame] | 317 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 318 | void DispatchPendingEvent(const linked_ptr<Event>& event, |
[email protected] | b6536df | 2012-03-16 18:55:23 | [diff] [blame] | 319 | ExtensionHost* host); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 320 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 321 | // Implementation of EventListenerMap::Delegate. |
dcheng | 9168b2f | 2014-10-21 12:38:24 | [diff] [blame] | 322 | void OnListenerAdded(const EventListener* listener) override; |
| 323 | void OnListenerRemoved(const EventListener* listener) override; |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 324 | |
amistry | 69e9ee42 | 2015-05-22 07:40:25 | [diff] [blame] | 325 | // RenderProcessHostObserver implementation. |
| 326 | void RenderProcessExited(content::RenderProcessHost* host, |
| 327 | base::TerminationStatus status, |
| 328 | int exit_code) override; |
| 329 | void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
| 330 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 331 | content::BrowserContext* browser_context_; |
| 332 | |
[email protected] | 513b803 | 2013-11-18 07:47:49 | [diff] [blame] | 333 | // The ExtensionPrefs associated with |browser_context_|. May be NULL in |
| 334 | // tests. |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 335 | ExtensionPrefs* extension_prefs_; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 336 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 337 | content::NotificationRegistrar registrar_; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 338 | |
[email protected] | 4243f9a | 2014-08-04 18:53:12 | [diff] [blame] | 339 | ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 340 | extension_registry_observer_; |
| 341 | |
[email protected] | d81d3a19 | 2012-11-09 00:32:50 | [diff] [blame] | 342 | EventListenerMap listeners_; |
[email protected] | 58c9095 | 2012-11-09 00:03:30 | [diff] [blame] | 343 | |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 344 | // Map from base event name to observer. |
[email protected] | cf118b96 | 2012-12-11 03:08:22 | [diff] [blame] | 345 | typedef base::hash_map<std::string, Observer*> ObserverMap; |
[email protected] | c4dc5cc | 2012-11-09 08:48:39 | [diff] [blame] | 346 | ObserverMap observers_; |
| 347 | |
amistry | 69e9ee42 | 2015-05-22 07:40:25 | [diff] [blame] | 348 | std::set<content::RenderProcessHost*> observed_process_set_; |
| 349 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 350 | DISALLOW_COPY_AND_ASSIGN(EventRouter); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 351 | }; |
| 352 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 353 | struct Event { |
reillyg | 5464e7e | 2014-12-11 00:35:08 | [diff] [blame] | 354 | // This callback should return true if the event should be dispatched to the |
| 355 | // given context and extension, and false otherwise. |
| 356 | typedef base::Callback<bool(content::BrowserContext*, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 357 | const Extension*, |
lionel.g.landwerlin | 7b464ad | 2015-08-11 20:59:09 | [diff] [blame] | 358 | Event*, |
lionel.g.landwerlin | b87f1c9c | 2015-07-27 16:53:14 | [diff] [blame] | 359 | const base::DictionaryValue*)> |
| 360 | WillDispatchCallback; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 361 | |
kalman | f1b4d78 | 2015-06-24 21:14:05 | [diff] [blame] | 362 | // The identifier for the event, for histograms. In most cases this |
| 363 | // correlates 1:1 with |event_name|, in some cases events will generate |
| 364 | // their own names, but they cannot generate their own identifier. |
| 365 | events::HistogramValue histogram_value; |
| 366 | |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 367 | // The event to dispatch. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 368 | std::string event_name; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 369 | |
| 370 | // Arguments to send to the event listener. |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 371 | scoped_ptr<base::ListValue> event_args; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 372 | |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 373 | // If non-NULL, then the event will not be sent to other BrowserContexts |
| 374 | // unless the extension has permission (e.g. incognito tab update -> normal |
| 375 | // tab only works if extension is allowed incognito access). |
| 376 | content::BrowserContext* restrict_to_browser_context; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 377 | |
| 378 | // If not empty, the event is only sent to extensions with host permissions |
| 379 | // for this url. |
| 380 | GURL event_url; |
| 381 | |
| 382 | // Whether a user gesture triggered the event. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 383 | EventRouter::UserGestureState user_gesture; |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 384 | |
| 385 | // Extra information used to filter which events are sent to the listener. |
| 386 | EventFilteringInfo filter_info; |
| 387 | |
| 388 | // If specified, this is called before dispatching an event to each |
| 389 | // extension. The third argument is a mutable reference to event_args, |
| 390 | // allowing the caller to provide different arguments depending on the |
| 391 | // extension and profile. This is guaranteed to be called synchronously with |
| 392 | // DispatchEvent, so callers don't need to worry about lifetime. |
[email protected] | 2c6e3b04c | 2014-07-24 12:48:09 | [diff] [blame] | 393 | // |
| 394 | // NOTE: the Extension argument to this may be NULL because it's possible for |
| 395 | // this event to be dispatched to non-extension processes, like WebUI. |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 396 | WillDispatchCallback will_dispatch_callback; |
| 397 | |
kalman | f1b4d78 | 2015-06-24 21:14:05 | [diff] [blame] | 398 | Event(events::HistogramValue histogram_value, |
| 399 | const std::string& event_name, |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 400 | scoped_ptr<base::ListValue> event_args); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 401 | |
kalman | f1b4d78 | 2015-06-24 21:14:05 | [diff] [blame] | 402 | Event(events::HistogramValue histogram_value, |
| 403 | const std::string& event_name, |
[email protected] | c9bd90f | 2012-08-07 23:58:15 | [diff] [blame] | 404 | scoped_ptr<base::ListValue> event_args, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 405 | content::BrowserContext* restrict_to_browser_context); |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 406 | |
kalman | f1b4d78 | 2015-06-24 21:14:05 | [diff] [blame] | 407 | Event(events::HistogramValue histogram_value, |
| 408 | const std::string& event_name, |
[email protected] | f0eb58a | 2012-12-17 22:10:49 | [diff] [blame] | 409 | scoped_ptr<base::ListValue> event_args, |
[email protected] | 45fd9417 | 2013-11-13 03:29:52 | [diff] [blame] | 410 | content::BrowserContext* restrict_to_browser_context, |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 411 | const GURL& event_url, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 412 | EventRouter::UserGestureState user_gesture, |
| 413 | const EventFilteringInfo& info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 414 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 415 | ~Event(); |
[email protected] | 6e85092 | 2012-12-05 03:22:48 | [diff] [blame] | 416 | |
| 417 | // Makes a deep copy of this instance. Ownership is transferred to the |
| 418 | // caller. |
| 419 | Event* DeepCopy(); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 420 | }; |
| 421 | |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 422 | struct EventListenerInfo { |
| 423 | EventListenerInfo(const std::string& event_name, |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 424 | const std::string& extension_id, |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 425 | const GURL& listener_url, |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 426 | content::BrowserContext* browser_context); |
| 427 | // The event name including any sub-event, e.g. "runtime.onStartup" or |
| 428 | // "webRequest.onCompleted/123". |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 429 | const std::string event_name; |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 430 | |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 431 | const std::string extension_id; |
[email protected] | c1abb323 | 2014-07-30 18:28:39 | [diff] [blame] | 432 | const GURL listener_url; |
[email protected] | c761a96 | 2013-11-20 04:19:41 | [diff] [blame] | 433 | content::BrowserContext* browser_context; |
[email protected] | 954e1349 | 2012-11-15 03:18:23 | [diff] [blame] | 434 | }; |
| 435 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame] | 436 | } // namespace extensions |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 437 | |
[email protected] | 3442353 | 2013-11-21 18:13:10 | [diff] [blame] | 438 | #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |