Skip to content

Commit

Permalink
Handled vpn connection state managing in global instance.
Browse files Browse the repository at this point in the history
fix brave/brave-browser#25789

Initially vpn state was per-profile.
However, we decided to share vpn state between all profiles.
With this change, one profile can have same vpn status if other
profile is purchased user.
So far vpn service manages two states.
The one is purchased state and the other is connection state.
Purchased state is still per-profile because skus service is per profile.
Although purchased state is managed by each profile,
same state is shared for all profiles eventually because skus service's
internal data is global.

In this PR connection state managing is moved from per-profile vpn service
to BraveVPNOSConnectionAPI global instance. Before this PR,
BraveVPNOSConnectionAPI didn't have state and only provided abstract interface
for os dependent vpn command such as connect or disconnect.
In the os system, there is only one connection state.
So, connection state also should be managed globally.
With that each profile can see same connection state.
  • Loading branch information
simonhong committed Oct 19, 2022
1 parent 991c593 commit 8dbfe02
Show file tree
Hide file tree
Showing 21 changed files with 941 additions and 720 deletions.
8 changes: 7 additions & 1 deletion browser/brave_vpn/brave_vpn_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
#include "brave/browser/brave_vpn/brave_vpn_service_factory.h"

#include "base/feature_list.h"
#include "brave/browser/brave_browser_process.h"
#include "brave/browser/profiles/profile_util.h"
#include "brave/browser/skus/skus_service_factory.h"
#include "brave/components/brave_vpn/brave_vpn_os_connection_api.h"
#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "brave/components/brave_vpn/brave_vpn_utils.h"
#include "brave/components/skus/common/features.h"
Expand Down Expand Up @@ -50,6 +50,12 @@ BraveVpnServiceFactory::BraveVpnServiceFactory()
"BraveVpnService",
BrowserContextDependencyManager::GetInstance()) {
DependsOn(skus::SkusServiceFactory::GetInstance());
#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC)
auto* connection_api = BraveVPNOSConnectionAPI::GetInstance();
connection_api->set_shared_url_loader_factory(
g_browser_process->shared_url_loader_factory());
connection_api->set_local_prefs(g_browser_process->local_state());
#endif
}

BraveVpnServiceFactory::~BraveVpnServiceFactory() = default;
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/views/toolbar/brave_vpn_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ void BraveVPNButton::UpdateButtonState() {
}

bool BraveVPNButton::IsConnected() {
return service_->is_connected();
return service_->IsConnected();
}

void BraveVPNButton::OnButtonPressed(const ui::Event& event) {
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/views/toolbar/brave_vpn_status_label.cc
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ gfx::Size BraveVPNStatusLabel::CalculatePreferredSize() const {
}

void BraveVPNStatusLabel::UpdateState() {
const auto state = service_->connection_state();
const auto state = service_->GetConnectionState();

SetText(brave_l10n::GetLocalizedResourceUTF16String(
GetStringIdForConnectionState(state)));
Expand Down
2 changes: 1 addition & 1 deletion browser/ui/views/toolbar/brave_vpn_toggle_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void BraveVPNToggleButton::OnButtonPressed(const ui::Event& event) {
}

void BraveVPNToggleButton::UpdateState() {
const auto state = service_->connection_state();
const auto state = service_->GetConnectionState();
bool is_on = (state == ConnectionState::CONNECTING ||
state == ConnectionState::CONNECTED);
SetIsOn(is_on);
Expand Down
16 changes: 16 additions & 0 deletions components/brave_vpn/brave_vpn_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,22 @@ constexpr char kRegionNamePrettyKey[] = "name-pretty";
constexpr char kRegionCountryIsoCodeKey[] = "country-iso-code";
constexpr char kCreateSupportTicket[] = "api/v1.2/partners/support-ticket";

constexpr char kVpnHost[] = "connect-api.guardianapp.com";
constexpr char kAllServerRegions[] = "api/v1/servers/all-server-regions";
constexpr char kTimezonesForRegions[] =
"api/v1.1/servers/timezones-for-regions";
constexpr char kHostnameForRegion[] = "api/v1.2/servers/hostnames-for-region";
constexpr char kProfileCredential[] = "api/v1.1/register-and-create";
constexpr char kCredential[] = "api/v1.3/device/";
constexpr char kVerifyPurchaseToken[] = "api/v1.1/verify-purchase-token";
constexpr char kCreateSubscriberCredentialV12[] =
"api/v1.2/subscriber-credential/create";
constexpr int kP3AIntervalHours = 24;

#if !BUILDFLAG(IS_ANDROID)
constexpr char kTokenNoLongerValid[] = "Token No Longer Valid";
#endif // !BUILDFLAG(IS_ANDROID)

} // namespace brave_vpn

#endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_CONSTANTS_H_
Loading

0 comments on commit 8dbfe02

Please sign in to comment.