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