From 84ba860f7dad92fa30e4d0ed5835bfac8b64307e Mon Sep 17 00:00:00 2001 From: NejcZdovc Date: Fri, 11 Jan 2019 19:35:13 +0100 Subject: [PATCH] Adds update function for pending totals Resolves https://github.com/brave/brave-browser/issues/2890 --- browser/ui/webui/brave_rewards_ui.cc | 13 +++++++++++++ common/extensions/api/brave_rewards.json | 11 +++++++++++ .../extension_rewards_service_observer.cc | 18 ++++++++++++++++++ .../extension_rewards_service_observer.h | 3 +++ .../browser/rewards_service_impl.cc | 13 ++++++++----- .../browser/rewards_service_impl.h | 2 +- .../browser/rewards_service_observer.h | 3 +++ .../background/events/rewardsEvents.ts | 8 ++++++++ .../constants/rewards_panel_types.ts | 1 - .../resources/ui/brave_rewards.tsx | 9 ++++++++- components/definitions/chromel.d.ts | 3 +++ 11 files changed, 76 insertions(+), 8 deletions(-) diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc index af9eaf4344a0..d04cda9028e8 100644 --- a/browser/ui/webui/brave_rewards_ui.cc +++ b/browser/ui/webui/brave_rewards_ui.cc @@ -129,6 +129,10 @@ class RewardsDOMHandler : public WebUIMessageHandler, void OnCurrentTips(brave_rewards::RewardsService* rewards_service, brave_rewards::ContentSiteList) override; + void OnPendingContributionSaved( + brave_rewards::RewardsService* rewards_service, + int result) override; + // RewardsNotificationsServiceObserver implementation void OnNotificationAdded( brave_rewards::RewardsNotificationService* rewards_notification_service, @@ -864,6 +868,15 @@ 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)); + } +} + } // namespace BraveRewardsUI::BraveRewardsUI(content::WebUI* web_ui, const std::string& name) diff --git a/common/extensions/api/brave_rewards.json b/common/extensions/api/brave_rewards.json index 1a52d128ab2e..e95e6f737356 100644 --- a/common/extensions/api/brave_rewards.json +++ b/common/extensions/api/brave_rewards.json @@ -221,6 +221,17 @@ "type": "boolean" } ] + }, + { + "name": "onPendingContributionSaved", + "type": "function", + "description": "", + "parameters": [ + { + "name": "result", + "type": "integer" + } + ] } ], "functions": [ diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.cc b/components/brave_rewards/browser/extension_rewards_service_observer.cc index d5cd4e030eb9..804ce13443fc 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.cc +++ b/components/brave_rewards/browser/extension_rewards_service_observer.cc @@ -231,4 +231,22 @@ void ExtensionRewardsServiceObserver::OnRewardsMainEnabled( event_router->BroadcastEvent(std::move(event)); } +void ExtensionRewardsServiceObserver::OnPendingContributionSaved( + RewardsService* rewards_service, + int result) { + extensions::EventRouter* event_router = extensions::EventRouter::Get(profile_); + if (!event_router) { + return; + } + + std::unique_ptr args( + extensions::api::brave_rewards::OnPendingContributionSaved::Create(result) + .release()); + std::unique_ptr event(new extensions::Event( + extensions::events::BRAVE_START, + extensions::api::brave_rewards::OnPendingContributionSaved::kEventName, + std::move(args))); + event_router->BroadcastEvent(std::move(event)); +} + } // namespace brave_rewards diff --git a/components/brave_rewards/browser/extension_rewards_service_observer.h b/components/brave_rewards/browser/extension_rewards_service_observer.h index 41014589bd83..53381eaabda7 100644 --- a/components/brave_rewards/browser/extension_rewards_service_observer.h +++ b/components/brave_rewards/browser/extension_rewards_service_observer.h @@ -51,6 +51,9 @@ class ExtensionRewardsServiceObserver : public RewardsServiceObserver, void OnRewardsMainEnabled(RewardsService* rewards_service, bool rewards_main_enabled) override; + void OnPendingContributionSaved(RewardsService* rewards_service, + int result) override; + private: Profile* profile_; diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 93f2db6c21c2..593ab15f1439 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -2191,17 +2191,20 @@ void RewardsServiceImpl::SetShortRetries(bool short_retries) { bat_ledger_service_->SetShortRetries(short_retries); } -bool SavePendingContributionOnFileTaskRunner(PublisherInfoDatabase* backend, +ledger::Result SavePendingContributionOnFileTaskRunner(PublisherInfoDatabase* backend, const ledger::PendingContributionList& list) { if (!backend) { - return false; + return ledger::Result::LEDGER_ERROR; } - return backend->InsertPendingContribution(list); + bool result = backend->InsertPendingContribution(list); + + return result ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR; } -void RewardsServiceImpl::OnSavePendingContribution(bool result) { - // TODO(nejczdovc) add callback with db result +void RewardsServiceImpl::OnSavePendingContribution(ledger::Result result) { + for (auto& observer : observers_) + observer.OnPendingContributionSaved(this, result); } void RewardsServiceImpl::SavePendingContribution( diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index f5e99bfd3be9..b2aca5521e4d 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -313,7 +313,7 @@ class RewardsServiceImpl : public RewardsService, void SavePendingContribution( const ledger::PendingContributionList& list) override; - void OnSavePendingContribution(bool result); + void OnSavePendingContribution(ledger::Result result); // URLFetcherDelegate impl void OnURLFetchComplete(const net::URLFetcher* source) override; diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h index f95845de0907..37e4d993feca 100644 --- a/components/brave_rewards/browser/rewards_service_observer.h +++ b/components/brave_rewards/browser/rewards_service_observer.h @@ -55,6 +55,9 @@ class RewardsServiceObserver : public base::CheckedObserver { 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) {}; // 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. diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts b/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts index 6f654cefe04c..e016ed7a4c43 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/events/rewardsEvents.ts @@ -43,3 +43,11 @@ chrome.rewardsNotifications.onNotificationDeleted.addListener((id: string, type: chrome.braveRewards.onEnabledMain.addListener((enabledMain: boolean) => { rewardsPanelActions.onEnabledMain(enabledMain) }) + +chrome.braveRewards.onPendingContributionSaved.addListener((result: number) => { + if (result === 0) { + chrome.braveRewards.getPendingContributionsTotal(((amount: number) => { + rewardsPanelActions.OnPendingContributionsTotal(amount) + })) + } +}) diff --git a/components/brave_rewards/resources/extension/brave_rewards/constants/rewards_panel_types.ts b/components/brave_rewards/resources/extension/brave_rewards/constants/rewards_panel_types.ts index 9eed785856b1..c802cd520d3c 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/constants/rewards_panel_types.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/constants/rewards_panel_types.ts @@ -25,7 +25,6 @@ export const enum types { ON_GRANT_RESET = '@@rewards_panel/ON_GRANT_RESET', ON_GRANT_DELETE = '@@rewards_panel/ON_GRANT_DELETE', ON_GRANT_FINISH = '@@rewards_panel/ON_GRANT_FINISH', - GET_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/GET_PENDING_CONTRIBUTIONS_TOTAL', ON_PENDING_CONTRIBUTIONS_TOTAL = '@@rewards_panel/ON_PENDING_CONTRIBUTIONS_TOTAL', ON_ENABLED_MAIN = '@@rewards_panel/ON_ENABLED_MAIN' } diff --git a/components/brave_rewards/resources/ui/brave_rewards.tsx b/components/brave_rewards/resources/ui/brave_rewards.tsx index 4f012e972911..d102bca0d935 100644 --- a/components/brave_rewards/resources/ui/brave_rewards.tsx +++ b/components/brave_rewards/resources/ui/brave_rewards.tsx @@ -133,6 +133,12 @@ window.cr.define('brave_rewards', function () { getActions().onPendingContributionTotal(amount) } + function onPendingContributionSaved (result: number) { + if (result === 0) { + getActions().getPendingContributionsTotal() + } + } + return { initialize, walletCreated, @@ -155,7 +161,8 @@ window.cr.define('brave_rewards', function () { initAutoContributeSettings, imported, adsData, - pendingContributionTotal + pendingContributionTotal, + onPendingContributionSaved } }) diff --git a/components/definitions/chromel.d.ts b/components/definitions/chromel.d.ts index 07bca432e885..5fad30509bd9 100644 --- a/components/definitions/chromel.d.ts +++ b/components/definitions/chromel.d.ts @@ -50,6 +50,9 @@ declare namespace chrome.braveRewards { } const getRewardsMainEnabled: (callback: (enabled: boolean) => void) => {} const saveAdsSetting: (key: string, value: string) => {} + const onPendingContributionSaved: { + addListener: (callback: (result: number) => void) => void + } } declare namespace chrome.rewardsNotifications {