chore: remove native_mate (Part 14) (#21365)

* chore: remove uses of mate::Dictionary and mate::Handle

* chore: move CreateConstructor to gin_helper

* chore: remove native_mate

* chore: remove unneeded gin patch
This commit is contained in:
Cheng Zhao 2019-12-05 18:46:34 +09:00 committed by GitHub
parent 113b47d871
commit 0a741670a9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
74 changed files with 215 additions and 2120 deletions

View file

@ -90,8 +90,8 @@ void BrowserView::WebContentsDestroyed() {
}
// static
mate::WrappableBase* BrowserView::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
gin_helper::WrappableBase* BrowserView::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create BrowserView before app is ready");
return nullptr;

View file

@ -33,8 +33,8 @@ class WebContents;
class BrowserView : public gin_helper::TrackableObject<BrowserView>,
public content::WebContentsObserver {
public:
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -16,9 +16,9 @@
#include "shell/browser/unresponsive_suppressor.h"
#include "shell/browser/web_contents_preferences.h"
#include "shell/browser/window_list.h"
#include "shell/common/api/constructor.h"
#include "shell/common/color_util.h"
#include "shell/common/gin_converters/value_converter.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/node_includes.h"
@ -445,8 +445,8 @@ void BrowserWindow::OnWindowHide() {
}
// static
mate::WrappableBase* BrowserWindow::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
gin_helper::WrappableBase* BrowserWindow::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create BrowserWindow before app is ready");
return nullptr;
@ -502,7 +502,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("BrowserWindow",
mate::CreateConstructor<BrowserWindow>(
gin_helper::CreateConstructor<BrowserWindow>(
isolate, base::BindRepeating(&BrowserWindow::New)));
}

View file

@ -23,8 +23,8 @@ class BrowserWindow : public TopLevelWindow,
public content::WebContentsObserver,
public ExtendedWebContentsObserver {
public:
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -22,7 +22,7 @@ class Menu : public gin_helper::TrackableObject<Menu>,
public AtomMenuModel::Delegate,
public AtomMenuModel::Observer {
public:
static mate::WrappableBase* New(gin::Arguments* args);
static gin_helper::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -41,7 +41,7 @@ class Menu : public gin_helper::TrackableObject<Menu>,
explicit Menu(gin::Arguments* args);
~Menu() override;
// mate::Wrappable:
// gin_helper::Wrappable:
void AfterInit(v8::Isolate* isolate) override;
// ui::SimpleMenuModel::Delegate:
@ -150,17 +150,4 @@ struct Converter<electron::AtomMenuModel*> {
} // namespace gin
namespace mate {
template <>
struct Converter<electron::AtomMenuModel*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::AtomMenuModel** out) {
return gin::ConvertFromV8(isolate, val, out);
}
};
} // namespace mate
#endif // SHELL_BROWSER_API_ATOM_API_MENU_H_

View file

@ -177,7 +177,7 @@ void Menu::SendActionToFirstResponder(const std::string& action) {
}
// static
mate::WrappableBase* Menu::New(gin::Arguments* args) {
gin_helper::WrappableBase* Menu::New(gin::Arguments* args) {
return new MenuMac(args);
}

View file

@ -84,7 +84,7 @@ void MenuViews::OnClosed(int32_t window_id, base::OnceClosure callback) {
}
// static
mate::WrappableBase* Menu::New(gin::Arguments* args) {
gin_helper::WrappableBase* Menu::New(gin::Arguments* args) {
return new MenuViews(args);
}

View file

@ -5,13 +5,13 @@
#ifndef SHELL_BROWSER_API_ATOM_API_NET_H_
#define SHELL_BROWSER_API_ATOM_API_NET_H_
#include "native_mate/wrappable.h"
#include "shell/common/gin_helper/wrappable.h"
namespace electron {
namespace api {
class Net : public mate::Wrappable<Net> {
class Net : public gin_helper::Wrappable<Net> {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate);

View file

@ -80,8 +80,8 @@ Notification::~Notification() {
}
// static
mate::WrappableBase* Notification::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
gin_helper::WrappableBase* Notification::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create Notification before app is ready");
return nullptr;

View file

@ -28,8 +28,8 @@ namespace api {
class Notification : public gin_helper::TrackableObject<Notification>,
public NotificationDelegate {
public:
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static bool IsSupported();
static void BuildPrototype(v8::Isolate* isolate,

View file

@ -216,7 +216,7 @@ void DestroyGlobalHandle(v8::Isolate* isolate,
void* ptr = object->GetAlignedPointerFromInternalField(0);
if (!ptr)
return;
delete static_cast<mate::WrappableBase*>(ptr);
delete static_cast<gin_helper::WrappableBase*>(ptr);
object->SetAlignedPointerInInternalField(0, nullptr);
}
}

View file

@ -108,7 +108,7 @@ class Session : public gin_helper::TrackableObject<Session>,
download::DownloadItem* item) override;
private:
// Cached mate::Wrappable objects.
// Cached gin_helper::Wrappable objects.
v8::Global<v8::Value> cookies_;
v8::Global<v8::Value> protocol_;
v8::Global<v8::Value> net_log_;

View file

@ -1055,7 +1055,7 @@ void TopLevelWindow::RemoveFromParentChildWindows() {
}
// static
mate::WrappableBase* TopLevelWindow::New(gin_helper::Arguments* args) {
gin_helper::WrappableBase* TopLevelWindow::New(gin_helper::Arguments* args) {
gin_helper::Dictionary options =
gin::Dictionary::CreateEmpty(args->isolate());
args->GetNext(&options);

View file

@ -28,7 +28,7 @@ class View;
class TopLevelWindow : public gin_helper::TrackableObject<TopLevelWindow>,
public NativeWindowObserver {
public:
static mate::WrappableBase* New(gin_helper::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -65,9 +65,9 @@ Tray::Tray(gin::Handle<NativeImage> image, gin_helper::Arguments* args)
Tray::~Tray() = default;
// static
mate::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
gin_helper::Arguments* args) {
gin_helper::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
gin_helper::Arguments* args) {
if (!Browser::Get()->is_ready()) {
thrower.ThrowError("Cannot create Tray before app is ready");
return nullptr;

View file

@ -34,9 +34,9 @@ class NativeImage;
class Tray : public gin_helper::TrackableObject<Tray>, public TrayIconObserver {
public:
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
gin_helper::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Handle<NativeImage> image,
gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -318,7 +318,7 @@ void SimpleURLLoaderWrapper::Cancel() {
}
// static
mate::WrappableBase* SimpleURLLoaderWrapper::New(gin::Arguments* args) {
gin_helper::WrappableBase* SimpleURLLoaderWrapper::New(gin::Arguments* args) {
gin_helper::Dictionary opts;
if (!args->GetNext(&opts)) {
args->ThrowTypeError("Expected a dictionary");

View file

@ -38,7 +38,7 @@ class SimpleURLLoaderWrapper
public network::SimpleURLLoaderStreamConsumer {
public:
~SimpleURLLoaderWrapper() override;
static mate::WrappableBase* New(gin::Arguments* args);
static gin_helper::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -43,7 +43,7 @@ void View::AddChildViewAt(gin::Handle<View> child, size_t index) {
#endif
// static
mate::WrappableBase* View::New(gin::Arguments* args) {
gin_helper::WrappableBase* View::New(gin::Arguments* args) {
auto* view = new View();
view->InitWithArgs(args);
return view;

View file

@ -19,7 +19,7 @@ namespace api {
class View : public gin_helper::TrackableObject<View> {
public:
static mate::WrappableBase* New(gin::Arguments* args);
static gin_helper::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -8,7 +8,7 @@
#include "shell/browser/api/atom_api_web_contents.h"
#include "shell/browser/browser.h"
#include "shell/browser/ui/inspectable_web_contents_view.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
@ -79,7 +79,7 @@ void WebContentsView::WebContentsDestroyed() {
}
// static
mate::WrappableBase* WebContentsView::New(
gin_helper::WrappableBase* WebContentsView::New(
gin_helper::Arguments* args,
gin::Handle<WebContents> web_contents) {
// Currently we only support InspectableWebContents, e.g. the WebContents
@ -121,7 +121,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports);
dict.Set("WebContentsView",
mate::CreateConstructor<WebContentsView>(
gin_helper::CreateConstructor<WebContentsView>(
isolate, base::BindRepeating(&WebContentsView::New)));
}

View file

@ -18,8 +18,8 @@ class WebContents;
class WebContentsView : public View, public content::WebContentsObserver {
public:
static mate::WrappableBase* New(gin_helper::Arguments* args,
gin::Handle<WebContents> web_contents);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args,
gin::Handle<WebContents> web_contents);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -6,9 +6,9 @@
#include <string>
#include "native_mate/dictionary.h"
#include "shell/browser/api/atom_api_view.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace mate {
@ -42,14 +42,15 @@ BoxLayout::BoxLayout(views::BoxLayout::Orientation orientation)
BoxLayout::~BoxLayout() {}
void BoxLayout::SetFlexForView(mate::Handle<View> view, int flex) {
void BoxLayout::SetFlexForView(gin::Handle<View> view, int flex) {
auto* box_layout = static_cast<views::BoxLayout*>(layout_manager());
box_layout->SetFlexForView(view->view(), flex);
}
// static
mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
views::BoxLayout::Orientation orientation) {
gin_helper::WrappableBase* BoxLayout::New(
gin_helper::Arguments* args,
views::BoxLayout::Orientation orientation) {
auto* layout = new BoxLayout(orientation);
layout->InitWith(args->isolate(), args->GetThis());
return layout;
@ -58,8 +59,8 @@ mate::WrappableBase* BoxLayout::New(mate::Arguments* args,
// static
void BoxLayout::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "BoxLayout"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
prototype->SetClassName(gin_helper::StringTov8(isolate, "BoxLayout"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("setFlexForView", &BoxLayout::SetFlexForView);
}
@ -76,8 +77,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("BoxLayout", mate::CreateConstructor<BoxLayout>(
gin_helper::Dictionary dict(isolate, exports);
dict.Set("BoxLayout", gin_helper::CreateConstructor<BoxLayout>(
isolate, base::BindRepeating(&BoxLayout::New)));
}

View file

@ -5,7 +5,7 @@
#ifndef SHELL_BROWSER_API_VIEWS_ATOM_API_BOX_LAYOUT_H_
#define SHELL_BROWSER_API_VIEWS_ATOM_API_BOX_LAYOUT_H_
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "shell/browser/api/views/atom_api_layout_manager.h"
#include "ui/views/layout/box_layout.h"
@ -17,13 +17,14 @@ class View;
class BoxLayout : public LayoutManager {
public:
static mate::WrappableBase* New(mate::Arguments* args,
views::BoxLayout::Orientation orientation);
static gin_helper::WrappableBase* New(
gin_helper::Arguments* args,
views::BoxLayout::Orientation orientation);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
void SetFlexForView(mate::Handle<View> view, int flex);
void SetFlexForView(gin::Handle<View> view, int flex);
protected:
explicit BoxLayout(views::BoxLayout::Orientation orientation);

View file

@ -4,8 +4,8 @@
#include "shell/browser/api/views/atom_api_button.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -25,7 +25,7 @@ void Button::ButtonPressed(views::Button* sender, const ui::Event& event) {
}
// static
mate::WrappableBase* Button::New(mate::Arguments* args) {
gin_helper::WrappableBase* Button::New(gin_helper::Arguments* args) {
args->ThrowError("Button can not be created directly");
return nullptr;
}
@ -33,7 +33,7 @@ mate::WrappableBase* Button::New(mate::Arguments* args) {
// static
void Button::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Button"));
prototype->SetClassName(gin_helper::StringTov8(isolate, "Button"));
}
} // namespace api
@ -49,8 +49,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("Button", mate::CreateConstructor<Button>(
gin_helper::Dictionary dict(isolate, exports);
dict.Set("Button", gin_helper::CreateConstructor<Button>(
isolate, base::BindRepeating(&Button::New)));
}

View file

@ -5,7 +5,7 @@
#ifndef SHELL_BROWSER_API_VIEWS_ATOM_API_BUTTON_H_
#define SHELL_BROWSER_API_VIEWS_ATOM_API_BUTTON_H_
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "shell/browser/api/atom_api_view.h"
#include "ui/views/controls/button/button.h"
@ -15,7 +15,7 @@ namespace api {
class Button : public View, public views::ButtonListener {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -5,8 +5,8 @@
#include "shell/browser/api/views/atom_api_label_button.h"
#include "base/strings/utf_string_conversions.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -37,8 +37,8 @@ void LabelButton::SetIsDefault(bool is_default) {
}
// static
mate::WrappableBase* LabelButton::New(mate::Arguments* args,
const std::string& text) {
gin_helper::WrappableBase* LabelButton::New(gin_helper::Arguments* args,
const std::string& text) {
// Constructor call.
auto* view = new LabelButton(text);
view->InitWith(args->isolate(), args->GetThis());
@ -48,8 +48,8 @@ mate::WrappableBase* LabelButton::New(mate::Arguments* args,
// static
void LabelButton::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "LabelButton"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
prototype->SetClassName(gin_helper::StringTov8(isolate, "LabelButton"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("getText", &LabelButton::GetText)
.SetMethod("setText", &LabelButton::SetText)
.SetMethod("isDefault", &LabelButton::IsDefault)
@ -69,8 +69,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("LabelButton", mate::CreateConstructor<LabelButton>(
gin_helper::Dictionary dict(isolate, exports);
dict.Set("LabelButton", gin_helper::CreateConstructor<LabelButton>(
isolate, base::BindRepeating(&LabelButton::New)));
}

View file

@ -16,8 +16,8 @@ namespace api {
class LabelButton : public Button {
public:
static mate::WrappableBase* New(mate::Arguments* args,
const std::string& text);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args,
const std::string& text);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -4,8 +4,8 @@
#include "shell/browser/api/views/atom_api_layout_manager.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -30,7 +30,7 @@ std::unique_ptr<views::LayoutManager> LayoutManager::TakeOver() {
}
// static
mate::WrappableBase* LayoutManager::New(mate::Arguments* args) {
gin_helper::WrappableBase* LayoutManager::New(gin_helper::Arguments* args) {
args->ThrowError("LayoutManager can not be created directly");
return nullptr;
}
@ -52,9 +52,9 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
gin_helper::Dictionary dict(isolate, exports);
dict.Set("LayoutManager",
mate::CreateConstructor<LayoutManager>(
gin_helper::CreateConstructor<LayoutManager>(
isolate, base::BindRepeating(&LayoutManager::New)));
}

View file

@ -16,7 +16,7 @@ namespace api {
class LayoutManager : public gin_helper::TrackableObject<LayoutManager> {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -5,8 +5,8 @@
#include "shell/browser/api/views/atom_api_md_text_button.h"
#include "base/strings/utf_string_conversions.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -19,8 +19,8 @@ MdTextButton::MdTextButton(const std::string& text)
MdTextButton::~MdTextButton() {}
// static
mate::WrappableBase* MdTextButton::New(mate::Arguments* args,
const std::string& text) {
gin_helper::WrappableBase* MdTextButton::New(gin_helper::Arguments* args,
const std::string& text) {
// Constructor call.
auto* view = new MdTextButton(text);
view->InitWith(args->isolate(), args->GetThis());
@ -30,7 +30,7 @@ mate::WrappableBase* MdTextButton::New(mate::Arguments* args,
// static
void MdTextButton::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "MdTextButton"));
prototype->SetClassName(gin_helper::StringTov8(isolate, "MdTextButton"));
}
} // namespace api
@ -46,9 +46,9 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
gin_helper::Dictionary dict(isolate, exports);
dict.Set("MdTextButton",
mate::CreateConstructor<MdTextButton>(
gin_helper::CreateConstructor<MdTextButton>(
isolate, base::BindRepeating(&MdTextButton::New)));
}

View file

@ -16,8 +16,8 @@ namespace api {
class MdTextButton : public LabelButton {
public:
static mate::WrappableBase* New(mate::Arguments* args,
const std::string& text);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args,
const std::string& text);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -4,8 +4,8 @@
#include "shell/browser/api/views/atom_api_resize_area.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -23,7 +23,7 @@ void ResizeArea::OnResize(int resize_amount, bool done_resizing) {
}
// static
mate::WrappableBase* ResizeArea::New(mate::Arguments* args) {
gin_helper::WrappableBase* ResizeArea::New(gin_helper::Arguments* args) {
// Constructor call.
auto* view = new ResizeArea();
view->InitWith(args->isolate(), args->GetThis());
@ -33,7 +33,7 @@ mate::WrappableBase* ResizeArea::New(mate::Arguments* args) {
// static
void ResizeArea::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "ResizeArea"));
prototype->SetClassName(gin_helper::StringTov8(isolate, "ResizeArea"));
}
} // namespace api
@ -49,8 +49,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("ResizeArea", mate::CreateConstructor<ResizeArea>(
gin_helper::Dictionary dict(isolate, exports);
dict.Set("ResizeArea", gin_helper::CreateConstructor<ResizeArea>(
isolate, base::BindRepeating(&ResizeArea::New)));
}

View file

@ -5,7 +5,7 @@
#ifndef SHELL_BROWSER_API_VIEWS_ATOM_API_RESIZE_AREA_H_
#define SHELL_BROWSER_API_VIEWS_ATOM_API_RESIZE_AREA_H_
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "shell/browser/api/atom_api_view.h"
#include "ui/views/controls/resize_area.h"
#include "ui/views/controls/resize_area_delegate.h"
@ -16,7 +16,7 @@ namespace api {
class ResizeArea : public View, protected views::ResizeAreaDelegate {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -4,8 +4,8 @@
#include "shell/browser/api/views/atom_api_text_field.h"
#include "native_mate/dictionary.h"
#include "shell/common/api/constructor.h"
#include "shell/common/gin_helper/constructor.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {
@ -27,7 +27,7 @@ base::string16 TextField::GetText() const {
}
// static
mate::WrappableBase* TextField::New(mate::Arguments* args) {
gin_helper::WrappableBase* TextField::New(gin_helper::Arguments* args) {
// Constructor call.
auto* view = new TextField();
view->InitWith(args->isolate(), args->GetThis());
@ -37,8 +37,8 @@ mate::WrappableBase* TextField::New(mate::Arguments* args) {
// static
void TextField::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "TextField"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
prototype->SetClassName(gin_helper::StringTov8(isolate, "TextField"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("setText", &TextField::SetText)
.SetMethod("getText", &TextField::GetText);
}
@ -56,8 +56,8 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Context> context,
void* priv) {
v8::Isolate* isolate = context->GetIsolate();
mate::Dictionary dict(isolate, exports);
dict.Set("TextField", mate::CreateConstructor<TextField>(
gin_helper::Dictionary dict(isolate, exports);
dict.Set("TextField", gin_helper::CreateConstructor<TextField>(
isolate, base::BindRepeating(&TextField::New)));
}

View file

@ -5,7 +5,7 @@
#ifndef SHELL_BROWSER_API_VIEWS_ATOM_API_TEXT_FIELD_H_
#define SHELL_BROWSER_API_VIEWS_ATOM_API_TEXT_FIELD_H_
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "shell/browser/api/atom_api_view.h"
#include "ui/views/controls/textfield/textfield.h"
@ -15,7 +15,7 @@ namespace api {
class TextField : public View {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static gin_helper::WrappableBase* New(gin_helper::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);

View file

@ -13,7 +13,7 @@
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "libplatform/libplatform.h"
#include "native_mate/dictionary.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace electron {

View file

@ -6,18 +6,18 @@
#include <vector>
#include "native_mate/wrappable.h"
#include "shell/common/asar/archive.h"
#include "shell/common/asar/asar_util.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/gin_helper/wrappable.h"
#include "shell/common/node_includes.h"
#include "shell/common/node_util.h"
namespace {
class Archive : public mate::Wrappable<Archive> {
class Archive : public gin_helper::Wrappable<Archive> {
public:
static v8::Local<v8::Value> Create(v8::Isolate* isolate,
const base::FilePath& path) {

View file

@ -5,10 +5,10 @@
#ifndef SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
#define SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_
#include "native_mate/handle.h"
#include "native_mate/wrappable.h"
#include "gin/handle.h"
#include "shell/common/gin_converters/std_converter.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/gin_helper/wrappable.h"
#include "shell/common/key_weak_map.h"
namespace electron {
@ -16,10 +16,10 @@ namespace electron {
namespace api {
template <typename K>
class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
class KeyWeakMap : public gin_helper::Wrappable<KeyWeakMap<K>> {
public:
static mate::Handle<KeyWeakMap<K>> Create(v8::Isolate* isolate) {
return mate::CreateHandle(isolate, new KeyWeakMap<K>(isolate));
static gin::Handle<KeyWeakMap<K>> Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new KeyWeakMap<K>(isolate));
}
static void BuildPrototype(v8::Isolate* isolate,
@ -34,7 +34,7 @@ class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
protected:
explicit KeyWeakMap(v8::Isolate* isolate) {
mate::Wrappable<KeyWeakMap<K>>::Init(isolate);
gin_helper::Wrappable<KeyWeakMap<K>>::Init(isolate);
}
~KeyWeakMap() override {}

View file

@ -558,8 +558,8 @@ bool Converter<electron::api::NativeImage*>::FromV8(
}
*out = static_cast<electron::api::NativeImage*>(
static_cast<mate::WrappableBase*>(
mate::internal::FromV8Impl(isolate, val)));
static_cast<gin_helper::WrappableBase*>(
gin_helper::internal::FromV8Impl(isolate, val)));
return *out != nullptr;
}

View file

@ -10,8 +10,8 @@
#include "base/values.h"
#include "gin/handle.h"
#include "native_mate/wrappable.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "shell/common/gin_helper/wrappable.h"
#include "ui/gfx/image/image.h"
#if defined(OS_WIN)
@ -38,7 +38,7 @@ namespace electron {
namespace api {
class NativeImage : public mate::Wrappable<NativeImage> {
class NativeImage : public gin_helper::Wrappable<NativeImage> {
public:
static gin::Handle<NativeImage> CreateEmpty(v8::Isolate* isolate);
static gin::Handle<NativeImage> Create(v8::Isolate* isolate,

View file

@ -1,34 +0,0 @@
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_API_CONSTRUCTOR_H_
#define SHELL_COMMON_API_CONSTRUCTOR_H_
#include "native_mate/constructor.h"
namespace mate {
// Create a FunctionTemplate that can be "new"ed in JavaScript.
// It is user's responsibility to ensure this function is called for one type
// only ONCE in the program's whole lifetime, otherwise we would have memory
// leak.
template <typename T, typename Sig>
v8::Local<v8::Function> CreateConstructor(
v8::Isolate* isolate,
const base::RepeatingCallback<Sig>& func) {
#ifndef NDEBUG
static bool called = false;
CHECK(!called) << "CreateConstructor can only be called for one type once";
called = true;
#endif
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate, base::BindRepeating(&mate::internal::InvokeNew<Sig>, func));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
return templ->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();
}
} // namespace mate
#endif // SHELL_COMMON_API_CONSTRUCTOR_H_

View file

@ -3,8 +3,8 @@
// found in the LICENSE file.
#include "electron/buildflags/buildflags.h"
#include "native_mate/dictionary.h"
#include "printing/buildflags/buildflags.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
namespace {
@ -65,7 +65,7 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Local<v8::Value> unused,
v8::Local<v8::Context> context,
void* priv) {
mate::Dictionary dict(context->GetIsolate(), exports);
gin_helper::Dictionary dict(context->GetIsolate(), exports);
dict.SetMethod("isDesktopCapturerEnabled", &IsDesktopCapturerEnabled);
dict.SetMethod("isOffscreenRenderingEnabled", &IsOffscreenRenderingEnabled);
dict.SetMethod("isRemoteModuleEnabled", &IsRemoteModuleEnabled);

View file

@ -22,9 +22,8 @@ struct Converter<electron::NativeWindow*> {
}
electron::api::TopLevelWindow* window;
// TODO(deermichel): remove mate:: after dropping mate
if (!mate::Converter<electron::api::TopLevelWindow*>::FromV8(isolate, val,
&window))
if (!gin::Converter<electron::api::TopLevelWindow*>::FromV8(isolate, val,
&window))
return false;
*out = window->window();
return true;

View file

@ -0,0 +1,169 @@
// Copyright (c) 2018 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_CONSTRUCTOR_H_
#define SHELL_COMMON_GIN_HELPER_CONSTRUCTOR_H_
#include "shell/common/gin_helper/function_template.h"
#include "shell/common/gin_helper/wrappable_base.h"
namespace gin_helper {
namespace internal {
// This set of templates invokes a base::Callback by converting the Arguments
// into native types. It relies on the function_template.h to provide helper
// templates.
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*()>& callback) {
return callback.Run();
}
template <typename P1>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
if (!gin_helper::GetNextArgument(args, 0, true, &a1))
return nullptr;
return callback.Run(a1);
}
template <typename P1, typename P2>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
if (!gin_helper::GetNextArgument(args, 0, true, &a1) ||
!gin_helper::GetNextArgument(args, 0, false, &a2))
return nullptr;
return callback.Run(a1, a2);
}
template <typename P1, typename P2, typename P3>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
if (!gin_helper::GetNextArgument(args, 0, true, &a1) ||
!gin_helper::GetNextArgument(args, 0, false, &a2) ||
!gin_helper::GetNextArgument(args, 0, false, &a3))
return nullptr;
return callback.Run(a1, a2, a3);
}
template <typename P1, typename P2, typename P3, typename P4>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
typename CallbackParamTraits<P4>::LocalType a4;
if (!gin_helper::GetNextArgument(args, 0, true, &a1) ||
!gin_helper::GetNextArgument(args, 0, false, &a2) ||
!gin_helper::GetNextArgument(args, 0, false, &a3) ||
!gin_helper::GetNextArgument(args, 0, false, &a4))
return nullptr;
return callback.Run(a1, a2, a3, a4);
}
template <typename P1, typename P2, typename P3, typename P4, typename P5>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
typename CallbackParamTraits<P4>::LocalType a4;
typename CallbackParamTraits<P5>::LocalType a5;
if (!gin_helper::GetNextArgument(args, 0, true, &a1) ||
!gin_helper::GetNextArgument(args, 0, false, &a2) ||
!gin_helper::GetNextArgument(args, 0, false, &a3) ||
!gin_helper::GetNextArgument(args, 0, false, &a4) ||
!gin_helper::GetNextArgument(args, 0, false, &a5))
return nullptr;
return callback.Run(a1, a2, a3, a4, a5);
}
template <typename P1,
typename P2,
typename P3,
typename P4,
typename P5,
typename P6>
inline WrappableBase* InvokeFactory(
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5, P6)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
typename CallbackParamTraits<P4>::LocalType a4;
typename CallbackParamTraits<P5>::LocalType a5;
typename CallbackParamTraits<P6>::LocalType a6;
if (!gin_helper::GetNextArgument(args, 0, true, &a1) ||
!gin_helper::GetNextArgument(args, 0, false, &a2) ||
!gin_helper::GetNextArgument(args, 0, false, &a3) ||
!gin_helper::GetNextArgument(args, 0, false, &a4) ||
!gin_helper::GetNextArgument(args, 0, false, &a5) ||
!gin_helper::GetNextArgument(args, 0, false, &a6))
return nullptr;
return callback.Run(a1, a2, a3, a4, a5, a6);
}
template <typename Sig>
void InvokeNew(const base::Callback<Sig>& factory,
v8::Isolate* isolate,
gin_helper::Arguments* args) {
if (!args->IsConstructCall()) {
args->ThrowError("Requires constructor call");
return;
}
WrappableBase* object;
{
// Don't continue if the constructor throws an exception.
v8::TryCatch try_catch(isolate);
object = internal::InvokeFactory(args, factory);
if (try_catch.HasCaught()) {
try_catch.ReThrow();
return;
}
}
if (!object)
args->ThrowError();
return;
}
} // namespace internal
// Create a FunctionTemplate that can be "new"ed in JavaScript.
// It is user's responsibility to ensure this function is called for one type
// only ONCE in the program's whole lifetime, otherwise we would have memory
// leak.
template <typename T, typename Sig>
v8::Local<v8::Function> CreateConstructor(
v8::Isolate* isolate,
const base::RepeatingCallback<Sig>& func) {
#ifndef NDEBUG
static bool called = false;
CHECK(!called) << "CreateConstructor can only be called for one type once";
called = true;
#endif
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate, base::BindRepeating(&internal::InvokeNew<Sig>, func));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
return templ->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();
}
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_CONSTRUCTOR_H_

View file

@ -5,7 +5,7 @@
#include "shell/common/gin_helper/destroyable.h"
#include "gin/converter.h"
#include "native_mate/wrappable_base.h"
#include "shell/common/gin_helper/wrappable_base.h"
namespace gin_helper {
@ -20,8 +20,8 @@ void DestroyFunc(const v8::FunctionCallbackInfo<v8::Value>& info) {
if (Destroyable::IsDestroyed(holder))
return;
// TODO(zcbenz): mate::Wrappable will be removed.
delete static_cast<mate::WrappableBase*>(
// TODO(zcbenz): gin_helper::Wrappable will be removed.
delete static_cast<gin_helper::WrappableBase*>(
holder->GetAlignedPointerFromInternalField(0));
holder->SetAlignedPointerInInternalField(0, nullptr);
}

View file

@ -48,8 +48,6 @@ class Dictionary : public gin::Dictionary {
// Differences from the Set method in gin::Dictionary:
// 1. It accepts arbitrary type of key.
// 2. It forces using gin::ConvertFromV8 (would no longer be needed after
// removing native_mate).
template <typename K, typename V>
bool Set(const K& key, const V& val) {
v8::Local<v8::Value> v8_value;

View file

@ -10,8 +10,8 @@
#include "content/public/browser/browser_thread.h"
#include "electron/shell/common/api/api.mojom.h"
#include "native_mate/wrappable.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/wrappable.h"
namespace content {
class RenderFrameHost;
@ -36,9 +36,9 @@ v8::Local<v8::Object> CreateNativeEvent(
// Provide helperers to emit event in JavaScript.
template <typename T>
class EventEmitter : public mate::Wrappable<T> {
class EventEmitter : public gin_helper::Wrappable<T> {
public:
using Base = mate::Wrappable<T>;
using Base = gin_helper::Wrappable<T>;
using ValueArray = std::vector<v8::Local<v8::Value>>;
// Make the convinient methods visible:

View file

@ -55,14 +55,14 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
public:
// Mark the JS object as destroyed.
void MarkDestroyed() {
v8::Local<v8::Object> wrapper = mate::Wrappable<T>::GetWrapper();
v8::Local<v8::Object> wrapper = gin_helper::Wrappable<T>::GetWrapper();
if (!wrapper.IsEmpty()) {
wrapper->SetAlignedPointerInInternalField(0, nullptr);
}
}
bool IsDestroyed() {
v8::Local<v8::Object> wrapper = mate::Wrappable<T>::GetWrapper();
v8::Local<v8::Object> wrapper = gin_helper::Wrappable<T>::GetWrapper();
return wrapper->InternalFieldCount() == 0 ||
wrapper->GetAlignedPointerFromInternalField(0) == nullptr;
}
@ -110,7 +110,7 @@ class TrackableObject : public TrackableObjectBase, public EventEmitter<T> {
~TrackableObject() override { RemoveFromWeakMap(); }
void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper) override {
mate::WrappableBase::InitWith(isolate, wrapper);
gin_helper::WrappableBase::InitWith(isolate, wrapper);
if (!weak_map_) {
weak_map_ = new electron::KeyWeakMap<int32_t>;
}

View file

@ -0,0 +1,89 @@
// Copyright 2013 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/wrappable.h"
#include "base/logging.h"
#include "shell/common/gin_helper/dictionary.h"
namespace gin_helper {
WrappableBase::WrappableBase() = default;
WrappableBase::~WrappableBase() {
if (wrapper_.IsEmpty())
return;
GetWrapper()->SetAlignedPointerInInternalField(0, nullptr);
wrapper_.ClearWeak();
wrapper_.Reset();
}
v8::Local<v8::Object> WrappableBase::GetWrapper() const {
if (!wrapper_.IsEmpty())
return v8::Local<v8::Object>::New(isolate_, wrapper_);
else
return v8::Local<v8::Object>();
}
v8::MaybeLocal<v8::Object> WrappableBase::GetWrapper(
v8::Isolate* isolate) const {
if (!wrapper_.IsEmpty())
return v8::MaybeLocal<v8::Object>(
v8::Local<v8::Object>::New(isolate, wrapper_));
else
return v8::MaybeLocal<v8::Object>();
}
void WrappableBase::InitWithArgs(gin::Arguments* args) {
v8::Local<v8::Object> holder;
args->GetHolder(&holder);
InitWith(args->isolate(), holder);
}
void WrappableBase::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
CHECK(wrapper_.IsEmpty());
isolate_ = isolate;
wrapper->SetAlignedPointerInInternalField(0, this);
wrapper_.Reset(isolate, wrapper);
wrapper_.SetWeak(this, FirstWeakCallback, v8::WeakCallbackType::kParameter);
// Call object._init if we have one.
v8::Local<v8::Function> init;
if (Dictionary(isolate, wrapper).Get("_init", &init))
init->Call(isolate->GetCurrentContext(), wrapper, 0, nullptr).IsEmpty();
AfterInit(isolate);
}
// static
void WrappableBase::FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
wrappable->wrapper_.Reset();
data.SetSecondPassCallback(SecondWeakCallback);
}
// static
void WrappableBase::SecondWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data) {
WrappableBase* wrappable = data.GetParameter();
delete wrappable;
}
namespace internal {
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val) {
if (!val->IsObject())
return nullptr;
v8::Local<v8::Object> obj = v8::Local<v8::Object>::Cast(val);
if (obj->InternalFieldCount() != 1)
return nullptr;
return obj->GetAlignedPointerFromInternalField(0);
}
} // namespace internal
} // namespace gin_helper

View file

@ -0,0 +1,103 @@
// Copyright 2013 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.
#ifndef SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_
#define SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_
#include "base/bind.h"
#include "gin/per_isolate_data.h"
#include "shell/common/gin_helper/constructor.h"
namespace gin_helper {
namespace internal {
void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val);
} // namespace internal
template <typename T>
class Wrappable : public WrappableBase {
public:
Wrappable() = default;
template <typename Sig>
static void SetConstructor(v8::Isolate* isolate,
const base::Callback<Sig>& constructor) {
v8::Local<v8::FunctionTemplate> templ = CreateFunctionTemplate(
isolate, base::Bind(&internal::InvokeNew<Sig>, constructor));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
gin::PerIsolateData::From(isolate)->SetFunctionTemplate(&kWrapperInfo,
templ);
}
static v8::Local<v8::FunctionTemplate> GetConstructor(v8::Isolate* isolate) {
// Fill the object template.
auto* data = gin::PerIsolateData::From(isolate);
auto templ = data->GetFunctionTemplate(&kWrapperInfo);
if (templ.IsEmpty()) {
templ = v8::FunctionTemplate::New(isolate);
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
data->SetFunctionTemplate(&kWrapperInfo, templ);
}
return templ;
}
protected:
// Init the class with T::BuildPrototype.
void Init(v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> templ = GetConstructor(isolate);
// |wrapper| may be empty in some extreme cases, e.g., when
// Object.prototype.constructor is overwritten.
v8::Local<v8::Object> wrapper;
if (!templ->InstanceTemplate()
->NewInstance(isolate->GetCurrentContext())
.ToLocal(&wrapper)) {
// The current wrappable object will be no longer managed by V8. Delete
// this now.
delete this;
return;
}
InitWith(isolate, wrapper);
}
private:
static gin::WrapperInfo kWrapperInfo;
DISALLOW_COPY_AND_ASSIGN(Wrappable);
};
// static
template <typename T>
gin::WrapperInfo Wrappable<T>::kWrapperInfo = {gin::kEmbedderNativeGin};
} // namespace gin_helper
namespace gin {
template <typename T>
struct Converter<
T*,
typename std::enable_if<
std::is_convertible<T*, gin_helper::WrappableBase*>::value>::type> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, T* val) {
if (val)
return val->GetWrapper();
else
return v8::Null(isolate);
}
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
*out = static_cast<T*>(static_cast<gin_helper::WrappableBase*>(
gin_helper::internal::FromV8Impl(isolate, val)));
return *out != nullptr;
}
};
} // namespace gin
#endif // SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_

View file

@ -0,0 +1,69 @@
// Copyright 2013 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.
#ifndef SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_
#define SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_
#include "v8/include/v8.h"
namespace gin {
class Arguments;
} // namespace gin
namespace gin_helper {
// Wrappable is a base class for C++ objects that have corresponding v8 wrapper
// objects. To retain a Wrappable object on the stack, use a gin::Handle.
//
// USAGE:
// // my_class.h
// class MyClass : Wrappable<MyClass> {
// public:
// ...
// };
//
// Subclasses should also typically have private constructors and expose a
// static Create function that returns a gin::Handle. Forcing creators through
// this static Create function will enforce that clients actually create a
// wrapper for the object. If clients fail to create a wrapper for a wrappable
// object, the object will leak because we use the weak callback from the
// wrapper as the signal to delete the wrapped object.
class WrappableBase {
public:
WrappableBase();
virtual ~WrappableBase();
// Retrieve the v8 wrapper object cooresponding to this object.
v8::Local<v8::Object> GetWrapper() const;
v8::MaybeLocal<v8::Object> GetWrapper(v8::Isolate* isolate) const;
// Returns the Isolate this object is created in.
v8::Isolate* isolate() const { return isolate_; }
protected:
// Called after the "_init" method gets called in JavaScript.
virtual void AfterInit(v8::Isolate* isolate) {}
// Bind the C++ class to the JS wrapper.
// This method should only be called by classes using Constructor.
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
// Helper to init with arguments.
void InitWithArgs(gin::Arguments* args);
private:
static void FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data);
static void SecondWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data);
v8::Isolate* isolate_ = nullptr;
v8::Global<v8::Object> wrapper_; // Weak
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
};
} // namespace gin_helper
#endif // SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_