OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 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_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ | 5 #ifndef EXTENSIONS_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ |
6 #define EXTENSIONS_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ | 6 #define EXTENSIONS_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ |
7 | 7 |
8 #include <string> | 8 #include <string> |
9 #include <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
12 #include "base/macros.h" | 12 #include "base/macros.h" |
13 #include "extensions/common/value_counter.h" | 13 #include "extensions/common/value_counter.h" |
14 #include "extensions/renderer/bindings/api_binding_types.h" | 14 #include "extensions/renderer/bindings/api_binding_types.h" |
15 #include "v8/include/v8.h" | 15 #include "v8/include/v8.h" |
16 | 16 |
17 namespace base { | 17 namespace base { |
18 class DictionaryValue; | 18 class DictionaryValue; |
19 } | 19 } |
20 | 20 |
21 namespace extensions { | 21 namespace extensions { |
22 class EventFilter; | 22 class EventFilter; |
23 struct EventFilteringInfo; | 23 struct EventFilteringInfo; |
24 | 24 |
25 // A base class to hold listeners for a given event. This allows for adding, | 25 // A base class to hold listeners for a given event. This allows for adding, |
26 // removing, and querying listeners in the list, and calling a callback when | 26 // removing, and querying listeners in the list, and calling a callback when |
27 // transitioning from 0 -> 1 or 1 -> 0 listeners. | 27 // transitioning from 0 -> 1 or 1 -> 0 listeners. |
28 class APIEventListeners { | 28 class APIEventListeners { |
29 public: | 29 public: |
30 // The callback called when listeners change. |was_manual| indicates that the | 30 // The callback called when listeners change. |update_lazy_listeners| |
31 // listener change was triggered by a direct call from the extension to add | 31 // indicates that the lazy listener count for the event should potentially be |
32 // or remove listeners, rather than something like the context being | 32 // updated. This is true if a) the event supports lazy listeners and b) the |
33 // destroyed. | 33 // change was "manual" (i.e., triggered by a direct call from the extension |
| 34 // rather than something like the context being destroyed). |
34 using ListenersUpdated = | 35 using ListenersUpdated = |
35 base::Callback<void(binding::EventListenersChanged, | 36 base::Callback<void(binding::EventListenersChanged, |
36 const base::DictionaryValue* filter, | 37 const base::DictionaryValue* filter, |
37 bool was_manual, | 38 bool update_lazy_listeners, |
38 v8::Local<v8::Context> context)>; | 39 v8::Local<v8::Context> context)>; |
39 | 40 |
40 virtual ~APIEventListeners() = default; | 41 virtual ~APIEventListeners() = default; |
41 | 42 |
42 // Adds the given |listener| to the list, possibly associating it with the | 43 // Adds the given |listener| to the list, possibly associating it with the |
43 // given |filter|. Returns true if the listener is added. Populates |error| | 44 // given |filter|. Returns true if the listener is added. Populates |error| |
44 // with any errors encountered. Note that |error| is *not* always populated | 45 // with any errors encountered. Note that |error| is *not* always populated |
45 // if false is returned, since we don't consider trying to re-add a listener | 46 // if false is returned, since we don't consider trying to re-add a listener |
46 // to be an error. | 47 // to be an error. |
47 virtual bool AddListener(v8::Local<v8::Function> listener, | 48 virtual bool AddListener(v8::Local<v8::Function> listener, |
(...skipping 24 matching lines...) Expand all Loading... |
72 | 73 |
73 private: | 74 private: |
74 DISALLOW_COPY_AND_ASSIGN(APIEventListeners); | 75 DISALLOW_COPY_AND_ASSIGN(APIEventListeners); |
75 }; | 76 }; |
76 | 77 |
77 // A listener list implementation that doesn't support filtering. Each event | 78 // A listener list implementation that doesn't support filtering. Each event |
78 // dispatched is dispatched to all the associated listeners. | 79 // dispatched is dispatched to all the associated listeners. |
79 class UnfilteredEventListeners final : public APIEventListeners { | 80 class UnfilteredEventListeners final : public APIEventListeners { |
80 public: | 81 public: |
81 UnfilteredEventListeners(const ListenersUpdated& listeners_updated, | 82 UnfilteredEventListeners(const ListenersUpdated& listeners_updated, |
82 int max_listeners); | 83 int max_listeners, |
| 84 bool supports_lazy_listeners); |
83 ~UnfilteredEventListeners() override; | 85 ~UnfilteredEventListeners() override; |
84 | 86 |
85 bool AddListener(v8::Local<v8::Function> listener, | 87 bool AddListener(v8::Local<v8::Function> listener, |
86 v8::Local<v8::Object> filter, | 88 v8::Local<v8::Object> filter, |
87 v8::Local<v8::Context> context, | 89 v8::Local<v8::Context> context, |
88 std::string* error) override; | 90 std::string* error) override; |
89 void RemoveListener(v8::Local<v8::Function> listener, | 91 void RemoveListener(v8::Local<v8::Function> listener, |
90 v8::Local<v8::Context> context) override; | 92 v8::Local<v8::Context> context) override; |
91 bool HasListener(v8::Local<v8::Function> listener) override; | 93 bool HasListener(v8::Local<v8::Function> listener) override; |
92 size_t GetNumListeners() override; | 94 size_t GetNumListeners() override; |
(...skipping 11 matching lines...) Expand all Loading... |
104 // Invalidate()'d earlier can leak until context destruction. We could | 106 // Invalidate()'d earlier can leak until context destruction. We could |
105 // circumvent this by storing the listeners strongly in a private propery | 107 // circumvent this by storing the listeners strongly in a private propery |
106 // (thus traceable by v8), and optionally keep a weak cache on this object. | 108 // (thus traceable by v8), and optionally keep a weak cache on this object. |
107 std::vector<v8::Global<v8::Function>> listeners_; | 109 std::vector<v8::Global<v8::Function>> listeners_; |
108 | 110 |
109 ListenersUpdated listeners_updated_; | 111 ListenersUpdated listeners_updated_; |
110 | 112 |
111 // The maximum number of supported listeners. | 113 // The maximum number of supported listeners. |
112 int max_listeners_; | 114 int max_listeners_; |
113 | 115 |
| 116 // Whether the event supports lazy listeners. |
| 117 bool supports_lazy_listeners_; |
| 118 |
114 DISALLOW_COPY_AND_ASSIGN(UnfilteredEventListeners); | 119 DISALLOW_COPY_AND_ASSIGN(UnfilteredEventListeners); |
115 }; | 120 }; |
116 | 121 |
117 // A listener list implementation that supports filtering. Events should only | 122 // A listener list implementation that supports filtering. Events should only |
118 // be dispatched to those listeners whose filters match. Additionally, the | 123 // be dispatched to those listeners whose filters match. Additionally, the |
119 // updated callback is triggered any time a listener with a new filter is | 124 // updated callback is triggered any time a listener with a new filter is |
120 // added, or the last listener with a given filter is removed. | 125 // added, or the last listener with a given filter is removed. |
121 class FilteredEventListeners final : public APIEventListeners { | 126 class FilteredEventListeners final : public APIEventListeners { |
122 public: | 127 public: |
123 FilteredEventListeners(const ListenersUpdated& listeners_updated, | 128 FilteredEventListeners(const ListenersUpdated& listeners_updated, |
124 const std::string& event_name, | 129 const std::string& event_name, |
125 int max_listeners, | 130 int max_listeners, |
| 131 bool supports_lazy_listeners, |
126 EventFilter* event_filter); | 132 EventFilter* event_filter); |
127 ~FilteredEventListeners() override; | 133 ~FilteredEventListeners() override; |
128 | 134 |
129 bool AddListener(v8::Local<v8::Function> listener, | 135 bool AddListener(v8::Local<v8::Function> listener, |
130 v8::Local<v8::Object> filter, | 136 v8::Local<v8::Object> filter, |
131 v8::Local<v8::Context> context, | 137 v8::Local<v8::Context> context, |
132 std::string* error) override; | 138 std::string* error) override; |
133 void RemoveListener(v8::Local<v8::Function> listener, | 139 void RemoveListener(v8::Local<v8::Function> listener, |
134 v8::Local<v8::Context> context) override; | 140 v8::Local<v8::Context> context) override; |
135 bool HasListener(v8::Local<v8::Function> listener) override; | 141 bool HasListener(v8::Local<v8::Function> listener) override; |
(...skipping 13 matching lines...) Expand all Loading... |
149 // Note: See TODO on UnfilteredEventListeners::listeners_. | 155 // Note: See TODO on UnfilteredEventListeners::listeners_. |
150 std::vector<ListenerData> listeners_; | 156 std::vector<ListenerData> listeners_; |
151 | 157 |
152 ListenersUpdated listeners_updated_; | 158 ListenersUpdated listeners_updated_; |
153 | 159 |
154 std::string event_name_; | 160 std::string event_name_; |
155 | 161 |
156 // The maximum number of supported listeners. | 162 // The maximum number of supported listeners. |
157 int max_listeners_; | 163 int max_listeners_; |
158 | 164 |
| 165 // Whether the event supports lazy listeners. |
| 166 bool supports_lazy_listeners_; |
| 167 |
159 // The associated EventFilter; required to outlive this object. | 168 // The associated EventFilter; required to outlive this object. |
160 EventFilter* event_filter_ = nullptr; | 169 EventFilter* event_filter_ = nullptr; |
161 | 170 |
162 ValueCounter value_counter_; | 171 ValueCounter value_counter_; |
163 | 172 |
164 DISALLOW_COPY_AND_ASSIGN(FilteredEventListeners); | 173 DISALLOW_COPY_AND_ASSIGN(FilteredEventListeners); |
165 }; | 174 }; |
166 | 175 |
167 } // namespace extensions | 176 } // namespace extensions |
168 | 177 |
169 #endif // EXTENSIONS_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ | 178 #endif // EXTENSIONS_RENDERER_BINDINGS_API_EVENT_LISTENERS_H_ |
OLD | NEW |