Skip to content

Commit

Permalink
Fixed memory leaks
Browse files Browse the repository at this point in the history
  • Loading branch information
qiuguohua committed Dec 12, 2024
1 parent 075bb9b commit 8c59afe
Show file tree
Hide file tree
Showing 19 changed files with 294 additions and 379 deletions.
236 changes: 71 additions & 165 deletions native/vendor/google/billing/GoogleBilling.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,28 +38,39 @@
#include "vendor/google/billing/build-params/QueryPurchasesParams.h"

namespace cc {
BillingClient::scopedListener::scopedListener(se::Object* obj) : _obj(obj) {
if (_obj) {
_obj->root();
_obj->incRef();
}
}

#define ADD_JS_OBJ_REF(obj) \
do { \
obj->root(); \
obj->incRef(); \
} while (0)
BillingClient::scopedListener::~scopedListener() {
if (_obj) {
_obj->unroot();
_obj->decRef();
}
}

#define DEL_JS_OBJ_REF(obj) \
do { \
if (obj) { \
obj->unroot(); \
obj->decRef(); \
} \
} while (0)
void BillingClient::scopedListener::reset(se::Object* obj) {
if (_obj) {
_obj->unroot();
_obj->decRef();
}
_obj = obj;
if (_obj) {
_obj->root();
_obj->incRef();
}
}

BillingClient::Builder& BillingClient::Builder::enableUserChoiceBilling(se::Object* listener) {
if (!listener) {
CC_LOG_WARNING("Can't set an empty listener.");
return *this;
}
ADD_JS_OBJ_REF(listener);
_userChoiceBillingListener = listener;

_userChoiceBillingListener.reset(listener);
return *this;
}

Expand All @@ -68,17 +79,17 @@ BillingClient::Builder& BillingClient::Builder::setListener(se::Object* listener
CC_LOG_WARNING("Can't set an empty listener.");
return *this;
}
ADD_JS_OBJ_REF(listener);
_purchasesUpdatedListener = listener;
_purchasesUpdatedListener.reset(listener);
return *this;
}

BillingClient::BillingClient(Builder* builder) {
this->_enableAlternativeBillingOnly = builder->_enableAlternativeBillingOnly;
this->_enableExternalOffer = builder->_enableExternalOffer;
this->_pendingPurchasesParams = builder->_pendingPurchasesParams;
this->_purchasesUpdatedListener = builder->_purchasesUpdatedListener;
this->_userChoiceBillingListener = builder->_userChoiceBillingListener;

_purchasesUpdatedListener.reset(builder->_purchasesUpdatedListener.get());
_userChoiceBillingListener.reset(builder->_userChoiceBillingListener.get());

_tag = GoogleBillingHelper::createGoogleBilling(builder);

Expand All @@ -87,45 +98,26 @@ BillingClient::BillingClient(Builder* builder) {
delete builder;
}

void BillingClient::RemoveJsObject(std::vector<se::Object*>* listeners) {
for (auto* listener : *listeners) {
DEL_JS_OBJ_REF(listener);
}
listeners->clear();
}

BillingClient::~BillingClient() {
CC_ASSERT(_tag >= 0);
GoogleBillingHelper::removeGoogleBilling(_tag);
GoogleBillingManager::getInstance()->removeGoogleBilling(_tag);
_listeners.clear();
}

int BillingClient::getNextListenerId() {
return _nextListnerId++;
}

DEL_JS_OBJ_REF(_purchasesUpdatedListener);
DEL_JS_OBJ_REF(_userChoiceBillingListener);
RemoveJsObject(&_billingClientStateListeners);
RemoveJsObject(&_productDetailsResponseListeners);
RemoveJsObject(&_consumeResponseListeners);
RemoveJsObject(&_acknowledgePurchaseResponseListeners);
RemoveJsObject(&_queryPurchasesResponseListeners);
RemoveJsObject(&_alternativeBillingOnlyReportingDetailsListeners);
RemoveJsObject(&_alternativeBillingOnlyAvailabilityListeners);
RemoveJsObject(&_externalOfferReportingDetailsListeners);
RemoveJsObject(&_externalOfferAvailabilityListeners);
RemoveJsObject(&_alternativeBillingOnlyInformationDialogListeners);
RemoveJsObject(&_externalOfferInformationDialogListeners);
RemoveJsObject(&_inappListeners);
RemoveJsObject(&_billingConfigListeners);
int BillingClient::addListener(se::Object* listener) {
int listenerId = getNextListenerId();
_listeners[listenerId].reset(listener);
return listenerId;
}

void BillingClient::startConnection(se::Object* listener) {
auto it = std::find(_billingClientStateListeners.begin(), _billingClientStateListeners.end(), listener);
if (it == _billingClientStateListeners.end()) {
listener->root();
listener->incRef();
_billingClientStateListeners.push_back(listener);
GoogleBillingHelper::startConnection(_tag, _billingClientStateListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::startConnection(_tag, listenerId);
}

void BillingClient::endConnection() {
Expand All @@ -141,164 +133,78 @@ bool BillingClient::isReady() const {
}

void BillingClient::queryProductDetailsAsync(QueryProductDetailsParams* params, se::Object* listener) {
auto it = std::find(_productDetailsResponseListeners.begin(), _productDetailsResponseListeners.end(), listener);
if (it == _productDetailsResponseListeners.end()) {
listener->root();
listener->incRef();
_productDetailsResponseListeners.push_back(listener);
std::vector<std::string> productIDs;
std::vector<std::string> productTypes;
for (auto product : params->_productList) {
productIDs.push_back(product->_productID);
productTypes.push_back(product->_productType);
}
GoogleBillingHelper::queryProductDetailsAsync(_tag, _productDetailsResponseListeners.size() - 1, productIDs, productTypes);
} else {
//
int listenerId = addListener(listener);

std::vector<std::string> productIDs;
std::vector<std::string> productTypes;
for (auto product : params->_productList) {
productIDs.push_back(product->_productID);
productTypes.push_back(product->_productType);
}
GoogleBillingHelper::queryProductDetailsAsync(_tag, listenerId, productIDs, productTypes);
}

void BillingClient::launchBillingFlow(BillingFlowParams* params) {
GoogleBillingHelper::launchBillingFlow(_tag, params);
}

void BillingClient::consumeAsync(ConsumeParams* params, se::Object* listener) {
auto it = std::find(_consumeResponseListeners.begin(), _consumeResponseListeners.end(), listener);
if (it == _consumeResponseListeners.end()) {
listener->root();
listener->incRef();
_consumeResponseListeners.push_back(listener);
GoogleBillingHelper::consumeAsync(_tag, _consumeResponseListeners.size() - 1, params);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::consumeAsync(_tag, listenerId, params);
}

void BillingClient::acknowledgePurchase(AcknowledgePurchaseParams* params, se::Object* listener) {
auto it = std::find(_acknowledgePurchaseResponseListeners.begin(), _acknowledgePurchaseResponseListeners.end(), listener);
if (it == _acknowledgePurchaseResponseListeners.end()) {
listener->root();
listener->incRef();
_acknowledgePurchaseResponseListeners.push_back(listener);
GoogleBillingHelper::acknowledgePurchase(_tag, _acknowledgePurchaseResponseListeners.size() - 1, params);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::acknowledgePurchase(_tag, listenerId, params);
}

void BillingClient::queryPurchasesAsync(QueryPurchasesParams* params, se::Object* listener) {
auto it = std::find(_queryPurchasesResponseListeners.begin(), _queryPurchasesResponseListeners.end(), listener);
if (it == _queryPurchasesResponseListeners.end()) {
listener->root();
listener->incRef();
_queryPurchasesResponseListeners.push_back(listener);
GoogleBillingHelper::queryPurchasesAsync(_tag, _queryPurchasesResponseListeners.size() - 1, params->_productType);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::queryPurchasesAsync(_tag, listenerId, params->_productType);
}

void BillingClient::getBillingConfigAsync(GetBillingConfigParams* params, se::Object* listener) {
auto it = std::find(_billingConfigListeners.begin(), _billingConfigListeners.end(), listener);
if (it == _billingConfigListeners.end()) {
listener->root();
listener->incRef();
_billingConfigListeners.push_back(listener);
GoogleBillingHelper::getBillingConfigAsync(_tag, _billingConfigListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::getBillingConfigAsync(_tag, listenerId);
}

BillingResult* BillingClient::isFeatureSupported(const std::string& feature) {
return GoogleBillingHelper::isFeatureSupported(_tag, feature);
}

void BillingClient::createAlternativeBillingOnlyReportingDetailsAsync(se::Object* listener) {
auto it = std::find(_alternativeBillingOnlyReportingDetailsListeners.begin(), _alternativeBillingOnlyReportingDetailsListeners.end(), listener);
if (it == _alternativeBillingOnlyReportingDetailsListeners.end()) {
listener->root();
listener->incRef();
_alternativeBillingOnlyReportingDetailsListeners.push_back(listener);
GoogleBillingHelper::createAlternativeBillingOnlyReportingDetailsAsync(_tag, _alternativeBillingOnlyReportingDetailsListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::createAlternativeBillingOnlyReportingDetailsAsync(_tag, listenerId);
}

void BillingClient::isAlternativeBillingOnlyAvailableAsync(se::Object* listener) {
auto it = std::find(_alternativeBillingOnlyAvailabilityListeners.begin(), _alternativeBillingOnlyAvailabilityListeners.end(), listener);
if (it == _alternativeBillingOnlyAvailabilityListeners.end()) {
listener->root();
listener->incRef();
_alternativeBillingOnlyAvailabilityListeners.push_back(listener);
GoogleBillingHelper::isAlternativeBillingOnlyAvailableAsync(_tag, _alternativeBillingOnlyAvailabilityListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::isAlternativeBillingOnlyAvailableAsync(_tag, listenerId);
}

void BillingClient::createExternalOfferReportingDetailsAsync(se::Object* listener) {
auto it = std::find(_externalOfferReportingDetailsListeners.begin(), _externalOfferReportingDetailsListeners.end(), listener);
if (it == _externalOfferReportingDetailsListeners.end()) {
listener->root();
listener->incRef();
_externalOfferReportingDetailsListeners.push_back(listener);
GoogleBillingHelper::createExternalOfferReportingDetailsAsync(_tag, _externalOfferReportingDetailsListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::createExternalOfferReportingDetailsAsync(_tag, listenerId);
}

void BillingClient::isExternalOfferAvailableAsync(se::Object* listener) {
auto it = std::find(_externalOfferAvailabilityListeners.begin(), _externalOfferAvailabilityListeners.end(), listener);
if (it == _externalOfferAvailabilityListeners.end()) {
listener->root();
listener->incRef();
_externalOfferAvailabilityListeners.push_back(listener);
GoogleBillingHelper::isExternalOfferAvailableAsync(_tag, _externalOfferAvailabilityListeners.size() - 1);
} else {
//
}
int listenerId = addListener(listener);
GoogleBillingHelper::isExternalOfferAvailableAsync(_tag, listenerId);
}

BillingResult* BillingClient::showAlternativeBillingOnlyInformationDialog(se::Object* listener) {
auto it = std::find(_alternativeBillingOnlyInformationDialogListeners.begin(), _alternativeBillingOnlyInformationDialogListeners.end(), listener);
if (it == _alternativeBillingOnlyInformationDialogListeners.end()) {
listener->root();
listener->incRef();
_alternativeBillingOnlyInformationDialogListeners.push_back(listener);
return GoogleBillingHelper::showAlternativeBillingOnlyInformationDialog(_tag, _alternativeBillingOnlyInformationDialogListeners.size() - 1);
} else {
//
}
return nullptr;
int listenerId = addListener(listener);
return GoogleBillingHelper::showAlternativeBillingOnlyInformationDialog(_tag, listenerId);
}

BillingResult* BillingClient::showExternalOfferInformationDialog(se::Object* listener) {
auto it = std::find(_externalOfferInformationDialogListeners.begin(), _externalOfferInformationDialogListeners.end(), listener);
if (it == _externalOfferInformationDialogListeners.end()) {
listener->root();
listener->incRef();
_externalOfferInformationDialogListeners.push_back(listener);
return GoogleBillingHelper::showExternalOfferInformationDialog(_tag, _externalOfferInformationDialogListeners.size() - 1);
} else {
//
}
return nullptr;
int listenerId = addListener(listener);
return GoogleBillingHelper::showExternalOfferInformationDialog(_tag, listenerId);
}

BillingResult* BillingClient::showInAppMessages(InAppMessageParams* params, se::Object* listener) {
auto it = std::find(_inappListeners.begin(), _inappListeners.end(), listener);
if (it == _inappListeners.end()) {
listener->root();
listener->incRef();
_inappListeners.push_back(listener);
return GoogleBillingHelper::showInAppMessages(_tag, _inappListeners.size() - 1, params->_inAppMessageCategoryIds);
} else {
//
}
return nullptr;
int listenerId = addListener(listener);
return GoogleBillingHelper::showInAppMessages(_tag, listenerId, params->_inAppMessageCategoryIds);
}

} // namespace cc
Loading

0 comments on commit 8c59afe

Please sign in to comment.