chore: remove native_mate (Part 7) (#20561)

* refactor: use gin converters in api::Protocol

* refactor: convert JS constructor impl to gin

* refactor: use InitWithArgs helper

* fix: gin_helper::Dictionary should behave the same with mate

* fix cpplint warnings

* refactor: no more need to patch gin/dictionary.h
This commit is contained in:
Cheng Zhao 2019-10-15 10:15:23 +09:00 коммит произвёл GitHub
Родитель 6c6bff81ac
Коммит 1ecfcc8c70
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
43 изменённых файлов: 274 добавлений и 368 удалений

Просмотреть файл

@ -476,7 +476,6 @@ filenames = {
"shell/common/gin_converters/gurl_converter.h",
"shell/common/gin_converters/image_converter.cc",
"shell/common/gin_converters/image_converter.h",
"shell/common/gin_converters/native_mate_handle_converter.h",
"shell/common/gin_converters/message_box_converter.cc",
"shell/common/gin_converters/message_box_converter.h",
"shell/common/gin_converters/native_window_converter.h",

Просмотреть файл

@ -10,6 +10,7 @@ source_set("native_mate") {
"//v8:v8_headers",
]
public_configs = [ ":native_mate_config" ]
include_dirs = [ ".." ]
sources = [
"native_mate/arguments.cc",
"native_mate/arguments.h",

Просмотреть файл

@ -11,6 +11,7 @@
#include "base/bind.h"
#include "native_mate/function_template.h"
#include "shell/common/gin_helper/function_template.h"
namespace mate {
@ -30,7 +31,8 @@ inline WrappableBase* InvokeFactory(
Arguments* args,
const base::Callback<WrappableBase*(P1)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
if (!GetNextArgument(args, 0, true, &a1))
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1))
return nullptr;
return callback.Run(a1);
}
@ -41,8 +43,9 @@ inline WrappableBase* InvokeFactory(
const base::Callback<WrappableBase*(P1, P2)>& callback) {
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
if (!GetNextArgument(args, 0, true, &a1) ||
!GetNextArgument(args, 0, false, &a2))
gin::Arguments gin_args(args->info());
if (!gin_helper::GetNextArgument(&gin_args, 0, true, &a1) ||
!gin_helper::GetNextArgument(&gin_args, 0, false, &a2))
return nullptr;
return callback.Run(a1, a2);
}
@ -54,9 +57,10 @@ inline WrappableBase* InvokeFactory(
typename CallbackParamTraits<P1>::LocalType a1;
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
if (!GetNextArgument(args, 0, true, &a1) ||
!GetNextArgument(args, 0, false, &a2) ||
!GetNextArgument(args, 0, false, &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))
return nullptr;
return callback.Run(a1, a2, a3);
}
@ -69,10 +73,11 @@ inline WrappableBase* InvokeFactory(
typename CallbackParamTraits<P2>::LocalType a2;
typename CallbackParamTraits<P3>::LocalType a3;
typename CallbackParamTraits<P4>::LocalType a4;
if (!GetNextArgument(args, 0, true, &a1) ||
!GetNextArgument(args, 0, false, &a2) ||
!GetNextArgument(args, 0, false, &a3) ||
!GetNextArgument(args, 0, false, &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))
return nullptr;
return callback.Run(a1, a2, a3, a4);
}
@ -86,11 +91,12 @@ inline WrappableBase* InvokeFactory(
typename CallbackParamTraits<P3>::LocalType a3;
typename CallbackParamTraits<P4>::LocalType a4;
typename CallbackParamTraits<P5>::LocalType a5;
if (!GetNextArgument(args, 0, true, &a1) ||
!GetNextArgument(args, 0, false, &a2) ||
!GetNextArgument(args, 0, false, &a3) ||
!GetNextArgument(args, 0, false, &a4) ||
!GetNextArgument(args, 0, false, &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))
return nullptr;
return callback.Run(a1, a2, a3, a4, a5);
}
@ -110,12 +116,13 @@ inline WrappableBase* InvokeFactory(
typename CallbackParamTraits<P4>::LocalType a4;
typename CallbackParamTraits<P5>::LocalType a5;
typename CallbackParamTraits<P6>::LocalType a6;
if (!GetNextArgument(args, 0, true, &a1) ||
!GetNextArgument(args, 0, false, &a2) ||
!GetNextArgument(args, 0, false, &a3) ||
!GetNextArgument(args, 0, false, &a4) ||
!GetNextArgument(args, 0, false, &a5) ||
!GetNextArgument(args, 0, false, &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))
return nullptr;
return callback.Run(a1, a2, a3, a4, a5, a6);
}

Просмотреть файл

@ -115,4 +115,22 @@ struct Converter<Dictionary> {
} // 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_

Просмотреть файл

@ -5,11 +5,11 @@
#ifndef NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_
#define NATIVE_MATE_NATIVE_MATE_FUNCTION_TEMPLATE_H_
#include "../shell/common/gin_helper/destroyable.h"
#include "../shell/common/gin_helper/error_thrower.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

Просмотреть файл

@ -69,4 +69,22 @@ mate::Handle<T> CreateHandle(v8::Isolate* isolate, T* 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_

Просмотреть файл

@ -5,6 +5,7 @@
#include "native_mate/wrappable.h"
#include "base/logging.h"
#include "gin/arguments.h"
#include "native_mate/dictionary.h"
#include "native_mate/object_template_builder_deprecated.h"
@ -37,6 +38,12 @@ v8::MaybeLocal<v8::Object> WrappableBase::GetWrapper(
return v8::MaybeLocal<v8::Object>();
}
void WrappableBase::InitWithArgs(gin::Arguments* args) {
v8::Local<v8::Object> holder;
args->GetHolder(&holder);
InitWith(args->isolate(), holder);
}
void WrappableBase::InitWith(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper) {
CHECK(wrapper_.IsEmpty());

Просмотреть файл

@ -97,4 +97,28 @@ struct Converter<T*,
} // namespace mate
namespace gin {
// Provides compatibility for gin.
template <typename T>
struct Converter<
T*,
typename std::enable_if<
std::is_convertible<T*, mate::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<mate::WrappableBase*>(
mate::internal::FromV8Impl(isolate, val)));
return *out != nullptr;
}
};
} // namespace gin
#endif // NATIVE_MATE_NATIVE_MATE_WRAPPABLE_H_

Просмотреть файл

@ -6,8 +6,9 @@
#define NATIVE_MATE_NATIVE_MATE_WRAPPABLE_BASE_H_
namespace gin {
class Arguments;
struct Destroyable;
}
} // namespace gin
namespace mate {
@ -47,6 +48,9 @@ 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.
void InitWithArgs(gin::Arguments* args);
private:
friend struct gin::Destroyable;

Просмотреть файл

@ -57,15 +57,9 @@ namespace electron {
namespace api {
BrowserView::BrowserView(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
BrowserView::BrowserView(gin::Arguments* args,
const mate::Dictionary& options) {
Init(isolate, wrapper, options);
}
void BrowserView::Init(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options) {
v8::Isolate* isolate = args->isolate();
mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate);
options.Get(options::kWebPreferences, &web_preferences);
web_preferences.Set("type", "browserView");
@ -79,7 +73,7 @@ void BrowserView::Init(v8::Isolate* isolate,
view_.reset(
NativeBrowserView::Create(api_web_contents_->managed_web_contents()));
InitWith(isolate, wrapper);
InitWithArgs(args);
}
BrowserView::~BrowserView() {
@ -96,16 +90,17 @@ void BrowserView::WebContentsDestroyed() {
}
// static
mate::WrappableBase* BrowserView::New(mate::Arguments* args) {
mate::WrappableBase* BrowserView::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
args->ThrowError("Cannot create BrowserView before app is ready");
thrower.ThrowError("Cannot create BrowserView before app is ready");
return nullptr;
}
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
args->GetNext(&options);
return new BrowserView(args->isolate(), args->GetThis(), options);
return new BrowserView(args, options);
}
int32_t BrowserView::ID() const {

Просмотреть файл

@ -12,6 +12,7 @@
#include "native_mate/handle.h"
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/native_browser_view.h"
#include "shell/common/gin_helper/error_thrower.h"
namespace gfx {
class Rect;
@ -33,7 +34,8 @@ class WebContents;
class BrowserView : public mate::TrackableObject<BrowserView>,
public content::WebContentsObserver {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -44,19 +46,13 @@ class BrowserView : public mate::TrackableObject<BrowserView>,
int32_t ID() const;
protected:
BrowserView(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options);
BrowserView(gin::Arguments* args, const mate::Dictionary& options);
~BrowserView() override;
// content::WebContentsObserver:
void WebContentsDestroyed() override;
private:
void Init(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options);
void SetAutoResize(AutoResizeFlags flags);
void SetBounds(const gfx::Rect& bounds);
gfx::Rect GetBounds();

Просмотреть файл

@ -29,13 +29,13 @@ namespace electron {
namespace api {
BrowserWindow::BrowserWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
BrowserWindow::BrowserWindow(gin::Arguments* args,
const mate::Dictionary& options)
: TopLevelWindow(isolate, options), weak_factory_(this) {
: TopLevelWindow(args->isolate(), options), weak_factory_(this) {
mate::Handle<class WebContents> web_contents;
// Use options.webPreferences in WebContents.
v8::Isolate* isolate = args->isolate();
mate::Dictionary web_preferences = mate::Dictionary::CreateEmpty(isolate);
options.Get(options::kWebPreferences, &web_preferences);
@ -92,7 +92,7 @@ BrowserWindow::BrowserWindow(v8::Isolate* isolate,
if (host)
host->GetWidget()->AddInputEventObserver(this);
InitWith(isolate, wrapper);
InitWithArgs(args);
#if defined(OS_MACOSX)
OverrideNSWindowContentView(web_contents->managed_web_contents());
@ -444,9 +444,10 @@ void BrowserWindow::OnWindowHide() {
}
// static
mate::WrappableBase* BrowserWindow::New(mate::Arguments* args) {
mate::WrappableBase* BrowserWindow::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
args->ThrowError("Cannot create BrowserWindow before app is ready");
thrower.ThrowError("Cannot create BrowserWindow before app is ready");
return nullptr;
}
@ -460,7 +461,7 @@ mate::WrappableBase* BrowserWindow::New(mate::Arguments* args) {
options = mate::Dictionary::CreateEmpty(args->isolate());
}
return new BrowserWindow(args->isolate(), args->GetThis(), options);
return new BrowserWindow(args, options);
}
// static

Просмотреть файл

@ -12,6 +12,7 @@
#include "base/cancelable_callback.h"
#include "shell/browser/api/atom_api_top_level_window.h"
#include "shell/browser/api/atom_api_web_contents.h"
#include "shell/common/gin_helper/error_thrower.h"
namespace electron {
@ -22,7 +23,8 @@ class BrowserWindow : public TopLevelWindow,
public content::WebContentsObserver,
public ExtendedWebContentsObserver {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -36,9 +38,7 @@ class BrowserWindow : public TopLevelWindow,
}
protected:
BrowserWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options);
BrowserWindow(gin::Arguments* args, const mate::Dictionary& options);
~BrowserWindow() override;
// content::RenderWidgetHost::InputEventObserver:

Просмотреть файл

@ -28,9 +28,8 @@ namespace electron {
namespace api {
Menu::Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: model_(new AtomMenuModel(this)) {
InitWith(isolate, wrapper);
Menu::Menu(gin::Arguments* args) : model_(new AtomMenuModel(this)) {
InitWithArgs(args);
model_->AddObserver(this);
}

Просмотреть файл

@ -9,6 +9,7 @@
#include <string>
#include "base/callback.h"
#include "gin/arguments.h"
#include "shell/browser/api/atom_api_top_level_window.h"
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/ui/atom_menu_model.h"
@ -21,7 +22,7 @@ class Menu : public mate::TrackableObject<Menu>,
public AtomMenuModel::Delegate,
public AtomMenuModel::Observer {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -37,7 +38,7 @@ class Menu : public mate::TrackableObject<Menu>,
AtomMenuModel* model() const { return model_.get(); }
protected:
Menu(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
explicit Menu(gin::Arguments* args);
~Menu() override;
// mate::Wrappable:
@ -144,31 +145,4 @@ struct Converter<electron::AtomMenuModel*> {
} // namespace mate
namespace gin {
template <>
struct Converter<electron::AtomMenuModel*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::AtomMenuModel** out) {
return mate::ConvertFromV8(isolate, val, out);
}
};
// TODO(zcbenz): Remove this after converting Menu to gin::Wrapper.
template <>
struct Converter<electron::api::Menu*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::Menu** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::Menu* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_MENU_H_

Просмотреть файл

@ -20,7 +20,7 @@ namespace api {
class MenuMac : public Menu {
protected:
MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
explicit MenuMac(gin::Arguments* args);
~MenuMac() override;
void PopupAt(TopLevelWindow* window,

Просмотреть файл

@ -30,8 +30,7 @@ namespace electron {
namespace api {
MenuMac::MenuMac(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: Menu(isolate, wrapper), weak_factory_(this) {}
MenuMac::MenuMac(gin::Arguments* args) : Menu(args), weak_factory_(this) {}
MenuMac::~MenuMac() = default;
@ -173,8 +172,8 @@ void Menu::SendActionToFirstResponder(const std::string& action) {
}
// static
mate::WrappableBase* Menu::New(mate::Arguments* args) {
return new MenuMac(args->isolate(), args->GetThis());
mate::WrappableBase* Menu::New(gin::Arguments* args) {
return new MenuMac(args);
}
} // namespace api

Просмотреть файл

@ -16,8 +16,7 @@ namespace electron {
namespace api {
MenuViews::MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper)
: Menu(isolate, wrapper), weak_factory_(this) {}
MenuViews::MenuViews(gin::Arguments* args) : Menu(args), weak_factory_(this) {}
MenuViews::~MenuViews() = default;
@ -75,8 +74,8 @@ void MenuViews::OnClosed(int32_t window_id, base::Closure callback) {
}
// static
mate::WrappableBase* Menu::New(mate::Arguments* args) {
return new MenuViews(args->isolate(), args->GetThis());
mate::WrappableBase* Menu::New(gin::Arguments* args) {
return new MenuViews(args);
}
} // namespace api

Просмотреть файл

@ -19,7 +19,7 @@ namespace api {
class MenuViews : public Menu {
public:
MenuViews(v8::Isolate* isolate, v8::Local<v8::Object> wrapper);
explicit MenuViews(gin::Arguments* args);
~MenuViews() override;
protected:

Просмотреть файл

@ -60,20 +60,6 @@ struct Converter<ui::NativeTheme::ThemeSource> {
ui::NativeTheme::ThemeSource* out);
};
// TODO(zcbenz): Remove this after converting NativeTheme to gin::Wrapper.
template <>
struct Converter<electron::api::NativeTheme*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::NativeTheme** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::NativeTheme* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_NATIVE_THEME_H_

Просмотреть файл

@ -49,9 +49,8 @@ namespace electron {
namespace api {
Notification::Notification(v8::Local<v8::Object> wrapper,
gin::Arguments* args) {
InitWith(args->isolate(), wrapper);
Notification::Notification(gin::Arguments* args) {
InitWithArgs(args);
presenter_ = static_cast<AtomBrowserClient*>(AtomBrowserClient::Get())
->GetNotificationPresenter();
@ -82,13 +81,13 @@ Notification::~Notification() {
}
// static
mate::WrappableBase* Notification::New(mate::Arguments* args) {
mate::WrappableBase* Notification::New(gin_helper::ErrorThrower thrower,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
args->ThrowError("Cannot create Notification before app is ready");
thrower.ThrowError("Cannot create Notification before app is ready");
return nullptr;
}
gin::Arguments gin_args(args->info());
return new Notification(args->GetThis(), &gin_args);
return new Notification(args);
}
// Getters

Просмотреть файл

@ -14,6 +14,7 @@
#include "shell/browser/notifications/notification.h"
#include "shell/browser/notifications/notification_delegate.h"
#include "shell/browser/notifications/notification_presenter.h"
#include "shell/common/gin_helper/error_thrower.h"
#include "ui/gfx/image/image.h"
namespace gin {
@ -27,7 +28,8 @@ namespace api {
class Notification : public mate::TrackableObject<Notification>,
public NotificationDelegate {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
gin::Arguments* args);
static bool IsSupported();
static void BuildPrototype(v8::Isolate* isolate,
@ -42,7 +44,7 @@ class Notification : public mate::TrackableObject<Notification>,
void NotificationClosed() override;
protected:
Notification(v8::Local<v8::Object> wrapper, gin::Arguments* args);
explicit Notification(gin::Arguments* args);
~Notification() override;
void Show();
@ -101,22 +103,4 @@ class Notification : public mate::TrackableObject<Notification>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting Notification to gin::Wrapper.
template <>
struct Converter<electron::api::Notification*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::Notification** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::Notification* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_NOTIFICATION_H_

Просмотреть файл

@ -13,8 +13,10 @@
#include "shell/browser/atom_browser_context.h"
#include "shell/browser/browser.h"
#include "shell/common/deprecate_util.h"
#include "shell/common/native_mate_converters/net_converter.h"
#include "shell/common/native_mate_converters/once_callback.h"
#include "shell/common/gin_converters/callback_converter.h"
#include "shell/common/gin_converters/net_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/options_switches.h"
#include "shell/common/promise_util.h"
@ -39,19 +41,19 @@ struct CustomScheme {
} // namespace
namespace mate {
namespace gin {
template <>
struct Converter<CustomScheme> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
CustomScheme* out) {
mate::Dictionary dict;
gin::Dictionary dict(isolate);
if (!ConvertFromV8(isolate, val, &dict))
return false;
if (!dict.Get("scheme", &(out->scheme)))
return false;
mate::Dictionary opt;
gin::Dictionary opt(isolate);
// options are optional. Default values specified in SchemeOptions are used
if (dict.Get("privileges", &opt)) {
opt.Get("standard", &(out->options.standard));
@ -66,7 +68,7 @@ struct Converter<CustomScheme> {
}
};
} // namespace mate
} // namespace gin
namespace electron {
namespace api {
@ -75,11 +77,11 @@ std::vector<std::string> GetStandardSchemes() {
return g_standard_schemes;
}
void RegisterSchemesAsPrivileged(v8::Local<v8::Value> val,
mate::Arguments* args) {
void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> val) {
std::vector<CustomScheme> custom_schemes;
if (!mate::ConvertFromV8(args->isolate(), val, &custom_schemes)) {
args->ThrowError("Argument must be an array of custom schemes.");
if (!gin::ConvertFromV8(thrower.isolate(), val, &custom_schemes)) {
thrower.ThrowError("Argument must be an array of custom schemes.");
return;
}
@ -181,7 +183,7 @@ ProtocolError ProtocolNS::RegisterProtocol(ProtocolType type,
}
void ProtocolNS::UnregisterProtocol(const std::string& scheme,
mate::Arguments* args) {
gin::Arguments* args) {
const bool removed = handlers_.erase(scheme) != 0;
const auto error =
removed ? ProtocolError::OK : ProtocolError::NOT_REGISTERED;
@ -201,7 +203,7 @@ ProtocolError ProtocolNS::InterceptProtocol(ProtocolType type,
}
void ProtocolNS::UninterceptProtocol(const std::string& scheme,
mate::Arguments* args) {
gin::Arguments* args) {
const bool removed = intercept_handlers_.erase(scheme) != 0;
const auto error =
removed ? ProtocolError::OK : ProtocolError::NOT_INTERCEPTED;
@ -213,7 +215,7 @@ bool ProtocolNS::IsProtocolIntercepted(const std::string& scheme) {
}
v8::Local<v8::Promise> ProtocolNS::IsProtocolHandled(const std::string& scheme,
mate::Arguments* args) {
gin::Arguments* args) {
node::Environment* env = node::Environment::GetCurrent(args->isolate());
EmitDeprecationWarning(
env,
@ -234,7 +236,7 @@ v8::Local<v8::Promise> ProtocolNS::IsProtocolHandled(const std::string& scheme,
base::Contains(kBuiltinSchemes, scheme));
}
void ProtocolNS::HandleOptionalCallback(mate::Arguments* args,
void ProtocolNS::HandleOptionalCallback(gin::Arguments* args,
ProtocolError error) {
CompletionCallback callback;
if (args->GetNext(&callback)) {
@ -247,22 +249,22 @@ void ProtocolNS::HandleOptionalCallback(mate::Arguments* args,
callback.Run(v8::Null(args->isolate()));
else
callback.Run(v8::Exception::Error(
mate::StringToV8(isolate(), ErrorCodeToString(error))));
gin::StringToV8(isolate(), ErrorCodeToString(error))));
}
}
// static
mate::Handle<ProtocolNS> ProtocolNS::Create(
gin::Handle<ProtocolNS> ProtocolNS::Create(
v8::Isolate* isolate,
AtomBrowserContext* browser_context) {
return mate::CreateHandle(isolate, new ProtocolNS(isolate, browser_context));
return gin::CreateHandle(isolate, new ProtocolNS(isolate, browser_context));
}
// static
void ProtocolNS::BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype) {
prototype->SetClassName(mate::StringToV8(isolate, "Protocol"));
mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
prototype->SetClassName(gin::StringToV8(isolate, "Protocol"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("registerStringProtocol",
&ProtocolNS::RegisterProtocolFor<ProtocolType::kString>)
.SetMethod("registerBufferProtocol",
@ -299,16 +301,16 @@ void ProtocolNS::BuildPrototype(v8::Isolate* isolate,
namespace {
void RegisterSchemesAsPrivileged(v8::Local<v8::Value> val,
mate::Arguments* args) {
void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> val) {
if (electron::Browser::Get()->is_ready()) {
args->ThrowError(
thrower.ThrowError(
"protocol.registerSchemesAsPrivileged should be called before "
"app is ready");
return;
}
electron::api::RegisterSchemesAsPrivileged(val, args);
electron::api::RegisterSchemesAsPrivileged(thrower, val);
}
void Initialize(v8::Local<v8::Object> exports,
@ -316,7 +318,7 @@ 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.SetMethod("registerSchemesAsPrivileged", &RegisterSchemesAsPrivileged);
dict.SetMethod("getStandardSchemes", &electron::api::GetStandardSchemes);
}

Просмотреть файл

@ -9,10 +9,10 @@
#include <vector>
#include "content/public/browser/content_browser_client.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "gin/handle.h"
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/net/atom_url_loader_factory.h"
#include "shell/common/gin_helper/dictionary.h"
namespace electron {
@ -22,8 +22,8 @@ namespace api {
std::vector<std::string> GetStandardSchemes();
void RegisterSchemesAsPrivileged(v8::Local<v8::Value> val,
mate::Arguments* args);
void RegisterSchemesAsPrivileged(gin_helper::ErrorThrower thrower,
v8::Local<v8::Value> val);
// Possible errors.
enum class ProtocolError {
@ -37,8 +37,8 @@ enum class ProtocolError {
// Protocol implementation based on network services.
class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
public:
static mate::Handle<ProtocolNS> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
static gin::Handle<ProtocolNS> Create(v8::Isolate* isolate,
AtomBrowserContext* browser_context);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -61,35 +61,35 @@ class ProtocolNS : public mate::TrackableObject<ProtocolNS> {
ProtocolError RegisterProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
void UnregisterProtocol(const std::string& scheme, mate::Arguments* args);
void UnregisterProtocol(const std::string& scheme, gin::Arguments* args);
bool IsProtocolRegistered(const std::string& scheme);
ProtocolError InterceptProtocol(ProtocolType type,
const std::string& scheme,
const ProtocolHandler& handler);
void UninterceptProtocol(const std::string& scheme, mate::Arguments* args);
void UninterceptProtocol(const std::string& scheme, gin::Arguments* args);
bool IsProtocolIntercepted(const std::string& scheme);
// Old async version of IsProtocolRegistered.
v8::Local<v8::Promise> IsProtocolHandled(const std::string& scheme,
mate::Arguments* args);
gin::Arguments* args);
// Helper for converting old registration APIs to new RegisterProtocol API.
template <ProtocolType type>
void RegisterProtocolFor(const std::string& scheme,
const ProtocolHandler& handler,
mate::Arguments* args) {
gin::Arguments* args) {
HandleOptionalCallback(args, RegisterProtocol(type, scheme, handler));
}
template <ProtocolType type>
void InterceptProtocolFor(const std::string& scheme,
const ProtocolHandler& handler,
mate::Arguments* args) {
gin::Arguments* args) {
HandleOptionalCallback(args, InterceptProtocol(type, scheme, handler));
}
// Be compatible with old interface, which accepts optional callback.
void HandleOptionalCallback(mate::Arguments* args, ProtocolError error);
void HandleOptionalCallback(gin::Arguments* args, ProtocolError error);
HandlersMap handlers_;
HandlersMap intercept_handlers_;

Просмотреть файл

@ -121,22 +121,4 @@ class Session : public mate::TrackableObject<Session>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting Session to gin::Wrapper.
template <>
struct Converter<electron::api::Session*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::Session** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::Session* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_SESSION_H_

Просмотреть файл

@ -108,11 +108,10 @@ TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
#endif
}
TopLevelWindow::TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
TopLevelWindow::TopLevelWindow(gin::Arguments* args,
const mate::Dictionary& options)
: TopLevelWindow(isolate, options) {
InitWith(isolate, wrapper);
: TopLevelWindow(args->isolate(), options) {
InitWithArgs(args);
// Init window after everything has been setup.
window()->InitFromOptions(options);
}
@ -1057,11 +1056,11 @@ void TopLevelWindow::RemoveFromParentChildWindows() {
}
// static
mate::WrappableBase* TopLevelWindow::New(mate::Arguments* args) {
mate::WrappableBase* TopLevelWindow::New(gin::Arguments* args) {
mate::Dictionary options = mate::Dictionary::CreateEmpty(args->isolate());
args->GetNext(&options);
return new TopLevelWindow(args->isolate(), args->GetThis(), options);
return new TopLevelWindow(args, options);
}
// static

Просмотреть файл

@ -28,7 +28,7 @@ class View;
class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
public NativeWindowObserver {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
@ -43,9 +43,7 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
// Common constructor.
TopLevelWindow(v8::Isolate* isolate, const mate::Dictionary& options);
// Creating independent TopLevelWindow instance.
TopLevelWindow(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
const mate::Dictionary& options);
TopLevelWindow(gin::Arguments* args, const mate::Dictionary& options);
~TopLevelWindow() override;
// TrackableObject:
@ -266,22 +264,4 @@ class TopLevelWindow : public mate::TrackableObject<TopLevelWindow>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting TopLevelWindow to gin::Wrapper.
template <>
struct Converter<electron::api::TopLevelWindow*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::TopLevelWindow** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::TopLevelWindow* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_TOP_LEVEL_WINDOW_H_

Просмотреть файл

@ -55,26 +55,25 @@ namespace electron {
namespace api {
Tray::Tray(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
mate::Handle<NativeImage> image)
Tray::Tray(mate::Handle<NativeImage> image, gin::Arguments* args)
: tray_icon_(TrayIcon::Create()) {
SetImage(isolate, image);
SetImage(args->isolate(), image);
tray_icon_->AddObserver(this);
InitWith(isolate, wrapper);
InitWithArgs(args);
}
Tray::~Tray() = default;
// static
mate::WrappableBase* Tray::New(mate::Handle<NativeImage> image,
mate::Arguments* args) {
mate::WrappableBase* Tray::New(gin_helper::ErrorThrower thrower,
mate::Handle<NativeImage> image,
gin::Arguments* args) {
if (!Browser::Get()->is_ready()) {
args->ThrowError("Cannot create Tray before app is ready");
thrower.ThrowError("Cannot create Tray before app is ready");
return nullptr;
}
return new Tray(args->isolate(), args->GetThis(), image);
return new Tray(image, args);
}
void Tray::OnClicked(const gfx::Rect& bounds,

Просмотреть файл

@ -13,6 +13,7 @@
#include "shell/browser/api/trackable_object.h"
#include "shell/browser/ui/tray_icon.h"
#include "shell/browser/ui/tray_icon_observer.h"
#include "shell/common/gin_helper/error_thrower.h"
namespace gfx {
class Image;
@ -34,16 +35,15 @@ class NativeImage;
class Tray : public mate::TrackableObject<Tray>, public TrayIconObserver {
public:
static mate::WrappableBase* New(mate::Handle<NativeImage> image,
mate::Arguments* args);
static mate::WrappableBase* New(gin_helper::ErrorThrower thrower,
mate::Handle<NativeImage> image,
gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
protected:
Tray(v8::Isolate* isolate,
v8::Local<v8::Object> wrapper,
mate::Handle<NativeImage> image);
Tray(mate::Handle<NativeImage> image, gin::Arguments* args);
~Tray() override;
// TrayIconObserver:

Просмотреть файл

@ -6,21 +6,22 @@
#include <utility>
#include "gin/handle.h"
#include "mojo/public/cpp/bindings/receiver_set.h"
#include "mojo/public/cpp/system/string_data_source.h"
#include "native_mate/dictionary.h"
#include "net/http/http_util.h"
#include "services/network/public/mojom/chunked_data_pipe_getter.mojom.h"
#include "shell/browser/api/atom_api_session.h"
#include "shell/browser/atom_browser_context.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/net_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/gin_helper/event_emitter_caller.h"
#include "shell/common/gin_helper/object_template_builder.h"
#include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
#include "shell/common/node_includes.h"
namespace mate {
namespace gin {
template <>
struct Converter<network::mojom::RedirectMode> {
@ -42,7 +43,7 @@ struct Converter<network::mojom::RedirectMode> {
}
};
} // namespace mate
} // namespace gin
namespace electron {
@ -165,9 +166,9 @@ class ChunkedDataPipeGetter : public UploadDataPipeGetter,
mojo::ReceiverSet<network::mojom::ChunkedDataPipeGetter> receiver_set_;
};
URLRequestNS::URLRequestNS(mate::Arguments* args) : weak_factory_(this) {
URLRequestNS::URLRequestNS(gin::Arguments* args) : weak_factory_(this) {
request_ = std::make_unique<network::ResourceRequest>();
mate::Dictionary dict;
gin_helper::Dictionary dict;
if (args->GetNext(&dict)) {
dict.Get("method", &request_->method);
dict.Get("url", &request_->url);
@ -186,7 +187,7 @@ URLRequestNS::URLRequestNS(mate::Arguments* args) : weak_factory_(this) {
url_loader_factory_ = session->browser_context()->GetURLLoaderFactory();
InitWith(args->isolate(), args->GetThis());
InitWithArgs(args);
}
URLRequestNS::~URLRequestNS() = default;
@ -518,7 +519,7 @@ void URLRequestNS::EmitEvent(EventType type, Args... args) {
}
// static
mate::WrappableBase* URLRequestNS::New(mate::Arguments* args) {
mate::WrappableBase* URLRequestNS::New(gin::Arguments* args) {
return new URLRequestNS(args);
}

Просмотреть файл

@ -28,13 +28,13 @@ class UploadDataPipeGetter;
class URLRequestNS : public mate::EventEmitter<URLRequestNS>,
public network::SimpleURLLoaderStreamConsumer {
public:
static mate::WrappableBase* New(mate::Arguments* args);
static mate::WrappableBase* New(gin::Arguments* args);
static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> prototype);
protected:
explicit URLRequestNS(mate::Arguments* args);
explicit URLRequestNS(gin::Arguments* args);
~URLRequestNS() override;
bool NotStarted() const;
@ -141,22 +141,4 @@ class URLRequestNS : public mate::EventEmitter<URLRequestNS>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting URLRequestNS to gin::Wrapper.
template <>
struct Converter<electron::api::URLRequestNS*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::URLRequestNS** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::URLRequestNS* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_URL_REQUEST_NS_H_

Просмотреть файл

@ -42,9 +42,9 @@ void View::AddChildViewAt(mate::Handle<View> child, size_t index) {
#endif
// static
mate::WrappableBase* View::New(mate::Arguments* args) {
mate::WrappableBase* View::New(gin::Arguments* args) {
auto* view = new View();
view->InitWith(args->isolate(), args->GetThis());
view->InitWithArgs(args);
return view;
}

Просмотреть файл

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

Просмотреть файл

@ -575,22 +575,4 @@ class WebContents : public mate::TrackableObject<WebContents>,
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting WebContents to gin::Wrapper.
template <>
struct Converter<electron::api::WebContents*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::WebContents** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::WebContents* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_BROWSER_API_ATOM_API_WEB_CONTENTS_H_

Просмотреть файл

@ -80,7 +80,7 @@ void WebContentsView::WebContentsDestroyed() {
// static
mate::WrappableBase* WebContentsView::New(
mate::Arguments* args,
gin::Arguments* args,
mate::Handle<WebContents> web_contents) {
// Currently we only support InspectableWebContents, e.g. the WebContents
// created by users directly. To support devToolsWebContents we need to create
@ -101,7 +101,7 @@ mate::WrappableBase* WebContentsView::New(
// Constructor call.
auto* view = new WebContentsView(args->isolate(), web_contents,
web_contents->managed_web_contents());
view->InitWith(args->isolate(), args->GetThis());
view->InitWithArgs(args);
return view;
}

Просмотреть файл

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

Просмотреть файл

@ -13,8 +13,6 @@
#include "gin/arguments.h"
#include "gin/handle.h"
#include "gin/wrappable.h"
#include "native_mate/dictionary.h"
#include "native_mate/handle.h"
#include "shell/browser/net/proxying_url_loader_factory.h"
namespace content {

Просмотреть файл

@ -23,16 +23,16 @@
#include "shell/browser/net/node_stream_loader.h"
#include "shell/browser/net/url_pipe_loader.h"
#include "shell/common/atom_constants.h"
#include "shell/common/native_mate_converters/file_path_converter.h"
#include "shell/common/native_mate_converters/gurl_converter.h"
#include "shell/common/native_mate_converters/net_converter.h"
#include "shell/common/native_mate_converters/value_converter.h"
#include "shell/common/gin_converters/file_path_converter.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/net_converter.h"
#include "shell/common/gin_converters/value_converter_gin_adapter.h"
#include "shell/common/node_includes.h"
using content::BrowserThread;
namespace mate {
namespace gin {
template <>
struct Converter<electron::ProtocolType> {
@ -58,7 +58,7 @@ struct Converter<electron::ProtocolType> {
}
};
} // namespace mate
} // namespace gin
namespace electron {
@ -77,17 +77,18 @@ bool ResponseMustBeObject(ProtocolType type) {
}
// Helper to convert value to Dictionary.
mate::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
gin::Dictionary ToDict(v8::Isolate* isolate, v8::Local<v8::Value> value) {
if (!value->IsFunction() && value->IsObject())
return mate::Dictionary(
return gin::Dictionary(
isolate,
value->ToObject(isolate->GetCurrentContext()).ToLocalChecked());
else
return mate::Dictionary();
return gin::Dictionary(isolate);
}
// Parse headers from response object.
network::ResourceResponseHead ToResponseHead(const mate::Dictionary& dict) {
network::ResourceResponseHead ToResponseHead(
const gin_helper::Dictionary& dict) {
network::ResourceResponseHead head;
head.mime_type = "text/html";
head.charset = "utf-8";
@ -197,7 +198,7 @@ void AtomURLLoaderFactory::StartLoading(
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
network::mojom::URLLoaderFactory* proxy_factory,
ProtocolType type,
mate::Arguments* args) {
gin::Arguments* args) {
// Send network error when there is no argument passed.
//
// Note that we should not throw JS error in the callback no matter what is
@ -210,7 +211,7 @@ void AtomURLLoaderFactory::StartLoading(
}
// Parse {error} object.
mate::Dictionary dict = ToDict(args->isolate(), response);
gin_helper::Dictionary dict = ToDict(args->isolate(), response);
if (!dict.IsEmpty()) {
int error_code;
if (dict.Get("error", &error_code)) {
@ -251,7 +252,7 @@ void AtomURLLoaderFactory::StartLoading(
} else {
StartLoadingHttp(std::move(loader), new_request, std::move(client),
traffic_annotation,
mate::Dictionary::CreateEmpty(args->isolate()));
gin::Dictionary::CreateEmpty(args->isolate()));
}
return;
}
@ -285,7 +286,7 @@ void AtomURLLoaderFactory::StartLoading(
break;
case ProtocolType::kFree:
ProtocolType type;
if (!mate::ConvertFromV8(args->isolate(), response, &type)) {
if (!gin::ConvertFromV8(args->isolate(), response, &type)) {
client->OnComplete(network::URLLoaderCompletionStatus(net::ERR_FAILED));
return;
}
@ -300,7 +301,7 @@ void AtomURLLoaderFactory::StartLoading(
void AtomURLLoaderFactory::StartLoadingBuffer(
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
v8::Local<v8::Value> buffer = dict.GetHandle();
dict.Get("data", &buffer);
if (!node::Buffer::HasInstance(buffer)) {
@ -317,7 +318,7 @@ void AtomURLLoaderFactory::StartLoadingBuffer(
void AtomURLLoaderFactory::StartLoadingString(
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response) {
std::string contents;
@ -339,11 +340,11 @@ void AtomURLLoaderFactory::StartLoadingFile(
network::ResourceRequest request,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response) {
base::FilePath path;
if (mate::ConvertFromV8(isolate, response, &path)) {
if (gin::ConvertFromV8(isolate, response, &path)) {
request.url = net::FilePathToFileURL(path);
} else if (!dict.IsEmpty()) {
dict.Get("referrer", &request.referrer);
@ -366,7 +367,7 @@ void AtomURLLoaderFactory::StartLoadingHttp(
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
auto request = std::make_unique<network::ResourceRequest>();
request->headers = original_request.headers;
request->cors_exempt_headers = original_request.cors_exempt_headers;
@ -407,7 +408,7 @@ void AtomURLLoaderFactory::StartLoadingStream(
network::mojom::URLLoaderRequest loader,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict) {
const gin_helper::Dictionary& dict) {
v8::Local<v8::Value> stream;
if (!dict.Get("data", &stream)) {
// Assume the opts is already a stream.
@ -435,7 +436,7 @@ void AtomURLLoaderFactory::StartLoadingStream(
return;
}
mate::Dictionary data = ToDict(dict.isolate(), stream);
gin_helper::Dictionary data = ToDict(dict.isolate(), stream);
v8::Local<v8::Value> method;
if (!data.Get("on", &method) || !method->IsFunction() ||
!data.Get("removeListener", &method) || !method->IsFunction()) {

Просмотреть файл

@ -10,10 +10,10 @@
#include <utility>
#include "mojo/public/cpp/bindings/binding_set.h"
#include "native_mate/dictionary.h"
#include "net/url_request/url_request_job_factory.h"
#include "services/network/public/cpp/resource_response.h"
#include "services/network/public/mojom/url_loader_factory.mojom.h"
#include "shell/common/gin_helper/dictionary.h"
namespace electron {
@ -27,7 +27,7 @@ enum class ProtocolType {
kFree, // special type for returning arbitrary type of response.
};
using StartLoadingCallback = base::OnceCallback<void(mate::Arguments*)>;
using StartLoadingCallback = base::OnceCallback<void(gin::Arguments*)>;
using ProtocolHandler =
base::Callback<void(const network::ResourceRequest&, StartLoadingCallback)>;
@ -62,22 +62,22 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
network::mojom::URLLoaderFactory* proxy_factory,
ProtocolType type,
mate::Arguments* args);
gin::Arguments* args);
private:
static void StartLoadingBuffer(network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
static void StartLoadingString(network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
static void StartLoadingFile(network::mojom::URLLoaderRequest loader,
network::ResourceRequest request,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict,
const gin_helper::Dictionary& dict,
v8::Isolate* isolate,
v8::Local<v8::Value> response);
static void StartLoadingHttp(
@ -85,11 +85,11 @@ class AtomURLLoaderFactory : public network::mojom::URLLoaderFactory {
const network::ResourceRequest& original_request,
network::mojom::URLLoaderClientPtr client,
const net::MutableNetworkTrafficAnnotationTag& traffic_annotation,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
static void StartLoadingStream(network::mojom::URLLoaderRequest loader,
network::mojom::URLLoaderClientPtr client,
network::ResourceResponseHead head,
const mate::Dictionary& dict);
const gin_helper::Dictionary& dict);
// Helper to send string as response.
static void SendContents(network::mojom::URLLoaderClientPtr client,

Просмотреть файл

@ -61,22 +61,4 @@ class KeyWeakMap : public mate::Wrappable<KeyWeakMap<K>> {
} // namespace electron
namespace gin {
// TODO(zcbenz): Remove this after converting KeyWeakMap to gin::Wrapper.
template <typename T>
struct Converter<electron::api::KeyWeakMap<T>*> {
static bool FromV8(v8::Isolate* isolate,
v8::Local<v8::Value> val,
electron::api::KeyWeakMap<T>** out) {
return mate::ConvertFromV8(isolate, val, out);
}
static v8::Local<v8::Value> ToV8(v8::Isolate* isolate,
electron::api::KeyWeakMap<T>* in) {
return mate::ConvertToV8(isolate, in);
}
};
} // namespace gin
#endif // SHELL_COMMON_API_ATOM_API_KEY_WEAK_MAP_H_

Просмотреть файл

@ -8,7 +8,6 @@
#include "base/hash/hash.h"
#include "electron/buildflags/buildflags.h"
#include "shell/common/gin_converters/gurl_converter.h"
#include "shell/common/gin_converters/native_mate_handle_converter.h"
#include "shell/common/gin_helper/dictionary.h"
#include "shell/common/node_includes.h"
#include "url/origin.h"

Просмотреть файл

@ -1,30 +0,0 @@
// Copyright (c) 2019 GitHub, Inc.
// Use of this source code is governed by the MIT license that can be
// found in the LICENSE file.
#ifndef SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_
#define SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_
#include "gin/converter.h"
#include "native_mate/handle.h"
namespace gin {
// TODO(zcbenz): Remove this converter after native_mate is removed.
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 // SHELL_COMMON_GIN_CONVERTERS_NATIVE_MATE_HANDLE_CONVERTER_H_

Просмотреть файл

@ -27,6 +27,23 @@ class Dictionary : public gin::Dictionary {
Dictionary(const gin::Dictionary& dict) // NOLINT(runtime/explicit)
: gin::Dictionary(dict) {}
// Difference from the Get method in gin::Dictionary:
// 1. This is a const method;
// 2. It checks whether the key exists before reading.
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 = gin::StringToV8(isolate(), key);
v8::Local<v8::Value> value;
v8::Maybe<bool> result = GetHandle()->Has(context, v8_key);
if (result.IsJust() && result.FromJust() &&
GetHandle()->Get(context, v8_key).ToLocal(&value))
return gin::ConvertFromV8(isolate(), value, out);
return false;
}
template <typename T>
bool GetHidden(base::StringPiece key, T* out) const {
v8::Local<v8::Context> context = isolate()->GetCurrentContext();
@ -80,6 +97,8 @@ class Dictionary : public gin::Dictionary {
return !result.IsNothing() && result.FromJust();
}
bool IsEmpty() const { return isolate() == nullptr || GetHandle().IsEmpty(); }
v8::Local<v8::Object> GetHandle() const {
return gin::ConvertToV8(isolate(),
*static_cast<const gin::Dictionary*>(this))