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

Get region list from vpn service to webui #10066

Merged
merged 1 commit into from
Oct 8, 2021
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
5 changes: 4 additions & 1 deletion .storybook/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,8 @@ group("storybook") {
# Explicitly defined here so that even when those targets
# are disabled in a regular brave build due to build flags,
# they will be generated before storybook is compiled.
deps = [ "//ui/webui/resources/js:cr.m" ]
deps = [
"//brave/components/brave_vpn:mojom_js",
"//ui/webui/resources/js:cr.m",
]
}
1 change: 0 additions & 1 deletion components/brave_vpn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ preprocess_folder = "preprocessed"
preprocess_mojo_manifest = "preprocessed_mojo_manifest.json"

static_library("brave_vpn") {
assert(enable_brave_vpn)
sources = [
"brave_vpn_service.cc",
"brave_vpn_service.h",
Expand Down
14 changes: 8 additions & 6 deletions components/brave_vpn/brave_vpn.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,6 @@ interface ServiceObserver {
OnPurchasedStateChanged(PurchasedState state);
};

struct Region {
string continent;
string name;
string name_pretty;
};

// Browser-side handler for vpn service things
interface ServiceHandler {
GetConnectionState() => (ConnectionState state);
Expand All @@ -43,7 +37,9 @@ interface ServiceHandler {
CreateVPNConnection();
Connect();
Disconnect();
// Gets all region from internal cache which is fetched from Guardian API
GetAllRegions() => (array<Region> regions);
// Gets region based on user's device timezone
GetDeviceRegion() => (Region device_region);
GetSelectedRegion() => (Region current_region);
SetSelectedRegion(Region region);
Expand Down Expand Up @@ -73,3 +69,9 @@ enum PurchasedState {
PURCHASED,
EXPIRED,
};

struct Region {
string continent;
string name;
string name_pretty;
};
7 changes: 7 additions & 0 deletions components/brave_vpn/brave_vpn_service_desktop.cc
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,13 @@ bool BraveVpnServiceDesktop::ParseAndCacheRegionList(base::Value region_value) {
regions_.push_back(region);
}

// Sort region list alphabetically
std::sort(regions_.begin(), regions_.end(),
[](brave_vpn::mojom::Region& a, brave_vpn::mojom::Region& b) {
return (a.name_pretty < b.name_pretty);
});

// If we can't get region list, we can't determine device region.
if (regions_.empty())
return false;

Expand Down
21 changes: 0 additions & 21 deletions components/brave_vpn/resources/panel/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,6 @@

import("//brave/components/common/typescript.gni")
import("//mojo/public/tools/bindings/mojom.gni")
import("//tools/grit/preprocess_if_expr.gni")
nullhook marked this conversation as resolved.
Show resolved Hide resolved

preprocess_folder = "preprocessed"
preprocess_panel_manifest = "preprocessed_panel_manifest.json"

transpile_web_ui("brave_vpn_panel_ui") {
entry_points = [ [
Expand All @@ -17,8 +13,6 @@ transpile_web_ui("brave_vpn_panel_ui") {
] ]
resource_name = "brave_vpn_panel"
deps = [
":preprocess_panel",
":vpn_panel_api_proxy",
"//brave/components/brave_vpn:mojom_js",
"//brave/components/brave_vpn:preprocess_mojo",
]
Expand All @@ -29,18 +23,3 @@ pack_web_resources("brave_vpn_panel_generated") {
output_dir = "$root_gen_dir/brave/components/brave_vpn/resources/panel"
deps = [ ":brave_vpn_panel_ui" ]
}

preprocess_if_expr("preprocess_panel") {
in_folder = "./"
out_folder = "$target_gen_dir/$preprocess_folder"
out_manifest = "$target_gen_dir/$preprocess_panel_manifest"
in_files = [ "vpn_panel_api_proxy.js" ]
}

js_library("vpn_panel_api_proxy") {
deps = [
"//brave/components/brave_vpn:mojom_js_library_for_compile",
"//ui/webui/resources/js:cr.m",
"//url/mojom:url_mojom_gurl_js_library_for_compile",
]
}
7 changes: 7 additions & 0 deletions components/brave_vpn/resources/panel/api/panel_browser_api.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
// 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/.

// Provide access to all the generated types
export * from 'gen/brave/components/brave_vpn/brave_vpn.mojom.m.js'
10 changes: 10 additions & 0 deletions components/brave_vpn/resources/panel/api/region_interface.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export interface Region {
continent: string
name: string
namePretty: string
}
export interface RegionState {
all?: Array<Region>,
current?: Region,
hasError: boolean
}
Comment on lines +1 to +10
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wondering why we're adding new types when these seem to be in .mojom and now you're pulling in mojom types directly to './panel_browser_api'?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are state types and then there are mojom types. The mojom types will be directly pulled from m.js file in another PR and I will move all state types to another folder dir.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@
// 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/.
import { ConnectionState } from './types/connection_state'
import { ConnectionState } from './panel_browser_api'
import { Region } from './region_interface'

export default class APIProxy {
static getInstance: () => APIProxy
showUI: () => {}
Expand All @@ -12,12 +14,22 @@ export default class APIProxy {
connect: () => {}
disconnect: () => {}
addVPNObserver: (obj: ServiceObserver) => {}
getAllRegions: () => Promise<RegionResponse>
getSelectedRegion: () => Promise<DeviceRegionResponse>
setSelectedRegion: (region: Region) => {}
}
interface ServiceObserver {
onConnectionCreated?: Function
onConnectionRemoved?: Function
onConnectionStateChanged?: Function
onConnectionCreated: Function
onConnectionRemoved: Function
onConnectionStateChanged: Function
onPurchasedStateChanged: Function
}
interface StateResponse {
state: ConnectionState
}
interface RegionResponse {
regions: Array<Region>
}
interface DeviceRegionResponse {
currentRegion: Region
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,18 @@ export default class VpnPanelApiProxyImpl {
return this.vpnService.disconnect();
}

getAllRegions() {
return this.vpnService.getAllRegions();
}

getSelectedRegion() {
return this.vpnService.getSelectedRegion();
}

setSelectedRegion(region) {
return this.vpnService.setSelectedRegion(region);
}

addVPNObserver(obj) {
const serviceObserverReceiver = new braveVpn.mojom.ServiceObserverReceiver(obj)
this.vpnService.addObserver(serviceObserverReceiver.$.bindNewPipeAndPassRemote())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import * as S from './style'
import { Button } from 'brave-ui'
import { AlertCircleIcon } from 'brave-ui/components/icons'
import locale from '../../constants/locale'

import { RegionState } from '../../api/region_interface'
interface Props {
onTryAgainClick: Function
onChooseServerClick: Function
region: string
region: RegionState
}

function ErrorPanel (props: Props) {
Expand All @@ -21,7 +21,8 @@ function ErrorPanel (props: Props) {
<AlertCircleIcon color='#84889C' />
</S.IconBox>
<S.ReasonTitle>{locale.cantConnectError}</S.ReasonTitle>
<S.ReasonDesc>Brave Firewall + VPN couldn't connect to the {props.region} server.
<S.ReasonDesc>Brave Firewall + VPN couldn't connect to the{' '}
{props.region?.current?.namePretty} server.
You can try again, or choose another.</S.ReasonDesc>
<S.ActionArea>
<Button
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ import StatusIndicator from '../../components/status-indicator'
import PanelBox from '../../components/panel-box'
import Toggle from '../../components/toggle'
import { SettingsAdvancedIcon, CaratStrongRightIcon } from 'brave-ui/components/icons'
import { ConnectionState } from '../../types/connection_state'

import { ConnectionState } from '../../api/panel_browser_api'
import { Region } from '../../api/region_interface'
interface Props {
isOn: boolean
status: ConnectionState
region: string
region: Region
onToggleClick: () => void
onSelectRegionButtonClick: () => void
}
Expand Down Expand Up @@ -41,7 +41,7 @@ function MainPanel (props: Props) {
type='button'
onClick={props.onSelectRegionButtonClick}
>
<S.RegionLabel>{props.region}</S.RegionLabel>
<S.RegionLabel>{props.region.namePretty}</S.RegionLabel>
<CaratStrongRightIcon />
</S.RegionSelectorButton>
</S.PanelContent>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import * as React from 'react'
import * as S from './style'
import { Button, Radio } from 'brave-ui'
import { CaratStrongLeftIcon } from 'brave-ui/components/icons'
import { Region } from '../../api/region_interface'
import locale from '../../constants/locale'
interface Props {
onDone: Function
onRegionClick: Function
onConnectClick: Function
regions: Array<Region>
selectedRegion: Region
}

function SelectRegion (props: Props) {
// TODO(nullhook): Scroll to the selected radio input when this component loads
// TODO(nullhook): Add search region functionality
const handleGoBackClick = () => {
props.onDone()
}

const handleItemClick = (currentRegion: Region) => {
props.onRegionClick(currentRegion)
}

const handleConnect = () => {
props.onConnectClick()
}

return (
<S.Box>
<S.PanelContent>
<S.PanelHeader>
<S.BackButton
type='button'
onClick={handleGoBackClick}
>
<CaratStrongLeftIcon />
</S.BackButton>
</S.PanelHeader>
<S.RegionList>
<Radio
value={{ [props.selectedRegion.name]: true }}
size={'small'}
disabled={false}
>
{props.regions.map((entry: Region, i: number) => (
<div
key={i}
data-value={entry.name}
>
<S.RegionLabel
onClick={handleItemClick.bind(this, entry)}
>
{entry.namePretty}
</S.RegionLabel>
</div>
))}
</Radio>
</S.RegionList>
<S.ActionArea>
<Button
onClick={handleConnect}
level='primary'
type='accent'
brand='rewards'
text={locale.connectLabel}
/>
</S.ActionArea>
</S.PanelContent>
</S.Box>
)
}

export default SelectRegion
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export const PanelHeader = styled.section`
`

export const PanelContent = styled.section`
padding: 0 0 25px 0;
padding: 0 0 24px 0;
`

export const RegionList = styled.div`
Expand Down Expand Up @@ -66,3 +66,13 @@ export const BackButton = styled.button`
fill: ${(p) => p.theme.color.interactive05}
}
`

export const ActionArea = styled.div`
box-sizing: border-box;
width: 100%;
padding: 18px 18px 0 18px;

button {
width: 100%;
}
`

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import * as React from 'react'
import * as S from './style'
import locale from '../../constants/locale'
import { ConnectionState } from '../../types/connection_state'
import { ConnectionState } from '../../api/panel_browser_api'
import { LoaderIcon } from 'brave-ui/components/icons'
interface Props {
status: ConnectionState
Expand Down
Loading