| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef EXTENSIONS_RENDERER_EVENT_EMITTER_H_ | |
| 6 #define EXTENSIONS_RENDERER_EVENT_EMITTER_H_ | |
| 7 | |
| 8 #include <vector> | |
| 9 | |
| 10 #include "extensions/renderer/api_binding_types.h" | |
| 11 #include "gin/wrappable.h" | |
| 12 #include "v8/include/v8.h" | |
| 13 | |
| 14 namespace gin { | |
| 15 class Arguments; | |
| 16 } | |
| 17 | |
| 18 namespace extensions { | |
| 19 class APIEventListeners; | |
| 20 struct EventFilteringInfo; | |
| 21 | |
| 22 // A gin::Wrappable Event object. One is expected to be created per event, per | |
| 23 // context. Note: this object *does not* clear any events, so it must be | |
| 24 // destroyed with the context to avoid leaking. | |
| 25 class EventEmitter final : public gin::Wrappable<EventEmitter> { | |
| 26 public: | |
| 27 EventEmitter(bool supports_filters, | |
| 28 std::unique_ptr<APIEventListeners> listeners, | |
| 29 const binding::RunJSFunction& run_js, | |
| 30 const binding::RunJSFunctionSync& run_js_sync); | |
| 31 ~EventEmitter() override; | |
| 32 | |
| 33 static gin::WrapperInfo kWrapperInfo; | |
| 34 | |
| 35 // gin::Wrappable: | |
| 36 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | |
| 37 v8::Isolate* isolate) final; | |
| 38 | |
| 39 void Fire(v8::Local<v8::Context> context, | |
| 40 std::vector<v8::Local<v8::Value>>* args, | |
| 41 const EventFilteringInfo* filter); | |
| 42 | |
| 43 // Removes all listeners and marks this object as invalid so that no more | |
| 44 // are added. | |
| 45 void Invalidate(v8::Local<v8::Context> context); | |
| 46 | |
| 47 // TODO(devlin): Consider making this a test-only method and exposing | |
| 48 // HasListeners() instead. | |
| 49 size_t GetNumListeners() const; | |
| 50 | |
| 51 private: | |
| 52 // Bound methods for the Event JS object. | |
| 53 void AddListener(gin::Arguments* arguments); | |
| 54 void RemoveListener(gin::Arguments* arguments); | |
| 55 bool HasListener(v8::Local<v8::Function> function); | |
| 56 bool HasListeners(); | |
| 57 void Dispatch(gin::Arguments* arguments); | |
| 58 | |
| 59 // Notifies the listeners of an event with the given |args|. If |run_sync| is | |
| 60 // true, runs JS synchronously and populates |out_values| with the results of | |
| 61 // the listeners. | |
| 62 void DispatchImpl(v8::Local<v8::Context> context, | |
| 63 std::vector<v8::Local<v8::Value>>* args, | |
| 64 const EventFilteringInfo* filter, | |
| 65 bool run_sync, | |
| 66 std::vector<v8::Global<v8::Value>>* out_values); | |
| 67 | |
| 68 // Whether or not this object is still valid; false upon context release. | |
| 69 // When invalid, no listeners can be added or removed. | |
| 70 bool valid_ = true; | |
| 71 | |
| 72 // Whether the event supports filters. | |
| 73 bool supports_filters_ = false; | |
| 74 | |
| 75 std::unique_ptr<APIEventListeners> listeners_; | |
| 76 | |
| 77 binding::RunJSFunction run_js_; | |
| 78 binding::RunJSFunctionSync run_js_sync_; | |
| 79 | |
| 80 DISALLOW_COPY_AND_ASSIGN(EventEmitter); | |
| 81 }; | |
| 82 | |
| 83 } // namespace extensions | |
| 84 | |
| 85 #endif // EXTENSIONS_RENDERER_EVENT_EMITTER_H_ | |
| OLD | NEW |