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

Simplifies tip flow from service to shared lib #4870

Merged
merged 1 commit into from
Mar 6, 2020
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
1 change: 1 addition & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ class RewardsService : public KeyedService {
const double amount,
const bool recurring) = 0;

// Used in importer from muon days
virtual void OnTip(
const std::string& publisher_key,
double amount,
Expand Down
77 changes: 31 additions & 46 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2109,37 +2109,6 @@ void RewardsServiceImpl::OnPublisherBanner(
std::move(callback).Run(std::move(new_banner));
}

void RewardsServiceImpl::OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring,
ledger::PublisherInfoPtr publisher_info) {
if (!Connected()) {
return;
}

bat_ledger_->DoTip(
publisher_key,
amount,
std::move(publisher_info),
recurring,
base::BindOnce(&RewardsServiceImpl::OnDoTip, AsWeakPtr(), recurring));
}

void RewardsServiceImpl::OnDoTip(
const bool recurring,
const ledger::Result result) {
if (!recurring) {
return;
}

bool success = result == ledger::Result::LEDGER_OK;

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

void RewardsServiceImpl::OnSaveRecurringTipUI(
SaveRecurringTipCallback callback,
const ledger::Result result) {
Expand Down Expand Up @@ -2544,37 +2513,53 @@ void RewardsServiceImpl::GetRewardsInternalsInfo(
AsWeakPtr(), std::move(callback)));
}

void RewardsServiceImpl::OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring) {
OnTip(
publisher_key,
amount,
recurring,
static_cast<ledger::PublisherInfoPtr>(nullptr));
}

void RewardsServiceImpl::OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring,
std::unique_ptr<brave_rewards::ContentSite> site) {

if (!site) {
return;
}

auto info = ledger::PublisherInfo::New();
info->id = publisher_key;
info->status = static_cast<ledger::PublisherStatus>(site->status);
info->excluded = ledger::PublisherExclude::DEFAULT;
info->name = site->name;
info->url = site->url;
info->provider = site->provider;
info->favicon_url = site->favicon_url;

OnTip(publisher_key, amount, recurring, std::move(info));
bat_ledger_->SavePublisherInfo(
std::move(info),
base::BindOnce(&RewardsServiceImpl::OnTipPublisherSaved,
AsWeakPtr(),
publisher_key,
amount,
recurring));
}

void RewardsServiceImpl::OnTipPublisherSaved(
const std::string& publisher_key,
const double amount,
const bool recurring,
const ledger::Result result) {
if (result != ledger::Result::LEDGER_OK) {
return;
}

OnTip(publisher_key, amount, recurring);
}

void RewardsServiceImpl::OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring) {
if (recurring) {
SaveRecurringTipUI(publisher_key, amount, base::DoNothing());
return;
}

bat_ledger_->OneTimeTip(publisher_key, amount, base::DoNothing());
}

bool RewardsServiceImpl::Connected() const {
Expand Down
23 changes: 10 additions & 13 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,13 +250,13 @@ class RewardsServiceImpl : public RewardsService,
void OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring) override;
const bool recurring,
std::unique_ptr<brave_rewards::ContentSite> site) override;

void OnTip(
const std::string& publisher_key,
const double amount,
const bool recurring,
std::unique_ptr<brave_rewards::ContentSite> site) override;
const bool recurring) override;

void SetPublisherMinVisitTime(uint64_t duration_in_seconds) const override;

Expand Down Expand Up @@ -351,16 +351,6 @@ class RewardsServiceImpl : public RewardsService,
const bool recurring,
const double amount);

void OnTip(
const std::string& publisher_key,
double amount,
bool recurring,
ledger::PublisherInfoPtr publisher_info);

void OnDoTip(
const bool recurring,
const ledger::Result result);

void OnResetTheWholeState(base::Callback<void(bool)> callback,
bool success);
void OnRecurringTipUI(const ledger::Result result);
Expand Down Expand Up @@ -606,6 +596,12 @@ class RewardsServiceImpl : public RewardsService,

void PendingContributionSaved(const ledger::Result result) override;

