chore: remove native_mate (Part 9) (#20645)
* refactor: remove a few uses of native_mate/gfx_converter.h * refactor: deprecate mate::EventEmitter * refactor: add gin_helper::EventEmitter * refactor: convert a few classes to use gin_helper::EventEmitter * refactor: get rid of native_mate_converters/gfx_converter.h * fix: follow native_mate on reporting errors * fix: gin is weak at guessing parameter types * fix: incorrect full class name * fix: gin::Handle does not accept null
This commit is contained in:
parent
b155ebeeb3
commit
eb0e55c514
38 changed files with 532 additions and 326 deletions
|
@ -39,6 +39,14 @@ struct Converter<const char*> {
|
|||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<char[]> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
|
||||
return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal)
|
||||
.ToLocalChecked();
|
||||
}
|
||||
};
|
||||
|
||||
template <size_t n>
|
||||
struct Converter<char[n]> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val) {
|
||||
|
|
22
shell/common/gin_helper/arguments.cc
Normal file
22
shell/common/gin_helper/arguments.cc
Normal file
|
@ -0,0 +1,22 @@
|
|||
// Copyright 2019 The Chromium Authors. All rights reserved.
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE.chromium file.
|
||||
|
||||
#include "shell/common/gin_helper/arguments.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
void Arguments::ThrowError() const {
|
||||
// Gin advances |next_| counter when conversion fails while we do not, so we
|
||||
// have to manually advance the counter here to make gin report error with the
|
||||
// correct index.
|
||||
const_cast<Arguments*>(this)->Skip();
|
||||
gin::Arguments::ThrowError();
|
||||
}
|
||||
|
||||
void Arguments::ThrowError(base::StringPiece message) const {
|
||||
isolate()->ThrowException(
|
||||
v8::Exception::Error(gin::StringToV8(isolate(), message)));
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
|
@ -5,6 +5,8 @@
|
|||
#ifndef SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_
|
||||
|
||||
#include "gin/arguments.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
// Provides additional APIs to the gin::Arguments class.
|
||||
|
@ -25,6 +27,21 @@ class Arguments : public gin::Arguments {
|
|||
return true;
|
||||
}
|
||||
|
||||
// Gin always returns true when converting V8 value to boolean, we do not want
|
||||
// this behavior when parsing parameters.
|
||||
bool GetNext(bool* out) {
|
||||
v8::Local<v8::Value> val = PeekNext();
|
||||
if (val.IsEmpty() || !val->IsBoolean())
|
||||
return false;
|
||||
*out = val->BooleanValue(isolate());
|
||||
Skip();
|
||||
return true;
|
||||
}
|
||||
|
||||
// Throw error with custom error message.
|
||||
void ThrowError() const;
|
||||
void ThrowError(base::StringPiece message) const;
|
||||
|
||||
private:
|
||||
// MUST NOT ADD ANY DATA MEMBER.
|
||||
};
|
||||
|
|
67
shell/common/gin_helper/event_emitter.cc
Normal file
67
shell/common/gin_helper/event_emitter.cc
Normal file
|
@ -0,0 +1,67 @@
|
|||
// 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 "shell/common/gin_helper/dictionary.h"
|
||||
#include "shell/common/gin_helper/object_template_builder.h"
|
||||
#include "ui/events/event_constants.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
namespace 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> CreateEventObject(v8::Isolate* isolate) {
|
||||
if (event_template.IsEmpty()) {
|
||||
event_template.Reset(
|
||||
isolate,
|
||||
ObjectTemplateBuilder(isolate, v8::ObjectTemplate::New(isolate))
|
||||
.SetMethod("preventDefault", &PreventDefault)
|
||||
.Build());
|
||||
}
|
||||
|
||||
return v8::Local<v8::ObjectTemplate>::New(isolate, event_template)
|
||||
->NewInstance(isolate->GetCurrentContext())
|
||||
.ToLocalChecked();
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> object,
|
||||
v8::Local<v8::Object> custom_event) {
|
||||
v8::Local<v8::Object> event = CreateEventObject(isolate);
|
||||
event->SetPrototype(custom_event->CreationContext(), custom_event).IsJust();
|
||||
Dictionary(isolate, event).Set("sender", object);
|
||||
return event;
|
||||
}
|
||||
|
||||
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags) {
|
||||
const int mouse_button_flags =
|
||||
(ui::EF_RIGHT_MOUSE_BUTTON | ui::EF_LEFT_MOUSE_BUTTON |
|
||||
ui::EF_MIDDLE_MOUSE_BUTTON | ui::EF_BACK_MOUSE_BUTTON |
|
||||
ui::EF_FORWARD_MOUSE_BUTTON);
|
||||
const int is_mouse_click = static_cast<bool>(flags & mouse_button_flags);
|
||||
Dictionary obj = gin::Dictionary::CreateEmpty(isolate);
|
||||
obj.Set("shiftKey", static_cast<bool>(flags & ui::EF_SHIFT_DOWN));
|
||||
obj.Set("ctrlKey", static_cast<bool>(flags & ui::EF_CONTROL_DOWN));
|
||||
obj.Set("altKey", static_cast<bool>(flags & ui::EF_ALT_DOWN));
|
||||
obj.Set("metaKey", static_cast<bool>(flags & ui::EF_COMMAND_DOWN));
|
||||
obj.Set("triggeredByAccelerator", !is_mouse_click);
|
||||
return obj.GetHandle();
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
} // namespace gin_helper
|
107
shell/common/gin_helper/event_emitter.h
Normal file
107
shell/common/gin_helper/event_emitter.h
Normal file
|
@ -0,0 +1,107 @@
|
|||
// Copyright (c) 2019 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
||||
#define SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
||||
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
#include "base/optional.h"
|
||||
#include "electron/shell/common/api/api.mojom.h"
|
||||
#include "shell/common/gin_helper/event_emitter_caller.h"
|
||||
|
||||
namespace gin_helper {
|
||||
|
||||
namespace internal {
|
||||
|
||||
v8::Local<v8::Object> CreateEventObject(v8::Isolate* isolate);
|
||||
v8::Local<v8::Object> CreateCustomEvent(v8::Isolate* isolate,
|
||||
v8::Local<v8::Object> object,
|
||||
v8::Local<v8::Object> event);
|
||||
v8::Local<v8::Object> CreateEventFromFlags(v8::Isolate* isolate, int flags);
|
||||
|
||||
} // namespace internal
|
||||
|
||||
// Provide helperers to emit event in JavaScript.
|
||||
//
|
||||
// TODO(zcbenz): Inherit from Wrappable directly after removing native_mate.
|
||||
template <typename Base>
|
||||
class EventEmitter : public Base {
|
||||
public:
|
||||
typedef std::vector<v8::Local<v8::Value>> ValueArray;
|
||||
|
||||
// Make the convinient methods visible:
|
||||
// https://isocpp.org/wiki/faq/templates#nondependent-name-lookup-members
|
||||
v8::Isolate* isolate() const { return Base::isolate(); }
|
||||
v8::Local<v8::Object> GetWrapper() const { return Base::GetWrapper(); }
|
||||
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const {
|
||||
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(flags), args...);
|
||||
template <typename... Args>
|
||||
bool EmitWithFlags(base::StringPiece name, int flags, Args&&... args) {
|
||||
return EmitCustomEvent(name,
|
||||
internal::CreateEventFromFlags(isolate(), flags),
|
||||
std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
// this.emit(name, new Event(), args...);
|
||||
template <typename... Args>
|
||||
bool Emit(base::StringPiece name, Args&&... args) {
|
||||
v8::Locker locker(isolate());
|
||||
v8::HandleScope handle_scope(isolate());
|
||||
v8::Local<v8::Object> wrapper = GetWrapper();
|
||||
if (wrapper.IsEmpty()) {
|
||||
return false;
|
||||
}
|
||||
v8::Local<v8::Object> event = internal::CreateEventObject(isolate());
|
||||
event
|
||||
->Set(isolate()->GetCurrentContext(),
|
||||
gin::StringToV8(isolate(), "sender"), wrapper)
|
||||
.IsJust();
|
||||
return EmitWithEvent(name, event, std::forward<Args>(args)...);
|
||||
}
|
||||
|
||||
protected:
|
||||
EventEmitter() {}
|
||||
|
||||
private:
|
||||
// this.emit(name, event, args...);
|
||||
template <typename... Args>
|
||||
bool EmitWithEvent(base::StringPiece name,
|
||||
v8::Local<v8::Object> 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();
|
||||
v8::Locker locker(isolate);
|
||||
v8::HandleScope handle_scope(isolate);
|
||||
auto context = isolate->GetCurrentContext();
|
||||
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;
|
||||
}
|
||||
|
||||
DISALLOW_COPY_AND_ASSIGN(EventEmitter);
|
||||
};
|
||||
|
||||
} // namespace gin_helper
|
||||
|
||||
#endif // SHELL_COMMON_GIN_HELPER_EVENT_EMITTER_H_
|
|
@ -29,4 +29,10 @@ ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl(
|
|||
return *this;
|
||||
}
|
||||
|
||||
v8::Local<v8::ObjectTemplate> ObjectTemplateBuilder::Build() {
|
||||
v8::Local<v8::ObjectTemplate> result = template_;
|
||||
template_.Clear();
|
||||
return result;
|
||||
}
|
||||
|
||||
} // namespace gin_helper
|
||||
|
|
|
@ -56,6 +56,8 @@ class ObjectTemplateBuilder {
|
|||
CallbackTraits<U>::CreateTemplate(isolate_, setter));
|
||||
}
|
||||
|
||||
v8::Local<v8::ObjectTemplate> Build();
|
||||
|
||||
private:
|
||||
ObjectTemplateBuilder& SetImpl(const base::StringPiece& name,
|
||||
v8::Local<v8::Data> val);
|
||||
|
|
|
@ -1,78 +0,0 @@
|
|||
// Copyright (c) 2014 GitHub, Inc.
|
||||
// Use of this source code is governed by the MIT license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_GFX_CONVERTER_H_
|
||||
#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_GFX_CONVERTER_H_
|
||||
|
||||
#include "native_mate/converter.h"
|
||||
#include "shell/common/gin_converters/gfx_converter.h"
|
||||
|
||||
namespace mate {
|
||||
|
||||
template <>
|
||||
struct Converter<gfx::Point> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::Point& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
gfx::Point* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<gfx::PointF> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const gfx::PointF& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
gfx::PointF* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<gfx::Size> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const gfx::Size& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
gfx::Size* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<gfx::Rect> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const gfx::Rect& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
gfx::Rect* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
template <>
|
||||
struct Converter<display::Display> {
|
||||
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
|
||||
const display::Display& val) {
|
||||
return gin::ConvertToV8(isolate, val);
|
||||
}
|
||||
static bool FromV8(v8::Isolate* isolate,
|
||||
v8::Local<v8::Value> val,
|
||||
display::Display* out) {
|
||||
return gin::ConvertFromV8(isolate, val, out);
|
||||
}
|
||||
};
|
||||
|
||||
} // namespace mate
|
||||
|
||||
#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_GFX_CONVERTER_H_
|
|
@ -55,12 +55,13 @@ class Promise {
|
|||
|
||||
static void ResolvePromise(Promise<RT> promise, RT result) {
|
||||
if (!content::BrowserThread::CurrentlyOn(content::BrowserThread::UI)) {
|
||||
base::PostTask(FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<RT> promise,
|
||||
RT result) { promise.Resolve(result); },
|
||||
std::move(promise), std::move(result)));
|
||||
base::PostTask(
|
||||
FROM_HERE, {content::BrowserThread::UI},
|
||||
base::BindOnce([](Promise<RT> promise,
|
||||
RT result) { promise.ResolveWithGin(result); },
|
||||
std::move(promise), std::move(result)));
|
||||
} else {
|
||||
promise.Resolve(result);
|
||||
promise.ResolveWithGin(result);
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue