Browse Source

refactor: ginify autoUpdater (#24678)

Jeremy Rose 4 years ago
parent
commit
38fafe4986

+ 1 - 6
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;

+ 49 - 39
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<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() {
@@ -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> 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
@@ -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

+ 11 - 6
shell/browser/api/electron_api_auto_updater.h

@@ -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.
@@ -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);

+ 1 - 1
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() {}
 

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

+ 7 - 4
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<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 "
@@ -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");