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 <map> | 8 #include <map> |
9 #include <set> | 9 #include <set> |
10 #include <string> | 10 #include <string> |
(...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
61 // the base name of the event (e.g. adding an event listener for event name | 61 // the base name of the event (e.g. adding an event listener for event name |
62 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). | 62 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). |
63 class Observer { | 63 class Observer { |
64 public: | 64 public: |
65 // Called when a listener is added. | 65 // Called when a listener is added. |
66 virtual void OnListenerAdded(const EventListenerInfo& details) {} | 66 virtual void OnListenerAdded(const EventListenerInfo& details) {} |
67 // Called when a listener is removed. | 67 // Called when a listener is removed. |
68 virtual void OnListenerRemoved(const EventListenerInfo& details) {} | 68 virtual void OnListenerRemoved(const EventListenerInfo& details) {} |
69 }; | 69 }; |
70 | 70 |
71 // The EventDispatchObserver is notified on the UI thread whenever | |
72 // an event is dispatched. There can be only one EventDispatchObserver. | |
73 class EventDispatchObserver { | |
74 public: | |
75 virtual void OnWillDispatchEvent(scoped_ptr<EventDispatchInfo> details) = 0; | |
76 }; | |
77 | |
78 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names | 71 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names |
79 // without a "/" are returned unchanged. | 72 // without a "/" are returned unchanged. |
80 static std::string GetBaseEventName(const std::string& full_event_name); | 73 static std::string GetBaseEventName(const std::string& full_event_name); |
81 | 74 |
82 // Sends an event via ipc_sender to the given extension. Can be called on any | 75 // Sends an event via ipc_sender to the given extension. Can be called on any |
83 // thread. | 76 // thread. |
84 static void DispatchEvent(IPC::Sender* ipc_sender, | 77 static void DispatchEvent(IPC::Sender* ipc_sender, |
85 void* browser_context_id, | 78 void* browser_context_id, |
86 const std::string& extension_id, | 79 const std::string& extension_id, |
87 const std::string& event_name, | 80 const std::string& event_name, |
(...skipping 22 matching lines...) Expand all Loading... |
110 | 103 |
111 // Registers an observer to be notified when an event listener for | 104 // Registers an observer to be notified when an event listener for |
112 // |event_name| is added or removed. There can currently be only one observer | 105 // |event_name| is added or removed. There can currently be only one observer |
113 // for each distinct |event_name|. | 106 // for each distinct |event_name|. |
114 void RegisterObserver(Observer* observer, | 107 void RegisterObserver(Observer* observer, |
115 const std::string& event_name); | 108 const std::string& event_name); |
116 | 109 |
117 // Unregisters an observer from all events. | 110 // Unregisters an observer from all events. |
118 void UnregisterObserver(Observer* observer); | 111 void UnregisterObserver(Observer* observer); |
119 | 112 |
120 // Sets the observer to be notified whenever an event is dispatched to an | |
121 // extension. | |
122 void SetEventDispatchObserver(EventDispatchObserver* observer); | |
123 | |
124 // Add or remove the extension as having a lazy background page that listens | 113 // Add or remove the extension as having a lazy background page that listens |
125 // to the event. The difference from the above methods is that these will be | 114 // to the event. The difference from the above methods is that these will be |
126 // remembered even after the process goes away. We use this list to decide | 115 // remembered even after the process goes away. We use this list to decide |
127 // which extension pages to load when dispatching an event. | 116 // which extension pages to load when dispatching an event. |
128 void AddLazyEventListener(const std::string& event_name, | 117 void AddLazyEventListener(const std::string& event_name, |
129 const std::string& extension_id); | 118 const std::string& extension_id); |
130 void RemoveLazyEventListener(const std::string& event_name, | 119 void RemoveLazyEventListener(const std::string& event_name, |
131 const std::string& extension_id); | 120 const std::string& extension_id); |
132 | 121 |
133 // If |add_lazy_listener| is true also add the lazy version of this listener. | 122 // If |add_lazy_listener| is true also add the lazy version of this listener. |
(...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
185 | 174 |
186 // A map between an event name and a set of extensions that are listening | 175 // A map between an event name and a set of extensions that are listening |
187 // to that event. | 176 // to that event. |
188 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; | 177 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
189 | 178 |
190 // An identifier for an event dispatch that is used to prevent double dispatch | 179 // An identifier for an event dispatch that is used to prevent double dispatch |
191 // due to race conditions between the direct and lazy dispatch paths. | 180 // due to race conditions between the direct and lazy dispatch paths. |
192 typedef std::pair<const content::BrowserContext*, std::string> | 181 typedef std::pair<const content::BrowserContext*, std::string> |
193 EventDispatchIdentifier; | 182 EventDispatchIdentifier; |
194 | 183 |
195 // Sends a notification about an event to the event dispatch observer on the | |
196 // UI thread. Can be called from any thread. | |
197 static void NotifyExtensionDispatchObserverOnUIThread( | |
198 void* browser_context_id, | |
199 scoped_ptr<EventDispatchInfo> details); | |
200 | |
201 // TODO(gdk): Document this. | 184 // TODO(gdk): Document this. |
202 static void DispatchExtensionMessage( | 185 static void DispatchExtensionMessage( |
203 IPC::Sender* ipc_sender, | 186 IPC::Sender* ipc_sender, |
204 void* browser_context_id, | 187 void* browser_context_id, |
205 const std::string& extension_id, | 188 const std::string& extension_id, |
206 const std::string& event_name, | 189 const std::string& event_name, |
207 base::ListValue* event_args, | 190 base::ListValue* event_args, |
208 UserGestureState user_gesture, | 191 UserGestureState user_gesture, |
209 const extensions::EventFilteringInfo& info); | 192 const extensions::EventFilteringInfo& info); |
210 | 193 |
(...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
293 ExtensionPrefs* extension_prefs_; | 276 ExtensionPrefs* extension_prefs_; |
294 | 277 |
295 content::NotificationRegistrar registrar_; | 278 content::NotificationRegistrar registrar_; |
296 | 279 |
297 EventListenerMap listeners_; | 280 EventListenerMap listeners_; |
298 | 281 |
299 // Map from base event name to observer. | 282 // Map from base event name to observer. |
300 typedef base::hash_map<std::string, Observer*> ObserverMap; | 283 typedef base::hash_map<std::string, Observer*> ObserverMap; |
301 ObserverMap observers_; | 284 ObserverMap observers_; |
302 | 285 |
303 EventDispatchObserver* event_dispatch_observer_; | |
304 | |
305 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 286 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
306 }; | 287 }; |
307 | 288 |
308 struct Event { | 289 struct Event { |
309 typedef base::Callback<void(content::BrowserContext*, | 290 typedef base::Callback<void(content::BrowserContext*, |
310 const Extension*, | 291 const Extension*, |
311 base::ListValue*)> WillDispatchCallback; | 292 base::ListValue*)> WillDispatchCallback; |
312 | 293 |
313 // The event to dispatch. | 294 // The event to dispatch. |
314 std::string event_name; | 295 std::string event_name; |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
370 const std::string& extension_id, | 351 const std::string& extension_id, |
371 content::BrowserContext* browser_context); | 352 content::BrowserContext* browser_context); |
372 // The event name including any sub-event, e.g. "runtime.onStartup" or | 353 // The event name including any sub-event, e.g. "runtime.onStartup" or |
373 // "webRequest.onCompleted/123". | 354 // "webRequest.onCompleted/123". |
374 const std::string event_name; | 355 const std::string event_name; |
375 | 356 |
376 const std::string extension_id; | 357 const std::string extension_id; |
377 content::BrowserContext* browser_context; | 358 content::BrowserContext* browser_context; |
378 }; | 359 }; |
379 | 360 |
380 struct EventDispatchInfo { | |
381 EventDispatchInfo(const std::string& extension_id, | |
382 const std::string& event_name, | |
383 scoped_ptr<base::ListValue> event_args); | |
384 ~EventDispatchInfo(); | |
385 | |
386 const std::string extension_id; | |
387 const std::string event_name; | |
388 scoped_ptr<base::ListValue> event_args; | |
389 }; | |
390 | |
391 } // namespace extensions | 361 } // namespace extensions |
392 | 362 |
393 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 363 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |