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 EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 5 #ifndef EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 6 #define EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
7 | 7 |
8 #include <set> | 8 #include <set> |
9 #include <string> | 9 #include <string> |
10 #include <utility> | 10 #include <utility> |
(...skipping 145 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
156 const base::DictionaryValue& filter, | 156 const base::DictionaryValue& filter, |
157 bool remove_lazy_listener); | 157 bool remove_lazy_listener); |
158 | 158 |
159 // Returns true if there is at least one listener for the given event. | 159 // Returns true if there is at least one listener for the given event. |
160 bool HasEventListener(const std::string& event_name); | 160 bool HasEventListener(const std::string& event_name); |
161 | 161 |
162 // Returns true if the extension is listening to the given event. | 162 // Returns true if the extension is listening to the given event. |
163 virtual bool ExtensionHasEventListener(const std::string& extension_id, | 163 virtual bool ExtensionHasEventListener(const std::string& extension_id, |
164 const std::string& event_name); | 164 const std::string& event_name); |
165 | 165 |
166 // Return or set the list of events for which the given extension has | |
167 // registered. | |
168 std::set<std::string> GetRegisteredEvents(const std::string& extension_id); | |
169 void SetRegisteredEvents(const std::string& extension_id, | |
170 const std::set<std::string>& events); | |
171 | |
172 // Broadcasts an event to every listener registered for that event. | 166 // Broadcasts an event to every listener registered for that event. |
173 virtual void BroadcastEvent(std::unique_ptr<Event> event); | 167 virtual void BroadcastEvent(std::unique_ptr<Event> event); |
174 | 168 |
175 // Dispatches an event to the given extension. | 169 // Dispatches an event to the given extension. |
176 virtual void DispatchEventToExtension(const std::string& extension_id, | 170 virtual void DispatchEventToExtension(const std::string& extension_id, |
177 std::unique_ptr<Event> event); | 171 std::unique_ptr<Event> event); |
178 | 172 |
179 // Dispatches |event| to the given extension as if the extension has a lazy | 173 // Dispatches |event| to the given extension as if the extension has a lazy |
180 // listener for it. NOTE: This should be used rarely, for dispatching events | 174 // listener for it. NOTE: This should be used rarely, for dispatching events |
181 // to extensions that haven't had a chance to add their own listeners yet, eg: | 175 // to extensions that haven't had a chance to add their own listeners yet, eg: |
182 // newly installed extensions. | 176 // newly installed extensions. |
183 void DispatchEventWithLazyListener(const std::string& extension_id, | 177 void DispatchEventWithLazyListener(const std::string& extension_id, |
184 std::unique_ptr<Event> event); | 178 std::unique_ptr<Event> event); |
185 | 179 |
186 // Record the Event Ack from the renderer. (One less event in-flight.) | 180 // Record the Event Ack from the renderer. (One less event in-flight.) |
187 void OnEventAck(content::BrowserContext* context, | 181 void OnEventAck(content::BrowserContext* context, |
188 const std::string& extension_id); | 182 const std::string& extension_id); |
189 | 183 |
| 184 // Returns whether or not the given extension has any registered events. |
| 185 bool HasRegisteredEvents(const ExtensionId& extension_id) const { |
| 186 return !GetRegisteredEvents(extension_id).empty(); |
| 187 } |
| 188 |
| 189 // Clears registered events for testing purposes. |
| 190 void ClearRegisteredEventsForTest(const ExtensionId& extension_id) { |
| 191 SetRegisteredEvents(extension_id, std::set<std::string>()); |
| 192 } |
| 193 |
190 // Reports UMA for an event dispatched to |extension| with histogram value | 194 // Reports UMA for an event dispatched to |extension| with histogram value |
191 // |histogram_value|. Must be called on the UI thread. | 195 // |histogram_value|. Must be called on the UI thread. |
192 // | 196 // |
193 // |did_enqueue| should be true if the event was queued waiting for a process | 197 // |did_enqueue| should be true if the event was queued waiting for a process |
194 // to start, like an event page. | 198 // to start, like an event page. |
195 void ReportEvent(events::HistogramValue histogram_value, | 199 void ReportEvent(events::HistogramValue histogram_value, |
196 const Extension* extension, | 200 const Extension* extension, |
197 bool did_enqueue); | 201 bool did_enqueue); |
198 | 202 |
199 private: | 203 private: |
200 friend class EventRouterFilterTest; | 204 friend class EventRouterFilterTest; |
201 friend class EventRouterTest; | 205 friend class EventRouterTest; |
202 | 206 |
203 // An identifier for an event dispatch that is used to prevent double dispatch | 207 // An identifier for an event dispatch that is used to prevent double dispatch |
204 // due to race conditions between the direct and lazy dispatch paths. | 208 // due to race conditions between the direct and lazy dispatch paths. |
205 typedef std::pair<const content::BrowserContext*, std::string> | 209 typedef std::pair<const content::BrowserContext*, std::string> |
206 EventDispatchIdentifier; | 210 EventDispatchIdentifier; |
207 | 211 |
208 // TODO(gdk): Document this. | 212 // TODO(gdk): Document this. |
209 static void DispatchExtensionMessage( | 213 static void DispatchExtensionMessage( |
210 IPC::Sender* ipc_sender, | 214 IPC::Sender* ipc_sender, |
211 void* browser_context_id, | 215 void* browser_context_id, |
212 const std::string& extension_id, | 216 const std::string& extension_id, |
213 int event_id, | 217 int event_id, |
214 const std::string& event_name, | 218 const std::string& event_name, |
215 base::ListValue* event_args, | 219 base::ListValue* event_args, |
216 UserGestureState user_gesture, | 220 UserGestureState user_gesture, |
217 const extensions::EventFilteringInfo& info); | 221 const extensions::EventFilteringInfo& info); |
218 | 222 |
| 223 // Returns or sets the list of events for which the given extension has |
| 224 // registered. |
| 225 std::set<std::string> GetRegisteredEvents( |
| 226 const std::string& extension_id) const; |
| 227 void SetRegisteredEvents(const std::string& extension_id, |
| 228 const std::set<std::string>& events); |
| 229 |
219 void Observe(int type, | 230 void Observe(int type, |
220 const content::NotificationSource& source, | 231 const content::NotificationSource& source, |
221 const content::NotificationDetails& details) override; | 232 const content::NotificationDetails& details) override; |
222 // ExtensionRegistryObserver implementation. | 233 // ExtensionRegistryObserver implementation. |
223 void OnExtensionLoaded(content::BrowserContext* browser_context, | 234 void OnExtensionLoaded(content::BrowserContext* browser_context, |
224 const Extension* extension) override; | 235 const Extension* extension) override; |
225 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 236 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
226 const Extension* extension, | 237 const Extension* extension, |
227 UnloadedExtensionReason reason) override; | 238 UnloadedExtensionReason reason) override; |
228 | 239 |
(...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
411 const std::string event_name; | 422 const std::string event_name; |
412 | 423 |
413 const std::string extension_id; | 424 const std::string extension_id; |
414 const GURL listener_url; | 425 const GURL listener_url; |
415 content::BrowserContext* browser_context; | 426 content::BrowserContext* browser_context; |
416 }; | 427 }; |
417 | 428 |
418 } // namespace extensions | 429 } // namespace extensions |
419 | 430 |
420 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 431 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |