From 19223952a86e7e25bdf1413e1efed5428c9ac2f1 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Fri, 18 Oct 2019 09:31:29 +0900 Subject: [PATCH] chore: remove native_mate (Part 8) (#20598) * refactor: convert methods of AutoUpdater to gin * refactor: converter in map_converter.h is no more needed * refactor: use gin in crash_reporter * refactor: remove native_mate_converters/map_converter.h * refactor: implement gfx_converter with gin * refactor: convert methods of NativeImage to gin * refactor: add gin_helper::Arguments * fix: use gin_helper::Arguments to parse multi-type parameters --- filenames.gni | 5 +- shell/app/node_main.cc | 10 +-- shell/browser/api/atom_api_auto_updater.cc | 29 +++---- shell/browser/api/atom_api_auto_updater.h | 7 +- shell/browser/api/atom_api_web_contents.cc | 4 +- shell/browser/auto_updater.cc | 2 +- shell/browser/auto_updater.h | 10 ++- shell/browser/auto_updater_mac.mm | 19 +++-- shell/common/api/atom_api_crash_reporter.cc | 3 +- shell/common/api/atom_api_native_image.cc | 84 ++++++++++--------- shell/common/api/atom_api_native_image.h | 57 +++++++++---- shell/common/api/atom_api_native_image_mac.mm | 2 +- shell/common/api/atom_api_shell.cc | 5 +- shell/common/crash_reporter/crash_reporter.cc | 6 +- shell/common/crash_reporter/crash_reporter.h | 10 ++- .../gfx_converter.cc | 15 ++-- shell/common/gin_converters/gfx_converter.h | 67 +++++++++++++++ shell/common/gin_converters/std_converter.h | 44 ++++++++++ shell/common/gin_helper/arguments.h | 34 ++++++++ shell/common/gin_helper/dictionary.h | 27 ++++-- shell/common/gin_helper/function_template.h | 10 +++ .../native_mate_converters/gfx_converter.h | 53 +++++++----- .../native_mate_converters/map_converter.h | 70 ---------------- 23 files changed, 361 insertions(+), 212 deletions(-) rename shell/common/{native_mate_converters => gin_converters}/gfx_converter.cc (93%) create mode 100644 shell/common/gin_converters/gfx_converter.h create mode 100644 shell/common/gin_helper/arguments.h delete mode 100644 shell/common/native_mate_converters/map_converter.h diff --git a/filenames.gni b/filenames.gni index fc03b63bb3..2e369d0723 100644 --- a/filenames.gni +++ b/filenames.gni @@ -473,6 +473,8 @@ filenames = { "shell/common/gin_converters/file_dialog_converter.cc", "shell/common/gin_converters/file_dialog_converter.h", "shell/common/gin_converters/file_path_converter.h", + "shell/common/gin_converters/gfx_converter.cc", + "shell/common/gin_converters/gfx_converter.h", "shell/common/gin_converters/gurl_converter.h", "shell/common/gin_converters/image_converter.cc", "shell/common/gin_converters/image_converter.h", @@ -484,6 +486,7 @@ filenames = { "shell/common/gin_converters/std_converter.h", "shell/common/gin_converters/blink_converter_gin_adapter.h", "shell/common/gin_converters/value_converter_gin_adapter.h", + "shell/common/gin_helper/arguments.h", "shell/common/gin_helper/callback.cc", "shell/common/gin_helper/callback.h", "shell/common/gin_helper/destroyable.cc", @@ -518,11 +521,9 @@ filenames = { "shell/common/native_mate_converters/content_converter.h", "shell/common/native_mate_converters/file_dialog_converter.h", "shell/common/native_mate_converters/file_path_converter.h", - "shell/common/native_mate_converters/gfx_converter.cc", "shell/common/native_mate_converters/gfx_converter.h", "shell/common/native_mate_converters/gurl_converter.h", "shell/common/native_mate_converters/image_converter.h", - "shell/common/native_mate_converters/map_converter.h", "shell/common/native_mate_converters/native_window_converter.h", "shell/common/native_mate_converters/net_converter.h", "shell/common/native_mate_converters/network_converter.cc", diff --git a/shell/app/node_main.cc b/shell/app/node_main.cc index 56a17878df..e586e49b84 100644 --- a/shell/app/node_main.cc +++ b/shell/app/node_main.cc @@ -16,13 +16,12 @@ #include "gin/array_buffer.h" #include "gin/public/isolate_holder.h" #include "gin/v8_initializer.h" -#include "native_mate/dictionary.h" #include "shell/app/uv_task_runner.h" #include "shell/browser/javascript_environment.h" #include "shell/browser/node_debugger.h" #include "shell/common/api/electron_bindings.h" #include "shell/common/crash_reporter/crash_reporter.h" -#include "shell/common/native_mate_converters/string16_converter.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/node_bindings.h" #include "shell/common/node_includes.h" @@ -94,14 +93,15 @@ int NodeMain(int argc, char* argv[]) { node::BootstrapEnvironment(env); - mate::Dictionary process(gin_env.isolate(), env->process_object()); + gin_helper::Dictionary process(gin_env.isolate(), env->process_object()); #if defined(OS_WIN) process.SetMethod("log", &ElectronBindings::Log); #endif process.SetMethod("crash", &ElectronBindings::Crash); // Setup process.crashReporter.start in child node processes - auto reporter = mate::Dictionary::CreateEmpty(gin_env.isolate()); + gin_helper::Dictionary reporter = + gin::Dictionary::CreateEmpty(gin_env.isolate()); reporter.SetMethod("start", &crash_reporter::CrashReporter::StartInstance); #if !defined(OS_LINUX) @@ -111,7 +111,7 @@ int NodeMain(int argc, char* argv[]) { process.Set("crashReporter", reporter); - mate::Dictionary versions; + gin_helper::Dictionary versions; if (process.Get("versions", &versions)) { versions.SetReadOnly(ELECTRON_PROJECT_NAME, ELECTRON_VERSION_STRING); } diff --git a/shell/browser/api/atom_api_auto_updater.cc b/shell/browser/api/atom_api_auto_updater.cc index 2b2342c375..89a85c3c43 100644 --- a/shell/browser/api/atom_api_auto_updater.cc +++ b/shell/browser/api/atom_api_auto_updater.cc @@ -5,15 +5,16 @@ #include "shell/browser/api/atom_api_auto_updater.h" #include "base/time/time.h" -#include "native_mate/dictionary.h" -#include "native_mate/object_template_builder_deprecated.h" #include "shell/browser/browser.h" #include "shell/browser/native_window.h" #include "shell/browser/window_list.h" +#include "shell/common/gin_converters/callback_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/node_includes.h" -// TODO(zcbenz): Remove this after removing mate::ObjectTemplateBuilder. +// TODO(zcbenz): Remove this after removing mate::EventEmitter. #include "shell/common/native_mate_converters/callback_converter_deprecated.h" namespace mate { @@ -49,7 +50,7 @@ AutoUpdater::~AutoUpdater() { void AutoUpdater::OnError(const std::string& message) { v8::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); - auto error = v8::Exception::Error(mate::StringToV8(isolate(), message)); + auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); gin_helper::EmitEvent( isolate(), GetWrapper(), "error", error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(), @@ -62,7 +63,7 @@ void AutoUpdater::OnError(const std::string& message, const std::string& domain) { v8::Locker locker(isolate()); v8::HandleScope handle_scope(isolate()); - auto error = v8::Exception::Error(mate::StringToV8(isolate(), message)); + auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); auto errorObject = error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(); @@ -70,12 +71,12 @@ void AutoUpdater::OnError(const std::string& message, // add two new params for better error handling errorObject - ->Set(context, mate::StringToV8(isolate(), "code"), + ->Set(context, gin::StringToV8(isolate(), "code"), v8::Integer::New(isolate(), code)) .Check(); errorObject - ->Set(context, mate::StringToV8(isolate(), "domain"), - mate::StringToV8(isolate(), domain)) + ->Set(context, gin::StringToV8(isolate(), "domain"), + gin::StringToV8(isolate(), domain)) .Check(); gin_helper::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message); @@ -107,7 +108,7 @@ void AutoUpdater::OnWindowAllClosed() { QuitAndInstall(); } -void AutoUpdater::SetFeedURL(mate::Arguments* args) { +void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { auto_updater::AutoUpdater::SetFeedURL(args); } @@ -126,15 +127,15 @@ void AutoUpdater::QuitAndInstall() { } // static -mate::Handle AutoUpdater::Create(v8::Isolate* isolate) { - return mate::CreateHandle(isolate, new AutoUpdater(isolate)); +gin::Handle AutoUpdater::Create(v8::Isolate* isolate) { + return gin::CreateHandle(isolate, new AutoUpdater(isolate)); } // static void AutoUpdater::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { - prototype->SetClassName(mate::StringToV8(isolate, "AutoUpdater")); - mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + prototype->SetClassName(gin::StringToV8(isolate, "AutoUpdater")); + gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates) .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL) .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL) @@ -154,7 +155,7 @@ void Initialize(v8::Local exports, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); + gin_helper::Dictionary dict(isolate, exports); dict.Set("autoUpdater", AutoUpdater::Create(isolate)); dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate) ->GetFunction(context) diff --git a/shell/browser/api/atom_api_auto_updater.h b/shell/browser/api/atom_api_auto_updater.h index ab361cf663..8d02a7273e 100644 --- a/shell/browser/api/atom_api_auto_updater.h +++ b/shell/browser/api/atom_api_auto_updater.h @@ -7,8 +7,7 @@ #include -#include "native_mate/arguments.h" -#include "native_mate/handle.h" +#include "gin/handle.h" #include "shell/browser/api/event_emitter.h" #include "shell/browser/auto_updater.h" #include "shell/browser/window_list_observer.h" @@ -21,7 +20,7 @@ class AutoUpdater : public mate::EventEmitter, public auto_updater::Delegate, public WindowListObserver { public: - static mate::Handle Create(v8::Isolate* isolate); + static gin::Handle Create(v8::Isolate* isolate); static void BuildPrototype(v8::Isolate* isolate, v8::Local prototype); @@ -48,7 +47,7 @@ class AutoUpdater : public mate::EventEmitter, private: std::string GetFeedURL(); - void SetFeedURL(mate::Arguments* args); + void SetFeedURL(gin_helper::Arguments* args); void QuitAndInstall(); DISALLOW_COPY_AND_ASSIGN(AutoUpdater); diff --git a/shell/browser/api/atom_api_web_contents.cc b/shell/browser/api/atom_api_web_contents.cc index e1aa853569..4bd6d6816a 100644 --- a/shell/browser/api/atom_api_web_contents.cc +++ b/shell/browser/api/atom_api_web_contents.cc @@ -72,6 +72,7 @@ #include "shell/browser/web_view_guest_delegate.h" #include "shell/common/api/atom_api_native_image.h" #include "shell/common/color_util.h" +#include "shell/common/gin_helper/dictionary.h" #include "shell/common/mouse_util.h" #include "shell/common/native_mate_converters/blink_converter.h" #include "shell/common/native_mate_converters/content_converter.h" @@ -79,7 +80,6 @@ #include "shell/common/native_mate_converters/gfx_converter.h" #include "shell/common/native_mate_converters/gurl_converter.h" #include "shell/common/native_mate_converters/image_converter.h" -#include "shell/common/native_mate_converters/map_converter.h" #include "shell/common/native_mate_converters/net_converter.h" #include "shell/common/native_mate_converters/network_converter.h" #include "shell/common/native_mate_converters/once_callback.h" @@ -130,7 +130,7 @@ template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, const printing::PrinterBasicInfo& val) { - mate::Dictionary dict(isolate, v8::Object::New(isolate)); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); dict.Set("name", val.printer_name); dict.Set("description", val.printer_description); dict.Set("status", val.printer_status); diff --git a/shell/browser/auto_updater.cc b/shell/browser/auto_updater.cc index f1b4f4b68d..a9bcf5fa66 100644 --- a/shell/browser/auto_updater.cc +++ b/shell/browser/auto_updater.cc @@ -21,7 +21,7 @@ std::string AutoUpdater::GetFeedURL() { return ""; } -void AutoUpdater::SetFeedURL(mate::Arguments* args) {} +void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) {} void AutoUpdater::CheckForUpdates() {} diff --git a/shell/browser/auto_updater.h b/shell/browser/auto_updater.h index 98f1ef6a94..1e76287649 100644 --- a/shell/browser/auto_updater.h +++ b/shell/browser/auto_updater.h @@ -10,12 +10,15 @@ #include "base/macros.h" #include "build/build_config.h" -#include "native_mate/arguments.h" namespace base { class Time; } +namespace gin_helper { +class Arguments; +} + namespace auto_updater { class Delegate { @@ -55,7 +58,10 @@ class AutoUpdater { static void SetDelegate(Delegate* delegate); static std::string GetFeedURL(); - static void SetFeedURL(mate::Arguments* args); + // FIXME(zcbenz): We should not do V8 in this file, this method should only + // accept C++ struct as parameter, and atom_api_auto_updater.cc is responsible + // for parsing the parameter from JavaScript. + static void SetFeedURL(gin_helper::Arguments* args); static void CheckForUpdates(); static void QuitAndInstall(); diff --git a/shell/browser/auto_updater_mac.mm b/shell/browser/auto_updater_mac.mm index 9bf86539fd..d2abbc042d 100644 --- a/shell/browser/auto_updater_mac.mm +++ b/shell/browser/auto_updater_mac.mm @@ -14,10 +14,11 @@ #include "base/bind.h" #include "base/strings/sys_string_conversions.h" #include "base/time/time.h" -#include "native_mate/dictionary.h" +#include "gin/arguments.h" #include "shell/browser/browser.h" -#include "shell/common/native_mate_converters/map_converter.h" -#include "shell/common/native_mate_converters/value_converter.h" +#include "shell/common/gin_converters/value_converter_gin_adapter.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/error_thrower.h" namespace auto_updater { @@ -40,14 +41,16 @@ std::string AutoUpdater::GetFeedURL() { } // static -void AutoUpdater::SetFeedURL(mate::Arguments* args) { - mate::Dictionary opts; +void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { + gin_helper::ErrorThrower thrower(args->isolate()); + gin_helper::Dictionary opts; + std::string feed; HeaderMap requestHeaders; std::string serverType = "default"; if (args->GetNext(&opts)) { if (!opts.Get("url", &feed)) { - args->ThrowError( + thrower.ThrowError( "Expected options object to contain a 'url' string property in " "setFeedUrl call"); return; @@ -55,13 +58,13 @@ void AutoUpdater::SetFeedURL(mate::Arguments* args) { opts.Get("headers", &requestHeaders); opts.Get("serverType", &serverType); if (serverType != "default" && serverType != "json") { - args->ThrowError("Expected serverType to be 'default' or 'json'"); + thrower.ThrowError("Expected serverType to be 'default' or 'json'"); return; } } else if (args->GetNext(&feed)) { args->GetNext(&requestHeaders); } else { - args->ThrowError( + thrower.ThrowError( "Expected an options object with a 'url' property to be provided"); return; } diff --git a/shell/common/api/atom_api_crash_reporter.cc b/shell/common/api/atom_api_crash_reporter.cc index 2b6019409b..61ee66c2fb 100644 --- a/shell/common/api/atom_api_crash_reporter.cc +++ b/shell/common/api/atom_api_crash_reporter.cc @@ -9,9 +9,8 @@ #include "gin/data_object_builder.h" #include "shell/common/crash_reporter/crash_reporter.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/native_mate_converters/file_path_converter.h" -#include "shell/common/native_mate_converters/map_converter.h" #include "shell/common/node_includes.h" diff --git a/shell/common/api/atom_api_native_image.cc b/shell/common/api/atom_api_native_image.cc index 3d6d395cbc..2f7c3be865 100644 --- a/shell/common/api/atom_api_native_image.cc +++ b/shell/common/api/atom_api_native_image.cc @@ -13,13 +13,14 @@ #include "base/strings/pattern.h" #include "base/strings/string_util.h" #include "base/threading/thread_restrictions.h" -#include "native_mate/object_template_builder_deprecated.h" #include "net/base/data_url.h" #include "shell/common/asar/asar_util.h" -#include "shell/common/native_mate_converters/file_path_converter.h" -#include "shell/common/native_mate_converters/gfx_converter.h" -#include "shell/common/native_mate_converters/gurl_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/gfx_converter.h" +#include "shell/common/gin_converters/gurl_converter.h" +#include "shell/common/gin_converters/value_converter_gin_adapter.h" +#include "shell/common/gin_helper/dictionary.h" +#include "shell/common/gin_helper/object_template_builder.h" #include "shell/common/node_includes.h" #include "shell/common/skia_util.h" #include "third_party/skia/include/core/SkBitmap.h" @@ -47,9 +48,9 @@ namespace api { namespace { // Get the scale factor from options object at the first argument -float GetScaleFactorFromOptions(mate::Arguments* args) { +float GetScaleFactorFromOptions(gin::Arguments* args) { float scale_factor = 1.0f; - mate::Dictionary options; + gin_helper::Dictionary options; if (args->GetNext(&options)) options.Get("scaleFactor", &scale_factor); return scale_factor; @@ -151,7 +152,7 @@ HICON NativeImage::GetHICON(int size) { } #endif -v8::Local NativeImage::ToPNG(mate::Arguments* args) { +v8::Local NativeImage::ToPNG(gin::Arguments* args) { float scale_factor = GetScaleFactorFromOptions(args); if (scale_factor == 1.0f) { @@ -173,7 +174,7 @@ v8::Local NativeImage::ToPNG(mate::Arguments* args) { return node::Buffer::Copy(args->isolate(), data, size).ToLocalChecked(); } -v8::Local NativeImage::ToBitmap(mate::Arguments* args) { +v8::Local NativeImage::ToBitmap(gin::Arguments* args) { float scale_factor = GetScaleFactorFromOptions(args); const SkBitmap bitmap = @@ -198,7 +199,7 @@ v8::Local NativeImage::ToJPEG(v8::Isolate* isolate, int quality) { .ToLocalChecked(); } -std::string NativeImage::ToDataURL(mate::Arguments* args) { +std::string NativeImage::ToDataURL(gin::Arguments* args) { float scale_factor = GetScaleFactorFromOptions(args); if (scale_factor == 1.0f) { @@ -212,7 +213,7 @@ std::string NativeImage::ToDataURL(mate::Arguments* args) { image_.AsImageSkia().GetRepresentation(scale_factor).GetBitmap()); } -v8::Local NativeImage::GetBitmap(mate::Arguments* args) { +v8::Local NativeImage::GetBitmap(gin::Arguments* args) { float scale_factor = GetScaleFactorFromOptions(args); const SkBitmap bitmap = @@ -226,19 +227,19 @@ v8::Local NativeImage::GetBitmap(mate::Arguments* args) { .ToLocalChecked(); } -v8::Local NativeImage::GetNativeHandle(v8::Isolate* isolate, - mate::Arguments* args) { +v8::Local NativeImage::GetNativeHandle( + gin_helper::ErrorThrower thrower) { #if defined(OS_MACOSX) if (IsEmpty()) - return node::Buffer::New(isolate, 0).ToLocalChecked(); + return node::Buffer::New(thrower.isolate(), 0).ToLocalChecked(); NSImage* ptr = image_.AsNSImage(); - return node::Buffer::Copy(isolate, reinterpret_cast(ptr), + return node::Buffer::Copy(thrower.isolate(), reinterpret_cast(ptr), sizeof(void*)) .ToLocalChecked(); #else - args->ThrowError("Not implemented"); - return v8::Undefined(isolate); + thrower.ThrowError("Not implemented"); + return v8::Undefined(thrower.isolate()); #endif } @@ -301,7 +302,7 @@ mate::Handle NativeImage::Crop(v8::Isolate* isolate, new NativeImage(isolate, gfx::Image(cropped))); } -void NativeImage::AddRepresentation(const mate::Dictionary& options) { +void NativeImage::AddRepresentation(const gin_helper::Dictionary& options) { int width = 0; int height = 0; float scale_factor = 1.0f; @@ -400,11 +401,11 @@ mate::Handle NativeImage::CreateFromPath( // static mate::Handle NativeImage::CreateFromBitmap( - mate::Arguments* args, + gin_helper::ErrorThrower thrower, v8::Local buffer, - const mate::Dictionary& options) { + const gin_helper::Dictionary& options) { if (!node::Buffer::HasInstance(buffer)) { - args->ThrowError("buffer must be a node Buffer"); + thrower.ThrowError("buffer must be a node Buffer"); return mate::Handle(); } @@ -413,12 +414,12 @@ mate::Handle NativeImage::CreateFromBitmap( double scale_factor = 1.; if (!options.Get("width", &width)) { - args->ThrowError("width is required"); + thrower.ThrowError("width is required"); return mate::Handle(); } if (!options.Get("height", &height)) { - args->ThrowError("height is required"); + thrower.ThrowError("height is required"); return mate::Handle(); } @@ -426,14 +427,14 @@ mate::Handle NativeImage::CreateFromBitmap( auto size_bytes = info.computeMinByteSize(); if (size_bytes != node::Buffer::Length(buffer)) { - args->ThrowError("invalid buffer size"); + thrower.ThrowError("invalid buffer size"); return mate::Handle(); } options.Get("scaleFactor", &scale_factor); if (width == 0 || height == 0) { - return CreateEmpty(args->isolate()); + return CreateEmpty(thrower.isolate()); } SkBitmap bitmap; @@ -443,15 +444,16 @@ mate::Handle NativeImage::CreateFromBitmap( gfx::ImageSkia image_skia; image_skia.AddRepresentation(gfx::ImageSkiaRep(bitmap, scale_factor)); - return Create(args->isolate(), gfx::Image(image_skia)); + return Create(thrower.isolate(), gfx::Image(image_skia)); } // static mate::Handle NativeImage::CreateFromBuffer( - mate::Arguments* args, - v8::Local buffer) { + gin_helper::ErrorThrower thrower, + v8::Local buffer, + gin::Arguments* args) { if (!node::Buffer::HasInstance(buffer)) { - args->ThrowError("buffer must be a node Buffer"); + thrower.ThrowError("buffer must be a node Buffer"); return mate::Handle(); } @@ -459,7 +461,7 @@ mate::Handle NativeImage::CreateFromBuffer( int height = 0; double scale_factor = 1.; - mate::Dictionary options; + gin_helper::Dictionary options; if (args->GetNext(&options)) { options.Get("width", &width); options.Get("height", &height); @@ -489,7 +491,7 @@ mate::Handle NativeImage::CreateFromDataURL(v8::Isolate* isolate, #if !defined(OS_MACOSX) mate::Handle NativeImage::CreateFromNamedImage( - mate::Arguments* args, + gin::Arguments* args, const std::string& name) { return CreateEmpty(args->isolate()); } @@ -498,8 +500,8 @@ mate::Handle NativeImage::CreateFromNamedImage( // static void NativeImage::BuildPrototype(v8::Isolate* isolate, v8::Local prototype) { - prototype->SetClassName(mate::StringToV8(isolate, "NativeImage")); - mate::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) + prototype->SetClassName(gin::StringToV8(isolate, "NativeImage")); + gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) .SetMethod("toPNG", &NativeImage::ToPNG) .SetMethod("toJPEG", &NativeImage::ToJPEG) .SetMethod("toBitmap", &NativeImage::ToBitmap) @@ -522,7 +524,7 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate, } // namespace electron -namespace mate { +namespace gin { v8::Local Converter>::ToV8( v8::Isolate* isolate, @@ -542,17 +544,17 @@ bool Converter>::FromV8( return !(*out)->image().IsEmpty(); } - WrappableBase* wrapper = - static_cast(internal::FromV8Impl(isolate, val)); + auto* wrapper = static_cast( + mate::internal::FromV8Impl(isolate, val)); if (!wrapper) return false; - *out = - CreateHandle(isolate, static_cast(wrapper)); + *out = mate::CreateHandle(isolate, + static_cast(wrapper)); return true; } -} // namespace mate +} // namespace gin namespace { @@ -563,11 +565,11 @@ void Initialize(v8::Local exports, v8::Local context, void* priv) { v8::Isolate* isolate = context->GetIsolate(); - mate::Dictionary dict(isolate, exports); + gin_helper::Dictionary dict(isolate, exports); dict.Set("NativeImage", NativeImage::GetConstructor(isolate) ->GetFunction(context) .ToLocalChecked()); - mate::Dictionary native_image = mate::Dictionary::CreateEmpty(isolate); + gin_helper::Dictionary native_image = gin::Dictionary::CreateEmpty(isolate); dict.Set("nativeImage", native_image); native_image.SetMethod("createEmpty", &NativeImage::CreateEmpty); diff --git a/shell/common/api/atom_api_native_image.h b/shell/common/api/atom_api_native_image.h index 5915c74e10..dc2c8b1914 100644 --- a/shell/common/api/atom_api_native_image.h +++ b/shell/common/api/atom_api_native_image.h @@ -9,10 +9,9 @@ #include #include "base/values.h" -#include "native_mate/dictionary.h" #include "native_mate/handle.h" #include "native_mate/wrappable.h" -#include "ui/gfx/geometry/rect.h" +#include "shell/common/gin_helper/error_thrower.h" #include "ui/gfx/image/image.h" #if defined(OS_WIN) @@ -27,11 +26,12 @@ class FilePath; } namespace gfx { +class Rect; class Size; -} +} // namespace gfx -namespace mate { -class Arguments; +namespace gin_helper { +class Dictionary; } namespace electron { @@ -52,16 +52,17 @@ class NativeImage : public mate::Wrappable { static mate::Handle CreateFromPath(v8::Isolate* isolate, const base::FilePath& path); static mate::Handle CreateFromBitmap( - mate::Arguments* args, + gin_helper::ErrorThrower thrower, v8::Local buffer, - const mate::Dictionary& options); + const gin_helper::Dictionary& options); static mate::Handle CreateFromBuffer( - mate::Arguments* args, - v8::Local buffer); + gin_helper::ErrorThrower thrower, + v8::Local buffer, + gin::Arguments* args); static mate::Handle CreateFromDataURL(v8::Isolate* isolate, const GURL& url); static mate::Handle CreateFromNamedImage( - mate::Arguments* args, + gin::Arguments* args, const std::string& name); static void BuildPrototype(v8::Isolate* isolate, @@ -81,20 +82,19 @@ class NativeImage : public mate::Wrappable { ~NativeImage() override; private: - v8::Local ToPNG(mate::Arguments* args); + v8::Local ToPNG(gin::Arguments* args); v8::Local ToJPEG(v8::Isolate* isolate, int quality); - v8::Local ToBitmap(mate::Arguments* args); - v8::Local GetBitmap(mate::Arguments* args); - v8::Local GetNativeHandle(v8::Isolate* isolate, - mate::Arguments* args); + v8::Local ToBitmap(gin::Arguments* args); + v8::Local GetBitmap(gin::Arguments* args); + v8::Local GetNativeHandle(gin_helper::ErrorThrower thrower); mate::Handle Resize(v8::Isolate* isolate, const base::DictionaryValue& options); mate::Handle Crop(v8::Isolate* isolate, const gfx::Rect& rect); - std::string ToDataURL(mate::Arguments* args); + std::string ToDataURL(gin::Arguments* args); bool IsEmpty(); gfx::Size GetSize(); float GetAspectRatio(); - void AddRepresentation(const mate::Dictionary& options); + void AddRepresentation(const gin_helper::Dictionary& options); // Mark the image as template image. void SetTemplateImage(bool setAsTemplate); @@ -115,7 +115,7 @@ class NativeImage : public mate::Wrappable { } // namespace electron -namespace mate { +namespace gin { // A custom converter that allows converting path to NativeImage. template <> @@ -128,6 +128,27 @@ struct Converter> { mate::Handle* out); }; +} // namespace gin + +namespace mate { + +// Keep compatibility with native_mate code. +// +// TODO(zcbenz): Remove this after removing native_mate. +template <> +struct Converter> { + static v8::Local ToV8( + v8::Isolate* isolate, + const mate::Handle& val) { + return gin::ConvertToV8(isolate, val); + } + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + mate::Handle* out) { + return gin::ConvertFromV8(isolate, val, out); + } +}; + } // namespace mate #endif // SHELL_COMMON_API_ATOM_API_NATIVE_IMAGE_H_ diff --git a/shell/common/api/atom_api_native_image_mac.mm b/shell/common/api/atom_api_native_image_mac.mm index b0ac6cc580..5396cbf9cf 100644 --- a/shell/common/api/atom_api_native_image_mac.mm +++ b/shell/common/api/atom_api_native_image_mac.mm @@ -34,7 +34,7 @@ double safeShift(double in, double def) { } mate::Handle NativeImage::CreateFromNamedImage( - mate::Arguments* args, + gin::Arguments* args, const std::string& name) { @autoreleasepool { std::vector hsl_shift; diff --git a/shell/common/api/atom_api_shell.cc b/shell/common/api/atom_api_shell.cc index 532f7272cf..4a6fe31940 100644 --- a/shell/common/api/atom_api_shell.cc +++ b/shell/common/api/atom_api_shell.cc @@ -84,10 +84,9 @@ bool MoveItemToTrash(gin::Arguments* args) { #if defined(OS_WIN) bool WriteShortcutLink(const base::FilePath& shortcut_path, - gin::Arguments* args) { + gin_helper::Arguments* args) { base::win::ShortcutOperation operation = base::win::SHORTCUT_CREATE_ALWAYS; - if (gin::ConvertFromV8(args->isolate(), args->PeekNext(), &operation)) - args->Skip(); + args->GetNext(&operation); gin::Dictionary options = gin::Dictionary::CreateEmpty(args->isolate()); if (!args->GetNext(&options)) { args->ThrowError(); diff --git a/shell/common/crash_reporter/crash_reporter.cc b/shell/common/crash_reporter/crash_reporter.cc index b14a6e2285..ec756ed3fe 100644 --- a/shell/common/crash_reporter/crash_reporter.cc +++ b/shell/common/crash_reporter/crash_reporter.cc @@ -16,8 +16,8 @@ #include "electron/electron_version.h" #include "shell/browser/browser.h" #include "shell/common/atom_constants.h" -#include "shell/common/native_mate_converters/file_path_converter.h" -#include "shell/common/native_mate_converters/map_converter.h" +#include "shell/common/gin_converters/file_path_converter.h" +#include "shell/common/gin_helper/dictionary.h" namespace crash_reporter { @@ -126,7 +126,7 @@ CrashReporter* CrashReporter::GetInstance() { } #endif -void CrashReporter::StartInstance(const mate::Dictionary& options) { +void CrashReporter::StartInstance(const gin_helper::Dictionary& options) { auto* reporter = GetInstance(); if (!reporter) return; diff --git a/shell/common/crash_reporter/crash_reporter.h b/shell/common/crash_reporter/crash_reporter.h index e186e6698e..369d9089da 100644 --- a/shell/common/crash_reporter/crash_reporter.h +++ b/shell/common/crash_reporter/crash_reporter.h @@ -12,7 +12,10 @@ #include "base/files/file_path.h" #include "base/macros.h" -#include "native_mate/dictionary.h" + +namespace gin_helper { +class Dictionary; +} namespace crash_reporter { @@ -25,7 +28,10 @@ class CrashReporter { typedef std::pair UploadReportResult; // upload-date, id static CrashReporter* GetInstance(); - static void StartInstance(const mate::Dictionary& options); + // FIXME(zcbenz): We should not do V8 in this file, this method should only + // accept C++ struct as parameter, and atom_api_crash_reporter.cc is + // responsible for parsing the parameter from JavaScript. + static void StartInstance(const gin_helper::Dictionary& options); bool IsInitialized(); void Start(const std::string& product_name, diff --git a/shell/common/native_mate_converters/gfx_converter.cc b/shell/common/gin_converters/gfx_converter.cc similarity index 93% rename from shell/common/native_mate_converters/gfx_converter.cc rename to shell/common/gin_converters/gfx_converter.cc index c1cf1aef07..2925743add 100644 --- a/shell/common/native_mate_converters/gfx_converter.cc +++ b/shell/common/gin_converters/gfx_converter.cc @@ -1,18 +1,18 @@ -// Copyright (c) 2015 GitHub, Inc. +// Copyright (c) 2019 GitHub, Inc. // Use of this source code is governed by the MIT license that can be // found in the LICENSE file. -#include "shell/common/native_mate_converters/gfx_converter.h" +#include "shell/common/gin_converters/gfx_converter.h" -#include "native_mate/dictionary.h" #include "shell/common/gin_helper/dictionary.h" #include "ui/display/display.h" #include "ui/display/screen.h" #include "ui/gfx/geometry/point.h" +#include "ui/gfx/geometry/point_f.h" #include "ui/gfx/geometry/rect.h" #include "ui/gfx/geometry/size.h" -namespace mate { +namespace gin { v8::Local Converter::ToV8(v8::Isolate* isolate, const gfx::Point& val) { @@ -140,9 +140,8 @@ struct Converter { v8::Local Converter::ToV8( v8::Isolate* isolate, const display::Display& val) { - mate::Dictionary dict = mate::Dictionary::CreateEmpty(isolate); - // TODO(zcbenz): Just call SetHidden when this file is converted to gin. - gin_helper::Dictionary(isolate, dict.GetHandle()).SetHidden("simple", true); + gin_helper::Dictionary dict = gin::Dictionary::CreateEmpty(isolate); + dict.SetHidden("simple", true); dict.Set("id", val.id()); dict.Set("bounds", val.bounds()); dict.Set("workArea", val.work_area()); @@ -160,4 +159,4 @@ v8::Local Converter::ToV8( return dict.GetHandle(); } -} // namespace mate +} // namespace gin diff --git a/shell/common/gin_converters/gfx_converter.h b/shell/common/gin_converters/gfx_converter.h new file mode 100644 index 0000000000..beb425f682 --- /dev/null +++ b/shell/common/gin_converters/gfx_converter.h @@ -0,0 +1,67 @@ +// Copyright (c) 2019 GitHub, Inc. +// Use of this source code is governed by the MIT license that can be +// found in the LICENSE file. + +#ifndef SHELL_COMMON_GIN_CONVERTERS_GFX_CONVERTER_H_ +#define SHELL_COMMON_GIN_CONVERTERS_GFX_CONVERTER_H_ + +#include "gin/converter.h" + +namespace display { +class Display; +} + +namespace gfx { +class Point; +class PointF; +class Size; +class Rect; +} // namespace gfx + +namespace gin { + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, const gfx::Point& val); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + gfx::Point* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const gfx::PointF& val); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + gfx::PointF* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, const gfx::Size& val); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + gfx::Size* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, const gfx::Rect& val); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + gfx::Rect* out); +}; + +template <> +struct Converter { + static v8::Local ToV8(v8::Isolate* isolate, + const display::Display& val); + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + display::Display* out); +}; + +} // namespace gin + +#endif // SHELL_COMMON_GIN_CONVERTERS_GFX_CONVERTER_H_ diff --git a/shell/common/gin_converters/std_converter.h b/shell/common/gin_converters/std_converter.h index 3e4ff39cf3..3c9b9c8a83 100644 --- a/shell/common/gin_converters/std_converter.h +++ b/shell/common/gin_converters/std_converter.h @@ -5,6 +5,7 @@ #ifndef SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ #define SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ +#include #include #include @@ -115,6 +116,49 @@ struct Converter> { } }; +template +struct Converter> { + static bool FromV8(v8::Isolate* isolate, + v8::Local value, + std::map* out) { + if (!value->IsObject()) + return false; + out->clear(); + v8::Local context = isolate->GetCurrentContext(); + v8::Local obj = value.As(); + v8::Local keys = obj->GetPropertyNames(context).ToLocalChecked(); + for (uint32_t i = 0; i < keys->Length(); ++i) { + v8::MaybeLocal maybe_v8key = keys->Get(context, i); + if (maybe_v8key.IsEmpty()) + return false; + v8::Local v8key = maybe_v8key.ToLocalChecked(); + v8::MaybeLocal maybe_v8value = obj->Get(context, v8key); + if (maybe_v8value.IsEmpty()) + return false; + K key; + V value; + if (!ConvertFromV8(isolate, v8key, &key) || + !ConvertFromV8(isolate, maybe_v8value.ToLocalChecked(), &value)) + return false; + (*out)[key] = std::move(value); + } + return true; + } + + static v8::Local ToV8(v8::Isolate* isolate, + const std::map& dict) { + v8::Local obj = v8::Object::New(isolate); + v8::Local context = isolate->GetCurrentContext(); + for (const auto& it : dict) { + if (obj->Set(context, ConvertToV8(isolate, it.first), + ConvertToV8(isolate, it.second)) + .IsNothing()) + break; + } + return obj; + } +}; + } // namespace gin #endif // SHELL_COMMON_GIN_CONVERTERS_STD_CONVERTER_H_ diff --git a/shell/common/gin_helper/arguments.h b/shell/common/gin_helper/arguments.h new file mode 100644 index 0000000000..10e37d7602 --- /dev/null +++ b/shell/common/gin_helper/arguments.h @@ -0,0 +1,34 @@ +// Copyright 2019 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE.chromium file. + +#ifndef SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_ +#define SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_ + +namespace gin_helper { + +// Provides additional APIs to the gin::Arguments class. +class Arguments : public gin::Arguments { + public: + // Get the next argument, if conversion to T fails then state is unchanged. + // + // This is difference from gin::Arguments::GetNext which always advances the + // |next_| counter no matter whether the conversion succeeds. + template + bool GetNext(T* out) { + v8::Local val = PeekNext(); + if (val.IsEmpty()) + return false; + if (!gin::ConvertFromV8(isolate(), val, out)) + return false; + Skip(); + return true; + } + + private: + // MUST NOT ADD ANY DATA MEMBER. +}; + +} // namespace gin_helper + +#endif // SHELL_COMMON_GIN_HELPER_ARGUMENTS_H_ diff --git a/shell/common/gin_helper/dictionary.h b/shell/common/gin_helper/dictionary.h index 05fdb4e35c..752a884ccf 100644 --- a/shell/common/gin_helper/dictionary.h +++ b/shell/common/gin_helper/dictionary.h @@ -8,6 +8,7 @@ #include #include "gin/dictionary.h" +#include "shell/common/gin_converters/std_converter.h" #include "shell/common/gin_helper/function_template.h" namespace gin_helper { @@ -27,15 +28,16 @@ class Dictionary : public gin::Dictionary { Dictionary(const gin::Dictionary& dict) // NOLINT(runtime/explicit) : gin::Dictionary(dict) {} - // Difference from the Get method in gin::Dictionary: + // Differences from the Get method in gin::Dictionary: // 1. This is a const method; - // 2. It checks whether the key exists before reading. - template - bool Get(base::StringPiece key, T* out) const { + // 2. It checks whether the key exists before reading; + // 3. It accepts arbitrary type of key. + template + bool Get(const K& key, V* out) const { // Check for existence before getting, otherwise this method will always // returns true when T == v8::Local. v8::Local context = isolate()->GetCurrentContext(); - v8::Local v8_key = gin::StringToV8(isolate(), key); + v8::Local v8_key = gin::ConvertToV8(isolate(), key); v8::Local value; v8::Maybe result = GetHandle()->Has(context, v8_key); if (result.IsJust() && result.FromJust() && @@ -44,6 +46,21 @@ class Dictionary : public gin::Dictionary { return false; } + // 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 + bool Set(const K& key, const V& val) { + v8::Local v8_value; + if (!gin::TryConvertToV8(isolate(), val, &v8_value)) + return false; + v8::Maybe result = + GetHandle()->Set(isolate()->GetCurrentContext(), + gin::ConvertToV8(isolate(), key), v8_value); + return !result.IsNothing() && result.FromJust(); + } + template bool GetHidden(base::StringPiece key, T* out) const { v8::Local context = isolate()->GetCurrentContext(); diff --git a/shell/common/gin_helper/function_template.h b/shell/common/gin_helper/function_template.h index 67b6f5edf6..7adbc42422 100644 --- a/shell/common/gin_helper/function_template.h +++ b/shell/common/gin_helper/function_template.h @@ -8,6 +8,7 @@ #include "base/bind.h" #include "base/callback.h" #include "gin/arguments.h" +#include "shell/common/gin_helper/arguments.h" #include "shell/common/gin_helper/destroyable.h" #include "shell/common/gin_helper/error_thrower.h" @@ -118,6 +119,15 @@ inline bool GetNextArgument(gin::Arguments* args, return true; } +// Supports the gin_helper::Arguments. +inline bool GetNextArgument(gin::Arguments* args, + int create_flags, + bool is_first, + gin_helper::Arguments** result) { + *result = static_cast(args); + 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 diff --git a/shell/common/native_mate_converters/gfx_converter.h b/shell/common/native_mate_converters/gfx_converter.h index 0c4ca45fbb..8e81a9d34a 100644 --- a/shell/common/native_mate_converters/gfx_converter.h +++ b/shell/common/native_mate_converters/gfx_converter.h @@ -6,60 +6,71 @@ #define SHELL_COMMON_NATIVE_MATE_CONVERTERS_GFX_CONVERTER_H_ #include "native_mate/converter.h" -#include "ui/gfx/geometry/point_f.h" - -namespace display { -class Display; -} - -namespace gfx { -class Point; -class Size; -class Rect; -} // namespace gfx +#include "shell/common/gin_converters/gfx_converter.h" namespace mate { template <> struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, const gfx::Point& val); + static v8::Local ToV8(v8::Isolate* isolate, + const gfx::Point& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - gfx::Point* out); + gfx::Point* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const gfx::PointF& val); + const gfx::PointF& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - gfx::PointF* out); + gfx::PointF* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; template <> struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, const gfx::Size& val); + static v8::Local ToV8(v8::Isolate* isolate, const gfx::Size& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - gfx::Size* out); + gfx::Size* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; template <> struct Converter { - static v8::Local ToV8(v8::Isolate* isolate, const gfx::Rect& val); + static v8::Local ToV8(v8::Isolate* isolate, const gfx::Rect& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - gfx::Rect* out); + gfx::Rect* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; template <> struct Converter { static v8::Local ToV8(v8::Isolate* isolate, - const display::Display& val); + const display::Display& val) { + return gin::ConvertToV8(isolate, val); + } static bool FromV8(v8::Isolate* isolate, v8::Local val, - display::Display* out); + display::Display* out) { + return gin::ConvertFromV8(isolate, val, out); + } }; } // namespace mate diff --git a/shell/common/native_mate_converters/map_converter.h b/shell/common/native_mate_converters/map_converter.h deleted file mode 100644 index 5a4ac891fa..0000000000 --- a/shell/common/native_mate_converters/map_converter.h +++ /dev/null @@ -1,70 +0,0 @@ -// Copyright (c) 2019 Slack Technologies, Inc. -// Use of this source code is governed by the MIT license that can be -// found in the LICENSE file. - -#ifndef SHELL_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_ -#define SHELL_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_ - -#include -#include -#include - -#include "gin/converter.h" - -namespace gin { - -template -struct Converter> { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - std::map* out) { - if (!val->IsObject()) - return false; - - v8::Local context = isolate->GetCurrentContext(); - v8::Local dict = val->ToObject(context).ToLocalChecked(); - v8::Local keys = - dict->GetOwnPropertyNames(context).ToLocalChecked(); - for (uint32_t i = 0; i < keys->Length(); ++i) { - v8::Local key = keys->Get(context, i).ToLocalChecked(); - T value; - if (Converter::FromV8( - isolate, dict->Get(context, key).ToLocalChecked(), &value)) - (*out)[gin::V8ToString(isolate, key)] = std::move(value); - } - return true; - } - static v8::Local ToV8(v8::Isolate* isolate, - const std::map& val) { - v8::Local result = v8::Object::New(isolate); - auto context = isolate->GetCurrentContext(); - for (auto i = val.begin(); i != val.end(); i++) { - result - ->Set(context, Converter::ToV8(isolate, i->first), - Converter::ToV8(isolate, i->second)) - .Check(); - } - return result; - } -}; - -} // namespace gin - -namespace mate { - -template -struct Converter> { - static bool FromV8(v8::Isolate* isolate, - v8::Local val, - std::map* out) { - return gin::Converter>::FromV8(isolate, val, out); - } - static v8::Local ToV8(v8::Isolate* isolate, - const std::map& val) { - return gin::Converter>::ToV8(isolate, val); - } -}; - -} // namespace mate - -#endif // SHELL_COMMON_NATIVE_MATE_CONVERTERS_MAP_CONVERTER_H_