chore: remove native_mate (Part 1) (#20085)

* move Destroyable utilities out of native_mate

* do not set "destroy" in ObjectTemplateBuilder

* remove ObjectTemplateBuilder::MakeDestroyable

* do not pollute gin namespace

* add more comments

* remove hack of Arguments
This commit is contained in:
Cheng Zhao 2019-09-04 11:14:16 +09:00 committed by GitHub
parent 96b42bddb8
commit b601f3a031
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
17 changed files with 108 additions and 42 deletions

View file

@ -140,8 +140,8 @@ v8::Local<v8::Value> BrowserView::GetWebContents() {
void BrowserView::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "BrowserView"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setAutoResize", &BrowserView::SetAutoResize)
.SetMethod("setBounds", &BrowserView::SetBounds)
.SetMethod("getBounds", &BrowserView::GetBounds)

View file

@ -190,8 +190,8 @@ double DownloadItem::GetStartTime() const {
void DownloadItem::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "DownloadItem"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("pause", &DownloadItem::Pause)
.SetMethod("isPaused", &DownloadItem::IsPaused)
.SetMethod("resume", &DownloadItem::Resume)

View file

@ -222,8 +222,8 @@ void Menu::OnMenuWillShow() {
void Menu::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Menu"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("insertItem", &Menu::InsertItemAt)
.SetMethod("insertCheckItem", &Menu::InsertCheckItemAt)
.SetMethod("insertRadioItem", &Menu::InsertRadioItemAt)

View file

@ -225,8 +225,8 @@ bool Notification::IsSupported() {
void Notification::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Notification"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("show", &Notification::Show)
.SetMethod("close", &Notification::Close)
.SetProperty("title", &Notification::GetTitle, &Notification::SetTitle)

View file

@ -115,9 +115,8 @@ v8::Local<v8::Value> PowerMonitor::Create(v8::Isolate* isolate) {
void PowerMonitor::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "PowerMonitor"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
#if defined(OS_LINUX)
.SetMethod("blockShutdown", &PowerMonitor::BlockShutdown)
.SetMethod("unblockShutdown", &PowerMonitor::UnblockShutdown)

View file

@ -682,8 +682,8 @@ mate::Handle<Session> Session::FromPartition(
void Session::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Session"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("resolveProxy", &Session::ResolveProxy)
.SetMethod("getCacheSize", &Session::GetCacheSize)
.SetMethod("clearCache", &Session::ClearCache)

View file

@ -1064,8 +1064,8 @@ mate::WrappableBase* TopLevelWindow::New(mate::Arguments* args) {
void TopLevelWindow::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "TopLevelWindow"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setContentView", &TopLevelWindow::SetContentView)
.SetMethod("close", &TopLevelWindow::Close)
.SetMethod("focus", &TopLevelWindow::Focus)

View file

@ -246,8 +246,8 @@ gfx::Rect Tray::GetBounds() {
void Tray::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Tray"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setImage", &Tray::SetImage)
.SetMethod("setPressedImage", &Tray::SetPressedImage)
.SetMethod("setToolTip", &Tray::SetToolTip)

View file

@ -520,8 +520,8 @@ mate::WrappableBase* URLRequestNS::New(mate::Arguments* args) {
void URLRequestNS::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "URLRequest"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("write", &URLRequestNS::Write)
.SetMethod("cancel", &URLRequestNS::Cancel)
.SetMethod("setExtraHeader", &URLRequestNS::SetExtraHeader)

View file

@ -2381,8 +2381,8 @@ v8::Local<v8::Promise> WebContents::TakeHeapSnapshot(
void WebContents::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "WebContents"));
gin_helper::Destroyable::MakeDestroyable(isolate, prototype);
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.MakeDestroyable()
.SetMethod("setBackgroundThrottling",
&WebContents::SetBackgroundThrottling)
.SetMethod("getProcessId", &WebContents::GetProcessID)

View file

@ -0,0 +1,66 @@
// 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/destroyable.h"
#include "gin/converter.h"
#include "native_mate/wrappable_base.h"
namespace gin_helper {
namespace {
// Cached function templates, leaked on exit. (They are leaked in V8 anyway.)
v8::Global<v8::FunctionTemplate> g_destroy_func;
v8::Global<v8::FunctionTemplate> g_is_destroyed_func;
void DestroyFunc(const v8::FunctionCallbackInfo<v8::Value>& info) {
v8::Local<v8::Object> holder = info.Holder();
if (Destroyable::IsDestroyed(holder))
return;
// TODO(zcbenz): mate::Wrappable will be removed.
delete static_cast<mate::WrappableBase*>(
holder->GetAlignedPointerFromInternalField(0));
holder->SetAlignedPointerInInternalField(0, nullptr);
}
void IsDestroyedFunc(const v8::FunctionCallbackInfo<v8::Value>& info) {
info.GetReturnValue().Set(gin::ConvertToV8(
info.GetIsolate(), Destroyable::IsDestroyed(info.Holder())));
}
} // namespace
// static
bool Destroyable::IsDestroyed(v8::Local<v8::Object> object) {
// An object is considered destroyed if it has no internal pointer or its
// internal has been destroyed.
return object->InternalFieldCount() == 0 ||
object->GetAlignedPointerFromInternalField(0) == nullptr;
}
// static
void Destroyable::MakeDestroyable(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
// Cache the FunctionTemplate of "destroy" and "isDestroyed".
if (g_destroy_func.IsEmpty()) {
auto templ = v8::FunctionTemplate::New(isolate, DestroyFunc);
templ->RemovePrototype();
g_destroy_func.Reset(isolate, templ);
templ = v8::FunctionTemplate::New(isolate, IsDestroyedFunc);
templ->RemovePrototype();
g_is_destroyed_func.Reset(isolate, templ);
}
auto proto_templ = prototype->PrototypeTemplate();
proto_templ->Set(
gin::StringToSymbol(isolate, "destroy"),
v8::Local<v8::FunctionTemplate>::New(isolate, g_destroy_func));
proto_templ->Set(
gin::StringToSymbol(isolate, "isDestroyed"),
v8::Local<v8::FunctionTemplate>::New(isolate, g_is_destroyed_func));
}
} // namespace gin_helper

View file

@ -0,0 +1,24 @@
// 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_DESTROYABLE_H_
#define SHELL_COMMON_GIN_HELPER_DESTROYABLE_H_
#include "v8/include/v8.h"
namespace gin_helper {
// Manage the native object wrapped in JS wrappers.
struct Destroyable {
// Determine whether the native object has been destroyed.
static bool IsDestroyed(v8::Local<v8::Object> object);
// Add "destroy" and "isDestroyed" to prototype chain.
static void MakeDestroyable(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_DESTROYABLE_H_