Skip to content

Commit

Permalink
Adds detailed view for pending contributions
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 20, 2019
1 parent 42bc703 commit 6870c7c
Show file tree
Hide file tree
Showing 46 changed files with 1,350 additions and 74 deletions.
96 changes: 91 additions & 5 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,12 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetOneTimeTips(
std::unique_ptr<brave_rewards::ContentSiteList> list);

void GetPendingContributions(const base::ListValue* args);
void OnGetPendingContributions(
std::unique_ptr<brave_rewards::PendingContributionInfoList> list);
void RemovePendingContribution(const base::ListValue* args);
void RemoveAllPendingContributions(const base::ListValue* args);

// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
uint32_t result) override;
Expand Down Expand Up @@ -143,7 +149,6 @@ class RewardsDOMHandler : public WebUIMessageHandler,
const std::string& viewing_id,
const std::string& category,
const std::string& probi) override;

void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) override;
Expand All @@ -170,6 +175,10 @@ class RewardsDOMHandler : public WebUIMessageHandler,
bool success,
int category) override;

void OnPendingContributionRemoved(
brave_rewards::RewardsService* rewards_service,
int32_t result) override;

// RewardsNotificationsServiceObserver implementation
void OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
Expand Down Expand Up @@ -292,6 +301,16 @@ void RewardsDOMHandler::RegisterMessages() {
web_ui()->RegisterMessageCallback("brave_rewards.getExcludedPublishersNumber",
base::BindRepeating(&RewardsDOMHandler::GetExcludedPublishersNumber,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getPendingContributions",
base::BindRepeating(&RewardsDOMHandler::GetPendingContributions,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.removePendingContribution",
base::BindRepeating(&RewardsDOMHandler::RemovePendingContribution,
base::Unretained(this)));
web_ui()->RegisterMessageCallback(
"brave_rewards.removeAllPendingContribution",
base::BindRepeating(&RewardsDOMHandler::RemoveAllPendingContributions,
base::Unretained(this)));
}

void RewardsDOMHandler::Init() {
Expand Down Expand Up @@ -941,6 +960,7 @@ void RewardsDOMHandler::SetBackupCompleted(const base::ListValue *args) {

void RewardsDOMHandler::GetPendingContributionsTotal(
const base::ListValue* args) {
// refactor this as total is not needed and we should just fetch all of them
if (rewards_service_) {
rewards_service_->GetPendingContributionsTotal(base::Bind(
&RewardsDOMHandler::OnGetPendingContributionsTotal,
Expand All @@ -958,10 +978,10 @@ void RewardsDOMHandler::OnGetPendingContributionsTotal(double amount) {
void RewardsDOMHandler::OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onPendingContributionSaved", base::Value(result));
}
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onPendingContributionSaved", base::Value(result));
}
}

void RewardsDOMHandler::OnRewardsMainEnabled(
Expand Down Expand Up @@ -1079,6 +1099,72 @@ void RewardsDOMHandler::OnContributionSaved(
"brave_rewards.onContributionSaved", result);
}

void RewardsDOMHandler::GetPendingContributions(
const base::ListValue* args) {
if (rewards_service_) {
rewards_service_->GetPendingContributionsUI(base::Bind(
&RewardsDOMHandler::OnGetPendingContributions,
weak_factory_.GetWeakPtr()));
}
}

void RewardsDOMHandler::OnGetPendingContributions(
std::unique_ptr<brave_rewards::PendingContributionInfoList> list) {
if (web_ui()->CanCallJavascript()) {
auto contributions = std::make_unique<base::ListValue>();
for (auto const& item : *list) {
auto contribution = std::make_unique<base::DictionaryValue>();
contribution->SetString("publisherKey", item.publisher_key);
contribution->SetBoolean("verified", item.verified);
contribution->SetString("name", item.name);
contribution->SetString("provider", item.provider);
contribution->SetString("url", item.url);
contribution->SetString("favIcon", item.favicon_url);
contribution->SetDouble("amount", item.amount);
contribution->SetInteger("addedDate", item.added_date);
contribution->SetInteger("category", item.category);
contribution->SetString("viewingId", item.viewing_id);
contribution->SetInteger("expirationDate", item.expiration_date);
contributions->Append(std::move(contribution));
}

web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.pendingContributions",
*contributions);
}
}

void RewardsDOMHandler::RemovePendingContribution(
const base::ListValue* args) {
if (rewards_service_) {
std::string publisher_key;
std::string viewing_id;
int added_date;
args->GetString(0, &publisher_key);
args->GetString(1, &viewing_id);
args->GetInteger(2, &added_date);
rewards_service_->RemovePendingContributionUI(
publisher_key,
viewing_id,
static_cast<uint64_t>(added_date));
}
}

void RewardsDOMHandler::RemoveAllPendingContributions(
const base::ListValue* args) {
if (rewards_service_) {
rewards_service_->RemoveAllPendingContributionsUI();
}
}

void RewardsDOMHandler::OnPendingContributionRemoved(
brave_rewards::RewardsService* rewards_service,
int32_t result) {
if (web_ui()->CanCallJavascript()) {
web_ui()->CallJavascriptFunctionUnsafe(
"brave_rewards.onRemovePendingContribution", base::Value(result));
}
}

} // namespace

BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name)
Expand Down
8 changes: 8 additions & 0 deletions browser/ui/webui/brave_webui_source.cc
Original file line number Diff line number Diff line change
Expand Up @@ -368,6 +368,13 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "paymentMonthly", IDS_BRAVE_UI_PAYMENT_MONTHLY },
{ "paymentNotMade", IDS_BRAVE_UI_PAYMENT_NOT_MADE },
{ "paymentWarning", IDS_BRAVE_UI_PAYMENT_WARNING },
{ "pendingContributions", IDS_BRAVE_UI_PENDING_CONTRIBUTIONS },
{ "pendingContributionEmpty", IDS_BRAVE_UI_PENDING_CONTRIBUTION_EMPTY },
{ "pendingContributionRemoveAll", IDS_BRAVE_UI_PENDING_CONTRIBUTION_REMOVE_ALL }, // NOLINT
{ "pendingTyperecurring", IDS_BRAVE_UI_PENDING_TYPE_RECURRING },
{ "pendingTypetip", IDS_BRAVE_UI_PENDING_TYPE_TIP },
{ "pendingTypeac", IDS_BRAVE_UI_PENDING_TYPE_AC },
{ "pendingUntil", IDS_BRAVE_UI_PENDING_UNTIL },
{ "pinnedSitesHeader", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_HEADER },
{ "pinnedSitesMsg", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_MSG },
{ "pinnedSitesOne", IDS_BRAVE_UI_PAYMENT_PINNED_SITES_ONE },
Expand All @@ -386,6 +393,7 @@ void CustomizeWebUIHTMLSource(const std::string &name,
{ "remove", IDS_BRAVE_UI_REMOVE },
{ "reservedAmountText", IDS_BRAVE_UI_RESERVED_AMOUNT_TEXT },
{ "reservedMoreLink", IDS_BRAVE_UI_RESERVED_MORE_LINK },
{ "reservedAllLink", IDS_BRAVE_UI_RESERVED_ALL_LINK },
{ "restore", IDS_BRAVE_UI_RESTORE },
{ "restoreAll", IDS_BRAVE_UI_RESTORE_ALL },
{ "reviewSitesMsg", IDS_BRAVE_UI_REVIEW_SITE_MSG },
Expand Down
11 changes: 11 additions & 0 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,17 @@
"type": "boolean"
}
]
},
{
"name": "onPendingContributionRemoved",
"type": "function",
"description": "",
"parameters": [
{
"name": "result",
"type": "integer"
}
]
}
],
"functions": [
Expand Down
6 changes: 6 additions & 0 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,12 @@ class MockRewardsService : public RewardsService {
brave_rewards::RefreshPublisherCallback));
MOCK_METHOD0(GetAllNotifications,
const brave_rewards::RewardsNotificationService::RewardsNotificationsMap&());
MOCK_METHOD1(GetPendingContributionsUI,
void(brave_rewards::GetPendingContributionsCallback));
MOCK_METHOD3(RemovePendingContributionUI, void(const std::string&,
const std::string&,
uint64_t));
MOCK_METHOD0(RemoveAllPendingContributionsUI, void());
};

