Skip to content

Commit

Permalink
rewire SaveRecurringTip through ledger
Browse files Browse the repository at this point in the history
  • Loading branch information
cg505 committed Jul 19, 2019
1 parent 3cf90d8 commit 1a75192
Show file tree
Hide file tree
Showing 20 changed files with 216 additions and 31 deletions.
20 changes: 16 additions & 4 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -477,12 +477,24 @@ BraveRewardsSaveRecurringTipFunction::Run() {
RewardsService* rewards_service_ =
RewardsServiceFactory::GetForProfile(profile);

if (rewards_service_) {
rewards_service_->SaveRecurringTip(params->publisher_key,
params->new_amount);
if (!rewards_service_) {
return RespondNow(NoArguments());
}

return RespondNow(NoArguments());
rewards_service_->SaveRecurringTipUI(
params->publisher_key,
params->new_amount,
base::Bind(&BraveRewardsSaveRecurringTipFunction::OnSaveRecurringTip, this));

return RespondLater();
}

void BraveRewardsSaveRecurringTipFunction::OnSaveRecurringTip(bool success) {
if (!success) {
Respond(Error("Failed to save"));
return;
}
Respond(NoArguments());
}

BraveRewardsRemoveRecurringTipFunction::
Expand Down
3 changes: 3 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,9 @@ class BraveRewardsSaveRecurringTipFunction :
~BraveRewardsSaveRecurringTipFunction() override;

ResponseAction Run() override;

private:
void OnSaveRecurringTip(bool success);
};

class BraveRewardsRemoveRecurringTipFunction :
Expand Down
6 changes: 4 additions & 2 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ using GetTransactionHistoryCallback =
base::OnceCallback<void(double, uint64_t, uint64_t)>;
using GetRewardsInternalsInfoCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::RewardsInternalsInfo>)>;
using SaveRecurringTipCallback = base::OnceCallback<void(bool)>;
using GetRecurringTipsCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::ContentSiteList>)>;
using GetOneTimeTipsCallback = base::OnceCallback<void(
Expand Down Expand Up @@ -217,8 +218,9 @@ class RewardsService : public KeyedService {

static void RegisterProfilePrefs(PrefRegistrySimple* registry);

virtual void SaveRecurringTip(const std::string& publisher_key,
const int amount) = 0;
virtual void SaveRecurringTipUI(const std::string& publisher_key,
const int amount,
SaveRecurringTipCallback callback) = 0;

virtual const RewardsNotificationService::RewardsNotificationsMap&
GetAllNotifications() = 0;
Expand Down
57 changes: 48 additions & 9 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2171,7 +2171,7 @@ void RewardsServiceImpl::OnTip(const std::string& publisher_key,
this, _1, _2));
}

SaveRecurringTip(publisher_key, amount);
SaveRecurringTipUI(publisher_key, amount, base::DoNothing());
return;
}

Expand Down Expand Up @@ -2230,25 +2230,64 @@ bool SaveRecurringTipOnFileTaskRunner(
return false;
}

