[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> |
| 11 | |
[email protected] | 1790275 | 2011-08-31 22:52:54 | [diff] [blame] | 12 | #include "base/compiler_specific.h" |
[email protected] | f72d0c6 | 2011-08-31 16:27:44 | [diff] [blame] | 13 | #include "base/memory/linked_ptr.h" |
[email protected] | 3b63f8f4 | 2011-03-28 01:54:15 | [diff] [blame] | 14 | #include "base/memory/ref_counted.h" |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 15 | #include "base/values.h" |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 16 | #include "chrome/browser/extensions/event_listener_map.h" |
| 17 | #include "chrome/common/extensions/event_filtering_info.h" |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 18 | #include "content/public/browser/notification_observer.h" |
| 19 | #include "content/public/browser/notification_registrar.h" |
[email protected] | b44f8ad | 2012-06-15 20:52:58 | [diff] [blame] | 20 | #include "ipc/ipc_sender.h" |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 21 | |
| 22 | class GURL; |
[email protected] | b6536df | 2012-03-16 18:55:23 | [diff] [blame] | 23 | class ExtensionHost; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 24 | class ExtensionDevToolsManager; |
| 25 | class Profile; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 26 | |
| 27 | namespace content { |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 28 | class RenderProcessHost; |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 29 | } |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 30 | |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 31 | namespace extensions { |
| 32 | class Extension; |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 33 | struct Event; |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 34 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 35 | class EventRouter : public content::NotificationObserver, |
| 36 | public EventListenerMap::Delegate { |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 37 | public: |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 38 | // These constants convey the state of our knowledge of whether we're in |
| 39 | // a user-caused gesture as part of DispatchEvent. |
| 40 | enum UserGestureState { |
| 41 | USER_GESTURE_UNKNOWN = 0, |
| 42 | USER_GESTURE_ENABLED = 1, |
| 43 | USER_GESTURE_NOT_ENABLED = 2, |
| 44 | }; |
| 45 | |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 46 | // Sends an event via ipc_sender to the given extension. Can be called on |
| 47 | // any thread. |
[email protected] | b44f8ad | 2012-06-15 20:52:58 | [diff] [blame] | 48 | static void DispatchEvent(IPC::Sender* ipc_sender, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 49 | const std::string& extension_id, |
| 50 | const std::string& event_name, |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 51 | const base::Value& event_args, |
| 52 | const GURL& event_url, |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 53 | UserGestureState user_gesture, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 54 | const EventFilteringInfo& info); |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 55 | |
| 56 | // This invocation is deprecated. All future consumers of this API should be |
| 57 | // sending Values as event arguments, using the above version. |
| 58 | static void DispatchEvent(IPC::Sender* ipc_sender, |
| 59 | const std::string& extension_id, |
| 60 | const std::string& event_name, |
[email protected] | c357acb4 | 2011-06-09 20:52:42 | [diff] [blame] | 61 | const std::string& event_args, |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 62 | const GURL& event_url, |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 63 | UserGestureState user_gesture, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 64 | const EventFilteringInfo& info); |
[email protected] | 5a7b5eaf | 2010-11-02 20:52:19 | [diff] [blame] | 65 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 66 | explicit EventRouter(Profile* profile); |
| 67 | virtual ~EventRouter(); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 68 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 69 | // Add or remove the process/extension pair as a listener for |event_name|. |
| 70 | // Note that multiple extensions can share a process due to process |
| 71 | // collapsing. Also, a single extension can have 2 processes if it is a split |
| 72 | // mode extension. |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 73 | void AddEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 74 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 75 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 76 | void RemoveEventListener(const std::string& event_name, |
[email protected] | f3b1a08 | 2011-11-18 00:34:30 | [diff] [blame] | 77 | content::RenderProcessHost* process, |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 78 | const std::string& extension_id); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 79 | |
[email protected] | 3653122 | 2012-02-07 19:41:27 | [diff] [blame] | 80 | // Add or remove the extension as having a lazy background page that listens |
| 81 | // to the event. The difference from the above methods is that these will be |
| 82 | // remembered even after the process goes away. We use this list to decide |
| 83 | // which extension pages to load when dispatching an event. |
| 84 | void AddLazyEventListener(const std::string& event_name, |
| 85 | const std::string& extension_id); |
| 86 | void RemoveLazyEventListener(const std::string& event_name, |
| 87 | const std::string& extension_id); |
| 88 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 89 | // If |add_lazy_listener| is true also add the lazy version of this listener. |
| 90 | void AddFilteredEventListener(const std::string& event_name, |
| 91 | content::RenderProcessHost* process, |
| 92 | const std::string& extension_id, |
| 93 | const base::DictionaryValue& filter, |
| 94 | bool add_lazy_listener); |
| 95 | |
| 96 | // If |remove_lazy_listener| is true also remove the lazy version of this |
| 97 | // listener. |
| 98 | void RemoveFilteredEventListener(const std::string& event_name, |
| 99 | content::RenderProcessHost* process, |
| 100 | const std::string& extension_id, |
| 101 | const base::DictionaryValue& filter, |
| 102 | bool remove_lazy_listener); |
| 103 | |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 104 | // Returns true if there is at least one listener for the given event. |
| 105 | bool HasEventListener(const std::string& event_name); |
| 106 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 107 | // Returns true if the extension is listening to the given event. |
| 108 | bool ExtensionHasEventListener(const std::string& extension_id, |
| 109 | const std::string& event_name); |
| 110 | |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 111 | // Send an event to every registered extension renderer. If |
| 112 | // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
| 113 | // profiles unless the extension has permission (e.g. incognito tab update -> |
| 114 | // normal profile only works if extension is allowed incognito access). If |
| 115 | // |event_url| is not empty, the event is only sent to extension with host |
| 116 | // permissions for this url. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 117 | void DispatchEventToRenderers(const std::string& event_name, |
| 118 | const std::string& event_args, |
| 119 | Profile* restrict_to_profile, |
| 120 | const GURL& event_url, |
| 121 | EventFilteringInfo info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 122 | |
| 123 | // As above, but defaults |info| to EventFilteringInfo(). |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 124 | void DispatchEventToRenderers(const std::string& event_name, |
| 125 | const std::string& event_args, |
| 126 | Profile* restrict_to_profile, |
| 127 | const GURL& event_url); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 128 | |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 129 | // Same as above, except only send the event to the given extension. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 130 | virtual void DispatchEventToExtension(const std::string& extension_id, |
| 131 | const std::string& event_name, |
| 132 | const base::Value& event_args, |
| 133 | Profile* restrict_to_profile, |
| 134 | const GURL& event_url); |
[email protected] | 8a16a03 | 2012-06-18 19:37:31 | [diff] [blame] | 135 | |
| 136 | // This invocation is deprecated. The above variant which uses a Value for |
| 137 | // event_args is to be used instead. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 138 | virtual void DispatchEventToExtension(const std::string& extension_id, |
| 139 | const std::string& event_name, |
| 140 | const std::string& event_args, |
| 141 | Profile* restrict_to_profile, |
| 142 | const GURL& event_url); |
[email protected] | 0a184b5 | 2011-06-23 00:41:13 | [diff] [blame] | 143 | |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 144 | // Dispatch an event to particular extension. Also include an |
| 145 | // explicit user gesture indicator. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 146 | virtual void DispatchEventToExtension(const std::string& extension_id, |
| 147 | const std::string& event_name, |
| 148 | const std::string& event_args, |
| 149 | Profile* restrict_to_profile, |
| 150 | const GURL& event_url, |
| 151 | UserGestureState user_gesture); |
[email protected] | b085856f | 2012-03-02 04:37:25 | [diff] [blame] | 152 | |
[email protected] | 0a184b5 | 2011-06-23 00:41:13 | [diff] [blame] | 153 | // Send different versions of an event to extensions in different profiles. |
| 154 | // This is used in the case of sending one event to extensions that have |
| 155 | // incognito access, and another event to extensions that don't (here), |
| 156 | // in order to avoid sending 2 events to "spanning" extensions. |
| 157 | // If |cross_incognito_profile| is non-NULL and different from |
| 158 | // restrict_to_profile, send the event with cross_incognito_args to the |
| 159 | // extensions in that profile that can't cross incognito. |
| 160 | void DispatchEventsToRenderersAcrossIncognito( |
| 161 | const std::string& event_name, |
| 162 | const std::string& event_args, |
| 163 | Profile* restrict_to_profile, |
| 164 | const std::string& cross_incognito_args, |
| 165 | const GURL& event_url); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 166 | |
[email protected] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 167 | // Record the Event Ack from the renderer. (One less event in-flight.) |
[email protected] | 7042b68 | 2012-04-19 22:57:51 | [diff] [blame] | 168 | void OnEventAck(Profile* profile, const std::string& extension_id); |
[email protected] | 8910201 | 2011-11-01 21:23:56 | [diff] [blame] | 169 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 170 | private: |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 171 | // The extension and process that contains the event listener for a given |
| 172 | // event. |
| 173 | struct ListenerProcess; |
| 174 | |
| 175 | // A map between an event name and a set of extensions that are listening |
| 176 | // to that event. |
| 177 | typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
[email protected] | a7ab1b78 | 2010-10-21 23:24:16 | [diff] [blame] | 178 | |
[email protected] | 43211582 | 2011-07-10 15:52:27 | [diff] [blame] | 179 | virtual void Observe(int type, |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 180 | const content::NotificationSource& source, |
| 181 | const content::NotificationDetails& details) OVERRIDE; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 182 | |
[email protected] | 61f5fc8 | 2012-02-15 20:10:45 | [diff] [blame] | 183 | // Returns true if the given listener map contains a event listeners for |
| 184 | // the given event. If |extension_id| is non-empty, we also check that that |
| 185 | // extension is one of the listeners. |
| 186 | bool HasEventListenerImpl(const ListenerMap& listeners, |
| 187 | const std::string& extension_id, |
| 188 | const std::string& event_name); |
| 189 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 190 | // Shared by DispatchEvent*. If |restrict_to_extension_id| is empty, the |
| 191 | // event is broadcast. |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 192 | // 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] | 193 | void DispatchEventImpl(const std::string& restrict_to_extension_id, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 194 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 195 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 196 | // Ensures that all lazy background pages that are interested in the given |
| 197 | // event are loaded, and queues the event if the page is not ready yet. |
| 198 | void DispatchLazyEvent(const std::string& extension_id, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 199 | const linked_ptr<Event>& event); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 200 | |
| 201 | // Dispatches the event to the specified extension running in |process|. |
| 202 | void DispatchEventToProcess(const std::string& extension_id, |
| 203 | content::RenderProcessHost* process, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 204 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 205 | |
| 206 | // Returns false when the event is scoped to a profile and the listening |
| 207 | // extension does not have access to events from that profile. Also fills |
| 208 | // |event_args| with the proper arguments to send, which may differ if |
| 209 | // the event crosses the incognito boundary. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 210 | bool CanDispatchEventToProfile(Profile* profile, |
| 211 | const Extension* extension, |
| 212 | const linked_ptr<Event>& event, |
| 213 | const base::Value** event_args); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 214 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 215 | // Possibly loads given extension's background page in preparation to |
| 216 | // dispatch an event. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 217 | void MaybeLoadLazyBackgroundPageToDispatchEvent( |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 218 | Profile* profile, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 219 | const Extension* extension, |
| 220 | const linked_ptr<Event>& event); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 221 | |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 222 | // Track of the number of dispatched events that have not yet sent an |
| 223 | // ACK from the renderer. |
[email protected] | 1c321ee | 2012-05-21 03:02:34 | [diff] [blame] | 224 | void IncrementInFlightEvents(Profile* profile, |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 225 | const Extension* extension); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 226 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 227 | void DispatchPendingEvent(const linked_ptr<Event>& event, |
[email protected] | b6536df | 2012-03-16 18:55:23 | [diff] [blame] | 228 | ExtensionHost* host); |
[email protected] | fb6ff23b | 2012-03-13 23:13:42 | [diff] [blame] | 229 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 230 | // Implementation of EventListenerMap::Delegate. |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 231 | virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; |
| 232 | virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
| 233 | |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 234 | Profile* profile_; |
| 235 | |
[email protected] | 6c2381d | 2011-10-19 02:52:53 | [diff] [blame] | 236 | content::NotificationRegistrar registrar_; |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 237 | |
| 238 | scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
| 239 | |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 240 | EventListenerMap listeners_; |
[email protected] | 3653122 | 2012-02-07 19:41:27 | [diff] [blame] | 241 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 242 | DISALLOW_COPY_AND_ASSIGN(EventRouter); |
[email protected] | 2c69965 | 2010-10-15 18:22:41 | [diff] [blame] | 243 | }; |
| 244 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 245 | struct Event { |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 246 | std::string event_name; |
| 247 | scoped_ptr<Value> event_args; |
| 248 | GURL event_url; |
| 249 | Profile* restrict_to_profile; |
| 250 | scoped_ptr<Value> cross_incognito_args; |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 251 | EventRouter::UserGestureState user_gesture; |
| 252 | EventFilteringInfo info; |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 253 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 254 | Event(const std::string& event_name, |
| 255 | const Value& event_args, |
| 256 | const GURL& event_url, |
| 257 | Profile* restrict_to_profile, |
| 258 | const Value& cross_incognito_args, |
| 259 | EventRouter::UserGestureState user_gesture, |
| 260 | const EventFilteringInfo& info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 261 | |
| 262 | // TODO(gdk): This variant should be retired once the callers are switched to |
| 263 | // providing Values instead of just strings. |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 264 | Event(const std::string& event_name, |
| 265 | const std::string& event_args, |
| 266 | const GURL& event_url, |
| 267 | Profile* restrict_to_profile, |
| 268 | const std::string& cross_incognito_args, |
| 269 | EventRouter::UserGestureState user_gesture, |
| 270 | const EventFilteringInfo& info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 271 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 272 | Event(const std::string& event_name, |
| 273 | const Value& event_args, |
| 274 | const GURL& event_url, |
| 275 | Profile* restrict_to_profile, |
| 276 | EventRouter::UserGestureState user_gesture, |
| 277 | const EventFilteringInfo& info); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 278 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 279 | ~Event(); |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 280 | }; |
| 281 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 282 | } // namespace extensions |
[email protected] | d9e559d | 2012-07-05 01:04:57 | [diff] [blame] | 283 | |
[email protected] | 5a38dfd | 2012-07-23 23:22:10 | [diff] [blame^] | 284 | #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |