refactor: ginify autoUpdater (#24678)

This commit is contained in:
Jeremy Rose 2020-07-28 11:04:34 -07:00 committed by GitHub
parent e6cf5906f6
commit 38fafe4986
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 68 additions and 55 deletions

View file

@ -1,8 +1,3 @@
import { EventEmitter } from 'events'; const { autoUpdater } = process._linkedBinding('electron_browser_auto_updater');
const { autoUpdater, AutoUpdater } = process._linkedBinding('electron_browser_auto_updater');
// AutoUpdater is an EventEmitter.
Object.setPrototypeOf(AutoUpdater.prototype, EventEmitter.prototype);
EventEmitter.call(autoUpdater);
export default autoUpdater; export default autoUpdater;

View file

@ -6,6 +6,7 @@
#include "base/time/time.h" #include "base/time/time.h"
#include "shell/browser/browser.h" #include "shell/browser/browser.h"
#include "shell/browser/javascript_environment.h"
#include "shell/browser/native_window.h" #include "shell/browser/native_window.h"
#include "shell/browser/window_list.h" #include "shell/browser/window_list.h"
#include "shell/common/gin_converters/callback_converter.h" #include "shell/common/gin_converters/callback_converter.h"
@ -19,9 +20,10 @@ namespace electron {
namespace api { namespace api {
AutoUpdater::AutoUpdater(v8::Isolate* isolate) { gin::WrapperInfo AutoUpdater::kWrapperInfo = {gin::kEmbedderNativeGin};
AutoUpdater::AutoUpdater() {
auto_updater::AutoUpdater::SetDelegate(this); auto_updater::AutoUpdater::SetDelegate(this);
Init(isolate);
} }
AutoUpdater::~AutoUpdater() { AutoUpdater::~AutoUpdater() {
@ -29,38 +31,46 @@ AutoUpdater::~AutoUpdater() {
} }
void AutoUpdater::OnError(const std::string& message) { void AutoUpdater::OnError(const std::string& message) {
v8::Locker locker(isolate()); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate()); v8::Locker locker(isolate);
auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> wrapper;
if (GetWrapper(isolate).ToLocal(&wrapper)) {
auto error = v8::Exception::Error(gin::StringToV8(isolate, message));
gin_helper::EmitEvent( gin_helper::EmitEvent(
isolate(), GetWrapper(), "error", isolate, wrapper, "error",
error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(), error->ToObject(isolate->GetCurrentContext()).ToLocalChecked(),
// Message is also emitted to keep compatibility with old code. // Message is also emitted to keep compatibility with old code.
message); message);
}
} }
void AutoUpdater::OnError(const std::string& message, void AutoUpdater::OnError(const std::string& message,
const int code, const int code,
const std::string& domain) { const std::string& domain) {
v8::Locker locker(isolate()); v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::HandleScope handle_scope(isolate()); v8::Locker locker(isolate);
auto error = v8::Exception::Error(gin::StringToV8(isolate(), message)); v8::HandleScope handle_scope(isolate);
v8::Local<v8::Object> wrapper;
if (GetWrapper(isolate).ToLocal(&wrapper)) {
auto error = v8::Exception::Error(gin::StringToV8(isolate, message));
auto errorObject = auto errorObject =
error->ToObject(isolate()->GetCurrentContext()).ToLocalChecked(); error->ToObject(isolate->GetCurrentContext()).ToLocalChecked();
auto context = isolate()->GetCurrentContext(); auto context = isolate->GetCurrentContext();
// add two new params for better error handling // add two new params for better error handling
errorObject errorObject
->Set(context, gin::StringToV8(isolate(), "code"), ->Set(context, gin::StringToV8(isolate, "code"),
v8::Integer::New(isolate(), code)) v8::Integer::New(isolate, code))
.Check(); .Check();
errorObject errorObject
->Set(context, gin::StringToV8(isolate(), "domain"), ->Set(context, gin::StringToV8(isolate, "domain"),
gin::StringToV8(isolate(), domain)) gin::StringToV8(isolate, domain))
.Check(); .Check();
gin_helper::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message); gin_helper::EmitEvent(isolate, wrapper, "error", errorObject, message);
}
} }
void AutoUpdater::OnCheckingForUpdate() { void AutoUpdater::OnCheckingForUpdate() {
@ -89,7 +99,7 @@ void AutoUpdater::OnWindowAllClosed() {
QuitAndInstall(); QuitAndInstall();
} }
void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { void AutoUpdater::SetFeedURL(gin::Arguments* args) {
auto_updater::AutoUpdater::SetFeedURL(args); auto_updater::AutoUpdater::SetFeedURL(args);
} }
@ -109,20 +119,23 @@ void AutoUpdater::QuitAndInstall() {
// static // static
gin::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) { gin::Handle<AutoUpdater> AutoUpdater::Create(v8::Isolate* isolate) {
return gin::CreateHandle(isolate, new AutoUpdater(isolate)); return gin::CreateHandle(isolate, new AutoUpdater());
} }
// static gin::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
void AutoUpdater::BuildPrototype(v8::Isolate* isolate, v8::Isolate* isolate) {
v8::Local<v8::FunctionTemplate> prototype) { return gin_helper::EventEmitterMixin<AutoUpdater>::GetObjectTemplateBuilder(
prototype->SetClassName(gin::StringToV8(isolate, "AutoUpdater")); isolate)
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
.SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates) .SetMethod("checkForUpdates", &auto_updater::AutoUpdater::CheckForUpdates)
.SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL) .SetMethod("getFeedURL", &auto_updater::AutoUpdater::GetFeedURL)
.SetMethod("setFeedURL", &AutoUpdater::SetFeedURL) .SetMethod("setFeedURL", &AutoUpdater::SetFeedURL)
.SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall); .SetMethod("quitAndInstall", &AutoUpdater::QuitAndInstall);
} }
const char* AutoUpdater::GetTypeName() {
return "AutoUpdater";
}
} // namespace api } // namespace api
} // namespace electron } // namespace electron
@ -138,9 +151,6 @@ void Initialize(v8::Local<v8::Object> exports,
v8::Isolate* isolate = context->GetIsolate(); v8::Isolate* isolate = context->GetIsolate();
gin_helper::Dictionary dict(isolate, exports); gin_helper::Dictionary dict(isolate, exports);
dict.Set("autoUpdater", AutoUpdater::Create(isolate)); dict.Set("autoUpdater", AutoUpdater::Create(isolate));
dict.Set("AutoUpdater", AutoUpdater::GetConstructor(isolate)
->GetFunction(context)
.ToLocalChecked());
} }
} // namespace } // namespace