void RewardsServiceImpl::OnRecurringTipSaved(bool success) {
void RewardsServiceImpl::OnSaveRecurringTipUI(
SaveRecurringTipCallback callback,
int32_t result) {
ledger::Result result_converted = static_cast<ledger::Result>(result);
std::move(callback).Run(result_converted == ledger::Result::LEDGER_OK);
}

void RewardsServiceImpl::SaveRecurringTipUI(
const std::string& publisher_key,
const int amount,
SaveRecurringTipCallback callback) {
ledger::ContributionInfoPtr info = ledger::ContributionInfo::New();
info->publisher = publisher_key;
info->value = amount;
info->date = GetCurrentTimestamp();

bat_ledger_->SaveRecurringTip(
std::move(info),
base::BindOnce(&RewardsServiceImpl::OnSaveRecurringTipUI,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnRecurringTipSaved(
ledger::SaveRecurringTipCallback callback,
bool success) {
for (auto& observer : observers_) {
observer.OnRecurringTipSaved(this, success);
}

if (!Connected()) {
return;
}

callback(success ? ledger::Result::LEDGER_OK
: ledger::Result::LEDGER_ERROR);
}

void RewardsServiceImpl::SaveRecurringTip(
const std::string& publisher_key, const int amount) {
brave_rewards::RecurringDonation info;
info.publisher_key = publisher_key;
info.amount = amount;
info.added_date = GetCurrentTimestamp();
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) {
if (!info) {
callback(ledger::Result::NOT_FOUND);
return;
}

brave_rewards::RecurringDonation new_info;
new_info.publisher_key = info->publisher;
new_info.amount = info->value;
new_info.added_date = info->date;

base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
base::Bind(&SaveRecurringTipOnFileTaskRunner,
info,
new_info,
publisher_info_backend_.get()),
base::Bind(&RewardsServiceImpl::OnRecurringTipSaved,
AsWeakPtr()));
AsWeakPtr(),
callback));
}

void RewardsServiceImpl::OnTwitterPublisherInfoSaved(
Expand Down
16 changes: 13 additions & 3 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -208,8 +208,13 @@ class RewardsServiceImpl : public RewardsService,
const std::string& publisher_key,
RefreshPublisherCallback callback) override;

void SaveRecurringTip(const std::string& publisher_key,
const int amount) override;
void OnSaveRecurringTipUI(
SaveRecurringTipCallback callback,
int32_t result);
void SaveRecurringTipUI(
const std::string& publisher_key,
const int amount,
SaveRecurringTipCallback callback) override;

const RewardsNotificationService::RewardsNotificationsMap&
GetAllNotifications() override;
Expand Down Expand Up @@ -319,7 +324,9 @@ class RewardsServiceImpl : public RewardsService,
ledger::PublisherInfoPtr publisher_info);
void OnContributionInfoSaved(const ledger::REWARDS_CATEGORY category,
bool success);
void OnRecurringTipSaved(bool success);
void OnRecurringTipSaved(
ledger::SaveRecurringTipCallback callback,
bool success);
void OnGetRecurringTips(
const ledger::PublisherInfoListCallback callback,
ledger::PublisherInfoList list);
Expand Down Expand Up @@ -484,6 +491,9 @@ class RewardsServiceImpl : public RewardsService,
const uint32_t date,
const std::string& publisher_key,
const ledger::REWARDS_CATEGORY category) override;
void SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) override;
void GetRecurringTips(
ledger::PublisherInfoListCallback callback) override;
std::unique_ptr<ledger::LogStream> Log(
Expand Down
18 changes: 18 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc
Original file line number Diff line number Diff line change
Expand Up @@ -454,6 +454,24 @@ void BatLedgerClientMojoProxy::FetchFavIcon(const std::string& url,
base::BindOnce(&OnFetchFavIcon, std::move(callback)));
}

void OnSaveRecurringTip(const ledger::SaveRecurringTipCallback& callback,
int32_t result) {
callback(ToLedgerResult(result));
}

void BatLedgerClientMojoProxy::SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) {
if (!Connected()) {
callback(ledger::Result::LEDGER_ERROR);
return;
}

bat_ledger_client_->SaveRecurringTip(
std::move(info),
base::BindOnce(&OnSaveRecurringTip, std::move(callback)));
}

void OnGetRecurringTips(const ledger::PublisherInfoListCallback& callback,
ledger::PublisherInfoList publisher_info_list,
uint32_t next_record) {
Expand Down
3 changes: 3 additions & 0 deletions components/services/bat_ledger/bat_ledger_client_mojo_proxy.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,9 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient,
const uint32_t date,
const std::string& publisher_key,
const ledger::REWARDS_CATEGORY category) override;
void SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) override;
void GetRecurringTips(ledger::PublisherInfoListCallback callback) override;
void GetOneTimeTips(ledger::PublisherInfoListCallback callback) override;
std::unique_ptr<ledger::LogStream> Log(const char* file,
Expand Down
21 changes: 21 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,27 @@ void BatLedgerImpl::GetRewardsInternalsInfo(
std::move(callback).Run(info.ToJson());
}

// static
void BatLedgerImpl::OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
ledger::Result result) {
if (holder->is_valid())
std::move(holder->get()).Run(result);

delete holder;
}

void BatLedgerImpl::SaveRecurringTip(
ledger::ContributionInfoPtr info,
SaveRecurringTipCallback callback) {
// deleted in OnSaveRecurringTip
auto* holder = new CallbackHolder<SaveRecurringTipCallback>(
AsWeakPtr(), std::move(callback));

ledger_->SaveRecurringTip(std::move(info), std::bind(
BatLedgerImpl::OnSaveRecurringTip, holder, _1));
}

// static
void BatLedgerImpl::OnGetRecurringTips(
CallbackHolder<GetRecurringTipsCallback>* holder,
Expand Down
7 changes: 7 additions & 0 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,9 @@ class BatLedgerImpl : public mojom::BatLedger,
RefreshPublisherCallback callback) override;
void StartMonthlyContribution() override;

void SaveRecurringTip(
ledger::ContributionInfoPtr info,
SaveRecurringTipCallback callback) override;
void GetRecurringTips(GetRecurringTipsCallback callback) override;

void GetOneTimeTips(GetOneTimeTipsCallback callback) override;
Expand Down Expand Up @@ -224,6 +227,10 @@ class BatLedgerImpl : public mojom::BatLedger,
CallbackHolder<GetTransactionHistoryCallback>* holder,
std::unique_ptr<ledger::TransactionsInfo> history);

