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

@ -355,7 +355,6 @@ source_set("electron_lib") {
"buildflags",
"chromium_src:chrome",
"chromium_src:chrome_spellchecker",
"native_mate",
"shell/common/api:mojo",
"//base:base_static",
"//base/allocator:buildflags",

View file

@ -42,8 +42,6 @@ Electron
| | └── api/ - Javascript API implementation.
| └── renderer/ - Javascript renderer process initialization code.
| └── api/ - Javascript API implementation.
├── native_mate/ - A fork of Chromium's gin library that makes it easier to marshal
| types between C++ and JavaScript.
├── spec/ - Automatic tests.
└── BUILD.gn - Building rules of Electron.
```

View file

@ -433,10 +433,8 @@ filenames = {
"shell/common/api/atom_api_native_image_mac.mm",
"shell/common/api/atom_api_shell.cc",
"shell/common/api/atom_api_v8_util.cc",
"shell/common/api/constructor.h",
"shell/common/api/electron_bindings.cc",
"shell/common/api/electron_bindings.h",
"shell/common/api/constructor.h",
"shell/common/api/features.cc",
"shell/common/application_info.cc",
"shell/common/application_info.h",
@ -496,6 +494,7 @@ filenames = {
"shell/common/gin_helper/arguments.h",
"shell/common/gin_helper/callback.cc",
"shell/common/gin_helper/callback.h",
"shell/common/gin_helper/constructor.h",
"shell/common/gin_helper/destroyable.cc",
"shell/common/gin_helper/destroyable.h",
"shell/common/gin_helper/dictionary.h",
@ -517,6 +516,9 @@ filenames = {
"shell/common/gin_helper/promise.cc",
"shell/common/gin_helper/trackable_object.cc",
"shell/common/gin_helper/trackable_object.h",
"shell/common/gin_helper/wrappable.cc",
"shell/common/gin_helper/wrappable.h",
"shell/common/gin_helper/wrappable_base.h",
"shell/common/heap_snapshot.cc",
"shell/common/heap_snapshot.h",
"shell/common/keyboard_util.cc",

View file

@ -1,32 +0,0 @@
config("native_mate_config") {
include_dirs = [ "." ]
}
source_set("native_mate") {
deps = [
"//base",
"//net",
"//third_party/electron_node:node_lib",
"//v8:v8_headers",
]
public_configs = [ ":native_mate_config" ]
include_dirs = [ ".." ]
sources = [
"native_mate/arguments.cc",
"native_mate/arguments.h",
"native_mate/compat.h",
"native_mate/constructor.h",
"native_mate/converter.cc",
"native_mate/converter.h",
"native_mate/dictionary.cc",
"native_mate/dictionary.h",
"native_mate/function_template.cc",
"native_mate/function_template.h",
"native_mate/handle.h",
"native_mate/object_template_builder.cc",
"native_mate/object_template_builder_deprecated.h",
"native_mate/wrappable.cc",
"native_mate/wrappable.h",
"native_mate/wrappable_base.h",
]
}

View file

@ -1,27 +0,0 @@
// Copyright 2014 The Chromium Authors. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google Inc. nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

View file

@ -1,56 +0,0 @@
> A fork of Chromium's [gin library][chromium-gin-lib] that makes it easier to
> marshal types between C++ and JavaScript.
# Overview
`native-mate` was forked from `gin` so that it could be used in
[Electron][electron] without conflicting with Node's Environment. It has also
been extended to allow Electron to create classes in JavaScript.
With the help of Chromium's `base` library, `native-mate` makes writing JS
bindings very easy, and most of the intricate details of converting V8 types
to C++ types and back are taken care of auto-magically. In most cases there's
no need to use the raw V8 API to implement an API binding.
For example, here's an API binding that doesn't use `native-mate`:
```c++
// static
void Shell::OpenItem(const v8::FunctionCallbackInfo<v8::Value>& args) {
base::FilePath file_path;
if (!FromV8Arguments(args, &file_path))
return node::ThrowTypeError("Bad argument");
platform_util::OpenItem(file_path);
}
// static
void Shell::Initialize(v8::Handle<v8::Object> target) {
NODE_SET_METHOD(target, "openItem", OpenItem);
}
```
And here's the same API binding using `native-mate`:
```c++
void Initialize(v8::Handle<v8::Object> exports) {
mate::Dictionary dict(v8::Isolate::GetCurrent(), exports);
dict.SetMethod("openItem", &platform_util::OpenItem);
}
```
# Code Structure
* `converter.h` - Templatized JS<->C++ conversion routines for many common C++
types. You can define your own by specializing `Converter`.
* `function_template.h` - Create JavaScript functions that dispatch to any C++
function, member function pointer, or `base::Callback`.
* `object_template_builder_deprecated.h` - A handy utility for creation of `v8::ObjectTemplate`.
* `wrappable.h` - Base class for C++ classes that want to be owned by the V8 GC.
Wrappable objects are automatically deleted when GC discovers that nothing in
the V8 heap refers to them. This is also an easy way to expose C++ objects to
JavaScript.
[chromium-gin-lib]: https://code.google.com/p/chromium/codesearch#chromium/src/gin/README.md&sq=package:chromium
[electron]: https://electronjs.org/

View file

@ -1,61 +0,0 @@
// 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 "native_mate/arguments.h"
#include "base/strings/stringprintf.h"
#include "native_mate/converter.h"
namespace mate {
namespace {
std::string V8TypeAsString(v8::Isolate* isolate, v8::Local<v8::Value> value) {
if (value.IsEmpty())
return "<empty handle>";
v8::MaybeLocal<v8::String> details =
value->ToDetailString(isolate->GetCurrentContext());
std::string result;
if (!details.IsEmpty())
ConvertFromV8(isolate, details.ToLocalChecked(), &result);
return result;
}
} // namespace
Arguments::Arguments() = default;
Arguments::Arguments(const v8::FunctionCallbackInfo<v8::Value>& info)
: isolate_(info.GetIsolate()), info_(&info) {}
Arguments::~Arguments() = default;
v8::Local<v8::Value> Arguments::PeekNext() const {
if (next_ >= info_->Length())
return v8::Local<v8::Value>();
return (*info_)[next_];
}
v8::Local<v8::Value> Arguments::ThrowError() const {
if (insufficient_arguments_)
return ThrowTypeError("Insufficient number of arguments.");
return ThrowTypeError(base::StringPrintf(
"Error processing argument at index %d, conversion failure from %s",
next_, V8TypeAsString(isolate_, (*info_)[next_]).c_str()));
}
v8::Local<v8::Value> Arguments::ThrowError(const std::string& message) const {
isolate_->ThrowException(v8::Exception::Error(StringToV8(isolate_, message)));
return v8::Undefined(isolate_);
}
v8::Local<v8::Value> Arguments::ThrowTypeError(
const std::string& message) const {
isolate_->ThrowException(
v8::Exception::TypeError(StringToV8(isolate_, message)));
return v8::Undefined(isolate_);
}
} // namespace mate

View file

@ -1,111 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_ARGUMENTS_H_
#define NATIVE_MATE_NATIVE_MATE_ARGUMENTS_H_
#include <string>
#include <vector>
#include "base/macros.h"
#include "base/optional.h"
#include "native_mate/converter.h"
// =============================== NOTICE ===============================
// Do not add code here, native_mate is being removed. Any new code
// should use gin instead.
namespace mate {
// Arguments is a wrapper around v8::FunctionCallbackInfo that integrates
// with Converter to make it easier to marshall arguments and return values
// between V8 and C++.
class Arguments {
public:
Arguments();
explicit Arguments(const v8::FunctionCallbackInfo<v8::Value>& info);
~Arguments();
v8::Local<v8::Object> GetHolder() const { return info_->Holder(); }
template <typename T>
bool GetHolder(T* out) {
return mate::ConvertFromV8(isolate_, info_->Holder(), out);
}
template <typename T>
bool GetData(T* out) {
return ConvertFromV8(isolate_, info_->Data(), out);
}
template <typename T>
bool GetNext(base::Optional<T>* out) {
if (next_ >= info_->Length())
return true;
v8::Local<v8::Value> val = (*info_)[next_];
bool success = ConvertFromV8(isolate_, val, out);
if (success)
next_++;
return success;
}
template <typename T>
bool GetNext(T* out) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
v8::Local<v8::Value> val = (*info_)[next_];
bool success = mate::ConvertFromV8(isolate_, val, out);
if (success)
next_++;
return success;
}
template <typename T>
bool GetRemaining(std::vector<T>* out) {
if (next_ >= info_->Length()) {
insufficient_arguments_ = true;
return false;
}
int remaining = info_->Length() - next_;
out->resize(remaining);
for (int i = 0; i < remaining; ++i) {
v8::Local<v8::Value> val = (*info_)[next_++];
if (!ConvertFromV8(isolate_, val, &out->at(i)))
return false;
}
return true;
}
v8::Local<v8::Object> GetThis() { return info_->This(); }
bool IsConstructCall() const { return info_->IsConstructCall(); }
int Length() const { return info_->Length(); }
template <typename T>
void Return(const T& val) {
info_->GetReturnValue().Set(ConvertToV8(isolate_, val));
}
v8::Local<v8::Value> PeekNext() const;
v8::Local<v8::Value> ThrowError() const;
v8::Local<v8::Value> ThrowError(const std::string& message) const;
v8::Local<v8::Value> ThrowTypeError(const std::string& message) const;
v8::Isolate* isolate() const { return isolate_; }
const v8::FunctionCallbackInfo<v8::Value>& info() const { return *info_; }
private:
v8::Isolate* isolate_ = nullptr;
const v8::FunctionCallbackInfo<v8::Value>* info_ = nullptr;
int next_ = 0;
bool insufficient_arguments_ = false;
};
} // namespace mate
#endif // NATIVE_MATE_NATIVE_MATE_ARGUMENTS_H_

View file

@ -1,269 +0,0 @@
// 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 "native_mate/converter.h"
#include "v8/include/v8.h"
namespace mate {
namespace {
template <typename T, typename U>
bool FromMaybe(v8::Maybe<T> maybe, U* out) {
if (maybe.IsNothing())
return false;
*out = static_cast<U>(maybe.FromJust());
return true;
}
} // namespace
v8::Local<v8::Value> Converter<bool>::ToV8(v8::Isolate* isolate, bool val) {
return v8::Boolean::New(isolate, val);
}
bool Converter<bool>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
bool* out) {
if (!val->IsBoolean())
return false;
*out = val.As<v8::Boolean>()->Value();
return true;
}
#if !defined(OS_LINUX) && !defined(OS_FREEBSD)
v8::Local<v8::Value> Converter<unsigned long>::ToV8( // NOLINT(runtime/int)
v8::Isolate* isolate,
unsigned long val) { // NOLINT(runtime/int)
return v8::Integer::New(isolate, val);
}
bool Converter<unsigned long>::FromV8( // NOLINT(runtime/int)
v8::Isolate* isolate,
v8::Local<v8::Value> val,
unsigned long* out) { // NOLINT(runtime/int)
if (!val->IsNumber())
return false;
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
}
#endif
v8::Local<v8::Value> Converter<int32_t>::ToV8(v8::Isolate* isolate,
int32_t val) {
return v8::Integer::New(isolate, val);
}
bool Converter<int32_t>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int32_t* out) {
if (!val->IsInt32())
return false;
*out = val.As<v8::Int32>()->Value();
return true;
}
v8::Local<v8::Value> Converter<uint32_t>::ToV8(v8::Isolate* isolate,
uint32_t val) {
return v8::Integer::NewFromUnsigned(isolate, val);
}
bool Converter<uint32_t>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint32_t* out) {
if (!val->IsUint32())
return false;
*out = val.As<v8::Uint32>()->Value();
return true;
}
v8::Local<v8::Value> Converter<int64_t>::ToV8(v8::Isolate* isolate,
int64_t val) {
return v8::Number::New(isolate, static_cast<double>(val));
}
bool Converter<int64_t>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int64_t* out) {
if (!val->IsNumber())
return false;
// Even though IntegerValue returns int64_t, JavaScript cannot represent
// the full precision of int64_t, which means some rounding might occur.
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
}
v8::Local<v8::Value> Converter<uint64_t>::ToV8(v8::Isolate* isolate,
uint64_t val) {
return v8::Number::New(isolate, static_cast<double>(val));
}
bool Converter<uint64_t>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint64_t* out) {
if (!val->IsNumber())
return false;
return FromMaybe(val->IntegerValue(isolate->GetCurrentContext()), out);
}
v8::Local<v8::Value> Converter<float>::ToV8(v8::Isolate* isolate, float val) {
return v8::Number::New(isolate, val);
}
bool Converter<float>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
float* out) {
if (!val->IsNumber())
return false;
*out = static_cast<float>(val.As<v8::Number>()->Value());
return true;
}
v8::Local<v8::Value> Converter<double>::ToV8(v8::Isolate* isolate, double val) {
return v8::Number::New(isolate, val);
}
bool Converter<double>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
double* out) {
if (!val->IsNumber())
return false;
*out = val.As<v8::Number>()->Value();
return true;
}
v8::Local<v8::Value> Converter<const char*>::ToV8(v8::Isolate* isolate,
const char* val) {
return v8::String::NewFromUtf8(isolate, val, v8::NewStringType::kNormal)
.ToLocalChecked();
}
v8::Local<v8::Value> Converter<base::StringPiece>::ToV8(v8::Isolate* isolate,
base::StringPiece val) {
return v8::String::NewFromUtf8(isolate, val.data(),
v8::NewStringType::kNormal,
static_cast<uint32_t>(val.length()))
.ToLocalChecked();
}
v8::Local<v8::Value> Converter<std::string>::ToV8(v8::Isolate* isolate,
const std::string& val) {
return Converter<base::StringPiece>::ToV8(isolate, val);
}
bool Converter<std::string>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::string* out) {
if (!val->IsString())
return false;
v8::Local<v8::String> str = v8::Local<v8::String>::Cast(val);
int length = str->Utf8Length(isolate);
out->resize(length);
str->WriteUtf8(isolate, &(*out)[0], length, nullptr,
v8::String::NO_NULL_TERMINATION);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::Function>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::Function> val) {
return val;
}
bool Converter<v8::Local<v8::Function>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Function>* out) {
if (!val->IsFunction())
return false;
*out = v8::Local<v8::Function>::Cast(val);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::Object>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::Object> val) {
return val;
}
bool Converter<v8::Local<v8::Object>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Object>* out) {
if (!val->IsObject())
return false;
*out = v8::Local<v8::Object>::Cast(val);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::String>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::String> val) {
return val;
}
bool Converter<v8::Local<v8::String>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::String>* out) {
if (!val->IsString())
return false;
*out = v8::Local<v8::String>::Cast(val);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::External>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::External> val) {
return val;
}
bool Converter<v8::Local<v8::External>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::External>* out) {
if (!val->IsExternal())
return false;
*out = v8::Local<v8::External>::Cast(val);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::Array>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::Array> val) {
return val;
}
bool Converter<v8::Local<v8::Array>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Array>* out) {
if (!val->IsArray())
return false;
*out = v8::Local<v8::Array>::Cast(val);
return true;
}
v8::Local<v8::Value> Converter<v8::Local<v8::Value>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::Value> val) {
return val;
}
v8::Local<v8::Promise> Converter<v8::Local<v8::Promise>>::ToV8(
v8::Isolate* isolate,
v8::Local<v8::Promise> val) {
return val;
}
bool Converter<v8::Local<v8::Value>>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Value>* out) {
*out = val;
return true;
}
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
base::StringPiece val) {
return v8::String::NewFromUtf8(isolate, val.data(),
v8::NewStringType::kInternalized,
static_cast<uint32_t>(val.length()))
.ToLocalChecked();
}
} // namespace mate

View file

@ -1,351 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_CONVERTER_H_
#define NATIVE_MATE_NATIVE_MATE_CONVERTER_H_
#include <map>
#include <set>
#include <string>
#include <type_traits>
#include <utility>
#include <vector>
#include "base/strings/string_piece.h"
#include "gin/converter.h"
#include "v8/include/v8.h"
namespace mate {
template <typename KeyType>
bool SetProperty(v8::Isolate* isolate,
v8::Local<v8::Object> object,
KeyType key,
v8::Local<v8::Value> value) {
auto maybe = object->Set(isolate->GetCurrentContext(), key, value);
return !maybe.IsNothing() && maybe.FromJust();
}
template <typename T>
struct ToV8ReturnsMaybe {
static const bool value = false;
};
template <typename T, typename Enable = void>
struct Converter {};
template <>
struct Converter<void*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, void* val) {
return v8::Undefined(isolate);
}
};
template <>
struct Converter<std::nullptr_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, std::nullptr_t val) {
return v8::Null(isolate);
}
};
template <>
struct Converter<bool> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, bool val);
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, bool* out);
};
#if !defined(OS_LINUX) && !defined(OS_FREEBSD)
template <>
struct Converter<unsigned long> { // NOLINT(runtime/int)
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
unsigned long val); // NOLINT(runtime/int)
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
unsigned long* out); // NOLINT(runtime/int)
};
#endif
template <>
struct Converter<int32_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, int32_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int32_t* out);
};
template <>
struct Converter<uint32_t> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, uint32_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint32_t* out);
};
template <>
struct Converter<int64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, int64_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
int64_t* out);
};
template <>
struct Converter<uint64_t> {
// Warning: JavaScript cannot represent 64 integers precisely.
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, uint64_t val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
uint64_t* out);
};
template <>
struct Converter<float> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, float val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
float* out);
};
template <>
struct Converter<double> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, double val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
double* out);
};
template <>
struct Converter<const char*> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, const char* val);
};
template <>
struct Converter<base::StringPiece> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, base::StringPiece val);
// No conversion out is possible because StringPiece does not contain storage.
};
template <>
struct Converter<std::string> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::string& val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::string* out);
};
v8::Local<v8::String> StringToSymbol(v8::Isolate* isolate,
base::StringPiece input);
template <>
struct Converter<v8::Local<v8::Function>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Function> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Function>* out);
};
template <>
struct Converter<v8::Local<v8::Object>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Object> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Object>* out);
};
template <>
struct Converter<v8::Local<v8::String>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::String> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::String>* out);
};
template <>
struct Converter<v8::Local<v8::External>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::External> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::External>* out);
};
template <>
struct Converter<v8::Local<v8::Array>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Array> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Array>* out);
};
template <>
struct Converter<v8::Local<v8::Value>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
v8::Local<v8::Value> val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
v8::Local<v8::Value>* out);
};
template <>
struct Converter<v8::Local<v8::Promise>> {
static v8::Local<v8::Promise> ToV8(v8::Isolate* isolate,
v8::Local<v8::Promise> val);
// static bool FromV8(v8::Isolate* isolate,
// v8::Local<v8::Value> val,
// v8::Local<v8::Value>* out);
};
template <typename T>
struct Converter<std::vector<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::vector<T>& val) {
v8::Local<v8::Array> result(
v8::Array::New(isolate, static_cast<int>(val.size())));
auto context = isolate->GetCurrentContext();
for (size_t i = 0; i < val.size(); ++i) {
result
->Set(context, static_cast<int>(i),
Converter<T>::ToV8(isolate, val[i]))
.Check();
}
return result;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::vector<T>* out) {
if (!val->IsArray())
return false;
auto context = isolate->GetCurrentContext();
std::vector<T> result;
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
T item;
if (!Converter<T>::FromV8(isolate,
array->Get(context, i).ToLocalChecked(), &item))
return false;
result.push_back(item);
}
out->swap(result);
return true;
}
};
template <typename T>
struct Converter<std::set<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const std::set<T>& val) {
v8::Local<v8::Array> result(
v8::Array::New(isolate, static_cast<int>(val.size())));
auto context = isolate->GetCurrentContext();
typename std::set<T>::const_iterator it;
int i;
for (i = 0, it = val.begin(); it != val.end(); ++it, ++i)
result->Set(context, i, Converter<T>::ToV8(isolate, *it)).Check();
return result;
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
std::set<T>* out) {
if (!val->IsArray())
return false;
auto context = isolate->GetCurrentContext();
std::set<T> result;
v8::Local<v8::Array> array(v8::Local<v8::Array>::Cast(val));
uint32_t length = array->Length();
for (uint32_t i = 0; i < length; ++i) {
T item;
if (!Converter<T>::FromV8(isolate,
array->Get(context, i).ToLocalChecked(), &item))
return false;
result.insert(item);
}
out->swap(result);
return true;
}
};
// Convenience functions that deduce T.
template <typename T>
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, const T& input) {
return Converter<T>::ToV8(isolate, input);
}
template <typename T>
v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate, T&& input) {
return Converter<typename std::remove_reference<T>::type>::ToV8(
isolate, std::move(input));
}
inline v8::Local<v8::Value> ConvertToV8(v8::Isolate* isolate,
const char* input) {
return Converter<const char*>::ToV8(isolate, input);
}
template <typename T>
v8::MaybeLocal<v8::Value> ConvertToV8(v8::Local<v8::Context> context,
const T& input) {
return Converter<T>::ToV8(context, input);
}
template <typename T, bool = ToV8ReturnsMaybe<T>::value>
struct ToV8Traits;
template <typename T>
struct ToV8Traits<T, true> {
static bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
auto maybe = ConvertToV8(isolate->GetCurrentContext(), input);
if (maybe.IsEmpty())
return false;
*output = maybe.ToLocalChecked();
return true;
}
};
template <typename T>
struct ToV8Traits<T, false> {
static bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
*output = ConvertToV8(isolate, input);
return true;
}
};
template <typename T>
bool TryConvertToV8(v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
return ToV8Traits<T>::TryConvertToV8(isolate, input, output);
}
template <typename T>
bool ConvertFromV8(v8::Isolate* isolate,
v8::Local<v8::Value> input,
T* result) {
return Converter<T>::FromV8(isolate, input, result);
}
inline v8::Local<v8::String> StringToV8(v8::Isolate* isolate,
base::StringPiece input) {
return ConvertToV8(isolate, input).As<v8::String>();
}
} // namespace mate
#endif // NATIVE_MATE_NATIVE_MATE_CONVERTER_H_

View file

@ -1,40 +0,0 @@
// 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 "native_mate/dictionary.h"
namespace mate {
Dictionary::Dictionary() = default;
Dictionary::Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object)
: isolate_(isolate), object_(object) {}
Dictionary::Dictionary(const Dictionary& other) = default;
Dictionary::~Dictionary() = default;
Dictionary Dictionary::CreateEmpty(v8::Isolate* isolate) {
return Dictionary(isolate, v8::Object::New(isolate));
}
v8::Local<v8::Object> Dictionary::GetHandle() const {
return object_;
}
v8::Local<v8::Value> Converter<Dictionary>::ToV8(v8::Isolate* isolate,
Dictionary val) {
return val.GetHandle();
}
bool Converter<Dictionary>::FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
Dictionary* out) {
if (!val->IsObject() || val->IsFunction())
return false;
*out = Dictionary(isolate, v8::Local<v8::Object>::Cast(val));
return true;
}
} // namespace mate

View file

@ -1,136 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_DICTIONARY_H_
#define NATIVE_MATE_NATIVE_MATE_DICTIONARY_H_
#include "native_mate/converter.h"
#include "native_mate/object_template_builder_deprecated.h"
namespace mate {
namespace internal {
// Returns true if |maybe| is both a value, and that value is true.
inline bool IsTrue(v8::Maybe<bool> maybe) {
return maybe.IsJust() && maybe.FromJust();
}
} // namespace internal
// Dictionary is useful when writing bindings for a function that either
// receives an arbitrary JavaScript object as an argument or returns an
// arbitrary JavaScript object as a result. For example, Dictionary is useful
// when you might use the |dictionary| type in WebIDL:
//
// http://heycam.github.io/webidl/#idl-dictionaries
//
// WARNING: You cannot retain a Dictionary object in the heap. The underlying
// storage for Dictionary is tied to the closest enclosing
// v8::HandleScope. Generally speaking, you should store a Dictionary
// on the stack.
//
class Dictionary {
public:
Dictionary();
Dictionary(v8::Isolate* isolate, v8::Local<v8::Object> object);
Dictionary(const Dictionary& other);
virtual ~Dictionary();
static Dictionary CreateEmpty(v8::Isolate* isolate);
template <typename T>
bool Get(base::StringPiece key, T* out) const {
// Check for existence before getting, otherwise this method will always
// returns true when T == v8::Local<v8::Value>.
v8::Local<v8::Context> context = isolate_->GetCurrentContext();
v8::Local<v8::String> v8_key = StringToV8(isolate_, key);
if (!internal::IsTrue(GetHandle()->Has(context, v8_key)))
return false;
v8::Local<v8::Value> val;
if (!GetHandle()->Get(context, v8_key).ToLocal(&val))
return false;
return ConvertFromV8(isolate_, val, out);
}
template <typename T>
bool Set(base::StringPiece key, const T& val) {
v8::Local<v8::Value> v8_value;
if (!TryConvertToV8(isolate_, val, &v8_value))
return false;
v8::Maybe<bool> result = GetHandle()->Set(
isolate_->GetCurrentContext(), StringToV8(isolate_, key), v8_value);
return !result.IsNothing() && result.FromJust();
}
template <typename T>
bool SetReadOnly(base::StringPiece key, T val) {
v8::Local<v8::Value> v8_value;
if (!TryConvertToV8(isolate_, val, &v8_value))
return false;
v8::Maybe<bool> result = GetHandle()->DefineOwnProperty(
isolate_->GetCurrentContext(), StringToV8(isolate_, key), v8_value,
v8::ReadOnly);
return !result.IsNothing() && result.FromJust();
}
template <typename T>
bool SetMethod(base::StringPiece key, const T& callback) {
return GetHandle()
->Set(isolate_->GetCurrentContext(), StringToV8(isolate_, key),
CallbackTraits<T>::CreateTemplate(isolate_, callback)
->GetFunction(isolate_->GetCurrentContext())
.ToLocalChecked())
.ToChecked();
}
bool Delete(base::StringPiece key) {
v8::Maybe<bool> result = GetHandle()->Delete(isolate_->GetCurrentContext(),
StringToV8(isolate_, key));
return !result.IsNothing() && result.FromJust();
}
bool IsEmpty() const { return isolate() == NULL; }
virtual v8::Local<v8::Object> GetHandle() const;
v8::Isolate* isolate() const { return isolate_; }
protected:
v8::Isolate* isolate_ = nullptr;
private:
v8::Local<v8::Object> object_;
};
template <>
struct Converter<Dictionary> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, Dictionary val);
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
Dictionary* out);
};
} // namespace mate
namespace gin {
// Keep compatibility with gin.
template <>
struct Converter<mate::Dictionary> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const mate::Dictionary& in) {
return mate::ConvertToV8(isolate, in);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
mate::Dictionary* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // NATIVE_MATE_NATIVE_MATE_DICTIONARY_H_

View file

@ -1,40 +0,0 @@
// 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 "native_mate/function_template.h"
namespace mate {
namespace internal {
CallbackHolderBase::CallbackHolderBase(v8::Isolate* isolate)
: v8_ref_(isolate, v8::External::New(isolate, this)) {
v8_ref_.SetWeak(this, &CallbackHolderBase::FirstWeakCallback,
v8::WeakCallbackType::kParameter);
}
CallbackHolderBase::~CallbackHolderBase() {
DCHECK(v8_ref_.IsEmpty());
}
v8::Local<v8::External> CallbackHolderBase::GetHandle(v8::Isolate* isolate) {
return v8::Local<v8::External>::New(isolate, v8_ref_);
}
// static
void CallbackHolderBase::FirstWeakCallback(
const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
data.GetParameter()->v8_ref_.Reset();
data.SetSecondPassCallback(SecondWeakCallback);
}
// static
void CallbackHolderBase::SecondWeakCallback(
const v8::WeakCallbackInfo<CallbackHolderBase>& data) {
delete data.GetParameter();
}
} // namespace internal
} // namespace mate

View file

@ -1,285 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_
#define NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_
#include "base/callback.h"
#include "native_mate/arguments.h"
#include "native_mate/wrappable_base.h"
#include "shell/common/gin_helper/destroyable.h"
#include "shell/common/gin_helper/error_thrower.h"
// =============================== NOTICE ===============================
// Do not add code here, native_mate is being removed. Any new code
// should use gin instead.
namespace mate {
enum CreateFunctionTemplateFlags {
HolderIsFirstArgument = 1 << 0,
};
namespace internal {
template <typename T>
struct CallbackParamTraits {
typedef T LocalType;
};
template <typename T>
struct CallbackParamTraits<const T&> {
typedef T LocalType;
};
template <typename T>
struct CallbackParamTraits<const T*> {
typedef T* LocalType;
};
// CallbackHolder and CallbackHolderBase are used to pass a base::Callback from
// CreateFunctionTemplate through v8 (via v8::FunctionTemplate) to
// DispatchToCallback, where it is invoked.
// This simple base class is used so that we can share a single object template
// among every CallbackHolder instance.
class CallbackHolderBase {
public:
v8::Local<v8::External> GetHandle(v8::Isolate* isolate);
protected:
explicit CallbackHolderBase(v8::Isolate* isolate);
virtual ~CallbackHolderBase();
private:
static void FirstWeakCallback(
const v8::WeakCallbackInfo<CallbackHolderBase>& data);
static void SecondWeakCallback(
const v8::WeakCallbackInfo<CallbackHolderBase>& data);
v8::Global<v8::External> v8_ref_;
DISALLOW_COPY_AND_ASSIGN(CallbackHolderBase);
};
template <typename Sig>
class CallbackHolder : public CallbackHolderBase {
public:
CallbackHolder(v8::Isolate* isolate,
const base::Callback<Sig>& callback,
int flags)
: CallbackHolderBase(isolate), callback(callback), flags(flags) {}
base::Callback<Sig> callback;
int flags = 0;
private:
virtual ~CallbackHolder() = default;
DISALLOW_COPY_AND_ASSIGN(CallbackHolder);
};
template <typename T>
bool GetNextArgument(Arguments* args,
int create_flags,
bool is_first,
T* result) {
if (is_first && (create_flags & HolderIsFirstArgument) != 0) {
return args->GetHolder(result);
} else {
return args->GetNext(result);
}
}
// For advanced use cases, we allow callers to request the unparsed Arguments
// object and poke around in it directly.
inline bool GetNextArgument(Arguments* args,
int create_flags,
bool is_first,
Arguments* result) {
*result = *args;
return true;
}
inline bool GetNextArgument(Arguments* args,
int create_flags,
bool is_first,
Arguments** result) {
*result = args;
return true;
}
// It's common for clients to just need the isolate, so we make that easy.
inline bool GetNextArgument(Arguments* args,
int create_flags,
bool is_first,
v8::Isolate** result) {
*result = args->isolate();
return true;
}
// Allow clients to pass a util::Error to throw errors if they
// don't need the full mate::Arguments
inline bool GetNextArgument(Arguments* args,
int create_flags,
bool is_first,
gin_helper::ErrorThrower* result) {
*result = gin_helper::ErrorThrower(args->isolate());
return true;
}
// Classes for generating and storing an argument pack of integer indices
// (based on well-known "indices trick", see: http://goo.gl/bKKojn):
template <size_t... indices>
struct IndicesHolder {};
template <size_t requested_index, size_t... indices>
struct IndicesGenerator {
using type = typename IndicesGenerator<requested_index - 1,
requested_index - 1,
indices...>::type;
};
template <size_t... indices>
struct IndicesGenerator<0, indices...> {
using type = IndicesHolder<indices...>;
};
// Class template for extracting and storing single argument for callback
// at position |index|.
template <size_t index, typename ArgType>
struct ArgumentHolder {
using ArgLocalType = typename CallbackParamTraits<ArgType>::LocalType;
ArgLocalType value;
bool ok = false;
ArgumentHolder(Arguments* args, int create_flags) {
if (index == 0 && (create_flags & HolderIsFirstArgument) &&
gin_helper::Destroyable::IsDestroyed(args->GetHolder())) {
args->ThrowError("Object has been destroyed");
return;
}
ok = GetNextArgument(args, create_flags, index == 0, &value);
if (!ok) {
// Ideally we would include the expected c++ type in the error
// message which we can access via typeid(ArgType).name()
// however we compile with no-rtti, which disables typeid.
args->ThrowError();
}
}
};
// Class template for converting arguments from JavaScript to C++ and running
// the callback with them.
template <typename IndicesType, typename... ArgTypes>
class Invoker {};
template <size_t... indices, typename... ArgTypes>
class Invoker<IndicesHolder<indices...>, ArgTypes...>
: public ArgumentHolder<indices, ArgTypes>... {
public:
// Invoker<> inherits from ArgumentHolder<> for each argument.
// C++ has always been strict about the class initialization order,
// so it is guaranteed ArgumentHolders will be initialized (and thus, will
// extract arguments from Arguments) in the right order.
Invoker(Arguments* args, int create_flags)
: ArgumentHolder<indices, ArgTypes>(args, create_flags)..., args_(args) {
// GCC thinks that create_flags is going unused, even though the
// expansion above clearly makes use of it. Per jyasskin@, casting
// to void is the commonly accepted way to convince the compiler
// that you're actually using a parameter/varible.
(void)create_flags;
}
bool IsOK() { return And(ArgumentHolder<indices, ArgTypes>::ok...); }
template <typename ReturnType>
void DispatchToCallback(base::Callback<ReturnType(ArgTypes...)> callback) {
v8::MicrotasksScope script_scope(args_->isolate(),
v8::MicrotasksScope::kRunMicrotasks);
args_->Return(callback.Run(ArgumentHolder<indices, ArgTypes>::value...));
}
// In C++, you can declare the function foo(void), but you can't pass a void
// expression to foo. As a result, we must specialize the case of Callbacks
// that have the void return type.
void DispatchToCallback(base::Callback<void(ArgTypes...)> callback) {
v8::MicrotasksScope script_scope(args_->isolate(),
v8::MicrotasksScope::kRunMicrotasks);
callback.Run(ArgumentHolder<indices, ArgTypes>::value...);
}
private:
static bool And() { return true; }
template <typename... T>
static bool And(bool arg1, T... args) {
return arg1 && And(args...);
}
Arguments* args_;
};
// DispatchToCallback converts all the JavaScript arguments to C++ types and
// invokes the base::Callback.
template <typename Sig>
struct Dispatcher {};
template <typename ReturnType, typename... ArgTypes>
struct Dispatcher<ReturnType(ArgTypes...)> {
static void DispatchToCallback(
const v8::FunctionCallbackInfo<v8::Value>& info) {
Arguments args(info);
v8::Local<v8::External> v8_holder;
args.GetData(&v8_holder);
CallbackHolderBase* holder_base =
reinterpret_cast<CallbackHolderBase*>(v8_holder->Value());
typedef CallbackHolder<ReturnType(ArgTypes...)> HolderT;
HolderT* holder = static_cast<HolderT*>(holder_base);
using Indices = typename IndicesGenerator<sizeof...(ArgTypes)>::type;
Invoker<Indices, ArgTypes...> invoker(&args, holder->flags);
if (invoker.IsOK())
invoker.DispatchToCallback(holder->callback);
}
};
} // namespace internal
// CreateFunctionTemplate creates a v8::FunctionTemplate that will create
// JavaScript functions that execute a provided C++ function or base::Callback.
// JavaScript arguments are automatically converted via gin::Converter, as is
// the return value of the C++ function, if any.
//
// NOTE: V8 caches FunctionTemplates for a lifetime of a web page for its own
// internal reasons, thus it is generally a good idea to cache the template
// returned by this function. Otherwise, repeated method invocations from JS
// will create substantial memory leaks. See http://crbug.com/463487.
template <typename Sig>
v8::Local<v8::FunctionTemplate> CreateFunctionTemplate(
v8::Isolate* isolate,
const base::Callback<Sig> callback,
int callback_flags = 0) {
typedef internal::CallbackHolder<Sig> HolderT;
HolderT* holder = new HolderT(isolate, callback, callback_flags);
return v8::FunctionTemplate::New(
isolate, &internal::Dispatcher<Sig>::DispatchToCallback,
ConvertToV8<v8::Local<v8::External>>(isolate,
holder->GetHandle(isolate)));
}
// CreateFunctionHandler installs a CallAsFunction handler on the given
// object template that forwards to a provided C++ function or base::Callback.
template <typename Sig>
void CreateFunctionHandler(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> tmpl,
const base::Callback<Sig> callback,
int callback_flags = 0) {
typedef internal::CallbackHolder<Sig> HolderT;
HolderT* holder = new HolderT(isolate, callback, callback_flags);
tmpl->SetCallAsFunctionHandler(&internal::Dispatcher<Sig>::DispatchToCallback,
ConvertToV8<v8::Local<v8::External>>(
isolate, holder->GetHandle(isolate)));
}
} // namespace mate
#endif // NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_

View file

@ -1,90 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_HANDLE_H_
#define NATIVE_MATE_NATIVE_MATE_HANDLE_H_
#include "native_mate/converter.h"
namespace mate {
// You can use mate::Handle on the stack to retain a mate::Wrappable object.
// Currently we don't have a mechanism for retaining a mate::Wrappable object
// in the C++ heap because strong references from C++ to V8 can cause memory
// leaks.
template <typename T>
class Handle {
public:
Handle() = default;
Handle(v8::Local<v8::Object> wrapper, T* object)
: wrapper_(wrapper), object_(object) {}
bool IsEmpty() const { return !object_; }
void Clear() {
wrapper_.Clear();
object_ = NULL;
}
T* operator->() const { return object_; }
v8::Local<v8::Object> ToV8() const { return wrapper_; }
T* get() const { return object_; }
private:
v8::Local<v8::Object> wrapper_;
T* object_ = nullptr;
};
template <typename T>
struct Converter<mate::Handle<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const mate::Handle<T>& val) {
return val.ToV8();
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
mate::Handle<T>* out) {
T* object = NULL;
if (val->IsNull() || val->IsUndefined()) {
*out = mate::Handle<T>();
return true;
}
if (!Converter<T*>::FromV8(isolate, val, &object)) {
return false;
}
v8::Local<v8::Context> context = isolate->GetCurrentContext();
*out = mate::Handle<T>(val->ToObject(context).ToLocalChecked(), object);
return true;
}
};
// This function is a convenient way to create a handle from a raw pointer
// without having to write out the type of the object explicitly.
template <typename T>
mate::Handle<T> CreateHandle(v8::Isolate* isolate, T* object) {
return mate::Handle<T>(object->GetWrapper(), object);
}
} // namespace mate
namespace gin {
// Keep compatibility with gin.
template <typename T>
struct Converter<mate::Handle<T>> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
const mate::Handle<T>& in) {
return mate::ConvertToV8(isolate, in);
}
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
mate::Handle<T>* out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
} // namespace gin
#endif // NATIVE_MATE_NATIVE_MATE_HANDLE_H_

View file

@ -1,37 +0,0 @@
// 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 "native_mate/object_template_builder_deprecated.h"
namespace mate {
ObjectTemplateBuilder::ObjectTemplateBuilder(
v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ)
: isolate_(isolate), template_(templ) {}
ObjectTemplateBuilder::~ObjectTemplateBuilder() = default;
ObjectTemplateBuilder& ObjectTemplateBuilder::SetImpl(base::StringPiece name,
v8::Local<v8::Data> val) {
template_->Set(StringToSymbol(isolate_, name), val);
return *this;
}
ObjectTemplateBuilder& ObjectTemplateBuilder::SetPropertyImpl(
base::StringPiece name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter) {
template_->SetAccessorProperty(StringToSymbol(isolate_, name), getter,
setter);
return *this;
}
v8::Local<v8::ObjectTemplate> ObjectTemplateBuilder::Build() {
v8::Local<v8::ObjectTemplate> result = template_;
template_.Clear();
return result;
}
} // namespace mate

View file

@ -1,120 +0,0 @@
// 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 NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_
#define NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_
#include "base/bind.h"
#include "base/callback.h"
#include "base/strings/string_piece.h"
#include "native_mate/converter.h"
#include "native_mate/function_template.h"
#include "v8/include/v8.h"
namespace mate {
// Base template - used only for non-member function pointers. Other types
// either go to one of the below specializations, or go here and fail to compile
// because of base::Bind().
template <typename T, typename Enable = void>
struct CallbackTraits {
static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate,
T callback) {
return mate::CreateFunctionTemplate(isolate, base::Bind(callback));
}
};
// Specialization for base::Callback.
template <typename T>
struct CallbackTraits<base::Callback<T>> {
static v8::Local<v8::FunctionTemplate> CreateTemplate(
v8::Isolate* isolate,
const base::Callback<T>& callback) {
return mate::CreateFunctionTemplate(isolate, callback);
}
};
// Specialization for member function pointers. We need to handle this case
// specially because the first parameter for callbacks to MFP should typically
// come from the the JavaScript "this" object the function was called on, not
// from the first normal parameter.
template <typename T>
struct CallbackTraits<
T,
typename std::enable_if<std::is_member_function_pointer<T>::value>::type> {
static v8::Local<v8::FunctionTemplate> CreateTemplate(v8::Isolate* isolate,
T callback) {
int flags = HolderIsFirstArgument;
return mate::CreateFunctionTemplate(isolate, base::Bind(callback), flags);
}
};
// This specialization allows people to construct function templates directly if
// they need to do fancier stuff.
template <>
struct CallbackTraits<v8::Local<v8::FunctionTemplate>> {
static v8::Local<v8::FunctionTemplate> CreateTemplate(
v8::Local<v8::FunctionTemplate> templ) {
return templ;
}
};
// ObjectTemplateBuilder provides a handy interface to creating
// v8::ObjectTemplate instances with various sorts of properties.
class ObjectTemplateBuilder {
public:
explicit ObjectTemplateBuilder(v8::Isolate* isolate,
v8::Local<v8::ObjectTemplate> templ);
~ObjectTemplateBuilder();
// It's against Google C++ style to return a non-const ref, but we take some
// poetic license here in order that all calls to Set() can be via the '.'
// operator and line up nicely.
template <typename T>
ObjectTemplateBuilder& SetValue(base::StringPiece name, T val) {
return SetImpl(name, ConvertToV8(isolate_, val));
}
// In the following methods, T and U can be function pointer, member function
// pointer, base::Callback, or v8::FunctionTemplate. Most clients will want to
// use one of the first two options. Also see mate::CreateFunctionTemplate()
// for creating raw function templates.
template <typename T>
ObjectTemplateBuilder& SetMethod(base::StringPiece name, T callback) {
return SetImpl(name, CallbackTraits<T>::CreateTemplate(isolate_, callback));
}
template <typename T>
ObjectTemplateBuilder& SetProperty(base::StringPiece name, T getter) {
return SetPropertyImpl(name,
CallbackTraits<T>::CreateTemplate(isolate_, getter),
v8::Local<v8::FunctionTemplate>());
}
template <typename T, typename U>
ObjectTemplateBuilder& SetProperty(base::StringPiece name,
T getter,
U setter) {
return SetPropertyImpl(name,
CallbackTraits<T>::CreateTemplate(isolate_, getter),
CallbackTraits<U>::CreateTemplate(isolate_, setter));
}
v8::Local<v8::ObjectTemplate> Build();
private:
ObjectTemplateBuilder& SetImpl(base::StringPiece name,
v8::Local<v8::Data> val);
ObjectTemplateBuilder& SetPropertyImpl(
base::StringPiece name,
v8::Local<v8::FunctionTemplate> getter,
v8::Local<v8::FunctionTemplate> setter);
v8::Isolate* isolate_;
// ObjectTemplateBuilder should only be used on the stack.
v8::Local<v8::ObjectTemplate> template_;
};
} // namespace mate
#endif // NATIVE_MATE_NATIVE_MATE_OBJECT_TEMPLATE_BUILDER_DEPRECATED_H_

View file

@ -1,44 +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.
#include "native_mate/promise.h"
namespace mate {
Promise::Promise() = default;
Promise::Promise(v8::Isolate* isolate) : isolate_(isolate) {
resolver_ = v8::Promise::Resolver::New(isolate);
}
Promise::~Promise() = default;
Promise Promise::Create(v8::Isolate* isolate) {
return Promise(isolate);
}
Promise Promise::Create() {
return Promise::Create(v8::Isolate::GetCurrent());
}
void Promise::RejectWithErrorMessage(const std::string& string) {
v8::Local<v8::String> error_message =
v8::String::NewFromUtf8(isolate(), string.c_str(),
v8::NewStringType::kNormal,
static_cast<int>(string.size()))
.ToLocalChecked();
v8::Local<v8::Value> error = v8::Exception::Error(error_message);
resolver_->Reject(mate::ConvertToV8(isolate(), error));
}
v8::Local<v8::Object> Promise::GetHandle() const {
return resolver_->GetPromise();
}
v8::Local<v8::Value> Converter<Promise>::ToV8(v8::Isolate* isolate,
Promise val) {
return val.GetHandle();
}
} // namespace mate

View file

@ -1,58 +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 NATIVE_MATE_NATIVE_MATE_PROMISE_H_
#define NATIVE_MATE_NATIVE_MATE_PROMISE_H_
#include <string>
#include "native_mate/converter.h"
namespace mate {
class Promise {
public:
Promise();
explicit Promise(v8::Isolate* isolate);
virtual ~Promise();
static Promise Create(v8::Isolate* isolate);
static Promise Create();
v8::Isolate* isolate() const { return isolate_; }
virtual v8::Local<v8::Object> GetHandle() const;
template <typename T>
void Resolve(T* value) {
resolver_->Resolve(mate::ConvertToV8(isolate(), value));
}
template <typename T>
void Reject(T* value) {
resolver_->Reject(mate::ConvertToV8(isolate(), value));
}
void RejectWithErrorMessage(const std::string& error);
protected:
v8::Isolate* isolate_ = nullptr;
private:
v8::Local<v8::Promise::Resolver> resolver_;
};
template <>
struct Converter<Promise> {
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate, Promise val);
// TODO(MarshallOfSound): Implement FromV8 to allow promise chaining
// in native land
// static bool FromV8(v8::Isolate* isolate,
// v8::Local<v8::Value> val,
// Promise* out);
};
} // namespace mate
#endif // NATIVE_MATE_NATIVE_MATE_PROMISE_H_

View file

@ -15,7 +15,6 @@ web_contents.patch
webview_cross_drag.patch
disable_user_gesture_requirement_for_beforeunload_dialogs.patch
gin_enable_disable_v8_platform.patch
gin_with_namespace.patch
blink-worker-enable-csp-in-file-scheme.patch
disable-redraw-lock.patch
v8_context_snapshot_generator.patch

View file

@ -1,83 +0,0 @@
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Cheng Zhao <zcbenz@gmail.com>
Date: Thu, 20 Sep 2018 17:47:44 -0700
Subject: gin_with_namespace.patch
When using gin with native_mate together we may have C++ confused with
finding the correct ConvertFromV8. We add gin:: namespace explicitly in
those calls to work around the ambiguous compilation error.
Note that this is only a work around to make it easier to remove
native_mate, and we should remove this patch once native_mate is erased
from Electron.
diff --git a/gin/arguments.h b/gin/arguments.h
index eaded13e29919793494dfe2f7f85fad7dcb125cf..03e1495566d1ab561dcd67517053173911288cea 100644
--- a/gin/arguments.h
+++ b/gin/arguments.h
@@ -28,14 +28,14 @@ class GIN_EXPORT Arguments {
v8::Local<v8::Object> holder = is_for_property_
? info_for_property_->Holder()
: info_for_function_->Holder();
- return ConvertFromV8(isolate_, holder, out);
+ return gin::ConvertFromV8(isolate_, holder, out);
}
template<typename T>
bool GetData(T* out) {
v8::Local<v8::Value> data = is_for_property_ ? info_for_property_->Data()
: info_for_function_->Data();
- return ConvertFromV8(isolate_, data, out);
+ return gin::ConvertFromV8(isolate_, data, out);
}
template<typename T>
@@ -45,7 +45,7 @@ class GIN_EXPORT Arguments {
return false;
}
v8::Local<v8::Value> val = (*info_for_function_)[next_++];
- return ConvertFromV8(isolate_, val, out);
+ return gin::ConvertFromV8(isolate_, val, out);
}
template<typename T>
@@ -58,7 +58,7 @@ class GIN_EXPORT Arguments {
out->resize(remaining);
for (int i = 0; i < remaining; ++i) {
v8::Local<v8::Value> val = (*info_for_function_)[next_++];
- if (!ConvertFromV8(isolate_, val, &out->at(i)))
+ if (!gin::ConvertFromV8(isolate_, val, &out->at(i)))
return false;
}
return true;
@@ -80,7 +80,7 @@ class GIN_EXPORT Arguments {
template<typename T>
void Return(T val) {
v8::Local<v8::Value> v8_value;
- if (!TryConvertToV8(isolate_, val, &v8_value))
+ if (!gin::TryConvertToV8(isolate_, val, &v8_value))
return;
(is_for_property_ ? info_for_property_->GetReturnValue()
: info_for_function_->GetReturnValue())
diff --git a/gin/converter.h b/gin/converter.h
index 27b4d0acd016df378e4cb44ccda1a433244fe2c6..b19209a8534a497373c5a2f861b26502e96144c9 100644
--- a/gin/converter.h
+++ b/gin/converter.h
@@ -250,7 +250,7 @@ std::enable_if_t<ToV8ReturnsMaybe<T>::value, bool> TryConvertToV8(
v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
- return ConvertToV8(isolate, input).ToLocal(output);
+ return gin::ConvertToV8(isolate, input).ToLocal(output);
}
template <typename T>
@@ -258,7 +258,7 @@ std::enable_if_t<!ToV8ReturnsMaybe<T>::value, bool> TryConvertToV8(
v8::Isolate* isolate,
const T& input,
v8::Local<v8::Value>* output) {
- *output = ConvertToV8(isolate, input);
+ *output = gin::ConvertToV8(isolate, input);
return true;
}

View file

@ -55,7 +55,7 @@ function cpplint (args) {
const LINTERS = [ {
key: 'c++',
roots: ['shell', 'native_mate'],
roots: ['shell'],
test: filename => filename.endsWith('.cc') || filename.endsWith('.h'),
run: (opts, filenames) => {
if (opts.fix) {

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

@ -1,19 +1,14 @@
// This file was GENERATED by command:
// pump.py constructor.h.pump
// DO NOT EDIT BY HAND!!!
// Copyright (c) 2018 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
// Copyright 2014 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_CONSTRUCTOR_H_
#define SHELL_COMMON_GIN_HELPER_CONSTRUCTOR_H_
#ifndef NATIVE_MATE_NATIVE_MATE_CONSTRUCTOR_H_
#define NATIVE_MATE_NATIVE_MATE_CONSTRUCTOR_H_
#include "base/bind.h"
#include "native_mate/function_template.h"
#include "shell/common/gin_helper/function_template.h"
#include "shell/common/gin_helper/wrappable_base.h"
namespace mate {
namespace gin_helper {
namespace internal {
@ -21,82 +16,77 @@ namespace internal {
// into native types. It relies on the function_template.h to provide helper
// templates.
inline WrappableBase* InvokeFactory(
Arguments* args,
gin::Arguments* args,
const base::Callback<WrappableBase*()>& callback) {
return callback.Run();
}
template <typename P1>
inline WrappableBase* InvokeFactory(
Arguments* args,
gin::Arguments* args,
const base::Callback<WrappableBase*(P1)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1))
if (!gin_helper::GetNextArgument(args, 0, true, &a1))
return nullptr;
return callback.Run(a1);
}
template <typename P1, typename P2>
inline WrappableBase* InvokeFactory(
Arguments* args,
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &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(
Arguments* args,
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;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a2) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &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(
Arguments* args,
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;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a2) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a3) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &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(
Arguments* args,
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;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a2) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a3) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a4) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &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);
}
@ -108,7 +98,7 @@ template <typename P1,
typename P5,
typename P6>
inline WrappableBase* InvokeFactory(
Arguments* args,
gin::Arguments* args,
const base::Callback<WrappableBase*(P1, P2, P3, P4, P5, P6)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
@ -116,13 +106,12 @@ inline WrappableBase* InvokeFactory(
typename CallbackParamTraits<P4>::LocalType a4;
typename CallbackParamTraits<P5>::LocalType a5;
typename CallbackParamTraits<P6>::LocalType a6;
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a2) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a3) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a4) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a5) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &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);
}
@ -130,7 +119,7 @@ inline WrappableBase* InvokeFactory(
template <typename Sig>
void InvokeNew(const base::Callback<Sig>& factory,
v8::Isolate* isolate,
Arguments* args) {
gin_helper::Arguments* args) {
if (!args->IsConstructCall()) {
args->ThrowError("Requires constructor call");
return;
@ -155,6 +144,26 @@ void InvokeNew(const base::Callback<Sig>& factory,
} // namespace internal
} // 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(&internal::InvokeNew<Sig>, func));
templ->InstanceTemplate()->SetInternalFieldCount(1);
T::BuildPrototype(isolate, templ);
return templ->GetFunction(isolate->GetCurrentContext()).ToLocalChecked();
}
#endif // NATIVE_MATE_NATIVE_MATE_CONSTRUCTOR_H_
} // 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

@ -2,14 +2,12 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#include "native_mate/wrappable.h"
#include "shell/common/gin_helper/wrappable.h"
#include "base/logging.h"
#include "gin/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder_deprecated.h"
#include "shell/common/gin_helper/dictionary.h"
namespace mate {
namespace gin_helper {
WrappableBase::WrappableBase() = default;
@ -88,4 +86,4 @@ void* FromV8Impl(v8::Isolate* isolate, v8::Local<v8::Value> val) {
} // namespace internal
} // namespace mate
} // namespace gin_helper

View file

@ -2,15 +2,14 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#ifndef NATIVE_MATE_NATIVE_MATE_WRAPPABLE_H_
#define NATIVE_MATE_NATIVE_MATE_WRAPPABLE_H_
#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 "native_mate/constructor.h"
#include "native_mate/converter.h"
#include "shell/common/gin_helper/constructor.h"
namespace mate {
namespace gin_helper {
namespace internal {
@ -76,35 +75,15 @@ class Wrappable : public WrappableBase {
template <typename T>
gin::WrapperInfo Wrappable<T>::kWrapperInfo = {gin::kEmbedderNativeGin};
// This converter handles any subclass of Wrappable.
template <typename T>
struct Converter<T*,
typename std::enable_if<
std::is_convertible<T*, 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<WrappableBase*>(internal::FromV8Impl(isolate, val)));
return *out != nullptr;
}
};
} // namespace mate
} // namespace gin_helper
namespace gin {
// Provides compatibility for gin.
template <typename T>
struct Converter<
T*,
typename std::enable_if<
std::is_convertible<T*, mate::WrappableBase*>::value>::type> {
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();
@ -113,12 +92,12 @@ struct Converter<
}
static bool FromV8(v8::Isolate* isolate, v8::Local<v8::Value> val, T** out) {
*out = static_cast<T*>(static_cast<mate::WrappableBase*>(
mate::internal::FromV8Impl(isolate, val)));
*out = static_cast<T*>(static_cast<gin_helper::WrappableBase*>(
gin_helper::internal::FromV8Impl(isolate, val)));
return *out != nullptr;
}
};
} // namespace gin
#endif // NATIVE_MATE_NATIVE_MATE_WRAPPABLE_H_
#endif // SHELL_COMMON_GIN_HELPER_WRAPPABLE_H_

View file

@ -2,15 +2,16 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE.chromium file.
#ifndef NATIVE_MATE_NATIVE_MATE_WRAPPABLE_BASE_H_
#define NATIVE_MATE_NATIVE_MATE_WRAPPABLE_BASE_H_
#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;
struct Destroyable;
} // namespace gin
namespace mate {
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.
@ -23,7 +24,7 @@ namespace mate {
// };
//
// Subclasses should also typically have private constructors and expose a
// static Create function that returns a mate::Handle. Forcing creators through
// 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
@ -48,12 +49,10 @@ class WrappableBase {
// This method should only be called by classes using Constructor.
virtual void InitWith(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
// Helper to migrate from native_mate to gin.
// Helper to init with arguments.
void InitWithArgs(gin::Arguments* args);
private:
friend struct gin::Destroyable;
static void FirstWeakCallback(
const v8::WeakCallbackInfo<WrappableBase>& data);
static void SecondWeakCallback(
@ -65,6 +64,6 @@ class WrappableBase {
DISALLOW_COPY_AND_ASSIGN(WrappableBase);
};
} // namespace mate
} // namespace gin_helper
#endif // NATIVE_MATE_NATIVE_MATE_WRAPPABLE_BASE_H_
#endif // SHELL_COMMON_GIN_HELPER_WRAPPABLE_BASE_H_