Skip to content

Commit

Permalink
Added handler to take actions from page and replaced extensions api c…
Browse files Browse the repository at this point in the history
…alls in vpn panel popup
  • Loading branch information
spylogsster committed May 18, 2022
1 parent fe5a1da commit a977eee
Show file tree
Hide file tree
Showing 9 changed files with 44 additions and 14 deletions.
30 changes: 30 additions & 0 deletions browser/ui/webui/brave_vpn/vpn_panel_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,32 @@
#include <utility>

#include "brave/browser/brave_vpn/brave_vpn_service_factory.h"
#include "brave/components/brave_vpn/brave_vpn_constants.h"
#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "brave/components/brave_vpn/brave_vpn_utils.h"
#include "chrome/browser/ui/browser_finder.h"
#include "chrome/browser/ui/browser_tabstrip.h"

namespace {

GURL GetURLForUIType(const std::string& type) {
if (type == "recover" || type == "checkout") {
GURL manage_url = GURL(brave_vpn::GetManageUrl());
DCHECK(manage_url.is_valid());
std::string query = "intent=" + type + "&product=vpn";
GURL::Replacements replacements;
replacements.SetQueryStr(query);
return manage_url.ReplaceComponents(replacements);
} else if (type == "privacy") {
return GURL("https://brave.com/privacy/browser/#vpn");
} else if (type == "about") {
return GURL(brave_vpn::kAboutUrl);
}
DCHECK_EQ(type, "manage");
return GURL(brave_vpn::GetManageUrl());
}

} // namespace

VPNPanelHandler::VPNPanelHandler(
mojo::PendingReceiver<brave_vpn::mojom::PanelHandler> receiver,
Expand Down Expand Up @@ -38,3 +63,8 @@ void VPNPanelHandler::CloseUI() {
embedder->CloseUI();
}
}

void VPNPanelHandler::OpenVpnUI(const std::string& type) {
auto* browser = chrome::FindLastActiveWithProfile(profile_);
chrome::AddTabAt(browser, GetURLForUIType(type), -1, true);
}
1 change: 1 addition & 0 deletions browser/ui/webui/brave_vpn/vpn_panel_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ class VPNPanelHandler : public brave_vpn::mojom::PanelHandler {
// brave_vpn::mojom::PanelHandler:
void ShowUI() override;
void CloseUI() override;
void OpenVpnUI(const std::string& type) override;

private:
mojo::Receiver<brave_vpn::mojom::PanelHandler> receiver_;
Expand Down
3 changes: 1 addition & 2 deletions common/extensions/api/_api_features.json
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,7 @@
"chrome://tab-strip/*",
"chrome://wallet-panel.top-chrome/*",
"chrome://wallet/*",
"chrome://brave-shields.top-chrome/*",
"chrome://vpn-panel.top-chrome/*"
"chrome://brave-shields.top-chrome/*"
]
}, {
"channel": "stable",
Expand Down
3 changes: 3 additions & 0 deletions components/brave_vpn/mojom/brave_vpn.mojom
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ interface PanelHandler {

// Notify the backend that the dialog should be closed.
CloseUI();

// Run purchase action.
OpenVpnUI(string type);
};

interface ServiceObserver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ function ContactSupport (props: Props) {
}

const handlePrivacyPolicyClick = () => {
chrome.tabs.create({ url: 'https://brave.com/privacy/browser/#vpn' })
getPanelBrowserAPI().panelHandler.openVpnUI('privacy')
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@ import Button from '$web-components/button'
import * as S from './style'
import { AlertCircleIcon } from 'brave-ui/components/icons'
import { getLocale } from '../../../../../common/locale'
import { useSelector } from '../../state/hooks'
import ContactSupport from '../contact-support'
import getPanelBrowserAPI from '../../api/panel_browser_api'

function ErrorSubscriptionFailed () {
const productUrls = useSelector(state => state.productUrls)
const [isContactSupportVisible, setContactSupportVisible] = React.useState(false)

const handleEditPayment = () => {
chrome.tabs.create({ url: productUrls?.manage })
getPanelBrowserAPI().panelHandler.openVpnUI('manage')
}

const handleContactSupport = () => setContactSupportVisible(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { getLocale } from '../../../../../common/locale'
import { useSelector } from '../../state/hooks'
import * as S from './style'
import Button from '$web-components/button'
import getPanelBrowserAPI from '../../api/panel_browser_api'

function SellPanel () {
const productUrls = useSelector(state => state.productUrls)
Expand All @@ -18,8 +19,7 @@ function SellPanel () {

const handleClick = (intent: string) => {
if (!productUrls) return
const url = new URL(`?intent=${intent}&product=vpn`, productUrls.manage)
chrome.tabs.create({ url: url.href })
getPanelBrowserAPI().panelHandler.openVpnUI(intent)
}

return (
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import * as React from 'react'

import { getLocale } from '../../../../../common/locale'
import { useSelector } from '../../state/hooks'
import * as S from './style'
import { CaratStrongLeftIcon } from 'brave-ui/components/icons'
import getPanelBrowserAPI from '../../api/panel_browser_api'

interface Props {
closeSettingsPanel: React.MouseEventHandler<HTMLButtonElement>
showContactSupport: React.MouseEventHandler<HTMLAnchorElement>
}

function SettingsPanel (props: Props) {
const productUrls = useSelector(state => state.productUrls)

const handleClick = (entry: string) => {
if (!productUrls) return
chrome.tabs.create({ url: productUrls?.[entry] })
getPanelBrowserAPI().panelHandler.openVpnUI(entry)
}

return (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ BraveVPN.setPanelBrowserApiForTesting({
},
panelHandler: {
showUI: doNothing,
closeUI: doNothing
closeUI: doNothing,
openVpnUI: doNothing
},
serviceHandler: {
addObserver: doNothing,
Expand Down

0 comments on commit a977eee

Please sign in to comment.