refactor: simplify events (#37099)

This commit is contained in:
Jeremy Rose 2023-02-13 13:39:18 -08:00 committed by GitHub
parent 8b3e498436
commit 71944f2c3b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
32 changed files with 290 additions and 409 deletions

View file

@ -7,7 +7,7 @@
#include "gin/per_isolate_data.h"
#include "gin/wrappable.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/common/gin_helper/event_emitter_template.h"
#include "shell/common/gin_helper/function_template_extensions.h"
namespace gin_helper {

View file

@ -0,0 +1,31 @@
// Copyright (c) 2023 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/event.h"
#include "gin/dictionary.h"
#include "gin/object_template_builder.h"
namespace gin_helper::internal {
// static
gin::Handle<Event> Event::New(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new Event());
}
// static
v8::Local<v8::ObjectTemplate> Event::FillObjectTemplate(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ) {
return gin::ObjectTemplateBuilder(isolate, "Event", templ)
.SetMethod("preventDefault", &Event::PreventDefault)
.SetProperty("defaultPrevented", &Event::GetDefaultPrevented)
.Build();
}
Event::Event() = default;
Event::~Event() = default;
gin::WrapperInfo Event::kWrapperInfo = {gin::kEmbedderNativeGin};
} // namespace gin_helper::internal

View file

@ -0,0 +1,48 @@
// Copyright (c) 2023 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_H_
#include "gin/handle.h"
#include "gin/wrappable.h"
#include "shell/common/gin_helper/constructible.h"
namespace v8 {
class Isolate;
template <typename T>
class Local;
class Object;
class ObjectTemplate;
} // namespace v8
namespace gin_helper::internal {
class Event : public gin::Wrappable<Event>,
public gin_helper::Constructible<Event> {
public:
// gin_helper::Constructible
static gin::Handle<Event> New(v8::Isolate* isolate);
static v8::Local<v8::ObjectTemplate> FillObjectTemplate(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> prototype);
// gin::Wrappable
static gin::WrapperInfo kWrapperInfo;
~Event() override;
void PreventDefault() { default_prevented_ = true; }
bool GetDefaultPrevented() { return default_prevented_; }
private:
Event();
bool default_prevented_ = false;
};
} // namespace gin_helper::internal
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_H_

View file

@ -1,76 +0,0 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/event_emitter.h"
#include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_process_host.h"
#include "shell/browser/api/event.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
namespace gin_helper::internal {
namespace {
v8::Persistent<v8::ObjectTemplate> event_template;
void PreventDefault(gin_helper::Arguments* args) {
Dictionary self;
if (args->GetHolder(&self))
self.Set("defaultPrevented", true);
}
} // namespace
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
v8::Local<v8::Object> sender,
v8::Local<v8::Object> custom_event) {
if (event_template.IsEmpty()) {
event_template.Reset(
isolate,
ObjectTemplateBuilder(isolate, v8::ObjectTemplate::New(isolate))
.SetMethod("preventDefault", &PreventDefault)
.Build());
}
v8::Local<v8::Context> context = isolate->GetCurrentContext();
CHECK(!context.IsEmpty());
v8::Local<v8::Object> event =
v8::Local<v8::ObjectTemplate>::New(isolate, event_template)
->NewInstance(context)
.ToLocalChecked();
if (!sender.IsEmpty())
Dictionary(isolate, event).Set("sender", sender);
if (!custom_event.IsEmpty())
event->SetPrototype(context, custom_event).IsJust();
return event;
}
v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,
content::RenderFrameHost* frame,
electron::mojom::ElectronApiIPC::MessageSyncCallback callback) {
v8::Local<v8::Object> event;
if (frame && callback) {
gin::Handle<Event> native_event = Event::Create(isolate);
native_event->SetCallback(std::move(callback));
event = native_event.ToV8().As<v8::Object>();
} else {
// No need to create native event if we do not need to send reply.
event = CreateCustomEvent(isolate);
}
Dictionary dict(isolate, event);
dict.Set("sender", sender);
// Should always set frameId even when callback is null.
if (frame) {
dict.Set("frameId", frame->GetRoutingID());
dict.Set("processId", frame->GetProcess()->GetID());
}
return event;
}
} // namespace gin_helper::internal

View file

