diff --git a/DEPS b/DEPS
index 16da45f2d170..4dcd1ac99ac9 100644
--- a/DEPS
+++ b/DEPS
@@ -13,7 +13,7 @@ deps = {
"vendor/sparkle": "https://github.com/brave/Sparkle.git@c0759cce415d7c0feae45005c8a013b1898711f0",
"vendor/bat-native-rapidjson": "https://github.com/brave-intl/bat-native-rapidjson.git@86aafe2ef89835ae71c9ed7c2527e3bb3000930e",
"vendor/bip39wally-core-native": "https://github.com/brave-intl/bip39wally-core-native.git@9b119931c702d55be994117eb505d56310720b1d",
- "vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@adeff3254bb90ccdc9699040d5a4e1cd6b8393b7",
+ "vendor/bat-native-anonize": "https://github.com/brave-intl/bat-native-anonize.git@19fb43fd58b852fd2c6e4d4c68daa99c835f5182",
"vendor/bat-native-tweetnacl": "https://github.com/brave-intl/bat-native-tweetnacl.git@1b4362968c8f22720bfb75af6f506d4ecc0f3116",
"components/brave_sync/extension/brave-sync": "https://github.com/brave/sync.git@249a663a6f163f8fe5d2e96f9605760fb7627da0",
"components/brave_sync/extension/brave-crypto": "https://github.com/brave/crypto@7e391cec6975106fa9f686016f494cb8a782afcd",
diff --git a/app/brave_generated_resources.grd b/app/brave_generated_resources.grd
index b054a6bed697..6d6c4f26ac45 100644
--- a/app/brave_generated_resources.grd
+++ b/app/brave_generated_resources.grd
@@ -258,6 +258,9 @@ By installing this extension, you are agreeing to the Google Widevine Terms of U
Bat Ads Service
+
+ Bat Ledger Service
+
Brave shields defaults
diff --git a/browser/BUILD.gn b/browser/BUILD.gn
index 7eea62ea21fe..d302c39321f4 100644
--- a/browser/BUILD.gn
+++ b/browser/BUILD.gn
@@ -116,6 +116,7 @@ source_set("browser") {
"//base",
"//brave/components/brave_ads/browser",
"//brave/components/brave_referrals/browser",
+ "//brave/components/brave_rewards/browser",
"//brave/components/brave_shields/browser:brave_shields",
"//brave/components/brave_webtorrent/browser",
"//brave/components/resources",
diff --git a/browser/brave_content_browser_client.cc b/browser/brave_content_browser_client.cc
index 3ecc7bf62f87..9559709120aa 100644
--- a/browser/brave_content_browser_client.cc
+++ b/browser/brave_content_browser_client.cc
@@ -17,6 +17,7 @@
#include "brave/common/tor/tor_launcher.mojom.h"
#include "brave/common/tor/switches.h"
#include "brave/components/brave_ads/browser/buildflags/buildflags.h"
+#include "brave/components/brave_rewards/browser/buildflags/buildflags.h"
#include "brave/components/brave_shields/browser/brave_shields_util.h"
#include "brave/components/brave_shields/browser/brave_shields_web_contents_observer.h"
#include "brave/components/brave_shields/common/brave_shield_constants.h"
@@ -49,6 +50,10 @@ using brave_shields::BraveShieldsWebContentsObserver;
#include "brave/components/services/bat_ads/public/interfaces/bat_ads.mojom.h"
#endif
+#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
+#include "brave/components/services/bat_ledger/public/interfaces/bat_ledger.mojom.h"
+#endif
+
#if BUILDFLAG(ENABLE_EXTENSIONS)
using extensions::ChromeContentBrowserClientExtensionsPart;
#endif
@@ -194,6 +199,10 @@ void BraveContentBrowserClient::RegisterOutOfProcessServices(
(*services)[bat_ads::mojom::kServiceName] = base::BindRepeating(
l10n_util::GetStringUTF16, IDS_SERVICE_BAT_ADS);
#endif
+#if BUILDFLAG(BRAVE_REWARDS_ENABLED)
+ (*services)[bat_ledger::mojom::kServiceName] = base::BindRepeating(
+ l10n_util::GetStringUTF16, IDS_UTILITY_PROCESS_LEDGER_NAME);
+#endif
}
std::unique_ptr
diff --git a/browser/extensions/BUILD.gn b/browser/extensions/BUILD.gn
index fbccfcbb3694..35b51bf7e264 100644
--- a/browser/extensions/BUILD.gn
+++ b/browser/extensions/BUILD.gn
@@ -39,6 +39,7 @@ source_set("extensions") {
]
deps = [
+ "//base",
"//brave/browser/resources:brave_extension_grit",
"//brave/common/extensions/api",
"//brave/components/brave_rewards/resources/extension:extension_generated_resources",
diff --git a/browser/extensions/api/brave_rewards_api.cc b/browser/extensions/api/brave_rewards_api.cc
index eb9ec9ff3467..97944ed53360 100644
--- a/browser/extensions/api/brave_rewards_api.cc
+++ b/browser/extensions/api/brave_rewards_api.cc
@@ -6,6 +6,7 @@
#include
+#include "base/bind.h"
#include "brave/browser/brave_rewards/donations_dialog.h"
#include "brave/common/extensions/api/brave_rewards.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
@@ -177,11 +178,20 @@ BraveRewardsGetNonVerifiedSettingsFunction::Run() {
RewardsServiceFactory::GetForProfile(profile);
bool non_verified = true;
- if (rewards_service_) {
- non_verified = rewards_service_->GetPublisherAllowNonVerified();
+ if (!rewards_service_) {
+ return RespondNow(OneArgument(
+ std::make_unique(non_verified)));
}
- return RespondNow(OneArgument(std::make_unique(non_verified)));
+ rewards_service_->GetPublisherAllowNonVerified(base::Bind(
+ &BraveRewardsGetNonVerifiedSettingsFunction::OnGetAllowNonVerified,
+ this));
+ return RespondLater();
+}
+
+void BraveRewardsGetNonVerifiedSettingsFunction::OnGetAllowNonVerified(
+ bool non_verified) {
+ Respond(OneArgument(std::make_unique(non_verified)));
}
} // namespace api
diff --git a/browser/extensions/api/brave_rewards_api.h b/browser/extensions/api/brave_rewards_api.h
index 35a973b0d560..395c59dbd284 100644
--- a/browser/extensions/api/brave_rewards_api.h
+++ b/browser/extensions/api/brave_rewards_api.h
@@ -109,6 +109,9 @@ class BraveRewardsGetNonVerifiedSettingsFunction : public UIThreadExtensionFunct
~BraveRewardsGetNonVerifiedSettingsFunction() override;
ResponseAction Run() override;
+
+ private:
+ void OnGetAllowNonVerified(bool non_verified);
};
} // namespace api
diff --git a/browser/importer/brave_profile_writer.cc b/browser/importer/brave_profile_writer.cc
index c5a4808645d6..1b71ce9f4123 100644
--- a/browser/importer/brave_profile_writer.cc
+++ b/browser/importer/brave_profile_writer.cc
@@ -300,9 +300,13 @@ void BraveProfileWriter::UpdateLedger(const BraveLedger& ledger) {
}
ledger_ = BraveLedger(ledger);
+ rewards_service_->IsWalletCreated(
+ base::Bind(&BraveProfileWriter::OnIsWalletCreated, AsWeakPtr()));
+}
+void BraveProfileWriter::OnIsWalletCreated(bool created) {
// If a wallet doesn't exist, we need to create one (needed for RecoverWallet)
- if (!rewards_service_->IsWalletCreated()) {
+ if (!created) {
rewards_service_->AddObserver(this);
LOG(INFO) << "Creating wallet to use for import...";
rewards_service_->CreateWallet();
diff --git a/browser/importer/brave_profile_writer.h b/browser/importer/brave_profile_writer.h
index 8717618df15d..4e13e7dcb6fa 100644
--- a/browser/importer/brave_profile_writer.h
+++ b/browser/importer/brave_profile_writer.h
@@ -8,6 +8,7 @@
#include
#include "base/macros.h"
+#include "base/memory/weak_ptr.h"
#include "chrome/browser/importer/profile_writer.h"
#include "net/cookies/canonical_cookie.h"
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
@@ -19,7 +20,8 @@ class BraveInProcessImporterBridge;
struct ImportedWindowState;
class BraveProfileWriter : public ProfileWriter,
- public brave_rewards::RewardsServiceObserver {
+ public brave_rewards::RewardsServiceObserver,
+ public base::SupportsWeakPtr{
public:
explicit BraveProfileWriter(Profile* profile);
@@ -32,6 +34,8 @@ class BraveProfileWriter : public ProfileWriter,
void SetBridge(BraveInProcessImporterBridge* bridge);
+ void OnIsWalletCreated(bool created);
+
// brave_rewards::RewardsServiceObserver:
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
int error_code) override;
diff --git a/browser/profiles/BUILD.gn b/browser/profiles/BUILD.gn
index 18486beff78a..50f1c3a73dd2 100644
--- a/browser/profiles/BUILD.gn
+++ b/browser/profiles/BUILD.gn
@@ -13,6 +13,7 @@ source_set("profiles") {
"//brave/common/tor",
"//brave/components/brave_ads/browser",
"//brave/components/brave_sync",
+ "//brave/components/brave_rewards/browser",
"//ui/base",
]
}
diff --git a/browser/profiles/brave_profile_manager.cc b/browser/profiles/brave_profile_manager.cc
index 5d09e455df94..871a3c581f53 100644
--- a/browser/profiles/brave_profile_manager.cc
+++ b/browser/profiles/brave_profile_manager.cc
@@ -12,6 +12,7 @@
#include "brave/common/tor/pref_names.h"
#include "brave/common/tor/tor_constants.h"
#include "brave/components/brave_ads/browser/ads_service_factory.h"
+#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/components/brave_sync/brave_sync_service_factory.h"
#include "chrome/browser/browser_process.h"
#include "chrome/common/chrome_constants.h"
@@ -98,6 +99,7 @@ void BraveProfileManager::DoFinalInitForServices(Profile* profile,
// it will only be constructed only when we open chrome:/sync/
brave_sync::BraveSyncServiceFactory::GetForProfile(profile);
brave_ads::AdsServiceFactory::GetForProfile(profile);
+ brave_rewards::RewardsServiceFactory::GetForProfile(profile);
}
void BraveProfileManager::LaunchTorProcess(Profile* profile) {
diff --git a/browser/ui/webui/brave_rewards_ui.cc b/browser/ui/webui/brave_rewards_ui.cc
index ea568a0839dc..360b3bf5cc20 100644
--- a/browser/ui/webui/brave_rewards_ui.cc
+++ b/browser/ui/webui/brave_rewards_ui.cc
@@ -68,6 +68,8 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void OnGetCurrentContributeList(
std::unique_ptr,
uint32_t record);
+ void OnGetAllBalanceReports(
+ const std::map& reports);
void GetBalanceReports(const base::ListValue* args);
void ExcludePublisher(const base::ListValue* args);
void RestorePublishers(const base::ListValue* args);
@@ -81,6 +83,18 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void GetAdsData(const base::ListValue* args);
void SaveAdsSetting(const base::ListValue* args);
void SetBackupCompleted(const base::ListValue* args);
+ void OnGetWalletPassphrase(const std::string& pass);
+ void OnGetContributionAmount(double amount);
+ void OnGetAddresses(const std::map& addresses);
+ void OnGetNumExcludedSites(const std::string& publisher_id, uint32_t num);
+ void OnGetAutoContributeProps(
+ int error_code,
+ std::unique_ptr wallet_properties,
+ std::unique_ptr auto_contri_props);
+ void OnGetReconcileStamp(uint64_t reconcile_stamp);
+ void OnAutoContributePropsReady(
+ std::unique_ptr auto_contri_props);
+ void OnIsWalletCreated(bool created);
// RewardsServiceObserver implementation
void OnWalletInitialized(brave_rewards::RewardsService* rewards_service,
@@ -230,9 +244,9 @@ void RewardsDOMHandler::Init() {
rewards_service_->AddObserver(this);
}
-void RewardsDOMHandler::GetAllBalanceReports() {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- std::map reports = rewards_service_->GetAllBalanceReports();
+void RewardsDOMHandler::OnGetAllBalanceReports(
+ const std::map& reports) {
+ if (web_ui()->CanCallJavascript()) {
base::DictionaryValue newReports;
if (!reports.empty()) {
for (auto const& report : reports) {
@@ -251,10 +265,18 @@ void RewardsDOMHandler::GetAllBalanceReports() {
}
}
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.balanceReports", newReports);
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.balanceReports",
+ newReports);
}
}
+void RewardsDOMHandler::GetAllBalanceReports() {
+ if (rewards_service_)
+ rewards_service_->GetAllBalanceReports(
+ base::Bind(&RewardsDOMHandler::OnGetAllBalanceReports,
+ weak_factory_.GetWeakPtr()));
+}
+
void RewardsDOMHandler::HandleCreateWalletRequested(const base::ListValue* args) {
if (!rewards_service_)
return;
@@ -281,22 +303,22 @@ void RewardsDOMHandler::OnWalletInitialized(
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletCreateFailed");
}
-void RewardsDOMHandler::OnWalletProperties(
- brave_rewards::RewardsService* rewards_service,
+void RewardsDOMHandler::OnGetAutoContributeProps(
int error_code,
- std::unique_ptr wallet_properties) {
+ std::unique_ptr wallet_properties,
+ std::unique_ptr auto_contri_props) {
if (web_ui()->CanCallJavascript()) {
base::DictionaryValue values;
values.SetBoolean("enabledContribute",
- rewards_service->GetAutoContribute());
+ auto_contri_props->enabled_contribute);
values.SetInteger("contributionMinTime",
- rewards_service->GetPublisherMinVisitTime());
+ auto_contri_props->contribution_min_time);
values.SetInteger("contributionMinVisits",
- rewards_service->GetPublisherMinVisits());
+ auto_contri_props->contribution_min_visits);
values.SetBoolean("contributionNonVerified",
- rewards_service->GetPublisherAllowNonVerified());
+ auto_contri_props->contribution_non_verified);
values.SetBoolean("contributionVideos",
- rewards_service->GetPublisherAllowVideos());
+ auto_contri_props->contribution_videos);
auto ui_values = std::make_unique();
@@ -351,6 +373,16 @@ void RewardsDOMHandler::OnWalletProperties(
}
}
+void RewardsDOMHandler::OnWalletProperties(
+ brave_rewards::RewardsService* rewards_service,
+ int error_code,
+ std::unique_ptr wallet_properties) {
+ rewards_service->GetAutoContributeProps(
+ base::Bind(&RewardsDOMHandler::OnGetAutoContributeProps,
+ weak_factory_.GetWeakPtr(), error_code,
+ base::Passed(std::move(wallet_properties))));
+}
+
void RewardsDOMHandler::OnGrant(
brave_rewards::RewardsService* rewards_service,
unsigned int result,
@@ -396,11 +428,18 @@ void RewardsDOMHandler::GetGrantCaptcha(const base::ListValue* args) {
}
}
-void RewardsDOMHandler::GetWalletPassphrase(const base::ListValue* args) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- std::string pass = rewards_service_->GetWalletPassphrase();
+void RewardsDOMHandler::OnGetWalletPassphrase(const std::string& pass) {
+ if (web_ui()->CanCallJavascript()) {
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletPassphrase",
+ base::Value(pass));
+ }
+}
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletPassphrase", base::Value(pass));
+void RewardsDOMHandler::GetWalletPassphrase(const base::ListValue* args) {
+ if (rewards_service_) {
+ rewards_service_->GetWalletPassphrase(
+ base::Bind(&RewardsDOMHandler::OnGetWalletPassphrase,
+ weak_factory_.GetWeakPtr()));
}
}
@@ -459,49 +498,78 @@ void RewardsDOMHandler::OnGrantFinish(
}
}
-void RewardsDOMHandler::GetReconcileStamp(const base::ListValue* args) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- std::string stamp = std::to_string(rewards_service_->GetReconcileStamp());
-
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.reconcileStamp", base::Value(stamp));
+void RewardsDOMHandler::OnGetReconcileStamp(uint64_t reconcile_stamp) {
+ if (web_ui()->CanCallJavascript()) {
+ std::string stamp = std::to_string(reconcile_stamp);
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.reconcileStamp",
+ base::Value(stamp));
}
}
-void RewardsDOMHandler::GetAddresses(const base::ListValue* args) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- std::map addresses = rewards_service_->GetAddresses();
+void RewardsDOMHandler::GetReconcileStamp(const base::ListValue* args) {
+ if (rewards_service_)
+ rewards_service_->GetReconcileStamp(base::Bind(
+ &RewardsDOMHandler::OnGetReconcileStamp,
+ weak_factory_.GetWeakPtr()));
+}
+void RewardsDOMHandler::OnGetAddresses(
+ const std::map& addresses) {
+ if (web_ui()->CanCallJavascript()) {
base::DictionaryValue data;
- data.SetString("BAT", addresses["BAT"]);
- data.SetString("BTC", addresses["BTC"]);
- data.SetString("ETH", addresses["ETH"]);
- data.SetString("LTC", addresses["LTC"]);
+ data.SetString("BAT", addresses.at("BAT"));
+ data.SetString("BTC", addresses.at("BTC"));
+ data.SetString("ETH", addresses.at("ETH"));
+ data.SetString("LTC", addresses.at("LTC"));
web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.addresses", data);
}
}
-void RewardsDOMHandler::OnContentSiteUpdated(brave_rewards::RewardsService* rewards_service) {
- rewards_service_->GetCurrentContributeList(
- 0,
- 0,
+void RewardsDOMHandler::GetAddresses(const base::ListValue* args) {
+ if (rewards_service_)
+ rewards_service_->GetAddresses(base::Bind(
+ &RewardsDOMHandler::OnGetAddresses, weak_factory_.GetWeakPtr()));
+}
+
+void RewardsDOMHandler::OnAutoContributePropsReady(
+ std::unique_ptr props) {
+ rewards_service_->GetCurrentContributeList(0, 0,
+ props->contribution_min_time, props->reconcile_stamp,
+ props->contribution_non_verified,
base::Bind(&RewardsDOMHandler::OnGetCurrentContributeList,
- weak_factory_.GetWeakPtr()));
+ weak_factory_.GetWeakPtr()));
}
-void RewardsDOMHandler::OnExcludedSitesChanged(brave_rewards::RewardsService* rewards_service,
- std::string publisher_id) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- base::DictionaryValue excludedSitesInfo;
- int num = rewards_service_->GetNumExcludedSites();
+void RewardsDOMHandler::OnContentSiteUpdated(
+ brave_rewards::RewardsService* rewards_service) {
+ rewards_service_->GetAutoContributeProps(
+ base::Bind(&RewardsDOMHandler::OnAutoContributePropsReady,
+ weak_factory_.GetWeakPtr()));
+}
+void RewardsDOMHandler::OnGetNumExcludedSites(const std::string& publisher_id,
+ uint32_t num) {
+ if (web_ui()->CanCallJavascript()) {
+ base::DictionaryValue excludedSitesInfo;
excludedSitesInfo.SetString("num", std::to_string(num));
excludedSitesInfo.SetString("publisherKey", publisher_id);
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.numExcludedSites", excludedSitesInfo);
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.numExcludedSites",
+ excludedSitesInfo);
}
}
+void RewardsDOMHandler::OnExcludedSitesChanged(
+ brave_rewards::RewardsService* rewards_service,
+ std::string publisher_id) {
+ if (rewards_service_)
+ rewards_service_->GetNumExcludedSites(base::Bind(
+ &RewardsDOMHandler::OnGetNumExcludedSites,
+ weak_factory_.GetWeakPtr(),
+ publisher_id));
+}
+
void RewardsDOMHandler::OnNotificationAdded(
brave_rewards::RewardsNotificationService* rewards_notification_service,
const brave_rewards::RewardsNotificationService::RewardsNotification&
@@ -610,20 +678,31 @@ void RewardsDOMHandler::GetBalanceReports(const base::ListValue* args) {
GetAllBalanceReports();
}
+void RewardsDOMHandler::OnIsWalletCreated(bool created) {
+ if (web_ui()->CanCallJavascript())
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletExists",
+ base::Value(created));
+}
+
void RewardsDOMHandler::WalletExists(const base::ListValue* args) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- bool exist = rewards_service_->IsWalletCreated();
+ if (rewards_service_)
+ rewards_service_->IsWalletCreated(
+ base::Bind(&RewardsDOMHandler::OnIsWalletCreated,
+ weak_factory_.GetWeakPtr()));
+}
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.walletExists", base::Value(exist));
+void RewardsDOMHandler::OnGetContributionAmount(double amount) {
+ if (web_ui()->CanCallJavascript()) {
+ web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.contributionAmount",
+ base::Value(amount));
}
}
void RewardsDOMHandler::GetContributionAmount(const base::ListValue* args) {
- if (rewards_service_ && web_ui()->CanCallJavascript()) {
- double amount = rewards_service_->GetContributionAmount();
-
- web_ui()->CallJavascriptFunctionUnsafe("brave_rewards.contributionAmount", base::Value(amount));
- }
+ if (rewards_service_)
+ rewards_service_->GetContributionAmount(
+ base::Bind(&RewardsDOMHandler::OnGetContributionAmount,
+ weak_factory_.GetWeakPtr()));
}
void RewardsDOMHandler::OnReconcileComplete(
diff --git a/components/brave_rewards/browser/BUILD.gn b/components/brave_rewards/browser/BUILD.gn
index 6a5eccd10f97..47ec10694f7c 100644
--- a/components/brave_rewards/browser/BUILD.gn
+++ b/components/brave_rewards/browser/BUILD.gn
@@ -9,6 +9,8 @@ source_set("browser") {
sources = [
"switches.cc",
"switches.h",
+ "auto_contribution_props.cc",
+ "auto_contribution_props.h",
"balance_report.cc",
"balance_report.h",
"rewards_notification_service.cc",
@@ -92,7 +94,10 @@ source_set("browser") {
deps += [
"//brave/vendor/bat-native-ledger",
+ "//brave/components/services/bat_ledger/public/cpp",
+ "//mojo/public/cpp/bindings",
"//net",
+ "//services/service_manager/public/cpp",
"//url",
]
}
diff --git a/components/brave_rewards/browser/auto_contribution_props.cc b/components/brave_rewards/browser/auto_contribution_props.cc
new file mode 100644
index 000000000000..252e85b23c45
--- /dev/null
+++ b/components/brave_rewards/browser/auto_contribution_props.cc
@@ -0,0 +1,20 @@
+/* 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/auto_contribution_props.h"
+
+namespace brave_rewards {
+
+AutoContributeProps::AutoContributeProps()
+ : enabled_contribute(false),
+ contribution_min_time(0),
+ contribution_min_visits(0),
+ contribution_non_verified(false),
+ contribution_videos(false),
+ reconcile_stamp(0) {
+}
+
+AutoContributeProps::~AutoContributeProps() { }
+
+} // namespace brave_rewards
diff --git a/components/brave_rewards/browser/auto_contribution_props.h b/components/brave_rewards/browser/auto_contribution_props.h
new file mode 100644
index 000000000000..270573c0d773
--- /dev/null
+++ b/components/brave_rewards/browser/auto_contribution_props.h
@@ -0,0 +1,27 @@
+/* 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_AUTO_CONTRIBUTION_PROPS_H_
+#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_AUTO_CONTRIBUTION_PROPS_H_
+
+#include
+
+namespace brave_rewards {
+
+struct AutoContributeProps {
+ AutoContributeProps();
+ ~AutoContributeProps();
+ AutoContributeProps(const AutoContributeProps& properties) = default;
+
+ bool enabled_contribute;
+ uint64_t contribution_min_time;
+ int32_t contribution_min_visits;
+ bool contribution_non_verified;
+ bool contribution_videos;
+ uint64_t reconcile_stamp;
+};
+
+} // namespace brave_rewards
+
+#endif //BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_AUTO_CONTRIBUTION_PROPS_H_
diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h
index 18cd98ea56ee..5b02519c9335 100644
--- a/components/brave_rewards/browser/rewards_service.h
+++ b/components/brave_rewards/browser/rewards_service.h
@@ -9,8 +9,9 @@
#include "base/macros.h"
#include "base/observer_list.h"
-#include "brave/components/brave_rewards/browser/content_site.h"
+#include "brave/components/brave_rewards/browser/auto_contribution_props.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
+#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"
#include "build/build_config.h"
#include "components/sessions/core/session_id.h"
@@ -22,7 +23,6 @@ class Profile;
namespace ledger {
struct PublisherInfo;
-class Ledger;
}
namespace content {
@@ -41,6 +41,22 @@ class RewardsServiceObserver;
using GetCurrentContributeListCallback =
base::Callback,
uint32_t /* next_record */)>;
+using GetAllBalanceReportsCallback = base::Callback&)>;
+using GetWalletPassphraseCallback = base::Callback;
+using GetContributionAmountCallback = base::Callback;
+using GetAddressesCallback = base::Callback&)>;
+using GetNumExcludedSitesCallback = base::Callback;
+using GetAutoContributePropsCallback = base::Callback)>;
+using GetPublisherMinVisitTimeCallback = base::Callback;
+using GetPublisherMinVisitsCallback = base::Callback;
+using GetPublisherAllowNonVerifiedCallback = base::Callback;
+using GetPublisherAllowVideosCallback = base::Callback;
+using GetAutoContributeCallback = base::Callback;
+using GetReconcileStampCallback = base::Callback;
+using IsWalletCreatedCallback = base::Callback;
class RewardsService : public KeyedService {
public:
@@ -52,12 +68,17 @@ class RewardsService : public KeyedService {
virtual void GetCurrentContributeList(
uint32_t start,
uint32_t limit,
+ uint64_t min_visit_time,
+ uint64_t reconcile_stamp,
+ bool allow_non_verified,
const GetCurrentContributeListCallback& callback) = 0;
virtual void FetchGrant(const std::string& lang, const std::string& paymentId) = 0;
virtual void GetGrantCaptcha() = 0;
virtual void SolveGrantCaptcha(const std::string& solution) const = 0;
- virtual std::string GetWalletPassphrase() const = 0;
- virtual unsigned int GetNumExcludedSites() const = 0;
+ virtual void GetWalletPassphrase(
+ const GetWalletPassphraseCallback& callback) = 0;
+ virtual void GetNumExcludedSites(
+ const GetNumExcludedSitesCallback& callback) = 0;
virtual void RecoverWallet(const std::string passPhrase) const = 0;
virtual void ExcludePublisher(const std::string publisherKey) const = 0;
virtual void RestorePublishers() = 0;
@@ -79,27 +100,35 @@ class RewardsService : public KeyedService {
const GURL& referrer,
const std::string& post_data) = 0;
- virtual uint64_t GetReconcileStamp() const = 0;
- virtual std::map GetAddresses() const = 0;
+ virtual void GetReconcileStamp(
+ const GetReconcileStampCallback& callback) = 0;
+ virtual void GetAddresses(const GetAddressesCallback& callback) = 0;
virtual void SetRewardsMainEnabled(bool enabled) const = 0;
- virtual uint64_t GetPublisherMinVisitTime() const = 0;
+ virtual void GetPublisherMinVisitTime(
+ const GetPublisherMinVisitTimeCallback& callback) = 0;
virtual void SetPublisherMinVisitTime(uint64_t duration_in_seconds) const = 0;
- virtual unsigned int GetPublisherMinVisits() const = 0;
+ virtual void GetPublisherMinVisits(
+ const GetPublisherMinVisitsCallback& callback) = 0;
virtual void SetPublisherMinVisits(unsigned int visits) const = 0;
- virtual bool GetPublisherAllowNonVerified() const = 0;
+ virtual void GetPublisherAllowNonVerified(
+ const GetPublisherAllowNonVerifiedCallback& callback) = 0;
virtual void SetPublisherAllowNonVerified(bool allow) const = 0;
- virtual bool GetPublisherAllowVideos() const = 0;
+ virtual void GetPublisherAllowVideos(
+ const GetPublisherAllowVideosCallback& callback) = 0;
virtual void SetPublisherAllowVideos(bool allow) const = 0;
virtual void SetContributionAmount(double amount) const = 0;
virtual void SetUserChangedContribution() const = 0;
- virtual bool GetAutoContribute() const = 0;
+ virtual void GetAutoContribute(
+ const GetAutoContributeCallback& callback) = 0;
virtual void SetAutoContribute(bool enabled) const = 0;
virtual void SetTimer(uint64_t time_offset, uint32_t& timer_id) = 0;
- virtual std::map GetAllBalanceReports() = 0;
+ virtual void GetAllBalanceReports(
+ const GetAllBalanceReportsCallback& callback) = 0;
virtual void GetCurrentBalanceReport() = 0;
- virtual bool IsWalletCreated() = 0;
+ virtual void IsWalletCreated(const IsWalletCreatedCallback& callback) = 0;
virtual void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) = 0;
- virtual double GetContributionAmount() = 0;
+ virtual void GetContributionAmount(
+ const GetContributionAmountCallback& callback) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id) = 0;
virtual void OnDonate(const std::string& publisher_key, int amount,
bool recurring, const ledger::PublisherInfo* publisher_info = NULL) = 0;
@@ -109,11 +138,12 @@ class RewardsService : public KeyedService {
virtual void UpdateRecurringDonationsList() = 0;
virtual void UpdateTipsList() = 0;
virtual void SetContributionAutoInclude(
- std::string publisher_key, bool excluded, uint64_t windowId) = 0;
+ const std::string& publisher_key, bool excluded, uint64_t windowId) = 0;
virtual RewardsNotificationService* GetNotificationService() const = 0;
virtual bool CheckImported() = 0;
- virtual void SetLedgerClient(std::unique_ptr new_ledger) = 0;
virtual void SetBackupCompleted() = 0;
+ virtual void GetAutoContributeProps(
+ const GetAutoContributePropsCallback& callback) = 0;
void AddObserver(RewardsServiceObserver* observer);
void RemoveObserver(RewardsServiceObserver* observer);
diff --git a/components/brave_rewards/browser/rewards_service_browsertest.cc b/components/brave_rewards/browser/rewards_service_browsertest.cc
index d965e4a4c2d1..9821ce7f0171 100644
--- a/components/brave_rewards/browser/rewards_service_browsertest.cc
+++ b/components/brave_rewards/browser/rewards_service_browsertest.cc
@@ -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/. */
+#include "base/run_loop.h"
#include "base/path_service.h"
+#include "bat/ledger/ledger.h"
#include "brave/common/brave_paths.h"
+#include "brave/components/brave_rewards/browser/rewards_service_impl.h"
+#include "brave/components/brave_rewards/browser/rewards_service_factory.h"
#include "brave/vendor/bat-native-ledger/src/bat_helper.h"
#include "brave/vendor/bat-native-ledger/src/static_values.h"
#include "chrome/browser/ui/browser.h"
@@ -12,6 +16,10 @@
#include "content/public/test/browser_test_utils.h"
#include "google_apis/gaia/mock_url_fetcher_factory.h"
#include "net/url_request/url_fetcher_delegate.h"
+#include "testing/gmock/include/gmock/gmock.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+using namespace brave_rewards;
namespace brave_test_resp {
std::string registrarVK_;
@@ -106,13 +114,38 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest {
InProcessBrowserTest::SetUpOnMainThread();
brave::RegisterPathProvider();
ReadTestData();
- braveledger_bat_helper::set_ignore_for_testing(true);
+ rewards_service_ = static_cast(
+ RewardsServiceFactory::GetForProfile(browser()->profile()));
+ rewards_service_->SetLedgerEnvForTesting();
}
void TearDown() override {
InProcessBrowserTest::TearDown();
}
+ void RunUntilIdle() {
+ base::RunLoop loop;
+ loop.RunUntilIdle();
+ }
+
+ void GetReconcileTime() {
+ rewards_service()->GetReconcileTime(
+ base::Bind(&BraveRewardsBrowserTest::OnGetReconcileTime,
+ base::Unretained(this)));
+ }
+
+ void GetShortRetries() {
+ rewards_service()->GetShortRetries(
+ base::Bind(&BraveRewardsBrowserTest::OnGetShortRetries,
+ base::Unretained(this)));
+ }
+
+ void GetProduction() {
+ rewards_service()->GetProduction(
+ base::Bind(&BraveRewardsBrowserTest::OnGetProduction,
+ base::Unretained(this)));
+ }
+
void ReadTestData() {
base::FilePath path;
ASSERT_TRUE(base::PathService::Get(brave::DIR_TEST_DATA, &path));
@@ -166,6 +199,13 @@ class BraveRewardsBrowserTest : public InProcessBrowserTest {
ASSERT_TRUE(jsResult.ExtractBool());
}
+ RewardsServiceImpl* rewards_service() { return rewards_service_; }
+
+ MOCK_METHOD1(OnGetProduction, void(bool));
+ MOCK_METHOD1(OnGetReconcileTime, void(int32_t));
+ MOCK_METHOD1(OnGetShortRetries, void(bool));
+
+ RewardsServiceImpl* rewards_service_;
MockURLFetcherFactory factory;
};
@@ -305,3 +345,115 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, ActivateSettingsModal) {
content::ISOLATED_WORLD_ID_CONTENT_END);
ASSERT_TRUE(modalResult.ExtractBool());
}
+
+IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
+ testing::InSequence s;
+ // SetProduction(true)
+ EXPECT_CALL(*this, OnGetProduction(true));
+ // Staging - true and 1
+ EXPECT_CALL(*this, OnGetProduction(false)).Times(2);
+ // Staging - false and random
+ EXPECT_CALL(*this, OnGetProduction(true)).Times(2);
+
+ rewards_service()->SetProduction(true);
+ GetProduction();
+ RunUntilIdle();
+
+ // Staging - true
+ rewards_service()->SetProduction(true);
+ rewards_service()->HandleFlags("staging=true");
+ GetProduction();
+ RunUntilIdle();
+
+ // Staging - 1
+ rewards_service()->SetProduction(true);
+ rewards_service()->HandleFlags("staging=1");
+ GetProduction();
+ RunUntilIdle();
+
+ // Staging - false
+ rewards_service()->SetProduction(false);
+ rewards_service()->HandleFlags("staging=false");
+ GetProduction();
+ RunUntilIdle();
+
+ // Staging - random
+ rewards_service()->SetProduction(false);
+ rewards_service()->HandleFlags("staging=werwe");
+ GetProduction();
+ RunUntilIdle();
+
+ // positive number
+ EXPECT_CALL(*this, OnGetReconcileTime(10));
+ // negative number and string
+ EXPECT_CALL(*this, OnGetReconcileTime(0)).Times(2);
+
+ // Reconcile interval - positive number
+ rewards_service()->SetReconcileTime(0);
+ rewards_service()->HandleFlags("reconcile-interval=10");
+ GetReconcileTime();
+ RunUntilIdle();
+
+ // Reconcile interval - negative number
+ rewards_service()->SetReconcileTime(0);
+ rewards_service()->HandleFlags("reconcile-interval=-1");
+ GetReconcileTime();
+ RunUntilIdle();
+
+ // Reconcile interval - string
+ rewards_service()->SetReconcileTime(0);
+ rewards_service()->HandleFlags("reconcile-interval=sdf");
+ GetReconcileTime();
+ RunUntilIdle();
+
+ EXPECT_CALL(*this, OnGetShortRetries(true)); // on
+ EXPECT_CALL(*this, OnGetShortRetries(false)); // off
+
+ // Short retries - on
+ rewards_service()->SetShortRetries(false);
+ rewards_service()->HandleFlags("short-retries=true");
+ GetShortRetries();
+ RunUntilIdle();
+
+ // Short retries - off
+ rewards_service()->SetShortRetries(true);
+ rewards_service()->HandleFlags("short-retries=false");
+ GetShortRetries();
+ RunUntilIdle();
+}
+
+IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) {
+ EXPECT_CALL(*this, OnGetProduction(false));
+ EXPECT_CALL(*this, OnGetReconcileTime(10));
+ EXPECT_CALL(*this, OnGetShortRetries(true));
+
+ rewards_service()->SetProduction(true);
+ rewards_service()->SetReconcileTime(0);
+ rewards_service()->SetShortRetries(false);
+
+ rewards_service()->HandleFlags(
+ "staging=true,short-retries=true,reconcile-interval=10");
+
+ GetReconcileTime();
+ GetShortRetries();
+ GetProduction();
+ RunUntilIdle();
+}
+
+IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsWrongInput) {
+ EXPECT_CALL(*this, OnGetProduction(true));
+ EXPECT_CALL(*this, OnGetReconcileTime(0));
+ EXPECT_CALL(*this, OnGetShortRetries(false));
+
+ rewards_service()->SetProduction(true);
+ rewards_service()->SetReconcileTime(0);
+ rewards_service()->SetShortRetries(false);
+
+ rewards_service()->HandleFlags(
+ "staging=,shortretries=true,reconcile-interval");
+
+ GetReconcileTime();
+ GetShortRetries();
+ GetProduction();
+ RunUntilIdle();
+}
diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc
index c478ce4f63fc..f834eb6bb462 100644
--- a/components/brave_rewards/browser/rewards_service_impl.cc
+++ b/components/brave_rewards/browser/rewards_service_impl.cc
@@ -10,6 +10,7 @@
#include "base/bind.h"
#include "base/command_line.h"
+#include "base/containers/flat_map.h"
#include "base/files/file_util.h"
#include "base/files/important_file_writer.h"
#include "base/guid.h"
@@ -23,11 +24,13 @@
#include "base/task_runner_util.h"
#include "base/threading/sequenced_task_runner_handle.h"
#include "bat/ledger/ledger.h"
+#include "bat/ledger/auto_contribute_props.h"
#include "bat/ledger/media_publisher_info.h"
#include "bat/ledger/publisher_info.h"
#include "bat/ledger/wallet_info.h"
#include "brave/browser/ui/webui/brave_rewards_source.h"
#include "brave/common/pref_names.h"
+#include "brave/components/brave_rewards/browser/auto_contribution_props.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
#include "brave/components/brave_rewards/browser/publisher_info_database.h"
#include "brave/components/brave_rewards/browser/rewards_fetcher_service_observer.h"
@@ -37,6 +40,7 @@
#include "brave/components/brave_rewards/browser/rewards_service_observer.h"
#include "brave/components/brave_rewards/browser/switches.h"
#include "brave/components/brave_rewards/browser/wallet_properties.h"
+#include "brave/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service_factory.h"
#include "chrome/browser/browser_process_impl.h"
#include "chrome/browser/favicon/favicon_service_factory.h"
@@ -46,13 +50,16 @@
#include "components/prefs/pref_service.h"
#include "content/public/browser/browser_task_traits.h"
#include "content/public/browser/url_data_source.h"
+#include "content/public/common/service_manager_connection.h"
#include "content_site.h"
#include "extensions/buildflags/buildflags.h"
+#include "mojo/public/cpp/bindings/map.h"
#include "net/base/escape.h"
#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
#include "net/base/url_util.h"
#include "net/url_request/url_fetcher.h"
#include "publisher_banner.h"
+#include "services/service_manager/public/cpp/connector.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/image/image.h"
#include "url/gurl.h"
@@ -106,26 +113,6 @@ class LogStreamImpl : public ledger::LogStream {
namespace {
-class LedgerURLLoaderImpl : public ledger::LedgerURLLoader {
- public:
- LedgerURLLoaderImpl(uint64_t request_id, net::URLFetcher* fetcher) :
- request_id_(request_id),
- fetcher_(fetcher) {}
- ~LedgerURLLoaderImpl() override = default;
-
- void Start() override {
- fetcher_->Start();
- }
-
- uint64_t request_id() override {
- return request_id_;
- }
-
- private:
- uint64_t request_id_;
- net::URLFetcher* fetcher_; // NOT OWNED
-};
-
ledger::PUBLISHER_MONTH GetPublisherMonth(const base::Time& time) {
base::Time::Exploded exploded;
time.LocalExplode(&exploded);
@@ -254,8 +241,6 @@ time_t GetCurrentTimestamp() {
return base::Time::NowFromSystemTime().ToTimeT();
}
-static uint64_t next_id = 1;
-
} // namespace
bool IsMediaLink(const GURL& url,
@@ -282,7 +267,7 @@ const base::FilePath::StringType kPublishers_list("publishers_list");
RewardsServiceImpl::RewardsServiceImpl(Profile* profile)
: profile_(profile),
- ledger_(ledger::Ledger::CreateInstance(this)),
+ bat_ledger_client_binding_(new bat_ledger::LedgerClientMojoProxy(this)),
#if BUILDFLAG(ENABLE_EXTENSIONS)
extension_rewards_service_observer_(
std::make_unique(profile_)),
@@ -302,12 +287,55 @@ RewardsServiceImpl::RewardsServiceImpl(Profile* profile)
std::make_unique(profile_)),
#endif
next_timer_id_(0) {
+ // Set up the rewards data source
+ content::URLDataSource::Add(profile_,
+ std::make_unique(profile_));
+}
+
+RewardsServiceImpl::~RewardsServiceImpl() {
+ file_task_runner_->DeleteSoon(FROM_HERE, publisher_info_backend_.release());
+ StopNotificationTimers();
+}
+
+void RewardsServiceImpl::ConnectionClosed() {
+ base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(FROM_HERE,
+ base::BindOnce(&RewardsServiceImpl::StartLedger, AsWeakPtr()),
+ base::TimeDelta::FromSeconds(1));
+}
+
+void RewardsServiceImpl::Init() {
+ AddObserver(notification_service_.get());
+#if BUILDFLAG(ENABLE_EXTENSIONS)
+ AddObserver(extension_rewards_service_observer_.get());
+ private_observers_.AddObserver(private_observer_.get());
+#endif
+
+ StartLedger();
+}
+
+void RewardsServiceImpl::StartLedger() {
+ bat_ledger::mojom::BatLedgerClientAssociatedPtrInfo client_ptr_info;
+ bat_ledger_client_binding_.Bind(mojo::MakeRequest(&client_ptr_info));
+
+ content::ServiceManagerConnection* connection =
+ content::ServiceManagerConnection::GetForProcess();
+ if (!connection) {
+ return;
+ }
+
+ connection->GetConnector()->BindInterface(
+ bat_ledger::mojom::kServiceName, &bat_ledger_service_);
+ bat_ledger_service_.set_connection_error_handler(
+ base::Bind(&RewardsServiceImpl::ConnectionClosed, AsWeakPtr()));
+
+ bool isProduction = true;
// Environment
#if defined(OFFICIAL_BUILD)
- ledger::is_production = true;
+ isProduction = true;
#else
- ledger::is_production = false;
+ isProduction = false;
#endif
+ SetProduction(isProduction);
const base::CommandLine& command_line =
*base::CommandLine::ForCurrentProcess();
@@ -320,33 +348,19 @@ RewardsServiceImpl::RewardsServiceImpl(Profile* profile)
}
}
- // Set up the rewards data source
- content::URLDataSource::Add(profile_,
- std::make_unique(profile_));
-}
+ bat_ledger_service_->Create(std::move(client_ptr_info),
+ MakeRequest(&bat_ledger_));
-RewardsServiceImpl::~RewardsServiceImpl() {
- file_task_runner_->DeleteSoon(FROM_HERE, publisher_info_backend_.release());
- StopNotificationTimers();
-}
-
-void RewardsServiceImpl::Init() {
- AddObserver(notification_service_.get());
-#if BUILDFLAG(ENABLE_EXTENSIONS)
- AddObserver(extension_rewards_service_observer_.get());
- private_observers_.AddObserver(private_observer_.get());
-#endif
- ledger_->Initialize();
+ bat_ledger_->Initialize();
}
-void RewardsServiceImpl::MaybeShowBackupNotification() {
+void RewardsServiceImpl::MaybeShowBackupNotification(uint64_t boot_stamp) {
PrefService* pref_service = profile_->GetPrefs();
bool user_has_funded = pref_service->GetBoolean(kRewardsUserHasFunded);
bool backup_succeeded = pref_service->GetBoolean(kRewardsBackupSucceeded);
if (user_has_funded && !backup_succeeded) {
base::Time now = base::Time::Now();
- base::Time boot_timestamp =
- base::Time::FromDoubleT(ledger_->GetBootStamp());
+ base::Time boot_timestamp = base::Time::FromDoubleT(boot_stamp);
base::TimeDelta backup_notification_frequency =
pref_service->GetTimeDelta(kRewardsBackupNotificationFrequency);
base::TimeDelta backup_notification_interval =
@@ -365,22 +379,23 @@ void RewardsServiceImpl::MaybeShowBackupNotification() {
}
}
-void RewardsServiceImpl::MaybeShowAddFundsNotification() {
+void RewardsServiceImpl::MaybeShowAddFundsNotification(
+ uint64_t reconcile_stamp) {
// Show add funds notification if reconciliation will occur in the
// next 3 days and balance is too low.
base::Time now = base::Time::Now();
- if (ledger_->GetReconcileStamp() - now.ToDoubleT() <
+ if (reconcile_stamp - now.ToDoubleT() <
3 * base::Time::kHoursPerDay * base::Time::kSecondsPerHour) {
- if (!HasSufficientBalanceToReconcile() &&
- ShouldShowNotificationAddFunds()) {
- ShowNotificationAddFunds();
+ if (ShouldShowNotificationAddFunds()) {
+ MaybeShowNotificationAddFunds();
}
}
}
void RewardsServiceImpl::CreateWallet() {
if (ready().is_signaled()) {
- ledger_->CreateWallet();
+ if (Connected())
+ bat_ledger_->CreateWallet();
} else {
ready().Post(FROM_HERE,
base::Bind(&brave_rewards::RewardsService::CreateWallet,
@@ -391,30 +406,55 @@ void RewardsServiceImpl::CreateWallet() {
void RewardsServiceImpl::GetCurrentContributeList(
uint32_t start,
uint32_t limit,
+ uint64_t min_visit_time,
+ uint64_t reconcile_stamp,
+ bool allow_non_verified,
const GetCurrentContributeListCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
ledger::PublisherInfoFilter filter;
filter.category = ledger::PUBLISHER_CATEGORY::AUTO_CONTRIBUTE;
filter.month = ledger::PUBLISHER_MONTH::ANY;
filter.year = -1;
- filter.min_duration = ledger_->GetPublisherMinVisitTime();
+ filter.min_duration = min_visit_time;
filter.order_by.push_back(std::pair("ai.percent", false));
- filter.reconcile_stamp = ledger_->GetReconcileStamp();
+ filter.reconcile_stamp = reconcile_stamp;
filter.excluded =
ledger::PUBLISHER_EXCLUDE_FILTER::FILTER_ALL_EXCEPT_EXCLUDED;
filter.percent = 1;
- filter.non_verified = ledger_->GetPublisherAllowNonVerified();
+ filter.non_verified = allow_non_verified;
- ledger_->GetPublisherInfoList(
- start,
- limit,
- filter,
- std::bind(&GetContentSiteListInternal,
+ bat_ledger_->GetPublisherInfoList(start, limit,
+ filter.ToJson(),
+ base::BindOnce(&RewardsServiceImpl::OnGetPublisherInfoList, AsWeakPtr(),
start,
limit,
- callback, _1, _2));
+ callback));
+}
+
+void RewardsServiceImpl::OnGetPublisherInfoList(
+ uint32_t start, uint32_t limit,
+ const GetCurrentContributeListCallback& callback,
+ const std::vector& publisher_info_list,
+ uint32_t next_record) {
+ ledger::PublisherInfoList list;
+
+ for (const auto& i : publisher_info_list) {
+ ledger::PublisherInfo info;
+ info.loadFromJson(i);
+ list.push_back(info);
+ }
+
+ GetContentSiteListInternal(start, limit, callback, std::move(list),
+ next_record);
}
void RewardsServiceImpl::OnLoad(SessionID tab_id, const GURL& url) {
+ if (!Connected())
+ return;
+
auto origin = url.GetOrigin();
const std::string baseDomain =
GetDomainAndRegistry(origin.host(), INCLUDE_PRIVATE_REGISTRIES);
@@ -435,36 +475,56 @@ void RewardsServiceImpl::OnLoad(SessionID tab_id, const GURL& url) {
publisher_url,
"",
"");
- ledger_->OnLoad(data, GetCurrentTimestamp());
+ bat_ledger_->OnLoad(data.ToJson(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnUnload(SessionID tab_id) {
+ if (!Connected())
+ return;
- ledger_->OnUnload(tab_id.id(), GetCurrentTimestamp());
+ bat_ledger_->OnUnload(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnShow(SessionID tab_id) {
- ledger_->OnShow(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnShow(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnHide(SessionID tab_id) {
- ledger_->OnHide(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnHide(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnForeground(SessionID tab_id) {
- ledger_->OnForeground(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnForeground(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnBackground(SessionID tab_id) {
- ledger_->OnBackground(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnBackground(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnMediaStart(SessionID tab_id) {
- ledger_->OnMediaStart(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnMediaStart(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnMediaStop(SessionID tab_id) {
- ledger_->OnMediaStop(tab_id.id(), GetCurrentTimestamp());
+ if (!Connected())
+ return;
+
+ bat_ledger_->OnMediaStop(tab_id.id(), GetCurrentTimestamp());
}
void RewardsServiceImpl::OnPostData(SessionID tab_id,
@@ -472,6 +532,9 @@ void RewardsServiceImpl::OnPostData(SessionID tab_id,
const GURL& first_party_url,
const GURL& referrer,
const std::string& post_data) {
+ if (!Connected())
+ return;
+
std::string output;
url::RawCanonOutputW<1024> canonOutput;
url::DecodeURLEscapeSequences(post_data.c_str(),
@@ -496,17 +559,20 @@ void RewardsServiceImpl::OnPostData(SessionID tab_id,
"",
"");
- ledger_->OnPostData(url.spec(),
- first_party_url.spec(),
- referrer.spec(),
- output,
- visit_data);
+ bat_ledger_->OnPostData(url.spec(),
+ first_party_url.spec(),
+ referrer.spec(),
+ output,
+ visit_data.ToJson());
}
void RewardsServiceImpl::OnXHRLoad(SessionID tab_id,
const GURL& url,
const GURL& first_party_url,
const GURL& referrer) {
+ if (!Connected())
+ return;
+
std::map parts;
for (net::QueryIterator it(url); !it.IsAtEnd(); it.Advance()) {
@@ -518,12 +584,12 @@ void RewardsServiceImpl::OnXHRLoad(SessionID tab_id,
GetPublisherMonth(now), GetPublisherYear(now),
"", "", "", "");
- ledger_->OnXHRLoad(tab_id.id(),
+ bat_ledger_->OnXHRLoad(tab_id.id(),
url.spec(),
- parts,
+ mojo::MapToFlatMap(parts),
first_party_url.spec(),
referrer.spec(),
- data);
+ data.ToJson());
}
void RewardsServiceImpl::LoadMediaPublisherInfo(
@@ -540,6 +606,9 @@ void RewardsServiceImpl::LoadMediaPublisherInfo(
void RewardsServiceImpl::OnMediaPublisherInfoLoaded(
ledger::PublisherInfoCallback callback,
std::unique_ptr info) {
+ if (!Connected())
+ return;
+
if (!info) {
callback(ledger::Result::NOT_FOUND, std::move(info));
return;
@@ -561,11 +630,17 @@ base::PostTaskAndReplyWithResult(file_task_runner_.get(), FROM_HERE,
}
void RewardsServiceImpl::ExcludePublisher(const std::string publisherKey) const {
- ledger_->SetPublisherExclude(publisherKey, ledger::PUBLISHER_EXCLUDE::EXCLUDED);
+ if (!Connected())
+ return;
+
+ bat_ledger_->SetPublisherExclude(publisherKey, ledger::PUBLISHER_EXCLUDE::EXCLUDED);
}
void RewardsServiceImpl::RestorePublishers() {
- ledger_->RestorePublishers();
+ if (!Connected())
+ return;
+
+ bat_ledger_->RestorePublishers();
}
void RewardsServiceImpl::OnMediaPublisherInfoSaved(bool success) {
@@ -601,7 +676,7 @@ void RewardsServiceImpl::Shutdown() {
}
fetchers_.clear();
- ledger_.reset();
+ bat_ledger_.reset();
RewardsService::Shutdown();
}
@@ -612,7 +687,7 @@ void RewardsServiceImpl::OnWalletInitialized(ledger::Result result) {
if (result == ledger::Result::WALLET_CREATED) {
SetRewardsMainEnabled(true);
SetAutoContribute(true);
- StartNotificationTimers();
+ StartNotificationTimers(true);
result = ledger::Result::LEDGER_OK;
}
@@ -624,6 +699,34 @@ void RewardsServiceImpl::OnWalletProperties(ledger::Result result,
TriggerOnWalletProperties(result, std::move(wallet_info));
}
+void RewardsServiceImpl::OnGetAutoContributeProps(
+ const GetAutoContributePropsCallback& callback,
+ const std::string& json_props) {
+ ledger::AutoContributeProps props;
+ props.loadFromJson(json_props);
+
+ auto auto_contri_props =
+ std::make_unique();
+ auto_contri_props->enabled_contribute = props.enabled_contribute;
+ auto_contri_props->contribution_min_time = props.contribution_min_time;
+ auto_contri_props->contribution_min_visits = props.contribution_min_visits;
+ auto_contri_props->contribution_non_verified =
+ props.contribution_non_verified;
+ auto_contri_props->contribution_videos = props.contribution_videos;
+ auto_contri_props->reconcile_stamp = props.reconcile_stamp;
+
+ callback.Run(std::move(auto_contri_props));
+}
+
+void RewardsServiceImpl::GetAutoContributeProps(
+ const GetAutoContributePropsCallback& callback) {
+ if (!Connected())
+ return;
+
+ bat_ledger_->GetAutoContributeProps(base::BindOnce(
+ &RewardsServiceImpl::OnGetAutoContributeProps, AsWeakPtr(), callback));
+}
+
void RewardsServiceImpl::OnGrant(ledger::Result result,
const ledger::Grant& grant) {
TriggerOnGrant(result, grant);
@@ -644,10 +747,13 @@ void RewardsServiceImpl::OnGrantFinish(ledger::Result result,
ledger::BalanceReportInfo report_info;
auto now = base::Time::Now();
if (result == ledger::Result::LEDGER_OK) {
- ledger_->SetBalanceReportItem(GetPublisherMonth(now),
- GetPublisherYear(now),
- ledger::ReportType::GRANT,
- grant.probi);
+ if (!Connected())
+ return;
+
+ bat_ledger_->SetBalanceReportItem(GetPublisherMonth(now),
+ GetPublisherYear(now),
+ ledger::ReportType::GRANT,
+ grant.probi);
}
GetCurrentBalanceReport();
@@ -660,8 +766,11 @@ void RewardsServiceImpl::OnReconcileComplete(ledger::Result result,
const std::string& probi) {
if (result == ledger::Result::LEDGER_OK) {
auto now = base::Time::Now();
+ if (!Connected())
+ return;
+
FetchWalletProperties();
- ledger_->OnReconcileCompleteSuccess(viewing_id,
+ bat_ledger_->OnReconcileCompleteSuccess(viewing_id,
category,
probi,
GetPublisherMonth(now),
@@ -690,12 +799,15 @@ void RewardsServiceImpl::LoadLedgerState(
void RewardsServiceImpl::OnLedgerStateLoaded(
ledger::LedgerCallbackHandler* handler,
const std::string& data) {
+ if (!Connected())
+ return;
+
handler->OnLedgerStateLoaded(data.empty() ? ledger::Result::LEDGER_ERROR
: ledger::Result::LEDGER_OK,
data);
- if (ledger_->GetRewardsMainEnabled()) {
- StartNotificationTimers();
- }
+ bat_ledger_->GetRewardsMainEnabled(
+ base::BindOnce(&RewardsServiceImpl::StartNotificationTimers,
+ AsWeakPtr()));
}
void RewardsServiceImpl::LoadPublisherState(
@@ -710,6 +822,9 @@ void RewardsServiceImpl::LoadPublisherState(
void RewardsServiceImpl::OnPublisherStateLoaded(
ledger::LedgerCallbackHandler* handler,
const std::string& data) {
+ if (!Connected())
+ return;
+
handler->OnPublisherStateLoaded(
data.empty() ? ledger::Result::NO_PUBLISHER_STATE
: ledger::Result::LEDGER_OK,
@@ -735,6 +850,9 @@ void RewardsServiceImpl::SaveLedgerState(const std::string& ledger_state,
void RewardsServiceImpl::OnLedgerStateSaved(
ledger::LedgerCallbackHandler* handler,
bool success) {
+ if (!Connected())
+ return;
+
handler->OnLedgerStateSaved(success ? ledger::Result::LEDGER_OK
: ledger::Result::NO_LEDGER_STATE);
}
@@ -757,12 +875,18 @@ void RewardsServiceImpl::SavePublisherState(const std::string& publisher_state,
void RewardsServiceImpl::OnPublisherStateSaved(
ledger::LedgerCallbackHandler* handler,
bool success) {
+ if (!Connected())
+ return;
+
handler->OnPublisherStateSaved(success ? ledger::Result::LEDGER_OK
: ledger::Result::LEDGER_ERROR);
}
void RewardsServiceImpl::LoadNicewareList(
ledger::GetNicewareListCallback callback) {
+ if (!Connected())
+ return;
+
std::string data = ui::ResourceBundle::GetSharedInstance().GetRawDataResource(
IDR_BRAVE_REWARDS_NICEWARE_LIST).as_string();
@@ -793,8 +917,10 @@ void RewardsServiceImpl::OnPublisherInfoSaved(
ledger::PublisherInfoCallback callback,
std::unique_ptr info,
bool success) {
- callback(success ? ledger::Result::LEDGER_OK
- : ledger::Result::LEDGER_ERROR, std::move(info));
+ if (Connected()) {
+ callback(success ? ledger::Result::LEDGER_OK
+ : ledger::Result::LEDGER_ERROR, std::move(info));
+ }
TriggerOnContentSiteUpdated();
}
@@ -815,6 +941,10 @@ void RewardsServiceImpl::LoadPublisherInfo(
void RewardsServiceImpl::OnPublisherInfoLoaded(
ledger::PublisherInfoCallback callback,
const ledger::PublisherInfoList list) {
+ if (!Connected()) {
+ return;
+ }
+
if (list.size() == 0) {
callback(ledger::Result::NOT_FOUND,
std::unique_ptr());
@@ -850,6 +980,10 @@ void RewardsServiceImpl::OnPublisherInfoListLoaded(
uint32_t limit,
ledger::PublisherInfoListCallback callback,
const ledger::PublisherInfoList& list) {
+ if (!Connected()) {
+ return;
+ }
+
uint32_t next_record = 0;
if (list.size() == limit)
next_record = start + limit + 1;
@@ -857,13 +991,13 @@ void RewardsServiceImpl::OnPublisherInfoListLoaded(
callback(std::cref(list), next_record);
}
-std::unique_ptr RewardsServiceImpl::LoadURL(
+void RewardsServiceImpl::LoadURL(
const std::string& url,
const std::vector& headers,
const std::string& content,
const std::string& contentType,
const ledger::URL_METHOD& method,
- ledger::LedgerCallbackHandler* handler) {
+ ledger::LoadURLCallback callback) {
net::URLFetcher::RequestType request_type = URLMethodToRequestType(method);
net::URLFetcher* fetcher = net::URLFetcher::Create(
@@ -905,17 +1039,8 @@ std::unique_ptr RewardsServiceImpl::LoadURL(
<< "[ END REQUEST ]";
}
- FetchCallback callback = base::Bind(
- &ledger::LedgerCallbackHandler::OnURLRequestResponse,
- base::Unretained(handler),
- next_id,
- url);
fetchers_[fetcher] = callback;
-
- std::unique_ptr loader(
- new LedgerURLLoaderImpl(next_id++, fetcher));
-
- return loader;
+ fetcher->Start();
}
void RewardsServiceImpl::OnURLFetchComplete(
@@ -950,30 +1075,11 @@ void RewardsServiceImpl::OnURLFetchComplete(
delete source;
- callback.Run(response_code, body, headers);
-}
-
-void RunIOTaskCallback(
- base::WeakPtr rewards_service,
- std::function callback) {
- base::PostTaskWithTraits(FROM_HERE, {content::BrowserThread::UI},
- base::BindOnce(&RewardsServiceImpl::OnIOTaskComplete,
- rewards_service,
- callback));
-}
-
-void RewardsServiceImpl::OnIOTaskComplete(std::function callback) {
- callback();
-}
-
-void RewardsServiceImpl::RunIOTask(
- std::unique_ptr task) {
- ledger::LedgerTaskRunner::CallerThreadCallback callback =
- std::bind(&RunIOTaskCallback, AsWeakPtr(), _1);
+ if (!Connected()) {
+ return;
+ }
- file_task_runner_->PostTask(FROM_HERE,
- base::BindOnce(&ledger::LedgerTaskRunner::Run,
- std::move(task), std::move(callback)));
+ callback(response_code == 200, body, headers);
}
void RewardsServiceImpl::TriggerOnWalletInitialized(int error_code) {
@@ -1017,7 +1123,11 @@ void RewardsServiceImpl::TriggerOnWalletProperties(int error_code,
void RewardsServiceImpl::FetchWalletProperties() {
if (ready().is_signaled()) {
- ledger_->FetchWalletProperties();
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->FetchWalletProperties();
} else {
ready().Post(FROM_HERE,
base::Bind(&brave_rewards::RewardsService::FetchWalletProperties,
@@ -1027,7 +1137,11 @@ void RewardsServiceImpl::FetchWalletProperties() {
void RewardsServiceImpl::FetchGrant(const std::string& lang,
const std::string& payment_id) {
- ledger_->FetchGrant(lang, payment_id);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->FetchGrant(lang, payment_id);
}
void RewardsServiceImpl::TriggerOnGrant(ledger::Result result,
@@ -1044,7 +1158,11 @@ void RewardsServiceImpl::TriggerOnGrant(ledger::Result result,
}
void RewardsServiceImpl::GetGrantCaptcha() {
- ledger_->GetGrantCaptcha();
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetGrantCaptcha();
}
void RewardsServiceImpl::TriggerOnGrantCaptcha(const std::string& image, const std::string& hint) {
@@ -1052,16 +1170,30 @@ void RewardsServiceImpl::TriggerOnGrantCaptcha(const std::string& image, const s
observer.OnGrantCaptcha(this, image, hint);
}
-std::string RewardsServiceImpl::GetWalletPassphrase() const {
- return ledger_->GetWalletPassphrase();
+void RewardsServiceImpl::GetWalletPassphrase(
+ const GetWalletPassphraseCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetWalletPassphrase(callback);
}
-unsigned int RewardsServiceImpl::GetNumExcludedSites() const {
- return ledger_->GetNumExcludedSites();
+void RewardsServiceImpl::GetNumExcludedSites(
+ const GetNumExcludedSitesCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetNumExcludedSites(callback);
}
void RewardsServiceImpl::RecoverWallet(const std::string passPhrase) const {
- return ledger_->RecoverWallet(passPhrase);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->RecoverWallet(passPhrase);
}
void RewardsServiceImpl::TriggerOnRecoverWallet(ledger::Result result,
@@ -1082,7 +1214,11 @@ void RewardsServiceImpl::TriggerOnRecoverWallet(ledger::Result result,
}
void RewardsServiceImpl::SolveGrantCaptcha(const std::string& solution) const {
- return ledger_->SolveGrantCaptcha(solution);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SolveGrantCaptcha(solution);
}
void RewardsServiceImpl::TriggerOnGrantFinish(ledger::Result result,
@@ -1098,74 +1234,141 @@ void RewardsServiceImpl::TriggerOnGrantFinish(ledger::Result result,
observer.OnGrantFinish(this, result, properties);
}
-uint64_t RewardsServiceImpl::GetReconcileStamp() const {
- return ledger_->GetReconcileStamp();
+void RewardsServiceImpl::GetReconcileStamp(
+ const GetReconcileStampCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetReconcileStamp(callback);
+}
+
+void RewardsServiceImpl::OnGetAddresses(const GetAddressesCallback& callback,
+ const base::flat_map& addresses) {
+ callback.Run(mojo::FlatMapToMap(addresses));
}
-std::map RewardsServiceImpl::GetAddresses() const {
- std::map addresses;
- addresses.emplace("BAT", ledger_->GetBATAddress());
- addresses.emplace("BTC", ledger_->GetBTCAddress());
- addresses.emplace("ETH", ledger_->GetETHAddress());
- addresses.emplace("LTC", ledger_->GetLTCAddress());
- return addresses;
+void RewardsServiceImpl::GetAddresses(const GetAddressesCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetAddresses(base::BindOnce(&RewardsServiceImpl::OnGetAddresses,
+ AsWeakPtr(), callback));
}
void RewardsServiceImpl::SetRewardsMainEnabled(bool enabled) const {
- return ledger_->SetRewardsMainEnabled(enabled);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetRewardsMainEnabled(enabled);
}
-uint64_t RewardsServiceImpl::GetPublisherMinVisitTime() const {
- return ledger_->GetPublisherMinVisitTime();
+void RewardsServiceImpl::GetPublisherMinVisitTime(
+ const GetPublisherMinVisitTimeCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetPublisherMinVisitTime(callback);
}
void RewardsServiceImpl::SetPublisherMinVisitTime(
uint64_t duration_in_seconds) const {
- return ledger_->SetPublisherMinVisitTime(duration_in_seconds);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetPublisherMinVisitTime(duration_in_seconds);
}
-unsigned int RewardsServiceImpl::GetPublisherMinVisits() const {
- return ledger_->GetPublisherMinVisits();
+void RewardsServiceImpl::GetPublisherMinVisits(
+ const GetPublisherMinVisitsCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetPublisherMinVisits(callback);
}
void RewardsServiceImpl::SetPublisherMinVisits(unsigned int visits) const {
- return ledger_->SetPublisherMinVisits(visits);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetPublisherMinVisits(visits);
}
-bool RewardsServiceImpl::GetPublisherAllowNonVerified() const {
- return ledger_->GetPublisherAllowNonVerified();
+void RewardsServiceImpl::GetPublisherAllowNonVerified(
+ const GetPublisherAllowNonVerifiedCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetPublisherAllowNonVerified(callback);
}
void RewardsServiceImpl::SetPublisherAllowNonVerified(bool allow) const {
- return ledger_->SetPublisherAllowNonVerified(allow);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetPublisherAllowNonVerified(allow);
}
-bool RewardsServiceImpl::GetPublisherAllowVideos() const {
- return ledger_->GetPublisherAllowVideos();
+void RewardsServiceImpl::GetPublisherAllowVideos(
+ const GetPublisherAllowVideosCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetPublisherAllowVideos(callback);
}
void RewardsServiceImpl::SetPublisherAllowVideos(bool allow) const {
- return ledger_->SetPublisherAllowVideos(allow);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetPublisherAllowVideos(allow);
}
void RewardsServiceImpl::SetContributionAmount(double amount) const {
- ledger_->SetUserChangedContribution();
- ledger_->SetContributionAmount(amount);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetUserChangedContribution();
+ bat_ledger_->SetContributionAmount(amount);
}
// TODO: remove me (and pure virtual definition)
// see https://github.com/brave/brave-core/commit/c4ef62c954a64fca18ae83ff8ffd611137323420#diff-aa3505dbf36b5d03d8ba0751e0c99904R385
// and https://github.com/brave-intl/bat-native-ledger/commit/27f3ceb471d61c84052737ff201fe18cb9a6af32#diff-e303122e010480b2226895b9470891a3R135
void RewardsServiceImpl::SetUserChangedContribution() const {
- ledger_->SetUserChangedContribution();
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetUserChangedContribution();
}
-bool RewardsServiceImpl::GetAutoContribute() const {
- return ledger_->GetAutoContribute();
+void RewardsServiceImpl::GetAutoContribute(
+ const GetAutoContributeCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetAutoContribute(callback);
}
void RewardsServiceImpl::SetAutoContribute(bool enabled) const {
- return ledger_->SetAutoContribute(enabled);
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->SetAutoContribute(enabled);
}
void RewardsServiceImpl::TriggerOnContentSiteUpdated() {
@@ -1192,6 +1395,10 @@ void RewardsServiceImpl::SavePublishersList(const std::string& publishers_list,
void RewardsServiceImpl::OnPublishersListSaved(
ledger::LedgerCallbackHandler* handler,
bool success) {
+ if (!Connected()) {
+ return;
+ }
+
handler->OnPublishersListSaved(success ? ledger::Result::LEDGER_OK
: ledger::Result::LEDGER_ERROR);
}
@@ -1213,8 +1420,12 @@ void RewardsServiceImpl::SetTimer(uint64_t time_offset,
}
void RewardsServiceImpl::OnTimer(uint32_t timer_id) {
- ledger_->OnTimer(timer_id);
+ if (!Connected()) {
+ return;
+ }
+
timers_.erase(timer_id);
+ bat_ledger_->OnTimer(timer_id);
}
void RewardsServiceImpl::LoadPublisherList(
@@ -1229,14 +1440,25 @@ void RewardsServiceImpl::LoadPublisherList(
void RewardsServiceImpl::OnPublisherListLoaded(
ledger::LedgerCallbackHandler* handler,
const std::string& data) {
+ if (!Connected()) {
+ return;
+ }
+
handler->OnPublisherListLoaded(
data.empty() ? ledger::Result::NO_PUBLISHER_LIST
: ledger::Result::LEDGER_OK,
data);
}
-std::map RewardsServiceImpl::GetAllBalanceReports() {
- std::map reports = ledger_->GetAllBalanceReports();
+void RewardsServiceImpl::OnGetAllBalanceReports(
+ const GetAllBalanceReportsCallback& callback,
+ const base::flat_map& json_reports) {
+ std::map reports;
+ for (auto const& report : json_reports) {
+ ledger::BalanceReportInfo info;
+ info.loadFromJson(report.second);
+ reports[report.first] = info;
+ }
std::map newReports;
for (auto const& report : reports) {
@@ -1253,21 +1475,49 @@ std::map RewardsServiceImpl::GetAllBa
newReports[report.first] = newReport;
}
- return newReports;
+ callback.Run(newReports);
}
-void RewardsServiceImpl::GetCurrentBalanceReport() {
+void RewardsServiceImpl::GetAllBalanceReports(
+ const GetAllBalanceReportsCallback& callback) {
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetAllBalanceReports(
+ base::BindOnce(&RewardsServiceImpl::OnGetAllBalanceReports,
+ AsWeakPtr(), callback));
+}
+
+void RewardsServiceImpl::OnGetCurrentBalanceReport(
+ bool success, const std::string& json_report) {
ledger::BalanceReportInfo report;
- auto now = base::Time::Now();
- bool success = ledger_->GetBalanceReport(GetPublisherMonth(now),
- GetPublisherYear(now), &report);
+ report.loadFromJson(json_report);
+
if (success) {
TriggerOnGetCurrentBalanceReport(report);
}
}
-bool RewardsServiceImpl::IsWalletCreated() {
- return ledger_->IsWalletCreated();
+void RewardsServiceImpl::GetCurrentBalanceReport() {
+ auto now = base::Time::Now();
+ if (!Connected()) {
+ return;
+ }
+
+ bat_ledger_->GetBalanceReport(GetPublisherMonth(now), GetPublisherYear(now),
+ base::BindOnce(&RewardsServiceImpl::OnGetCurrentBalanceReport,
+ AsWeakPtr()));
+}
+
+void RewardsServiceImpl::IsWalletCreated(
+ const IsWalletCreatedCallback& callback) {
+ if (!Connected()) {
+ callback.Run(false);
+ return;
+ }
+
+ bat_ledger_->IsWalletCreated(callback);
}
void RewardsServiceImpl::GetPublisherActivityFromUrl(uint64_t windowId,
@@ -1290,6 +1540,9 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl(uint64_t windowId,
return;
}
+ if (!Connected())
+ return;
+
ledger::VisitData visitData;
visitData.domain = baseDomain;
visitData.path = parsedUrl.PathForRequest();
@@ -1299,7 +1552,7 @@ void RewardsServiceImpl::GetPublisherActivityFromUrl(uint64_t windowId,
visitData.url = origin.spec();
visitData.favicon_url = favicon_url;
- ledger_->GetPublisherActivityFromUrl(windowId, visitData);
+ bat_ledger_->GetPublisherActivityFromUrl(windowId, visitData.ToJson());
}
void RewardsServiceImpl::OnExcludedSitesChanged(const std::string& publisher_id) {
@@ -1317,8 +1570,12 @@ void RewardsServiceImpl::OnPublisherActivity(ledger::Result result,
TriggerOnGetPublisherActivityFromUrl(result, std::move(info), windowId);
}
-double RewardsServiceImpl::GetContributionAmount() {
- return ledger_->GetContributionAmount();
+void RewardsServiceImpl::GetContributionAmount(
+ const GetContributionAmountCallback& callback) {
+ if (!Connected())
+ return;
+
+ bat_ledger_->GetContributionAmount(callback);
}
void RewardsServiceImpl::FetchFavIcon(const std::string& url,
@@ -1403,12 +1660,29 @@ void RewardsServiceImpl::OnFetchFavIconCompleted(ledger::FetchIconCallback callb
void RewardsServiceImpl::OnSetOnDemandFaviconComplete(const std::string& favicon_url,
ledger::FetchIconCallback callback,
bool success) {
+ if (!Connected())
+ return;
+
callback(success, favicon_url);
}
void RewardsServiceImpl::GetPublisherBanner(const std::string& publisher_id) {
- ledger_->GetPublisherBanner(publisher_id,
- std::bind(&RewardsServiceImpl::OnPublisherBanner, this, _1));
+ if (!Connected())
+ return;
+
+ bat_ledger_->GetPublisherBanner(publisher_id,
+ base::BindOnce(&RewardsServiceImpl::OnPublisherBannerMojoProxy,
+ AsWeakPtr()));
+}
+
+void RewardsServiceImpl::OnPublisherBannerMojoProxy(
+ const std::string& banner) {
+ std::unique_ptr publisher_banner;
+ if (!banner.empty()) {
+ publisher_banner.reset(new ledger::PublisherBanner());
+ publisher_banner->loadFromJson(banner);
+ }
+ OnPublisherBanner(std::move(publisher_banner));
}
void RewardsServiceImpl::OnPublisherBanner(std::unique_ptr banner) {
@@ -1452,12 +1726,15 @@ void RewardsServiceImpl::OnDonate(const std::string& publisher_key, int amount,
return;
}
+ if (!Connected())
+ return;
+
ledger::PublisherInfo publisher(
publisher_key,
ledger::PUBLISHER_MONTH::ANY,
-1);
- ledger_->DoDirectDonation(publisher, amount, "BAT");
+ bat_ledger_->DoDirectDonation(publisher.ToJson(), amount, "BAT");
}
bool SaveContributionInfoOnFileTaskRunner(const brave_rewards::ContributionInfo info,
@@ -1540,6 +1817,10 @@ ledger::PublisherInfoList GetRecurringDonationsOnFileTaskRunner(PublisherInfoDat
void RewardsServiceImpl::OnRecurringDonationsData(const ledger::PublisherInfoListCallback callback,
const ledger::PublisherInfoList list) {
+ if (!Connected()) {
+ return;
+ }
+
callback(list, 0);
}
@@ -1602,7 +1883,10 @@ void RewardsServiceImpl::OnTipsUpdatedData(const ledger::PublisherInfoList list)
}
void RewardsServiceImpl::RemoveRecurring(const std::string& publisher_key) {
- ledger_->RemoveRecurring(publisher_key);
+ if (!Connected())
+ return;
+
+ bat_ledger_->RemoveRecurring(publisher_key);
}
void RewardsServiceImpl::TipsUpdated() {
@@ -1623,7 +1907,11 @@ bool RemoveRecurringOnFileTaskRunner(const std::string publisher_key, PublisherI
}
void RewardsServiceImpl::OnRemovedRecurring(ledger::RecurringRemoveCallback callback, bool success) {
- callback(success ? ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR);
+ if (Connected()) {
+ callback(success ?
+ ledger::Result::LEDGER_OK : ledger::Result::LEDGER_ERROR);
+ }
+
UpdateRecurringDonationsList();
}
@@ -1661,9 +1949,12 @@ void RewardsServiceImpl::TriggerOnGetPublisherActivityFromUrl(
windowId);
}
-void RewardsServiceImpl::SetContributionAutoInclude(std::string publisher_key,
- bool excluded, uint64_t windowId) {
- ledger_->SetPublisherPanelExclude(publisher_key, excluded ?
+void RewardsServiceImpl::SetContributionAutoInclude(
+ const std::string& publisher_key, bool excluded, uint64_t windowId) {
+ if (!Connected())
+ return;
+
+ bat_ledger_->SetPublisherPanelExclude(publisher_key, excluded ?
ledger::PUBLISHER_EXCLUDE::EXCLUDED : ledger::PUBLISHER_EXCLUDE::INCLUDED,
windowId);
}
@@ -1672,7 +1963,9 @@ RewardsNotificationService* RewardsServiceImpl::GetNotificationService() const {
return notification_service_.get();
}
-void RewardsServiceImpl::StartNotificationTimers() {
+void RewardsServiceImpl::StartNotificationTimers(bool main_enabled) {
+ if (!main_enabled) return;
+
// Startup timer, begins after 3-second delay.
notification_startup_timer_ = std::make_unique();
notification_startup_timer_->Start(
@@ -1697,12 +1990,21 @@ void RewardsServiceImpl::StopNotificationTimers() {
}
void RewardsServiceImpl::OnNotificationTimerFired() {
- MaybeShowBackupNotification();
- MaybeShowAddFundsNotification();
+ if (!Connected())
+ return;
+
+ bat_ledger_->GetBootStamp(
+ base::BindOnce(&RewardsServiceImpl::MaybeShowBackupNotification,
+ AsWeakPtr()));
+ GetReconcileStamp(
+ base::Bind(&RewardsServiceImpl::MaybeShowAddFundsNotification,
+ AsWeakPtr()));
}
-bool RewardsServiceImpl::HasSufficientBalanceToReconcile() const {
- return (ledger_->GetBalance() >= ledger_->GetContributionAmount());
+void RewardsServiceImpl::MaybeShowNotificationAddFunds() {
+ bat_ledger_->HasSufficientBalanceToReconcile(
+ base::BindOnce(&RewardsServiceImpl::ShowNotificationAddFunds,
+ AsWeakPtr()));
}
bool RewardsServiceImpl::ShouldShowNotificationAddFunds() const {
@@ -1711,7 +2013,9 @@ bool RewardsServiceImpl::ShouldShowNotificationAddFunds() const {
return (next_time.is_null() || base::Time::Now() > next_time);
}
-void RewardsServiceImpl::ShowNotificationAddFunds() {
+void RewardsServiceImpl::ShowNotificationAddFunds(bool sufficient) {
+ if (sufficient) return;
+
base::Time next_time = base::Time::Now() + base::TimeDelta::FromDays(3);
profile_->GetPrefs()->SetTime(kRewardsAddFundsNotification, next_time);
RewardsNotificationService::RewardsNotificationArgs args;
@@ -1752,31 +2056,41 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) {
}
if (name == "staging") {
+ bool is_production;
std::string lower = base::ToLowerASCII(value);
+
if (lower == "true" || lower == "1") {
- ledger::is_production = false;
+ is_production = false;
} else {
- ledger::is_production = true;
+ is_production = true;
}
+
+ SetProduction(is_production);
continue;
}
if (name == "reconcile-interval") {
int reconcile_int;
bool success = base::StringToInt(value, &reconcile_int);
+
if (success && reconcile_int > 0) {
- ledger::reconcile_time = reconcile_int;
+ SetReconcileTime(reconcile_int);
}
+
continue;
}
if (name == "short-retries") {
std::string lower = base::ToLowerASCII(value);
+ bool short_retries;
+
if (lower == "true" || lower == "1") {
- ledger::short_retries = true;
+ short_retries = true;
} else {
- ledger::short_retries = false;
+ short_retries = false;
}
+
+ SetShortRetries(short_retries);
}
}
}
@@ -1820,9 +2134,46 @@ void RewardsServiceImpl::OnDonate(
OnDonate(publisher_key, amount, recurring, &info);
}
-void RewardsServiceImpl::SetLedgerClient(
- std::unique_ptr new_ledger) {
- ledger_ = std::move(new_ledger);
+bool RewardsServiceImpl::Connected() const {
+ return bat_ledger_.is_bound();
+}
+
+void RewardsServiceImpl::SetLedgerEnvForTesting() {
+ bat_ledger_service_->SetTesting();
+
+ // this is needed because we are using braveledger_bat_helper::buildURL
+ // directly in BraveRewardsBrowserTest
+ #if defined(OFFICIAL_BUILD)
+ ledger::is_production = true;
+ #else
+ ledger::is_production = false;
+ #endif
+}
+
+void RewardsServiceImpl::GetProduction(const GetProductionCallback& callback) {
+ bat_ledger_service_->GetProduction(callback);
+}
+
+void RewardsServiceImpl::GetReconcileTime(
+ const GetReconcileTimeCallback& callback) {
+ bat_ledger_service_->GetReconcileTime(callback);
+}
+
+void RewardsServiceImpl::GetShortRetries(
+ const GetShortRetriesCallback& callback) {
+ bat_ledger_service_->GetShortRetries(callback);
+}
+
+void RewardsServiceImpl::SetProduction(bool production) {
+ bat_ledger_service_->SetProduction(production);
+}
+
+void RewardsServiceImpl::SetReconcileTime(int32_t time) {
+ bat_ledger_service_->SetReconcileTime(time);
+}
+
+void RewardsServiceImpl::SetShortRetries(bool short_retries) {
+ bat_ledger_service_->SetShortRetries(short_retries);
}
} // namespace brave_rewards
diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h
index 069810a55821..addfee321d98 100644
--- a/components/brave_rewards/browser/rewards_service_impl.h
+++ b/components/brave_rewards/browser/rewards_service_impl.h
@@ -16,11 +16,13 @@
#include "base/observer_list.h"
#include "base/memory/weak_ptr.h"
#include "bat/ledger/ledger_client.h"
+#include "brave/components/services/bat_ledger/public/interfaces/bat_ledger.mojom.h"
#include "brave/components/brave_rewards/browser/rewards_service.h"
#include "chrome/browser/bitmap_fetcher/bitmap_fetcher_service.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/buildflags/buildflags.h"
#include "extensions/common/one_shot_event.h"
+#include "mojo/public/cpp/bindings/associated_binding.h"
#include "net/url_request/url_fetcher_delegate.h"
#include "brave/components/brave_rewards/browser/balance_report.h"
#include "brave/components/brave_rewards/browser/content_site.h"
@@ -60,6 +62,10 @@ namespace brave_rewards {
class PublisherInfoDatabase;
class RewardsNotificationServiceImpl;
+using GetProductionCallback = base::Callback;
+using GetReconcileTimeCallback = base::Callback;
+using GetShortRetriesCallback = base::Callback;
+
class RewardsServiceImpl : public RewardsService,
public ledger::LedgerClient,
public net::URLFetcherDelegate,
@@ -72,17 +78,23 @@ class RewardsServiceImpl : public RewardsService,
void Shutdown() override;
void Init();
+ void StartLedger();
void CreateWallet() override;
void FetchWalletProperties() override;
void FetchGrant(const std::string& lang, const std::string& paymentId) override;
void GetGrantCaptcha() override;
void SolveGrantCaptcha(const std::string& solution) const override;
- std::string GetWalletPassphrase() const override;
- unsigned int GetNumExcludedSites() const override;
+ void GetWalletPassphrase(
+ const GetWalletPassphraseCallback& callback) override;
+ void GetNumExcludedSites(
+ const GetNumExcludedSitesCallback& callback) override;
void RecoverWallet(const std::string passPhrase) const override;
void GetCurrentContributeList(
uint32_t start,
uint32_t limit,
+ uint64_t min_visit_time,
+ uint64_t reconcile_stamp,
+ bool allow_non_verified,
const GetCurrentContributeListCallback& callback) override;
void OnLoad(SessionID tab_id, const GURL& url) override;
void OnUnload(SessionID tab_id) override;
@@ -102,48 +114,60 @@ class RewardsServiceImpl : public RewardsService,
const GURL& referrer,
const std::string& post_data) override;
std::string URIEncode(const std::string& value) override;
- uint64_t GetReconcileStamp() const override;
- std::map GetAddresses() const override;
- bool GetAutoContribute() const override;
- uint64_t GetPublisherMinVisitTime() const override;
- unsigned int GetPublisherMinVisits() const override;
- bool GetPublisherAllowNonVerified() const override;
- bool GetPublisherAllowVideos() const override;
+ void GetReconcileStamp(const GetReconcileStampCallback& callback) override;
+ void GetAddresses(const GetAddressesCallback& callback) override;
+ void GetAutoContribute(
+ const GetAutoContributeCallback& callback) override;
+ void GetPublisherMinVisitTime(
+ const GetPublisherMinVisitTimeCallback& callback) override;
+ void GetPublisherMinVisits(
+ const GetPublisherMinVisitsCallback& callback) override;
+ void GetPublisherAllowNonVerified(
+ const GetPublisherAllowNonVerifiedCallback& callback) override;
+ void GetPublisherAllowVideos(
+ const GetPublisherAllowVideosCallback& callback) override;
void LoadMediaPublisherInfo(
const std::string& media_key,
ledger::PublisherInfoCallback callback) override;
void SaveMediaPublisherInfo(const std::string& media_key, const std::string& publisher_id) override;
void ExcludePublisher(const std::string publisherKey) const override;
void RestorePublishers() override;
- std::map GetAllBalanceReports() override;
+ void GetAllBalanceReports(
+ const GetAllBalanceReportsCallback& callback) override;
void GetCurrentBalanceReport() override;
- bool IsWalletCreated() override;
+ void IsWalletCreated(const IsWalletCreatedCallback& callback) override;
void GetPublisherActivityFromUrl(uint64_t windowId, const std::string& url, const std::string& favicon_url) override;
- double GetContributionAmount() override;
+ void GetContributionAmount(const GetContributionAmountCallback& callback) override;
void GetPublisherBanner(const std::string& publisher_id) override;
void OnPublisherBanner(std::unique_ptr banner);
void RemoveRecurring(const std::string& publisher_key) override;
void UpdateRecurringDonationsList() override;
void UpdateTipsList() override;
void SetContributionAutoInclude(
- std::string publisher_key, bool excluded, uint64_t windowId) override;
+ const std::string& publisher_key, bool excluded, uint64_t windowId) override;
RewardsNotificationService* GetNotificationService() const override;
bool CheckImported() override;
void SetBackupCompleted() override;
- static void HandleFlags(const std::string& options);
+ void HandleFlags(const std::string& options);
+ void SetProduction(bool production);
+ void GetProduction(const GetProductionCallback& callback);
+ void SetReconcileTime(int32_t time);
+ void GetReconcileTime(const GetReconcileTimeCallback& callback);
+ void SetShortRetries(bool short_retries);
+ void GetShortRetries(const GetShortRetriesCallback& callback);
+
void OnWalletProperties(ledger::Result result,
std::unique_ptr info) override;
void OnDonate(const std::string& publisher_key, int amount, bool recurring,
std::unique_ptr site) override;
- void SetLedgerClient(std::unique_ptr new_ledger) override;
+ void GetAutoContributeProps(
+ const GetAutoContributePropsCallback& callback) override;
- private:
- friend void RunIOTaskCallback(
- base::WeakPtr,
- std::function);
- typedef base::Callback& headers)> FetchCallback;
+ // Testing methods
+ void SetLedgerEnvForTesting();
+ private:
const extensions::OneShotEvent& ready() const { return ready_; }
void OnLedgerStateSaved(ledger::LedgerCallbackHandler* handler,
bool success);
@@ -201,8 +225,8 @@ class RewardsServiceImpl : public RewardsService,
ledger::Result result,
std::unique_ptr info,
uint64_t windowId);
- void MaybeShowBackupNotification();
- void MaybeShowAddFundsNotification();
+ void MaybeShowBackupNotification(uint64_t boot_stamp);
+ void MaybeShowAddFundsNotification(uint64_t reconcile_stamp);
// ledger::LedgerClient
std::string GenerateGUID() const override;
@@ -239,14 +263,13 @@ class RewardsServiceImpl : public RewardsService,
void SetTimer(uint64_t time_offset, uint32_t& timer_id) override;
void LoadPublisherList(ledger::LedgerCallbackHandler* handler) override;
- std::unique_ptr LoadURL(const std::string& url,
+ void LoadURL(const std::string& url,
const std::vector& headers,
const std::string& content,
const std::string& contentType,
const ledger::URL_METHOD& method,
- ledger::LedgerCallbackHandler* handler) override;
+ ledger::LoadURLCallback callback) override;
- void RunIOTask(std::unique_ptr task) override;
void SetRewardsMainEnabled(bool enabled) const override;
void SetPublisherMinVisitTime(uint64_t duration_in_seconds) const override;
void SetPublisherMinVisits(unsigned int visits) const override;
@@ -282,21 +305,44 @@ class RewardsServiceImpl : public RewardsService,
int line,
const ledger::LogLevel log_level) const override;
- void OnIOTaskComplete(std::function callback);
-
// URLFetcherDelegate impl
void OnURLFetchComplete(const net::URLFetcher* source) override;
- void StartNotificationTimers();
+ void StartNotificationTimers(bool main_enabled);
void StopNotificationTimers();
void OnNotificationTimerFired();
- bool HasSufficientBalanceToReconcile() const;
+ void MaybeShowNotificationAddFunds();
bool ShouldShowNotificationAddFunds() const;
- void ShowNotificationAddFunds();
+ void ShowNotificationAddFunds(bool sufficient);
+
+ // Mojo Proxy methods
+ void OnPublisherBannerMojoProxy(const std::string& banner);
+ void OnGetPublisherInfoList(uint32_t start, uint32_t limit,
+ const GetCurrentContributeListCallback& callback,
+ const std::vector& publisher_info_list,
+ uint32_t next_record);
+ void OnGetAllBalanceReports(
+ const GetAllBalanceReportsCallback& callback,
+ const base::flat_map& json_reports);
+ void OnGetCurrentBalanceReport(
+ bool success, const std::string& json_report);
+ void OnGetAddresses(
+ const GetAddressesCallback& callback,
+ const base::flat_map& addresses);
+ void OnGetAutoContributeProps(
+ const GetAutoContributePropsCallback& callback,
+ const std::string& json_props);
+
+ bool Connected() const;
+ void ConnectionClosed();
Profile* profile_; // NOT OWNED
- std::unique_ptr ledger_;
+ mojo::AssociatedBinding
+ bat_ledger_client_binding_;
+ bat_ledger::mojom::BatLedgerAssociatedPtr bat_ledger_;
+ bat_ledger::mojom::BatLedgerServicePtr bat_ledger_service_;
+
#if BUILDFLAG(ENABLE_EXTENSIONS)
std::unique_ptr
extension_rewards_service_observer_;
@@ -314,7 +360,7 @@ class RewardsServiceImpl : public RewardsService,
#endif
extensions::OneShotEvent ready_;
- std::map fetchers_;
+ std::map fetchers_;
std::map> timers_;
std::vector current_media_fetchers_;
std::vector request_ids_;
diff --git a/components/brave_rewards/browser/rewards_service_impl_unittest.cc b/components/brave_rewards/browser/rewards_service_impl_unittest.cc
index 0edaa20b3585..dc47f2f3e6b7 100644
--- a/components/brave_rewards/browser/rewards_service_impl_unittest.cc
+++ b/components/brave_rewards/browser/rewards_service_impl_unittest.cc
@@ -14,7 +14,6 @@
#include "content/public/test/test_browser_thread_bundle.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
-#include "url/gurl.h"
// npm run test -- brave_unit_tests --filter=RewardsServiceTest.*
@@ -55,7 +54,6 @@ class MockRewardsServiceObserver : public RewardsServiceObserver {
void(RewardsService*, int, ledger::PublisherInfo*, uint64_t));
};
-
class RewardsServiceTest : public testing::Test {
public:
RewardsServiceTest() {}
@@ -67,15 +65,11 @@ class RewardsServiceTest : public testing::Test {
profile_ = CreateBraveRewardsProfile(temp_dir_.GetPath());
ASSERT_TRUE(profile_.get() != NULL);
rewards_service_ = static_cast(
- RewardsServiceFactory::GetInstance()->GetForProfile(profile()));
+ RewardsServiceFactory::GetForProfile(profile()));
ASSERT_TRUE(RewardsServiceFactory::GetInstance() != NULL);
ASSERT_TRUE(rewards_service() != NULL);
observer_.reset(new MockRewardsServiceObserver);
rewards_service_->AddObserver(observer_.get());
-
- auto ledger_client = std::make_unique();
- ledger_client_ = ledger_client.get();
- rewards_service()->SetLedgerClient(std::move(ledger_client));
}
void TearDown() override {
@@ -86,7 +80,6 @@ class RewardsServiceTest : public testing::Test {
Profile* profile() { return profile_.get(); }
RewardsServiceImpl* rewards_service() { return rewards_service_; }
MockRewardsServiceObserver* observer() { return observer_.get(); }
- MockLedgerClient* ledger_client() { return ledger_client_; }
private:
// Need this as a very first member to run tests in UI thread
@@ -97,98 +90,12 @@ class RewardsServiceTest : public testing::Test {
RewardsServiceImpl* rewards_service_;
std::unique_ptr observer_;
base::ScopedTempDir temp_dir_;
- MockLedgerClient* ledger_client_; // not owned
};
-TEST_F(RewardsServiceTest, HandleFlags) {
- // Staging - true
- ledger::is_production = true;
- ASSERT_TRUE(ledger::is_production);
- rewards_service()->HandleFlags("staging=true");
- ASSERT_FALSE(ledger::is_production);
-
- // Staging - 1
- ledger::is_production = true;
- ASSERT_TRUE(ledger::is_production);
- RewardsServiceImpl::HandleFlags("staging=1");
- ASSERT_FALSE(ledger::is_production);
-
- // Staging - false
- ledger::is_production = true;
- ASSERT_TRUE(ledger::is_production);
- RewardsServiceImpl::HandleFlags("staging=false");
- ASSERT_TRUE(ledger::is_production);
-
- // Staging - random
- ledger::is_production = true;
- ASSERT_TRUE(ledger::is_production);
- RewardsServiceImpl::HandleFlags("staging=werwe");
- ASSERT_TRUE(ledger::is_production);
-
- // Reconcile interval - positive number
- ledger::reconcile_time = 0;
- ASSERT_EQ(ledger::reconcile_time, 0);
- RewardsServiceImpl::HandleFlags("reconcile-interval=10");
- ASSERT_EQ(ledger::reconcile_time, 10);
-
- // Reconcile interval - negative number
- ledger::reconcile_time = 0;
- ASSERT_EQ(ledger::reconcile_time, 0);
- RewardsServiceImpl::HandleFlags("reconcile-interval=-1");
- ASSERT_EQ(ledger::reconcile_time, 0);
-
- // Reconcile interval - string
- ledger::reconcile_time = 0;
- ASSERT_EQ(ledger::reconcile_time, 0);
- RewardsServiceImpl::HandleFlags("reconcile-interval=sdf");
- ASSERT_EQ(ledger::reconcile_time, 0);
-
- // Short retries - on
- ledger::short_retries = false;
- ASSERT_FALSE(ledger::short_retries);
- RewardsServiceImpl::HandleFlags("short-retries=true");
- ASSERT_TRUE(ledger::short_retries);
-
- // Short retries - off
- ledger::short_retries = true;
- ASSERT_TRUE(ledger::short_retries);
- RewardsServiceImpl::HandleFlags("short-retries=false");
- ASSERT_FALSE(ledger::short_retries);
-
- // Mixture of flags
- ASSERT_FALSE(ledger::short_retries);
- ASSERT_TRUE(ledger::is_production);
- ASSERT_EQ(ledger::reconcile_time, 0);
- RewardsServiceImpl::HandleFlags(
- "staging=true,short-retries=true,reconcile-interval=10");
- ASSERT_TRUE(ledger::short_retries);
- ASSERT_FALSE(ledger::is_production);
- ASSERT_EQ(ledger::reconcile_time, 10);
-
- // Wrong input
- ledger::short_retries = false;
- ledger::reconcile_time = 0;
- ledger::is_production = true;
- ASSERT_FALSE(ledger::short_retries);
- ASSERT_TRUE(ledger::is_production);
- ASSERT_EQ(ledger::reconcile_time, 0);
- RewardsServiceImpl::HandleFlags(
- "staging=,shortretries=true,reconcile-interval");
- ASSERT_FALSE(ledger::short_retries);
- ASSERT_TRUE(ledger::is_production);
- ASSERT_EQ(ledger::reconcile_time, 0);
-}
-
TEST_F(RewardsServiceTest, OnWalletProperties) {
// We always need to call observer as we report errors back even when we have null pointer
EXPECT_CALL(*observer(), OnWalletProperties(_, 1, _)).Times(1);
rewards_service()->OnWalletProperties(ledger::Result::LEDGER_ERROR, nullptr);
}
-TEST_F(RewardsServiceTest, OnLoad) {
- EXPECT_CALL(*ledger_client(), OnLoad(_, _)).Times(1);
- rewards_service()->OnLoad(SessionID::FromSerializedValue(1),
- GURL("https://brave.com"));
-}
-
// add test for strange entries
diff --git a/components/brave_rewards/browser/rewards_service_observer.h b/components/brave_rewards/browser/rewards_service_observer.h
index aba4195d9461..30e824b34752 100644
--- a/components/brave_rewards/browser/rewards_service_observer.h
+++ b/components/brave_rewards/browser/rewards_service_observer.h
@@ -6,6 +6,7 @@
#define BRAVE_BROWSER_PAYMENTS_PAYMENTS_SERVICE_OBSERVER_H_
#include "base/observer_list_types.h"
+#include "brave/components/brave_rewards/browser/balance_report.h"
#include "brave/components/brave_rewards/browser/content_site.h"
#include "brave/components/brave_rewards/browser/grant.h"
#include "brave/components/brave_rewards/browser/publisher_banner.h"
diff --git a/components/brave_rewards/browser/test_util.cc b/components/brave_rewards/browser/test_util.cc
index 26bc1dfe4a06..37802de52878 100644
--- a/components/brave_rewards/browser/test_util.cc
+++ b/components/brave_rewards/browser/test_util.cc
@@ -17,10 +17,6 @@
namespace brave_rewards {
-MockLedgerClient::MockLedgerClient() {}
-
-MockLedgerClient::~MockLedgerClient() {}
-
std::unique_ptr CreateBraveRewardsProfile(const base::FilePath& path) {
// Bitmap fetcher service needed for rewards service
BitmapFetcherServiceFactory::GetInstance();
diff --git a/components/brave_rewards/browser/test_util.h b/components/brave_rewards/browser/test_util.h
index f3bd8c6ac3df..ecbd65934593 100644
--- a/components/brave_rewards/browser/test_util.h
+++ b/components/brave_rewards/browser/test_util.h
@@ -5,216 +5,15 @@
#ifndef BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_TEST_UTIL_H_
#define BRAVE_COMPONENTS_BRAVE_REWARDS_BROWSER_TEST_UTIL_H_
-#include