| 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 <memory> |
| 10 #include <set> | 10 #include <set> |
| 11 #include <string> | 11 #include <string> |
| 12 #include <vector> | 12 #include <vector> |
| 13 | 13 |
| 14 #include "base/macros.h" | 14 #include "base/macros.h" |
| 15 #include "extensions/common/event_filter.h" | 15 #include "extensions/common/event_filter.h" |
| 16 #include "extensions/common/extension_id.h" |
| 16 #include "url/gurl.h" | 17 #include "url/gurl.h" |
| 17 | 18 |
| 18 namespace base { | 19 namespace base { |
| 19 class DictionaryValue; | 20 class DictionaryValue; |
| 20 } | 21 } |
| 21 | 22 |
| 22 namespace content { | 23 namespace content { |
| 23 class BrowserContext; | 24 class BrowserContext; |
| 24 class RenderProcessHost; | 25 class RenderProcessHost; |
| 25 } | 26 } |
| (...skipping 25 matching lines...) Expand all Loading... |
| 51 static std::unique_ptr<EventListener> ForExtension( | 52 static std::unique_ptr<EventListener> ForExtension( |
| 52 const std::string& event_name, | 53 const std::string& event_name, |
| 53 const std::string& extension_id, | 54 const std::string& extension_id, |
| 54 content::RenderProcessHost* process, | 55 content::RenderProcessHost* process, |
| 55 std::unique_ptr<base::DictionaryValue> filter); | 56 std::unique_ptr<base::DictionaryValue> filter); |
| 56 static std::unique_ptr<EventListener> ForURL( | 57 static std::unique_ptr<EventListener> ForURL( |
| 57 const std::string& event_name, | 58 const std::string& event_name, |
| 58 const GURL& listener_url, | 59 const GURL& listener_url, |
| 59 content::RenderProcessHost* process, | 60 content::RenderProcessHost* process, |
| 60 std::unique_ptr<base::DictionaryValue> filter); | 61 std::unique_ptr<base::DictionaryValue> filter); |
| 62 // Constructs EventListener for an Extension service worker. |
| 63 // Similar to ForExtension above with the only difference that |
| 64 // |worker_thread_id_| contains a valid worker thread, as opposed to |
| 65 // kNonWorkerThreadId. |
| 66 static std::unique_ptr<EventListener> ForExtensionServiceWorker( |
| 67 const std::string& event_name, |
| 68 const std::string& extension_id, |
| 69 content::RenderProcessHost* process, |
| 70 int worker_thread_id, |
| 71 std::unique_ptr<base::DictionaryValue> filter); |
| 61 | 72 |
| 62 ~EventListener(); | 73 ~EventListener(); |
| 63 | 74 |
| 64 bool Equals(const EventListener* other) const; | 75 bool Equals(const EventListener* other) const; |
| 65 | 76 |
| 66 std::unique_ptr<EventListener> Copy() const; | 77 std::unique_ptr<EventListener> Copy() const; |
| 67 | 78 |
| 68 // Returns true in the case of a lazy background page, and thus no process. | 79 // Returns true if the listener is for a lazy context: e.g. a background page |
| 80 // or an extension service worker. This listener does not have |process_|. |
| 69 bool IsLazy() const; | 81 bool IsLazy() const; |
| 70 | 82 |
| 83 // Returns true if this listener was registered for an extension service |
| 84 // worker. |
| 85 bool IsForServiceWorker() const; |
| 86 |
| 71 // Modifies this listener to be a lazy listener, clearing process references. | 87 // Modifies this listener to be a lazy listener, clearing process references. |
| 72 void MakeLazy(); | 88 void MakeLazy(); |
| 73 | 89 |
| 74 // Returns the browser context associated with the listener, or NULL if | 90 // Returns the browser context associated with the listener, or NULL if |
| 75 // IsLazy. | 91 // IsLazy. |
| 76 content::BrowserContext* GetBrowserContext() const; | 92 content::BrowserContext* GetBrowserContext() const; |
| 77 | 93 |
| 78 const std::string& event_name() const { return event_name_; } | 94 const std::string& event_name() const { return event_name_; } |
| 79 const std::string& extension_id() const { return extension_id_; } | 95 const std::string& extension_id() const { return extension_id_; } |
| 80 const GURL& listener_url() const { return listener_url_; } | 96 const GURL& listener_url() const { return listener_url_; } |
| 81 content::RenderProcessHost* process() const { return process_; } | 97 content::RenderProcessHost* process() const { return process_; } |
| 82 base::DictionaryValue* filter() const { return filter_.get(); } | 98 base::DictionaryValue* filter() const { return filter_.get(); } |
| 83 EventFilter::MatcherID matcher_id() const { return matcher_id_; } | 99 EventFilter::MatcherID matcher_id() const { return matcher_id_; } |
| 84 void set_matcher_id(EventFilter::MatcherID id) { matcher_id_ = id; } | 100 void set_matcher_id(EventFilter::MatcherID id) { matcher_id_ = id; } |
| 101 int worker_thread_id() const { return worker_thread_id_; } |
| 85 | 102 |
| 86 private: | 103 private: |
| 87 EventListener(const std::string& event_name, | 104 EventListener(const std::string& event_name, |
| 88 const std::string& extension_id, | 105 const std::string& extension_id, |
| 89 const GURL& listener_url, | 106 const GURL& listener_url, |
| 90 content::RenderProcessHost* process, | 107 content::RenderProcessHost* process, |
| 108 int worker_thread_id, |
| 91 std::unique_ptr<base::DictionaryValue> filter); | 109 std::unique_ptr<base::DictionaryValue> filter); |
| 92 | 110 |
| 93 const std::string event_name_; | 111 const std::string event_name_; |
| 94 const std::string extension_id_; | 112 const std::string extension_id_; |
| 95 const GURL listener_url_; | 113 const GURL listener_url_; |
| 96 content::RenderProcessHost* process_; | 114 content::RenderProcessHost* process_; |
| 115 const int worker_thread_id_; |
| 97 std::unique_ptr<base::DictionaryValue> filter_; | 116 std::unique_ptr<base::DictionaryValue> filter_; |
| 98 EventFilter::MatcherID matcher_id_; // -1 if unset. | 117 EventFilter::MatcherID matcher_id_; // -1 if unset. |
| 99 | 118 |
| 100 DISALLOW_COPY_AND_ASSIGN(EventListener); | 119 DISALLOW_COPY_AND_ASSIGN(EventListener); |
| 101 }; | 120 }; |
| 102 | 121 |
| 103 // Holds listeners for extension events and can answer questions about which | 122 // Holds listeners for extension events and can answer questions about which |
| 104 // listeners are interested in what events. | 123 // listeners are interested in what events. |
| 105 class EventListenerMap { | 124 class EventListenerMap { |
| 106 public: | 125 public: |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 143 // Returns true if there are any listeners on |event_name| from | 162 // Returns true if there are any listeners on |event_name| from |
| 144 // |extension_id|. | 163 // |extension_id|. |
| 145 bool HasListenerForExtension(const std::string& extension_id, | 164 bool HasListenerForExtension(const std::string& extension_id, |
| 146 const std::string& event_name); | 165 const std::string& event_name); |
| 147 | 166 |
| 148 // Returns true if this map contains an EventListener that .Equals() | 167 // Returns true if this map contains an EventListener that .Equals() |
| 149 // |listener|. | 168 // |listener|. |
| 150 bool HasListener(const EventListener* listener); | 169 bool HasListener(const EventListener* listener); |
| 151 | 170 |
| 152 // Returns true if there is a listener for |extension_id| in |process|. | 171 // Returns true if there is a listener for |extension_id| in |process|. |
| 172 // |worker_thread_id| is the thread id of the service worker the listener is |
| 173 // for, or kNonWorkerThreadId if the listener is not for a service worker. |
| 153 bool HasProcessListener(content::RenderProcessHost* process, | 174 bool HasProcessListener(content::RenderProcessHost* process, |
| 175 int worker_thread_id, |
| 154 const std::string& extension_id); | 176 const std::string& extension_id); |
| 155 | 177 |
| 156 // Removes any listeners that |extension_id| has added, both lazy and regular. | 178 // Removes any listeners that |extension_id| has added, both lazy and regular. |
| 157 void RemoveListenersForExtension(const std::string& extension_id); | 179 void RemoveListenersForExtension(const std::string& extension_id); |
| 158 | 180 |
| 159 // Adds unfiltered lazy listeners as described their serialised descriptions. | 181 // Adds unfiltered lazy listeners as described their serialised descriptions. |
| 160 // |event_names| the names of the lazy events. | 182 // |event_names| the names of the lazy events. |
| 161 // Note that we can only load lazy listeners in this fashion, because there | 183 // Note that we can only load lazy listeners in this fashion, because there |
| 162 // is no way to serialise a RenderProcessHost*. | 184 // is no way to serialise a RenderProcessHost*. |
| 163 void LoadUnfilteredLazyListeners(const std::string& extension_id, | 185 void LoadUnfilteredLazyListeners(const std::string& extension_id, |
| (...skipping 24 matching lines...) Expand all Loading... |
| 188 std::map<EventFilter::MatcherID, EventListener*> listeners_by_matcher_id_; | 210 std::map<EventFilter::MatcherID, EventListener*> listeners_by_matcher_id_; |
| 189 | 211 |
| 190 EventFilter event_filter_; | 212 EventFilter event_filter_; |
| 191 | 213 |
| 192 DISALLOW_COPY_AND_ASSIGN(EventListenerMap); | 214 DISALLOW_COPY_AND_ASSIGN(EventListenerMap); |
| 193 }; | 215 }; |
| 194 | 216 |
| 195 } // namespace extensions | 217 } // namespace extensions |
| 196 | 218 |
| 197 #endif // EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ | 219 #endif // EXTENSIONS_BROWSER_EVENT_LISTENER_MAP_H_ |
| OLD | NEW |