| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_LISTENER_MAP_H_ | 5 #ifndef EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ |
| 6 #define EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ | 6 #define EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> |
| 9 #include <set> | 10 #include <set> |
| 10 #include <string> | 11 #include <string> |
| 11 #include <vector> | 12 #include <vector> |
| 12 | 13 |
| 13 #include "base/macros.h" | 14 #include "base/macros.h" |
| 14 #include "base/memory/scoped_ptr.h" | |
| 15 #include "extensions/common/event_filter.h" | 15 #include "extensions/common/event_filter.h" |
| 16 #include "url/gurl.h" | 16 #include "url/gurl.h" |
| 17 | 17 |
| 18 namespace base { | 18 namespace base { |
| 19 class DictionaryValue; | 19 class DictionaryValue; |
| 20 } | 20 } |
| 21 | 21 |
| 22 namespace content { | 22 namespace content { |
| 23 class BrowserContext; | 23 class BrowserContext; |
| 24 class RenderProcessHost; | 24 class RenderProcessHost; |
| (...skipping 18 matching lines...) Expand all Loading... |
| 43 public: | 43 public: |
| 44 // Constructs EventListeners for either an Extension or a URL. | 44 // Constructs EventListeners for either an Extension or a URL. |
| 45 // | 45 // |
| 46 // |filter| represents a generic filter structure that EventFilter knows how | 46 // |filter| represents a generic filter structure that EventFilter knows how |
| 47 // to filter events with. A typical filter instance will look like | 47 // to filter events with. A typical filter instance will look like |
| 48 // | 48 // |
| 49 // { | 49 // { |
| 50 // url: [{hostSuffix: 'google.com'}], | 50 // url: [{hostSuffix: 'google.com'}], |
| 51 // tabId: 5 | 51 // tabId: 5 |
| 52 // } | 52 // } |
| 53 static scoped_ptr<EventListener> ForExtension( | 53 static std::unique_ptr<EventListener> ForExtension( |
| 54 const std::string& event_name, | 54 const std::string& event_name, |
| 55 const std::string& extension_id, | 55 const std::string& extension_id, |
| 56 content::RenderProcessHost* process, | 56 content::RenderProcessHost* process, |
| 57 scoped_ptr<base::DictionaryValue> filter); | 57 std::unique_ptr<base::DictionaryValue> filter); |
| 58 static scoped_ptr<EventListener> ForURL( | 58 static std::unique_ptr<EventListener> ForURL( |
| 59 const std::string& event_name, | 59 const std::string& event_name, |
| 60 const GURL& listener_url, | 60 const GURL& listener_url, |
| 61 content::RenderProcessHost* process, | 61 content::RenderProcessHost* process, |
| 62 scoped_ptr<base::DictionaryValue> filter); | 62 std::unique_ptr<base::DictionaryValue> filter); |
| 63 | 63 |
| 64 ~EventListener(); | 64 ~EventListener(); |
| 65 | 65 |
| 66 bool Equals(const EventListener* other) const; | 66 bool Equals(const EventListener* other) const; |
| 67 | 67 |
| 68 scoped_ptr<EventListener> Copy() const; | 68 std::unique_ptr<EventListener> Copy() const; |
| 69 | 69 |
| 70 // Returns true in the case of a lazy background page, and thus no process. | 70 // Returns true in the case of a lazy background page, and thus no process. |
| 71 bool IsLazy() const; | 71 bool IsLazy() const; |
| 72 | 72 |
| 73 // Modifies this listener to be a lazy listener, clearing process references. | 73 // Modifies this listener to be a lazy listener, clearing process references. |
| 74 void MakeLazy(); | 74 void MakeLazy(); |
| 75 | 75 |
| 76 // Returns the browser context associated with the listener, or NULL if | 76 // Returns the browser context associated with the listener, or NULL if |
| 77 // IsLazy. | 77 // IsLazy. |
| 78 content::BrowserContext* GetBrowserContext() const; | 78 content::BrowserContext* GetBrowserContext() const; |
| 79 | 79 |
| 80 const std::string& event_name() const { return event_name_; } | 80 const std::string& event_name() const { return event_name_; } |
| 81 const std::string& extension_id() const { return extension_id_; } | 81 const std::string& extension_id() const { return extension_id_; } |
| 82 const GURL& listener_url() const { return listener_url_; } | 82 const GURL& listener_url() const { return listener_url_; } |
| 83 content::RenderProcessHost* process() const { return process_; } | 83 content::RenderProcessHost* process() const { return process_; } |
| 84 base::DictionaryValue* filter() const { return filter_.get(); } | 84 base::DictionaryValue* filter() const { return filter_.get(); } |
| 85 EventFilter::MatcherID matcher_id() const { return matcher_id_; } | 85 EventFilter::MatcherID matcher_id() const { return matcher_id_; } |
| 86 void set_matcher_id(EventFilter::MatcherID id) { matcher_id_ = id; } | 86 void set_matcher_id(EventFilter::MatcherID id) { matcher_id_ = id; } |
| 87 | 87 |
| 88 private: | 88 private: |
| 89 EventListener(const std::string& event_name, | 89 EventListener(const std::string& event_name, |
| 90 const std::string& extension_id, | 90 const std::string& extension_id, |
| 91 const GURL& listener_url, | 91 const GURL& listener_url, |
| 92 content::RenderProcessHost* process, | 92 content::RenderProcessHost* process, |
| 93 scoped_ptr<base::DictionaryValue> filter); | 93 std::unique_ptr<base::DictionaryValue> filter); |
| 94 | 94 |
| 95 const std::string event_name_; | 95 const std::string event_name_; |
| 96 const std::string extension_id_; | 96 const std::string extension_id_; |
| 97 const GURL listener_url_; | 97 const GURL listener_url_; |
| 98 content::RenderProcessHost* process_; | 98 content::RenderProcessHost* process_; |
| 99 scoped_ptr<base::DictionaryValue> filter_; | 99 std::unique_ptr<base::DictionaryValue> filter_; |
| 100 EventFilter::MatcherID matcher_id_; // -1 if unset. | 100 EventFilter::MatcherID matcher_id_; // -1 if unset. |
| 101 | 101 |
| 102 DISALLOW_COPY_AND_ASSIGN(EventListener); | 102 DISALLOW_COPY_AND_ASSIGN(EventListener); |
| 103 }; | 103 }; |
| 104 | 104 |
| 105 // Holds listeners for extension events and can answer questions about which | 105 // Holds listeners for extension events and can answer questions about which |
| 106 // listeners are interested in what events. | 106 // listeners are interested in what events. |
| 107 class EventListenerMap { | 107 class EventListenerMap { |
| 108 public: | 108 public: |
| 109 typedef std::vector<linked_ptr<EventListener> > ListenerList; | 109 typedef std::vector<linked_ptr<EventListener> > ListenerList; |
| 110 | 110 |
| 111 class Delegate { | 111 class Delegate { |
| 112 public: | 112 public: |
| 113 virtual ~Delegate() {} | 113 virtual ~Delegate() {} |
| 114 virtual void OnListenerAdded(const EventListener* listener) = 0; | 114 virtual void OnListenerAdded(const EventListener* listener) = 0; |
| 115 virtual void OnListenerRemoved(const EventListener* listener) = 0; | 115 virtual void OnListenerRemoved(const EventListener* listener) = 0; |
| 116 }; | 116 }; |
| 117 | 117 |
| 118 explicit EventListenerMap(Delegate* delegate); | 118 explicit EventListenerMap(Delegate* delegate); |
| 119 ~EventListenerMap(); | 119 ~EventListenerMap(); |
| 120 | 120 |
| 121 // Add a listener for a particular event. GetEventListeners() will include a | 121 // Add a listener for a particular event. GetEventListeners() will include a |
| 122 // weak pointer to |listener| in its results if passed a relevant | 122 // weak pointer to |listener| in its results if passed a relevant |
| 123 // extensions::Event. | 123 // extensions::Event. |
| 124 // Returns true if the listener was added (in the case that it has never been | 124 // Returns true if the listener was added (in the case that it has never been |
| 125 // seen before). | 125 // seen before). |
| 126 bool AddListener(scoped_ptr<EventListener> listener); | 126 bool AddListener(std::unique_ptr<EventListener> listener); |
| 127 | 127 |
| 128 // Remove a listener that .Equals() |listener|. | 128 // Remove a listener that .Equals() |listener|. |
| 129 // Returns true if the listener was removed . | 129 // Returns true if the listener was removed . |
| 130 bool RemoveListener(const EventListener* listener); | 130 bool RemoveListener(const EventListener* listener); |
| 131 | 131 |
| 132 // Returns the set of listeners that want to be notified of |event|. | 132 // Returns the set of listeners that want to be notified of |event|. |
| 133 std::set<const EventListener*> GetEventListeners(const Event& event); | 133 std::set<const EventListener*> GetEventListeners(const Event& event); |
| 134 | 134 |
| 135 const ListenerList& GetEventListenersByName(const std::string& event_name) { | 135 const ListenerList& GetEventListenersByName(const std::string& event_name) { |
| 136 return listeners_[event_name]; | 136 return listeners_[event_name]; |
| (...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 171 void LoadFilteredLazyListeners( | 171 void LoadFilteredLazyListeners( |
| 172 const std::string& extension_id, | 172 const std::string& extension_id, |
| 173 const base::DictionaryValue& filtered); | 173 const base::DictionaryValue& filtered); |
| 174 | 174 |
| 175 private: | 175 private: |
| 176 // The key here is an event name. | 176 // The key here is an event name. |
| 177 typedef std::map<std::string, ListenerList> ListenerMap; | 177 typedef std::map<std::string, ListenerList> ListenerMap; |
| 178 | 178 |
| 179 void CleanupListener(EventListener* listener); | 179 void CleanupListener(EventListener* listener); |
| 180 bool IsFilteredEvent(const Event& event) const; | 180 bool IsFilteredEvent(const Event& event) const; |
| 181 scoped_ptr<EventMatcher> ParseEventMatcher( | 181 std::unique_ptr<EventMatcher> ParseEventMatcher( |
| 182 base::DictionaryValue* filter_dict); | 182 base::DictionaryValue* filter_dict); |
| 183 | 183 |
| 184 // Listens for removals from this map. | 184 // Listens for removals from this map. |
| 185 Delegate* delegate_; | 185 Delegate* delegate_; |
| 186 | 186 |
| 187 std::set<std::string> filtered_events_; | 187 std::set<std::string> filtered_events_; |
| 188 ListenerMap listeners_; | 188 ListenerMap listeners_; |
| 189 | 189 |
| 190 std::map<EventFilter::MatcherID, EventListener*> listeners_by_matcher_id_; | 190 std::map<EventFilter::MatcherID, EventListener*> listeners_by_matcher_id_; |
| 191 | 191 |
| 192 EventFilter event_filter_; | 192 EventFilter event_filter_; |
| 193 | 193 |
| 194 DISALLOW_COPY_AND_ASSIGN(EventListenerMap); | 194 DISALLOW_COPY_AND_ASSIGN(EventListenerMap); |
| 195 }; | 195 }; |
| 196 | 196 |
| 197 } // namespace extensions | 197 } // namespace extensions |
| 198 | 198 |
| 199 #endif // EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ | 199 #endif // EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ |
| OLD | NEW |