Turn addTransactionListener into transaction-updated event

This commit is contained in:
Cheng Zhao 2018-01-10 16:55:49 +09:00
parent ac6f895f64
commit 133bef3deb
5 changed files with 80 additions and 37 deletions

View file

@ -8,6 +8,13 @@
#include <string>
#include "base/callback.h"
#include "base/memory/weak_ptr.h"
#if defined(__OBJC__)
@class InAppTransactionObserver;
#else // __OBJC__
class InAppTransactionObserver;
#endif // __OBJC__
namespace in_app_purchase {
@ -27,14 +34,23 @@ struct Transaction {
std::string transactionState = "";
};
// --------------------------- Typedefs ---------------------------
// --------------------------- Classes ---------------------------
typedef base::RepeatingCallback<void(const Payment, const Transaction)>
InAppTransactionCallback;
class TransactionObserver {
public:
TransactionObserver();
virtual ~TransactionObserver();
// --------------------------- Functions ---------------------------
virtual void OnTransactionUpdated(const Payment& payment,
const Transaction& transaction) = 0;
void AddTransactionObserver(const InAppTransactionCallback& callback);
private:
InAppTransactionObserver* obeserver_;
base::WeakPtrFactory<TransactionObserver> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(TransactionObserver);
};
} // namespace in_app_purchase

View file

@ -15,13 +15,20 @@
// InAppTransactionObserver
// ============================================================================
namespace {
using InAppTransactionCallback =
base::RepeatingCallback<void(const in_app_purchase::Payment&,
const in_app_purchase::Transaction&)>;
} // namespace
@interface InAppTransactionObserver : NSObject<SKPaymentTransactionObserver> {
@private
in_app_purchase::InAppTransactionCallback callback_;
InAppTransactionCallback callback_;
}
- (id)initWithCallback:
(const in_app_purchase::InAppTransactionCallback&)callback;
- (id)initWithCallback:(const InAppTransactionCallback&)callback;
@end
@ -33,8 +40,7 @@
* @param callback - The callback that will be called for each transaction
* update.
*/
- (id)initWithCallback:
(const in_app_purchase::InAppTransactionCallback&)callback {
- (id)initWithCallback:(const InAppTransactionCallback&)callback {
if ((self = [super init])) {
callback_ = callback;
@ -143,7 +149,8 @@
if (transaction.transactionState < 5) {
transactionStruct.transactionState = [[@[
@"SKPaymentTransactionStatePurchasing",
@"SKPaymentTransactionStatePurchased", @"SKPaymentTransactionStateFailed",
@"SKPaymentTransactionStatePurchased",
@"SKPaymentTransactionStateFailed",
@"SKPaymentTransactionStateRestored",
@"SKPaymentTransactionStateDeferred"
] objectAtIndex:transaction.transactionState] UTF8String];
@ -176,10 +183,14 @@
namespace in_app_purchase {
void AddTransactionObserver(const InAppTransactionCallback& callback) {
// This is leaked, but we should be fine since we don't have a way to remove
// callback and the inAppPurchase module is never unloaded.
[[InAppTransactionObserver alloc] initWithCallback:callback];
TransactionObserver::TransactionObserver() : weak_ptr_factory_(this) {
obeserver_ = [[InAppTransactionObserver alloc]
initWithCallback:base::Bind(&TransactionObserver::OnTransactionUpdated,
weak_ptr_factory_.GetWeakPtr())];
}
TransactionObserver::~TransactionObserver() {
[obeserver_ release];
}
} // namespace in_app_purchase