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> |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
43 public extensions::EventListenerMap::Delegate { | 43 public extensions::EventListenerMap::Delegate { |
44 public: | 44 public: |
45 // These constants convey the state of our knowledge of whether we're in | 45 // These constants convey the state of our knowledge of whether we're in |
46 // a user-caused gesture as part of DispatchEvent. | 46 // a user-caused gesture as part of DispatchEvent. |
47 enum UserGestureState { | 47 enum UserGestureState { |
48 USER_GESTURE_UNKNOWN = 0, | 48 USER_GESTURE_UNKNOWN = 0, |
49 USER_GESTURE_ENABLED = 1, | 49 USER_GESTURE_ENABLED = 1, |
50 USER_GESTURE_NOT_ENABLED = 2, | 50 USER_GESTURE_NOT_ENABLED = 2, |
51 }; | 51 }; |
52 | 52 |
53 // Sends an event via ipc_sender to the given extension. Can be called on | 53 // Sends an event via ipc_sender to the given extension. Can be called on any |
54 // any thread. | 54 // thread. Takes ownership of |event_args|, but can be safely invoked with |
55 // NULL. | |
55 static void DispatchEvent(IPC::Sender* ipc_sender, | 56 static void DispatchEvent(IPC::Sender* ipc_sender, |
miket_OOO
2012/07/10 22:33:19
Great, makes more sense now. I'd like to see simil
| |
56 const std::string& extension_id, | 57 const std::string& extension_id, |
57 const std::string& event_name, | 58 const std::string& event_name, |
58 const base::Value& event_args, | 59 base::ListValue* event_args, |
sky
2012/07/16 22:29:06
Did you consider making this take a const base::Li
| |
59 const GURL& event_url, | 60 const GURL& event_url, |
60 UserGestureState user_gesture, | 61 UserGestureState user_gesture, |
61 const extensions::EventFilteringInfo& info); | 62 const extensions::EventFilteringInfo& info); |
62 | |
63 // This invocation is deprecated. All future consumers of this API should be | |
64 // sending Values as event arguments, using the above version. | |
65 static void DispatchEvent(IPC::Sender* ipc_sender, | |
66 const std::string& extension_id, | |
67 const std::string& event_name, | |
68 const std::string& event_args, | |
69 const GURL& event_url, | |
70 UserGestureState user_gesture, | |
71 const extensions::EventFilteringInfo& info); | |
72 | 63 |
73 explicit ExtensionEventRouter(Profile* profile); | 64 explicit ExtensionEventRouter(Profile* profile); |
74 virtual ~ExtensionEventRouter(); | 65 virtual ~ExtensionEventRouter(); |
75 | 66 |
76 // Add or remove the process/extension pair as a listener for |event_name|. | 67 // Add or remove the process/extension pair as a listener for |event_name|. |
77 // Note that multiple extensions can share a process due to process | 68 // Note that multiple extensions can share a process due to process |
78 // collapsing. Also, a single extension can have 2 processes if it is a split | 69 // collapsing. Also, a single extension can have 2 processes if it is a split |
79 // mode extension. | 70 // mode extension. |
80 void AddEventListener(const std::string& event_name, | 71 void AddEventListener(const std::string& event_name, |
81 content::RenderProcessHost* process, | 72 content::RenderProcessHost* process, |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
113 | 104 |
114 // Returns true if the extension is listening to the given event. | 105 // Returns true if the extension is listening to the given event. |
115 bool ExtensionHasEventListener(const std::string& extension_id, | 106 bool ExtensionHasEventListener(const std::string& extension_id, |
116 const std::string& event_name); | 107 const std::string& event_name); |
117 | 108 |
118 // Send an event to every registered extension renderer. If | 109 // Send an event to every registered extension renderer. If |
119 // |restrict_to_profile| is non-NULL, then the event will not be sent to other | 110 // |restrict_to_profile| is non-NULL, then the event will not be sent to other |
120 // profiles unless the extension has permission (e.g. incognito tab update -> | 111 // profiles unless the extension has permission (e.g. incognito tab update -> |
121 // normal profile only works if extension is allowed incognito access). If | 112 // normal profile only works if extension is allowed incognito access). If |
122 // |event_url| is not empty, the event is only sent to extension with host | 113 // |event_url| is not empty, the event is only sent to extension with host |
123 // permissions for this url. | 114 // permissions for this url. Takes ownership of |event_args|, but can be |
115 // safely invoked with NULL. | |
124 void DispatchEventToRenderers( | 116 void DispatchEventToRenderers( |
125 const std::string& event_name, | 117 const std::string& event_name, |
126 const std::string& event_args, | 118 base::ListValue* event_args, |
127 Profile* restrict_to_profile, | 119 Profile* restrict_to_profile, |
128 const GURL& event_url, | 120 const GURL& event_url, |
129 extensions::EventFilteringInfo info); | 121 extensions::EventFilteringInfo info); |
130 | 122 |
131 // As above, but defaults |info| to EventFilteringInfo(). | 123 // As above, but defaults |info| to EventFilteringInfo(). Takes ownership of |
124 // |event_args|, but can be safely invoked with NULL. | |
132 void DispatchEventToRenderers( | 125 void DispatchEventToRenderers( |
133 const std::string& event_name, | 126 const std::string& event_name, |
134 const std::string& event_args, | 127 base::ListValue* event_args, |
135 Profile* restrict_to_profile, | 128 Profile* restrict_to_profile, |
136 const GURL& event_url); | 129 const GURL& event_url); |
137 | 130 |
138 // Same as above, except only send the event to the given extension. | 131 // Same as above, except only send the event to the given extension. Takes |
132 // ownership of |event_args|, but can be safely invoked with NULL. | |
139 virtual void DispatchEventToExtension( | 133 virtual void DispatchEventToExtension( |
140 const std::string& extension_id, | 134 const std::string& extension_id, |
141 const std::string& event_name, | 135 const std::string& event_name, |
142 const base::Value& event_args, | 136 base::ListValue* event_args, |
143 Profile* restrict_to_profile, | 137 Profile* restrict_to_profile, |
144 const GURL& event_url); | 138 const GURL& event_url); |
145 | 139 |
146 // This invocation is deprecated. The above variant which uses a Value for | 140 // Dispatch an event to particular extension. Also include an explicit user |
147 // event_args is to be used instead. | 141 // gesture indicator. Takes ownership of |event_args|, but can be safely |
142 // invoked with NULL. | |
148 virtual void DispatchEventToExtension( | 143 virtual void DispatchEventToExtension( |
149 const std::string& extension_id, | 144 const std::string& extension_id, |
150 const std::string& event_name, | 145 const std::string& event_name, |
151 const std::string& event_args, | 146 base::ListValue* event_args, |
152 Profile* restrict_to_profile, | |
153 const GURL& event_url); | |
154 | |
155 // Dispatch an event to particular extension. Also include an | |
156 // explicit user gesture indicator. | |
157 virtual void DispatchEventToExtension( | |
158 const std::string& extension_id, | |
159 const std::string& event_name, | |
160 const std::string& event_args, | |
161 Profile* restrict_to_profile, | 147 Profile* restrict_to_profile, |
162 const GURL& event_url, | 148 const GURL& event_url, |
163 UserGestureState user_gesture); | 149 UserGestureState user_gesture); |
164 | 150 |
165 // Send different versions of an event to extensions in different profiles. | 151 // Send different versions of an event to extensions in different profiles. |
166 // This is used in the case of sending one event to extensions that have | 152 // This is used in the case of sending one event to extensions that have |
167 // incognito access, and another event to extensions that don't (here), | 153 // incognito access, and another event to extensions that don't (here), |
168 // in order to avoid sending 2 events to "spanning" extensions. | 154 // in order to avoid sending 2 events to "spanning" extensions. |
169 // If |cross_incognito_profile| is non-NULL and different from | 155 // If |cross_incognito_profile| is non-NULL and different from |
170 // restrict_to_profile, send the event with cross_incognito_args to the | 156 // restrict_to_profile, send the event with cross_incognito_args to the |
171 // extensions in that profile that can't cross incognito. | 157 // extensions in that profile that can't cross incognito. Takes ownership of |
158 // |event_args| and |cross_incognito_args|, but can be safely invoked with | |
159 // NULL for either. | |
172 void DispatchEventsToRenderersAcrossIncognito( | 160 void DispatchEventsToRenderersAcrossIncognito( |
173 const std::string& event_name, | 161 const std::string& event_name, |
174 const std::string& event_args, | 162 base::ListValue* event_args, |
175 Profile* restrict_to_profile, | 163 Profile* restrict_to_profile, |
176 const std::string& cross_incognito_args, | 164 base::ListValue* cross_incognito_args, |
177 const GURL& event_url); | 165 const GURL& event_url); |
178 | 166 |
179 // Record the Event Ack from the renderer. (One less event in-flight.) | 167 // Record the Event Ack from the renderer. (One less event in-flight.) |
180 void OnEventAck(Profile* profile, const std::string& extension_id); | 168 void OnEventAck(Profile* profile, const std::string& extension_id); |
181 | 169 |
182 private: | 170 private: |
183 // The extension and process that contains the event listener for a given | 171 // The extension and process that contains the event listener for a given |
184 // event. | 172 // event. |
185 struct ListenerProcess; | 173 struct ListenerProcess; |
186 | 174 |
(...skipping 23 matching lines...) Expand all Loading... | |
210 void DispatchLazyEvent(const std::string& extension_id, | 198 void DispatchLazyEvent(const std::string& extension_id, |
211 const linked_ptr<ExtensionEvent>& event); | 199 const linked_ptr<ExtensionEvent>& event); |
212 | 200 |
213 // Dispatches the event to the specified extension running in |process|. | 201 // Dispatches the event to the specified extension running in |process|. |
214 void DispatchEventToProcess(const std::string& extension_id, | 202 void DispatchEventToProcess(const std::string& extension_id, |
215 content::RenderProcessHost* process, | 203 content::RenderProcessHost* process, |
216 const linked_ptr<ExtensionEvent>& event); | 204 const linked_ptr<ExtensionEvent>& event); |
217 | 205 |
218 // Returns false when the event is scoped to a profile and the listening | 206 // Returns false when the event is scoped to a profile and the listening |
219 // extension does not have access to events from that profile. Also fills | 207 // extension does not have access to events from that profile. Also fills |
220 // |event_args| with the proper arguments to send, which may differ if | 208 // |event_args| (if non-NULL) with the proper arguments to send, which may |
221 // the event crosses the incognito boundary. | 209 // differ if the event crosses the incognito boundary. |
222 bool CanDispatchEventToProfile( | 210 bool CanDispatchEventToProfile( |
223 Profile* profile, | 211 Profile* profile, |
224 const extensions::Extension* extension, | 212 const extensions::Extension* extension, |
225 const linked_ptr<ExtensionEvent>& event, | 213 const linked_ptr<ExtensionEvent>& event, |
226 const base::Value** event_args); | 214 base::ListValue** event_args); |
227 | 215 |
228 // Possibly loads given extension's background page in preparation to | 216 // Possibly loads given extension's background page in preparation to |
229 // dispatch an event. | 217 // dispatch an event. |
230 void MaybeLoadLazyBackgroundPageToDispatchEvent( | 218 void MaybeLoadLazyBackgroundPageToDispatchEvent( |
231 Profile* profile, | 219 Profile* profile, |
232 const extensions::Extension* extension, | 220 const extensions::Extension* extension, |
233 const linked_ptr<ExtensionEvent>& event); | 221 const linked_ptr<ExtensionEvent>& event); |
234 | 222 |
235 // Track of the number of dispatched events that have not yet sent an | 223 // Track of the number of dispatched events that have not yet sent an |
236 // ACK from the renderer. | 224 // ACK from the renderer. |
(...skipping 13 matching lines...) Expand all Loading... | |
250 | 238 |
251 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; | 239 scoped_refptr<ExtensionDevToolsManager> extension_devtools_manager_; |
252 | 240 |
253 EventListenerMap listeners_; | 241 EventListenerMap listeners_; |
254 | 242 |
255 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); | 243 DISALLOW_COPY_AND_ASSIGN(ExtensionEventRouter); |
256 }; | 244 }; |
257 | 245 |
258 struct ExtensionEvent { | 246 struct ExtensionEvent { |
259 std::string event_name; | 247 std::string event_name; |
260 scoped_ptr<Value> event_args; | 248 scoped_ptr<ListValue> event_args; |
261 GURL event_url; | 249 GURL event_url; |
262 Profile* restrict_to_profile; | 250 Profile* restrict_to_profile; |
263 scoped_ptr<Value> cross_incognito_args; | 251 scoped_ptr<ListValue> cross_incognito_args; |
264 ExtensionEventRouter::UserGestureState user_gesture; | 252 ExtensionEventRouter::UserGestureState user_gesture; |
265 extensions::EventFilteringInfo info; | 253 extensions::EventFilteringInfo info; |
266 | 254 |
267 ExtensionEvent(const std::string& event_name, | 255 ExtensionEvent(const std::string& event_name, |
268 const Value& event_args, | 256 ListValue* event_args, |
269 const GURL& event_url, | 257 const GURL& event_url, |
270 Profile* restrict_to_profile, | 258 Profile* restrict_to_profile, |
271 const Value& cross_incognito_args, | 259 ListValue* cross_incognito_args, |
272 ExtensionEventRouter::UserGestureState user_gesture, | 260 ExtensionEventRouter::UserGestureState user_gesture, |
273 const extensions::EventFilteringInfo& info); | 261 const extensions::EventFilteringInfo& info); |
274 | |
275 // TODO(gdk): This variant should be retired once the callers are switched to | |
276 // providing Values instead of just strings. | |
277 ExtensionEvent(const std::string& event_name, | |
278 const std::string& event_args, | |
279 const GURL& event_url, | |
280 Profile* restrict_to_profile, | |
281 const std::string& cross_incognito_args, | |
282 ExtensionEventRouter::UserGestureState user_gesture, | |
283 const extensions::EventFilteringInfo& info); | |
284 | |
285 ExtensionEvent(const std::string& event_name, | |
286 const Value& event_args, | |
287 const GURL& event_url, | |
288 Profile* restrict_to_profile, | |
289 ExtensionEventRouter::UserGestureState user_gesture, | |
290 const extensions::EventFilteringInfo& info); | |
291 | |
292 ~ExtensionEvent(); | 262 ~ExtensionEvent(); |
293 }; | 263 }; |
294 | 264 |
295 | |
296 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ | 265 #endif // CHROME_BROWSER_EXTENSIONS_EXTENSION_EVENT_ROUTER_H_ |
OLD | NEW |