Skip to content

Commit

Permalink
Merge pull request #2069 from brave/monthly-amount-dropdown-2
Browse files Browse the repository at this point in the history
Adds functionality to monthly contribution dropdown (Panel, #3 origin)
  • Loading branch information
ryanml authored Apr 8, 2019
2 parents 619d140 + 5d9baec commit 8746c0c
Show file tree
Hide file tree
Showing 29 changed files with 616 additions and 66 deletions.
134 changes: 134 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,139 @@ ExtensionFunction::ResponseAction BraveRewardsSaveSettingFunction::Run() {
return RespondNow(NoArguments());
}

BraveRewardsSaveRecurringTipFunction::
~BraveRewardsSaveRecurringTipFunction() {
}

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

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

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

return RespondNow(NoArguments());
}

BraveRewardsRemoveRecurringTipFunction::
~BraveRewardsRemoveRecurringTipFunction() {
}

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

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

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

return RespondNow(NoArguments());
}

BraveRewardsGetRecurringTipsFunction::
~BraveRewardsGetRecurringTipsFunction() {
}

ExtensionFunction::ResponseAction
BraveRewardsGetRecurringTipsFunction::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->GetRecurringTipsUI(base::Bind(
&BraveRewardsGetRecurringTipsFunction::OnGetRecurringTips,
this));
return RespondLater();
}

void BraveRewardsGetRecurringTipsFunction::OnGetRecurringTips(
std::unique_ptr<::brave_rewards::ContentSiteList> list) {
std::unique_ptr<base::DictionaryValue> result(new base::DictionaryValue());
auto recurringTips = std::make_unique<base::ListValue>();

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

result->SetList("recurringTips", std::move(recurringTips));
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 BraveRewardsSaveRecurringTipFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.saveRecurringTip", UNKNOWN)

protected:
~BraveRewardsSaveRecurringTipFunction() override;

ResponseAction Run() override;
};

class BraveRewardsRemoveRecurringTipFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.removeRecurringTip", UNKNOWN)

protected:
~BraveRewardsRemoveRecurringTipFunction() override;

ResponseAction Run() override;
};

class BraveRewardsGetRecurringTipsFunction :
public UIThreadExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("braveRewards.getRecurringTips", UNKNOWN)

protected:
~BraveRewardsGetRecurringTipsFunction() override;

ResponseAction Run() override;

private:
void OnGetRecurringTips(
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
64 changes: 41 additions & 23 deletions browser/ui/webui/brave_donate_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,15 +57,18 @@ 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 OnRecurringTipSaved(brave_rewards::RewardsService* rewards_service,
bool success) override;

void OnRecurringTipRemoved(brave_rewards::RewardsService* rewards_service,
bool success) override;
Expand Down Expand Up @@ -118,7 +121,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 +235,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 Expand Up @@ -312,5 +319,16 @@ void RewardsDonateDOMHandler::OnRecurringTipRemoved(
"brave_rewards_donate.recurringTipRemoved", base::Value(success));
}

void RewardsDonateDOMHandler::OnRecurringTipSaved(
brave_rewards::RewardsService* rewards_service,
bool success) {
if (!web_ui()->CanCallJavascript()) {
return;
}

web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards_donate.recurringTipSaved", base::Value(success));
}

BraveDonateUI::~BraveDonateUI() {
}
Loading

0 comments on commit 8746c0c

Please sign in to comment.