Skip to content

Commit

Permalink
Merge pull request #1782 from brave/include-switch
Browse files Browse the repository at this point in the history
Fixes include/excluded update in the panel
  • Loading branch information
NejcZdovc authored Feb 28, 2019
2 parents 1a63dc2 + 4c0698b commit 027fa5c
Show file tree
Hide file tree
Showing 32 changed files with 270 additions and 178 deletions.
2 changes: 1 addition & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ ExtensionFunction::ResponseAction
RewardsServiceFactory::GetForProfile(profile);
if (rewards_service) {
rewards_service->SetContributionAutoInclude(
params->publisher_key, params->excluded, params->window_id);
params->publisher_key, params->excluded);
}
return RespondNow(NoArguments());
}
Expand Down
6 changes: 4 additions & 2 deletions browser/ui/webui/brave_rewards_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
unsigned int result,
brave_rewards::Grant grant) override;
void OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service,
std::string publisher_id) override;
std::string publisher_id,
bool excluded) override;
void OnReconcileComplete(brave_rewards::RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
Expand Down Expand Up @@ -621,7 +622,8 @@ void RewardsDOMHandler::OnGetNumExcludedSites(const std::string& publisher_id,

void RewardsDOMHandler::OnExcludedSitesChanged(
brave_rewards::RewardsService* rewards_service,
std::string publisher_id) {
std::string publisher_id,
bool excluded) {
if (rewards_service_)
rewards_service_->GetNumExcludedSites(base::Bind(
&RewardsDOMHandler::OnGetNumExcludedSites,
Expand Down
25 changes: 21 additions & 4 deletions common/extensions/api/brave_rewards.json
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,27 @@
}
}
]
},
{
"name": "onExcludedSitesChanged",
"type": "function",
"description": "Fired when grant process is finished",
"parameters": [
{
"name": "properties",
"type": "object",
"properties": {
"publisher_key": {
"type": "string",
"description": "publisher key, unique identifier"
},
"excluded": {
"type": "boolean",
"description": "indicate if publisher is excluded or not"
}
}
}
]
}
],
"functions": [
Expand Down Expand Up @@ -337,10 +358,6 @@
{
"name": "excluded",
"type": "boolean"
},
{
"name": "window_id",
"type": "integer"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,27 @@ void ExtensionRewardsServiceObserver::OnPublisherListNormalized(
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnExcludedSitesChanged(
RewardsService* rewards_service,
std::string publisher_key,
bool excluded) {
auto* event_router = extensions::EventRouter::Get(profile_);
if (!event_router) {
return;
}

extensions::api::brave_rewards::OnExcludedSitesChanged::Properties result;
result.publisher_key = publisher_key;
result.excluded = excluded;

std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnExcludedSitesChanged::Create(result)
.release());
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnExcludedSitesChanged::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

} // namespace brave_rewards
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_
#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_H_

#include <memory>
#include <string>

#include "base/macros.h"
#include "brave/components/brave_rewards/browser/content_site.h"
Expand All @@ -21,7 +23,7 @@ class RewardsService;
class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
public RewardsServicePrivateObserver {
public:
ExtensionRewardsServiceObserver(Profile* profile);
explicit ExtensionRewardsServiceObserver(Profile* profile);
~ExtensionRewardsServiceObserver() override;

// RewardsServiceObserver implementation
Expand All @@ -34,6 +36,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
void OnPublisherListNormalized(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) override;
void OnExcludedSitesChanged(RewardsService* rewards_service,
std::string publisher_key,
bool excluded) override;

// RewardsServicePrivateObserver implementation
void OnGetCurrentBalanceReport(RewardsService* rewards_service,
Expand Down Expand Up @@ -66,4 +71,4 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,

} // namespace brave_rewards

#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_
#endif // BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_EXTENSION_REWARDS_SERVICE_OBSERVER_H_
2 changes: 1 addition & 1 deletion components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ class RewardsService : public KeyedService {
virtual void UpdateRecurringDonationsList() = 0;
virtual void UpdateTipsList() = 0;
virtual void SetContributionAutoInclude(
const std::string& publisher_key, bool excluded, uint64_t windowId) = 0;
const std::string& publisher_key, bool excluded) = 0;
virtual RewardsNotificationService* GetNotificationService() const = 0;
virtual bool CheckImported() = 0;
virtual void SetBackupCompleted() = 0;
Expand Down
21 changes: 14 additions & 7 deletions components/brave_rewards/browser/rewards_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -709,7 +709,7 @@ void RewardsServiceImpl::ExcludePublisher(
return;

bat_ledger_->SetPublisherExclude(publisherKey,
ledger::PUBLISHER_EXCLUDE::EXCLUDED);
ledger::PUBLISHER_EXCLUDE::EXCLUDED);
}

void RewardsServiceImpl::RestorePublishers() {
Expand Down Expand Up @@ -1871,9 +1871,12 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl(
}

void RewardsServiceImpl::OnExcludedSitesChanged(
const std::string& publisher_id) {
const std::string& publisher_id,
ledger::PUBLISHER_EXCLUDE exclude) {

bool excluded = exclude == ledger::PUBLISHER_EXCLUDE::EXCLUDED;
for (auto& observer : observers_)
observer.OnExcludedSitesChanged(this, publisher_id);
observer.OnExcludedSitesChanged(this, publisher_id, excluded);
}

void RewardsServiceImpl::OnPanelPublisherInfo(
Expand Down Expand Up @@ -2277,13 +2280,17 @@ void RewardsServiceImpl::TriggerOnGetCurrentBalanceReport(
}

void RewardsServiceImpl::SetContributionAutoInclude(
const std::string& publisher_key, bool excluded, uint64_t windowId) {
const std::string& publisher_key,
bool excluded) {
if (!Connected())
return;

bat_ledger_->SetPublisherPanelExclude(publisher_key, excluded ?
ledger::PUBLISHER_EXCLUDE::EXCLUDED : ledger::PUBLISHER_EXCLUDE::INCLUDED,
windowId);
ledger::PUBLISHER_EXCLUDE exclude =
excluded
? ledger::PUBLISHER_EXCLUDE::EXCLUDED
: ledger::PUBLISHER_EXCLUDE::INCLUDED;

bat_ledger_->SetPublisherExclude(publisher_key, exclude);
}

RewardsNotificationService* RewardsServiceImpl::GetNotificationService() const {
Expand Down
7 changes: 4 additions & 3 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <map>
#include <memory>
#include <string>
#include <utility>
#include <vector>

#include "bat/ledger/ledger.h"
Expand Down Expand Up @@ -163,8 +164,7 @@ class RewardsServiceImpl : public RewardsService,
void UpdateTipsList() override;
void SetContributionAutoInclude(
const std::string& publisher_key,
bool excluded,
uint64_t window_id) override;
bool excluded) override;
RewardsNotificationService* GetNotificationService() const override;
bool CheckImported() override;
void SetBackupCompleted() override;
Expand Down Expand Up @@ -333,7 +333,8 @@ class RewardsServiceImpl : public RewardsService,
brave_rewards::ConfirmationsHistoryCallback callback) override;
void ConfirmationsTransactionHistoryDidChange() override;

void OnExcludedSitesChanged(const std::string& publisher_id) override;
void OnExcludedSitesChanged(const std::string& publisher_id,
ledger::PUBLISHER_EXCLUDE exclude) override;
void OnPanelPublisherInfo(ledger::Result result,
std::unique_ptr<ledger::PublisherInfo> info,
uint64_t window_id) override;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
/* 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/. */

Expand Down Expand Up @@ -39,7 +40,9 @@ class MockRewardsServiceObserver : public RewardsServiceObserver {
MOCK_METHOD3(OnGrantFinish,
void(RewardsService*, unsigned int, brave_rewards::Grant));
MOCK_METHOD1(OnContentSiteUpdated, void(RewardsService*));
MOCK_METHOD2(OnExcludedSitesChanged, void(RewardsService*, std::string));
MOCK_METHOD3(OnExcludedSitesChanged, void(RewardsService*,
std::string,
bool));
MOCK_METHOD5(OnReconcileComplete, void(RewardsService*,
unsigned int,
const std::string&,
Expand Down
4 changes: 3 additions & 1 deletion components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_REWARDS_SERVICE_OBSERVER_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_REWARDS_SERVICE_OBSERVER_H_

#include <memory>
#include <string>
#include <vector>

Expand Down Expand Up @@ -52,7 +53,8 @@ class RewardsServiceObserver : public base::CheckedObserver {
RewardsService* rewards_service) {}
virtual void OnExcludedSitesChanged(
RewardsService* rewards_service,
std::string publisher_id) {}
std::string publisher_id,
bool excluded) {}
virtual void OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,9 @@ export const deleteNotification = (id: string) => action(types.DELETE_NOTIFICATI
id
})

export const includeInAutoContribution = (publisherKey: string, excluded: boolean, windowId: number) => action(types.INCLUDE_IN_AUTO_CONTRIBUTION, {
export const includeInAutoContribution = (publisherKey: string, excluded: boolean) => action(types.INCLUDE_IN_AUTO_CONTRIBUTION, {
publisherKey,
excluded,
windowId
excluded
})

export const getGrants = () => action(types.GET_GRANTS)
Expand Down Expand Up @@ -106,3 +105,8 @@ export const onPublisherListNormalized = (properties: RewardsExtension.Publisher
action(types.ON_PUBLISHER_LIST_NORMALIZED, {
properties
})

export const onExcludedSitesChanged = (properties: RewardsExtension.ExcludedSitesChanged) =>
action(types.ON_EXCLUDED_SITES_CHANGED, {
properties
})
Original file line number Diff line number Diff line change
Expand Up @@ -59,3 +59,7 @@ chrome.braveRewards.onWalletFailed.addListener(() => {
chrome.braveRewards.onPublisherListNormalized.addListener((properties: RewardsExtension.PublisherNormalized[]) => {
rewardsPanelActions.onPublisherListNormalized(properties)
})

chrome.braveRewards.onExcludedSitesChanged.addListener((properties: RewardsExtension.ExcludedSitesChanged) => {
rewardsPanelActions.onExcludedSitesChanged(properties)
})
Original file line number Diff line number Diff line change
Expand Up @@ -214,9 +214,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
case types.INCLUDE_IN_AUTO_CONTRIBUTION: {
let publisherKey = payload.publisherKey
let excluded = payload.excluded
let windowId = payload.windowId
chrome.braveRewards.includeInAutoContribution(publisherKey, excluded,
windowId)
chrome.braveRewards.includeInAutoContribution(publisherKey, excluded)
break
}
case types.ON_PENDING_CONTRIBUTIONS_TOTAL: {
Expand Down Expand Up @@ -254,11 +252,41 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a
if (updated) {
publisher.verified = updated.verified
publisher.percentage = updated.percentage
publisher.excluded = false
} else {
publisher.percentage = 0
}
}

state = {
...state,
publishers
}
break
}
case types.ON_EXCLUDED_SITES_CHANGED: {
if (!payload.properties) {
break
}

const publisherKey: string = payload.properties.publisher_key

if (!publisherKey) {
break
}

const excluded: boolean = payload.properties.excluded

let publishers: Record<string, RewardsExtension.Publisher> = state.publishers

for (const key in publishers) {
let publisher = publishers[key]

if (publisher.publisher_key === publisherKey) {
publisher.excluded = !!excluded
}
}

state = {
...state,
publishers
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class Panel extends React.Component<Props, State> {
const publisherKey = publisher && publisher.publisher_key
const excluded = publisher && publisher.excluded
if (publisherKey && publisherKey.length > 0 && excluded !== undefined) {
this.props.actions.includeInAutoContribution(publisherKey, !excluded, this.props.windowId)
this.props.actions.includeInAutoContribution(publisherKey, !excluded)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ export const enum types {
ON_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/ON_PENDING_CONTRIBUTIONS_TOTAL',
ON_ENABLED_MAIN = '@@rewards_panel/ON_ENABLED_MAIN',
ON_ENABLED_AC = '@@rewards_panel/ON_ENABLED_AC',
ON_PUBLISHER_LIST_NORMALIZED = '@@rewards_panel/ON_PUBLISHER_LIST_NORMALIZED'
ON_PUBLISHER_LIST_NORMALIZED = '@@rewards_panel/ON_PUBLISHER_LIST_NORMALIZED',
ON_EXCLUDED_SITES_CHANGED = '@@rewards_panel/ON_EXCLUDED_SITES_CHANGED'
}

// Note: This declaration must match the RewardsNotificationType enum in
Expand Down
5 changes: 4 additions & 1 deletion components/definitions/chromel.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ declare namespace chrome.braveRewards {
const onGrantCaptcha: {
addListener: (callback: (properties: RewardsExtension.Captcha) => void) => void
}
const includeInAutoContribution: (publisherKey: string, excluded: boolean, windowId: number) => {}
const includeInAutoContribution: (publisherKey: string, excluded: boolean) => {}
const getGrants: () => {}
const getGrantCaptcha: () => {}
const solveGrantCaptcha: (solution: string, promotionId: string) => {}
Expand All @@ -60,6 +60,9 @@ declare namespace chrome.braveRewards {
const onPublisherListNormalized: {
addListener: (callback: (properties: RewardsExtension.PublisherNormalized[]) => void) => void
}
const onExcludedSitesChanged: {
addListener: (callback: (properties: RewardsExtension.ExcludedSitesChanged) => void) => void
}
}

declare namespace chrome.rewardsNotifications {
Expand Down
5 changes: 5 additions & 0 deletions components/definitions/rewardsExtensions.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,9 @@ declare namespace RewardsExtension {
percentage: number
verified: boolean
}

interface ExcludedSitesChanged {
publisher_key: string
excluded: boolean
}
}
Loading

0 comments on commit 027fa5c

Please sign in to comment.