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 explicit EventRouter(Profile* profile); |
61 virtual ~EventRouter(); | 70 virtual ~EventRouter(); |
62 | 71 |
72 // Completes initialization once ExtensionPrefs are available. | |
73 void InitWithPrefs(ExtensionPrefs* extension_prefs); | |
74 | |
63 // Add or remove the process/extension pair as a listener for |event_name|. | 75 // 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 | 76 // 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 | 77 // collapsing. Also, a single extension can have 2 processes if it is a split |
66 // mode extension. | 78 // mode extension. |
67 void AddEventListener(const std::string& event_name, | 79 void AddEventListener(const std::string& event_name, |
68 content::RenderProcessHost* process, | 80 content::RenderProcessHost* process, |
69 const std::string& extension_id); | 81 const std::string& extension_id); |
70 void RemoveEventListener(const std::string& event_name, | 82 void RemoveEventListener(const std::string& event_name, |
71 content::RenderProcessHost* process, | 83 content::RenderProcessHost* process, |
72 const std::string& extension_id); | 84 const std::string& extension_id); |
73 | 85 |
74 EventListenerMap& listeners() { return listeners_; } | 86 EventListenerMap& listeners() { return listeners_; } |
75 | 87 |
88 // Registers an observer, interested in listeners that receive the event | |
Aaron Boodman
2012/11/07 23:51:28
Nit: comment a little unclear. How about just "Reg
Yoyo Zhou
2012/11/08 00:18:16
Done.
| |
89 // |event_name|, to be notified when such a listener is added or removed. | |
90 // There can currently be only one observer for each distinct |event_name|. | |
91 void RegisterObserver(Observer* observer, | |
92 const std::string& event_name); | |
93 | |
94 // Unregisters an observer from all events. | |
95 void UnregisterObserver(Observer* observer); | |
96 | |
76 // Add or remove the extension as having a lazy background page that listens | 97 // 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 | 98 // 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 | 99 // remembered even after the process goes away. We use this list to decide |
79 // which extension pages to load when dispatching an event. | 100 // which extension pages to load when dispatching an event. |
80 void AddLazyEventListener(const std::string& event_name, | 101 void AddLazyEventListener(const std::string& event_name, |
81 const std::string& extension_id); | 102 const std::string& extension_id); |
82 void RemoveLazyEventListener(const std::string& event_name, | 103 void RemoveLazyEventListener(const std::string& event_name, |
83 const std::string& extension_id); | 104 const std::string& extension_id); |
84 | 105 |
85 // If |add_lazy_listener| is true also add the lazy version of this listener. | 106 // 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); | 263 ExtensionHost* host); |
243 | 264 |
244 // Implementation of EventListenerMap::Delegate. | 265 // Implementation of EventListenerMap::Delegate. |
245 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; | 266 virtual void OnListenerAdded(const EventListener* listener) OVERRIDE; |
246 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; | 267 virtual void OnListenerRemoved(const EventListener* listener) OVERRIDE; |
247 | 268 |
248 Profile* profile_; | 269 Profile* profile_; |
249 | 270 |
250 content::NotificationRegistrar registrar_; | 271 content::NotificationRegistrar registrar_; |
251 | 272 |
252 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 273 EventListenerMap listeners_; |
253 | 274 |
254 EventListenerMap listeners_; | 275 typedef std::map<std::string, Observer*> ObserverMap; |
276 ObserverMap observers_; | |
255 | 277 |
256 // True if we should dispatch the event signalling that Chrome was updated | 278 // True if we should dispatch the event signalling that Chrome was updated |
257 // upon loading an extension. | 279 // upon loading an extension. |
258 bool dispatch_chrome_updated_event_; | 280 bool dispatch_chrome_updated_event_; |
259 | 281 |
260 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 282 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
261 }; | 283 }; |
262 | 284 |
263 struct Event { | 285 struct Event { |
264 std::string event_name; | 286 std::string event_name; |
(...skipping 18 matching lines...) Expand all Loading... | |
283 Profile* restrict_to_profile, | 305 Profile* restrict_to_profile, |
284 EventRouter::UserGestureState user_gesture, | 306 EventRouter::UserGestureState user_gesture, |
285 const EventFilteringInfo& info); | 307 const EventFilteringInfo& info); |
286 | 308 |
287 ~Event(); | 309 ~Event(); |
288 }; | 310 }; |
289 | 311 |
290 } // namespace extensions | 312 } // namespace extensions |
291 | 313 |
292 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ | 314 #endif // CHROME_BROWSER_EXTENSIONS_EVENT_ROUTER_H_ |
OLD | NEW |