| 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 EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 5 #ifndef EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
| 6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
| 7 | 7 |
| 8 #include <set> | 8 #include <set> |
| 9 #include <string> | 9 #include <string> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/macros.h" | 15 #include "base/macros.h" |
| 16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
| 19 #include "base/values.h" | 19 #include "base/values.h" |
| 20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
| 21 #include "content/public/browser/render_process_host_observer.h" | 21 #include "content/public/browser/render_process_host_observer.h" |
| 22 #include "extensions/browser/event_listener_map.h" | 22 #include "extensions/browser/event_listener_map.h" |
| 23 #include "extensions/browser/events/lazy_event_dispatch_util.h" | 23 #include "extensions/browser/events/lazy_event_dispatch_util.h" |
| 24 #include "extensions/browser/extension_event_histogram_value.h" | 24 #include "extensions/browser/extension_event_histogram_value.h" |
| 25 #include "extensions/browser/extension_registry_observer.h" | 25 #include "extensions/browser/extension_registry_observer.h" |
| 26 #include "extensions/common/constants.h" |
| 26 #include "extensions/common/event_filtering_info.h" | 27 #include "extensions/common/event_filtering_info.h" |
| 27 #include "ipc/ipc_sender.h" | 28 #include "ipc/ipc_sender.h" |
| 28 #include "url/gurl.h" | 29 #include "url/gurl.h" |
| 29 | 30 |
| 30 class GURL; | 31 class GURL; |
| 31 | 32 |
| 32 namespace content { | 33 namespace content { |
| 33 class BrowserContext; | 34 class BrowserContext; |
| 34 class RenderProcessHost; | 35 class RenderProcessHost; |
| 35 } | 36 } |
| 36 | 37 |
| 37 namespace extensions { | 38 namespace extensions { |
| 38 class Extension; | 39 class Extension; |
| 39 class ExtensionHost; | 40 class ExtensionHost; |
| 40 class ExtensionPrefs; | 41 class ExtensionPrefs; |
| 41 class ExtensionRegistry; | 42 class ExtensionRegistry; |
| 42 | 43 |
| 43 struct Event; | 44 struct Event; |
| 44 struct EventListenerInfo; | 45 struct EventListenerInfo; |
| 45 | 46 |
| 47 // TODO(lazyboy): Document how extension events work, including how listeners |
| 48 // are registered and how listeners are tracked in renderer and browser process. |
| 46 class EventRouter : public KeyedService, | 49 class EventRouter : public KeyedService, |
| 47 public ExtensionRegistryObserver, | 50 public ExtensionRegistryObserver, |
| 48 public EventListenerMap::Delegate, | 51 public EventListenerMap::Delegate, |
| 49 public content::RenderProcessHostObserver { | 52 public content::RenderProcessHostObserver { |
| 50 public: | 53 public: |
| 51 // These constants convey the state of our knowledge of whether we're in | 54 // These constants convey the state of our knowledge of whether we're in |
| 52 // a user-caused gesture as part of DispatchEvent. | 55 // a user-caused gesture as part of DispatchEvent. |
| 53 enum UserGestureState { | 56 enum UserGestureState { |
| 54 USER_GESTURE_UNKNOWN = 0, | 57 USER_GESTURE_UNKNOWN = 0, |
| 55 USER_GESTURE_ENABLED = 1, | 58 USER_GESTURE_ENABLED = 1, |
| 56 USER_GESTURE_NOT_ENABLED = 2, | 59 USER_GESTURE_NOT_ENABLED = 2, |
| 57 }; | 60 }; |
| 58 | 61 |
| 59 // The pref key for the list of event names for which an extension has | 62 // The pref key for the list of event names for which an extension has |
| 60 // registered from its lazy background page. | 63 // registered from its lazy background page. |
| 61 static const char kRegisteredEvents[]; | 64 static const char kRegisteredLazyEvents[]; |
| 65 // The pref key for the list of event names for which an extension has |
| 66 // registered from its service worker. |
| 67 static const char kRegisteredServiceWorkerEvents[]; |
| 62 | 68 |
| 63 // Observers register interest in events with a particular name and are | 69 // Observers register interest in events with a particular name and are |
| 64 // notified when a listener is added or removed. Observers are matched by | 70 // notified when a listener is added or removed. Observers are matched by |
| 65 // the base name of the event (e.g. adding an event listener for event name | 71 // the base name of the event (e.g. adding an event listener for event name |
| 66 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). | 72 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). |
| 67 class Observer { | 73 class Observer { |
| 68 public: | 74 public: |
| 69 // Called when a listener is added. | 75 // Called when a listener is added. |
| 70 virtual void OnListenerAdded(const EventListenerInfo& details) {} | 76 virtual void OnListenerAdded(const EventListenerInfo& details) {} |
| 71 // Called when a listener is removed. | 77 // Called when a listener is removed. |
| (...skipping 30 matching lines...) Expand all Loading... |
| 102 ExtensionPrefs* extension_prefs); | 108 ExtensionPrefs* extension_prefs); |
| 103 ~EventRouter() override; | 109 ~EventRouter() override; |
| 104 | 110 |
| 105 // Add or remove an extension as an event listener for |event_name|. | 111 // Add or remove an extension as an event listener for |event_name|. |
| 106 // | 112 // |
| 107 // Note that multiple extensions can share a process due to process | 113 // Note that multiple extensions can share a process due to process |
| 108 // collapsing. Also, a single extension can have 2 processes if it is a split | 114 // collapsing. Also, a single extension can have 2 processes if it is a split |
| 109 // mode extension. | 115 // mode extension. |
| 110 void AddEventListener(const std::string& event_name, | 116 void AddEventListener(const std::string& event_name, |
| 111 content::RenderProcessHost* process, | 117 content::RenderProcessHost* process, |
| 112 const std::string& extension_id); | 118 const ExtensionId& extension_id); |
| 119 void AddServiceWorkerEventListener(const std::string& event_name, |
| 120 content::RenderProcessHost* process, |
| 121 const ExtensionId& extension_id, |
| 122 int worker_thread_id); |
| 113 void RemoveEventListener(const std::string& event_name, | 123 void RemoveEventListener(const std::string& event_name, |
| 114 content::RenderProcessHost* process, | 124 content::RenderProcessHost* process, |
| 115 const std::string& extension_id); | 125 const ExtensionId& extension_id); |
| 126 void RemoveServiceWorkerEventListener(const std::string& event_name, |
| 127 content::RenderProcessHost* process, |
| 128 const ExtensionId& extension_id, |
| 129 int worker_thread_id); |
| 116 | 130 |
| 117 // Add or remove a URL as an event listener for |event_name|. | 131 // Add or remove a URL as an event listener for |event_name|. |
| 118 void AddEventListenerForURL(const std::string& event_name, | 132 void AddEventListenerForURL(const std::string& event_name, |
| 119 content::RenderProcessHost* process, | 133 content::RenderProcessHost* process, |
| 120 const GURL& listener_url); | 134 const GURL& listener_url); |
| 121 void RemoveEventListenerForURL(const std::string& event_name, | 135 void RemoveEventListenerForURL(const std::string& event_name, |
| 122 content::RenderProcessHost* process, | 136 content::RenderProcessHost* process, |
| 123 const GURL& listener_url); | 137 const GURL& listener_url); |
| 124 | 138 |
| 125 EventListenerMap& listeners() { return listeners_; } | 139 EventListenerMap& listeners() { return listeners_; } |
| 126 | 140 |
| 127 // Registers an observer to be notified when an event listener for | 141 // Registers an observer to be notified when an event listener for |
| 128 // |event_name| is added or removed. There can currently be only one observer | 142 // |event_name| is added or removed. There can currently be only one observer |
| 129 // for each distinct |event_name|. | 143 // for each distinct |event_name|. |
| 130 void RegisterObserver(Observer* observer, const std::string& event_name); | 144 void RegisterObserver(Observer* observer, const std::string& event_name); |
| 131 | 145 |
| 132 // Unregisters an observer from all events. | 146 // Unregisters an observer from all events. |
| 133 void UnregisterObserver(Observer* observer); | 147 void UnregisterObserver(Observer* observer); |
| 134 | 148 |
| 135 // Add or remove the extension as having a lazy background page that listens | 149 // Add or remove the extension as having a lazy background page that listens |
| 136 // to the event. The difference from the above methods is that these will be | 150 // to the event. The difference from the above methods is that these will be |
| 137 // remembered even after the process goes away. We use this list to decide | 151 // remembered even after the process goes away. We use this list to decide |
| 138 // which extension pages to load when dispatching an event. | 152 // which extension pages to load when dispatching an event. |
| 139 void AddLazyEventListener(const std::string& event_name, | 153 void AddLazyEventListener(const std::string& event_name, |
| 140 const std::string& extension_id); | 154 const ExtensionId& extension_id); |
| 141 void RemoveLazyEventListener(const std::string& event_name, | 155 void RemoveLazyEventListener(const std::string& event_name, |
| 142 const std::string& extension_id); | 156 const ExtensionId& extension_id); |
| 157 // Similar to Add/RemoveLazyEventListener, but applies to extension service |
| 158 // workers. |
| 159 void AddLazyServiceWorkerEventListener(const std::string& event_name, |
| 160 const ExtensionId& extension_id, |
| 161 int worker_thread_id); |
| 162 void RemoveLazyServiceWorkerEventListener(const std::string& event_name, |
| 163 const ExtensionId& extension_id, |
| 164 int worker_thread_id); |
| 143 | 165 |
| 144 // If |add_lazy_listener| is true also add the lazy version of this listener. | 166 // If |add_lazy_listener| is true also add the lazy version of this listener. |
| 145 void AddFilteredEventListener(const std::string& event_name, | 167 void AddFilteredEventListener(const std::string& event_name, |
| 146 content::RenderProcessHost* process, | 168 content::RenderProcessHost* process, |
| 147 const std::string& extension_id, | 169 const std::string& extension_id, |
| 148 const base::DictionaryValue& filter, | 170 const base::DictionaryValue& filter, |
| 149 bool add_lazy_listener); | 171 bool add_lazy_listener); |
| 150 | 172 |
| 151 // If |remove_lazy_listener| is true also remove the lazy version of this | 173 // If |remove_lazy_listener| is true also remove the lazy version of this |
| 152 // listener. | 174 // listener. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 177 // newly installed extensions. | 199 // newly installed extensions. |
| 178 void DispatchEventWithLazyListener(const std::string& extension_id, | 200 void DispatchEventWithLazyListener(const std::string& extension_id, |
| 179 std::unique_ptr<Event> event); | 201 std::unique_ptr<Event> event); |
| 180 | 202 |
| 181 // Record the Event Ack from the renderer. (One less event in-flight.) | 203 // Record the Event Ack from the renderer. (One less event in-flight.) |
| 182 void OnEventAck(content::BrowserContext* context, | 204 void OnEventAck(content::BrowserContext* context, |
| 183 const std::string& extension_id); | 205 const std::string& extension_id); |
| 184 | 206 |
| 185 // Returns whether or not the given extension has any registered events. | 207 // Returns whether or not the given extension has any registered events. |
| 186 bool HasRegisteredEvents(const ExtensionId& extension_id) const { | 208 bool HasRegisteredEvents(const ExtensionId& extension_id) const { |
| 187 return !GetRegisteredEvents(extension_id).empty(); | 209 return !GetRegisteredEvents(extension_id, RegisteredEventType::kLazy) |
| 210 .empty(); |
| 188 } | 211 } |
| 189 | 212 |
| 190 // Clears registered events for testing purposes. | 213 // Clears registered events for testing purposes. |
| 191 void ClearRegisteredEventsForTest(const ExtensionId& extension_id) { | 214 void ClearRegisteredEventsForTest(const ExtensionId& extension_id); |
| 192 SetRegisteredEvents(extension_id, std::set<std::string>()); | |
| 193 } | |
| 194 | 215 |
| 195 // Reports UMA for an event dispatched to |extension| with histogram value | 216 // Reports UMA for an event dispatched to |extension| with histogram value |
| 196 // |histogram_value|. Must be called on the UI thread. | 217 // |histogram_value|. Must be called on the UI thread. |
| 197 // | 218 // |
| 198 // |did_enqueue| should be true if the event was queued waiting for a process | 219 // |did_enqueue| should be true if the event was queued waiting for a process |
| 199 // to start, like an event page. | 220 // to start, like an event page. |
| 200 void ReportEvent(events::HistogramValue histogram_value, | 221 void ReportEvent(events::HistogramValue histogram_value, |
| 201 const Extension* extension, | 222 const Extension* extension, |
| 202 bool did_enqueue); | 223 bool did_enqueue); |
| 203 | 224 |
| 204 LazyEventDispatchUtil* lazy_event_dispatch_util() { | 225 LazyEventDispatchUtil* lazy_event_dispatch_util() { |
| 205 return &lazy_event_dispatch_util_; | 226 return &lazy_event_dispatch_util_; |
| 206 } | 227 } |
| 207 | 228 |
| 208 private: | 229 private: |
| 209 friend class EventRouterFilterTest; | 230 friend class EventRouterFilterTest; |
| 210 friend class EventRouterTest; | 231 friend class EventRouterTest; |
| 211 | 232 |
| 233 enum class RegisteredEventType { |
| 234 kLazy, |
| 235 kServiceWorker, |
| 236 }; |
| 237 |
| 212 // An identifier for an event dispatch that is used to prevent double dispatch | 238 // An identifier for an event dispatch that is used to prevent double dispatch |
| 213 // due to race conditions between the direct and lazy dispatch paths. | 239 // due to race conditions between the direct and lazy dispatch paths. |
| 214 typedef std::pair<const content::BrowserContext*, std::string> | 240 typedef std::tuple<const content::BrowserContext*, std::string, int> |
| 215 EventDispatchIdentifier; | 241 EventDispatchIdentifier; |
| 216 | 242 |
| 217 // TODO(gdk): Document this. | 243 // TODO(gdk): Document this. |
| 218 static void DispatchExtensionMessage( | 244 static void DispatchExtensionMessage( |
| 219 IPC::Sender* ipc_sender, | 245 IPC::Sender* ipc_sender, |
| 246 int worker_thread_id, |
| 220 void* browser_context_id, | 247 void* browser_context_id, |
| 221 const std::string& extension_id, | 248 const std::string& extension_id, |
| 222 int event_id, | 249 int event_id, |
| 223 const std::string& event_name, | 250 const std::string& event_name, |
| 224 base::ListValue* event_args, | 251 base::ListValue* event_args, |
| 225 UserGestureState user_gesture, | 252 UserGestureState user_gesture, |
| 226 const extensions::EventFilteringInfo& info); | 253 const extensions::EventFilteringInfo& info); |
| 227 | 254 |
| 228 // Returns or sets the list of events for which the given extension has | 255 // Returns or sets the list of events for which the given extension has |
| 229 // registered. | 256 // registered. |
| 230 std::set<std::string> GetRegisteredEvents( | 257 std::set<std::string> GetRegisteredEvents(const std::string& extension_id, |
| 231 const std::string& extension_id) const; | 258 RegisteredEventType type) const; |
| 232 void SetRegisteredEvents(const std::string& extension_id, | 259 void SetRegisteredEvents(const std::string& extension_id, |
| 233 const std::set<std::string>& events); | 260 const std::set<std::string>& events, |
| 261 RegisteredEventType type); |
| 234 | 262 |
| 235 // ExtensionRegistryObserver implementation. | 263 // ExtensionRegistryObserver implementation. |
| 236 void OnExtensionLoaded(content::BrowserContext* browser_context, | 264 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 237 const Extension* extension) override; | 265 const Extension* extension) override; |
| 238 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 266 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 239 const Extension* extension, | 267 const Extension* extension, |
| 240 UnloadedExtensionReason reason) override; | 268 UnloadedExtensionReason reason) override; |
| 241 | 269 |
| 270 void AddLazyEventListenerImpl(const std::string& event_name, |
| 271 const ExtensionId& extension_id, |
| 272 int worker_thread_id); |
| 273 void RemoveLazyEventListenerImpl(const std::string& event_name, |
| 274 const ExtensionId& extension_id, |
| 275 int worker_thread_id); |
| 276 |
| 242 // Shared by all event dispatch methods. If |restrict_to_extension_id| is | 277 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
| 243 // empty, the event is broadcast. An event that just came off the pending | 278 // empty, the event is broadcast. An event that just came off the pending |
| 244 // list may not be delayed again. | 279 // list may not be delayed again. |
| 245 void DispatchEventImpl(const std::string& restrict_to_extension_id, | 280 void DispatchEventImpl(const std::string& restrict_to_extension_id, |
| 246 const linked_ptr<Event>& event); | 281 const linked_ptr<Event>& event); |
| 247 | 282 |
| 248 // Ensures that all lazy background pages that are interested in the given | 283 // Ensures that all lazy background pages that are interested in the given |
| 249 // event are loaded, and queues the event if the page is not ready yet. | 284 // event are loaded, and queues the event if the page is not ready yet. |
| 250 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy | 285 // Inserts an EventDispatchIdentifier into |already_dispatched| for each lazy |
| 251 // event dispatch that is queued. | 286 // event dispatch that is queued. |
| 252 void DispatchLazyEvent(const std::string& extension_id, | 287 void DispatchLazyEvent(const std::string& extension_id, |
| 253 const linked_ptr<Event>& event, | 288 const linked_ptr<Event>& event, |
| 254 std::set<EventDispatchIdentifier>* already_dispatched, | 289 std::set<EventDispatchIdentifier>* already_dispatched, |
| 255 const base::DictionaryValue* listener_filter); | 290 const base::DictionaryValue* listener_filter); |
| 256 | 291 |
| 257 // Dispatches the event to the specified extension or URL running in | 292 // Dispatches the event to the specified extension or URL running in |
| 258 // |process|. | 293 // |process|. |
| 259 void DispatchEventToProcess(const std::string& extension_id, | 294 void DispatchEventToProcess(const std::string& extension_id, |
| 260 const GURL& listener_url, | 295 const GURL& listener_url, |
| 261 content::RenderProcessHost* process, | 296 content::RenderProcessHost* process, |
| 297 int worker_thread_id, |
| 262 const linked_ptr<Event>& event, | 298 const linked_ptr<Event>& event, |
| 263 const base::DictionaryValue* listener_filter, | 299 const base::DictionaryValue* listener_filter, |
| 264 bool did_enqueue); | 300 bool did_enqueue); |
| 265 | 301 |
| 266 // Returns false when the event is scoped to a context and the listening | 302 // Returns false when the event is scoped to a context and the listening |
| 267 // extension does not have access to events from that context. Also fills | 303 // extension does not have access to events from that context. Also fills |
| 268 // |event_args| with the proper arguments to send, which may differ if | 304 // |event_args| with the proper arguments to send, which may differ if |
| 269 // the event crosses the incognito boundary. | 305 // the event crosses the incognito boundary. |
| 270 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, | 306 bool CanDispatchEventToBrowserContext(content::BrowserContext* context, |
| 271 const Extension* extension, | 307 const Extension* extension, |
| (...skipping 157 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 429 const std::string event_name; | 465 const std::string event_name; |
| 430 | 466 |
| 431 const std::string extension_id; | 467 const std::string extension_id; |
| 432 const GURL listener_url; | 468 const GURL listener_url; |
| 433 content::BrowserContext* const browser_context; | 469 content::BrowserContext* const browser_context; |
| 434 }; | 470 }; |
| 435 | 471 |
| 436 } // namespace extensions | 472 } // namespace extensions |
| 437 | 473 |
| 438 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 474 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
| OLD | NEW |