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> |
11 #include <utility> | 11 #include <utility> |
12 | 12 |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/memory/linked_ptr.h" | 14 #include "base/memory/linked_ptr.h" |
15 #include "base/memory/ref_counted.h" | 15 #include "base/memory/ref_counted.h" |
16 #include "base/values.h" | 16 #include "base/values.h" |
17 #include "chrome/browser/extensions/event_listener_map.h" | 17 #include "chrome/browser/extensions/event_listener_map.h" |
18 #include "chrome/common/extensions/event_filtering_info.h" | 18 #include "chrome/common/extensions/event_filtering_info.h" |
19 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
20 #include "content/public/browser/notification_observer.h" | 20 #include "content/public/browser/notification_observer.h" |
21 #include "content/public/browser/notification_registrar.h" | 21 #include "content/public/browser/notification_registrar.h" |
22 #include "ipc/ipc_sender.h" | 22 #include "ipc/ipc_sender.h" |
23 | 23 |
24 class GURL; | 24 class GURL; |
25 class ExtensionDevToolsManager; | |
26 class Profile; | 25 class Profile; |
27 | 26 |
28 namespace content { | 27 namespace content { |
29 class RenderProcessHost; | 28 class RenderProcessHost; |
30 } | 29 } |
31 | 30 |
32 namespace extensions { | 31 namespace extensions { |
33 class Extension; | 32 class Extension; |
34 class ExtensionHost; | 33 class ExtensionHost; |
35 class ExtensionPrefs; | 34 class ExtensionPrefs; |
36 | 35 |
37 struct Event; | 36 struct Event; |
38 | 37 |
39 class EventRouter : public content::NotificationObserver, | 38 class EventRouter : public content::NotificationObserver, |
40 public EventListenerMap::Delegate { | 39 public EventListenerMap::Delegate { |
41 public: | 40 public: |
42 // These constants convey the state of our knowledge of whether we're in | 41 // These constants convey the state of our knowledge of whether we're in |
43 // a user-caused gesture as part of DispatchEvent. | 42 // a user-caused gesture as part of DispatchEvent. |
44 enum UserGestureState { | 43 enum UserGestureState { |
45 USER_GESTURE_UNKNOWN = 0, | 44 USER_GESTURE_UNKNOWN = 0, |
46 USER_GESTURE_ENABLED = 1, | 45 USER_GESTURE_ENABLED = 1, |
47 USER_GESTURE_NOT_ENABLED = 2, | 46 USER_GESTURE_NOT_ENABLED = 2, |
48 }; | 47 }; |
49 | 48 |
| 49 // Observers register interest in events with a particular name and are |
| 50 // notified when a listener is added or removed for that |event_name|. |
| 51 class Observer { |
| 52 public: |
| 53 // Called when a listener is added. |
| 54 virtual void OnListenerAdded(const std::string& event_name) {} |
| 55 // Called when a listener is removed. |
| 56 virtual void OnListenerRemoved(const std::string& event_name) {} |
| 57 }; |
| 58 |
50 // Sends an event via ipc_sender to the given extension. Can be called on any | 59 // Sends an event via ipc_sender to the given extension. Can be called on any |
51 // thread. | 60 // thread. |
52 static void DispatchEvent(IPC::Sender* ipc_sender, | 61 static void DispatchEvent(IPC::Sender* ipc_sender, |
53 const std::string& extension_id, | 62 const std::string& extension_id, |
54 const std::string& event_name, | 63 const std::string& event_name, |
55 scoped_ptr<base::ListValue> event_args, | 64 scoped_ptr<base::ListValue> event_args, |
56 const GURL& event_url, | 65 const GURL& event_url, |
57 UserGestureState user_gesture, | 66 UserGestureState user_gesture, |
58 const EventFilteringInfo& info); | 67 const EventFilteringInfo& info); |
59 | 68 |
60 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs); | 69 EventRouter(Profile* profile, ExtensionPrefs* extension_prefs); |
61 virtual ~EventRouter(); | 70 virtual ~EventRouter(); |
62 | 71 |
63 // Add or remove the process/extension pair as a listener for |event_name|. | 72 // 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 | 73 // 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 | 74 // collapsing. Also, a single extension can have 2 processes if it is a split |
66 // mode extension. | 75 // mode extension. |
67 void AddEventListener(const std::string& event_name, | 76 void AddEventListener(const std::string& event_name, |
68 content::RenderProcessHost* process, | 77 content::RenderProcessHost* process, |
69 const std::string& extension_id); | 78 const std::string& extension_id); |
70 void RemoveEventListener(const std::string& event_name, | 79 void RemoveEventListener(const std::string& event_name, |
71 content::RenderProcessHost* process, | 80 content::RenderProcessHost* process, |
72 const std::string& extension_id); | 81 const std::string& extension_id); |
73 | 82 |
74 EventListenerMap& listeners() { return listeners_; } | 83 EventListenerMap& listeners() { return listeners_; } |
75 | 84 |
| 85 // Registers an observer to be notified when an event listener for |
| 86 // |event_name| is added or removed. There can currently be only one observer |
| 87 // for each distinct |event_name|. |
| 88 void RegisterObserver(Observer* observer, |
| 89 const std::string& event_name); |
| 90 |
| 91 // Unregisters an observer from all events. |
| 92 void UnregisterObserver(Observer* observer); |
| 93 |
76 // Add or remove the extension as having a lazy background page that listens | 94 // 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 | 95 // 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 | 96 // remembered even after the process goes away. We use this list to decide |
79 // which extension pages to load when dispatching an event. | 97 // which extension pages to load when dispatching an event. |
80 void AddLazyEventListener(const std::string& event_name, | 98 void AddLazyEventListener(const std::string& event_name, |
81 const std::string& extension_id); | 99 const std::string& extension_id); |
82 void RemoveLazyEventListener(const std::string& event_name, | 100 void RemoveLazyEventListener(const std::string& event_name, |
83 const std::string& extension_id); | 101 const std::string& extension_id); |
84 | 102 |
85 // If |add_lazy_listener| is true also add the lazy version of this listener. | 103 // If |add_lazy_listener| is true also add the lazy version of this listener. |
(...skipping 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
242 ExtensionHost* host); | 260 ExtensionHost* host); |
243 | 261 |
244 // Implementation of EventListenerMap::Delegate. | 262 // Implementation of EventListenerMap::Delegate. |
245 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; | 263 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; |
246 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; | 264 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
247 | 265 |
248 Profile* profile_; | 266 Profile* profile_; |
249 | 267 |
250 content::NotificationRegistrar registrar_; | 268 content::NotificationRegistrar registrar_; |
251 | 269 |
252 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 270 EventListenerMap listeners_; |
253 | 271 |
254 EventListenerMap listeners_; | 272 typedef std::map<std::string, Observer*> ObserverMap; |
| 273 ObserverMap observers_; |
255 | 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; |
(...skipping 18 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 |