| 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 <unordered_map> | 10 #include <unordered_map> |
| 11 #include <utility> | 11 #include <utility> |
| 12 | 12 |
| 13 #include "base/callback.h" | 13 #include "base/callback.h" |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.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 137 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 | 194 |
| 197 // Reports UMA for an event dispatched to |extension| with histogram value | 195 // Reports UMA for an event dispatched to |extension| with histogram value |
| 198 // |histogram_value|. Must be called on the UI thread. | 196 // |histogram_value|. Must be called on the UI thread. |
| 199 // | 197 // |
| 200 // |did_enqueue| should be true if the event was queued waiting for a process | 198 // |did_enqueue| should be true if the event was queued waiting for a process |
| 201 // to start, like an event page. | 199 // to start, like an event page. |
| 202 void ReportEvent(events::HistogramValue histogram_value, | 200 void ReportEvent(events::HistogramValue histogram_value, |
| 203 const Extension* extension, | 201 const Extension* extension, |
| 204 bool did_enqueue); | 202 bool did_enqueue); |
| 205 | 203 |
| 204 LazyEventDispatchUtil* lazy_event_dispatch_util() { |
| 205 return &lazy_event_dispatch_util_; |
| 206 } |
| 207 |
| 206 private: | 208 private: |
| 207 friend class EventRouterFilterTest; | 209 friend class EventRouterFilterTest; |
| 208 friend class EventRouterTest; | 210 friend class EventRouterTest; |
| 209 | 211 |
| 210 // An identifier for an event dispatch that is used to prevent double dispatch | 212 // An identifier for an event dispatch that is used to prevent double dispatch |
| 211 // due to race conditions between the direct and lazy dispatch paths. | 213 // due to race conditions between the direct and lazy dispatch paths. |
| 212 typedef std::pair<const content::BrowserContext*, std::string> | 214 typedef std::pair<const content::BrowserContext*, std::string> |
| 213 EventDispatchIdentifier; | 215 EventDispatchIdentifier; |
| 214 | 216 |
| 215 // TODO(gdk): Document this. | 217 // TODO(gdk): Document this. |
| 216 static void DispatchExtensionMessage( | 218 static void DispatchExtensionMessage( |
| 217 IPC::Sender* ipc_sender, | 219 IPC::Sender* ipc_sender, |
| 218 void* browser_context_id, | 220 void* browser_context_id, |
| 219 const std::string& extension_id, | 221 const std::string& extension_id, |
| 220 int event_id, | 222 int event_id, |
| 221 const std::string& event_name, | 223 const std::string& event_name, |
| 222 base::ListValue* event_args, | 224 base::ListValue* event_args, |
| 223 UserGestureState user_gesture, | 225 UserGestureState user_gesture, |
| 224 const extensions::EventFilteringInfo& info); | 226 const extensions::EventFilteringInfo& info); |
| 225 | 227 |
| 226 // Returns or sets the list of events for which the given extension has | 228 // Returns or sets the list of events for which the given extension has |
| 227 // registered. | 229 // registered. |
| 228 std::set<std::string> GetRegisteredEvents( | 230 std::set<std::string> GetRegisteredEvents( |
| 229 const std::string& extension_id) const; | 231 const std::string& extension_id) const; |
| 230 void SetRegisteredEvents(const std::string& extension_id, | 232 void SetRegisteredEvents(const std::string& extension_id, |
| 231 const std::set<std::string>& events); | 233 const std::set<std::string>& events); |
| 232 | 234 |
| 233 void Observe(int type, | |
| 234 const content::NotificationSource& source, | |
| 235 const content::NotificationDetails& details) override; | |
| 236 // ExtensionRegistryObserver implementation. | 235 // ExtensionRegistryObserver implementation. |
| 237 void OnExtensionLoaded(content::BrowserContext* browser_context, | 236 void OnExtensionLoaded(content::BrowserContext* browser_context, |
| 238 const Extension* extension) override; | 237 const Extension* extension) override; |
| 239 void OnExtensionUnloaded(content::BrowserContext* browser_context, | 238 void OnExtensionUnloaded(content::BrowserContext* browser_context, |
| 240 const Extension* extension, | 239 const Extension* extension, |
| 241 UnloadedExtensionReason reason) override; | 240 UnloadedExtensionReason reason) override; |
| 242 | 241 |
| 243 // Shared by all event dispatch methods. If |restrict_to_extension_id| is | 242 // Shared by all event dispatch methods. If |restrict_to_extension_id| is |
| 244 // empty, the event is broadcast. An event that just came off the pending | 243 // empty, the event is broadcast. An event that just came off the pending |
| 245 // list may not be delayed again. | 244 // list may not be delayed again. |
| (...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 323 base::TerminationStatus status, | 322 base::TerminationStatus status, |
| 324 int exit_code) override; | 323 int exit_code) override; |
| 325 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 324 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
| 326 | 325 |
| 327 content::BrowserContext* const browser_context_; | 326 content::BrowserContext* const browser_context_; |
| 328 | 327 |
| 329 // The ExtensionPrefs associated with |browser_context_|. May be NULL in | 328 // The ExtensionPrefs associated with |browser_context_|. May be NULL in |
| 330 // tests. | 329 // tests. |
| 331 ExtensionPrefs* const extension_prefs_; | 330 ExtensionPrefs* const extension_prefs_; |
| 332 | 331 |
| 333 content::NotificationRegistrar registrar_; | |
| 334 | |
| 335 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 332 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
| 336 extension_registry_observer_; | 333 extension_registry_observer_; |
| 337 | 334 |
| 338 EventListenerMap listeners_; | 335 EventListenerMap listeners_; |
| 339 | 336 |
| 340 // Map from base event name to observer. | 337 // Map from base event name to observer. |
| 341 using ObserverMap = std::unordered_map<std::string, Observer*>; | 338 using ObserverMap = std::unordered_map<std::string, Observer*>; |
| 342 ObserverMap observers_; | 339 ObserverMap observers_; |
| 343 | 340 |
| 344 std::set<content::RenderProcessHost*> observed_process_set_; | 341 std::set<content::RenderProcessHost*> observed_process_set_; |
| 345 | 342 |
| 343 LazyEventDispatchUtil lazy_event_dispatch_util_; |
| 344 |
| 346 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 345 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
| 347 }; | 346 }; |
| 348 | 347 |
| 349 struct Event { | 348 struct Event { |
| 350 // This callback should return true if the event should be dispatched to the | 349 // This callback should return true if the event should be dispatched to the |
| 351 // given context and extension, and false otherwise. | 350 // given context and extension, and false otherwise. |
| 352 using WillDispatchCallback = | 351 using WillDispatchCallback = |
| 353 base::Callback<bool(content::BrowserContext*, | 352 base::Callback<bool(content::BrowserContext*, |
| 354 const Extension*, | 353 const Extension*, |
| 355 Event*, | 354 Event*, |
| (...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 430 const std::string event_name; | 429 const std::string event_name; |
| 431 | 430 |
| 432 const std::string extension_id; | 431 const std::string extension_id; |
| 433 const GURL listener_url; | 432 const GURL listener_url; |
| 434 content::BrowserContext* const browser_context; | 433 content::BrowserContext* const browser_context; |
| 435 }; | 434 }; |
| 436 | 435 |
| 437 } // namespace extensions | 436 } // namespace extensions |
| 438 | 437 |
| 439 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 438 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
| OLD | NEW |