Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: ginify InAppPurchase #24674

Merged
merged 2 commits into from
Jul 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 1 addition & 5 deletions lib/browser/api/in-app-purchase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
23 changes: 12 additions & 11 deletions shell/browser/api/electron_api_in_app_purchase.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -73,17 +74,18 @@ namespace electron {

namespace api {

gin::WrapperInfo InAppPurchase::kWrapperInfo = {gin::kEmbedderNativeGin};

#if defined(OS_MACOSX)
// static
gin::Handle<InAppPurchase> 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<v8::FunctionTemplate> prototype) {
prototype->SetClassName(gin::StringToV8(isolate, "InAppPurchase"));
gin_helper::ObjectTemplateBuilder(isolate, prototype->PrototypeTemplate())
gin::ObjectTemplateBuilder InAppPurchase::GetObjectTemplateBuilder(
v8::Isolate* isolate) {
return gin_helper::EventEmitterMixin<InAppPurchase>::GetObjectTemplateBuilder(
isolate)
.SetMethod("canMakePayments", &in_app_purchase::CanMakePayments)
.SetMethod("restoreCompletedTransactions",
&in_app_purchase::RestoreCompletedTransactions)
Expand All @@ -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<v8::Promise> InAppPurchase::PurchaseProduct(
Expand Down Expand Up @@ -158,9 +162,6 @@ void Initialize(v8::Local<v8::Object> 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
}

Expand Down
17 changes: 11 additions & 6 deletions shell/browser/api/electron_api_in_app_purchase.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,31 @@
#include <vector>

#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<InAppPurchase>,
class InAppPurchase : public gin::Wrappable<InAppPurchase>,
public gin_helper::EventEmitterMixin<InAppPurchase>,
public in_app_purchase::TransactionObserver {
public:
static gin::Handle<InAppPurchase> 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 InAppPurchase(v8::Isolate* isolate);
InAppPurchase();
~InAppPurchase() override;

v8::Local<v8::Promise> PurchaseProduct(const std::string& product_id,
Expand Down