| OLD | NEW |
| 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; | 19 class APIEventListeners; |
| 20 class EventFilteringInfo; | 20 class EventFilteringInfo; |
| 21 | 21 |
| 22 // 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 |
| 23 // 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 |
| 24 // destroyed with the context to avoid leaking. | 24 // destroyed with the context to avoid leaking. |
| 25 class EventEmitter final : public gin::Wrappable<EventEmitter> { | 25 class EventEmitter final : public gin::Wrappable<EventEmitter> { |
| 26 public: | 26 public: |
| 27 EventEmitter(bool supports_filters, | 27 EventEmitter(bool supports_filters, |
| 28 std::unique_ptr<APIEventListeners> listeners, | 28 std::unique_ptr<APIEventListeners> listeners, |
| 29 const binding::RunJSFunction& run_js); | 29 const binding::RunJSFunction& run_js, |
| 30 const binding::RunJSFunctionSync& run_js_sync); |
| 30 ~EventEmitter() override; | 31 ~EventEmitter() override; |
| 31 | 32 |
| 32 static gin::WrapperInfo kWrapperInfo; | 33 static gin::WrapperInfo kWrapperInfo; |
| 33 | 34 |
| 34 // gin::Wrappable: | 35 // gin::Wrappable: |
| 35 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( | 36 gin::ObjectTemplateBuilder GetObjectTemplateBuilder( |
| 36 v8::Isolate* isolate) final; | 37 v8::Isolate* isolate) final; |
| 37 | 38 |
| 38 void Fire(v8::Local<v8::Context> context, | 39 void Fire(v8::Local<v8::Context> context, |
| 39 std::vector<v8::Local<v8::Value>>* args, | 40 std::vector<v8::Local<v8::Value>>* args, |
| 40 const EventFilteringInfo* filter); | 41 const EventFilteringInfo* filter); |
| 41 | 42 |
| 42 // Removes all listeners and marks this object as invalid so that no more | 43 // Removes all listeners and marks this object as invalid so that no more |
| 43 // are added. | 44 // are added. |
| 44 void Invalidate(v8::Local<v8::Context> context); | 45 void Invalidate(v8::Local<v8::Context> context); |
| 45 | 46 |
| 46 size_t GetNumListeners() const; | 47 size_t GetNumListeners() const; |
| 47 | 48 |
| 48 private: | 49 private: |
| 49 // Bound methods for the Event JS object. | 50 // Bound methods for the Event JS object. |
| 50 void AddListener(gin::Arguments* arguments); | 51 void AddListener(gin::Arguments* arguments); |
| 51 void RemoveListener(gin::Arguments* arguments); | 52 void RemoveListener(gin::Arguments* arguments); |
| 52 bool HasListener(v8::Local<v8::Function> function); | 53 bool HasListener(v8::Local<v8::Function> function); |
| 53 bool HasListeners(); | 54 bool HasListeners(); |
| 54 void Dispatch(gin::Arguments* arguments); | 55 void Dispatch(gin::Arguments* arguments); |
| 55 | 56 |
| 57 // Notifies the listeners of an event with the given |args|. If |run_sync| is |
| 58 // true, runs JS synchronously and populates |out_values| with the results of |
| 59 // the listeners. |
| 60 void DispatchImpl(v8::Local<v8::Context> context, |
| 61 std::vector<v8::Local<v8::Value>>* args, |
| 62 const EventFilteringInfo* filter, |
| 63 bool run_sync, |
| 64 std::vector<v8::Global<v8::Value>>* out_values); |
| 65 |
| 56 // Whether or not this object is still valid; false upon context release. | 66 // Whether or not this object is still valid; false upon context release. |
| 57 // When invalid, no listeners can be added or removed. | 67 // When invalid, no listeners can be added or removed. |
| 58 bool valid_ = true; | 68 bool valid_ = true; |
| 59 | 69 |
| 60 // Whether the event supports filters. | 70 // Whether the event supports filters. |
| 61 bool supports_filters_ = false; | 71 bool supports_filters_ = false; |
| 62 | 72 |
| 63 std::unique_ptr<APIEventListeners> listeners_; | 73 std::unique_ptr<APIEventListeners> listeners_; |
| 64 | 74 |
| 65 binding::RunJSFunction run_js_; | 75 binding::RunJSFunction run_js_; |
| 76 binding::RunJSFunctionSync run_js_sync_; |
| 66 | 77 |
| 67 DISALLOW_COPY_AND_ASSIGN(EventEmitter); | 78 DISALLOW_COPY_AND_ASSIGN(EventEmitter); |
| 68 }; | 79 }; |
| 69 | 80 |
| 70 } // namespace extensions | 81 } // namespace extensions |
| 71 | 82 |
| 72 #endif // EXTENSIONS_RENDERER_EVENT_EMITTER_H_ | 83 #endif // EXTENSIONS_RENDERER_EVENT_EMITTER_H_ |
| OLD | NEW |