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 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
65 // Observers register interest in events with a particular name and are | 65 // Observers register interest in events with a particular name and are |
66 // notified when a listener is added or removed. Observers are matched by | 66 // notified when a listener is added or removed. Observers are matched by |
67 // the base name of the event (e.g. adding an event listener for event name | 67 // the base name of the event (e.g. adding an event listener for event name |
68 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). | 68 // "foo.onBar/123" will trigger observers registered for "foo.onBar"). |
69 class Observer { | 69 class Observer { |
70 public: | 70 public: |
71 // Called when a listener is added. | 71 // Called when a listener is added. |
72 virtual void OnListenerAdded(const EventListenerInfo& details) {} | 72 virtual void OnListenerAdded(const EventListenerInfo& details) {} |
73 // Called when a listener is removed. | 73 // Called when a listener is removed. |
74 virtual void OnListenerRemoved(const EventListenerInfo& details) {} | 74 virtual void OnListenerRemoved(const EventListenerInfo& details) {} |
75 | |
76 protected: | |
77 virtual ~Observer() {} | |
75 }; | 78 }; |
76 | 79 |
77 // Gets the EventRouter for |browser_context|. | 80 // Gets the EventRouter for |browser_context|. |
78 static EventRouter* Get(content::BrowserContext* browser_context); | 81 static EventRouter* Get(content::BrowserContext* browser_context); |
79 | 82 |
80 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names | 83 // Converts event names like "foo.onBar/123" into "foo.onBar". Event names |
81 // without a "/" are returned unchanged. | 84 // without a "/" are returned unchanged. |
82 static std::string GetBaseEventName(const std::string& full_event_name); | 85 static std::string GetBaseEventName(const std::string& full_event_name); |
83 | 86 |
84 // Sends an event via ipc_sender to the given extension. Can be called on any | 87 // Sends an event via ipc_sender to the given extension. Can be called on any |
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
119 const GURL& listener_url); | 122 const GURL& listener_url); |
120 void RemoveEventListenerForURL(const std::string& event_name, | 123 void RemoveEventListenerForURL(const std::string& event_name, |
121 content::RenderProcessHost* process, | 124 content::RenderProcessHost* process, |
122 const GURL& listener_url); | 125 const GURL& listener_url); |
123 | 126 |
124 EventListenerMap& listeners() { return listeners_; } | 127 EventListenerMap& listeners() { return listeners_; } |
125 | 128 |
126 // Registers an observer to be notified when an event listener for | 129 // Registers an observer to be notified when an event listener for |
127 // |event_name| is added or removed. There can currently be only one observer | 130 // |event_name| is added or removed. There can currently be only one observer |
128 // for each distinct |event_name|. | 131 // for each distinct |event_name|. |
129 void RegisterObserver(Observer* observer, | 132 void RegisterObserver(Observer* observer, const std::string& event_name); |
130 const std::string& event_name); | |
131 | 133 |
132 // Unregisters an observer from all events. | 134 // Unregisters an observer from all events. |
133 void UnregisterObserver(Observer* observer); | 135 void UnregisterObserver(Observer* observer); |
134 | 136 |
135 // Add or remove the extension as having a lazy background page that listens | 137 // Add or remove the extension as having a lazy background page that listens |
136 // to the event. The difference from the above methods is that these will be | 138 // to the event. The difference from the above methods is that these will be |
137 // remembered even after the process goes away. We use this list to decide | 139 // remembered even after the process goes away. We use this list to decide |
138 // which extension pages to load when dispatching an event. | 140 // which extension pages to load when dispatching an event. |
139 void AddLazyEventListener(const std::string& event_name, | 141 void AddLazyEventListener(const std::string& event_name, |
140 const std::string& extension_id); | 142 const std::string& extension_id); |
141 void RemoveLazyEventListener(const std::string& event_name, | 143 void RemoveLazyEventListener(const std::string& event_name, |
142 const std::string& extension_id); | 144 const std::string& extension_id); |
143 | 145 |
144 // If |add_lazy_listener| is true also add the lazy version of this listener. | 146 // If |add_lazy_listener| is true also add the lazy version of this listener. |
145 void AddFilteredEventListener(const std::string& event_name, | 147 void AddFilteredEventListener(const std::string& event_name, |
146 content::RenderProcessHost* process, | 148 content::RenderProcessHost* process, |
147 const std::string& extension_id, | 149 const std::string& extension_id, |
148 const base::DictionaryValue& filter, | 150 const base::DictionaryValue& filter, |
149 bool add_lazy_listener); | 151 bool add_lazy_listener); |
150 | 152 |
151 // If |remove_lazy_listener| is true also remove the lazy version of this | 153 // If |remove_lazy_listener| is true also remove the lazy version of this |
152 // listener. | 154 // listener. |
153 void RemoveFilteredEventListener(const std::string& event_name, | 155 void RemoveFilteredEventListener(const std::string& event_name, |
154 content::RenderProcessHost* process, | 156 content::RenderProcessHost* process, |
155 const std::string& extension_id, | 157 const std::string& extension_id, |
156 const base::DictionaryValue& filter, | 158 const base::DictionaryValue& filter, |
157 bool remove_lazy_listener); | 159 bool remove_lazy_listener); |
158 | 160 |
159 // Returns true if there is at least one listener for the given event. | 161 // Returns true if there is at least one listener for the given event. |
160 bool HasEventListener(const std::string& event_name); | 162 bool HasEventListener(const std::string& event_name) const; |
161 | 163 |
162 // Returns true if the extension is listening to the given event. | 164 // Returns true if the extension is listening to the given event. |
165 // (virtual for testing only.) | |
163 virtual bool ExtensionHasEventListener(const std::string& extension_id, | 166 virtual bool ExtensionHasEventListener(const std::string& extension_id, |
164 const std::string& event_name); | 167 const std::string& event_name) const; |
165 | 168 |
166 // Broadcasts an event to every listener registered for that event. | 169 // Broadcasts an event to every listener registered for that event. |
167 virtual void BroadcastEvent(std::unique_ptr<Event> event); | 170 virtual void BroadcastEvent(std::unique_ptr<Event> event); |
168 | 171 |
169 // Dispatches an event to the given extension. | 172 // Dispatches an event to the given extension. |
170 virtual void DispatchEventToExtension(const std::string& extension_id, | 173 virtual void DispatchEventToExtension(const std::string& extension_id, |
171 std::unique_ptr<Event> event); | 174 std::unique_ptr<Event> event); |
172 | 175 |
173 // Dispatches |event| to the given extension as if the extension has a lazy | 176 // Dispatches |event| to the given extension as if the extension has a lazy |
174 // listener for it. NOTE: This should be used rarely, for dispatching events | 177 // listener for it. NOTE: This should be used rarely, for dispatching events |
(...skipping 139 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
314 // Implementation of EventListenerMap::Delegate. | 317 // Implementation of EventListenerMap::Delegate. |
315 void OnListenerAdded(const EventListener* listener) override; | 318 void OnListenerAdded(const EventListener* listener) override; |
316 void OnListenerRemoved(const EventListener* listener) override; | 319 void OnListenerRemoved(const EventListener* listener) override; |
317 | 320 |
318 // RenderProcessHostObserver implementation. | 321 // RenderProcessHostObserver implementation. |
319 void RenderProcessExited(content::RenderProcessHost* host, | 322 void RenderProcessExited(content::RenderProcessHost* host, |
320 base::TerminationStatus status, | 323 base::TerminationStatus status, |
321 int exit_code) override; | 324 int exit_code) override; |
322 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; | 325 void RenderProcessHostDestroyed(content::RenderProcessHost* host) override; |
323 | 326 |
324 content::BrowserContext* browser_context_; | 327 content::BrowserContext* const browser_context_; |
325 | 328 |
326 // The ExtensionPrefs associated with |browser_context_|. May be NULL in | 329 // The ExtensionPrefs associated with |browser_context_|. May be NULL in |
327 // tests. | 330 // tests. |
328 ExtensionPrefs* extension_prefs_; | 331 ExtensionPrefs* const extension_prefs_; |
329 | 332 |
330 content::NotificationRegistrar registrar_; | 333 content::NotificationRegistrar registrar_; |
331 | 334 |
332 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> | 335 ScopedObserver<ExtensionRegistry, ExtensionRegistryObserver> |
333 extension_registry_observer_; | 336 extension_registry_observer_; |
334 | 337 |
335 EventListenerMap listeners_; | 338 EventListenerMap listeners_; |
336 | 339 |
337 // Map from base event name to observer. | 340 // Map from base event name to observer. |
338 typedef base::hash_map<std::string, Observer*> ObserverMap; | 341 using ObserverMap = base::hash_map<std::string, Observer*>; |
Devlin
2017/05/20 00:49:16
s/base::hash_map/std::unordered_map?
Lei Zhang
2017/05/20 01:04:14
Done.
| |
339 ObserverMap observers_; | 342 ObserverMap observers_; |
340 | 343 |
341 std::set<content::RenderProcessHost*> observed_process_set_; | 344 std::set<content::RenderProcessHost*> observed_process_set_; |
342 | 345 |
343 DISALLOW_COPY_AND_ASSIGN(EventRouter); | 346 DISALLOW_COPY_AND_ASSIGN(EventRouter); |
344 }; | 347 }; |
345 | 348 |
346 struct Event { | 349 struct Event { |
347 // This callback should return true if the event should be dispatched to the | 350 // This callback should return true if the event should be dispatched to the |
348 // given context and extension, and false otherwise. | 351 // given context and extension, and false otherwise. |
349 typedef base::Callback<bool(content::BrowserContext*, | 352 using WillDispatchCallback = |
350 const Extension*, | 353 base::Callback<bool(content::BrowserContext*, |
351 Event*, | 354 const Extension*, |
352 const base::DictionaryValue*)> | 355 Event*, |
353 WillDispatchCallback; | 356 const base::DictionaryValue*)>; |
354 | 357 |
355 // The identifier for the event, for histograms. In most cases this | 358 // The identifier for the event, for histograms. In most cases this |
356 // correlates 1:1 with |event_name|, in some cases events will generate | 359 // correlates 1:1 with |event_name|, in some cases events will generate |
357 // their own names, but they cannot generate their own identifier. | 360 // their own names, but they cannot generate their own identifier. |
358 events::HistogramValue histogram_value; | 361 const events::HistogramValue histogram_value; |
359 | 362 |
360 // The event to dispatch. | 363 // The event to dispatch. |
361 std::string event_name; | 364 const std::string event_name; |
362 | 365 |
363 // Arguments to send to the event listener. | 366 // Arguments to send to the event listener. |
364 std::unique_ptr<base::ListValue> event_args; | 367 std::unique_ptr<base::ListValue> event_args; |
365 | 368 |
366 // If non-NULL, then the event will not be sent to other BrowserContexts | 369 // If non-NULL, then the event will not be sent to other BrowserContexts |
367 // unless the extension has permission (e.g. incognito tab update -> normal | 370 // unless the extension has permission (e.g. incognito tab update -> normal |
368 // tab only works if extension is allowed incognito access). | 371 // tab only works if extension is allowed incognito access). |
369 content::BrowserContext* restrict_to_browser_context; | 372 content::BrowserContext* restrict_to_browser_context; |
370 | 373 |
371 // If not empty, the event is only sent to extensions with host permissions | 374 // If not empty, the event is only sent to extensions with host permissions |
(...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
416 EventListenerInfo(const std::string& event_name, | 419 EventListenerInfo(const std::string& event_name, |
417 const std::string& extension_id, | 420 const std::string& extension_id, |
418 const GURL& listener_url, | 421 const GURL& listener_url, |
419 content::BrowserContext* browser_context); | 422 content::BrowserContext* browser_context); |
420 // The event name including any sub-event, e.g. "runtime.onStartup" or | 423 // The event name including any sub-event, e.g. "runtime.onStartup" or |
421 // "webRequest.onCompleted/123". | 424 // "webRequest.onCompleted/123". |
422 const std::string event_name; | 425 const std::string event_name; |
423 | 426 |
424 const std::string extension_id; | 427 const std::string extension_id; |
425 const GURL listener_url; | 428 const GURL listener_url; |
426 content::BrowserContext* browser_context; | 429 content::BrowserContext* const browser_context; |
427 }; | 430 }; |
428 | 431 |
429 } // namespace extensions | 432 } // namespace extensions |
430 | 433 |
431 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ | 434 #endif // EXTENSIONS_BROWSER_EVENT_ROUTER_H_ |
OLD | NEW |