2020-03-18 19:57:08 +00:00
|
|
|
// Copyright (c) 2019 Slack Technologies, Inc.
|
|
|
|
// Use of this source code is governed by the MIT license that can be
|
|
|
|
// found in the LICENSE file.
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|
2020-03-18 19:57:08 +00:00
|
|
|
|
2024-01-11 01:00:37 +00:00
|
|
|
#include <string_view>
|
2020-03-18 19:57:08 +00:00
|
|
|
#include <utility>
|
|
|
|
|
2023-02-13 21:39:18 +00:00
|
|
|
#include "gin/handle.h"
|
2020-03-18 19:57:08 +00:00
|
|
|
#include "gin/object_template_builder.h"
|
2020-06-22 16:35:24 +00:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2023-02-13 21:39:18 +00:00
|
|
|
#include "shell/common/gin_helper/event.h"
|
2020-03-18 19:57:08 +00:00
|
|
|
#include "shell/common/gin_helper/event_emitter.h"
|
|
|
|
|
|
|
|
namespace gin_helper {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class EventEmitterMixin {
|
|
|
|
public:
|
2021-11-03 11:41:45 +00:00
|
|
|
// disable copy
|
|
|
|
EventEmitterMixin(const EventEmitterMixin&) = delete;
|
|
|
|
EventEmitterMixin& operator=(const EventEmitterMixin&) = delete;
|
|
|
|
|
2020-03-18 19:57:08 +00:00
|
|
|
// this.emit(name, new Event(), args...);
|
2020-11-10 17:06:03 +00:00
|
|
|
// Returns true if event.preventDefault() was called during processing.
|
2020-03-18 19:57:08 +00:00
|
|
|
template <typename... Args>
|
2024-01-11 01:00:37 +00:00
|
|
|
bool Emit(const std::string_view name, Args&&... args) {
|
2020-06-22 16:35:24 +00:00
|
|
|
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
|
2020-03-18 19:57:08 +00:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Object> wrapper;
|
|
|
|
if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
|
|
|
|
return false;
|
2023-02-13 21:39:18 +00:00
|
|
|
gin::Handle<internal::Event> event = internal::Event::New(isolate);
|
|
|
|
gin_helper::EmitEvent(isolate, wrapper, name, event,
|
|
|
|
std::forward<Args>(args)...);
|
|
|
|
return event->GetDefaultPrevented();
|
2020-03-18 19:57:08 +00:00
|
|
|
}
|
|
|
|
|
2022-10-20 05:49:49 +00:00
|
|
|
// this.emit(name, args...);
|
|
|
|
template <typename... Args>
|
2024-01-11 01:00:37 +00:00
|
|
|
void EmitWithoutEvent(const std::string_view name, Args&&... args) {
|
2022-10-20 05:49:49 +00:00
|
|
|
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
|
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Object> wrapper;
|
|
|
|
if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
|
|
|
|
return;
|
|
|
|
gin_helper::EmitEvent(isolate, wrapper, name, std::forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
2020-03-18 19:57:08 +00:00
|
|
|
protected:
|
|
|
|
EventEmitterMixin() = default;
|
|
|
|
|
|
|
|
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(v8::Isolate* isolate) {
|
|
|
|
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
|
|
|
|
auto* wrapper_info = &(static_cast<T*>(this)->kWrapperInfo);
|
|
|
|
v8::Local<v8::FunctionTemplate> constructor =
|
|
|
|
data->GetFunctionTemplate(wrapper_info);
|
|
|
|
if (constructor.IsEmpty()) {
|
|
|
|
constructor = v8::FunctionTemplate::New(isolate);
|
2020-05-19 17:18:12 +00:00
|
|
|
constructor->SetClassName(
|
|
|
|
gin::StringToV8(isolate, static_cast<T*>(this)->GetTypeName()));
|
2020-03-18 19:57:08 +00:00
|
|
|
constructor->Inherit(internal::GetEventEmitterTemplate(isolate));
|
|
|
|
data->SetFunctionTemplate(wrapper_info, constructor);
|
|
|
|
}
|
|
|
|
return gin::ObjectTemplateBuilder(isolate,
|
|
|
|
static_cast<T*>(this)->GetTypeName(),
|
|
|
|
constructor->InstanceTemplate());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace gin_helper
|
|
|
|
|
2021-11-22 07:34:31 +00:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|