| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
| 6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <string> | 10 #include <string> |
| (...skipping 26 matching lines...) Expand all Loading... |
| 37 public EventListenerMap::Delegate { | 37 public EventListenerMap::Delegate { |
| 38 public: | 38 public: |
| 39 // These constants convey the state of our knowledge of whether we're in | 39 // These constants convey the state of our knowledge of whether we're in |
| 40 // a user-caused gesture as part of DispatchEvent. | 40 // a user-caused gesture as part of DispatchEvent. |
| 41 enum UserGestureState { | 41 enum UserGestureState { |
| 42 USER_GESTURE_UNKNOWN = 0, | 42 USER_GESTURE_UNKNOWN = 0, |
| 43 USER_GESTURE_ENABLED = 1, | 43 USER_GESTURE_ENABLED = 1, |
| 44 USER_GESTURE_NOT_ENABLED = 2, | 44 USER_GESTURE_NOT_ENABLED = 2, |
| 45 }; | 45 }; |
| 46 | 46 |
| 47 // Sends an event via ipc_sender to the given extension. Can be called on | 47 // Sends an event via ipc_sender to the given extension. Can be called on any |
| 48 // any thread. | 48 // thread. |
| 49 static void DispatchEvent(IPC::Sender* ipc_sender, | 49 static void DispatchEvent(IPC::Sender* ipc_sender, |
| 50 const std::string& extension_id, | 50 const std::string& extension_id, |
| 51 const std::string& event_name, | 51 const std::string& event_name, |
| 52 const base::Value& event_args, | 52 scoped_ptr<base::ListValue> event_args, |
| 53 const GURL& event_url, | 53 const GURL& event_url, |
| 54 UserGestureState user_gesture, | 54 UserGestureState user_gesture, |
| 55 const EventFilteringInfo& info); | 55 const EventFilteringInfo& info); |
| 56 | |
| 57 // This invocation is deprecated. All future consumers of this API should be | |
| 58 // sending Values as event arguments, using the above version. | |
| 59 static void DispatchEvent(IPC::Sender* ipc_sender, | |
| 60 const std::string& extension_id, | |
| 61 const std::string& event_name, | |
| 62 const std::string& event_args, | |
| 63 const GURL& event_url, | |
| 64 UserGestureState user_gesture, | |
| 65 const EventFilteringInfo& info); | |
| 66 | 56 |
| 67 explicit EventRouter(Profile* profile); | 57 explicit EventRouter(Profile* profile); |
| 68 virtual ~EventRouter(); | 58 virtual ~EventRouter(); |
| 69 | 59 |
| 70 // Add or remove the process/extension pair as a listener for |event_name|. | 60 // Add or remove the process/extension pair as a listener for |event_name|. |
| 71 // Note that multiple extensions can share a process due to process | 61 // Note that multiple extensions can share a process due to process |
| 72 // collapsing. Also, a single extension can have 2 processes if it is a split | 62 // collapsing. Also, a single extension can have 2 processes if it is a split |
| 73 // mode extension. | 63 // mode extension. |
| 74 void AddEventListener(const std::string& event_name, | 64 void AddEventListener(const std::string& event_name, |
| 75 content::RenderProcessHost* process, | 65 content::RenderProcessHost* process, |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 109 bool ExtensionHasEventListener(const std::string& extension_id, | 99 bool ExtensionHasEventListener(const std::string& extension_id, |
| 110 const std::string& event_name); | 100 const std::string& event_name); |
| 111 | 101 |
| 112 // Send an event to every registered extension renderer. If | 102 // Send an event to every registered extension renderer. If |
| 113 // |restrict_to_profile| is non-NULL, then the event will not be sent to other | 103 // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
| 114 // profiles unless the extension has permission (e.g. incognito tab update -> | 104 // profiles unless the extension has permission (e.g. incognito tab update -> |
| 115 // normal profile only works if extension is allowed incognito access). If | 105 // normal profile only works if extension is allowed incognito access). If |
| 116 // |event_url| is not empty, the event is only sent to extension with host | 106 // |event_url| is not empty, the event is only sent to extension with host |
| 117 // permissions for this url. | 107 // permissions for this url. |
| 118 void DispatchEventToRenderers(const std::string& event_name, | 108 void DispatchEventToRenderers(const std::string& event_name, |
| 119 const std::string& event_args, | 109 scoped_ptr<base::ListValue> event_args, |
| 120 Profile* restrict_to_profile, | 110 Profile* restrict_to_profile, |
| 121 const GURL& event_url, | 111 const GURL& event_url, |
| 122 EventFilteringInfo info); | 112 EventFilteringInfo info); |
| 123 | 113 |
| 124 // As above, but defaults |info| to EventFilteringInfo(). | 114 // As above, but defaults |info| to EventFilteringInfo(). |
| 125 void DispatchEventToRenderers(const std::string& event_name, | 115 void DispatchEventToRenderers(const std::string& event_name, |
| 126 const std::string& event_args, | 116 scoped_ptr<base::ListValue> event_args, |
| 127 Profile* restrict_to_profile, | 117 Profile* restrict_to_profile, |
| 128 const GURL& event_url); | 118 const GURL& event_url); |
| 129 | 119 |
| 130 // As above, but enables sending an explicit user gesture indicator. | 120 // As above, but enables sending an explicit user gesture indicator. |
| 131 void DispatchEventToRenderers(const std::string& event_name, | 121 void DispatchEventToRenderers(const std::string& event_name, |
| 132 const std::string& event_args, | 122 scoped_ptr<ListValue> event_args, |
| 133 Profile* restrict_to_profile, | 123 Profile* restrict_to_profile, |
| 134 const GURL& event_url, | 124 const GURL& event_url, |
| 135 UserGestureState user_gesture); | 125 UserGestureState user_gesture); |
| 136 | 126 |
| 137 // Same as above, except only send the event to the given extension. | 127 // Same as above, except only send the event to the given extension. |
| 138 virtual void DispatchEventToExtension(const std::string& extension_id, | 128 virtual void DispatchEventToExtension(const std::string& extension_id, |
| 139 const std::string& event_name, | 129 const std::string& event_name, |
| 140 const base::Value& event_args, | 130 scoped_ptr<base::ListValue> event_args, |
| 141 Profile* restrict_to_profile, | 131 Profile* restrict_to_profile, |
| 142 const GURL& event_url); | 132 const GURL& event_url); |
| 143 | 133 |
| 144 // This invocation is deprecated. The above variant which uses a Value for | |
| 145 // event_args is to be used instead. | |
| 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 | |
| 152 // Dispatch an event to particular extension. Also include an | 134 // Dispatch an event to particular extension. Also include an |
| 153 // explicit user gesture indicator. | 135 // explicit user gesture indicator. |
| 154 virtual void DispatchEventToExtension(const std::string& extension_id, | 136 virtual void DispatchEventToExtension(const std::string& extension_id, |
| 155 const std::string& event_name, | 137 const std::string& event_name, |
| 156 const std::string& event_args, | 138 scoped_ptr<base::ListValue> event_args, |
| 157 Profile* restrict_to_profile, | 139 Profile* restrict_to_profile, |
| 158 const GURL& event_url, | 140 const GURL& event_url, |
| 159 UserGestureState user_gesture); | 141 UserGestureState user_gesture); |
| 160 | 142 |
| 161 // Send different versions of an event to extensions in different profiles. | 143 // Send different versions of an event to extensions in different profiles. |
| 162 // This is used in the case of sending one event to extensions that have | 144 // This is used in the case of sending one event to extensions that have |
| 163 // incognito access, and another event to extensions that don't (here), | 145 // incognito access, and another event to extensions that don't (here), |
| 164 // in order to avoid sending 2 events to "spanning" extensions. | 146 // in order to avoid sending 2 events to "spanning" extensions. |
| 165 // If |cross_incognito_profile| is non-NULL and different from | 147 // If |cross_incognito_profile| is non-NULL and different from |
| 166 // restrict_to_profile, send the event with cross_incognito_args to the | 148 // restrict_to_profile, send the event with cross_incognito_args to the |
| 167 // extensions in that profile that can't cross incognito. | 149 // extensions in that profile that can't cross incognito. |
| 168 void DispatchEventsToRenderersAcrossIncognito( | 150 void DispatchEventsToRenderersAcrossIncognito( |
| 169 const std::string& event_name, | 151 const std::string& event_name, |
| 170 const std::string& event_args, | 152 scoped_ptr<base::ListValue> event_args, |
| 171 Profile* restrict_to_profile, | 153 Profile* restrict_to_profile, |
| 172 const std::string& cross_incognito_args, | 154 scoped_ptr<base::ListValue> cross_incognito_args, |
| 173 const GURL& event_url); | 155 const GURL& event_url); |
| 174 | 156 |
| 175 // Record the Event Ack from the renderer. (One less event in-flight.) | 157 // Record the Event Ack from the renderer. (One less event in-flight.) |
| 176 void OnEventAck(Profile* profile, const std::string& extension_id); | 158 void OnEventAck(Profile* profile, const std::string& extension_id); |
| 177 | 159 |
| 178 private: | 160 private: |
| 179 // The extension and process that contains the event listener for a given | 161 // The extension and process that contains the event listener for a given |
| 180 // event. | 162 // event. |
| 181 struct ListenerProcess; | 163 struct ListenerProcess; |
| 182 | 164 |
| 183 // A map between an event name and a set of extensions that are listening | 165 // A map between an event name and a set of extensions that are listening |
| 184 // to that event. | 166 // to that event. |
| 185 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; | 167 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
| 186 | 168 |
| 169 // TODO(gdk): Document this. |
| 170 static void DispatchExtensionMessage( |
| 171 IPC::Sender* ipc_sender, |
| 172 const std::string& extension_id, |
| 173 const std::string& event_name, |
| 174 base::ListValue* event_args, |
| 175 const GURL& event_url, |
| 176 UserGestureState user_gesture, |
| 177 const extensions::EventFilteringInfo& info); |
| 178 |
| 187 virtual void Observe(int type, | 179 virtual void Observe(int type, |
| 188 const content::NotificationSource& source, | 180 const content::NotificationSource& source, |
| 189 const content::NotificationDetails& details) OVERRIDE; | 181 const content::NotificationDetails& details) OVERRIDE; |
| 190 | 182 |
| 191 // Returns true if the given listener map contains a event listeners for | 183 // Returns true if the given listener map contains a event listeners for |
| 192 // the given event. If |extension_id| is non-empty, we also check that that | 184 // the given event. If |extension_id| is non-empty, we also check that that |
| 193 // extension is one of the listeners. | 185 // extension is one of the listeners. |
| 194 bool HasEventListenerImpl(const ListenerMap& listeners, | 186 bool HasEventListenerImpl(const ListenerMap& listeners, |
| 195 const std::string& extension_id, | 187 const std::string& extension_id, |
| 196 const std::string& event_name); | 188 const std::string& event_name); |
| (...skipping 14 matching lines...) Expand all Loading... |
| 211 content::RenderProcessHost* process, | 203 content::RenderProcessHost* process, |
| 212 const linked_ptr<Event>& event); | 204 const linked_ptr<Event>& event); |
| 213 | 205 |
| 214 // Returns false when the event is scoped to a profile and the listening | 206 // Returns false when the event is scoped to a profile and the listening |
| 215 // extension does not have access to events from that profile. Also fills | 207 // extension does not have access to events from that profile. Also fills |
| 216 // |event_args| with the proper arguments to send, which may differ if | 208 // |event_args| with the proper arguments to send, which may differ if |
| 217 // the event crosses the incognito boundary. | 209 // the event crosses the incognito boundary. |
| 218 bool CanDispatchEventToProfile(Profile* profile, | 210 bool CanDispatchEventToProfile(Profile* profile, |
| 219 const Extension* extension, | 211 const Extension* extension, |
| 220 const linked_ptr<Event>& event, | 212 const linked_ptr<Event>& event, |
| 221 const base::Value** event_args); | 213 base::ListValue** event_args); |
| 222 | 214 |
| 223 // Possibly loads given extension's background page in preparation to | 215 // Possibly loads given extension's background page in preparation to |
| 224 // dispatch an event. | 216 // dispatch an event. |
| 225 void MaybeLoadLazyBackgroundPageToDispatchEvent( | 217 void MaybeLoadLazyBackgroundPageToDispatchEvent( |
| 226 Profile* profile, | 218 Profile* profile, |
| 227 const Extension* extension, | 219 const Extension* extension, |
| 228 const linked_ptr<Event>& event); | 220 const linked_ptr<Event>& event); |
| 229 | 221 |
| 230 // Track of the number of dispatched events that have not yet sent an | 222 // Track of the number of dispatched events that have not yet sent an |
| 231 // ACK from the renderer. | 223 // ACK from the renderer. |
| (...skipping 13 matching lines...) Expand all Loading... |
| 245 | 237 |
| 246 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 238 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
| 247 | 239 |
| 248 EventListenerMap listeners_; | 240 EventListenerMap listeners_; |
| 249 | 241 |
| 250 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 242 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
| 251 }; | 243 }; |
| 252 | 244 |
| 253 struct Event { | 245 struct Event { |
| 254 std::string event_name; | 246 std::string event_name; |
| 255 scoped_ptr<Value> event_args; | 247 scoped_ptr<base::ListValue> event_args; |
| 256 GURL event_url; | 248 GURL event_url; |
| 257 Profile* restrict_to_profile; | 249 Profile* restrict_to_profile; |
| 258 scoped_ptr<Value> cross_incognito_args; | 250 scoped_ptr<base::ListValue> cross_incognito_args; |
| 259 EventRouter::UserGestureState user_gesture; | 251 EventRouter::UserGestureState user_gesture; |
| 260 EventFilteringInfo info; | 252 EventFilteringInfo info; |
| 261 | 253 |
| 262 Event(const std::string& event_name, | 254 Event(const std::string& event_name, |
| 263 const Value& event_args, | 255 scoped_ptr<base::ListValue> event_args, |
| 264 const GURL& event_url, | 256 const GURL& event_url, |
| 265 Profile* restrict_to_profile, | 257 Profile* restrict_to_profile, |
| 266 const Value& cross_incognito_args, | 258 scoped_ptr<base::ListValue> cross_incognito_args, |
| 267 EventRouter::UserGestureState user_gesture, | |
| 268 const EventFilteringInfo& info); | |
| 269 | |
| 270 // TODO(gdk): This variant should be retired once the callers are switched to | |
| 271 // providing Values instead of just strings. | |
| 272 Event(const std::string& event_name, | |
| 273 const std::string& event_args, | |
| 274 const GURL& event_url, | |
| 275 Profile* restrict_to_profile, | |
| 276 const std::string& cross_incognito_args, | |
| 277 EventRouter::UserGestureState user_gesture, | 259 EventRouter::UserGestureState user_gesture, |
| 278 const EventFilteringInfo& info); | 260 const EventFilteringInfo& info); |
| 279 | 261 |
| 280 Event(const std::string& event_name, | 262 Event(const std::string& event_name, |
| 281 const Value& event_args, | 263 scoped_ptr<base::ListValue> event_args, |
| 282 const GURL& event_url, | 264 const GURL& event_url, |
| 283 Profile* restrict_to_profile, | 265 Profile* restrict_to_profile, |
| 284 EventRouter::UserGestureState user_gesture, | 266 EventRouter::UserGestureState user_gesture, |
| 285 const EventFilteringInfo& info); | 267 const EventFilteringInfo& info); |
| 286 | 268 |
| 287 ~Event(); | 269 ~Event(); |
| 288 }; | 270 }; |
| 289 | 271 |
| 290 } // namespace extensions | 272 } // namespace extensions |
| 291 | 273 |
| 292 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ | 274 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
| OLD | NEW |