diff --git a/lib/browser/api/auto-updater/auto-updater-native.ts b/lib/browser/api/auto-updater/auto-updater-native.ts index 82a66b96e088..6b36d2b67f29 100644 --- a/lib/browser/api/auto-updater/auto-updater-native.ts +++ b/lib/browser/api/auto-updater/auto-updater-native.ts @@ -1,8 +1,3 @@ -import { EventEmitter } from 'events'; -const { autoUpdater, AutoUpdater } = process._linkedBinding('electron_browser_auto_updater'); - -// AutoUpdater is an EventEmitter. -Object.setPrototypeOf(AutoUpdater.prototype, EventEmitter.prototype); -EventEmitter.call(autoUpdater); +const { autoUpdater } = process._linkedBinding('electron_browser_auto_updater'); export default autoUpdater; diff --git a/shell/browser/api/electron_api_auto_updater.cc b/shell/browser/api/electron_api_auto_updater.cc index 884b2dae5bcf..e49dcea4c5ea 100644 --- a/shell/browser/api/electron_api_auto_updater.cc +++ b/shell/browser/api/electron_api_auto_updater.cc @@ -6,6 +6,7 @@ #include "base/time/time.h" #include "shell/browser/browser.h" +#include "shell/browser/javascript_environment.h" #include "shell/browser/native_window.h" #include "shell/browser/window_list.h" #include "shell/common/gin_converters/callback_converter.h" @@ -19,9 +20,10 @@ namespace electron { namespace api { -AutoUpdater::AutoUpdater(v8::Isolate* isolate) { +gin::WrapperInfo AutoUpdater::kWrapperInfo = {gin::kEmbedderNativeGin}; + +AutoUpdater::AutoUpdater() { auto_updater::AutoUpdater::SetDelegate(this); - Init(isolate); } AutoUpdater::~AutoUpdater() { @@ -29,38 +31,46 @@ AutoUpdater::~AutoUpdater() { } void AutoUpdater::OnError(const std::string& message) { - v8::Locker locker(isolate()); - v8::HandleScope handle_scope(isolate()); - auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); - gin_helper::EmitEvent( - isolate(), GetWrapper(), "error", - error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(), - // Message is also emitted to keep compatibility with old code. - message); + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + v8::Local wrapper; + if (GetWrapper(isolate).ToLocal(&wrapper)) { + auto error = v8::Exception::Error(gin::StringToV8(isolate, message)); + gin_helper::EmitEvent( + isolate, wrapper, "error", + error->ToObject(isolate->GetCurrentContext()).ToLocalChecked(), + // Message is also emitted to keep compatibility with old code. + message); + } } void AutoUpdater::OnError(const std::string& message, const int code, const std::string& domain) { - v8::Locker locker(isolate()); - v8::HandleScope handle_scope(isolate()); - auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); - auto errorObject = - error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(); + v8::Isolate* isolate = JavascriptEnvironment::GetIsolate(); + v8::Locker locker(isolate); + v8::HandleScope handle_scope(isolate); + v8::Local wrapper; + if (GetWrapper(isolate).ToLocal(&wrapper)) { + auto error = v8::Exception::Error(gin::StringToV8(isolate, message)); + auto errorObject = + error->ToObject(isolate->GetCurrentContext()).ToLocalChecked(); - auto context = isolate()->GetCurrentContext(); + auto context = isolate->GetCurrentContext(); - // add two new params for better error handling - errorObject - ->Set(context, gin::StringToV8(isolate(), "code"), - v8::Integer::New(isolate(), code)) - .Check(); - errorObject - ->Set(context, gin::StringToV8(isolate(), "domain"), - gin::StringToV8(isolate(), domain)) - .Check(); + // add two new params for better error handling + errorObject + ->Set(context, gin::StringToV8(isolate, "code"), + v8::Integer::New(isolate, code)) + .Check(); + errorObject + ->Set(context, gin::StringToV8(isolate, "domain"), + gin::StringToV8(isolate, domain)) + .Check(); - gin_helper::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message); + gin_helper::EmitEvent(isolate, wrapper, "error", errorObject, message); + } } void AutoUpdater::OnCheckingForUpdate() { @@ -89,7 +99,7 @@ void AutoUpdater::OnWindowAllClosed() { QuitAndInstall(); } -void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { +void AutoUpdater::SetFeedURL(gin::Arguments* args) { auto_updater::AutoUpdater::SetFeedURL(args); } @@ -109,20 +119,23 @@ void AutoUpdater::QuitAndInstall() { // static gin::Handle AutoUpdater::Create(v8::Isolate* isolate) { - return gin::CreateHandle(isolate, new AutoUpdater(isolate)); + return gin::CreateHandle(isolate, new AutoUpdater()); } -// static -void AutoUpdater::BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(gin::StringToV8(isolate, "AutoUpdater")); - gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) +gin::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return gin_helper::EventEmitterMixin::GetObjectTemplateBuilder( + isolate) .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates) .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL) .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL) .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall); } +const char* AutoUpdater::GetTypeName() { + return "AutoUpdater"; +} + } // namespace api } // namespace electron @@ -138,9 +151,6 @@ void Initialize(v8::Local exports, v8::Isolate* isolate = context->GetIsolate(); gin_helper::Dictionary dict(isolate, exports); dict.Set("autoUpdater", AutoUpdater::Create(isolate)); - dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate) - ->GetFunction(context) - .ToLocalChecked()); } } // namespace diff --git a/shell/browser/api/electron_api_auto_updater.h b/shell/browser/api/electron_api_auto_updater.h index 7205fbf7753f..de72c84bef57 100644 --- a/shell/browser/api/electron_api_auto_updater.h +++ b/shell/browser/api/electron_api_auto_updater.h @@ -8,25 +8,30 @@ #include #include "gin/handle.h" +#include "gin/wrappable.h" #include "shell/browser/auto_updater.h" +#include "shell/browser/event_emitter_mixin.h" #include "shell/browser/window_list_observer.h" -#include "shell/common/gin_helper/event_emitter.h" namespace electron { namespace api { -class AutoUpdater : public gin_helper::EventEmitter, +class AutoUpdater : public gin::Wrappable, + public gin_helper::EventEmitterMixin, public auto_updater::Delegate, public WindowListObserver { public: static gin::Handle Create(v8::Isolate* isolate); - static void BuildPrototype(v8::Isolate* isolate, - v8::Local prototype); + // gin::Wrappable + static gin::WrapperInfo kWrapperInfo; + gin::ObjectTemplateBuilder GetObjectTemplateBuilder( + v8::Isolate* isolate) override; + const char* GetTypeName() override; protected: - explicit AutoUpdater(v8::Isolate* isolate); + AutoUpdater(); ~AutoUpdater() override; // Delegate implementations. @@ -47,7 +52,7 @@ class AutoUpdater : public gin_helper::EventEmitter, private: std::string GetFeedURL(); - void SetFeedURL(gin_helper::Arguments* args); + void SetFeedURL(gin::Arguments* args); void QuitAndInstall(); DISALLOW_COPY_AND_ASSIGN(AutoUpdater); diff --git a/shell/browser/auto_updater.cc b/shell/browser/auto_updater.cc index a9bcf5fa6679..8d6bda2fba4d 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(gin_helper::Arguments* args) {} +void AutoUpdater::SetFeedURL(gin::Arguments* args) {} void AutoUpdater::CheckForUpdates() {} diff --git a/shell/browser/auto_updater.h b/shell/browser/auto_updater.h index 1e7628764999..e84ed751ce3c 100644 --- a/shell/browser/auto_updater.h +++ b/shell/browser/auto_updater.h @@ -15,7 +15,7 @@ namespace base { class Time; } -namespace gin_helper { +namespace gin { class Arguments; } @@ -61,7 +61,7 @@ class AutoUpdater { // 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 SetFeedURL(gin::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 faffe323db8e..3dbf49e2da8a 100644 --- a/shell/browser/auto_updater_mac.mm +++ b/shell/browser/auto_updater_mac.mm @@ -41,14 +41,19 @@ std::string AutoUpdater::GetFeedURL() { } // static -void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { +void AutoUpdater::SetFeedURL(gin::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)) { + v8::Local first_arg = args->PeekNext(); + if (!first_arg.IsEmpty() && first_arg->IsString()) { + if (args->GetNext(&feed)) { + args->GetNext(&requestHeaders); + } + } else if (args->GetNext(&opts)) { if (!opts.Get("url", &feed)) { thrower.ThrowError( "Expected options object to contain a 'url' string property in " @@ -61,8 +66,6 @@ void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { thrower.ThrowError("Expected serverType to be 'default' or 'json'"); return; } - } else if (args->GetNext(&feed)) { - args->GetNext(&requestHeaders); } else { thrower.ThrowError( "Expected an options object with a 'url' property to be provided");