From 071c5930b997faec3dea24d5ea2dc7ea239441c4 Mon Sep 17 00:00:00 2001 From: Jeremy Rose Date: Thu, 23 Jul 2020 14:55:41 -0700 Subject: [PATCH] refactor: ginify InAppPurchase (#24674) --- lib/browser/api/in-app-purchase.ts | 6 +---- .../api/electron_api_in_app_purchase.cc | 23 ++++++++++--------- .../api/electron_api_in_app_purchase.h | 17 +++++++++----- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/lib/browser/api/in-app-purchase.ts b/lib/browser/api/in-app-purchase.ts index 47172492a2de..e67fd44bf39c 100644 --- a/lib/browser/api/in-app-purchase.ts +++ b/lib/browser/api/in-app-purchase.ts @@ -3,11 +3,7 @@ import { EventEmitter } from 'events'; let _inAppPurchase; if (process.platform === 'darwin') { - const { inAppPurchase, InAppPurchase } = process._linkedBinding('electron_browser_in_app_purchase'); - - // inAppPurchase is an EventEmitter. - Object.setPrototypeOf(InAppPurchase.prototype, EventEmitter.prototype); - EventEmitter.call(inAppPurchase); + const { inAppPurchase } = process._linkedBinding('electron_browser_in_app_purchase'); _inAppPurchase = inAppPurchase; } else { diff --git a/shell/browser/api/electron_api_in_app_purchase.cc b/shell/browser/api/electron_api_in_app_purchase.cc index 8f392c1303ad..1749b704fdb8 100644 --- a/shell/browser/api/electron_api_in_app_purchase.cc +++ b/shell/browser/api/electron_api_in_app_purchase.cc @@ -10,6 +10,7 @@ #include "shell/common/gin_helper/dictionary.h" #include "shell/common/gin_helper/object_template_builder.h" +#include "shell/common/gin_helper/promise.h" #include "shell/common/node_includes.h" namespace gin { @@ -73,17 +74,18 @@ namespace electron { namespace api { +gin::WrapperInfo InAppPurchase::kWrapperInfo = {gin::kEmbedderNativeGin}; + #if defined(OS_MACOSX) // static gin::Handle InAppPurchase::Create(v8::Isolate* isolate) { - return gin::CreateHandle(isolate, new InAppPurchase(isolate)); + return gin::CreateHandle(isolate, new InAppPurchase()); } -// static -void InAppPurchase::BuildPrototype(v8::Isolate* isolate, - v8::Local prototype) { - prototype->SetClassName(gin::StringToV8(isolate, "InAppPurchase")); - gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate()) +gin::ObjectTemplateBuilder InAppPurchase::GetObjectTemplateBuilder( + v8::Isolate* isolate) { + return gin_helper::EventEmitterMixin::GetObjectTemplateBuilder( + isolate) .SetMethod("canMakePayments", &in_app_purchase::CanMakePayments) .SetMethod("restoreCompletedTransactions", &in_app_purchase::RestoreCompletedTransactions) @@ -96,10 +98,12 @@ void InAppPurchase::BuildPrototype(v8::Isolate* isolate, .SetMethod("getProducts", &InAppPurchase::GetProducts); } -InAppPurchase::InAppPurchase(v8::Isolate* isolate) { - Init(isolate); +const char* InAppPurchase::GetTypeName() { + return "InAppPurchase"; } +InAppPurchase::InAppPurchase() {} + InAppPurchase::~InAppPurchase() {} v8::Local InAppPurchase::PurchaseProduct( @@ -158,9 +162,6 @@ void Initialize(v8::Local exports, v8::Isolate* isolate = context->GetIsolate(); gin_helper::Dictionary dict(isolate, exports); dict.Set("inAppPurchase", InAppPurchase::Create(isolate)); - dict.Set("InAppPurchase", InAppPurchase::GetConstructor(isolate) - ->GetFunction(context) - .ToLocalChecked()); #endif } diff --git a/shell/browser/api/electron_api_in_app_purchase.h b/shell/browser/api/electron_api_in_app_purchase.h index 21b833c44153..0629b6d1517c 100644 --- a/shell/browser/api/electron_api_in_app_purchase.h +++ b/shell/browser/api/electron_api_in_app_purchase.h @@ -9,26 +9,31 @@ #include #include "gin/handle.h" +#include "gin/wrappable.h" +#include "shell/browser/event_emitter_mixin.h" #include "shell/browser/mac/in_app_purchase.h" #include "shell/browser/mac/in_app_purchase_observer.h" #include "shell/browser/mac/in_app_purchase_product.h" -#include "shell/common/gin_helper/event_emitter.h" -#include "shell/common/gin_helper/promise.h" +#include "v8/include/v8.h" namespace electron { namespace api { -class InAppPurchase : public gin_helper::EventEmitter, +class InAppPurchase : public gin::Wrappable, + public gin_helper::EventEmitterMixin, public in_app_purchase::TransactionObserver { 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 InAppPurchase(v8::Isolate* isolate); + InAppPurchase(); ~InAppPurchase() override; v8::Local PurchaseProduct(const std::string& product_id,