static void OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
ledger::Result result);

static void OnGetRecurringTips(
CallbackHolder<GetRecurringTipsCallback>* holder,
ledger::PublisherInfoList list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,6 +347,28 @@ void LedgerClientMojoProxy::FetchFavIcon(const std::string& url,
std::bind(LedgerClientMojoProxy::OnFetchFavIcon, holder, _1, _2));
}

// static
void LedgerClientMojoProxy::OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
ledger::Result result) {
if (holder->is_valid())
std::move(holder->get()).Run(ToMojomResult(result));
delete holder;
}

void LedgerClientMojoProxy::SaveRecurringTip(
ledger::ContributionInfoPtr info,
SaveRecurringTipCallback callback) {
// deleted in OnSaveRecurringTip
auto* holder = new CallbackHolder<SaveRecurringTipCallback>(
AsWeakPtr(), std::move(callback));
ledger_client_->SaveRecurringTip(
std::move(info),
std::bind(LedgerClientMojoProxy::OnSaveRecurringTip,
holder,
_1));
}

// static
void LedgerClientMojoProxy::OnGetRecurringTips(
CallbackHolder<GetRecurringTipsCallback>* holder,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,

void FetchFavIcon(const std::string& url, const std::string& favicon_key,
FetchFavIconCallback callback) override;
void SaveRecurringTip(
ledger::ContributionInfoPtr info,
SaveRecurringTipCallback callback) override;
void GetRecurringTips(GetRecurringTipsCallback callback) override;

void GetOneTimeTips(GetOneTimeTipsCallback callback) override;
Expand Down Expand Up @@ -221,6 +224,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
bool success,
const std::string& favicon_url);

static void OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
ledger::Result result);

static void OnGetRecurringTips(
CallbackHolder<GetRecurringTipsCallback>* holder,
ledger::PublisherInfoList publisher_info_list,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,7 @@ interface BatLedger {
GetTransactionHistory() => (string transactions);
GetRewardsInternalsInfo() => (string info);

SaveRecurringTip(ledger.mojom.ContributionInfo info) => (int32 result);
GetRecurringTips() => (array<ledger.mojom.PublisherInfo> list);
GetOneTimeTips() => (array<ledger.mojom.PublisherInfo> list);

Expand Down Expand Up @@ -184,6 +185,7 @@ interface BatLedgerClient {
uint64 window_id);
FetchFavIcon(string url, string favicon_key) => (bool success,
string favicon_url);
SaveRecurringTip(ledger.mojom.ContributionInfo info) => (int32 result);
GetRecurringTips() => (
array<ledger.mojom.PublisherInfo> publisher_info_list,
uint32 next_record);
Expand Down
3 changes: 3 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,9 @@ class LEDGER_EXPORT Ledger {
GetTransactionHistoryCallback callback) = 0;
virtual void GetRewardsInternalsInfo(ledger::RewardsInternalsInfo* info) = 0;

virtual void SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) = 0;
virtual void GetRecurringTips(ledger::PublisherInfoListCallback callback) = 0;

virtual void GetOneTimeTips(ledger::PublisherInfoListCallback callback) = 0;
Expand Down
5 changes: 5 additions & 0 deletions vendor/bat-native-ledger/include/bat/ledger/ledger_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ using RemovePendingContributionCallback = std::function<void(Result)>;
using PendingContributionsTotalCallback = std::function<void(double)>;
using GetCountryCodesCallback =
std::function<void(const std::vector<int32_t>&)>;
using SaveRecurringTipCallback = std::function<void(ledger::Result)>;

class LEDGER_EXPORT LedgerClient {
public:
Expand Down Expand Up @@ -168,6 +169,10 @@ class LEDGER_EXPORT LedgerClient {
const std::string& publisher_key,
const ledger::REWARDS_CATEGORY category) = 0;

virtual void SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) = 0;

virtual void GetRecurringTips(
ledger::PublisherInfoListCallback callback) = 0;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -908,6 +908,13 @@ void LedgerImpl::OnTimer(uint32_t timer_id) {
bat_contribution_->OnTimer(timer_id);
}

void LedgerImpl::SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) {
ledger_client_->SaveRecurringTip(std::move(info),
callback);
}

void LedgerImpl::GetRecurringTips(
ledger::PublisherInfoListCallback callback) {
ledger_client_->GetRecurringTips(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,10 @@ class LedgerImpl : public ledger::Ledger,
const int year,
const uint32_t date) override;

void SaveRecurringTip(
ledger::ContributionInfoPtr info,
ledger::SaveRecurringTipCallback callback) override;

void GetRecurringTips(ledger::PublisherInfoListCallback callback) override;

void GetOneTimeTips(ledger::PublisherInfoListCallback callback) override;
Expand Down
Loading

0 comments on commit 1a75192

Please sign in to comment.