Skip to content

Commit

Permalink
Migrates ledger_state to preferences
Browse files Browse the repository at this point in the history
  • Loading branch information
NejcZdovc committed May 31, 2020
1 parent 653a268 commit 1228da2
Show file tree
Hide file tree
Showing 58 changed files with 553 additions and 580 deletions.
23 changes: 11 additions & 12 deletions browser/brave_rewards/extension_rewards_service_observer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,20 +30,19 @@ void ExtensionRewardsServiceObserver::OnWalletInitialized(
int32_t result) {
auto* event_router = extensions::EventRouter::Get(profile_);

auto converted_result = static_cast<ledger::Result>(result);
if (!event_router) {
return;
}

// Don't report back if there is no ledger file
if (event_router && converted_result != ledger::Result::NO_LEDGER_STATE) {
std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletInitialized::Create(
result).release());
std::unique_ptr<base::ListValue> args(
extensions::api::brave_rewards::OnWalletInitialized::Create(
result).release());

std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnWalletInitialized::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}
std::unique_ptr<extensions::Event> event(new extensions::Event(
extensions::events::BRAVE_START,
extensions::api::brave_rewards::OnWalletInitialized::kEventName,
std::move(args)));
event_router->BroadcastEvent(std::move(event));
}

void ExtensionRewardsServiceObserver::OnWalletProperties(
Expand Down
2 changes: 1 addition & 1 deletion browser/extensions/api/brave_rewards_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -629,7 +629,7 @@ BraveRewardsGetACEnabledFunction::Run() {
return RespondNow(Error("Rewards service is not initialized"));
}

rewards_service->GetAutoContribute(base::BindOnce(
rewards_service->GetAutoContributeEnabled(base::BindOnce(
&BraveRewardsGetACEnabledFunction::OnGetACEnabled,
this));
return RespondLater();
Expand Down
4 changes: 2 additions & 2 deletions browser/importer/brave_profile_writer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -272,8 +272,8 @@ void BraveProfileWriter::SetWalletProperties(brave_rewards::RewardsService*
new_contribution_amount_ = minimum_monthly_contribution;
}
}
rewards_service->SetContributionAmount(new_contribution_amount_);
rewards_service->SetAutoContribute(auto_contribute_enabled);
rewards_service->SetAutoContributionAmount(new_contribution_amount_);
rewards_service->SetAutoContributeEnabled(auto_contribute_enabled);
}

void BraveProfileWriter::UpdateLedger(const BraveLedger& ledger) {
Expand Down
13 changes: 6 additions & 7 deletions browser/ui/webui/brave_rewards_page_ui.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class RewardsDOMHandler : public WebUIMessageHandler,
void RestorePublishers(const base::ListValue* args);
void RestorePublisher(const base::ListValue* args);
void WalletExists(const base::ListValue* args);
void GetContributionAmount(const base::ListValue* args);
void GetAutoContributionAmount(const base::ListValue* args);
void RemoveRecurringTip(const base::ListValue* args);
void GetRecurringTips(const base::ListValue* args);
void GetOneTimeTips(const base::ListValue* args);
Expand Down Expand Up @@ -369,7 +369,7 @@ void RewardsDOMHandler::RegisterMessages() {
base::BindRepeating(&RewardsDOMHandler::WalletExists,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.getContributionAmount",
base::BindRepeating(&RewardsDOMHandler::GetContributionAmount,
base::BindRepeating(&RewardsDOMHandler::GetAutoContributionAmount,
base::Unretained(this)));
web_ui()->RegisterMessageCallback("brave_rewards.removeRecurringTip",
base::BindRepeating(&RewardsDOMHandler::RemoveRecurringTip,
Expand Down Expand Up @@ -842,8 +842,7 @@ void RewardsDOMHandler::SaveSetting(const base::ListValue* args) {
}

if (key == "contributionMonthly") {
rewards_service_->SetUserChangedContribution();
rewards_service_->SetContributionAmount(std::stod(value));
rewards_service_->SetAutoContributionAmount(std::stod(value));
}

if (key == "contributionMinTime") {
Expand Down Expand Up @@ -875,7 +874,7 @@ void RewardsDOMHandler::SaveSetting(const base::ListValue* args) {
}

if (key == "enabledContribute") {
rewards_service_->SetAutoContribute(value == "true");
rewards_service_->SetAutoContributeEnabled(value == "true");
}
}
}
Expand Down Expand Up @@ -979,9 +978,9 @@ void RewardsDOMHandler::OnGetContributionAmount(double amount) {
}
}

void RewardsDOMHandler::GetContributionAmount(const base::ListValue* args) {
void RewardsDOMHandler::GetAutoContributionAmount(const base::ListValue* args) {
if (rewards_service_)
rewards_service_->GetContributionAmount(
rewards_service_->GetAutoContributionAmount(
base::Bind(&RewardsDOMHandler::OnGetContributionAmount,
weak_factory_.GetWeakPtr()));
}
Expand Down
1 change: 0 additions & 1 deletion components/brave_ads/browser/ads_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,6 @@ class BraveAdsBrowserTest
int32_t result) {
const auto converted_result = static_cast<ledger::Result>(result);
ASSERT_TRUE(converted_result == ledger::Result::WALLET_CREATED ||
converted_result == ledger::Result::NO_LEDGER_STATE ||
converted_result == ledger::Result::LEDGER_OK);
wallet_initialized_ = true;
if (wait_for_wallet_initialization_loop_)
Expand Down
13 changes: 6 additions & 7 deletions components/brave_ads/browser/ads_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,10 @@ class MockRewardsService : public RewardsService {
MOCK_METHOD1(GetPublisherAllowVideos,
void(const brave_rewards::GetPublisherAllowVideosCallback&));
MOCK_CONST_METHOD1(SetPublisherAllowVideos, void(bool));
MOCK_CONST_METHOD1(SetContributionAmount, void(double));
MOCK_CONST_METHOD0(SetUserChangedContribution, void());
MOCK_METHOD1(GetAutoContribute,
void(brave_rewards::GetAutoContributeCallback));
MOCK_METHOD1(SetAutoContribute, void(bool));
MOCK_CONST_METHOD1(SetAutoContributionAmount, void(double));
MOCK_METHOD1(GetAutoContributeEnabled,
void(brave_rewards::GetAutoContributeEnabledCallback));
MOCK_METHOD1(SetAutoContributeEnabled, void(bool));
MOCK_CONST_METHOD0(UpdateAdsRewards, void());
MOCK_METHOD2(SetTimer, void(uint64_t, uint32_t*));
MOCK_METHOD1(IsWalletCreated,
Expand All @@ -101,8 +100,8 @@ class MockRewardsService : public RewardsService {
const std::string&,
const std::string&,
const std::string&));
MOCK_METHOD1(GetContributionAmount,
void(const brave_rewards::GetContributionAmountCallback&));
MOCK_METHOD1(GetAutoContributionAmount,
void(const brave_rewards::GetAutoContributionAmountCallback&));
MOCK_METHOD2(GetPublisherBanner,
void(const std::string&,
brave_rewards::GetPublisherBannerCallback));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ class RewardsDatabaseBrowserTest
int32_t result) override {
const auto converted_result = static_cast<ledger::Result>(result);
ASSERT_TRUE(converted_result == ledger::Result::WALLET_CREATED ||
converted_result == ledger::Result::NO_LEDGER_STATE ||
converted_result == ledger::Result::LEDGER_OK);
wallet_initialized_ = true;
if (wait_for_wallet_initialization_loop_) {
Expand Down
5 changes: 4 additions & 1 deletion components/brave_rewards/browser/rewards_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kRewardsUserHasFunded, false);
registry->RegisterTimePref(prefs::kRewardsAddFundsNotification, base::Time());
registry->RegisterBooleanPref(prefs::kBraveRewardsEnabled, false);
registry->RegisterBooleanPref(prefs::kBraveRewardsEnabledMigrated, false);
registry->RegisterDictionaryPref(prefs::kRewardsExternalWallets);
registry->RegisterUint64Pref(prefs::kStateServerPublisherListStamp, 0ull);
registry->RegisterStringPref(prefs::kStateUpholdAnonAddress, "");
Expand All @@ -74,6 +73,10 @@ void RewardsService::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(prefs::kStateAllowVideoContribution, true);
registry->RegisterDoublePref(prefs::kStateScoreA, 0.0);
registry->RegisterDoublePref(prefs::kStateScoreB, 0.0);
registry->RegisterBooleanPref(prefs::kStateAutoContributeEnabled, false);
registry->RegisterBooleanPref(prefs::kStateAutoContributeAmount, 0.0);
registry->RegisterUint64Pref(prefs::kStateNextReconcileStamp, 0ull);
registry->RegisterUint64Pref(prefs::kStateCreationStamp, 0ull);
}

} // namespace brave_rewards
17 changes: 8 additions & 9 deletions components/brave_rewards/browser/rewards_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,14 @@ class RewardsServicePrivateObserver;
using GetContentSiteListCallback =
base::Callback<void(std::unique_ptr<ContentSiteList>)>;
using GetWalletPassphraseCallback = base::Callback<void(const std::string&)>;
using GetContributionAmountCallback = base::Callback<void(double)>;
using GetAutoContributionAmountCallback = base::Callback<void(double)>;
using GetAutoContributePropsCallback = base::Callback<void(
std::unique_ptr<brave_rewards::AutoContributeProps>)>;
using GetPublisherMinVisitTimeCallback = base::Callback<void(int)>;
using GetPublisherMinVisitsCallback = base::Callback<void(int)>;
using GetPublisherAllowNonVerifiedCallback = base::Callback<void(bool)>;
using GetPublisherAllowVideosCallback = base::Callback<void(bool)>;
using GetAutoContributeCallback = base::OnceCallback<void(bool)>;
using GetAutoContributeEnabledCallback = base::OnceCallback<void(bool)>;
using GetReconcileStampCallback = base::Callback<void(uint64_t)>;
using IsWalletCreatedCallback = base::Callback<void(bool)>;
using GetPendingContributionsTotalCallback = base::Callback<void(double)>;
Expand Down Expand Up @@ -185,11 +185,10 @@ class RewardsService : public KeyedService {
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 void GetAutoContribute(
GetAutoContributeCallback callback) = 0;
virtual void SetAutoContribute(bool enabled) = 0;
virtual void SetAutoContributionAmount(double amount) const = 0;
virtual void GetAutoContributeEnabled(
GetAutoContributeEnabledCallback callback) = 0;
virtual void SetAutoContributeEnabled(bool enabled) = 0;
virtual void UpdateAdsRewards() const = 0;
virtual void SetTimer(uint64_t time_offset, uint32_t* timer_id) = 0;
virtual void GetBalanceReport(
Expand All @@ -202,8 +201,8 @@ class RewardsService : public KeyedService {
const std::string& url,
const std::string& favicon_url,
const std::string& publisher_blob) = 0;
virtual void GetContributionAmount(
const GetContributionAmountCallback& callback) = 0;
virtual void GetAutoContributionAmount(
const GetAutoContributionAmountCallback& callback) = 0;
virtual void GetPublisherBanner(const std::string& publisher_id,
GetPublisherBannerCallback callback) = 0;
virtual void OnTip(
Expand Down
41 changes: 20 additions & 21 deletions components/brave_rewards/browser/rewards_service_browsertest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -580,9 +580,9 @@ class BraveRewardsBrowserTest
return false;
}

void GetReconcileTime() {
rewards_service()->GetReconcileTime(
base::Bind(&BraveRewardsBrowserTest::OnGetReconcileTime,
void GetReconcileInterval() {
rewards_service()->GetReconcileInterval(
base::Bind(&BraveRewardsBrowserTest::OnGetReconcileInterval,
base::Unretained(this)));
}

Expand Down Expand Up @@ -1160,7 +1160,6 @@ class BraveRewardsBrowserTest
int32_t result) {
const auto converted_result = static_cast<ledger::Result>(result);
ASSERT_TRUE(converted_result == ledger::Result::WALLET_CREATED ||
converted_result == ledger::Result::NO_LEDGER_STATE ||
converted_result == ledger::Result::LEDGER_OK);
wallet_initialized_ = true;
if (wait_for_wallet_initialization_loop_)
Expand Down Expand Up @@ -1383,7 +1382,7 @@ class BraveRewardsBrowserTest

MOCK_METHOD1(OnGetEnvironment, void(ledger::Environment));
MOCK_METHOD1(OnGetDebug, void(bool));
MOCK_METHOD1(OnGetReconcileTime, void(int32_t));
MOCK_METHOD1(OnGetReconcileInterval, void(int32_t));
MOCK_METHOD1(OnGetShortRetries, void(bool));

std::unique_ptr<net::EmbeddedTestServer> https_server_;
Expand Down Expand Up @@ -1638,26 +1637,26 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
RunUntilIdle();

// positive number
EXPECT_CALL(*this, OnGetReconcileTime(10));
EXPECT_CALL(*this, OnGetReconcileInterval(10));
// negative number and string
EXPECT_CALL(*this, OnGetReconcileTime(0)).Times(2);
EXPECT_CALL(*this, OnGetReconcileInterval(0)).Times(2);

// Reconcile interval - positive number
rewards_service()->SetReconcileTime(0);
rewards_service()->SetReconcileInterval(0);
rewards_service()->HandleFlags("reconcile-interval=10");
GetReconcileTime();
GetReconcileInterval();
RunUntilIdle();

// Reconcile interval - negative number
rewards_service()->SetReconcileTime(0);
rewards_service()->SetReconcileInterval(0);
rewards_service()->HandleFlags("reconcile-interval=-1");
GetReconcileTime();
GetReconcileInterval();
RunUntilIdle();

// Reconcile interval - string
rewards_service()->SetReconcileTime(0);
rewards_service()->SetReconcileInterval(0);
rewards_service()->HandleFlags("reconcile-interval=sdf");
GetReconcileTime();
GetReconcileInterval();
RunUntilIdle();

EXPECT_CALL(*this, OnGetShortRetries(true)); // on
Expand All @@ -1679,18 +1678,18 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsSingleArg) {
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) {
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::STAGING));
EXPECT_CALL(*this, OnGetDebug(true));
EXPECT_CALL(*this, OnGetReconcileTime(10));
EXPECT_CALL(*this, OnGetReconcileInterval(10));
EXPECT_CALL(*this, OnGetShortRetries(true));

rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->SetDebug(true);
rewards_service()->SetReconcileTime(0);
rewards_service()->SetReconcileInterval(0);
rewards_service()->SetShortRetries(false);

rewards_service()->HandleFlags(
"staging=true,debug=true,short-retries=true,reconcile-interval=10");

GetReconcileTime();
GetReconcileInterval();
GetShortRetries();
GetEnvironment();
GetDebug();
Expand All @@ -1700,18 +1699,18 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsMultipleFlags) {
IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, HandleFlagsWrongInput) {
EXPECT_CALL(*this, OnGetEnvironment(ledger::Environment::PRODUCTION));
EXPECT_CALL(*this, OnGetDebug(false));
EXPECT_CALL(*this, OnGetReconcileTime(0));
EXPECT_CALL(*this, OnGetReconcileInterval(0));
EXPECT_CALL(*this, OnGetShortRetries(false));

rewards_service()->SetEnvironment(ledger::Environment::PRODUCTION);
rewards_service()->SetDebug(false);
rewards_service()->SetReconcileTime(0);
rewards_service()->SetReconcileInterval(0);
rewards_service()->SetShortRetries(false);

rewards_service()->HandleFlags(
"staging=,debug=,shortretries=true,reconcile-interval");

GetReconcileTime();
GetReconcileInterval();
GetShortRetries();
GetDebug();
GetEnvironment();
Expand Down Expand Up @@ -2695,7 +2694,7 @@ IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, PanelDontDoRequests) {

IN_PROC_BROWSER_TEST_F(BraveRewardsBrowserTest, ShowMonthlyIfACOff) {
EnableRewardsViaCode();
rewards_service_->SetAutoContribute(false);
rewards_service_->SetAutoContributeEnabled(false);

GURL url = https_server()->GetURL("3zsistemi.si", "/");
ui_test_utils::NavigateToURLWithDisposition(
Expand Down Expand Up @@ -2744,7 +2743,7 @@ IN_PROC_BROWSER_TEST_F(
VisitPublisher("3zsistemi.si", true);

// 30 form unblinded and 20 from uphold
rewards_service()->SetContributionAmount(50.0);
rewards_service()->SetAutoContributionAmount(50.0);

ledger::SKUOrderItemList items;
auto item = ledger::SKUOrderItem::New();
Expand Down
Loading

0 comments on commit 1228da2

Please sign in to comment.