Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Fixes brave/brave-browser#2823

Monthly donation functionality via panel
  • Loading branch information
ryanml authored and NejcZdovc committed Mar 25, 2019
1 parent 596d189 commit ebdb5b4
Show file tree
Hide file tree
Showing 23 changed files with 446 additions and 11 deletions.
102 changes: 102 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -270,5 +270,107 @@ void BraveRewardsGetACEnabledFunction::OnGetACEnabled(bool enabled) {
Respond(OneArgument(std::make_unique<base::Value>(enabled)));
}

BraveRewardsSaveRecurringDonationFunction::
~BraveRewardsSaveRecurringDonationFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsSaveRecurringDonationFunction::Run() {

std::unique_ptr<brave_rewards::SaveRecurringDonation::Params> params(
brave_rewards::SaveRecurringDonation::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service_ =
RewardsServiceFactory::GetForProfile(profile);

if (rewards_service_) {
rewards_service_->SaveRecurringDonation(
params->publisher_key, params->new_amount);
}

return RespondNow(NoArguments());
}

BraveRewardsRemoveRecurringDonationFunction::
~BraveRewardsRemoveRecurringDonationFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsRemoveRecurringDonationFunction::Run() {

std::unique_ptr<brave_rewards::RemoveRecurringDonation::Params> params(
brave_rewards::RemoveRecurringDonation::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service_ =
RewardsServiceFactory::GetForProfile(profile);

if (rewards_service_) {
rewards_service_->RemoveRecurring(params->publisher_key);
}

return RespondNow(NoArguments());
}

BraveRewardsGetRecurringDonationsFunction::
~BraveRewardsGetRecurringDonationsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetRecurringDonationsFunction::Run() {
Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->GetRecurringDonationsList(base::Bind(
&BraveRewardsGetRecurringDonationsFunction::OnGetRecurringDonations,
this));
return RespondLater();
}

void BraveRewardsGetRecurringDonationsFunction::OnGetRecurringDonations(
std::unique_ptr<::brave_rewards::ContentSiteList> list) {
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
auto recurringDonations = std::make_unique<base::ListValue>();

if (!list->empty()) {
for (auto const& item: *list) {
auto recurringDonation = std::make_unique<base::DictionaryValue>();
recurringDonation->SetString("publisherKey", item.id);
recurringDonation->SetInteger("amount", item.weight);
recurringDonations->Append(std::move(recurringDonation));
}
}

result->SetList("recurringDonations", std::move(recurringDonations));
Respond(OneArgument(std::move(result)));
}

BraveRewardsGetPublisherDonationAmountsFunction::
~BraveRewardsGetPublisherDonationAmountsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetPublisherDonationAmountsFunction::Run() {
std::unique_ptr<brave_rewards::GetPublisherDonationAmounts::Params> params(
brave_rewards::GetPublisherDonationAmounts::Params::Create(*args_));

Profile* profile = Profile::FromBrowserContext(browser_context());
RewardsService* rewards_service =
RewardsServiceFactory::GetForProfile(profile);

if (!rewards_service) {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->GetPublisherBanner(params->publisher_key);
return RespondNow(NoArguments());
}

} // namespace api
} // namespace extensions
50 changes: 50 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#define BRAVE_BROWSER_EXTENSIONS_API_BRAVE_REWARDS_API_H_

#include "extensions/browser/extension_function.h"
#include "brave/components/brave_rewards/browser/content_site.h"

namespace extensions {
namespace api {
Expand Down Expand Up @@ -155,6 +156,55 @@ class BraveRewardsGetACEnabledFunction : public UIThreadExtensionFunction {
void OnGetACEnabled(bool enabled);
};

class BraveRewardsSaveRecurringDonationFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.saveRecurringDonation", UNKNOWN)

protected:
~BraveRewardsSaveRecurringDonationFunction() override;

ResponseAction Run() override;
};

class BraveRewardsRemoveRecurringDonationFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.removeRecurringDonation", UNKNOWN)

protected:
~BraveRewardsRemoveRecurringDonationFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetRecurringDonationsFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getRecurringDonations", UNKNOWN)

protected:
~BraveRewardsGetRecurringDonationsFunction() override;

ResponseAction Run() override;

private:
void OnGetRecurringDonations(
std::unique_ptr<brave_rewards::ContentSiteList> list);
};

class BraveRewardsGetPublisherDonationAmountsFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"braveRewards.getPublisherDonationAmounts", UNKNOWN)

protected:
~BraveRewardsGetPublisherDonationAmountsFunction() override;

ResponseAction Run() override;
};

} // namespace api
} // namespace extensions

