Skip to content

Commit

Permalink
Adds promotion guards for UI triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 10, 2020
1 parent c411ce4 commit 58b4231
Show file tree
Hide file tree
Showing 20 changed files with 115 additions and 24 deletions.
8 changes: 5 additions & 3 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -480,11 +480,13 @@ ExtensionFunction::ResponseAction BraveRewardsClaimPromotionFunction::Run() {
data->SetIntKey("result", 1);
return RespondNow(OneArgument(std::move(data)));
}

rewards_service->ClaimPromotion(
params->promotion_id,
base::BindOnce(
&BraveRewardsClaimPromotionFunction::OnClaimPromotion,
this,
params->promotion_id));
&BraveRewardsClaimPromotionFunction::OnClaimPromotion,
this,
params->promotion_id));
return RespondLater();
}

Expand Down
1 change: 1 addition & 0 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -630,6 +630,7 @@ void RewardsDOMHandler::ClaimPromotion(const base::ListValue* args) {

#if !defined(OS_ANDROID)
rewards_service_->ClaimPromotion(
promotion_id,
base::Bind(&RewardsDOMHandler::OnClaimPromotion,
weak_factory_.GetWeakPtr(),
promotion_id));
Expand Down
3 changes: 2 additions & 1 deletion components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ class MockRewardsService : public RewardsService {
MOCK_METHOD1(GetExcludedList,
void(const brave_rewards::GetContentSiteListCallback&));
MOCK_METHOD0(FetchPromotions, void());
MOCK_METHOD1(ClaimPromotion, void(brave_rewards::ClaimPromotionCallback));
MOCK_METHOD1(ClaimPromotion, void(const std::string&,
brave_rewards::ClaimPromotionCallback));
MOCK_METHOD2(ClaimPromotion, void(const std::string&,
brave_rewards::AttestPromotionCallback));
MOCK_METHOD3(AttestPromotion, void(const std::string&,
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,9 @@ class RewardsService : public KeyedService {
virtual void GetExcludedList(const GetContentSiteListCallback& callback) = 0;
virtual void FetchPromotions() = 0;
// Used by desktop
virtual void ClaimPromotion(ClaimPromotionCallback callback) = 0;
virtual void ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) = 0;
// Used by Android
virtual void ClaimPromotion(
const std::string& promotion_id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,4 @@ void IsMediaTipsInjected(content::WebContents* context, bool should_appear) {
WaitForElementToAppear(context, ".action-brave-tip", should_appear);
}



} // namespace rewards_service_browsertest_utils
5 changes: 3 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,7 @@ void RewardsServiceImpl::OnAttestationAndroid(
}

void RewardsServiceImpl::ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) {
if (!Connected()) {
return;
Expand All @@ -1353,7 +1354,7 @@ void RewardsServiceImpl::ClaimPromotion(
AsWeakPtr(),
std::move(callback));

bat_ledger_->ClaimPromotion("", std::move(claim_callback));
bat_ledger_->ClaimPromotion(promotion_id, "", std::move(claim_callback));
}

void RewardsServiceImpl::ClaimPromotion(
Expand All @@ -1368,7 +1369,7 @@ void RewardsServiceImpl::ClaimPromotion(
promotion_id,
std::move(callback));

bat_ledger_->ClaimPromotion("", std::move(claim_callback));
bat_ledger_->ClaimPromotion(promotion_id, "", std::move(claim_callback));
}

void RewardsServiceImpl::GetWalletPassphrase(
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,9 @@ class RewardsServiceImpl : public RewardsService,
void CreateWallet(CreateWalletCallback callback) override;
void FetchWalletProperties() override;
void FetchPromotions() override;
void ClaimPromotion(ClaimPromotionCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
ClaimPromotionCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
AttestPromotionCallback callback) override;
Expand Down
2 changes: 2 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -238,12 +238,14 @@ void BatLedgerImpl::OnClaimPromotion(
delete holder;
}
void BatLedgerImpl::ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) {
// deleted in OnClaimPromotion
auto* holder = new CallbackHolder<ClaimPromotionCallback>(
AsWeakPtr(), std::move(callback));
ledger_->ClaimPromotion(
promotion_id,
payload,
std::bind(BatLedgerImpl::OnClaimPromotion, holder, _1, _2));
}
Expand Down
1 change: 1 addition & 0 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class BatLedgerImpl : public mojom::BatLedger,

void FetchPromotions(FetchPromotionsCallback callback) override;
void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) override;
void AttestPromotion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ interface BatLedger {
SetPublisherExclude(string publisher_key, ledger.mojom.PublisherExclude exclude) => (ledger.mojom.Result result);
RestorePublishers() => (ledger.mojom.Result result);
FetchPromotions() => (ledger.mojom.Result result, array<ledger.mojom.Promotion> promotion);
ClaimPromotion(string payload) => (ledger.mojom.Result result, string response);
ClaimPromotion(string promotion_id, string payload) => (ledger.mojom.Result result, string response);
AttestPromotion(string promotion_id, string solution) => (ledger.mojom.Result result, ledger.mojom.Promotion? promotion);
GetWalletPassphrase() => (string wallet_passphrase);
RecoverWallet(string passPhrase) => (ledger.mojom.Result result, double balance);
Expand Down
1 change: 1 addition & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class LEDGER_EXPORT Ledger {
// desktop: { "captchaImage": "{{captchaImage}}", "hint": "{{hint}}" }
// iOS and Android: { "nonce": "{{nonce}}" }
virtual void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ClaimPromotionCallback callback) const = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,8 @@ enum Result {
RETRY = 29,
RETRY_SHORT = 30,
RETRY_LONG = 31,
CONTINUE = 32
CONTINUE = 32,
IN_PROGRESS= 33
};

enum PublisherStatus {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -819,9 +819,10 @@ void LedgerImpl::FetchWalletProperties(
}

void LedgerImpl::ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback) const {
bat_promotion_->Claim(payload, std::move(callback));
bat_promotion_->Claim(promotion_id, payload, std::move(callback));
}

void LedgerImpl::AttestPromotion(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ class LedgerImpl : public ledger::Ledger {
void FetchPromotions(ledger::FetchPromotionCallback callback) const override;

void ClaimPromotion(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback) const override;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,10 @@ class MockLedgerImpl : public LedgerImpl {
MOCK_CONST_METHOD1(FetchPromotions,
void(ledger::FetchPromotionCallback));

MOCK_CONST_METHOD2(ClaimPromotion,
void(const std::string&, const ledger::ClaimPromotionCallback));
MOCK_CONST_METHOD3(ClaimPromotion, void(
const std::string&,
const std::string&,
const ledger::ClaimPromotionCallback));

MOCK_CONST_METHOD3(AttestPromotion,
void(const std::string&,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,13 @@ void Promotion::OnGetAllPromotions(
HandleExpiredPromotions(ledger_, &promotions);

ledger::PromotionList list;
ledger::Result success = ParseFetchResponse(response, &list);
std::vector<std::string> corrupted_promotions;
ledger::Result result = ParseFetchResponse(
response,
&list,
&corrupted_promotions);

if (success == ledger::Result::LEDGER_ERROR) {
if (result == ledger::Result::LEDGER_ERROR) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) << "Failed to parse promotions";
ProcessFetchedPromotions(
ledger::Result::LEDGER_ERROR,
Expand All @@ -195,9 +199,10 @@ void Promotion::OnGetAllPromotions(

// even though that some promotions are corrupted
// we should display non corrupted ones either way
if (success == ledger::Result::CORRUPTED_DATA) {
if (result == ledger::Result::CORRUPTED_DATA) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
"Some promotions are not formatted correctly";
"Promotions are not correct: " <<
base::JoinString(corrupted_promotions, ", ");
}

for (auto & item : list) {
Expand Down Expand Up @@ -258,24 +263,71 @@ void Promotion::LegacyClaimedSaved(
}

void Promotion::Claim(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback) {
auto promotion_callback = std::bind(&Promotion::OnClaimPromotion,
this,
_1,
payload,
callback);

ledger_->GetPromotion(promotion_id, promotion_callback);
}

void Promotion::OnClaimPromotion(
ledger::PromotionPtr promotion,
const std::string& payload,
ledger::ClaimPromotionCallback callback) {
if (!promotion) {
callback(ledger::Result::LEDGER_ERROR, "");
return;
}

if (promotion->status != ledger::PromotionStatus::ACTIVE) {
callback(ledger::Result::IN_PROGRESS, "");
return;
}

attestation_->Start(payload, callback);
}

void Promotion::Attest(
const std::string& promotion_id,
const std::string& solution,
ledger::AttestPromotionCallback callback) {
auto confirm_callback = std::bind(&Promotion::OnAttestPromotion,
auto promotion_callback = std::bind(&Promotion::OnAttestPromotion,
this,
_1,
promotion_id,
solution,
callback);
attestation_->Confirm(solution, confirm_callback);

ledger_->GetPromotion(promotion_id, promotion_callback);
}

void Promotion::OnAttestPromotion(
ledger::PromotionPtr promotion,
const std::string& solution,
ledger::AttestPromotionCallback callback) {
if (!promotion) {
callback(ledger::Result::LEDGER_ERROR, nullptr);
return;
}

if (promotion->status != ledger::PromotionStatus::ACTIVE) {
callback(ledger::Result::IN_PROGRESS, nullptr);
return;
}

auto confirm_callback = std::bind(&Promotion::OnAttestedPromotion,
this,
_1,
promotion->id,
callback);
attestation_->Confirm(solution, confirm_callback);
}

void Promotion::OnAttestedPromotion(
const ledger::Result result,
const std::string& promotion_id,
ledger::AttestPromotionCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Promotion {
void Fetch(ledger::FetchPromotionCallback callback);

void Claim(
const std::string& promotion_id,
const std::string& payload,
ledger::ClaimPromotionCallback callback);

Expand Down Expand Up @@ -66,7 +67,17 @@ class Promotion {
const ledger::Result result,
const std::string& promotion_string);

void OnClaimPromotion(
ledger::PromotionPtr promotion,
const std::string& payload,
ledger::ClaimPromotionCallback callback);

void OnAttestPromotion(
ledger::PromotionPtr promotion,
const std::string& solution,
ledger::AttestPromotionCallback callback);

void OnAttestedPromotion(
const ledger::Result result,
const std::string& promotion_id,
ledger::AttestPromotionCallback callback);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,9 @@ ledger::ReportType ConvertPromotionTypeToReportType(

ledger::Result ParseFetchResponse(
const std::string& response,
ledger::PromotionList* list) {
ledger::PromotionList* list,
std::vector<std::string>* corrupted_promotions) {
DCHECK(corrupted_promotions);
if (!list) {
return ledger::Result::LEDGER_ERROR;
}
Expand Down Expand Up @@ -116,32 +118,38 @@ ledger::Result ParseFetchResponse(

const auto version = item.FindIntKey("version");
if (!version) {
corrupted_promotions->push_back(promotion->id);
continue;
}
promotion->version = *version;

const auto* type = item.FindStringKey("type");
if (!type) {
corrupted_promotions->push_back(promotion->id);
continue;
}
promotion->type = ConvertStringToPromotionType(*type);

const auto suggestions = item.FindIntKey("suggestionsPerGrant");
if (!suggestions) {
corrupted_promotions->push_back(promotion->id);
continue;
}
promotion->suggestions = *suggestions;

const auto* approximate_value = item.FindStringKey("approximateValue");
if (!approximate_value) {
corrupted_promotions->push_back(promotion->id);
continue;
}
promotion->approximate_value = std::stod(*approximate_value);

const auto available = item.FindBoolKey("available");
if (!available) {
corrupted_promotions->push_back(promotion->id);
continue;
}

if (*available) {
promotion->status = ledger::PromotionStatus::ACTIVE;
} else {
Expand All @@ -150,6 +158,7 @@ ledger::Result ParseFetchResponse(

auto* expires_at = item.FindStringKey("expiresAt");
if (!expires_at) {
corrupted_promotions->push_back(promotion->id);
continue;
}

Expand All @@ -161,6 +170,7 @@ ledger::Result ParseFetchResponse(

auto* public_keys = item.FindListKey("publicKeys");
if (!public_keys || public_keys->GetList().empty()) {
corrupted_promotions->push_back(promotion->id);
continue;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ ledger::ReportType ConvertPromotionTypeToReportType(

ledger::Result ParseFetchResponse(
const std::string& response,
ledger::PromotionList* list);
ledger::PromotionList* list,
std::vector<std::string>* corrupted_promotions);

std::vector<ledger::PromotionType> GetEligiblePromotions();

Expand Down
1 change: 1 addition & 0 deletions vendor/brave-ios/Ledger/BATBraveLedger.mm
Original file line number Diff line number Diff line change
Expand Up @@ -949,6 +949,7 @@ - (void)claimPromotion:(NSString *)deviceCheckPublicKey completion:(void (^)(BAT
BLOG(ledger::LogLevel::LOG_ERROR) << "Missing JSON payload while attempting to claim promotion" << std::endl;
return;
}
// TODO we need to put promotion id in
const auto jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
ledger->ClaimPromotion(jsonString.UTF8String, ^(const ledger::Result result, const std::string& json) {
const auto jsonData = [[NSString stringWithUTF8String:json.c_str()] dataUsingEncoding:NSUTF8StringEncoding];
Expand Down

0 comments on commit 58b4231

Please sign in to comment.