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_EXTENSION_EVENT_ROUTER_H_ | 5 #ifndef CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 6 #define CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
7 #pragma once | 7 #pragma once |
8 | 8 |
9 #include <map> | 9 #include <map> |
10 #include <set> | 10 #include <set> |
11 #include <string> | 11 #include <string> |
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 "content/public/browser/notification_observer.h" | 16 #include "content/public/browser/notification_observer.h" |
17 #include "content/public/browser/notification_registrar.h" | 17 #include "content/public/browser/notification_registrar.h" |
| 18 #include "chrome/common/extensions/feature.h" |
18 #include "ipc/ipc_message.h" | 19 #include "ipc/ipc_message.h" |
19 | 20 |
20 class GURL; | 21 class GURL; |
21 class Extension; | 22 class Extension; |
22 class ExtensionHost; | 23 class ExtensionHost; |
23 class ExtensionDevToolsManager; | 24 class ExtensionDevToolsManager; |
24 class Profile; | 25 class Profile; |
25 | 26 |
| 27 using extensions::Feature; |
| 28 |
26 namespace content { | 29 namespace content { |
27 class RenderProcessHost; | 30 class RenderProcessHost; |
28 } | 31 } |
29 | 32 |
30 class ExtensionEventRouter : public content::NotificationObserver { | 33 class ExtensionEventRouter : public content::NotificationObserver { |
31 public: | 34 public: |
32 // These constants convey the state of our knowledge of whether we're in | 35 // These constants convey the state of our knowledge of whether we're in |
33 // a user-caused gesture as part of DispatchEvent. | 36 // a user-caused gesture as part of DispatchEvent. |
34 enum UserGestureState { | 37 enum UserGestureState { |
35 USER_GESTURE_UNKNOWN = 0, | 38 USER_GESTURE_UNKNOWN = 0, |
(...skipping 11 matching lines...) Expand all Loading... |
47 UserGestureState user_gesture); | 50 UserGestureState user_gesture); |
48 | 51 |
49 explicit ExtensionEventRouter(Profile* profile); | 52 explicit ExtensionEventRouter(Profile* profile); |
50 virtual ~ExtensionEventRouter(); | 53 virtual ~ExtensionEventRouter(); |
51 | 54 |
52 // Add or remove the process/extension pair as a listener for |event_name|. | 55 // Add or remove the process/extension pair as a listener for |event_name|. |
53 // Note that multiple extensions can share a process due to process | 56 // Note that multiple extensions can share a process due to process |
54 // collapsing. Also, a single extension can have 2 processes if it is a split | 57 // collapsing. Also, a single extension can have 2 processes if it is a split |
55 // mode extension. | 58 // mode extension. |
56 void AddEventListener(const std::string& event_name, | 59 void AddEventListener(const std::string& event_name, |
| 60 const std::string& extension_id, |
57 content::RenderProcessHost* process, | 61 content::RenderProcessHost* process, |
58 const std::string& extension_id); | 62 Feature::Context context); |
59 void RemoveEventListener(const std::string& event_name, | 63 void RemoveEventListener(const std::string& event_name, |
60 content::RenderProcessHost* process, | 64 content::RenderProcessHost* process, |
61 const std::string& extension_id); | 65 const std::string& extension_id); |
62 | 66 |
63 // Add or remove the extension as having a lazy background page that listens | 67 // Add or remove the extension as having a lazy background page that listens |
64 // to the event. The difference from the above methods is that these will be | 68 // to the event. The difference from the above methods is that these will be |
65 // remembered even after the process goes away. We use this list to decide | 69 // remembered even after the process goes away. We use this list to decide |
66 // which extension pages to load when dispatching an event. | 70 // which extension pages to load when dispatching an event. |
67 void AddLazyEventListener(const std::string& event_name, | 71 void AddLazyEventListener(const std::string& event_name, |
| 72 const std::string& extension_id, |
| 73 content::RenderProcessHost* process, |
| 74 Feature::Context context); |
| 75 // Use this overload when subscribing extensions to events from C++, or when |
| 76 // access checking the calling process and context is not required. |
| 77 void AddLazyEventListener(const std::string& event_name, |
68 const std::string& extension_id); | 78 const std::string& extension_id); |
69 void RemoveLazyEventListener(const std::string& event_name, | 79 void RemoveLazyEventListener(const std::string& event_name, |
70 const std::string& extension_id); | 80 const std::string& extension_id); |
71 | 81 |
72 // Returns true if there is at least one listener for the given event. | 82 // Returns true if there is at least one listener for the given event. |
73 bool HasEventListener(const std::string& event_name); | 83 bool HasEventListener(const std::string& event_name); |
74 | 84 |
75 // Returns true if the extension is listening to the given event. | 85 // Returns true if the extension is listening to the given event. |
76 bool ExtensionHasEventListener(const std::string& extension_id, | 86 bool ExtensionHasEventListener(const std::string& extension_id, |
77 const std::string& event_name); | 87 const std::string& event_name); |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
124 void OnExtensionEventAck(Profile* profile, const std::string& extension_id); | 134 void OnExtensionEventAck(Profile* profile, const std::string& extension_id); |
125 | 135 |
126 private: | 136 private: |
127 // The details of an event to be dispatched. | 137 // The details of an event to be dispatched. |
128 struct ExtensionEvent; | 138 struct ExtensionEvent; |
129 | 139 |
130 // The extension and process that contains the event listener for a given | 140 // The extension and process that contains the event listener for a given |
131 // event. | 141 // event. |
132 struct ListenerProcess; | 142 struct ListenerProcess; |
133 | 143 |
| 144 // Helper that returns whether the event is accessible to the calling context. |
| 145 bool IsEventAvailable(const std::string& event_name, |
| 146 const std::string& extension_id, |
| 147 content::RenderProcessHost* render_process, |
| 148 Feature::Context context_type) const; |
| 149 |
134 // A map between an event name and a set of extensions that are listening | 150 // A map between an event name and a set of extensions that are listening |
135 // to that event. | 151 // to that event. |
136 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; | 152 typedef std::map<std::string, std::set<ListenerProcess> > ListenerMap; |
137 | 153 |
138 virtual void Observe(int type, | 154 virtual void Observe(int type, |
139 const content::NotificationSource& source, | 155 const content::NotificationSource& source, |
140 const content::NotificationDetails& details) OVERRIDE; | 156 const content::NotificationDetails& details) OVERRIDE; |
141 | 157 |
142 // Returns true if the given listener map contains a event listeners for | 158 // Returns true if the given listener map contains a event listeners for |
143 // the given event. If |extension_id| is non-empty, we also check that that | 159 // the given event. If |extension_id| is non-empty, we also check that that |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
200 | 216 |
201 // The list of all the lazy (non-persistent) background pages that are | 217 // The list of all the lazy (non-persistent) background pages that are |
202 // listening to events. This is just a cache of the real list, which is | 218 // listening to events. This is just a cache of the real list, which is |
203 // stored on disk in the extension prefs. | 219 // stored on disk in the extension prefs. |
204 ListenerMap lazy_listeners_; | 220 ListenerMap lazy_listeners_; |
205 | 221 |
206 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); | 222 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); |
207 }; | 223 }; |
208 | 224 |
209 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 225 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
OLD | NEW |