Expand Down
68 changes: 68 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,20 @@
}
}
]
},
{
"name": "onPublisherDonationAmounts",
"type": "function",
"description": "Fired when publisher donation info is retreived",
"parameters": [
{
"name": "amounts",
"type": "array",
"items": {
"type": "number"
}
}
]
}
],
"functions": [
Expand Down Expand Up @@ -462,6 +476,60 @@
"type": "string"
}
]
},
{
"name": "saveRecurringDonation",
"type": "function",
"description": "Updates recurring donation amount for rewards panel",
"parameters": [
{
"name": "publisher_key",
"type": "string"
},
{
"name": "new_amount",
"type": "integer"
}
]
},
{
"name": "removeRecurringDonation",
"type": "function",
"description": "Removes recurring donation for rewards panel",
"parameters": [
{
"name": "publisher_key",
"type": "string"
}
]
},
{
"name": "getRecurringDonations",
"type": "function",
"description": "Gets list of sites with recurring donation data",
"parameters": [
{
"type": "function",
"name": "callback",
"parameters": [
{
"name": "siteList",
"type": "any"
}
]
}
]
},
{
"name": "getPublisherDonationAmounts",
"type": "function",
"description": "Retreives custom donation amounts",
"parameters": [
{
"name": "publisher_key",
"type": "string"
}
]
}
]
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/content_site.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace brave_rewards {
percentage(0),
verified(false),
excluded(0),
weight(0),
reconcile_stamp(0) {
}

Expand All @@ -26,6 +27,7 @@ namespace brave_rewards {
url = properties.url;
provider = properties.provider;
id = properties.id;
weight = properties.weight;
reconcile_stamp = properties.reconcile_stamp;
}

Expand Down
1 change: 1 addition & 0 deletions components/brave_rewards/browser/content_site.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ struct ContentSite {
std::string favicon_url;
std::string url;
std::string provider;
double weight;
uint64_t reconcile_stamp;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/event_router.h"
#include "bat/ledger/ledger_callback_handler.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"

namespace brave_rewards {

Expand Down Expand Up @@ -325,4 +326,30 @@ void ExtensionRewardsServiceObserver::OnExcludedSitesChanged(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnPublisherBanner(
RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
auto* event_router = extensions::EventRouter::Get(profile_);

if (!event_router) {
return;
}

std::vector<double> amounts;
for (int const& value : banner.amounts) {
amounts.push_back(value);
}

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::
OnPublisherDonationAmounts::Create(amounts)
.release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnPublisherDonationAmounts::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnPendingContributionSaved(RewardsService* rewards_service,
int result) override;

void OnPublisherBanner(
RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) override;

private:
Profile* profile_;

Expand Down
7 changes: 7 additions & 0 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ using GetRewardsMainEnabledCallback = base::Callback<void(bool)>;
using ConfirmationsHistoryCallback = base::Callback<void(int, double)>;
using GetRewardsInternalsInfoCallback = base::OnceCallback<void(
std::unique_ptr<brave_rewards::RewardsInternalsInfo>)>;
using GetRecurringDonationsListCallback =
base::OnceCallback<void(std::unique_ptr<brave_rewards::ContentSiteList>)>;

class RewardsService : public KeyedService {
public:
Expand Down Expand Up @@ -188,6 +190,11 @@ class RewardsService : public KeyedService {

static void RegisterProfilePrefs(PrefRegistrySimple* registry);

virtual void SaveRecurringDonation(
const std::string& publisher_key, const int amount) = 0;
virtual void GetRecurringDonationsList(
GetRecurringDonationsListCallback callback) = 0;

protected:
base::ObserverList<RewardsServiceObserver> observers_;

Expand Down
27 changes: 27 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ ContentSite PublisherInfoToContentSite(
content_site.provider = publisher_info.provider;
content_site.favicon_url = publisher_info.favicon_url;
content_site.id = publisher_info.id;
content_site.weight = publisher_info.weight;
content_site.reconcile_stamp = publisher_info.reconcile_stamp;
return content_site;
}
Expand Down Expand Up @@ -2208,6 +2209,32 @@ void RewardsServiceImpl::GetRecurringDonations(
callback));
}

void RewardsServiceImpl::GetRecurringDonationsList(
GetRecurringDonationsListCallback callback) {
base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
base::BindOnce(&GetRecurringDonationsOnFileTaskRunner,
publisher_info_backend_.get()),
base::BindOnce(&RewardsServiceImpl::OnRecurringDonationsDataList,
AsWeakPtr(),
std::move(callback)));
}

void RewardsServiceImpl::OnRecurringDonationsDataList(
GetRecurringDonationsListCallback callback,
ledger::PublisherInfoList list) {
if (!Connected()) {
return;
}

auto site_list =
std::make_unique<brave_rewards::ContentSiteList>();
for (auto& info : list) {
site_list->push_back(PublisherInfoToContentSite(info));
}

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

void RewardsServiceImpl::UpdateRecurringDonationsList() {
GetRecurringDonations(
std::bind(&RewardsServiceImpl::OnRecurringDonationUpdated, this, _1));
Expand Down
Loading

0 comments on commit ebdb5b4

Please sign in to comment.