void OnTipPublisherSaved(
const std::string& publisher_key,
const double amount,
const bool recurring,
const ledger::Result result);

// end ledger::LedgerClient

// Mojo Proxy methods
Expand All @@ -619,6 +615,7 @@ class RewardsServiceImpl : public RewardsService,
ledger::RewardsInternalsInfoPtr info);
void SetRewardsMainEnabledPref(bool enabled);
void SetRewardsMainEnabledMigratedPref(bool enabled);

void OnRefreshPublisher(
RefreshPublisherCallback callback,
const std::string& publisher_key,
Expand Down
45 changes: 33 additions & 12 deletions components/services/bat_ledger/bat_ledger_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -398,30 +398,26 @@ void BatLedgerImpl::GetContributionAmount(
std::move(callback).Run(ledger_->GetContributionAmount());
}

void BatLedgerImpl::OnDoTip(
CallbackHolder<DoTipCallback>* holder,
void BatLedgerImpl::OnOneTimeTip(
CallbackHolder<OneTimeTipCallback>* holder,
const ledger::Result result) {
DCHECK(holder);
if (holder->is_valid())
std::move(holder->get()).Run(result);
delete holder;
}

void BatLedgerImpl::DoTip(
void BatLedgerImpl::OneTimeTip(
const std::string& publisher_key,
const double amount,
ledger::PublisherInfoPtr info,
const bool recurring,
DoTipCallback callback) {
// deleted in OnDoTip
auto* holder = new CallbackHolder<DoTipCallback>(
OneTimeTipCallback callback) {
// deleted in OnOneTimeTip
auto* holder = new CallbackHolder<OneTimeTipCallback>(
AsWeakPtr(), std::move(callback));
ledger_->DoTip(
ledger_->OneTimeTip(
publisher_key,
amount,
std::move(info),
recurring,
std::bind(BatLedgerImpl::OnDoTip, holder, _1));
std::bind(BatLedgerImpl::OnOneTimeTip, holder, _1));
}

// static
Expand Down Expand Up @@ -984,4 +980,29 @@ void BatLedgerImpl::GetAllContributions(GetAllContributionsCallback callback) {
_1));
}

// static
void BatLedgerImpl::OnSavePublisherInfo(
CallbackHolder<SavePublisherInfoCallback>* holder,
const ledger::Result result) {
DCHECK(holder);
if (holder->is_valid()) {
std::move(holder->get()).Run(result);
}

delete holder;
}

void BatLedgerImpl::SavePublisherInfo(
ledger::PublisherInfoPtr info,
SavePublisherInfoCallback callback) {
auto* holder = new CallbackHolder<SavePublisherInfoCallback>(
AsWeakPtr(), std::move(callback));

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

} // namespace bat_ledger
18 changes: 12 additions & 6 deletions components/services/bat_ledger/bat_ledger_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,10 @@ class BatLedgerImpl : public mojom::BatLedger,
void GetPublisherBanner(const std::string& publisher_id,
GetPublisherBannerCallback callback) override;

void DoTip(
void OneTimeTip(
const std::string& publisher_key,
const double amount,
ledger::PublisherInfoPtr info,
const bool recurring,
DoTipCallback callback) override;
OneTimeTipCallback callback) override;

void RemoveRecurringTip(
const std::string& publisher_key,
Expand Down Expand Up @@ -208,6 +206,10 @@ class BatLedgerImpl : public mojom::BatLedger,

void GetAllContributions(GetAllContributionsCallback callback) override;

void SavePublisherInfo(
ledger::PublisherInfoPtr info,
SavePublisherInfoCallback callback) override;

private:
void SetCatalogIssuers(
const std::string& info) override;
Expand Down Expand Up @@ -286,8 +288,8 @@ class BatLedgerImpl : public mojom::BatLedger,
CallbackHolder<RemoveRecurringTipCallback>* holder,
const ledger::Result result);

static void OnDoTip(
CallbackHolder<DoTipCallback>* holder,
static void OnOneTimeTip(
CallbackHolder<OneTimeTipCallback>* holder,
const ledger::Result result);

static void OnGetTransactionHistory(
Expand Down Expand Up @@ -386,6 +388,10 @@ class BatLedgerImpl : public mojom::BatLedger,
CallbackHolder<GetAllContributionsCallback>* holder,
ledger::ContributionInfoList list);

static void OnSavePublisherInfo(
CallbackHolder<SavePublisherInfoCallback>* holder,
const ledger::Result result);

std::unique_ptr<BatLedgerClientMojoProxy> bat_ledger_client_mojo_proxy_;
std::unique_ptr<ledger::Ledger> ledger_;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ interface BatLedger {
GetPublisherBanner(string publisher_id) =>
(ledger.mojom.PublisherBanner? banner);

DoTip(string publisher_id, double amount, ledger.mojom.PublisherInfo? info, bool recurring) => (ledger.mojom.Result result);
OneTimeTip(string publisher_id, double amount) => (ledger.mojom.Result result);

RemoveRecurringTip(string publisher_key) => (ledger.mojom.Result result);
GetBootStamp() => (uint64 boot_stamp);
Expand Down Expand Up @@ -148,6 +148,8 @@ interface BatLedger {
GetContributionReport(ledger.mojom.ActivityMonth month, int32 year) => (array<ledger.mojom.ContributionReportInfo> list);

GetAllContributions() => (array<ledger.mojom.ContributionInfo> list);

SavePublisherInfo(ledger.mojom.PublisherInfo info) => (ledger.mojom.Result result);
};

interface BatLedgerClient {
Expand Down
8 changes: 5 additions & 3 deletions vendor/bat-native-ledger/include/bat/ledger/ledger.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,9 @@ class LEDGER_EXPORT Ledger {
virtual void CreateWallet(const std::string& safetynet_token,
CreateWalletCallback callback) = 0;

virtual void DoTip(
virtual void OneTimeTip(
const std::string& publisher_key,
const double amount,
ledger::PublisherInfoPtr info,
const bool recurring,
ledger::ResultCallback callback) = 0;

virtual void OnLoad(VisitDataPtr visit_data,
Expand Down Expand Up @@ -343,6 +341,10 @@ class LEDGER_EXPORT Ledger {

virtual void GetAllContributions(
ledger::ContributionInfoListCallback callback) = 0;

virtual void SavePublisherInfo(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does iOS need to call this for any reason?

Copy link
Contributor Author

@NejcZdovc NejcZdovc Mar 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nope, it's desktop specific usage

ledger::PublisherInfoPtr info,
ledger::ResultCallback callback) = 0;
};

} // namespace ledger
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -652,11 +652,9 @@ void Contribution::StartPhaseTwo(const std::string& viewing_id) {
phase_two_->Start(viewing_id);
}

void Contribution::DoTip(
void Contribution::OneTimeTip(
const std::string& publisher_key,
const double amount,
ledger::PublisherInfoPtr info,
const bool recurring,
ledger::ResultCallback callback) {
if (publisher_key.empty()) {
BLOG(ledger_, ledger::LogLevel::LOG_ERROR) <<
Expand All @@ -665,41 +663,6 @@ void Contribution::DoTip(
return;
}

if (info) {
auto save_callback = std::bind(&Contribution::ProcessTip,
this,
_1,
publisher_key,
amount,
recurring,
callback);
ledger_->SavePublisherInfo(std::move(info), save_callback);
return;
}

ProcessTip(
ledger::Result::LEDGER_OK,
publisher_key,
amount,
recurring,
callback);
}

void Contribution::ProcessTip(
const ledger::Result result,
const std::string& publisher_key,
const double amount,
const bool recurring,
ledger::ResultCallback callback) {
if (recurring) {
auto info = ledger::RecurringTip::New();
info->publisher_key = publisher_key;
info->amount = amount;
info->created_at = static_cast<uint64_t>(base::Time::Now().ToDoubleT());
ledger_->SaveRecurringTip(std::move(info), callback);
return;
}

const auto server_callback =
std::bind(&Contribution::OneTimeTipServerPublisher,
this,
Expand Down
Loading