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 Apr 8, 2019
1 parent 805ccaf commit 2b3debd
Show file tree
Hide file tree
Showing 26 changed files with 587 additions and 64 deletions.
135 changes: 135 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -291,5 +291,140 @@ ExtensionFunction::ResponseAction BraveRewardsSaveSettingFunction::Run() {
return RespondNow(NoArguments());
}

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)));
}

BraveRewardsGetPublisherBannerFunction::
~BraveRewardsGetPublisherBannerFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetPublisherBannerFunction::Run() {
std::unique_ptr<brave_rewards::GetPublisherBanner::Params> params(
brave_rewards::GetPublisherBanner::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,
base::BindOnce(
&BraveRewardsGetPublisherBannerFunction::OnPublisherBanner,
this));
return RespondLater();
}

void BraveRewardsGetPublisherBannerFunction::OnPublisherBanner(
std::unique_ptr<::brave_rewards::PublisherBanner> banner) {
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());

if (banner) {
result->SetString("publisherKey", banner->publisher_key);
result->SetString("title", banner->title);
result->SetString("name", banner->name);
result->SetString("description", banner->description);
result->SetString("background", banner->background);
result->SetString("logo", banner->logo);
result->SetString("provider", banner->provider);
result->SetBoolean("verified", banner->verified);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner->amounts) {
amounts->AppendInteger(value);
}
result->SetList("amounts", std::move(amounts));

auto social = std::make_unique<base::DictionaryValue>();
for (auto const& item : banner->social) {
social->SetString(item.first, item.second);
}
result->SetDictionary("social", std::move(social));
}

Respond(OneArgument(std::move(result)));
}

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

#include <memory>

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

namespace extensions {
namespace api {
Expand Down Expand Up @@ -165,6 +169,59 @@ class BraveRewardsSaveSettingFunction : public UIThreadExtensionFunction {
ResponseAction Run() override;
};

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 BraveRewardsGetPublisherBannerFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION(
"braveRewards.getPublisherBanner", UNKNOWN)

protected:
~BraveRewardsGetPublisherBannerFunction() override;

ResponseAction Run() override;

private:
void OnPublisherBanner(
std::unique_ptr<::brave_rewards::PublisherBanner> banner);
};

} // namespace api
} // namespace extensions

Expand Down
52 changes: 28 additions & 24 deletions browser/ui/webui/brave_donate_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,16 @@ class RewardsDonateDOMHandler : public WebUIMessageHandler,
void OnGetRecurringTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);

void OnPublisherBanner(
std::unique_ptr<brave_rewards::PublisherBanner> banner);

// RewardsServiceObserver implementation
void OnWalletProperties(
brave_rewards::RewardsService* rewards_service,
int error_code,
std::unique_ptr<brave_rewards::WalletProperties> wallet_properties)
override;

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

void OnRecurringTipRemoved(brave_rewards::RewardsService* rewards_service,
bool success) override;

Expand Down Expand Up @@ -118,7 +118,10 @@ void RewardsDonateDOMHandler::GetPublisherDonateData(
const base::ListValue* args) {
std::string publisher_key;
args->GetString(0, &publisher_key);
rewards_service_->GetPublisherBanner(publisher_key);
rewards_service_->GetPublisherBanner(
publisher_key,
base::Bind(&RewardsDonateDOMHandler::OnPublisherBanner,
weak_factory_.GetWeakPtr()));
}

void RewardsDonateDOMHandler::GetWalletProperties(const base::ListValue* args) {
Expand Down Expand Up @@ -229,33 +232,34 @@ void RewardsDonateDOMHandler::OnGetRecurringTips(
}

void RewardsDonateDOMHandler::OnPublisherBanner(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {
std::unique_ptr<brave_rewards::PublisherBanner> banner) {
if (!web_ui()->CanCallJavascript()) {
return;
}

base::DictionaryValue result;
result.SetString("publisherKey", banner.publisher_key);
result.SetString("title", banner.title);
result.SetString("name", banner.name);
result.SetString("description", banner.description);
result.SetString("background", banner.background);
result.SetString("logo", banner.logo);
result.SetString("provider", banner.provider);
result.SetBoolean("verified", banner.verified);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner.amounts) {
amounts->AppendInteger(value);
}
result.SetList("amounts", std::move(amounts));
if (banner) {
result.SetString("publisherKey", banner->publisher_key);
result.SetString("title", banner->title);
result.SetString("name", banner->name);
result.SetString("description", banner->description);
result.SetString("background", banner->background);
result.SetString("logo", banner->logo);
result.SetString("provider", banner->provider);
result.SetBoolean("verified", banner->verified);

auto amounts = std::make_unique<base::ListValue>();
for (int const& value : banner->amounts) {
amounts->AppendInteger(value);
}
result.SetList("amounts", std::move(amounts));

auto social = std::make_unique<base::DictionaryValue>();
for (auto const& item : banner.social) {
social->SetString(item.first, item.second);
auto social = std::make_unique<base::DictionaryValue>();
for (auto const& item : banner->social) {
social->SetString(item.first, item.second);
}
result.SetDictionary("social", std::move(social));
}
result.SetDictionary("social", std::move(social));

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.publisherBanner", result);
Expand Down
Loading

0 comments on commit 2b3debd

Please sign in to comment.