Skip to content

Commit

Permalink
Removes std::function<> from the Ledger::AttestPromotion() flow.
Browse files Browse the repository at this point in the history
  • Loading branch information
szilardszaloki committed Jul 27, 2022
1 parent dcb0d2c commit 093f0b5
Show file tree
Hide file tree
Showing 44 changed files with 789 additions and 991 deletions.
18 changes: 1 addition & 17 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -159,27 +159,11 @@ void BatLedgerImpl::ClaimPromotion(
ledger_->ClaimPromotion(promotion_id, payload, std::move(callback));
}

// static
void BatLedgerImpl::OnAttestPromotion(
CallbackHolder<AttestPromotionCallback>* holder,
const ledger::type::Result result,
ledger::type::PromotionPtr promotion) {
DCHECK(holder);
if (holder->is_valid())
std::move(holder->get()).Run(result, std::move(promotion));
delete holder;
}
void BatLedgerImpl::AttestPromotion(
const std::string& promotion_id,
const std::string& solution,
AttestPromotionCallback callback) {
// deleted in OnAttestPromotion
auto* holder = new CallbackHolder<AttestPromotionCallback>(
AsWeakPtr(), std::move(callback));
ledger_->AttestPromotion(
promotion_id,
solution,
std::bind(BatLedgerImpl::OnAttestPromotion, holder, _1, _2));
ledger_->AttestPromotion(promotion_id, solution, std::move(callback));
}

// static
Expand Down
5 changes: 0 additions & 5 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,11 +285,6 @@ class BatLedgerImpl :
const ledger::type::Result result,
ledger::type::BalanceReportInfoPtr report_info);

static void OnAttestPromotion(
CallbackHolder<AttestPromotionCallback>* holder,
const ledger::type::Result result,
ledger::type::PromotionPtr promotion);

static void OnInitialize(
CallbackHolder<InitializeCallback>* holder,
ledger::type::Result result);
Expand Down
6 changes: 3 additions & 3 deletions ios/browser/api/ledger/brave_ledger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,8 @@ - (void)attestPromotion:(NSString*)promotionId
ledger->AttestPromotion(
base::SysNSStringToUTF8(promotionId),
base::SysNSStringToUTF8(solution.JSONPayload),
^(const ledger::type::Result result,
ledger::type::PromotionPtr promotion) {
base::BindOnce(^(ledger::type::Result result,
ledger::type::PromotionPtr promotion) {
if (promotion.get() == nullptr) {
if (completion) {
dispatch_async(dispatch_get_main_queue(), ^{
Expand Down Expand Up @@ -1086,7 +1086,7 @@ - (void)attestPromotion:(NSString*)promotionId
}
}
});
});
}));
}

#pragma mark - History
Expand Down
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ using RewardsInternalsInfoCallback =
std::function<void(type::RewardsInternalsInfoPtr)>;

using AttestPromotionCallback =
std::function<void(type::Result, type::PromotionPtr)>;
base::OnceCallback<void(type::Result, type::PromotionPtr)>;