@ -10,6 +10,8 @@
#include "content/public/browser/browser_thread.h"
#include "electron/shell/common/api/api.mojom.h"
#include "gin/handle.h"
#include "shell/common/gin_helper/event.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/wrappable.h"
@ -19,20 +21,6 @@ class RenderFrameHost;
namespace gin_helper {
namespace internal {
v8::Local<v8::Object> CreateCustomEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender = v8::Local<v8::Object>(),
v8::Local<v8::Object> custom_event = v8::Local<v8::Object>());
v8::Local<v8::Object> CreateNativeEvent(
v8::Isolate* isolate,
v8::Local<v8::Object> sender,
content::RenderFrameHost* frame,
electron::mojom::ElectronApiIPC::MessageSyncCallback callback);
} // namespace internal
// Provide helperers to emit event in JavaScript.
template <typename T>
class EventEmitter : public gin_helper::Wrappable<T> {
@ -48,16 +36,6 @@ class EventEmitter : public gin_helper::Wrappable<T> {
return Base::GetWrapper(isolate);
}
// this.emit(name, event, args...);
template <typename... Args>
bool EmitCustomEvent(base::StringPiece name,
v8::Local<v8::Object> event,
Args&&... args) {
return EmitWithEvent(
name, internal::CreateCustomEvent(isolate(), GetWrapper(), event),
std::forward<Args>(args)...);
}
// this.emit(name, new Event(), args...);
template <typename... Args>
bool Emit(base::StringPiece name, Args&&... args) {
@ -65,8 +43,8 @@ class EventEmitter : public gin_helper::Wrappable<T> {
v8::Local<v8::Object> wrapper = GetWrapper();
if (wrapper.IsEmpty())
return false;
v8::Local<v8::Object> event =
internal::CreateCustomEvent(isolate(), wrapper);
gin::Handle<gin_helper::internal::Event> event =
internal::Event::New(isolate());
return EmitWithEvent(name, event, std::forward<Args>(args)...);
}
@ -81,20 +59,14 @@ class EventEmitter : public gin_helper::Wrappable<T> {
// this.emit(name, event, args...);
template <typename... Args>
bool EmitWithEvent(base::StringPiece name,
v8::Local<v8::Object> event,
gin::Handle<gin_helper::internal::Event> event,
Args&&... args) {
// It's possible that |this| will be deleted by EmitEvent, so save anything
// we need from |this| before calling EmitEvent.
auto* isolate = this->isolate();
auto context = isolate->GetCurrentContext();
gin_helper::EmitEvent(isolate, GetWrapper(), name, event,
std::forward<Args>(args)...);
v8::Local<v8::Value> defaultPrevented;
if (event->Get(context, gin::StringToV8(isolate, "defaultPrevented"))
.ToLocal(&defaultPrevented)) {
return defaultPrevented->BooleanValue(isolate);
}
return false;
return event->GetDefaultPrevented();
}
};

View file

@ -0,0 +1,44 @@
// Copyright (c) 2023 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#include "shell/common/gin_helper/event_emitter_template.h"
#include "gin/converter.h"
#include "gin/per_isolate_data.h"
#include "shell/browser/api/electron_api_event_emitter.h"
#include "v8/include/v8-function.h"
#include "v8/include/v8-template.h"
namespace gin_helper::internal {
gin::WrapperInfo kWrapperInfo = {gin::kEmbedderNativeGin};
v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate) {
gin::PerIsolateData* data = gin::PerIsolateData::From(isolate);
v8::Local<v8::FunctionTemplate> tmpl =
data->GetFunctionTemplate(&kWrapperInfo);
if (tmpl.IsEmpty()) {
tmpl = v8::FunctionTemplate::New(isolate);
v8::Local<v8::Context> context = isolate->GetCurrentContext();
v8::Local<v8::Function> func = tmpl->GetFunction(context).ToLocalChecked();
v8::Local<v8::Object> eventemitter_prototype =
electron::GetEventEmitterPrototype(isolate);
v8::Local<v8::Value> func_prototype;
CHECK(func->Get(context, gin::StringToSymbol(isolate, "prototype"))
.ToLocal(&func_prototype));
CHECK(func_prototype.As<v8::Object>()
->SetPrototype(context, eventemitter_prototype)
.ToChecked());
data->SetFunctionTemplate(&kWrapperInfo, tmpl);
}
return tmpl;
}
} // namespace gin_helper::internal

View file

@ -0,0 +1,19 @@
// Copyright (c) 2023 Salesforce, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_TEMPLATE_H_
#define ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_TEMPLATE_H_
namespace v8 {
class Isolate;
template <typename T>
class Local;
class FunctionTemplate;
} // namespace v8
namespace gin_helper::internal {
v8::Local<v8::FunctionTemplate> GetEventEmitterTemplate(v8::Isolate* isolate);
}
#endif // ELECTRON_SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_TEMPLATE_H_

View file

@ -31,6 +31,7 @@
#include "shell/common/electron_command_line.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/event.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/locker.h"
#include "shell/common/gin_helper/microtasks_scope.h"
@ -50,7 +51,6 @@
V(electron_browser_content_tracing) \
V(electron_browser_crash_reporter) \
V(electron_browser_dialog) \
V(electron_browser_event) \
V(electron_browser_event_emitter) \
V(electron_browser_global_shortcut) \
V(electron_browser_in_app_purchase) \
@ -463,6 +463,9 @@ void NodeBindings::Initialize() {
SetErrorMode(GetErrorMode() & ~SEM_NOGPFAULTERRORBOX);
#endif
v8::Isolate* isolate = v8::Isolate::GetCurrent();
gin_helper::internal::Event::GetConstructor(isolate->GetCurrentContext());
g_is_initialized = true;
}