diff --git a/browser/brave_rewards/extension_rewards_service_observer.cc b/browser/brave_rewards/extension_rewards_service_observer.cc index 8ad46e4eb045..d9e2fa95f919 100644 --- a/browser/brave_rewards/extension_rewards_service_observer.cc +++ b/browser/brave_rewards/extension_rewards_service_observer.cc @@ -30,20 +30,19 @@ void ExtensionRewardsServiceObserver::OnWalletInitialized( int32_t result) { auto* event_router = extensions::EventRouter::Get(profile_); - auto converted_result = static_cast(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 args( - extensions::api::brave_rewards::OnWalletInitialized::Create( - result).release()); + std::unique_ptr args( + extensions::api::brave_rewards::OnWalletInitialized::Create( + result).release()); - std::unique_ptr 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 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( diff --git a/browser/extensions/api/brave_rewards_api.cc b/browser/extensions/api/brave_rewards_api.cc index 9cbf5a43366f..f14579f2f29a 100644 --- a/browser/extensions/api/brave_rewards_api.cc +++ b/browser/extensions/api/brave_rewards_api.cc @@ -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(); diff --git a/browser/importer/brave_profile_writer.cc b/browser/importer/brave_profile_writer.cc index 68cd0c32b7e8..7192c4270881 100644 --- a/browser/importer/brave_profile_writer.cc +++ b/browser/importer/brave_profile_writer.cc @@ -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) { diff --git a/browser/ui/webui/brave_rewards_page_ui.cc b/browser/ui/webui/brave_rewards_page_ui.cc index f59083769a50..dabcc1baeee2 100644 --- a/browser/ui/webui/brave_rewards_page_ui.cc +++ b/browser/ui/webui/brave_rewards_page_ui.cc @@ -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); @@ -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, @@ -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") { @@ -875,7 +874,7 @@ void RewardsDOMHandler::SaveSetting(const base::ListValue* args) { } if (key == "enabledContribute") { - rewards_service_->SetAutoContribute(value == "true"); + rewards_service_->SetAutoContributeEnabled(value == "true"); } } } @@ -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())); } diff --git a/components/brave_ads/browser/ads_service_browsertest.cc b/components/brave_ads/browser/ads_service_browsertest.cc index ff47c334ddf4..781aa9a45e00 100644 --- a/components/brave_ads/browser/ads_service_browsertest.cc +++ b/components/brave_ads/browser/ads_service_browsertest.cc @@ -185,7 +185,6 @@ class BraveAdsBrowserTest int32_t result) { const auto converted_result = static_cast(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_) diff --git a/components/brave_ads/browser/ads_service_impl_unittest.cc b/components/brave_ads/browser/ads_service_impl_unittest.cc index df16624a17a0..94c4ad69b97f 100644 --- a/components/brave_ads/browser/ads_service_impl_unittest.cc +++ b/components/brave_ads/browser/ads_service_impl_unittest.cc @@ -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, @@ -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)); diff --git a/components/brave_rewards/browser/rewards_database_browsertest.cc b/components/brave_rewards/browser/rewards_database_browsertest.cc index ca2ecfa54a2d..cd25cb1786e0 100644 --- a/components/brave_rewards/browser/rewards_database_browsertest.cc +++ b/components/brave_rewards/browser/rewards_database_browsertest.cc @@ -133,7 +133,6 @@ class RewardsDatabaseBrowserTest int32_t result) override { const auto converted_result = static_cast(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_) { diff --git a/components/brave_rewards/browser/rewards_service.cc b/components/brave_rewards/browser/rewards_service.cc index 57297aab74c2..d746e2cc44c1 100644 --- a/components/brave_rewards/browser/rewards_service.cc +++ b/components/brave_rewards/browser/rewards_service.cc @@ -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, ""); @@ -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 diff --git a/components/brave_rewards/browser/rewards_service.h b/components/brave_rewards/browser/rewards_service.h index 0bbee030c88c..48ebcfbcace7 100644 --- a/components/brave_rewards/browser/rewards_service.h +++ b/components/brave_rewards/browser/rewards_service.h @@ -54,14 +54,14 @@ class RewardsServicePrivateObserver; using GetContentSiteListCallback = base::Callback)>; using GetWalletPassphraseCallback = base::Callback; -using GetContributionAmountCallback = base::Callback; +using GetAutoContributionAmountCallback = 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::OnceCallback; +using GetAutoContributeEnabledCallback = base::OnceCallback; using GetReconcileStampCallback = base::Callback; using IsWalletCreatedCallback = base::Callback; using GetPendingContributionsTotalCallback = base::Callback; @@ -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( @@ -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( diff --git a/components/brave_rewards/browser/rewards_service_browsertest.cc b/components/brave_rewards/browser/rewards_service_browsertest.cc index 0c64fecb805e..21dcdca3a70b 100644 --- a/components/brave_rewards/browser/rewards_service_browsertest.cc +++ b/components/brave_rewards/browser/rewards_service_browsertest.cc @@ -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))); } @@ -1160,7 +1160,6 @@ class BraveRewardsBrowserTest int32_t result) { const auto converted_result = static_cast(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_) @@ -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 https_server_; @@ -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 @@ -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(); @@ -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(); @@ -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( @@ -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(); diff --git a/components/brave_rewards/browser/rewards_service_impl.cc b/components/brave_rewards/browser/rewards_service_impl.cc index 4c245020cd9f..998ae8be6dc2 100644 --- a/components/brave_rewards/browser/rewards_service_impl.cc +++ b/components/brave_rewards/browser/rewards_service_impl.cc @@ -146,7 +146,6 @@ std::pair LoadStateOnFileTaskRunner( // Make sure the file isn't empty. if (!success || data.empty()) { - VLOG(0) << "Failed to read file: " << path.MaybeAsASCII(); return {}; } std::pair result; @@ -197,9 +196,9 @@ std::string LoadOnFileTaskRunner(const base::FilePath& path) { // Make sure the file isn't empty. if (!success || data.empty()) { - VLOG(0) << "Failed to read file: " << path.MaybeAsASCII(); - return std::string(); + return ""; } + return data; } @@ -737,7 +736,6 @@ void RewardsServiceImpl::Shutdown() { void RewardsServiceImpl::OnWalletInitialized(ledger::Result result) { if (result == ledger::Result::WALLET_CREATED || - result == ledger::Result::NO_LEDGER_STATE || result == ledger::Result::LEDGER_OK) { is_wallet_initialized_ = true; } @@ -746,8 +744,6 @@ void RewardsServiceImpl::OnWalletInitialized(ledger::Result result) { ready_.Signal(); if (result == ledger::Result::WALLET_CREATED) { - SetRewardsMainEnabled(true); - SetAutoContribute(true); StartNotificationTimers(true); // Record P3A: @@ -885,11 +881,6 @@ void RewardsServiceImpl::OnLedgerStateLoaded( return; if (state.second.is_dict()) { - // Extract some properties from the parsed json. - base::Value* auto_contribute = state.second.FindKey("auto_contribute"); - if (auto_contribute && auto_contribute->is_bool()) { - auto_contributions_enabled_ = auto_contribute->GetBool(); - } // Record stats. RecordBackendP3AStats(); MaybeRecordInitialAdsP3AState(profile_->GetPrefs()); @@ -930,39 +921,6 @@ void RewardsServiceImpl::OnPublisherStateLoaded( data); } -void RewardsServiceImpl::SaveLedgerState( - const std::string& ledger_state, - ledger::ResultCallback callback) { - if (reset_states_) { - return; - } - base::ImportantFileWriter writer( - ledger_state_path_, file_task_runner_); - - writer.RegisterOnNextWriteCallbacks( - base::Closure(), - base::Bind( - &PostWriteCallback, - base::Bind(&RewardsServiceImpl::OnLedgerStateSaved, - AsWeakPtr(), - callback), - base::SequencedTaskRunnerHandle::Get())); - - writer.WriteNow(std::make_unique(ledger_state)); -} - -void RewardsServiceImpl::OnLedgerStateSaved( - ledger::ResultCallback callback, - bool success) { - if (!Connected()) { - return; - } - - callback(success - ? ledger::Result::LEDGER_OK - : ledger::Result::NO_LEDGER_STATE); -} - void RewardsServiceImpl::LoadNicewareList( ledger::GetNicewareListCallback callback) { if (!Connected()) @@ -1392,18 +1350,19 @@ void RewardsServiceImpl::SetRewardsMainEnabled(bool enabled) { return; } - if (!enabled) { - RecordRewardsDisabledForSomeMetrics(); - } - SetRewardsMainEnabledPref(enabled); bat_ledger_->SetRewardsMainEnabled(enabled); TriggerOnRewardsMainEnabled(enabled); + #if BUILDFLAG(ENABLE_GREASELION) if (greaselion_service_) { greaselion_service_->SetFeatureEnabled(greaselion::REWARDS, enabled); greaselion_service_->SetFeatureEnabled(greaselion::TWITTER_TIPS, enabled); } #endif + + if (!enabled) { + RecordRewardsDisabledForSomeMetrics(); + } } void RewardsServiceImpl::GetRewardsMainEnabled( @@ -1415,16 +1374,6 @@ void RewardsServiceImpl::GetRewardsMainEnabled( bat_ledger_->GetRewardsMainEnabled(callback); } -void RewardsServiceImpl::SetRewardsMainEnabledPref(bool enabled) { - profile_->GetPrefs()->SetBoolean(prefs::kBraveRewardsEnabled, enabled); - SetRewardsMainEnabledMigratedPref(true); -} - -void RewardsServiceImpl::SetRewardsMainEnabledMigratedPref(bool enabled) { - profile_->GetPrefs()->SetBoolean( - prefs::kBraveRewardsEnabledMigrated, true); -} - void RewardsServiceImpl::SetCatalogIssuers(const std::string& json) { if (!Connected()) { return; @@ -1766,45 +1715,30 @@ void RewardsServiceImpl::SetPublisherAllowVideos(bool allow) const { bat_ledger_->SetPublisherAllowVideos(allow); } -void RewardsServiceImpl::SetContributionAmount(const double amount) const { +void RewardsServiceImpl::SetAutoContributionAmount(const double amount) const { if (!Connected()) { return; } - bat_ledger_->SetUserChangedContribution(); - bat_ledger_->SetContributionAmount(amount); + bat_ledger_->SetAutoContributionAmount(amount); } -// TODO(brave): 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 { +void RewardsServiceImpl::GetAutoContributeEnabled( + GetAutoContributeEnabledCallback callback) { if (!Connected()) { return; } - bat_ledger_->SetUserChangedContribution(); + bat_ledger_->GetAutoContributeEnabled(std::move(callback)); } -void RewardsServiceImpl::GetAutoContribute( - GetAutoContributeCallback callback) { +void RewardsServiceImpl::SetAutoContributeEnabled(bool enabled) { if (!Connected()) { return; } - bat_ledger_->GetAutoContribute(std::move(callback)); -} - -void RewardsServiceImpl::SetAutoContribute(bool enabled) { - if (!Connected()) { - return; - } - - bat_ledger_->SetAutoContribute(enabled); - auto_contributions_enabled_ = enabled; + bat_ledger_->SetAutoContributeEnabled(enabled); - // Record stats. - DCHECK(profile_->GetPrefs()->GetBoolean(prefs::kBraveRewardsEnabled)); if (!enabled) { // Just record the disabled state. RecordAutoContributionsState( @@ -1956,12 +1890,12 @@ void RewardsServiceImpl::OnPanelPublisherInfo( windowId); } -void RewardsServiceImpl::GetContributionAmount( - const GetContributionAmountCallback& callback) { +void RewardsServiceImpl::GetAutoContributionAmount( + const GetAutoContributionAmountCallback& callback) { if (!Connected()) return; - bat_ledger_->GetContributionAmount(callback); + bat_ledger_->GetAutoContributionAmount(callback); } void RewardsServiceImpl::FetchFavIcon(const std::string& url, @@ -2260,7 +2194,7 @@ void RewardsServiceImpl::OnNotificationTimerFired() { if (!Connected()) return; - bat_ledger_->GetBootStamp( + bat_ledger_->GetCreationStamp( base::BindOnce(&RewardsServiceImpl::MaybeShowBackupNotification, AsWeakPtr())); GetReconcileStamp( @@ -2309,7 +2243,7 @@ void RewardsServiceImpl::ShowNotificationAddFunds(bool sufficient) { } void RewardsServiceImpl::MaybeShowNotificationTipsPaid() { - GetAutoContribute(base::BindOnce( + GetAutoContributeEnabled(base::BindOnce( &RewardsServiceImpl::ShowNotificationTipsPaid, AsWeakPtr())); } @@ -2392,7 +2326,7 @@ void RewardsServiceImpl::HandleFlags(const std::string& options) { bool success = base::StringToInt(value, &reconcile_int); if (success && reconcile_int > 0) { - SetReconcileTime(reconcile_int); + SetReconcileInterval(reconcile_int); } continue; @@ -2539,9 +2473,9 @@ void RewardsServiceImpl::GetDebug(const GetDebugCallback& callback) { bat_ledger_service_->GetDebug(callback); } -void RewardsServiceImpl::GetReconcileTime( - const GetReconcileTimeCallback& callback) { - bat_ledger_service_->GetReconcileTime(callback); +void RewardsServiceImpl::GetReconcileInterval( + GetReconcileIntervalCallback callback) { + bat_ledger_service_->GetReconcileInterval(std::move(callback)); } void RewardsServiceImpl::GetShortRetries( @@ -2557,8 +2491,8 @@ void RewardsServiceImpl::SetDebug(bool debug) { bat_ledger_service_->SetDebug(debug); } -void RewardsServiceImpl::SetReconcileTime(int32_t time) { - bat_ledger_service_->SetReconcileTime(time); +void RewardsServiceImpl::SetReconcileInterval(const int32_t interval) { + bat_ledger_service_->SetReconcileInterval(interval); } void RewardsServiceImpl::SetShortRetries(bool short_retries) { @@ -3194,11 +3128,21 @@ void RewardsServiceImpl::OnRecordBackendP3AStatsContributions( queued_recurring = recurring_donation_size; } - const auto auto_contributions_state = auto_contributions_enabled_ ? + RecordTipsState(true, true, tips, queued_recurring); + + GetAutoContributeEnabled(base::BindOnce( + &RewardsServiceImpl::OnRecordBackendP3AStatsAC, + AsWeakPtr(), + auto_contributions)); +} + +void RewardsServiceImpl::OnRecordBackendP3AStatsAC( + const int auto_contributions, + bool ac_enabled) { + const auto auto_contributions_state = ac_enabled ? AutoContributionsP3AState::kAutoContributeOn : AutoContributionsP3AState::kWalletCreatedAutoContributeOff; RecordAutoContributionsState(auto_contributions_state, auto_contributions); - RecordTipsState(true, true, tips, queued_recurring); } #if defined(OS_ANDROID) diff --git a/components/brave_rewards/browser/rewards_service_impl.h b/components/brave_rewards/browser/rewards_service_impl.h index e0aa402b791b..fb651c7c8e53 100644 --- a/components/brave_rewards/browser/rewards_service_impl.h +++ b/components/brave_rewards/browser/rewards_service_impl.h @@ -75,7 +75,7 @@ class BraveRewardsBrowserTest; using GetEnvironmentCallback = base::Callback; using GetDebugCallback = base::Callback; -using GetReconcileTimeCallback = base::Callback; +using GetReconcileIntervalCallback = base::Callback; using GetShortRetriesCallback = base::Callback; using GetTestResponseCallback = base::Callback& callback) override; - void SetContributionAmount(const double amount) const override; + void SetAutoContributionAmount(const double amount) const override; void SaveInlineMediaInfo( const std::string& media_type, @@ -299,7 +299,7 @@ class RewardsServiceImpl : public RewardsService, void GetAnonWalletStatus(GetAnonWalletStatusCallback callback) override; - void SetAutoContribute(bool enabled) override; + void SetAutoContributeEnabled(bool enabled) override; void GetMonthlyReport( const uint32_t month, @@ -331,9 +331,6 @@ class RewardsServiceImpl : public RewardsService, void OnCreateWallet(CreateWalletCallback callback, ledger::Result result); - void OnLedgerStateSaved( - ledger::ResultCallback callback, - bool success); void OnLedgerStateLoaded(ledger::OnLoadCallback callback, std::pair data); void LoadNicewareList(ledger::GetNicewareListCallback callback) override; @@ -483,9 +480,6 @@ class RewardsServiceImpl : public RewardsService, ledger::PromotionPtr promotion); void LoadLedgerState(ledger::OnLoadCallback callback) override; void LoadPublisherState(ledger::OnLoadCallback callback) override; - void SaveLedgerState( - const std::string& ledger_state, - ledger::ResultCallback callback) override; void SetTimer(uint64_t time_offset, uint32_t* timer_id) override; void LoadURL(const std::string& url, const std::vector& headers, @@ -497,7 +491,6 @@ class RewardsServiceImpl : public RewardsService, void SetPublisherMinVisits(int visits) const override; void SetPublisherAllowNonVerified(bool allow) const override; void SetPublisherAllowVideos(bool allow) const override; - void SetUserChangedContribution() const override; void UpdateAdsRewards() const override; void SetCatalogIssuers( const std::string& json) override; @@ -615,8 +608,6 @@ class RewardsServiceImpl : public RewardsService, ledger::AutoContributePropsPtr props); void OnGetRewardsInternalsInfo(GetRewardsInternalsInfoCallback callback, ledger::RewardsInternalsInfoPtr info); - void SetRewardsMainEnabledPref(bool enabled); - void SetRewardsMainEnabledMigratedPref(bool enabled); void OnRefreshPublisher( RefreshPublisherCallback callback, @@ -645,6 +636,10 @@ class RewardsServiceImpl : public RewardsService, const uint32_t recurring_donation_size, ledger::ContributionInfoList list); + void OnRecordBackendP3AStatsAC( + const int auto_contributions, + bool ac_enabled); + void OnGetBalanceReport( GetBalanceReportCallback callback, const ledger::Result result, @@ -714,10 +709,6 @@ class RewardsServiceImpl : public RewardsService, GetTestResponseCallback test_response_callback_; - // At the moment we keep it only for the purpose of sending P3A stats. - // Used only on UI thread. - bool auto_contributions_enabled_ = false; - DISALLOW_COPY_AND_ASSIGN(RewardsServiceImpl); }; diff --git a/components/brave_rewards/browser/rewards_state_browsertest.cc b/components/brave_rewards/browser/rewards_state_browsertest.cc index 8ff14f16a9ed..2443cf7f1fd0 100644 --- a/components/brave_rewards/browser/rewards_state_browsertest.cc +++ b/components/brave_rewards/browser/rewards_state_browsertest.cc @@ -41,6 +41,7 @@ class RewardsStateBrowserTest int32_t version = 0; GetMigrationVersionFromTest(&version); CopyPublisherFile(version); + CopyStateFile(version); return true; } @@ -102,7 +103,6 @@ class RewardsStateBrowserTest int32_t result) override { const auto converted_result = static_cast(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_) { @@ -140,7 +140,7 @@ class RewardsStateBrowserTest } void CopyPublisherFile(const int32_t version) const { - if (version > 1) { + if (version != 1) { return; } @@ -151,6 +151,18 @@ class RewardsStateBrowserTest ASSERT_TRUE(base::CopyFile(test_path, profile_path)); } + void CopyStateFile(const int32_t version) const { + if (version != 2) { + return; + } + + base::FilePath profile_path; + GetFilePath("ledger_state", &profile_path); + base::FilePath test_path; + GetTestFile("ledger_state", &test_path); + ASSERT_TRUE(base::CopyFile(test_path, profile_path)); + } + brave_rewards::RewardsServiceImpl* rewards_service_; std::unique_ptr wait_for_wallet_initialization_loop_; @@ -203,3 +215,9 @@ IN_PROC_BROWSER_TEST_F(RewardsStateBrowserTest, State_1) { EXPECT_EQ(report.one_time_donation, 5.5); })); } + +IN_PROC_BROWSER_TEST_F(RewardsStateBrowserTest, State_2) { + EXPECT_EQ( + profile_->GetPrefs()->GetBoolean("brave.rewards.enabled"), + false); +} diff --git a/components/brave_rewards/common/pref_names.cc b/components/brave_rewards/common/pref_names.cc index 8f61c9d7c28b..b778cc49eae6 100644 --- a/components/brave_rewards/common/pref_names.cc +++ b/components/brave_rewards/common/pref_names.cc @@ -10,7 +10,6 @@ namespace prefs { const char kHideBraveRewardsButton[] = "brave.hide_brave_rewards_button"; const char kBraveRewardsEnabled[] = "brave.rewards.enabled"; -const char kBraveRewardsEnabledMigrated[] = "brave.rewards.enabled_migrated"; const char kRewardsNotifications[] = "brave.rewards.notifications"; const char kRewardsNotificationTimerInterval[]= "brave.rewards.notification_timer_interval"; @@ -44,5 +43,9 @@ const char kStateAllowVideoContribution[] = "brave.rewards.ac.allow_video_contributions"; const char kStateScoreA[] = "brave.rewards.ac.score.a"; const char kStateScoreB[] = "brave.rewards.ac.score.b"; +const char kStateAutoContributeEnabled[] = "brave.rewards.ac.enabled"; +const char kStateAutoContributeAmount[] = "brave.rewards.ac.amount"; +const char kStateNextReconcileStamp[] = "brave.rewards.ac.next_reconcile_stamp"; +const char kStateCreationStamp[] = "brave.rewards.creation_stamp"; } // namespace prefs } // namespace brave_rewards diff --git a/components/brave_rewards/common/pref_names.h b/components/brave_rewards/common/pref_names.h index 7c77acfd9edb..b3df8b08be0d 100644 --- a/components/brave_rewards/common/pref_names.h +++ b/components/brave_rewards/common/pref_names.h @@ -11,7 +11,6 @@ namespace prefs { extern const char kHideBraveRewardsButton[]; extern const char kBraveRewardsEnabled[]; -extern const char kBraveRewardsEnabledMigrated[]; extern const char kRewardsNotifications[]; extern const char kRewardsNotificationTimerInterval[]; extern const char kRewardsBackupNotificationFrequency[]; @@ -36,6 +35,10 @@ extern const char kStateAllowNonVerified[]; extern const char kStateAllowVideoContribution[]; extern const char kStateScoreA[]; extern const char kStateScoreB[]; +extern const char kStateAutoContributeEnabled[]; +extern const char kStateAutoContributeAmount[]; +extern const char kStateNextReconcileStamp[]; +extern const char kStateCreationStamp[]; extern const char kUseRewardsStagingServer[]; } // namespace prefs diff --git a/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts b/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts index d2c6afcf49cd..b153ab64a35e 100644 --- a/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts +++ b/components/brave_rewards/resources/extension/brave_rewards/background/reducers/rewards_panel_reducer.ts @@ -52,6 +52,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a state.walletCorrupted = false break case types.ON_WALLET_INITIALIZED: { + console.log(payload) const result: RewardsExtension.Result = payload.result state = { ...state } if (result === RewardsExtension.Result.WALLET_CREATED) { @@ -59,6 +60,7 @@ export const rewardsPanelReducer = (state: RewardsExtension.State | undefined, a state.walletCreateFailed = false state.walletCreating = false state.walletCorrupted = false + state.enabledMain = true chrome.braveRewards.saveAdsSetting('adsEnabled', 'true') chrome.storage.local.get(['is_dismissed'], function (result) { if (result && result['is_dismissed'] === 'false') { diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc index bdad00aa64e6..39010e50d228 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.cc @@ -149,25 +149,6 @@ void BatLedgerClientMojoProxy::LoadPublisherState( AsWeakPtr(), std::move(callback))); } -void OnSaveLedgerState( - ledger::ResultCallback callback, - const ledger::Result result) { - callback(result); -} - -void BatLedgerClientMojoProxy::SaveLedgerState( - const std::string& ledger_state, - ledger::ResultCallback callback) { - if (!Connected()) { - callback(ledger::Result::LEDGER_ERROR); - return; - } - - bat_ledger_client_->SaveLedgerState( - ledger_state, - base::BindOnce(&OnSaveLedgerState, std::move(callback))); -} - void BatLedgerClientMojoProxy::SetTimer(uint64_t time_offset, uint32_t* timer_id) { if (!Connected()) { diff --git a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h index ff6fdde18f04..d9507b1869b9 100644 --- a/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/bat_ledger_client_mojo_proxy.h @@ -36,9 +36,6 @@ class BatLedgerClientMojoProxy : public ledger::LedgerClient, const ledger::RewardsType type) override; void LoadLedgerState(ledger::OnLoadCallback callback) override; void LoadPublisherState(ledger::OnLoadCallback callback) override; - void SaveLedgerState( - const std::string& ledger_state, - ledger::ResultCallback callback) override; void SetTimer(uint64_t time_offset, uint32_t* timer_id) override; void KillTimer(const uint32_t timer_id) override; diff --git a/components/services/bat_ledger/bat_ledger_impl.cc b/components/services/bat_ledger/bat_ledger_impl.cc index e6a93fbd6022..a1e97019ed63 100644 --- a/components/services/bat_ledger/bat_ledger_impl.cc +++ b/components/services/bat_ledger/bat_ledger_impl.cc @@ -114,9 +114,9 @@ void BatLedgerImpl::GetPublisherAllowVideos( std::move(callback).Run(ledger_->GetPublisherAllowVideos()); } -void BatLedgerImpl::GetAutoContribute( - GetAutoContributeCallback callback) { - std::move(callback).Run(ledger_->GetAutoContribute()); +void BatLedgerImpl::GetAutoContributeEnabled( + GetAutoContributeEnabledCallback callback) { + std::move(callback).Run(ledger_->GetAutoContributeEnabled()); } void BatLedgerImpl::GetReconcileStamp(GetReconcileStampCallback callback) { @@ -320,16 +320,12 @@ void BatLedgerImpl::SetPublisherAllowVideos(bool allow) { ledger_->SetPublisherAllowVideos(allow); } -void BatLedgerImpl::SetUserChangedContribution() { - ledger_->SetUserChangedContribution(); +void BatLedgerImpl::SetAutoContributionAmount(double amount) { + ledger_->SetAutoContributionAmount(amount); } -void BatLedgerImpl::SetContributionAmount(double amount) { - ledger_->SetContributionAmount(amount); -} - -void BatLedgerImpl::SetAutoContribute(bool enabled) { - ledger_->SetAutoContribute(enabled); +void BatLedgerImpl::SetAutoContributeEnabled(bool enabled) { + ledger_->SetAutoContributeEnabled(enabled); } void BatLedgerImpl::UpdateAdsRewards() { @@ -393,9 +389,9 @@ void BatLedgerImpl::GetPublisherBanner(const std::string& publisher_id, std::bind(BatLedgerImpl::OnGetPublisherBanner, holder, _1)); } -void BatLedgerImpl::GetContributionAmount( - GetContributionAmountCallback callback) { - std::move(callback).Run(ledger_->GetContributionAmount()); +void BatLedgerImpl::GetAutoContributionAmount( + GetAutoContributionAmountCallback callback) { + std::move(callback).Run(ledger_->GetAutoContributionAmount()); } void BatLedgerImpl::OnOneTimeTip( @@ -441,8 +437,8 @@ void BatLedgerImpl::RemoveRecurringTip( std::bind(BatLedgerImpl::OnRemoveRecurringTip, holder, _1)); } -void BatLedgerImpl::GetBootStamp(GetBootStampCallback callback) { - std::move(callback).Run(ledger_->GetBootStamp()); +void BatLedgerImpl::GetCreationStamp(GetCreationStampCallback callback) { + std::move(callback).Run(ledger_->GetCreationStamp()); } void BatLedgerImpl::GetRewardsMainEnabled( diff --git a/components/services/bat_ledger/bat_ledger_impl.h b/components/services/bat_ledger/bat_ledger_impl.h index 4c1eb753654b..98b650f0ce09 100644 --- a/components/services/bat_ledger/bat_ledger_impl.h +++ b/components/services/bat_ledger/bat_ledger_impl.h @@ -45,7 +45,8 @@ class BatLedgerImpl : public mojom::BatLedger, GetPublisherAllowNonVerifiedCallback callback) override; void GetPublisherAllowVideos( GetPublisherAllowVideosCallback callback) override; - void GetAutoContribute(GetAutoContributeCallback callback) override; + void GetAutoContributeEnabled( + GetAutoContributeEnabledCallback callback) override; void GetReconcileStamp(GetReconcileStampCallback callback) override; void OnLoad(ledger::VisitDataPtr visit_data, uint64_t current_time) override; @@ -88,9 +89,8 @@ class BatLedgerImpl : public mojom::BatLedger, void SetPublisherMinVisits(int visits) override; void SetPublisherAllowNonVerified(bool allow) override; void SetPublisherAllowVideos(bool allow) override; - void SetUserChangedContribution() override; - void SetContributionAmount(double amount) override; - void SetAutoContribute(bool enabled) override; + void SetAutoContributionAmount(double amount) override; + void SetAutoContributeEnabled(bool enabled) override; void UpdateAdsRewards() override; void OnTimer(uint32_t timer_id) override; @@ -105,8 +105,8 @@ class BatLedgerImpl : public mojom::BatLedger, ledger::VisitDataPtr visit_data, const std::string& publisher_blob) override; - void GetContributionAmount( - GetContributionAmountCallback callback) override; + void GetAutoContributionAmount( + GetAutoContributionAmountCallback callback) override; void GetPublisherBanner(const std::string& publisher_id, GetPublisherBannerCallback callback) override; @@ -118,7 +118,7 @@ class BatLedgerImpl : public mojom::BatLedger, void RemoveRecurringTip( const std::string& publisher_key, RemoveRecurringTipCallback callback) override; - void GetBootStamp(GetBootStampCallback callback) override; + void GetCreationStamp(GetCreationStampCallback callback) override; void GetRewardsMainEnabled( GetRewardsMainEnabledCallback callback) override; void HasSufficientBalanceToReconcile( diff --git a/components/services/bat_ledger/bat_ledger_service_impl.cc b/components/services/bat_ledger/bat_ledger_service_impl.cc index 88c2f2265989..815ebc1bb096 100644 --- a/components/services/bat_ledger/bat_ledger_service_impl.cc +++ b/components/services/bat_ledger/bat_ledger_service_impl.cc @@ -48,9 +48,9 @@ void BatLedgerServiceImpl::SetDebug(bool is_debug) { ledger::is_debug = is_debug; } -void BatLedgerServiceImpl::SetReconcileTime(int32_t time) { +void BatLedgerServiceImpl::SetReconcileInterval(const int32_t interval) { DCHECK(!initialized_ || testing()); - ledger::reconcile_time = time; + ledger::reconcile_interval = interval; } void BatLedgerServiceImpl::SetShortRetries(bool short_retries) { @@ -70,8 +70,9 @@ void BatLedgerServiceImpl::GetDebug(GetDebugCallback callback) { std::move(callback).Run(ledger::is_debug); } -void BatLedgerServiceImpl::GetReconcileTime(GetReconcileTimeCallback callback) { - std::move(callback).Run(ledger::reconcile_time); +void BatLedgerServiceImpl::GetReconcileInterval( + GetReconcileIntervalCallback callback) { + std::move(callback).Run(ledger::reconcile_interval); } void BatLedgerServiceImpl::GetShortRetries(GetShortRetriesCallback callback) { diff --git a/components/services/bat_ledger/bat_ledger_service_impl.h b/components/services/bat_ledger/bat_ledger_service_impl.h index f60f7457ab75..793e85050f0f 100644 --- a/components/services/bat_ledger/bat_ledger_service_impl.h +++ b/components/services/bat_ledger/bat_ledger_service_impl.h @@ -26,13 +26,13 @@ class BatLedgerServiceImpl : public mojom::BatLedgerService { void SetEnvironment(ledger::Environment environment) override; void SetDebug(bool isDebug) override; - void SetReconcileTime(int32_t time) override; + void SetReconcileInterval(const int32_t interval) override; void SetShortRetries(bool short_retries) override; void SetTesting() override; void GetEnvironment(GetEnvironmentCallback callback) override; void GetDebug(GetDebugCallback callback) override; - void GetReconcileTime(GetReconcileTimeCallback callback) override; + void GetReconcileInterval(GetReconcileIntervalCallback callback) override; void GetShortRetries(GetShortRetriesCallback callback) override; private: diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc index 116ebc7af111..3a0db0d1ef21 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.cc @@ -71,29 +71,6 @@ void LedgerClientMojoProxy::LoadPublisherState( _2)); } -// static -void LedgerClientMojoProxy::OnSaveLedgerState( - CallbackHolder* holder, - const ledger::Result result) { - DCHECK(holder); - if (holder->is_valid()) { - std::move(holder->get()).Run(result); - } - delete holder; -} - -void LedgerClientMojoProxy::SaveLedgerState( - const std::string& ledger_state, - SaveLedgerStateCallback callback) { - auto* holder = new CallbackHolder( - AsWeakPtr(), std::move(callback)); - ledger_client_->SaveLedgerState( - ledger_state, - std::bind(LedgerClientMojoProxy::OnSaveLedgerState, - holder, - _1)); -} - void LedgerClientMojoProxy::OnReconcileComplete( const ledger::Result result, const std::string& contribution_id, diff --git a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h index 6f60c79a2b14..f6273a7f792e 100644 --- a/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h +++ b/components/services/bat_ledger/public/cpp/ledger_client_mojo_proxy.h @@ -36,8 +36,6 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, const ledger::RewardsType type) override; void LoadPublisherState(LoadPublisherStateCallback callback) override; - void SaveLedgerState(const std::string& ledger_state, - SaveLedgerStateCallback callback) override; void FetchFavIcon(const std::string& url, const std::string& favicon_key, FetchFavIconCallback callback) override; @@ -182,10 +180,6 @@ class LedgerClientMojoProxy : public mojom::BatLedgerClient, Callback callback_; }; - static void OnSaveLedgerState( - CallbackHolder* holder, - const ledger::Result result); - static void OnLoadLedgerState( CallbackHolder* holder, ledger::Result result, diff --git a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom index 31ce49a35841..cff4fa148921 100644 --- a/components/services/bat_ledger/public/interfaces/bat_ledger.mojom +++ b/components/services/bat_ledger/public/interfaces/bat_ledger.mojom @@ -13,13 +13,13 @@ interface BatLedgerService { associated BatLedger& bat_ledger); SetEnvironment(ledger.mojom.Environment environment); SetDebug(bool isDebug); - SetReconcileTime(int32 time); + SetReconcileInterval(int32 time); SetShortRetries(bool short_retries); SetTesting(); GetEnvironment() => (ledger.mojom.Environment environment); GetDebug() => (bool debug); - GetReconcileTime() => (int32 time); + GetReconcileInterval() => (int32 interval); GetShortRetries() => (bool short_retries); }; @@ -34,7 +34,7 @@ interface BatLedger { GetPublisherMinVisits() => (int32 min_visits); GetPublisherAllowNonVerified() => (bool allowed); GetPublisherAllowVideos() => (bool allowed); - GetAutoContribute() => (bool auto_contribute); + GetAutoContributeEnabled() => (bool auto_contribute); GetReconcileStamp() => (uint64 reconcile_stamp); OnLoad(ledger.mojom.VisitData visit_data, uint64 current_time); @@ -69,9 +69,8 @@ interface BatLedger { SetPublisherMinVisits(int32 visits); SetPublisherAllowNonVerified(bool allow); SetPublisherAllowVideos(bool allow); - SetUserChangedContribution(); - SetContributionAmount(double amount); - SetAutoContribute(bool enabled); + SetAutoContributionAmount(double amount); + SetAutoContributeEnabled(bool enabled); UpdateAdsRewards(); OnTimer(uint32 timer_id); @@ -84,14 +83,14 @@ interface BatLedger { GetPublisherActivityFromUrl(uint64 window_id, ledger.mojom.VisitData visit_data, string publisher_blob); - GetContributionAmount() => (double contribution_amount); + GetAutoContributionAmount() => (double contribution_amount); GetPublisherBanner(string publisher_id) => (ledger.mojom.PublisherBanner? banner); OneTimeTip(string publisher_id, double amount) => (ledger.mojom.Result result); RemoveRecurringTip(string publisher_key) => (ledger.mojom.Result result); - GetBootStamp() => (uint64 boot_stamp); + GetCreationStamp() => (uint64 boot_stamp); GetRewardsMainEnabled() => (bool main_enabled); HasSufficientBalanceToReconcile() => (bool sufficient); @@ -162,7 +161,6 @@ interface BatLedgerClient { [Sync] LoadLedgerState() => (ledger.mojom.Result result, string data); LoadPublisherState() => (ledger.mojom.Result result, string data); - SaveLedgerState(string ledger_state) => (ledger.mojom.Result result); OnWalletProperties(ledger.mojom.Result result, ledger.mojom.WalletProperties? properties); OnReconcileComplete(ledger.mojom.Result result, string viewing_id, diff --git a/test/data/rewards-data/state/ledger_state b/test/data/rewards-data/state/ledger_state new file mode 100644 index 000000000000..ee8c65bafe4c --- /dev/null +++ b/test/data/rewards-data/state/ledger_state @@ -0,0 +1,46 @@ +{ + "walletInfo": { + "paymentId": "3023921f-a841-4db8-8f33-8787bdc3f436", + "addressCARD_ID": "46167343-c24f-41d5-aa11-d2bd4095e944", + "keyInfoSeed": "daIBD/rSQpfjHczU702WEHhhdVdfIPlgJySVceXq3Js=" + }, + "bootStamp": 1578132997, + "reconcileStamp": 1591763936, + "fee_currency": "BAT", + "settings": "adFree", + "fee_amount": 20.0, + "user_changed_fee": false, + "rewards_enabled": true, + "auto_contribute": false, + "walletProperties": { + "fee_amount": 20.0, + "parameters": { + "adFree": { + "fee": { + "BAT": 20.0 + }, + "choices": { + "BAT": [ + 5.0, + 10.0, + 15.0, + 20.0, + 25.0, + 50.0, + 100.0 + ] + } + }, + "defaultTipChoices": [ + "1.000000", + "10.000000", + "100.000000" + ], + "defaultMonthlyChoices": [ + "1.000000", + "10.000000", + "100.000000" + ] + } + } +} \ No newline at end of file diff --git a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h index d28d1e1ea428..bc0999ac21de 100644 --- a/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h +++ b/vendor/bat-native-confirmations/src/bat/confirmations/internal/confirmations_client_mock.h @@ -41,10 +41,6 @@ class ConfirmationsClientMock : public ConfirmationsClient { MOCK_METHOD1(LoadLedgerState, void( ledger::OnLoadCallback callback)); - MOCK_METHOD2(SaveLedgerState, void( - const std::string& ledger_state, - ledger::ResultCallback callback)); - MOCK_METHOD1(LoadPublisherState, void( ledger::OnLoadCallback callback)); diff --git a/vendor/bat-native-ledger/BUILD.gn b/vendor/bat-native-ledger/BUILD.gn index 381487f143a2..a03d6008916b 100644 --- a/vendor/bat-native-ledger/BUILD.gn +++ b/vendor/bat-native-ledger/BUILD.gn @@ -282,6 +282,8 @@ source_set("ledger") { "src/bat/ledger/internal/state/state_migration.h", "src/bat/ledger/internal/state/state_migration_v1.cc", "src/bat/ledger/internal/state/state_migration_v1.h", + "src/bat/ledger/internal/state/state_migration_v2.cc", + "src/bat/ledger/internal/state/state_migration_v2.h", "src/bat/ledger/internal/state/state_util.cc", "src/bat/ledger/internal/state/state_util.h", "src/bat/ledger/internal/legacy/client_state.cc", diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger.h b/vendor/bat-native-ledger/include/bat/ledger/ledger.h index d8d1c1d21fa1..a146f857f899 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger.h @@ -23,7 +23,7 @@ namespace ledger { extern Environment _environment; extern bool is_debug; extern bool is_testing; -extern int reconcile_time; // minutes +extern int reconcile_interval; // minutes extern bool short_retries; using PublisherBannerCallback = @@ -145,17 +145,15 @@ class LEDGER_EXPORT Ledger { virtual void SetPublisherAllowVideos(bool allow) = 0; - virtual void SetContributionAmount(double amount) = 0; + virtual void SetAutoContributionAmount(double amount) = 0; - virtual void SetUserChangedContribution() = 0; - - virtual void SetAutoContribute(bool enabled) = 0; + virtual void SetAutoContributeEnabled(bool enabled) = 0; virtual void UpdateAdsRewards() = 0; - virtual uint64_t GetReconcileStamp() const = 0; + virtual uint64_t GetReconcileStamp() = 0; - virtual bool GetRewardsMainEnabled() const = 0; + virtual bool GetRewardsMainEnabled() = 0; virtual int GetPublisherMinVisitTime() = 0; // In milliseconds @@ -165,9 +163,9 @@ class LEDGER_EXPORT Ledger { virtual bool GetPublisherAllowVideos() = 0; - virtual double GetContributionAmount() const = 0; + virtual double GetAutoContributionAmount() = 0; - virtual bool GetAutoContribute() const = 0; + virtual bool GetAutoContributeEnabled() = 0; virtual void FetchWalletProperties( OnWalletPropertiesCallback callback) const = 0; @@ -250,7 +248,7 @@ class LEDGER_EXPORT Ledger { virtual double GetDefaultContributionAmount() = 0; - virtual uint64_t GetBootStamp() const = 0; + virtual uint64_t GetCreationStamp() = 0; virtual void HasSufficientBalanceToReconcile( HasSufficientBalanceToReconcileCallback callback) = 0; diff --git a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h index a30fe0edbe99..76ba0753fe91 100644 --- a/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h +++ b/vendor/bat-native-ledger/include/bat/ledger/ledger_client.h @@ -84,10 +84,6 @@ class LEDGER_EXPORT LedgerClient { virtual void LoadLedgerState(OnLoadCallback callback) = 0; - virtual void SaveLedgerState( - const std::string& ledger_state, - ResultCallback callback) = 0; - virtual void LoadPublisherState(OnLoadCallback callback) = 0; virtual void LoadNicewareList(ledger::GetNicewareListCallback callback) = 0; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc index 9f5686c50d35..6a5b4c2da7ad 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.cc @@ -170,8 +170,7 @@ bool getJSONRates(const std::string& json, } bool getJSONWalletInfo(const std::string& json, - ledger::WalletInfoProperties* walletInfo, - double* fee_amount) { + ledger::WalletInfoProperties* walletInfo) { rapidjson::Document d; d.Parse(json.c_str()); @@ -190,19 +189,8 @@ bool getJSONWalletInfo(const std::string& json, walletInfo->payment_id = d["wallet"]["paymentId"].GetString(); walletInfo->address_card_id = d["wallet"]["addresses"]["CARD_ID"].GetString(); - - const auto & fee = d["payload"]["adFree"]["fee"].GetObject(); - auto itr = fee.MemberBegin(); - if (itr != fee.MemberEnd()) { - *fee_amount = itr->value.GetDouble(); - } } else if (d.HasMember("parameters") && d["parameters"].IsObject()) { walletInfo->address_card_id = d["addresses"]["CARD_ID"].GetString(); - const auto & fee = d["parameters"]["adFree"]["fee"].GetObject(); - auto itr = fee.MemberBegin(); - if (itr != fee.MemberEnd()) { - *fee_amount = itr->value.GetDouble(); - } } } return !error; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.h b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.h index 799c3ccda158..47f2d36c0eb4 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/bat_helper.h @@ -30,8 +30,7 @@ bool getJSONList(const std::string& fieldName, std::vector* value); bool getJSONWalletInfo(const std::string& json, - ledger::WalletInfoProperties* walletInfo, - double* fee_amount); + ledger::WalletInfoProperties* walletInfo); bool getJSONRates(const std::string& json, std::map* rates); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h index 235ca32b8369..86e749965787 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution.h @@ -71,7 +71,7 @@ class Contribution { void SetTimer(uint32_t* timer_id, uint64_t start_timer_in = 0); - // Resets reconcile stamps + // Reset reconcile stamps void ResetReconcileStamp(); void ContributeUnverifiedPublishers(); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc index 0fc5f5ee8e3e..8faed7074023 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/contribution/contribution_ac.cc @@ -23,7 +23,8 @@ ContributionAC::ContributionAC(bat_ledger::LedgerImpl* ledger, ContributionAC::~ContributionAC() = default; void ContributionAC::Process(const uint64_t reconcile_stamp) { - if (!ledger_->GetRewardsMainEnabled() || !ledger_->GetAutoContribute()) { + if (!ledger_->GetRewardsMainEnabled() || + !ledger_->GetAutoContributeEnabled()) { BLOG(1, "Auto contribution is off"); return; } @@ -75,7 +76,7 @@ void ContributionAC::PreparePublisherList(ledger::PublisherInfoList list) { auto queue = ledger::ContributionQueue::New(); queue->type = ledger::RewardsType::AUTO_CONTRIBUTE; - queue->amount = ledger_->GetContributionAmount(); + queue->amount = ledger_->GetAutoContributionAmount(); queue->partial = true; queue->publishers = std::move(queue_list); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h index d6e83069759a..33997f2a0025 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_client_mock.h @@ -36,10 +36,6 @@ class MockLedgerClient : public LedgerClient { MOCK_METHOD1(LoadLedgerState, void( ledger::OnLoadCallback callback)); - MOCK_METHOD2(SaveLedgerState, void( - const std::string& ledger_state, - ledger::ResultCallback callback)); - MOCK_METHOD1(LoadPublisherState, void( ledger::OnLoadCallback callback)); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc index d301ea66e75e..81179dedc2f2 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.cc @@ -183,19 +183,12 @@ void LedgerImpl::MaybeInitializeConfirmations( const bool is_enabled = GetBooleanState(ledger::kStateEnabled); - const bool is_enabled_migrated = - GetBooleanState(ledger::kStateEnabledMigrated); - - if (is_enabled || !is_enabled_migrated) { + if (is_enabled) { InitializeConfirmations(callback); - } else { - auto on_load = std::bind(&LedgerImpl::OnLedgerStateLoaded, - this, - _1, - _2, - std::move(callback)); - LoadLedgerState(on_load); + return; } + + callback(ledger::Result::LEDGER_OK); } void LedgerImpl::InitializeConfirmations( @@ -219,12 +212,7 @@ void LedgerImpl::OnConfirmationsInitialized( BLOG(0, "Failed to initialize confirmations"); } - auto on_load = std::bind(&LedgerImpl::OnLedgerStateLoaded, - this, - _1, - _2, - std::move(callback)); - LoadLedgerState(on_load); + callback(ledger::Result::LEDGER_OK); } void LedgerImpl::StartConfirmations() { @@ -316,7 +304,7 @@ void LedgerImpl::OnShow(uint32_t tab_id, const uint64_t& current_time) { } void LedgerImpl::OnHide(uint32_t tab_id, const uint64_t& current_time) { - if (!GetRewardsMainEnabled() || !GetAutoContribute()) { + if (!GetRewardsMainEnabled() || !GetAutoContributeEnabled()) { return; } @@ -416,36 +404,6 @@ void LedgerImpl::LoadLedgerState(ledger::OnLoadCallback callback) { ledger_client_->LoadLedgerState(std::move(callback)); } -void LedgerImpl::OnLedgerStateLoaded( - ledger::Result result, - const std::string& data, - ledger::ResultCallback callback) { - if (result == ledger::Result::LEDGER_OK) { - if (!legacy_bat_state_->LoadState(data)) { - BLOG(0, "Successfully loaded but failed to parse ledger state."); - BLOG(1, "Failed ledger state: " << data); - - callback(ledger::Result::INVALID_LEDGER_STATE); - return; - } - - if (GetPaymentId().empty() || GetWalletPassphrase().empty()) { - BLOG(0, "Corrupted wallet"); - callback(ledger::Result::CORRUPTED_DATA); - return; - } - - callback(result); - return; - } - - if (result != ledger::Result::NO_LEDGER_STATE) { - BLOG(0, "Failed to load ledger state"); - BLOG(1, "Failed ledger state: " << data); - } - callback(result); -} - void LedgerImpl::SetConfirmationsWalletInfo( const ledger::WalletInfoProperties& wallet_info_properties) { if (!IsConfirmationsRunning()) { @@ -485,12 +443,6 @@ void LedgerImpl::LoadPublisherState(ledger::OnLoadCallback callback) { ledger_client_->LoadPublisherState(std::move(callback)); } -void LedgerImpl::SaveLedgerState( - const std::string& data, - ledger::ResultCallback callback) { - ledger_client_->SaveLedgerState(data, callback); -} - void LedgerImpl::LoadURL( const std::string& url, const std::vector& headers, @@ -643,7 +595,7 @@ void LedgerImpl::GetExcludedList(ledger::PublisherInfoListCallback callback) { } void LedgerImpl::SetRewardsMainEnabled(bool enabled) { - legacy_bat_state_->SetRewardsMainEnabled(enabled); + braveledger_state::SetRewardsMainEnabled(this, enabled); bat_publisher_->SetPublisherServerListTimer(enabled); if (enabled) { @@ -669,25 +621,17 @@ void LedgerImpl::SetPublisherAllowVideos(bool allow) { braveledger_state::SetPublisherAllowVideos(this, allow); } -void LedgerImpl::SetContributionAmount(double amount) { - legacy_bat_state_->SetContributionAmount(amount); -} - -void LedgerImpl::SetUserChangedContribution() { - legacy_bat_state_->SetUserChangedContribution(); -} - -bool LedgerImpl::GetUserChangedContribution() { - return legacy_bat_state_->GetUserChangedContribution(); +void LedgerImpl::SetAutoContributionAmount(double amount) { + braveledger_state::SetAutoContributionAmount(this, amount); } -void LedgerImpl::SetAutoContribute(bool enabled) { - legacy_bat_state_->SetAutoContribute(enabled); +void LedgerImpl::SetAutoContributeEnabled(bool enabled) { + braveledger_state::SetAutoContributeEnabled(this, enabled); } ledger::AutoContributePropsPtr LedgerImpl::GetAutoContributeProps() { ledger::AutoContributePropsPtr props = ledger::AutoContributeProps::New(); - props->enabled_contribute = GetAutoContribute(); + props->enabled_contribute = GetAutoContributeEnabled(); props->contribution_min_time = GetPublisherMinVisitTime(); props->contribution_min_visits = GetPublisherMinVisits(); props->contribution_non_verified = GetPublisherAllowNonVerified(); @@ -696,8 +640,8 @@ ledger::AutoContributePropsPtr LedgerImpl::GetAutoContributeProps() { return props; } -bool LedgerImpl::GetRewardsMainEnabled() const { - return legacy_bat_state_->GetRewardsMainEnabled(); +bool LedgerImpl::GetRewardsMainEnabled() { + return braveledger_state::GetRewardsMainEnabled(this); } int LedgerImpl::GetPublisherMinVisitTime() { @@ -716,16 +660,16 @@ bool LedgerImpl::GetPublisherAllowVideos() { return braveledger_state::GetPublisherAllowVideos(this); } -double LedgerImpl::GetContributionAmount() const { - return legacy_bat_state_->GetContributionAmount(); +double LedgerImpl::GetAutoContributionAmount() { + return braveledger_state::GetAutoContributionAmount(this); } -bool LedgerImpl::GetAutoContribute() const { - return legacy_bat_state_->GetAutoContribute(); +bool LedgerImpl::GetAutoContributeEnabled() { + return braveledger_state::GetAutoContributeEnabled(this); } -uint64_t LedgerImpl::GetReconcileStamp() const { - return legacy_bat_state_->GetReconcileStamp(); +uint64_t LedgerImpl::GetReconcileStamp() { + return braveledger_state::GetReconcileStamp(this); } void LedgerImpl::ContributionCompleted( @@ -949,7 +893,7 @@ void LedgerImpl::UpdateAdsRewards() { } void LedgerImpl::ResetReconcileStamp() { - legacy_bat_state_->ResetReconcileStamp(); + braveledger_state::SetReconcileStamp(this, ledger::reconcile_interval); ledger_client_->ReconcileStampReset(); } @@ -982,7 +926,7 @@ void LedgerImpl::GetRewardsInternalsInfo( info->payment_id = legacy_bat_state_->GetPaymentId(); // Retrieve the boot stamp. - info->boot_stamp = legacy_bat_state_->GetBootStamp(); + info->boot_stamp = legacy_bat_state_->GetCreationStamp(); // Retrieve the key info seed and validate it. const ledger::WalletInfoProperties wallet_info = @@ -1014,12 +958,12 @@ void LedgerImpl::SetWalletProperties( legacy_bat_state_->SetWalletProperties(properties); } -uint64_t LedgerImpl::GetBootStamp() const { - return legacy_bat_state_->GetBootStamp(); +uint64_t LedgerImpl::GetCreationStamp() { + return braveledger_state::GetCreationStamp(this); } -void LedgerImpl::SetBootStamp(uint64_t stamp) { - legacy_bat_state_->SetBootStamp(stamp); +void LedgerImpl::SetCreationStamp(uint64_t stamp) { + braveledger_state::SetCreationStamp(this, stamp); } void LedgerImpl::SaveContributionInfo( ledger::ContributionInfoPtr info, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h index 902fb239bdf5..51cb42c7f9cc 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl.h @@ -140,13 +140,9 @@ class LedgerImpl : public ledger::Ledger { void SetPublisherAllowVideos(bool allow) override; - void SetContributionAmount(double amount) override; + void SetAutoContributionAmount(double amount) override; - void SetUserChangedContribution() override; - - bool GetUserChangedContribution(); - - void SetAutoContribute(bool enabled) override; + void SetAutoContributeEnabled(bool enabled) override; void UpdateAdsRewards() override; @@ -156,9 +152,9 @@ class LedgerImpl : public ledger::Ledger { void PendingContributionSaved(const ledger::Result result); - uint64_t GetReconcileStamp() const override; + uint64_t GetReconcileStamp() override; - bool GetRewardsMainEnabled() const override; + bool GetRewardsMainEnabled() override; int GetPublisherMinVisitTime() override; // In milliseconds @@ -168,9 +164,9 @@ class LedgerImpl : public ledger::Ledger { bool GetPublisherAllowVideos() override; - double GetContributionAmount() const override; + double GetAutoContributionAmount() override; - bool GetAutoContribute() const override; + bool GetAutoContributeEnabled() override; void GetBalanceReport( const ledger::ActivityMonth month, @@ -182,10 +178,6 @@ class LedgerImpl : public ledger::Ledger { ledger::AutoContributePropsPtr GetAutoContributeProps() override; - void SaveLedgerState( - const std::string& data, - ledger::ResultCallback callback); - void LoadNicewareList(ledger::GetNicewareListCallback callback); void SetConfirmationsWalletInfo( @@ -323,9 +315,9 @@ class LedgerImpl : public ledger::Ledger { void SetWalletProperties( ledger::WalletProperties* properties); - uint64_t GetBootStamp() const override; + uint64_t GetCreationStamp() override; - void SetBootStamp(uint64_t stamp); + void SetCreationStamp(uint64_t stamp); void SaveContributionInfo( ledger::ContributionInfoPtr info, @@ -792,11 +784,6 @@ class LedgerImpl : public ledger::Ledger { const ledger::Result result, ledger::ResultCallback callback); - void OnLedgerStateLoaded( - ledger::Result result, - const std::string& data, - ledger::ResultCallback callback); - void RefreshPromotions(bool retryAfterError); void DownloadPublisherList( diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl_mock.h b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl_mock.h index 544443017825..ebd3eb6c6b7c 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl_mock.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/ledger_impl_mock.h @@ -77,13 +77,9 @@ class MockLedgerImpl : public LedgerImpl { MOCK_METHOD1(SetPublisherAllowVideos, void(bool)); - MOCK_METHOD1(SetContributionAmount, void(double)); + MOCK_METHOD1(SetAutoContributionAmount, void(double)); - MOCK_METHOD0(SetUserChangedContribution, void()); - - MOCK_METHOD0(GetUserChangedContribution, bool()); - - MOCK_METHOD1(SetAutoContribute, void(bool)); + MOCK_METHOD1(SetAutoContributeEnabled, void(bool)); MOCK_METHOD0(UpdateAdsRewards, void()); @@ -93,7 +89,7 @@ class MockLedgerImpl : public LedgerImpl { MOCK_CONST_METHOD0(GetReconcileStamp, uint64_t()); - MOCK_CONST_METHOD0(GetRewardsMainEnabled, bool()); + MOCK_METHOD0(GetRewardsMainEnabled, bool()); MOCK_METHOD0(GetPublisherMinVisitTime, int()); @@ -103,9 +99,9 @@ class MockLedgerImpl : public LedgerImpl { MOCK_METHOD0(GetPublisherAllowVideos, bool()); - MOCK_CONST_METHOD0(GetContributionAmount, double()); + MOCK_METHOD0(GetAutoContributionAmount, double()); - MOCK_CONST_METHOD0(GetAutoContribute, bool()); + MOCK_METHOD0(GetAutoContributeEnabled, bool()); MOCK_CONST_METHOD3(GetBalanceReport, void(const ledger::ActivityMonth, @@ -117,8 +113,6 @@ class MockLedgerImpl : public LedgerImpl { MOCK_METHOD0(GetAutoContributeProps, ledger::AutoContributePropsPtr()); - MOCK_METHOD1(SaveLedgerState, void(const std::string&)); - MOCK_METHOD1(LoadNicewareList, void(ledger::GetNicewareListCallback)); MOCK_METHOD1(SetConfirmationsWalletInfo, @@ -260,9 +254,9 @@ class MockLedgerImpl : public LedgerImpl { MOCK_METHOD1(SetWalletProperties, void(ledger::WalletProperties*)); - MOCK_CONST_METHOD0(GetBootStamp, uint64_t()); + MOCK_METHOD0(GetCreationStamp, uint64_t()); - MOCK_METHOD1(SetBootStamp, void(uint64_t)); + MOCK_METHOD1(SetCreationStamp, void(uint64_t)); MOCK_METHOD6(SaveContributionInfo, void(const std::string&, diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.cc index b1183659b096..3b4b6d3879a8 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.cc @@ -13,6 +13,7 @@ #include "bat/ledger/internal/legacy/client_state.h" using std::placeholders::_1; +using std::placeholders::_2; namespace braveledger_bat_state { @@ -21,105 +22,62 @@ LegacyBatState::LegacyBatState(bat_ledger::LedgerImpl* ledger) : state_(new ledger::ClientProperties()) { } -LegacyBatState::~LegacyBatState() { +LegacyBatState::~LegacyBatState() = default; + +void LegacyBatState::Load(ledger::ResultCallback callback) { + auto load_callback = std::bind(&LegacyBatState::OnLoad, + this, + _1, + _2, + callback); + ledger_->LoadLedgerState(load_callback); } -bool LegacyBatState::LoadState(const std::string& data) { +void LegacyBatState::OnLoad( + const ledger::Result result, + const std::string& data, + ledger::ResultCallback callback) { + if (result != ledger::Result::LEDGER_OK) { + callback(result); + return; + } + ledger::ClientProperties state; const ledger::ClientState client_state; if (!client_state.FromJson(data, &state)) { BLOG(0, "Failed to load client state: " << data); - return false; + callback(ledger::Result::LEDGER_ERROR); + return; } state_.reset(new ledger::ClientProperties(state)); - bool stateChanged = false; - // fix timestamp ms to s conversion if (std::to_string(state_->reconcile_timestamp).length() > 10) { state_->reconcile_timestamp = state_->reconcile_timestamp / 1000; - stateChanged = true; } // fix timestamp ms to s conversion if (std::to_string(state_->boot_timestamp).length() > 10) { state_->boot_timestamp = state_->boot_timestamp / 1000; - stateChanged = true; } - if (stateChanged) { - SaveState(); - } - - return true; -} - -void LegacyBatState::SaveState() { - const ledger::ClientState client_state; - const std::string data = client_state.ToJson(*state_); - - auto save_callback = std::bind(&LegacyBatState::OnSaveState, - this, - _1); - - ledger_->SaveLedgerState(data, save_callback); -} - -void LegacyBatState::OnSaveState(const ledger::Result result) { - if (result != ledger::Result::LEDGER_OK) { - BLOG(0, "Ledger state was not save successfully"); - return; - } -} - -void LegacyBatState::SetRewardsMainEnabled(bool enabled) { - state_->rewards_enabled = enabled; - SaveState(); + callback(ledger::Result::LEDGER_OK); } bool LegacyBatState::GetRewardsMainEnabled() const { return state_->rewards_enabled; } -void LegacyBatState::SetContributionAmount(double amount) { - ledger::WalletProperties properties = GetWalletProperties(); - auto hasAmount = std::find(properties.parameters_choices.begin(), - properties.parameters_choices.end(), - amount); - - if (hasAmount == properties.parameters_choices.end()) { - // amount is missing in the list - properties.parameters_choices.push_back(amount); - std::sort(properties.parameters_choices.begin(), - properties.parameters_choices.end()); - ledger_->OnWalletProperties(ledger::Result::LEDGER_OK, properties); - state_->wallet = properties; - } - - state_->fee_amount = amount; - SaveState(); -} - -double LegacyBatState::GetContributionAmount() const { +double LegacyBatState::GetAutoContributionAmount() const { return state_->fee_amount; } -void LegacyBatState::SetUserChangedContribution() { - state_->user_changed_fee = true; - SaveState(); -} - bool LegacyBatState::GetUserChangedContribution() const { return state_->user_changed_fee; } -void LegacyBatState::SetAutoContribute(bool enabled) { - state_->auto_contribute = enabled; - SaveState(); -} - -bool LegacyBatState::GetAutoContribute() const { +bool LegacyBatState::GetAutoContributeEnabled() const { return state_->auto_contribute; } @@ -131,18 +89,6 @@ uint64_t LegacyBatState::GetReconcileStamp() const { return state_->reconcile_timestamp; } -void LegacyBatState::ResetReconcileStamp() { - if (ledger::reconcile_time > 0) { - state_->reconcile_timestamp = - braveledger_time_util::GetCurrentTimeStamp() + - ledger::reconcile_time * 60; - } else { - state_->reconcile_timestamp = braveledger_time_util::GetCurrentTimeStamp() + - braveledger_ledger::_reconcile_default_interval; - } - SaveState(); -} - bool LegacyBatState::IsWalletCreated() const { return state_->boot_timestamp != 0u; } @@ -158,7 +104,6 @@ const ledger::WalletInfoProperties& LegacyBatState::GetWalletInfo() const { void LegacyBatState::SetWalletInfo( const ledger::WalletInfoProperties& wallet_info) { state_->wallet_info = wallet_info; - SaveState(); } const ledger::WalletProperties& LegacyBatState::GetWalletProperties() const { @@ -167,8 +112,7 @@ const ledger::WalletProperties& LegacyBatState::GetWalletProperties() const { void LegacyBatState::SetWalletProperties( ledger::WalletProperties* properties) { - double amount = GetContributionAmount(); - double new_amount = properties->fee_amount; + double amount = GetAutoContributionAmount(); bool amount_changed = GetUserChangedContribution(); if (amount_changed) { auto hasAmount = std::find(properties->parameters_choices.begin(), @@ -184,30 +128,18 @@ void LegacyBatState::SetWalletProperties( } state_->wallet = *properties; - - if (!amount_changed && amount != new_amount) { - SetContributionAmount(new_amount); - } - - SaveState(); } -uint64_t LegacyBatState::GetBootStamp() const { +uint64_t LegacyBatState::GetCreationStamp() const { return state_->boot_timestamp; } -void LegacyBatState::SetBootStamp(uint64_t stamp) { - state_->boot_timestamp = stamp; - SaveState(); -} - double LegacyBatState::GetDefaultContributionAmount() { return state_->wallet.fee_amount; } void LegacyBatState::SetInlineTipSetting(const std::string& key, bool enabled) { state_->inline_tips[key] = enabled; - SaveState(); } bool LegacyBatState::GetInlineTipSetting(const std::string& key) const { diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.h b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.h index 26ce94168800..9d2ca1d87297 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/legacy/bat_state.h @@ -25,30 +25,20 @@ class LegacyBatState { explicit LegacyBatState(bat_ledger::LedgerImpl* ledger); ~LegacyBatState(); - bool LoadState(const std::string& data); - - void SetRewardsMainEnabled(bool enabled); + void Load(ledger::ResultCallback callback); bool GetRewardsMainEnabled() const; - void SetContributionAmount(double amount); - - double GetContributionAmount() const; - - void SetUserChangedContribution(); + double GetAutoContributionAmount() const; bool GetUserChangedContribution() const; - void SetAutoContribute(bool enabled); - - bool GetAutoContribute() const; + bool GetAutoContributeEnabled() const; const std::string& GetCardIdAddress() const; uint64_t GetReconcileStamp() const; - void ResetReconcileStamp(); - bool IsWalletCreated() const; const std::string& GetPaymentId() const; @@ -62,9 +52,7 @@ class LegacyBatState { void SetWalletProperties( ledger::WalletProperties* properties); - uint64_t GetBootStamp() const; - - void SetBootStamp(uint64_t stamp); + uint64_t GetCreationStamp() const; double GetDefaultContributionAmount(); @@ -73,9 +61,10 @@ class LegacyBatState { bool GetInlineTipSetting(const std::string& key) const; private: - void SaveState(); - - void OnSaveState(const ledger::Result result); + void OnLoad( + const ledger::Result result, + const std::string& data, + ledger::ResultCallback callback); bat_ledger::LedgerImpl* ledger_; // NOT OWNED std::unique_ptr state_; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher.cc index 9c3c22293958..b5c907a4aeae 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/publisher/publisher.cc @@ -294,7 +294,7 @@ void Publisher::SaveVisitInternal( if (new_visit && (excluded || - !ledger_->GetAutoContribute() || + !ledger_->GetAutoContributeEnabled() || min_duration_new || verified_new)) { panel_info = publisher_info->Clone(); @@ -305,7 +305,7 @@ void Publisher::SaveVisitInternal( ledger_->SavePublisherInfo(std::move(publisher_info), callback); } else if (!excluded && - ledger_->GetAutoContribute() && + ledger_->GetAutoContributeEnabled() && min_duration_ok && verified_old) { publisher_info->visits += 1; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h index 33daad5c8fa9..9f5a9d086a4f 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_keys.h @@ -10,7 +10,6 @@ namespace ledger { const char kStateEnabled[] = "enabled"; - const char kStateEnabledMigrated[] = "enabled_migrated"; const char kStateServerPublisherListStamp[] = "server_publisher_list_stamp"; const char kStateUpholdAnonAddress[] = "uphold_anon_address"; const char kStatePromotionLastFetchStamp[] = "promotion_last_fetch_stamp"; @@ -18,12 +17,18 @@ namespace ledger { "promotion_corrupted_migrated2"; const char kStateAnonTransferChecked[] = "anon_transfer_checked"; const char kStateVersion[] = "version"; + + // Auto contributions const char kStateMinVisitTime[] = "ac.min_visit_time"; const char kStateMinVisits[] = "ac.min_visits"; const char kStateAllowNonVerified[] = "ac.allow_non_verified"; const char kStateAllowVideoContribution[] = "ac.allow_video_contributions"; const char kStateScoreA[] = "ac.score.a"; const char kStateScoreB[] = "ac.score.b"; + const char kStateAutoContributeEnabled[] = "ac.enabled"; + const char kStateAutoContributeAmount[] = "ac.amount"; + const char kStateNextReconcileStamp[] = "ac.next_reconcile_stamp"; + const char kStateCreationStamp[] = "creation_stamp"; } // namespace ledger diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc index 59bfcaa11631..acb326202f55 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.cc @@ -13,7 +13,7 @@ using std::placeholders::_1; namespace { -const int kCurrentVersionNumber = 1; +const int kCurrentVersionNumber = 2; } // namespace @@ -21,7 +21,9 @@ namespace braveledger_state { StateMigration::StateMigration(bat_ledger::LedgerImpl* ledger) : v1_(std::make_unique(ledger)), + v2_(std::make_unique(ledger)), ledger_(ledger) { + DCHECK(v1_ && v2_); } StateMigration::~StateMigration() = default; @@ -46,6 +48,10 @@ void StateMigration::Migrate(ledger::ResultCallback callback) { v1_->Migrate(migrate_callback); return; } + case 2: { + v2_->Migrate(migrate_callback); + return; + } } BLOG(0, "Migration version is not handled " << new_version); diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h index f59012398472..71751aae05d1 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration.h @@ -10,6 +10,7 @@ #include #include "bat/ledger/internal/state/state_migration_v1.h" +#include "bat/ledger/internal/state/state_migration_v2.h" #include "bat/ledger/ledger.h" namespace bat_ledger { @@ -32,6 +33,7 @@ class StateMigration { ledger::ResultCallback callback); std::unique_ptr v1_; + std::unique_ptr v2_; bat_ledger::LedgerImpl* ledger_; // NOT OWNED }; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.cc new file mode 100644 index 000000000000..89fbe1db1a66 --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.cc @@ -0,0 +1,73 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#include + +#include "bat/ledger/internal/ledger_impl.h" +#include "bat/ledger/internal/state/state_keys.h" +#include "bat/ledger/internal/state/state_migration_v2.h" +#include "bat/ledger/internal/state/state_util.h" + +using std::placeholders::_1; + +namespace braveledger_state { + +StateMigrationV2::StateMigrationV2(bat_ledger::LedgerImpl* ledger) : + ledger_(ledger) { +} + +StateMigrationV2::~StateMigrationV2() = default; + +void StateMigrationV2::Migrate(ledger::ResultCallback callback) { + legacy_state_ = + std::make_unique(ledger_); + + auto load_callback = std::bind(&StateMigrationV2::OnLoadState, + this, + _1, + callback); + + legacy_state_->Load(load_callback); +} + +void StateMigrationV2::OnLoadState( + const ledger::Result result, + ledger::ResultCallback callback) { + if (result == ledger::Result::NO_LEDGER_STATE) { + BLOG(1, "No ledger state"); + callback(ledger::Result::LEDGER_OK); + return; + } + + if (result != ledger::Result::LEDGER_OK) { + BLOG(0, "Failed to load ledger state file, setting default values"); + callback(ledger::Result::LEDGER_OK); + return; + } + + ledger_->SetBooleanState( + ledger::kStateEnabled, + legacy_state_->GetRewardsMainEnabled()); + + ledger_->SetBooleanState( + ledger::kStateAutoContributeEnabled, + legacy_state_->GetAutoContributeEnabled()); + + if (legacy_state_->GetUserChangedContribution()) { + ledger_->SetDoubleState( + ledger::kStateAutoContributeAmount, + legacy_state_->GetAutoContributionAmount()); + } + + ledger_->SetUint64State( + ledger::kStateNextReconcileStamp, + legacy_state_->GetReconcileStamp()); + + ledger_->SetUint64State( + ledger::kStateCreationStamp, + legacy_state_->GetCreationStamp()); +} + +} // namespace braveledger_state diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.h new file mode 100644 index 000000000000..f4502572dbff --- /dev/null +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_migration_v2.h @@ -0,0 +1,39 @@ +/* Copyright (c) 2020 The Brave Authors. All rights reserved. + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V2_H_ +#define BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V2_H_ + +#include +#include + +#include "bat/ledger/internal/legacy/bat_state.h" +#include "bat/ledger/ledger.h" + +namespace bat_ledger { +class LedgerImpl; +} + +namespace braveledger_state { + +class StateMigrationV2 { + public: + explicit StateMigrationV2(bat_ledger::LedgerImpl* ledger); + ~StateMigrationV2(); + + void Migrate(ledger::ResultCallback callback); + + private: + void OnLoadState( + const ledger::Result result, + ledger::ResultCallback callback); + + std::unique_ptr legacy_state_; + bat_ledger::LedgerImpl* ledger_; // NOT OWNED +}; + +} // namespace braveledger_state + +#endif // BRAVELEDGER_BAT_STATE_STATE_MIGRATION_V2_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc index af89895f2ef0..f8e7bb28f5d9 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.cc @@ -3,8 +3,10 @@ * 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 "bat/ledger/internal/common/time_util.h" #include "bat/ledger/internal/state/state_keys.h" #include "bat/ledger/internal/state/state_util.h" +#include "bat/ledger/internal/static_values.h" namespace braveledger_state { @@ -89,4 +91,71 @@ void GetScoreValues( *b = ledger->GetDoubleState(ledger::kStateScoreB); } +void SetRewardsMainEnabled(bat_ledger::LedgerImpl* ledger, bool enabled) { + DCHECK(ledger); + ledger->SetBooleanState(ledger::kStateEnabled, enabled); +} + +bool GetRewardsMainEnabled(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + return ledger->GetBooleanState(ledger::kStateEnabled); +} + +void SetAutoContributeEnabled(bat_ledger::LedgerImpl* ledger, bool enabled) { + DCHECK(ledger); + ledger->SetBooleanState(ledger::kStateAutoContributeEnabled, enabled); +} + +bool GetAutoContributeEnabled(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + return ledger->GetBooleanState(ledger::kStateAutoContributeEnabled); +} + +void SetAutoContributionAmount( + bat_ledger::LedgerImpl* ledger, + const double amount) { + DCHECK(ledger); + ledger->SetBooleanState(ledger::kStateAutoContributeAmount, amount); +} + +bool GetAutoContributionAmount(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + const double amount = + ledger->GetBooleanState(ledger::kStateAutoContributeAmount); + if (amount == 0.0) { + // TODO(nejc): get default value + } + + return amount; +} + +uint64_t GetReconcileStamp(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + return ledger->GetUint64State(ledger::kStateNextReconcileStamp); +} + +void SetReconcileStamp( + bat_ledger::LedgerImpl* ledger, + const int reconcile_interval) { + DCHECK(ledger); + uint64_t reconcile_stamp = braveledger_time_util::GetCurrentTimeStamp(); + if (reconcile_interval > 0) { + reconcile_stamp += reconcile_interval * 60; + } else { + reconcile_stamp += braveledger_ledger::_reconcile_default_interval; + } + + ledger->SetUint64State(ledger::kStateNextReconcileStamp, reconcile_stamp); +} + +uint64_t GetCreationStamp(bat_ledger::LedgerImpl* ledger) { + DCHECK(ledger); + return ledger->GetUint64State(ledger::kStateCreationStamp); +} + +void SetCreationStamp(bat_ledger::LedgerImpl* ledger, const uint64_t stamp) { + DCHECK(ledger); + ledger->SetUint64State(ledger::kStateCreationStamp, stamp); +} + } // namespace braveledger_state diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h index c7a23df3f186..a9b8c74df291 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/state/state_util.h @@ -49,6 +49,30 @@ void GetScoreValues( double* a, double* b); +void SetRewardsMainEnabled(bat_ledger::LedgerImpl* ledger, bool enabled); + +bool GetRewardsMainEnabled(bat_ledger::LedgerImpl* ledger); + +void SetAutoContributeEnabled(bat_ledger::LedgerImpl* ledger, bool enabled); + +bool GetAutoContributeEnabled(bat_ledger::LedgerImpl* ledger); + +void SetAutoContributionAmount( + bat_ledger::LedgerImpl* ledger, + const double amount); + +bool GetAutoContributionAmount(bat_ledger::LedgerImpl* ledger); + +uint64_t GetReconcileStamp(bat_ledger::LedgerImpl* ledger); + +void SetReconcileStamp( + bat_ledger::LedgerImpl* ledger, + const int reconcile_interval); + +uint64_t GetCreationStamp(bat_ledger::LedgerImpl* ledger); + +void SetCreationStamp(bat_ledger::LedgerImpl* ledger, const uint64_t stamp); + } // namespace braveledger_state #endif // BRAVELEDGER_STATE_STATE_UTIL_H_ diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc index 8e5aab7838a7..f8f82ab98a33 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/create.cc @@ -247,19 +247,18 @@ void Create::RegisterPersonaCallback( } ledger::WalletInfoProperties wallet_info = ledger_->GetWalletInfo(); - double fee_amount = .0; if (!braveledger_bat_helper::getJSONWalletInfo( response.body, - &wallet_info, - &fee_amount)) { + &wallet_info)) { BLOG(0, "Can't get wallet info"); callback(ledger::Result::BAD_REGISTRATION_RESPONSE); return; } + ledger_->SetRewardsMainEnabled(true); + ledger_->SetAutoContributeEnabled(true); ledger_->SetWalletInfo(wallet_info); - ledger_->SetContributionAmount(fee_amount); - ledger_->SetBootStamp(braveledger_time_util::GetCurrentTimeStamp()); + ledger_->SetCreationStamp(braveledger_time_util::GetCurrentTimeStamp()); ledger_->ResetReconcileStamp(); callback(ledger::Result::WALLET_CREATED); } diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc index 2260a9e70954..90af7444d386 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/recover.cc @@ -169,18 +169,13 @@ void Recover::RecoverWalletCallback( ledger::WalletInfoProperties wallet_info = ledger_->GetWalletInfo(); ledger::WalletProperties properties = ledger_->GetWalletProperties(); - double fee_amount = .0; double balance = .0; braveledger_bat_helper::getJSONWalletInfo( response.body, - &wallet_info, - &fee_amount); + &wallet_info); braveledger_bat_helper::getJSONRecoverWallet( response.body, &balance); - if (!ledger_->GetUserChangedContribution()) { - ledger_->SetContributionAmount(fee_amount); - } ledger_->SetWalletProperties(&properties); wallet_info.payment_id = recoveryId; diff --git a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc index b6143623da26..ddc54498efcd 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/internal/wallet/wallet.cc @@ -48,7 +48,7 @@ Wallet::~Wallet() { void Wallet::CreateWalletIfNecessary(ledger::ResultCallback callback) { const auto payment_id = ledger_->GetPaymentId(); - const auto stamp = ledger_->GetBootStamp(); + const auto stamp = ledger_->GetCreationStamp(); if (!payment_id.empty() && stamp != 0) { BLOG(1, "Wallet already exists"); @@ -89,7 +89,7 @@ ledger::WalletPropertiesPtr Wallet::WalletPropertiesToWalletInfo( const ledger::WalletProperties& properties) { ledger::WalletPropertiesPtr wallet = ledger::WalletProperties::New(); wallet->parameters_choices = properties.parameters_choices; - wallet->fee_amount = ledger_->GetContributionAmount(); + wallet->fee_amount = ledger_->GetAutoContributionAmount(); wallet->default_tip_choices = properties.default_tip_choices; wallet->default_monthly_tip_choices = properties.default_monthly_tip_choices; @@ -449,7 +449,7 @@ void Wallet::OnTransferAnonToExternalWalletAddress( void Wallet::GetAnonWalletStatus(ledger::ResultCallback callback) { const std::string payment_id = ledger_->GetPaymentId(); const std::string passphrase = GetWalletPassphrase(); - const uint64_t stamp = ledger_->GetBootStamp(); + const uint64_t stamp = ledger_->GetCreationStamp(); if (!payment_id.empty() && stamp != 0) { BLOG(1, "Wallet is ok"); diff --git a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc index d3926b9e9fa0..b11cfc12150a 100644 --- a/vendor/bat-native-ledger/src/bat/ledger/ledger.cc +++ b/vendor/bat-native-ledger/src/bat/ledger/ledger.cc @@ -14,7 +14,7 @@ Environment _environment = Environment::PRODUCTION; bool is_debug = false; bool is_testing = false; -int reconcile_time = 0; // minutes +int reconcile_interval = 0; // minutes bool short_retries = false; // static diff --git a/vendor/brave-ios/BATBraveRewards.m b/vendor/brave-ios/BATBraveRewards.m index 7ab90670e4f6..41ef46dd14d1 100644 --- a/vendor/brave-ios/BATBraveRewards.m +++ b/vendor/brave-ios/BATBraveRewards.m @@ -97,7 +97,7 @@ - (instancetype)initWithConfiguration:(BATBraveRewardsConfiguration *)configurat BATBraveLedger.environment = configuration.environment; BATBraveLedger.testing = configuration.testing; BATBraveLedger.useShortRetries = configuration.useShortRetries; - BATBraveLedger.reconcileTime = configuration.overridenNumberOfSecondsBetweenReconcile; + BATBraveLedger.reconcileInterval = configuration.overridenNumberOfSecondsBetweenReconcile; [self setupLedgerAndAds]; } diff --git a/vendor/brave-ios/Ledger/BATBraveLedger.h b/vendor/brave-ios/Ledger/BATBraveLedger.h index 58cc02f5cd74..af73a77ace62 100644 --- a/vendor/brave-ios/Ledger/BATBraveLedger.h +++ b/vendor/brave-ios/Ledger/BATBraveLedger.h @@ -73,7 +73,7 @@ NS_SWIFT_NAME(BraveLedger) /// Marks if this is being ran in a test environment. Defaults to false @property (nonatomic, class, getter=isTesting) BOOL testing; /// Number of minutes between reconciles override. Defaults to 0 (no override) -@property (nonatomic, class) int reconcileTime; +@property (nonatomic, class) int reconcileInterval; /// Whether or not to use short contribution retries. Defaults to false @property (nonatomic, class) BOOL useShortRetries; diff --git a/vendor/brave-ios/Ledger/BATBraveLedger.mm b/vendor/brave-ios/Ledger/BATBraveLedger.mm index 2eacb8e3e39a..c2db57cc94b0 100644 --- a/vendor/brave-ios/Ledger/BATBraveLedger.mm +++ b/vendor/brave-ios/Ledger/BATBraveLedger.mm @@ -380,7 +380,7 @@ - (void)removeObserver:(BATBraveLedgerObserver *)observer BATClassLedgerBridge(BOOL, isDebug, setDebug, is_debug) BATClassLedgerBridge(BOOL, isTesting, setTesting, is_testing) -BATClassLedgerBridge(int, reconcileTime, setReconcileTime, reconcile_time) +BATClassLedgerBridge(int, reconcileInterval, setReconcileInterval, reconcile_interval) BATClassLedgerBridge(BOOL, useShortRetries, setUseShortRetries, short_retries) + (BATEnvironment)environment @@ -423,9 +423,7 @@ - (void)createWallet:(void (^)(NSError * _Nullable))completion } error = [NSError errorWithDomain:BATBraveLedgerErrorDomain code:static_cast(result) userInfo:userInfo]; } - - strongSelf.enabled = YES; - strongSelf.autoContributeEnabled = YES; + strongSelf.ads.enabled = [BATBraveAds isCurrentLocaleSupported]; [strongSelf startNotificationTimers]; strongSelf.initializingWallet = NO; @@ -435,6 +433,12 @@ - (void)createWallet:(void (^)(NSError * _Nullable))completion completion(error); } + for (BATBraveLedgerObserver *observer in [strongSelf.observers copy]) { + if (observer.rewardsEnabledStateUpdated) { + observer.rewardsEnabledStateUpdated(strongSelf.isEnabled); + } + } + for (BATBraveLedgerObserver *observer in [strongSelf.observers copy]) { if (observer.walletInitalized) { observer.walletInitalized(static_cast(result)); @@ -1273,17 +1277,16 @@ - (void)setEnabled:(BOOL)enabled allowVideoContributions, setAllowVideoContributions, GetPublisherAllowVideos, SetPublisherAllowVideos) -BATLedgerReadonlyBridge(double, contributionAmount, GetContributionAmount) +BATLedgerReadonlyBridge(double, contributionAmount, GetAutoContributionAmount) - (void)setContributionAmount:(double)contributionAmount { - ledger->SetUserChangedContribution(); - ledger->SetContributionAmount(contributionAmount); + ledger->SetAutoContributionAmount(contributionAmount); } BATLedgerBridge(BOOL, isAutoContributeEnabled, setAutoContributeEnabled, - GetAutoContribute, SetAutoContribute) + GetAutoContributeEnabled, SetAutoContributeEnabled) - (void)setBooleanState:(const std::string&)name value:(bool)value { @@ -1562,7 +1565,7 @@ - (void)showBackupNotificationIfNeccessary { // This is currently not required as the user cannot manage their wallet on mobile... yet /* - auto bootstamp = ledger->GetBootStamp(); + auto bootstamp = ledger->GetCreationStamp(); auto userFunded = [self.prefs[kUserHasFundedKey] boolValue]; auto backupSucceeded = [self.prefs[kBackupSucceededKey] boolValue]; if (userFunded && !backupSucceeded) { @@ -1734,12 +1737,6 @@ - (void)loadLedgerState:(ledger::OnLoadCallback)callback [self startNotificationTimers]; } -- (void)saveLedgerState:(const std::string &)ledger_state callback:(ledger::ResultCallback)callback -{ - const auto result = [self.commonOps saveContents:ledger_state name:"ledger_state.json"]; - callback(result ? ledger::Result::LEDGER_OK : ledger::Result::NO_LEDGER_STATE); -} - - (void)loadPublisherState:(ledger::OnLoadCallback)callback { const auto contents = [self.commonOps loadContentsFromFileWithName:"publisher_state.json"]; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h index bb1a00cd024f..c73de36c8e69 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.h @@ -28,7 +28,6 @@ class NativeLedgerClient : public ledger::LedgerClient { void OnReconcileComplete(ledger::Result result, const std::string & viewing_id, const double amount, const ledger::RewardsType type) override; void OnWalletProperties(ledger::Result result, ledger::WalletPropertiesPtr arg1) override; void ResetState(const std::string & name, ledger::ResultCallback callback) override; - void SaveLedgerState(const std::string & ledger_state, ledger::ResultCallback callback) override; void PublisherListNormalized(ledger::PublisherInfoList list) override; void SaveState(const std::string & name, const std::string & value, ledger::ResultCallback callback) override; void SetConfirmationsIsReady(const bool is_ready) override; diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm index d024169ce78f..a2eb390871a5 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClient.mm @@ -50,9 +50,6 @@ void NativeLedgerClient::ResetState(const std::string & name, ledger::ResultCallback callback) { [bridge_ resetState:name callback:callback]; } -void NativeLedgerClient::SaveLedgerState(const std::string & ledger_state, ledger::ResultCallback callback) { - [bridge_ saveLedgerState:ledger_state callback:callback]; -} void NativeLedgerClient::PublisherListNormalized(ledger::PublisherInfoList list) { [bridge_ publisherListNormalized:std::move(list)]; } diff --git a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h index 3f649fea0d20..7debd31f14d9 100644 --- a/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h +++ b/vendor/brave-ios/Ledger/Generated/NativeLedgerClientBridge.h @@ -21,7 +21,6 @@ - (void)onReconcileComplete:(ledger::Result)result viewingId:(const std::string &)viewing_id type:(const ledger::RewardsType)type amount:(const double)amount; - (void)onWalletProperties:(ledger::Result)result arg1:(ledger::WalletPropertiesPtr)arg1; - (void)resetState:(const std::string &)name callback:(ledger::ResultCallback)callback; -- (void)saveLedgerState:(const std::string &)ledger_state callback:(ledger::ResultCallback)callback; - (void)publisherListNormalized:(ledger::PublisherInfoList)list; - (void)saveState:(const std::string &)name value:(const std::string &)value callback:(ledger::ResultCallback)callback; - (void)setConfirmationsIsReady:(const bool)is_ready;