Skip to content

Commit

Permalink
Refresh subscriber credential in background
Browse files Browse the repository at this point in the history
Timer for getting new subscriber credential will be fired when current
credential is expired.
  • Loading branch information
simonhong committed Nov 1, 2022
1 parent 74f8f00 commit 9ab1d10
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 1 deletion.
30 changes: 30 additions & 0 deletions components/brave_vpn/brave_vpn_service.cc
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ BraveVpnService::~BraveVpnService() = default;

void BraveVpnService::CheckInitialState() {
if (HasValidSubscriberCredential(local_prefs_)) {
ScheduleSubscriberCredentialRefresh();

#if BUILDFLAG(IS_ANDROID)
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::PURCHASED);
// Android has its own region data managing logic.
Expand Down Expand Up @@ -725,6 +727,10 @@ void BraveVpnService::OnGetSubscriberCredentialV12(

SetSubscriberCredential(local_prefs_, subscriber_credential, expiration_time);

// Launch one-shot timer for refreshing subscriber_credential before it's
// expired.
ScheduleSubscriberCredentialRefresh();

#if BUILDFLAG(IS_ANDROID)
SetPurchasedState(GetCurrentEnvironment(), PurchasedState::PURCHASED);
#else
Expand All @@ -742,6 +748,30 @@ void BraveVpnService::OnGetSubscriberCredentialV12(
#endif
}

void BraveVpnService::ScheduleSubscriberCredentialRefresh() {
if (subs_cred_refresh_timer_.IsRunning())
subs_cred_refresh_timer_.Stop();

const auto expiration_time = GetExpirationTime(local_prefs_);
if (!expiration_time)
return;

VLOG(2) << "Schedule subscriber credential fetching after "
<< *expiration_time - base::Time::Now();
subs_cred_refresh_timer_.Start(
FROM_HERE, *expiration_time - base::Time::Now(),
base::BindOnce(&BraveVpnService::RefreshSubscriberCredential,
base::Unretained(this)));
}

void BraveVpnService::RefreshSubscriberCredential() {
VLOG(2) << "Refresh subscriber credential";

// Clear current credentials to get newer one.
ClearSubscriberCredential(local_prefs_);
ReloadPurchasedState();
}

// TODO(simonhong): Should move p3a to BraveVPNOSConnectionAPI?
void BraveVpnService::InitP3A() {
p3a_timer_.Start(FROM_HERE, base::Hours(kP3AIntervalHours), this,
Expand Down
3 changes: 3 additions & 0 deletions components/brave_vpn/brave_vpn_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,8 @@ class BraveVpnService :
void OnGetSubscriberCredentialV12(const base::Time& expiration_time,
const std::string& subscriber_credential,
bool success);
void ScheduleSubscriberCredentialRefresh();
void RefreshSubscriberCredential();

// Check initial purchased/connected state.
void CheckInitialState();
Expand Down Expand Up @@ -247,6 +249,7 @@ class BraveVpnService :
mojo::RemoteSet<mojom::ServiceObserver> observers_;
BraveVpnAPIRequest api_request_;
base::RepeatingTimer p3a_timer_;
base::OneShotTimer subs_cred_refresh_timer_;
base::WeakPtrFactory<BraveVpnService> weak_ptr_factory_{this};
};

Expand Down
17 changes: 16 additions & 1 deletion components/brave_vpn/brave_vpn_service_helper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
#include "brave/components/brave_vpn/pref_names.h"
#include "brave/components/skus/browser/skus_utils.h"
#include "components/prefs/pref_service.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

namespace brave_vpn {

Expand Down Expand Up @@ -231,6 +230,22 @@ std::string GetSubscriberCredential(PrefService* local_prefs) {
return *cred;
}

absl::optional<base::Time> GetExpirationTime(PrefService* local_prefs) {
if (!HasValidSubscriberCredential(local_prefs))
return absl::nullopt;

const base::Value::Dict& sub_cred_dict =
local_prefs->GetDict(prefs::kBraveVPNSubscriberCredential);

const base::Value* expiration_time_value =
sub_cred_dict.Find(kSubscriberCredentialExpirationKey);

if (!expiration_time_value)
return absl::nullopt;

return base::ValueToTime(expiration_time_value);
}

void SetSubscriberCredential(PrefService* local_prefs,
const std::string& subscriber_credential,
const base::Time& expiration_time) {
Expand Down
2 changes: 2 additions & 0 deletions components/brave_vpn/brave_vpn_service_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

#include "base/values.h"
#include "brave/components/brave_vpn/mojom/brave_vpn.mojom.h"
#include "third_party/abseil-cpp/absl/types/optional.h"

class PrefService;

Expand Down Expand Up @@ -42,6 +43,7 @@ mojom::RegionPtr GetRegionPtrWithNameFromRegionList(
bool IsValidCredentialSummary(const base::Value& summary);
bool HasValidSubscriberCredential(PrefService* local_prefs);
std::string GetSubscriberCredential(PrefService* local_prefs);
absl::optional<base::Time> GetExpirationTime(PrefService* local_prefs);
void SetSubscriberCredential(PrefService* local_prefs,
const std::string& subscriber_credential,
const base::Time& expiration_time);
Expand Down

0 comments on commit 9ab1d10

Please sign in to comment.