Chromium Code Reviews| 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 29 matching lines...) Expand all Loading... | |
| 40 public EventListenerMap::Delegate { | 40 public EventListenerMap::Delegate { |
| 41 public: | 41 public: |
| 42 // These constants convey the state of our knowledge of whether we're in | 42 // These constants convey the state of our knowledge of whether we're in |
| 43 // a user-caused gesture as part of DispatchEvent. | 43 // a user-caused gesture as part of DispatchEvent. |
| 44 enum UserGestureState { | 44 enum UserGestureState { |
| 45 USER_GESTURE_UNKNOWN = 0, | 45 USER_GESTURE_UNKNOWN = 0, |
| 46 USER_GESTURE_ENABLED = 1, | 46 USER_GESTURE_ENABLED = 1, |
| 47 USER_GESTURE_NOT_ENABLED = 2, | 47 USER_GESTURE_NOT_ENABLED = 2, |
| 48 }; | 48 }; |
| 49 | 49 |
| 50 // Observers register interest in events with a particular name and are | |
| 51 // notified when a listener is added or removed for that |event_name|. | |
| 52 class Observer { | |
| 53 public: | |
| 54 // Called when a listener is added. | |
| 55 virtual void OnListenerAdded(const std::string& event_name) {} | |
| 56 // Called when a listener is removed. | |
| 57 virtual void OnListenerRemoved(const std::string& event_name) {} | |
| 58 }; | |
| 59 | |
| 50 // Sends an event via ipc_sender to the given extension. Can be called on any | 60 // Sends an event via ipc_sender to the given extension. Can be called on any |
| 51 // thread. | 61 // thread. |
| 52 static void DispatchEvent(IPC::Sender* ipc_sender, | 62 static void DispatchEvent(IPC::Sender* ipc_sender, |
| 53 const std::string& extension_id, | 63 const std::string& extension_id, |
| 54 const std::string& event_name, | 64 const std::string& event_name, |
| 55 scoped_ptr<base::ListValue> event_args, | 65 scoped_ptr<base::ListValue> event_args, |
| 56 const GURL& event_url, | 66 const GURL& event_url, |
| 57 UserGestureState user_gesture, | 67 UserGestureState user_gesture, |
| 58 const EventFilteringInfo& info); | 68 const EventFilteringInfo& info); |
| 59 | 69 |
| 60 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs); | 70 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs); |
| 61 virtual ~EventRouter(); | 71 virtual ~EventRouter(); |
| 62 | 72 |
| 63 // Add or remove the process/extension pair as a listener for |event_name|. | 73 // Add or remove the process/extension pair as a listener for |event_name|. |
| 64 // Note that multiple extensions can share a process due to process | 74 // Note that multiple extensions can share a process due to process |
| 65 // collapsing. Also, a single extension can have 2 processes if it is a split | 75 // collapsing. Also, a single extension can have 2 processes if it is a split |
| 66 // mode extension. | 76 // mode extension. |
| 67 void AddEventListener(const std::string& event_name, | 77 void AddEventListener(const std::string& event_name, |
| 68 content::RenderProcessHost* process, | 78 content::RenderProcessHost* process, |
| 69 const std::string& extension_id); | 79 const std::string& extension_id); |
| 70 void RemoveEventListener(const std::string& event_name, | 80 void RemoveEventListener(const std::string& event_name, |
| 71 content::RenderProcessHost* process, | 81 content::RenderProcessHost* process, |
| 72 const std::string& extension_id); | 82 const std::string& extension_id); |
| 73 | 83 |
| 74 EventListenerMap& listeners() { return listeners_; } | 84 EventListenerMap& listeners() { return listeners_; } |
| 75 | 85 |
| 86 // Registers an observer, interested in listeners that receive the event | |
| 87 // |event_name|, to be notified when such a listener is added or removed. | |
| 88 // There can currently be only one observer for each distinct |event_name|. | |
| 89 void RegisterObserver(Observer* observer, | |
|
Aaron Boodman
2012/11/07 04:08:20
Do we not need an UnregisterObserver? And if so, I
Yoyo Zhou
2012/11/07 22:53:36
After thinking about this, I do think UnregisterOb
| |
| 90 const std::string& event_name); | |
| 91 | |
| 76 // Add or remove the extension as having a lazy background page that listens | 92 // Add or remove the extension as having a lazy background page that listens |
| 77 // to the event. The difference from the above methods is that these will be | 93 // to the event. The difference from the above methods is that these will be |
| 78 // remembered even after the process goes away. We use this list to decide | 94 // remembered even after the process goes away. We use this list to decide |
| 79 // which extension pages to load when dispatching an event. | 95 // which extension pages to load when dispatching an event. |
| 80 void AddLazyEventListener(const std::string& event_name, | 96 void AddLazyEventListener(const std::string& event_name, |
| 81 const std::string& extension_id); | 97 const std::string& extension_id); |
| 82 void RemoveLazyEventListener(const std::string& event_name, | 98 void RemoveLazyEventListener(const std::string& event_name, |
| 83 const std::string& extension_id); | 99 const std::string& extension_id); |
| 84 | 100 |
| 85 // If |add_lazy_listener| is true also add the lazy version of this listener. | 101 // If |add_lazy_listener| is true also add the lazy version of this listener. |
| (...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 246 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; | 262 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
| 247 | 263 |
| 248 Profile* profile_; | 264 Profile* profile_; |
| 249 | 265 |
| 250 content::NotificationRegistrar registrar_; | 266 content::NotificationRegistrar registrar_; |
| 251 | 267 |
| 252 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 268 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
| 253 | 269 |
| 254 EventListenerMap listeners_; | 270 EventListenerMap listeners_; |
| 255 | 271 |
| 272 typedef std::map<std::string, Observer*> ObserverMap; | |
| 273 ObserverMap observers_; | |
| 274 | |
| 256 // True if we should dispatch the event signalling that Chrome was updated | 275 // True if we should dispatch the event signalling that Chrome was updated |
| 257 // upon loading an extension. | 276 // upon loading an extension. |
| 258 bool dispatch_chrome_updated_event_; | 277 bool dispatch_chrome_updated_event_; |
| 259 | 278 |
| 260 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 279 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
| 261 }; | 280 }; |
| 262 | 281 |
| 263 struct Event { | 282 struct Event { |
| 264 std::string event_name; | 283 std::string event_name; |
| 265 scoped_ptr<base::ListValue> event_args; | 284 scoped_ptr<base::ListValue> event_args; |
| (...skipping 17 matching lines...) Expand all Loading... | |
| 283 Profile* restrict_to_profile, | 302 Profile* restrict_to_profile, |
| 284 EventRouter::UserGestureState user_gesture, | 303 EventRouter::UserGestureState user_gesture, |
| 285 const EventFilteringInfo& info); | 304 const EventFilteringInfo& info); |
| 286 | 305 |
| 287 ~Event(); | 306 ~Event(); |
| 288 }; | 307 }; |
| 289 | 308 |
| 290 } // namespace extensions | 309 } // namespace extensions |
| 291 | 310 |
| 292 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ | 311 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
| OLD | NEW |