Skip to content

Commit

Permalink
Merge pull request #1817 from brave/include-switch-61
Browse files Browse the repository at this point in the history
Fixes include/excluded update in the panel
  • Loading branch information
NejcZdovc authored Mar 5, 2019
2 parents edac160 + e92f13e commit 214076b
Show file tree
Hide file tree
Showing 31 changed files with 303 additions and 201 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 @@ -88,7 +88,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 @@ -120,7 +120,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 @@ -595,7 +596,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 @@ -238,6 +238,27 @@
"type": "function",
"description": "Fired when wallet creation failed",
"parameters": []
},
{
"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 @@ -309,10 +330,6 @@
{
"name": "excluded",
"type": "boolean"
},
{
"name": "window_id",
"type": "integer"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -264,4 +264,27 @@ void ExtensionRewardsServiceObserver::OnPendingContributionSaved(
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/rewards_service_observer.h"
Expand All @@ -20,7 +22,7 @@ class RewardsService;
class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
public RewardsServicePrivateObserver {
public:
ExtensionRewardsServiceObserver(Profile* profile);
explicit ExtensionRewardsServiceObserver(Profile* profile);
~ExtensionRewardsServiceObserver() override;

// RewardsServiceObserver implementation
Expand All @@ -31,6 +33,10 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver,
std::unique_ptr<brave_rewards::WalletProperties>
wallet_properties) override;

void OnExcludedSitesChanged(RewardsService* rewards_service,
std::string publisher_key,
bool excluded) override;

// RewardsServicePrivateObserver implementation
void OnGetCurrentBalanceReport(RewardsService* rewards_service,
const BalanceReport& balance_report) override;
Expand Down Expand Up @@ -62,4 +68,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 @@ -145,7 +145,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 @@ -671,7 +671,7 @@ void RewardsServiceImpl::ExcludePublisher(
return;

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

void RewardsServiceImpl::RestorePublishers() {
Expand Down Expand Up @@ -1669,9 +1669,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 @@ -2078,13 +2081,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: 5 additions & 2 deletions components/brave_rewards/browser/rewards_service_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include <map>
#include <memory>
#include <string>
#include <utility>

#include "bat/ledger/ledger.h"
#include "bat/ledger/wallet_info.h"
Expand Down Expand Up @@ -156,7 +157,8 @@ class RewardsServiceImpl : public RewardsService,
void UpdateRecurringDonationsList() override;
void UpdateTipsList() override;
void SetContributionAutoInclude(
const std::string& publisher_key, bool excluded, uint64_t windowId) override;
const std::string& publisher_key,
bool excluded) override;
RewardsNotificationService* GetNotificationService() const override;
bool CheckImported() override;
void SetBackupCompleted() override;
Expand Down Expand Up @@ -301,7 +303,8 @@ class RewardsServiceImpl : public RewardsService,
void SetContributionAmount(double amount) const override;
void SetUserChangedContribution() const override;
void SetAutoContribute(bool enabled) const 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 windowId) 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 @@ -38,7 +39,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
89 changes: 53 additions & 36 deletions components/brave_rewards/browser/rewards_service_observer.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,12 @@
* 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_PAYMENTS_SERVICE_OBSERVER_H_
#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_OBSERVER_H_
#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>

#include "base/observer_list_types.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
Expand All @@ -20,47 +24,60 @@ class RewardsServiceObserver : public base::CheckedObserver {
public:
~RewardsServiceObserver() override {}

virtual void OnWalletInitialized(RewardsService* rewards_service,
int error_code) {};
virtual void OnWalletInitialized(
RewardsService* rewards_service,
int error_code) {}
virtual void OnWalletProperties(
RewardsService* rewards_service,
int error_code,
std::unique_ptr<brave_rewards::WalletProperties> properties) {};
virtual void OnGrant(RewardsService* rewards_service,
unsigned int error_code,
brave_rewards::Grant properties) {};
virtual void OnGrantCaptcha(RewardsService* rewards_service,
std::string image,
std::string hint) {};
virtual void OnRecoverWallet(RewardsService* rewards_service,
unsigned int result,
double balance,
std::vector<brave_rewards::Grant> grants) {};
virtual void OnGrantFinish(RewardsService* rewards_service,
unsigned int result,
brave_rewards::Grant grant) {};
virtual void OnContentSiteUpdated(RewardsService* rewards_service) {};
virtual void OnExcludedSitesChanged(RewardsService* rewards_service,
std::string publisher_id) {};
virtual void OnReconcileComplete(RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& category,
const std::string& probi) {};
virtual void OnRecurringDonationUpdated(RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
virtual void OnCurrentTips(RewardsService* rewards_service,
brave_rewards::ContentSiteList) {};
virtual void OnPublisherBanner(brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {};
virtual void OnRewardsMainEnabled(brave_rewards::RewardsService* rewards_service,
bool rewards_main_enabled) {};
std::unique_ptr<brave_rewards::WalletProperties> properties) {}
virtual void OnGrant(
RewardsService* rewards_service,
unsigned int error_code,
brave_rewards::Grant properties) {}
virtual void OnGrantCaptcha(
RewardsService* rewards_service,
std::string image,
std::string hint) {}
virtual void OnRecoverWallet(
RewardsService* rewards_service,
unsigned int result,
double balance,
std::vector<brave_rewards::Grant> grants) {}
virtual void OnGrantFinish(
RewardsService* rewards_service,
unsigned int result,
brave_rewards::Grant grant) {}
virtual void OnContentSiteUpdated(
RewardsService* rewards_service) {}
virtual void OnExcludedSitesChanged(
RewardsService* rewards_service,
std::string publisher_id,
bool excluded) {}
virtual void OnReconcileComplete(
RewardsService* rewards_service,
unsigned int result,
const std::string& viewing_id,
const std::string& category,
const std::string& probi) {}
virtual void OnRecurringDonationUpdated(
RewardsService* rewards_service,
brave_rewards::ContentSiteList) {}
virtual void OnCurrentTips(
RewardsService* rewards_service,
brave_rewards::ContentSiteList) {}
virtual void OnPublisherBanner(
brave_rewards::RewardsService* rewards_service,
const brave_rewards::PublisherBanner banner) {}
virtual void OnRewardsMainEnabled(
brave_rewards::RewardsService* rewards_service,
bool rewards_main_enabled) {}
virtual void OnPendingContributionSaved(
brave_rewards::RewardsService* rewards_service,
int result) {};
int result) {}
virtual void OnPublisherListNormalized(
RewardsService* rewards_service,
brave_rewards::ContentSiteList list) {};
brave_rewards::ContentSiteList list) {}
// DO NOT ADD ANY MORE METHODS HERE UNLESS IT IS A BROADCAST NOTIFICATION
// RewardsServiceObserver should not be used to return responses to the caller.
// Method calls on RewardsService should use callbacks to return responses.
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 getGrant = () => action(types.GET_GRANT, {})
Expand Down Expand Up @@ -99,3 +98,8 @@ export const onEnabledMain = (enabledMain: boolean) => action(types.ON_ENABLED_M
export const onEnabledAC = (enabled: boolean) => action(types.ON_ENABLED_AC, {
enabled
})

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 @@ -55,3 +55,7 @@ chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => {
chrome.braveRewards.onWalletFailed.addListener(() => {
rewardsPanelActions.onWalletCreateFailed()
})

chrome.braveRewards.onExcludedSitesChanged.addListener((properties: RewardsExtension.ExcludedSitesChanged) => {
rewardsPanelActions.onExcludedSitesChanged(properties)
})
Loading

0 comments on commit 214076b

Please sign in to comment.