Skip to content

Commit

Permalink
Fixes brave/brave-browser#2245, monthly donation functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanml committed Nov 30, 2018
1 parent f2e60e0 commit fb19e25
Show file tree
Hide file tree
Showing 22 changed files with 349 additions and 18 deletions.
39 changes: 39 additions & 0 deletions browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -138,5 +138,44 @@ ExtensionFunction::ResponseAction BraveRewardsGetGrantFunction::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_->AddRecurringPayment(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());
}


} // namespace api
} // namespace extensions
20 changes: 20 additions & 0 deletions browser/extensions/api/brave_rewards_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,26 @@ class BraveRewardsGetGrantFunction : 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;
};

} // namespace api
} // namespace extensions

Expand Down
37 changes: 37 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,17 @@
}
}
]
},
{
"name": "onRecurringDonations",
"type": "function",
"description": "",
"parameters": [
{
"name": "result",
"type": "any"
}
]
}
],
"functions": [
Expand Down Expand Up @@ -256,6 +267,32 @@
"type": "function",
"description": "Retrieves grant when panel is mounted",
"parameters": []
},
{
"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"
}
]
}
]
}
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 @@ -26,6 +26,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 @@ -3,7 +3,6 @@
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/extension_rewards_service_observer.h"

#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "chrome/browser/profiles/profile.h"
Expand Down Expand Up @@ -140,4 +139,37 @@ void ExtensionRewardsServiceObserver::OnGetPublisherActivityFromUrl(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnRecurringDonations(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {
extensions::EventRouter* event_router = extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

base::DictionaryValue result;
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));

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

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnRecurringDonations::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 @@ -9,6 +9,7 @@

#include "base/macros.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/rewards_service_private_observer.h"

class Profile;
Expand Down Expand Up @@ -40,6 +41,8 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t windowId) override;

void OnRecurringDonations(RewardsService* rewards_service,
brave_rewards::ContentSiteList) override;
private:
Profile* profile_;

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_rewards/browser/extension_rewards_service_observer.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/test/base/in_process_browser_test.h"
#include "chrome/test/base/ui_test_utils.h"
#include "content/public/test/browser_test_utils.h"

using namespace brave_rewards;

class ExtensionRewardsServiceObserverBrowserTest
: public InProcessBrowserTest,
public RewardsServiceObserver {
public:

void SetUpOnMainThread() override {
InProcessBrowserTest::SetUpOnMainThread();
rewards_service_ = RewardsServiceFactory::GetForProfile(browser()->profile());
}

void TearDown() override {
InProcessBrowserTest::TearDown();
}

void OnRecurringDonations(
RewardsService* rewards_service,
const ledger::PublisherInfoList& list) override {

ledger::PublisherInfo firstPublisher = list.front();
EXPECT_STREQ(firstPublisher.id.c_str(), "brave.com");
EXPECT_STREQ(std::to_string(firstPublisher.weight).c_str(), "10");

on_recurring_notifications_callback_was_called_ = true;
}

void WaitForOnRecurringDonationsCallback() {
if (on_recurring_notifications_callback_was_called_) {
return;
}

base::RunLoop run_loop;
run_loop.Run();
}

RewardsService* rewards_service_;
bool on_recurring_notifications_callback_was_called_ = false;
};

