Skip to content

Commit

Permalink
refactor: ginify autoUpdater (#24678)
Browse files Browse the repository at this point in the history
  • Loading branch information
nornagon committed Jul 28, 2020
1 parent e6cf590 commit 38fafe4
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 58 deletions.
7 changes: 1 addition & 6 deletions lib/browser/api/auto-updater/auto-updater-native.ts
Original file line number Diff line number Diff line change
@@ -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;
88 changes: 49 additions & 39 deletions shell/browser/api/electron_api_auto_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,48 +20,57 @@ 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() {
auto_updater::AutoUpdater::SetDelegate(nullptr);
}

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<v8::Object> 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();

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();

gin_helper::EmitEvent(isolate(), GetWrapper(), "error", errorObject, message);
v8::Isolate* isolate = JavascriptEnvironment::GetIsolate();
v8::Locker locker(isolate);
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 =
error->ToObject(isolate->GetCurrentContext()).ToLocalChecked();

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();

gin_helper::EmitEvent(isolate, wrapper, "error", errorObject, message);
}
}

void AutoUpdater::OnCheckingForUpdate() {
Expand Down Expand Up @@ -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);
}

Expand All @@ -109,20 +119,23 @@ void AutoUpdater::QuitAndInstall() {

// static
gin::Handle<AutoUpdater> 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<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "AutoUpdater"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin::ObjectTemplateBuilder AutoUpdater::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<AutoUpdater>::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
Expand All @@ -138,9 +151,6 @@ void Initialize(v8::Local<v8::Object> 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
Expand Down
17 changes: 11 additions & 6 deletions shell/browser/api/electron_api_auto_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,30 @@
#include <string>

#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<AutoUpdater>,
class AutoUpdater : public gin::Wrappable<AutoUpdater>,
public gin_helper::EventEmitterMixin<AutoUpdater>,
public auto_updater::Delegate,
public WindowListObserver {
public:
static gin::Handle<AutoUpdater> Create(v8::Isolate* isolate);

static void BuildPrototype(v8::Isolate* isolate,
v8::Local<v8::FunctionTemplate> 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.
Expand All @@ -47,7 +52,7 @@ class AutoUpdater : public gin_helper::EventEmitter<AutoUpdater>,

private:
std::string GetFeedURL();
void SetFeedURL(gin_helper::Arguments* args);
void SetFeedURL(gin::Arguments* args);
void QuitAndInstall();

DISALLOW_COPY_AND_ASSIGN(AutoUpdater);
Expand Down
2 changes: 1 addition & 1 deletion shell/browser/auto_updater.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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() {}

Expand Down
4 changes: 2 additions & 2 deletions shell/browser/auto_updater.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace base {
class Time;
}

namespace gin_helper {
namespace gin {
class Arguments;
}

Expand Down Expand Up @@ -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();

Expand Down
11 changes: 7 additions & 4 deletions shell/browser/auto_updater_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,19 @@
}

// 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<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)) {
thrower.ThrowError(
"Expected options object to contain a 'url' string property in "
Expand All @@ -61,8 +66,6 @@
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");
Expand Down

0 comments on commit 38fafe4

Please sign in to comment.