Skip to content

Commit

Permalink
Try to refresh subs credential on startup
Browse files Browse the repository at this point in the history
fix brave/brave-browser#26607

If cached credential is expired at startup, current user could be
purchased user but it could be expired while terminated.
So, trying to refresh credential by reloading purchased state at
startup.
  • Loading branch information
simonhong committed Nov 8, 2022
1 parent 552de92 commit be62767
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 1 deletion.
11 changes: 10 additions & 1 deletion components/brave_vpn/brave_vpn_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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_);
}
}
}

Expand Down
6 changes: 6 additions & 0 deletions components/brave_vpn/brave_vpn_service_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 "";
Expand Down
1 change: 1 addition & 0 deletions components/brave_vpn/brave_vpn_service_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ mojom::RegionPtr GetRegionPtrWithNameFromRegionList(
const std::vector<mojom::Region> 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<base::Time> GetExpirationTime(PrefService* local_prefs);
void SetSubscriberCredential(PrefService* local_prefs,
Expand Down
13 changes: 13 additions & 0 deletions components/brave_vpn/brave_vpn_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit be62767

Please sign in to comment.