Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rewire SaveRecurringTip through ledger #2962

Merged
merged 3 commits into from
Jul 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 18 additions & 4 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -481,12 +481,26 @@ 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 @@ -215,6 +215,9 @@ class BraveRewardsSaveRecurringTipFunction :
~BraveRewardsSaveRecurringTipFunction() override;

ResponseAction Run() override;

private:
void OnSaveRecurringTip(bool success);
};

class BraveRewardsRemoveRecurringTipFunction :
Expand Down
5 changes: 4 additions & 1 deletion components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,10 @@ class MockRewardsService : public RewardsService {
void(brave_rewards::GetRewardsInternalsInfoCallback));
MOCK_METHOD1(GetTransactionHistory,
void(brave_rewards::GetTransactionHistoryCallback));
MOCK_METHOD2(SaveRecurringTip, void(const std::string&, const int));
MOCK_METHOD3(SaveRecurringTipUI,
void(const std::string&,
const int,
brave_rewards::SaveRecurringTipCallback));
MOCK_METHOD2(RefreshPublisher, void(const std::string&,
brave_rewards::RefreshPublisherCallback));
MOCK_METHOD0(GetAllNotifications,
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 @@ -69,6 +69,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 @@ -220,8 +221,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
58 changes: 49 additions & 9 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2153,7 +2153,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 @@ -2212,25 +2212,65 @@ bool SaveRecurringTipOnFileTaskRunner(
return false;
}

void RewardsServiceImpl::OnRecurringTipSaved(bool success) {
void RewardsServiceImpl::OnSaveRecurringTipUI(
SaveRecurringTipCallback callback,
const ledger::Result result) {
bool success = result == ledger::Result::LEDGER_OK;

for (auto& observer : observers_) {
observer.OnRecurringTipSaved(this, success);
}

std::move(callback).Run(success);
}

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,
const bool 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::OnMediaInlineInfoSaved(
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 @@ -210,8 +210,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,
const ledger::Result result);
void SaveRecurringTipUI(
const std::string& publisher_key,
const int amount,
SaveRecurringTipCallback callback) override;

const RewardsNotificationService::RewardsNotificationsMap&
GetAllNotifications() override;
Expand Down Expand Up @@ -344,7 +349,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,
const bool success);
void OnGetRecurringTips(
const ledger::PublisherInfoListCallback callback,
ledger::PublisherInfoList list);
Expand Down Expand Up @@ -539,6 +546,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 @@ -429,6 +429,24 @@ void BatLedgerClientMojoProxy::FetchFavIcon(const std::string& url,
base::BindOnce(&OnFetchFavIcon, std::move(callback)));
}

void OnSaveRecurringTip(const ledger::SaveRecurringTipCallback& callback,
const ledger::Result result) {
callback(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 @@ -79,6 +79,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 @@ -476,6 +476,27 @@ void BatLedgerImpl::GetRewardsInternalsInfo(
std::bind(BatLedgerImpl::OnGetRewardsInternalsInfo, holder, _1));
}

// static
void BatLedgerImpl::OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
const 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 @@ -136,6 +136,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 @@ -254,6 +257,10 @@ class BatLedgerImpl : public mojom::BatLedger,
CallbackHolder<GetRewardsInternalsInfoCallback>* holder,
ledger::RewardsInternalsInfoPtr info);

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 @@ -360,6 +360,28 @@ void LedgerClientMojoProxy::FetchFavIcon(const std::string& url,
std::bind(LedgerClientMojoProxy::OnFetchFavIcon, holder, _1, _2));
}

// static
void LedgerClientMojoProxy::OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
const ledger::Result result) {
if (holder->is_valid())
std::move(holder->get()).Run(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 @@ -64,6 +64,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 @@ -245,6 +248,10 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient,
bool success,
const std::string& favicon_url);

static void OnSaveRecurringTip(
CallbackHolder<SaveRecurringTipCallback>* holder,
const 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 @@ -110,6 +110,7 @@ interface BatLedger {
GetTransactionHistory() => (string transactions);
GetRewardsInternalsInfo() => (ledger.mojom.RewardsInternalsInfo info);

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

Expand Down Expand Up @@ -183,6 +184,7 @@ interface BatLedgerClient {
uint64 window_id);
FetchFavIcon(string url, string favicon_key) => (bool success,
string favicon_url);
SaveRecurringTip(ledger.mojom.ContributionInfo info) => (ledger.mojom.Result result);
GetRecurringTips() => (
array<ledger.mojom.PublisherInfo> publisher_info_list,
uint32 next_record);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ class MockConfirmationsClient : public ConfirmationsClient {
const std::string& publisher_key,
const ledger::REWARDS_CATEGORY category));

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

MOCK_METHOD1(GetRecurringTips, void(
ledger::PublisherInfoListCallback callback));

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 @@ -242,6 +242,9 @@ class LEDGER_EXPORT Ledger {
virtual void GetRewardsInternalsInfo(
ledger::RewardsInternalsInfoCallback callback) = 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 @@ -77,6 +77,7 @@ using GetExternalWalletsCallback =
using ShowNotificationCallback = std::function<void(const Result)>;
using SavePendingContributionCallback = std::function<void(const Result)>;
using DeleteActivityInfoCallback = std::function<void(const ledger::Result)>;
using SaveRecurringTipCallback = std::function<void(const Result)>;

class LEDGER_EXPORT LedgerClient {
public:
Expand Down Expand Up @@ -161,6 +162,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 @@ -887,6 +887,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
Loading