using GetBalanceReportCallback =
std::function<void(type::Result, type::BalanceReportInfoPtr)>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ namespace attestation {
using StartCallback =
base::OnceCallback<void(type::Result, const std::string&)>;

using ConfirmCallback =
std::function<void(const type::Result)>;
using ConfirmCallback = base::OnceCallback<void(type::Result)>;

class Attestation {
public:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "bat/ledger/internal/attestation/attestation_androidx.h"
#include "bat/ledger/internal/ledger_impl.h"

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

namespace ledger {
namespace attestation {

Expand Down Expand Up @@ -87,24 +83,23 @@ void AttestationAndroid::Confirm(
std::string nonce;
ParseClaimSolution(solution, &token, &nonce);

auto url_callback = std::bind(&AttestationAndroid::OnConfirm,
this,
_1,
callback);
auto url_callback =
base::BindOnce(&AttestationAndroid::OnConfirm, base::Unretained(this),
std::move(callback));

promotion_server_->put_safetynet()->Request(token, nonce, url_callback);
promotion_server_->put_safetynet()->Request(token, nonce,
std::move(url_callback));
}

void AttestationAndroid::OnConfirm(
const type::Result result,
ConfirmCallback callback) {
void AttestationAndroid::OnConfirm(ConfirmCallback callback,
type::Result result) {
if (result != type::Result::LEDGER_OK) {
BLOG(0, "Failed to confirm attestation");
callback(result);
std::move(callback).Run(result);
return;
}

callback(type::Result::LEDGER_OK);
std::move(callback).Run(type::Result::LEDGER_OK);
}

} // namespace attestation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,7 @@ class AttestationAndroid : public Attestation {
type::Result result,
const std::string& confirmation);

void OnConfirm(
const type::Result result,
ConfirmCallback callback);
void OnConfirm(ConfirmCallback callback, type::Result result);

std::unique_ptr<endpoint::PromotionServer> promotion_server_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,6 @@
#include "bat/ledger/internal/attestation/attestation_desktop.h"
#include "bat/ledger/internal/ledger_impl.h"

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

namespace ledger {
namespace attestation {

Expand Down Expand Up @@ -123,32 +119,27 @@ void AttestationDesktop::Confirm(

if (result != type::Result::LEDGER_OK) {
BLOG(0, "Failed to parse solution");
callback(result);
std::move(callback).Run(result);
return;
}

auto url_callback = std::bind(&AttestationDesktop::OnConfirm,
this,
_1,
callback);
auto url_callback =
base::BindOnce(&AttestationDesktop::OnConfirm, base::Unretained(this),
std::move(callback));

promotion_server_->put_captcha()->Request(
x,
y,
captcha_id,
url_callback);
promotion_server_->put_captcha()->Request(x, y, captcha_id,
std::move(url_callback));
}

void AttestationDesktop::OnConfirm(
const type::Result result,
ConfirmCallback callback) {
void AttestationDesktop::OnConfirm(ConfirmCallback callback,
type::Result result) {
if (result != type::Result::LEDGER_OK) {
BLOG(0, "Failed to confirm attestation");
callback(result);
std::move(callback).Run(result);
return;
}

callback(type::Result::LEDGER_OK);
std::move(callback).Run(type::Result::LEDGER_OK);
}

} // namespace attestation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,7 @@ class AttestationDesktop : public Attestation {
type::Result result,
const std::string& image);

void OnConfirm(
const type::Result result,
ConfirmCallback callback);
void OnConfirm(ConfirmCallback callback, type::Result result);

std::unique_ptr<endpoint::PromotionServer> promotion_server_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ void AttestationImpl::Start(
void AttestationImpl::Confirm(
const std::string& solution,
ConfirmCallback callback) {
platform_instance_->Confirm(solution, callback);
platform_instance_->Confirm(solution, std::move(callback));
}

} // namespace attestation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,6 @@
#include "bat/ledger/internal/attestation/attestation_iosx.h"
#include "bat/ledger/internal/ledger_impl.h"

using std::placeholders::_1;
using std::placeholders::_2;
using std::placeholders::_3;

namespace ledger {
namespace attestation {

Expand Down Expand Up @@ -124,32 +120,25 @@ void AttestationIOS::Confirm(

if (result != type::Result::LEDGER_OK) {
BLOG(0, "Failed to parse solution");
callback(type::Result::LEDGER_ERROR);
std::move(callback).Run(type::Result::LEDGER_ERROR);
return;
}

auto url_callback = std::bind(&AttestationIOS::OnConfirm,
this,
_1,
callback);
auto url_callback = base::BindOnce(
&AttestationIOS::OnConfirm, base::Unretained(this), std::move(callback));

promotion_server_->put_devicecheck()->Request(
blob,
signature,
nonce,
url_callback);
promotion_server_->put_devicecheck()->Request(blob, signature, nonce,
std::move(url_callback));
}

void AttestationIOS::OnConfirm(
const type::Result result,
ConfirmCallback callback) {
void AttestationIOS::OnConfirm(ConfirmCallback callback, type::Result result) {
if (result != type::Result::LEDGER_OK) {
BLOG(0, "Failed to confirm attestation");
callback(type::Result::LEDGER_ERROR);
std::move(callback).Run(type::Result::LEDGER_ERROR);
return;
}

callback(type::Result::LEDGER_OK);
std::move(callback).Run(type::Result::LEDGER_OK);
}

} // namespace attestation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,7 @@ class AttestationIOS : public Attestation {
type::Result result,
const std::string& nonce);

void OnConfirm(
const type::Result result,
ConfirmCallback callback);
void OnConfirm(ConfirmCallback callback, type::Result result);

std::unique_ptr<endpoint::PromotionServer> promotion_server_;
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,10 @@ void ContributionSKU::OnGetOrder(type::SKUOrderPtr order,
credential::CredentialsTrigger trigger;
GetCredentialTrigger(order->Clone(), &trigger);

credentials_->Start(trigger, callback);
credentials_->Start(
trigger, base::BindOnce([](ledger::LegacyResultCallback callback,
type::Result result) { callback(result); },
std::move(callback)));
}

void ContributionSKU::Completed(type::Result result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ class Credentials {
virtual ~Credentials() = default;

virtual void Start(const CredentialsTrigger& trigger,
ledger::LegacyResultCallback callback) = 0;
ledger::ResultCallback callback) = 0;

virtual void RedeemTokens(const CredentialsRedeem& redeem,
ledger::LegacyResultCallback callback) = 0;

protected:
virtual void Blind(const CredentialsTrigger& trigger,
ledger::LegacyResultCallback callback) = 0;
virtual void Blind(ledger::ResultCallback callback,
const CredentialsTrigger& trigger) = 0;

virtual void Claim(type::CredsBatchPtr creds,
virtual void Claim(ledger::ResultCallback callback,
const CredentialsTrigger& trigger,
ledger::LegacyResultCallback callback) = 0;
type::CredsBatchPtr creds) = 0;

virtual void Unblind(type::CredsBatchPtr creds,
virtual void Unblind(ledger::ResultCallback callback,
const CredentialsTrigger& trigger,
ledger::LegacyResultCallback callback) = 0;
type::CredsBatchPtr creds) = 0;

virtual void Completed(type::Result result,
virtual void Completed(ledger::ResultCallback callback,
const CredentialsTrigger& trigger,
ledger::LegacyResultCallback callback) = 0;
type::Result result) = 0;
};

} // namespace credential
Expand Down
Loading

0 comments on commit 093f0b5

Please sign in to comment.