IN_PROC_BROWSER_TEST_F(ExtensionRewardsServiceObserverBrowserTest, SaveARecurringDonation) {
rewards_service_->AddObserver(this);

rewards_service_->AddRecurringPayment("brave.com", 10);
WaitForOnRecurringDonationsCallback();

rewards_service_->RemoveObserver(this);
}
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 @@ -92,6 +92,7 @@ class RewardsService : public KeyedService {
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount, bool recurring) = 0;
virtual void RemoveRecurring(const std::string& publisher_key) = 0;
virtual void AddRecurringPayment(const std::string& publisher_key, double new_amount) = 0;
virtual void UpdateRecurringDonationsList() = 0;
virtual void UpdateTipsList() = 0;
virtual void SetContributionAutoInclude(
Expand Down
18 changes: 18 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,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 @@ -1448,6 +1449,19 @@ ledger::PublisherInfoList GetRecurringDonationsOnFileTaskRunner(PublisherInfoDat
void RewardsServiceImpl::OnRecurringDonationsData(const ledger::PublisherInfoListCallback callback,
const ledger::PublisherInfoList list) {
callback(list, 0);

// Incoming ledger::PublisherInfoList needs to be converted to brave_rewards::ContentSiteList
brave_rewards::ContentSiteList site_list;
for (auto& info : list) {
site_list.push_back(PublisherInfoToContentSite(info));
}

TriggerOnRecurringDonations(site_list);
}

void RewardsServiceImpl::TriggerOnRecurringDonations(brave_rewards::ContentSiteList list) {
for (auto& observer : observers_)
observer.OnRecurringDonations(this, list);
}

void RewardsServiceImpl::GetRecurringDonations(ledger::PublisherInfoListCallback callback) {
Expand Down Expand Up @@ -1508,6 +1522,10 @@ void RewardsServiceImpl::OnTipsUpdatedData(const ledger::PublisherInfoList list)
}
}

void RewardsServiceImpl::AddRecurringPayment(const std::string& publisher_key, double new_amount) {
SaveRecurringDonation(publisher_key, new_amount);
}

void RewardsServiceImpl::RemoveRecurring(const std::string& publisher_key) {
ledger_->RemoveRecurring(publisher_key);
}
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ class RewardsServiceImpl : public RewardsService,
void RemoveRecurring(const std::string& publisher_key) override;
void UpdateRecurringDonationsList() override;
void UpdateTipsList() override;
void AddRecurringPayment(const std::string& publisher_key, double new_amount) override;
void SetContributionAutoInclude(
std::string publisher_key, bool excluded, uint64_t windowId) override;
RewardsNotificationService* GetNotificationService() const override;
Expand Down Expand Up @@ -184,6 +185,7 @@ class RewardsServiceImpl : public RewardsService,
ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t windowId);
void TriggerOnRecurringDonations(brave_rewards::ContentSiteList);

// ledger::LedgerClient
std::string GenerateGUID() const override;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ class MockRewardsServiceObserver : public RewardsServiceObserver {
MOCK_METHOD2(OnCurrentTips, void(RewardsService*, brave_rewards::ContentSiteList));
MOCK_METHOD2(OnPublisherBanner, void(RewardsService*, const brave_rewards::PublisherBanner));
MOCK_METHOD4(OnGetPublisherActivityFromUrl, void(RewardsService*, int, ledger::PublisherInfo*, uint64_t));
MOCK_METHOD2(OnRecurringDonations, void(brave_rewards::RewardsService*, const ledger::PublisherInfoList*));
};

class RewardsServiceTest : public testing::Test {
Expand Down
8 changes: 8 additions & 0 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,14 @@ class RewardsServiceObserver : public base::CheckedObserver {
brave_rewards::ContentSiteList) {};
virtual void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {};
virtual void OnGetPublisherActivityFromUrl(
brave_rewards::RewardsService* rewards_service,
int error_code,
ledger::PublisherInfo* info,
uint64_t windowId) {};
virtual void OnRecurringDonations(
brave_rewards::RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
};

} // namespace brave_rewards
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,3 +62,18 @@ export const includeInAutoContribution = (publisherKey: string, excluded: boolea
})

export const getGrant = () => action(types.GET_GRANT, {})

export const saveRecurringDonation = (publisherKey: string, newAmount: number) => action(types.SAVE_RECURRING_DONATION, {
publisherKey,
newAmount
})

export const removeRecurringContribution = (publisherKey: string) => action(types.REMOVE_RECURRING_DONATION, {
publisherKey
})

export const getRecurringDonations = () => action(types.GET_RECURRING_DONATIONS)

export const onRecurringDonations = (result: RewardsExtension.RecurringDonation) => action(types.ON_RECURRING_DONATIONS, {
result
})
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,7 @@ chrome.rewardsNotifications.onNotificationAdded.addListener((id: string, type: n
chrome.rewardsNotifications.onNotificationDeleted.addListener((id: string, type: number, timestamp: number) => {
rewardsPanelActions.onNotificationDeleted(id, type, timestamp)
})

chrome.braveRewards.onRecurringDonations.addListener((result: RewardsExtension.RecurringDonation) => {
rewardsPanelActions.onRecurringDonations(result)
})
Loading

0 comments on commit fb19e25

Please sign in to comment.