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> |
11 | 11 |
12 #include "base/callback.h" | 12 #include "base/callback.h" |
13 #include "base/compiler_specific.h" | 13 #include "base/compiler_specific.h" |
14 #include "base/containers/hash_tables.h" | 14 #include "base/containers/hash_tables.h" |
15 #include "base/macros.h" | 15 #include "base/macros.h" |
16 #include "base/memory/linked_ptr.h" | 16 #include "base/memory/linked_ptr.h" |
17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
18 #include "base/scoped_observer.h" | 18 #include "base/scoped_observer.h" |
19 #include "base/values.h" | 19 #include "base/values.h" |
20 #include "components/keyed_service/core/keyed_service.h" | 20 #include "components/keyed_service/core/keyed_service.h" |
21 #include "content/public/browser/notification_observer.h" | |
22 #include "content/public/browser/notification_registrar.h" | |
23 #include "content/public/browser/render_process_host_observer.h" | 21 #include "content/public/browser/render_process_host_observer.h" |
24 #include "extensions/browser/event_listener_map.h" | 22 #include "extensions/browser/event_listener_map.h" |
| 23 #include "extensions/browser/events/lazy_event_dispatch_util.h" |
25 #include "extensions/browser/extension_event_histogram_value.h" | 24 #include "extensions/browser/extension_event_histogram_value.h" |
26 #include "extensions/browser/extension_registry_observer.h" | 25 #include "extensions/browser/extension_registry_observer.h" |
27 #include "extensions/common/event_filtering_info.h" | 26 #include "extensions/common/event_filtering_info.h" |
28 #include "ipc/ipc_sender.h" | 27 #include "ipc/ipc_sender.h" |
29 #include "url/gurl.h" | 28 #include "url/gurl.h" |
30 | 29 |
31 class GURL; | 30 class GURL; |
32 | 31 |
33 namespace content { | 32 namespace content { |
34 class BrowserContext; | 33 class BrowserContext; |
35 class RenderProcessHost; | 34 class RenderProcessHost; |
36 } | 35 } |
37 | 36 |
38 namespace extensions { | 37 namespace extensions { |
39 class Extension; | 38 class Extension; |
40 class ExtensionHost; | 39 class ExtensionHost; |
41 class ExtensionPrefs; | 40 class ExtensionPrefs; |
42 class ExtensionRegistry; | 41 class ExtensionRegistry; |
43 | 42 |
44 struct Event; | 43 struct Event; |
45 struct EventListenerInfo; | 44 struct EventListenerInfo; |
46 | 45 |
47 class EventRouter : public KeyedService, | 46 class EventRouter : public KeyedService, |
48 public content::NotificationObserver, | |
49 public ExtensionRegistryObserver, | 47 public ExtensionRegistryObserver, |
50 public EventListenerMap::Delegate, | 48 public EventListenerMap::Delegate, |
51 public content::RenderProcessHostObserver { | 49 public content::RenderProcessHostObserver { |
52 public: | 50 public: |
53 // These constants convey the state of our knowledge of whether we're in | 51 // These constants convey the state of our knowledge of whether we're in |
54 // a user-caused gesture as part of DispatchEvent. | 52 // a user-caused gesture as part of DispatchEvent. |
55 enum UserGestureState { | 53 enum UserGestureState { |
56 USER_GESTURE_UNKNOWN = 0, | 54 USER_GESTURE_UNKNOWN = 0, |
57 USER_GESTURE_ENABLED = 1, | 55 USER_GESTURE_ENABLED = 1, |
58 USER_GESTURE_NOT_ENABLED = 2, | 56 USER_GESTURE_NOT_ENABLED = 2, |
(...skipping 134 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
193 | 191 |
194 // Reports UMA for an event dispatched to |extension| with histogram value | 192 // Reports UMA for an event dispatched to |extension| with histogram value |
195 // |histogram_value|. Must be called on the UI thread. | 193 // |histogram_value|. Must be called on the UI thread. |
196 // | 194 // |
197 // |did_enqueue| should be true if the event was queued waiting for a process | 195 // |did_enqueue| should be true if the event was queued waiting for a process |
198 // to start, like an event page. | 196 // to start, like an event page. |
199 void ReportEvent(events::HistogramValue histogram_value, | 197 void ReportEvent(events::HistogramValue histogram_value, |
200 const Extension* extension, | 198 const Extension* extension, |
201 bool did_enqueue); | 199 bool did_enqueue); |
202 | 200 |
| 201 LazyEventDispatchUtil* lazy_event_dispatch_util() { |
| 202 return &lazy_event_dispatch_util_; |
| 203 } |
| 204 |
203 private: | 205 private: |
204 friend class EventRouterFilterTest; | 206 friend class EventRouterFilterTest; |
205 friend class EventRouterTest; | 207 friend class EventRouterTest; |
206 | 208 |
207 // An identifier for an event dispatch that is used to prevent double dispatch | 209 // An identifier for an event dispatch that is used to prevent double dispatch |
208 // due to race conditions between the direct and lazy dispatch paths. | 210 // due to race conditions between the direct and lazy dispatch paths. |
209 typedef std::pair<const content::BrowserContext*, std::string> | 211 typedef std::pair<const content::BrowserContext*, std::string> |
210 EventDispatchIdentifier; | 212 EventDispatchIdentifier; |
211 | 213 |
212 // TODO(gdk): Document this. | 214 // TODO(gdk): Document this. |
213 static void DispatchExtensionMessage( | 215 static void DispatchExtensionMessage( |
214 IPC::Sender* ipc_sender, | 216 IPC::Sender* ipc_sender, |
215 void* browser_context_id, | 217 void* browser_context_id, |
216 const std::string& extension_id, | 218 const std::string& extension_id, |
217 int event_id, | 219 int event_id, |
218 const std::string& event_name, | 220 const std::string& event_name, |
219 base::ListValue* event_args, | 221 base::ListValue* event_args, |
220 UserGestureState user_gesture, | 222 UserGestureState user_gesture, |
221 const extensions::EventFilteringInfo& info); | 223 const extensions::EventFilteringInfo& info); |
222 | 224 |
223 // Returns or sets the list of events for which the given extension has | 225 // Returns or sets the list of events for which the given extension has |
224 // registered. | 226 // registered. |
225 std::set<std::string> GetRegisteredEvents( | 227 std::set<std::string> GetRegisteredEvents( |
226 const std::string& extension_id) const; | 228 const std::string& extension_id) const; |
227 void SetRegisteredEvents(const std::string& extension_id, | 229 void SetRegisteredEvents(const std::string& extension_id, |
228 const std::set<std::string>& events); | 230 const std::set<std::string>& events); |
229 | 231 |
230 void Observe(int type, | |
231 const content::NotificationSource& source, | |
232 const content::NotificationDetails& details) override; | |
233 // ExtensionRegistryObserver implementation. | 232 // ExtensionRegistryObserver implementation. |
234 void OnExtensionLoaded(content::BrowserContext* browser_context, | 233 void OnExtensionLoaded(content::BrowserContext* browser_context, |
235 const Extension* extension) override; | 234 const Extension* extension) override; |
236 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 235 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
237 const Extension* extension, | 236 const Extension* extension, |
238 UnloadedExtensionReason reason) override; | 237 UnloadedExtensionReason reason) override; |
239 | 238 |
240 // Shared by all event dispatch methods. If |restrict_to_extension_id| is | 239 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
241 // empty, the event is broadcast. An event that just came off the pending | 240 // empty, the event is broadcast. An event that just came off the pending |
242 // list may not be delayed again. | 241 // list may not be delayed again. |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
320 base::TerminationStatus status, | 319 base::TerminationStatus status, |
321 int exit_code) override; | 320 int exit_code) override; |
322 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 321 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
323 | 322 |
324 content::BrowserContext* browser_context_; | 323 content::BrowserContext* browser_context_; |
325 | 324 |
326 // The ExtensionPrefs associated with |browser_context_|. May be NULL in | 325 // The ExtensionPrefs associated with |browser_context_|. May be NULL in |
327 // tests. | 326 // tests. |
328 ExtensionPrefs* extension_prefs_; | 327 ExtensionPrefs* extension_prefs_; |
329 | 328 |
330 content::NotificationRegistrar registrar_; | |
331 | |
332 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 329 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
333 extension_registry_observer_; | 330 extension_registry_observer_; |
334 | 331 |
335 EventListenerMap listeners_; | 332 EventListenerMap listeners_; |
336 | 333 |
337 // Map from base event name to observer. | 334 // Map from base event name to observer. |
338 typedef base::hash_map<std::string, Observer*> ObserverMap; | 335 typedef base::hash_map<std::string, Observer*> ObserverMap; |
339 ObserverMap observers_; | 336 ObserverMap observers_; |
340 | 337 |
341 std::set<content::RenderProcessHost*> observed_process_set_; | 338 std::set<content::RenderProcessHost*> observed_process_set_; |
342 | 339 |
| 340 LazyEventDispatchUtil lazy_event_dispatch_util_; |
| 341 |
343 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 342 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
344 }; | 343 }; |
345 | 344 |
346 struct Event { | 345 struct Event { |
347 // This callback should return true if the event should be dispatched to the | 346 // This callback should return true if the event should be dispatched to the |
348 // given context and extension, and false otherwise. | 347 // given context and extension, and false otherwise. |
349 typedef base::Callback<bool(content::BrowserContext*, | 348 typedef base::Callback<bool(content::BrowserContext*, |
350 const Extension*, | 349 const Extension*, |
351 Event*, | 350 Event*, |
352 const base::DictionaryValue*)> | 351 const base::DictionaryValue*)> |
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
422 const std::string event_name; | 421 const std::string event_name; |
423 | 422 |
424 const std::string extension_id; | 423 const std::string extension_id; |
425 const GURL listener_url; | 424 const GURL listener_url; |
426 content::BrowserContext* browser_context; | 425 content::BrowserContext* browser_context; |
427 }; | 426 }; |
428 | 427 |
429 } // namespace extensions | 428 } // namespace extensions |
430 | 429 |
431 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 430 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |