2019-10-21 07:05:40 +00:00
|
|
|
// Copyright (c) 2019 GitHub, Inc.
|
2014-04-25 09:49:37 +00:00
|
|
|
// Use of this source code is governed by the MIT license that can be
|
2014-04-17 09:12:27 +00:00
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
|
|
|
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
#include <utility>
|
2014-10-25 03:30:35 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2019-04-02 22:38:16 +00:00
|
|
|
#include "base/optional.h"
|
2019-06-19 20:46:59 +00:00
|
|
|
#include "electron/shell/common/api/api.mojom.h"
|
2019-10-21 07:05:40 +00:00
|
|
|
#include "shell/common/gin_helper/event_emitter_caller.h"
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
namespace gin_helper {
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
namespace internal {
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate);
|
2018-04-18 01:44:10 +00:00
|
|
|
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
|
|
|
v8::Local<v8::Object> object,
|
|
|
|
v8::Local<v8::Object> event);
|
2016-06-22 02:00:45 +00:00
|
|
|
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
|
2016-04-25 01:17:54 +00:00
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
// Provide helperers to emit event in JavaScript.
|
2019-10-21 07:05:40 +00:00
|
|
|
//
|
|
|
|
// TODO(zcbenz): Inherit from Wrappable directly after removing native_mate.
|
|
|
|
template <typename Base>
|
|
|
|
class EventEmitter : public Base {
|
2014-10-25 03:30:35 +00:00
|
|
|
public:
|
2015-05-22 11:11:22 +00:00
|
|
|
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
2014-10-25 03:30:35 +00:00
|
|
|
|
2016-04-25 01:17:54 +00:00
|
|
|
// Make the convinient methods visible:
|
|
|
|
// https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
|
2019-10-21 07:05:40 +00:00
|
|
|
v8::Isolate* isolate() const { return Base::isolate(); }
|
|
|
|
v8::Local<v8::Object> GetWrapper() const { return Base::GetWrapper(); }
|
2019-04-30 13:45:05 +00:00
|
|
|
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const {
|
2019-10-21 07:05:40 +00:00
|
|
|
return Base::GetWrapper(isolate);
|
2019-04-30 13:45:05 +00:00
|
|
|
}
|
2016-04-25 01:17:54 +00:00
|
|
|
|
2015-07-29 06:24:45 +00:00
|
|
|
// this.emit(name, event, args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitCustomEvent(base::StringPiece name,
|
2015-07-29 06:24:45 +00:00
|
|
|
v8::Local<v8::Object> event,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2016-04-25 01:17:54 +00:00
|
|
|
return EmitWithEvent(
|
2018-04-18 01:44:10 +00:00
|
|
|
name, internal::CreateCustomEvent(isolate(), GetWrapper(), event),
|
2019-04-27 04:42:56 +00:00
|
|
|
std::forward<Args>(args)...);
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2016-06-22 02:00:45 +00:00
|
|
|
// this.emit(name, new Event(flags), args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitWithFlags(base::StringPiece name, int flags, Args&&... args) {
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitCustomEvent(name,
|
|
|
|
internal::CreateEventFromFlags(isolate(), flags),
|
|
|
|
std::forward<Args>(args)...);
|
2016-06-22 02:00:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-25 08:13:16 +00:00
|
|
|
// this.emit(name, new Event(), args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool Emit(base::StringPiece name, Args&&... args) {
|
2015-08-27 07:22:02 +00:00
|
|
|
v8::Locker locker(isolate());
|
|
|
|
v8::HandleScope handle_scope(isolate());
|
2017-10-24 05:38:55 +00:00
|
|
|
v8::Local<v8::Object> wrapper = GetWrapper();
|
2017-10-24 05:58:43 +00:00
|
|
|
if (wrapper.IsEmpty()) {
|
2017-10-24 05:38:55 +00:00
|
|
|
return false;
|
2017-10-24 05:58:43 +00:00
|
|
|
}
|
2019-10-21 07:05:40 +00:00
|
|
|
v8::Local<v8::Object> event = internal::CreateEventObject(isolate());
|
|
|
|
event
|
|
|
|
->Set(isolate()->GetCurrentContext(),
|
|
|
|
gin::StringToV8(isolate(), "sender"), wrapper)
|
|
|
|
.IsJust();
|
2019-04-27 04:42:56 +00:00
|
|
|
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
2015-01-15 01:51:54 +00:00
|
|
|
}
|
2014-04-25 08:13:16 +00:00
|
|
|
|
2015-06-25 06:28:13 +00:00
|
|
|
protected:
|
2016-04-25 01:17:54 +00:00
|
|
|
EventEmitter() {}
|
2015-06-25 06:28:13 +00:00
|
|
|
|
2015-01-15 01:51:54 +00:00
|
|
|
private:
|
2015-07-29 06:24:45 +00:00
|
|
|
// this.emit(name, event, args...);
|
2018-04-18 01:44:10 +00:00
|
|
|
template <typename... Args>
|
2019-07-25 15:19:04 +00:00
|
|
|
bool EmitWithEvent(base::StringPiece name,
|
2015-07-29 06:24:45 +00:00
|
|
|
v8::Local<v8::Object> event,
|
2019-04-27 04:42:56 +00:00
|
|
|
Args&&... args) {
|
2019-09-30 22:00:47 +00:00
|
|
|
// It's possible that |this| will be deleted by EmitEvent, so save anything
|
|
|
|
// we need from |this| before calling EmitEvent.
|
|
|
|
auto* isolate = this->isolate();
|
|
|
|
v8::Locker locker(isolate);
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
auto context = isolate->GetCurrentContext();
|
|
|
|
EmitEvent(isolate, GetWrapper(), name, event, std::forward<Args>(args)...);
|
2019-05-01 00:18:22 +00:00
|
|
|
v8::Local<v8::Value> defaultPrevented;
|
2019-10-21 07:05:40 +00:00
|
|
|
if (event->Get(context, gin::StringToV8(isolate, "defaultPrevented"))
|
2019-05-01 00:18:22 +00:00
|
|
|
.ToLocal(&defaultPrevented)) {
|
2019-09-30 22:00:47 +00:00
|
|
|
return defaultPrevented->BooleanValue(isolate);
|
2019-05-01 00:18:22 +00:00
|
|
|
}
|
|
|
|
return false;
|
2015-07-29 06:24:45 +00:00
|
|
|
}
|
|
|
|
|
2014-04-17 09:12:27 +00:00
|
|
|
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
|
|
|
};
|
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
} // namespace gin_helper
|
2014-04-17 09:12:27 +00:00
|
|
|
|
2019-10-21 07:05:40 +00:00
|
|
|
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|