class AdsServiceTest : public testing::Test {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_rewards/browser/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ source_set("browser") {
"wallet_properties.h",
"grant.cc",
"grant.h",
"pending_contribution.cc",
"pending_contribution.h",
"publisher_banner.cc",
"publisher_banner.h",
"contribution_info.cc",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,4 +359,23 @@ void ExtensionRewardsServiceObserver::OnRecurringTipRemoved(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnPendingContributionRemoved(
RewardsService* rewards_service,
int32_t result) {
extensions::EventRouter* event_router =
extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnPendingContributionRemoved::Create(
result).release());
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnPendingContributionRemoved::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 @@ -46,6 +46,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnRecurringTipRemoved(RewardsService* rewards_service,
bool success) override;

void OnPendingContributionRemoved(RewardsService* rewards_service,
int32_t result) override;

// RewardsServicePrivateObserver implementation
void OnGetCurrentBalanceReport(RewardsService* rewards_service,
const BalanceReport& balance_report) override;
Expand Down
30 changes: 23 additions & 7 deletions components/brave_rewards/browser/pending_contribution.cc
Original file line number Diff line number Diff line change
@@ -1,16 +1,32 @@
/* 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/. */
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/pending_contribution.h"

namespace brave_rewards {

PendingContribution::PendingContribution() :
PendingContributionInfo::PendingContributionInfo() :
amount(0),
added_date(0),
reconcile_date(0) {
added_date(0) {
}

PendingContribution::~PendingContribution() { }
PendingContributionInfo::~PendingContributionInfo() { }

PendingContributionInfo::PendingContributionInfo(
const PendingContributionInfo& data) {
publisher_key = data.publisher_key;
verified = data.verified;
name = data.name;
favicon_url = data.favicon_url;
url = data.url;
provider = data.provider;
amount = data.amount;
added_date = data.added_date;
viewing_id = data.viewing_id;
category = data.category;
expiration_date = data.expiration_date;
}

} // namespace brave_rewards
33 changes: 21 additions & 12 deletions components/brave_rewards/browser/pending_contribution.h
Original file line number Diff line number Diff line change
@@ -1,27 +1,36 @@
/* 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/. */
/* Copyright (c) 2019 The Brave Authors. All rights reserved.
* 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/. */

#ifndef BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_
#define BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_
#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_

#include <string>
#include <vector>

namespace brave_rewards {

struct PendingContribution {
PendingContribution();
~PendingContribution();
PendingContribution(const PendingContribution& data) = default;
struct PendingContributionInfo {
PendingContributionInfo();
~PendingContributionInfo();
PendingContributionInfo(const PendingContributionInfo& data);

std::string publisher_key;
bool verified;
std::string name;
std::string favicon_url;
std::string url;
std::string provider;
double amount = 0;
uint32_t added_date = 0;
uint32_t reconcile_date = 0;
std::string viewing_id;
int category;
uint32_t expiration_date = 0;
};

using PendingContributionList = std::vector<PendingContribution>;
using PendingContributionInfoList = std::vector<PendingContributionInfo>;

} // namespace brave_rewards

#endif //BRAVE_BROWSER_PAYMENTS_PENDING_CONTRIBUTION_
#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_PENDING_CONTRIBUTION_H_
Loading

0 comments on commit 6870c7c

Please sign in to comment.