Skip to content

Commit

Permalink
Merge pull request #10261 from brave/brave_vpn_hostnames
Browse files Browse the repository at this point in the history
Fetch and cache hostnames list for selected region
  • Loading branch information
simonhong authored Sep 28, 2021
2 parents 41155b8 + 658dad1 commit 932dacd
Show file tree
Hide file tree
Showing 5 changed files with 138 additions and 0 deletions.
1 change: 1 addition & 0 deletions components/brave_vpn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ static_library("brave_vpn") {
sources += [
"brave_vpn_connection_info.cc",
"brave_vpn_connection_info.h",
"brave_vpn_data_types.h",
"brave_vpn_os_connection_api.cc",
"brave_vpn_os_connection_api.h",
"brave_vpn_os_connection_api_sim.cc",
Expand Down
22 changes: 22 additions & 0 deletions components/brave_vpn/brave_vpn_data_types.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* Copyright (c) 2021 The Brave Authors. All rights reserved.
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */

#ifndef BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_DATA_TYPES_H_
#define BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_DATA_TYPES_H_

#include <string>

namespace brave_vpn {

struct Hostname {
std::string hostname;
std::string display_name;
bool is_offline;
int capacity_score;
};

} // namespace brave_vpn

#endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_DATA_TYPES_H_
60 changes: 60 additions & 0 deletions components/brave_vpn/brave_vpn_service_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,66 @@ void BraveVpnServiceDesktop::SetSelectedRegion(
dict->SetStringKey(kRegionContinentKey, region_ptr->continent);
dict->SetStringKey(kRegionNameKey, region_ptr->name);
dict->SetStringKey(kRegionNamePrettyKey, region_ptr->name_pretty);

// Start hostname fetching for selected region.
FetchHostnamesForRegion(region_ptr->name);
}

void BraveVpnServiceDesktop::FetchHostnamesForRegion(const std::string& name) {
// Unretained is safe here becasue this class owns request helper.
GetHostnamesForRegion(
base::BindOnce(&BraveVpnServiceDesktop::OnFetchHostnames,
base::Unretained(this), name),
name);
}

void BraveVpnServiceDesktop::OnFetchHostnames(const std::string& region,
const std::string& hostnames,
bool success) {
if (!success) {
// TODO(simonhong): Retry?
return;
}

absl::optional<base::Value> value = base::JSONReader::Read(hostnames);
if (value && value->is_list()) {
ParseAndCacheHostnames(region, std::move(*value));
return;
}

// TODO(simonhong): Retry?
}

void BraveVpnServiceDesktop::ParseAndCacheHostnames(
const std::string& region,
base::Value hostnames_value) {
DCHECK(hostnames_value.is_list());
if (!hostnames_value.is_list())
return;

constexpr char kHostnameKey[] = "hostname";
constexpr char kDisplayNameKey[] = "display-name";
constexpr char kOfflineKey[] = "offline";
constexpr char kCapacityScoreKey[] = "capacity-score";
std::vector<brave_vpn::Hostname> hostnames;
for (const auto& value : hostnames_value.GetList()) {
DCHECK(value.is_dict());
if (!value.is_dict())
continue;

const std::string* hostname_str = value.FindStringKey(kHostnameKey);
const std::string* display_name_str = value.FindStringKey(kDisplayNameKey);
absl::optional<bool> offline = value.FindBoolKey(kOfflineKey);
absl::optional<int> capacity_score = value.FindIntKey(kCapacityScoreKey);

if (!hostname_str || !display_name_str || !offline || !capacity_score)
continue;

hostnames.push_back(brave_vpn::Hostname{*hostname_str, *display_name_str,
*offline, *capacity_score});
}

hostnames_[region] = std::move(hostnames);
}

void BraveVpnServiceDesktop::SetPurchasedState(PurchasedState state) {
Expand Down
10 changes: 10 additions & 0 deletions components/brave_vpn/brave_vpn_service_desktop.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,11 @@
#include <string>
#include <vector>

#include "base/containers/flat_map.h"
#include "base/scoped_observation.h"
#include "brave/components/brave_vpn/brave_vpn.mojom.h"
#include "brave/components/brave_vpn/brave_vpn_connection_info.h"
#include "brave/components/brave_vpn/brave_vpn_data_types.h"
#include "brave/components/brave_vpn/brave_vpn_os_connection_api.h"
#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
Expand Down Expand Up @@ -75,6 +77,7 @@ class BraveVpnServiceDesktop
friend class BraveAppMenuBrowserTest;
friend class BraveBrowserCommandControllerTest;
FRIEND_TEST_ALL_PREFIXES(BraveVPNTest, RegionDataTest);
FRIEND_TEST_ALL_PREFIXES(BraveVPNTest, HostnamesTest);

// BraveVpnService overrides:
void Shutdown() override;
Expand All @@ -94,6 +97,12 @@ class BraveVpnServiceDesktop
void ParseAndCacheRegionList(base::Value region_value);
void OnFetchTimezones(const std::string& timezones_list, bool success);
void ParseAndCacheDefaultRegionName(base::Value timezons_value);
void FetchHostnamesForRegion(const std::string& name);
void OnFetchHostnames(const std::string& region,
const std::string& hostnames,
bool success);
void ParseAndCacheHostnames(const std::string& region,
base::Value hostnames_value);
void SetDeviceRegion(const std::string& name);
void SetFallbackDeviceRegion();
std::string GetCurrentTimeZone();
Expand All @@ -104,6 +113,7 @@ class BraveVpnServiceDesktop
}

PrefService* prefs_ = nullptr;
base::flat_map<std::string, std::vector<brave_vpn::Hostname>> hostnames_;
std::vector<brave_vpn::mojom::Region> regions_;
brave_vpn::mojom::Region device_region_;
ConnectionState connection_state_ = ConnectionState::DISCONNECTED;
Expand Down
45 changes: 45 additions & 0 deletions components/brave_vpn/brave_vpn_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,41 @@ class BraveVPNTest : public testing::Test {
])";
}

std::string GetHostnamesData() {
return R"([
{
"hostname": "host-1.brave.com",
"display-name": "host-1",
"offline": false,
"capacity-score": 0
},
{
"hostname": "host-2.brave.com",
"display-name": "host-2",
"offline": false,
"capacity-score": 1
},
{
"hostname": "host-3.brave.com",
"display-name": "Singapore",
"offline": false,
"capacity-score": 0
},
{
"hostname": "host-4.brave.com",
"display-name": "host-4",
"offline": false,
"capacity-score": 0
},
{
"hostname": "host-5.brave.com",
"display-name": "host-5",
"offline": false,
"capacity-score": 1
}
])";
}

content::BrowserTaskEnvironment task_environment_;
TestingPrefServiceSimple pref_service_;
std::unique_ptr<BraveVpnServiceDesktop> service_;
Expand Down Expand Up @@ -171,3 +206,13 @@ TEST_F(BraveVPNTest, RegionDataTest) {
service_->OnFetchTimezones(GetTimeZonesData(), true);
EXPECT_EQ(service_->regions_[0], service_->device_region_);
}

TEST_F(BraveVPNTest, HostnamesTest) {
// Set valid hostnames list
service_->OnFetchHostnames("region-a", GetHostnamesData(), true);
EXPECT_EQ(5UL, service_->hostnames_["region-a"].size());

// Set invalid hostnames list
service_->OnFetchHostnames("invalid-region-b", "", false);
EXPECT_EQ(0UL, service_->hostnames_["invalid-region-b"].size());
}

0 comments on commit 932dacd

Please sign in to comment.