Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(85)

Side by Side Diff: extensions/renderer/event_emitter.h

Issue 2768093002: [Reland][Extensions Bindings] Add support for filtered events (Closed)
Patch Set: Fix Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « extensions/renderer/dispatcher.cc ('k') | extensions/renderer/event_emitter.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 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_EVENT_EMITTER_H_ 5 #ifndef EXTENSIONS_RENDERER_EVENT_EMITTER_H_
6 #define EXTENSIONS_RENDERER_EVENT_EMITTER_H_ 6 #define EXTENSIONS_RENDERER_EVENT_EMITTER_H_
7 7
8 #include <vector> 8 #include <vector>
9 9
10 #include "extensions/renderer/api_binding_types.h" 10 #include "extensions/renderer/api_binding_types.h"
11 #include "gin/wrappable.h" 11 #include "gin/wrappable.h"
12 #include "v8/include/v8.h" 12 #include "v8/include/v8.h"
13 13
14 namespace gin { 14 namespace gin {
15 class Arguments; 15 class Arguments;
16 } 16 }
17 17
18 namespace extensions { 18 namespace extensions {
19 class APIEventListeners;
20 class EventFilteringInfo;
19 21
20 // A gin::Wrappable Event object. One is expected to be created per event, per 22 // A gin::Wrappable Event object. One is expected to be created per event, per
21 // context. Note: this object *does not* clear any events, so it must be 23 // context. Note: this object *does not* clear any events, so it must be
22 // destroyed with the context to avoid leaking. 24 // destroyed with the context to avoid leaking.
23 class EventEmitter final : public gin::Wrappable<EventEmitter> { 25 class EventEmitter final : public gin::Wrappable<EventEmitter> {
24 public: 26 public:
25 using Listeners = std::vector<v8::Global<v8::Function>>; 27 EventEmitter(bool supports_filters,
26 using ListenersChangedMethod = 28 std::unique_ptr<APIEventListeners> listeners,
27 base::Callback<void(binding::EventListenersChanged, 29 const binding::RunJSFunction& run_js);
28 v8::Local<v8::Context>)>;
29
30 EventEmitter(const binding::RunJSFunction& run_js,
31 const ListenersChangedMethod& listeners_changed);
32 ~EventEmitter() override; 30 ~EventEmitter() override;
33 31
34 static gin::WrapperInfo kWrapperInfo; 32 static gin::WrapperInfo kWrapperInfo;
35 33
36 // gin::Wrappable: 34 // gin::Wrappable:
37 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( 35 gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
38 v8::Isolate* isolate) final; 36 v8::Isolate* isolate) final;
39 37
40 void Fire(v8::Local<v8::Context> context, 38 void Fire(v8::Local<v8::Context> context,
41 std::vector<v8::Local<v8::Value>>* args); 39 std::vector<v8::Local<v8::Value>>* args,
40 const EventFilteringInfo* filter);
42 41
43 // Removes all listeners and marks this object as invalid so that no more 42 // Removes all listeners and marks this object as invalid so that no more
44 // are added. 43 // are added.
45 void Invalidate(); 44 void Invalidate(v8::Local<v8::Context> context);
46 45
47 const Listeners* listeners() const { return &listeners_; } 46 size_t GetNumListeners() const;
48 47
49 private: 48 private:
50 // Bound methods for the Event JS object. 49 // Bound methods for the Event JS object.
51 void AddListener(gin::Arguments* arguments); 50 void AddListener(gin::Arguments* arguments);
52 void RemoveListener(gin::Arguments* arguments); 51 void RemoveListener(gin::Arguments* arguments);
53 bool HasListener(v8::Local<v8::Function> function); 52 bool HasListener(v8::Local<v8::Function> function);
54 bool HasListeners(); 53 bool HasListeners();
55 void Dispatch(gin::Arguments* arguments); 54 void Dispatch(gin::Arguments* arguments);
56 55
57 // Whether or not this object is still valid; false upon context release. 56 // Whether or not this object is still valid; false upon context release.
58 // When invalid, no listeners can be added or removed. 57 // When invalid, no listeners can be added or removed.
59 bool valid_ = true; 58 bool valid_ = true;
60 59
61 // The event listeners associated with this event. 60 // Whether the event supports filters.
62 // TODO(devlin): Having these listeners held as v8::Globals means that we 61 bool supports_filters_ = false;
63 // need to worry about cycles when a listener holds a reference to the event, 62
64 // e.g. EventEmitter -> Listener -> EventEmitter. Right now, we handle that by 63 std::unique_ptr<APIEventListeners> listeners_;
65 // requiring Invalidate() to be called, but that means that events that aren't
66 // Invalidate()'d earlier can leak until context destruction. We could
67 // circumvent this by storing the listeners strongly in a private propery
68 // (thus traceable by v8), and optionally keep a weak cache on this object.
69 Listeners listeners_;
70 64
71 binding::RunJSFunction run_js_; 65 binding::RunJSFunction run_js_;
72 66
73 ListenersChangedMethod listeners_changed_;
74
75 DISALLOW_COPY_AND_ASSIGN(EventEmitter); 67 DISALLOW_COPY_AND_ASSIGN(EventEmitter);
76 }; 68 };
77 69
78 } // namespace extensions 70 } // namespace extensions
79 71
80 #endif // EXTENSIONS_RENDERER_EVENT_EMITTER_H_ 72 #endif // EXTENSIONS_RENDERER_EVENT_EMITTER_H_
OLDNEW
« no previous file with comments | « extensions/renderer/dispatcher.cc ('k') | extensions/renderer/event_emitter.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698