diff --git a/components/brave_vpn/brave_vpn_service.cc b/components/brave_vpn/brave_vpn_service.cc index 0c5f0b01e15b..ec52ca1ef5b5 100644 --- a/components/brave_vpn/brave_vpn_service.cc +++ b/components/brave_vpn/brave_vpn_service.cc @@ -101,7 +101,16 @@ void BraveVpnService::CheckInitialState() { ScheduleBackgroundRegionDataFetch(); #endif } else { - ClearSubscriberCredential(local_prefs_); + // Try to reload purchased state if cached credential is not valid because + // it could be invalidated when not running. + if (HasSubscriberCredential(local_prefs_)) { + VLOG(2) << __func__ << " " + << "Try to reload purchased as invalid credential is stored."; + ClearSubscriberCredential(local_prefs_); + ReloadPurchasedState(); + } else { + ClearSubscriberCredential(local_prefs_); + } } } diff --git a/components/brave_vpn/brave_vpn_service_helper.cc b/components/brave_vpn/brave_vpn_service_helper.cc index 0f1c1b139ad4..2774519af477 100644 --- a/components/brave_vpn/brave_vpn_service_helper.cc +++ b/components/brave_vpn/brave_vpn_service_helper.cc @@ -224,6 +224,12 @@ bool HasValidSubscriberCredential(PrefService* local_prefs) { return true; } +bool HasSubscriberCredential(PrefService* local_prefs) { + const base::Value::Dict& sub_cred_dict = + local_prefs->GetDict(prefs::kBraveVPNSubscriberCredential); + return !sub_cred_dict.empty(); +} + std::string GetSubscriberCredential(PrefService* local_prefs) { if (!HasValidSubscriberCredential(local_prefs)) return ""; diff --git a/components/brave_vpn/brave_vpn_service_helper.h b/components/brave_vpn/brave_vpn_service_helper.h index ac63b259f390..9d7adda83615 100644 --- a/components/brave_vpn/brave_vpn_service_helper.h +++ b/components/brave_vpn/brave_vpn_service_helper.h @@ -44,6 +44,7 @@ mojom::RegionPtr GetRegionPtrWithNameFromRegionList( const std::vector region_list); bool IsValidCredentialSummary(const base::Value& summary); bool HasValidSubscriberCredential(PrefService* local_prefs); +bool HasSubscriberCredential(PrefService* local_prefs); std::string GetSubscriberCredential(PrefService* local_prefs); absl::optional GetExpirationTime(PrefService* local_prefs); void SetSubscriberCredential(PrefService* local_prefs, diff --git a/components/brave_vpn/brave_vpn_unittest.cc b/components/brave_vpn/brave_vpn_unittest.cc index 247e7de8aed1..648b6f24b0f4 100644 --- a/components/brave_vpn/brave_vpn_unittest.cc +++ b/components/brave_vpn/brave_vpn_unittest.cc @@ -538,6 +538,12 @@ class BraveVPNServiceTest : public testing::Test { base::Time::Now() + base::Seconds(10)); } + void SetInvalidSubscriberCredential() { + // Set expired date. + SetSubscriberCredential(&local_pref_service_, "subscriber_credential", + base::Time::Now() - base::Seconds(10)); + } + std::string SetupTestingStoreForEnv(const std::string& env, bool active_subscription = true) { std::string domain = skus::GetDomain("vpn", env); @@ -1078,6 +1084,13 @@ TEST_F(BraveVPNServiceTest, CheckInitialPurchasedStateTest) { SetValidSubscriberCredential(); ResetVpnService(); EXPECT_EQ(PurchasedState::LOADING, GetPurchasedStateSync()); + + // Set in-valid subscriber credential but not empty to pretend it's purchased + // user but expired while browser is terminated. + // In this case, service should try to reload purchased state at startup. + SetInvalidSubscriberCredential(); + ResetVpnService(); + EXPECT_EQ(PurchasedState::LOADING, GetPurchasedStateSync()); } TEST_F(BraveVPNServiceTest, SubscribedCredentials) {