Skip to content

Commit

Permalink
Uplift of #6002 (squashed) to beta
Browse files Browse the repository at this point in the history
  • Loading branch information
brave-browser-releases authored and NejcZdovc committed Jul 9, 2020
1 parent 8700e2b commit 0588d23
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ window.cr.define('brave_rewards', function () {

function rewardsParameters (properties: Rewards.RewardsParameters) {
getActions().onRewardsParameters(properties)
// Get the current AC amount after rewards parameters have been
// updated, as the default AC amount may have been changed.
getActions().getContributionAmount()
}

function promotions (properties: Rewards.PromotionResponse) {
Expand Down
4 changes: 2 additions & 2 deletions vendor/bat-native-ledger/src/bat/ledger/internal/api/api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,8 @@ void API::OnTimer(const uint32_t timer_id) {
parameters_->OnTimer(timer_id);
}

void API::FetchParameters() {
parameters_->Fetch();
void API::FetchParameters(ledger::GetRewardsParametersCallback callback) {
parameters_->Fetch(callback);
}

} // namespace braveledger_api
2 changes: 1 addition & 1 deletion vendor/bat-native-ledger/src/bat/ledger/internal/api/api.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class API {

void OnTimer(const uint32_t timer_id);

void FetchParameters();
void FetchParameters(ledger::GetRewardsParametersCallback callback);

private:
bat_ledger::LedgerImpl* ledger_; // NOT OWNED
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include <string>
#include <utility>

#include "base/json/json_reader.h"
#include "base/time/time.h"
Expand Down Expand Up @@ -96,13 +97,24 @@ void APIParameters::Initialize() {

void APIParameters::OnTimer(const uint32_t timer_id) {
if (timer_id == refresh_timer_id_) {
refresh_timer_id_ = 0;
Fetch();
return;
}
}

void APIParameters::Fetch() {
Fetch([](ledger::RewardsParametersPtr) {});
}

void APIParameters::Fetch(ledger::GetRewardsParametersCallback callback) {
bool first_request = callbacks_.empty();
callbacks_.push_back(callback);
if (!first_request) {
BLOG(1, "API parameters fetch in progress");
return;
}

refresh_timer_id_ = 0;

auto url_callback = std::bind(&APIParameters::OnFetch,
this,
_1);
Expand All @@ -115,6 +127,7 @@ void APIParameters::OnFetch(const ledger::UrlResponse& response) {
BLOG(6, ledger::UrlResponseToString(__func__, response));

if (response.status_code != net::HTTP_OK) {
RunCallbacks();
SetRefreshTimer(brave_base::random::Geometric(90));
return;
}
Expand All @@ -123,15 +136,30 @@ void APIParameters::OnFetch(const ledger::UrlResponse& response) {
auto result = ParseFetchResponse(response.body, &parameters);
if (result != ledger::Result::LEDGER_OK) {
BLOG(1, "Couldn't parse response");
RunCallbacks();
SetRefreshTimer(
brave_base::random::Geometric(10 * base::Time::kSecondsPerMinute));
return;
}

braveledger_state::SetRewardsParameters(ledger_, parameters);
RunCallbacks();
SetRefreshTimer();
}

void APIParameters::RunCallbacks() {
// Execute callbacks with the current parameters stored in state.
// If the last fetch failed, callbacks will be run with the last
// successfully fetched parameters or a default set of parameters.
auto parameters = braveledger_state::GetRewardsParameters(ledger_);
DCHECK(parameters);

auto callbacks = std::move(callbacks_);
for (auto& callback : callbacks) {
callback(parameters->Clone());
}
}

void APIParameters::SetRefreshTimer(const int delay) {
if (refresh_timer_id_ != 0) {
BLOG(1, "Params timer in progress");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#ifndef BRAVELEDGER_API_API_PARAMETERS_H_
#define BRAVELEDGER_API_API_PARAMETERS_H_

#include <vector>

#include "bat/ledger/ledger.h"

namespace bat_ledger {
Expand All @@ -25,13 +27,18 @@ class APIParameters {

void Fetch();

void Fetch(ledger::GetRewardsParametersCallback callback);

private:
void OnFetch(const ledger::UrlResponse& response);

void RunCallbacks();

void SetRefreshTimer(const int delay = 0);

bat_ledger::LedgerImpl* ledger_; // NOT OWNED
uint32_t refresh_timer_id_;
std::vector<ledger::GetRewardsParametersCallback> callbacks_;
};

} // namespace braveledger_api
Expand Down
14 changes: 9 additions & 5 deletions vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -694,7 +694,15 @@ void LedgerImpl::ContributionCompleted(

void LedgerImpl::GetRewardsParameters(
ledger::GetRewardsParametersCallback callback) {
callback(braveledger_state::GetRewardsParameters(this));
auto params = braveledger_state::GetRewardsParameters(this);
if (params->rate == 0.0) {
// A rate of zero indicates that the rewards parameters have
// not yet been successfully initialized from the server.
BLOG(1, "Rewards parameters not set - fetching from server");
bat_api_->FetchParameters(callback);
} else {
callback(std::move(params));
}
}

void LedgerImpl::ClaimPromotion(
Expand Down Expand Up @@ -1651,10 +1659,6 @@ void LedgerImpl::SaveProcessedPublisherList(
bat_database_->SaveProcessedPublisherList(list, callback);
}

void LedgerImpl::FetchParameters() {
bat_api_->FetchParameters();
}

void LedgerImpl::GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -706,8 +706,6 @@ class LedgerImpl : public ledger::Ledger {
const std::vector<std::string>& list,
ledger::ResultCallback callback);

void FetchParameters();

void GetCredsBatchesByTriggers(
const std::vector<std::string>& trigger_ids,
ledger::GetCredsBatchListCallback callback);
Expand Down

0 comments on commit 0588d23

Please sign in to comment.