Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Try to refresh subs credential on startup (uplift to 1.46.x) #15861

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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