View file

@ -8,25 +8,30 @@
#include <string> #include <string>
#include "gin/handle.h" #include "gin/handle.h"
#include "gin/wrappable.h"
#include "shell/browser/auto_updater.h" #include "shell/browser/auto_updater.h"
#include "shell/browser/event_emitter_mixin.h"
#include "shell/browser/window_list_observer.h" #include "shell/browser/window_list_observer.h"
#include "shell/common/gin_helper/event_emitter.h"
namespace electron { namespace electron {
namespace api { namespace api {
class AutoUpdater : public gin_helper::EventEmitter<AutoUpdater>, class AutoUpdater : public gin::Wrappable<AutoUpdater>,
public gin_helper::EventEmitterMixin<AutoUpdater>,
public auto_updater::Delegate, public auto_updater::Delegate,
public WindowListObserver { public WindowListObserver {
public: public:
static gin::Handle<AutoUpdater> Create(v8::Isolate* isolate); static gin::Handle<AutoUpdater> Create(v8::Isolate* isolate);
static void BuildPrototype(v8::Isolate* isolate, // gin::Wrappable
v8::Local<v8::FunctionTemplate> prototype); static gin::WrapperInfo kWrapperInfo;
gin::ObjectTemplateBuilder GetObjectTemplateBuilder(
v8::Isolate* isolate) override;
const char* GetTypeName() override;
protected: protected:
explicit AutoUpdater(v8::Isolate* isolate); AutoUpdater();
~AutoUpdater() override; ~AutoUpdater() override;
// Delegate implementations. // Delegate implementations.
@ -47,7 +52,7 @@ class AutoUpdater : public gin_helper::EventEmitter<AutoUpdater>,
private: private:
std::string GetFeedURL(); std::string GetFeedURL();
void SetFeedURL(gin_helper::Arguments* args); void SetFeedURL(gin::Arguments* args);
void QuitAndInstall(); void QuitAndInstall();
DISALLOW_COPY_AND_ASSIGN(AutoUpdater); DISALLOW_COPY_AND_ASSIGN(AutoUpdater);

View file

@ -21,7 +21,7 @@ std::string AutoUpdater::GetFeedURL() {
return ""; return "";
} }
void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) {} void AutoUpdater::SetFeedURL(gin::Arguments* args) {}
void AutoUpdater::CheckForUpdates() {} void AutoUpdater::CheckForUpdates() {}

View file

@ -15,7 +15,7 @@ namespace base {
class Time; class Time;
} }
namespace gin_helper { namespace gin {
class Arguments; class Arguments;
} }
@ -61,7 +61,7 @@ class AutoUpdater {
// FIXME(zcbenz): We should not do V8 in this file, this method should only // 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 // accept C++ struct as parameter, and atom_api_auto_updater.cc is responsible
// for parsing the parameter from JavaScript. // for parsing the parameter from JavaScript.
static void SetFeedURL(gin_helper::Arguments* args); static void SetFeedURL(gin::Arguments* args);
static void CheckForUpdates(); static void CheckForUpdates();
static void QuitAndInstall(); static void QuitAndInstall();

View file

@ -41,14 +41,19 @@ std::string AutoUpdater::GetFeedURL() {
} }
// static // static
void AutoUpdater::SetFeedURL(gin_helper::Arguments* args) { void AutoUpdater::SetFeedURL(gin::Arguments* args) {
gin_helper::ErrorThrower thrower(args->isolate()); gin_helper::ErrorThrower thrower(args->isolate());
gin_helper::Dictionary opts; gin_helper::Dictionary opts;
std::string feed; std::string feed;
HeaderMap requestHeaders; HeaderMap requestHeaders;
std::string serverType = "default"; std::string serverType = "default";
if (args->GetNext(&opts)) { v8::Local<v8::Value> 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)) { if (!opts.Get("url", &feed)) {
thrower.ThrowError( thrower.ThrowError(
"Expected options object to contain a 'url' string property in " "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'"); thrower.ThrowError("Expected serverType to be 'default' or 'json'");
return; return;
} }
} else if (args->GetNext(&feed)) {
args->GetNext(&requestHeaders);
} else { } else {
thrower.ThrowError( thrower.ThrowError(
"Expected an options object with a 'url' property to be provided"); "Expected an options object with a 'url' property to be provided");