2020-03-18 22:57:08 +03: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 10:34:31 +03:00
|
|
|
#ifndef ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|
|
|
|
#define ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|
2020-03-18 22:57:08 +03:00
|
|
|
|
2024-01-11 04:00:37 +03:00
|
|
|
#include <string_view>
|
2020-03-18 22:57:08 +03:00
|
|
|
#include <utility>
|
|
|
|
|
2023-02-14 00:39:18 +03:00
|
|
|
#include "gin/handle.h"
|
2020-03-18 22:57:08 +03:00
|
|
|
#include "gin/object_template_builder.h"
|
2020-06-22 19:35:24 +03:00
|
|
|
#include "shell/browser/javascript_environment.h"
|
2023-02-14 00:39:18 +03:00
|
|
|
#include "shell/common/gin_helper/event.h"
|
2020-03-18 22:57:08 +03:00
|
|
|
#include "shell/common/gin_helper/event_emitter.h"
|
|
|
|
|
|
|
|
namespace gin_helper {
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class EventEmitterMixin {
|
|
|
|
public:
|
2021-11-03 14:41:45 +03:00
|
|
|
// disable copy
|
|
|
|
EventEmitterMixin(const EventEmitterMixin&) = delete;
|
|
|
|
EventEmitterMixin& operator=(const EventEmitterMixin&) = delete;
|
|
|
|
|
2020-03-18 22:57:08 +03:00
|
|
|
// this.emit(name, new Event(), args...);
|
2020-11-10 20:06:03 +03:00
|
|
|
// Returns true if event.preventDefault() was called during processing.
|
2020-03-18 22:57:08 +03:00
|
|
|
template <typename... Args>
|
2024-01-11 04:00:37 +03:00
|
|
|
bool Emit(const std::string_view name, Args&&... args) {
|
2020-06-22 19:35:24 +03:00
|
|
|
v8::Isolate* isolate = electron::JavascriptEnvironment::GetIsolate();
|
2020-03-18 22:57:08 +03:00
|
|
|
v8::HandleScope handle_scope(isolate);
|
|
|
|
v8::Local<v8::Object> wrapper;
|
|
|
|
if (!static_cast<T*>(this)->GetWrapper(isolate).ToLocal(&wrapper))
|
|
|
|
return false;
|
2023-02-14 00:39:18 +03: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 22:57:08 +03:00
|
|
|
}
|
|
|
|
|
2022-10-20 08:49:49 +03:00
|
|
|
// this.emit(name, args...);
|
|
|
|
template <typename... Args>
|
2024-01-11 04:00:37 +03:00
|
|
|
void EmitWithoutEvent(const std::string_view name, Args&&... args) {
|
2022-10-20 08:49:49 +03: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 22:57:08 +03: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 20:18:12 +03:00
|
|
|
constructor->SetClassName(
|
|
|
|
gin::StringToV8(isolate, static_cast<T*>(this)->GetTypeName()));
|
2020-03-18 22:57:08 +03: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 10:34:31 +03:00
|
|
|
#endif // ELECTRON_SHELL_BROWSER_EVENT_EMITTER_MIXIN_H_
|