Skip to content

Commit

Permalink
Merge pull request #9836 from brave/brave_vpn
Browse files Browse the repository at this point in the history
Wired up BraveVpnService with BraveVPNOSConnectionAPI
  • Loading branch information
simonhong authored Aug 25, 2021
2 parents 8ac5d7b + 0464df4 commit 9a6dc74
Show file tree
Hide file tree
Showing 17 changed files with 459 additions and 85 deletions.
27 changes: 26 additions & 1 deletion browser/brave_vpn/brave_vpn_service_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,41 @@

#include "brave/browser/brave_vpn/brave_vpn_service_factory.h"

#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "chrome/browser/profiles/incognito_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "components/keyed_service/content/browser_context_dependency_manager.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/storage_partition.h"

#if defined(OS_WIN) || defined(OS_MAC)
#include "brave/components/brave_vpn/brave_vpn_service_desktop.h"
#endif

#if defined(OS_ANDROID)
#include "brave/components/brave_vpn/brave_vpn_service.h"
#endif

// static
BraveVpnServiceFactory* BraveVpnServiceFactory::GetInstance() {
return base::Singleton<BraveVpnServiceFactory>::get();
}

#if defined(OS_WIN) || defined(OS_MAC)
// static
BraveVpnServiceDesktop* BraveVpnServiceFactory::GetForProfile(
Profile* profile) {
return static_cast<BraveVpnServiceDesktop*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
#endif

#if defined(OS_ANDROID)
// static
BraveVpnService* BraveVpnServiceFactory::GetForProfile(Profile* profile) {
return static_cast<BraveVpnService*>(
GetInstance()->GetServiceForBrowserContext(profile, true));
}
#endif

BraveVpnServiceFactory::BraveVpnServiceFactory()
: BrowserContextKeyedServiceFactory(
Expand All @@ -35,7 +53,14 @@ KeyedService* BraveVpnServiceFactory::BuildServiceInstanceFor(
auto* default_storage_partition = context->GetDefaultStoragePartition();
auto shared_url_loader_factory =
default_storage_partition->GetURLLoaderFactoryForBrowserProcess();

#if defined(OS_WIN) || defined(OS_MAC)
return new BraveVpnServiceDesktop(shared_url_loader_factory);
#endif

#if defined(OS_ANDROID)
return new BraveVpnService(shared_url_loader_factory);
#endif
}

content::BrowserContext* BraveVpnServiceFactory::GetBrowserContextToUse(
Expand Down
15 changes: 14 additions & 1 deletion browser/brave_vpn/brave_vpn_service_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,25 @@
#include "base/memory/singleton.h"
#include "components/keyed_service/content/browser_context_keyed_service_factory.h"

class BraveVpnService;
class Profile;

#if defined(OS_WIN) || defined(OS_MAC)
class BraveVpnServiceDesktop;
#endif

#if defined(OS_ANDROID)
class BraveVpnService;
#endif

class BraveVpnServiceFactory : public BrowserContextKeyedServiceFactory {
public:
#if defined(OS_WIN) || defined(OS_MAC)
static BraveVpnServiceDesktop* GetForProfile(Profile* profile);
#endif

#if defined(OS_ANDROID)
static BraveVpnService* GetForProfile(Profile* profile);
#endif
static BraveVpnServiceFactory* GetInstance();

BraveVpnServiceFactory(const BraveVpnServiceFactory&) = delete;
Expand Down
8 changes: 8 additions & 0 deletions browser/ui/views/toolbar/brave_vpn_button.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,14 @@ void BraveVPNButton::OnConnectionStateChanged(bool connected) {
UpdateButtonState();
}

void BraveVPNButton::OnConnectionCreated() {
// Do nothing.
}

void BraveVPNButton::OnConnectionRemoved() {
// Do nothing.
}

void BraveVPNButton::UpdateColorsAndInsets() {
if (const auto* tp = GetThemeProvider()) {
const gfx::Insets paint_insets =
Expand Down
13 changes: 9 additions & 4 deletions browser/ui/views/toolbar/brave_vpn_button.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,13 @@

#include "base/scoped_observation.h"
#include "brave/browser/ui/webui/brave_vpn/vpn_panel_ui.h"
#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "brave/components/brave_vpn/brave_vpn_service_desktop.h"
#include "chrome/browser/ui/views/bubble/webui_bubble_manager.h"
#include "chrome/browser/ui/views/toolbar/toolbar_button.h"
#include "ui/base/metadata/metadata_header_macros.h"
class BraveVPNButton : public ToolbarButton, public BraveVpnService::Observer {

class BraveVPNButton : public ToolbarButton,
public BraveVpnServiceDesktop::Observer {
public:
METADATA_HEADER(BraveVPNButton);

Expand All @@ -24,6 +26,8 @@ class BraveVPNButton : public ToolbarButton, public BraveVpnService::Observer {

// BraveVpnService::Observer overrides:
void OnConnectionStateChanged(bool connected) override;
void OnConnectionCreated() override;
void OnConnectionRemoved() override;

private:
void UpdateColorsAndInsets() override;
Expand All @@ -35,8 +39,9 @@ class BraveVPNButton : public ToolbarButton, public BraveVpnService::Observer {
void OnButtonPressed(const ui::Event& event);
void ShowBraveVPNPanel();

BraveVpnService* service_ = nullptr;
base::ScopedObservation<BraveVpnService, BraveVpnService::Observer>
BraveVpnServiceDesktop* service_ = nullptr;
base::ScopedObservation<BraveVpnServiceDesktop,
BraveVpnServiceDesktop::Observer>
observation_{this};
WebUIBubbleManagerT<VPNPanelUI> webui_bubble_manager_;
};
Expand Down
4 changes: 3 additions & 1 deletion browser/ui/webui/brave_vpn/vpn_panel_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

#include <utility>

#include "brave/components/brave_vpn/brave_vpn_service_desktop.h"

VPNPanelHandler::VPNPanelHandler(
mojo::PendingReceiver<brave_vpn::mojom::PanelHandler> receiver,
ui::MojoBubbleWebUIController* webui_controller,
BraveVpnService* service)
BraveVpnServiceDesktop* service)
: receiver_(this, std::move(receiver)),
webui_controller_(webui_controller),
service_(service) {}
Expand Down
8 changes: 5 additions & 3 deletions browser/ui/webui/brave_vpn/vpn_panel_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include <vector>

#include "brave/components/brave_vpn/brave_vpn.mojom.h"
#include "brave/components/brave_vpn/brave_vpn_service.h"
#include "brave/components/brave_vpn/brave_vpn_service_desktop.h"
#include "mojo/public/cpp/bindings/pending_receiver.h"
#include "mojo/public/cpp/bindings/receiver.h"
#include "ui/webui/mojo_bubble_web_ui_controller.h"
Expand All @@ -19,6 +19,8 @@ namespace content {
class WebUI;
} // namespace content

class BraveVpnServiceDesktop;

class VPNPanelHandler : public brave_vpn::mojom::PanelHandler {
public:
using GetWebContentsForTabCallback =
Expand All @@ -27,7 +29,7 @@ class VPNPanelHandler : public brave_vpn::mojom::PanelHandler {
VPNPanelHandler(
mojo::PendingReceiver<brave_vpn::mojom::PanelHandler> receiver,
ui::MojoBubbleWebUIController* webui_controller,
BraveVpnService* service);
BraveVpnServiceDesktop* service);

VPNPanelHandler(const VPNPanelHandler&) = delete;
VPNPanelHandler& operator=(const VPNPanelHandler&) = delete;
Expand All @@ -41,7 +43,7 @@ class VPNPanelHandler : public brave_vpn::mojom::PanelHandler {
private:
mojo::Receiver<brave_vpn::mojom::PanelHandler> receiver_;
ui::MojoBubbleWebUIController* const webui_controller_;
BraveVpnService* service_;
BraveVpnServiceDesktop* service_;
};

#endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_VPN_VPN_PANEL_HANDLER_H_
50 changes: 31 additions & 19 deletions components/brave_vpn/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,6 @@ preprocess_mojo_manifest = "preprocessed_mojo_manifest.json"

static_library("brave_vpn") {
sources = [
"brave_vpn_connection_info.cc",
"brave_vpn_connection_info.h",
"brave_vpn_os_connection_api.cc",
"brave_vpn_os_connection_api.h",
"brave_vpn_service.cc",
"brave_vpn_service.h",
"brave_vpn_utils.cc",
Expand All @@ -25,36 +21,52 @@ static_library("brave_vpn") {
]

deps = [
":brave_vpn_internal",
":mojom",
"//base",
"//brave/components/api_request_helper:api_request_helper",
"//components/keyed_service/core",
"//net",
"//services/network/public/cpp",
"//third_party/abseil-cpp:absl",
"//url",
]

libs = []

if (is_win) {
if (is_win || is_mac) {
sources += [
"brave_vpn_os_connection_api_win.cc",
"brave_vpn_os_connection_api_win.h",
"brave_vpn_connection_info.cc",
"brave_vpn_connection_info.h",
"brave_vpn_os_connection_api.cc",
"brave_vpn_os_connection_api.h",
"brave_vpn_os_connection_api_sim.cc",
"brave_vpn_os_connection_api_sim.h",
"brave_vpn_service_desktop.cc",
"brave_vpn_service_desktop.h",
"switches.h",
]

libs += [ "rasapi32.lib" ]
}
if (is_win) {
sources += [
"brave_vpn_os_connection_api_win.cc",
"brave_vpn_os_connection_api_win.h",
]

if (is_mac) {
sources += [
"brave_vpn_os_connection_api_mac.h",
"brave_vpn_os_connection_api_mac.mm",
]
libs += [ "rasapi32.lib" ]
}

if (is_mac) {
sources += [
"brave_vpn_os_connection_api_mac.h",
"brave_vpn_os_connection_api_mac.mm",
]

frameworks = [ "NetworkExtension.framework" ]
configs += [ "//build/config/compiler:enable_arc" ]
frameworks = [ "NetworkExtension.framework" ]
configs += [ "//build/config/compiler:enable_arc" ]
}

deps += [
":brave_vpn_internal",
":mojom",
]
}
}

Expand Down
10 changes: 10 additions & 0 deletions components/brave_vpn/brave_vpn_connection_info.cc
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ namespace brave_vpn {
BraveVPNConnectionInfo::BraveVPNConnectionInfo() = default;
BraveVPNConnectionInfo::~BraveVPNConnectionInfo() = default;

BraveVPNConnectionInfo::BraveVPNConnectionInfo(
const BraveVPNConnectionInfo& info) = default;
BraveVPNConnectionInfo& BraveVPNConnectionInfo::operator=(
const BraveVPNConnectionInfo& info) = default;

bool BraveVPNConnectionInfo::IsValid() const {
// TODO(simonhong): Improve credentials validation.
return !hostname_.empty() && !username_.empty() && !password_.empty();
}

void BraveVPNConnectionInfo::SetConnectionInfo(
const std::string& connection_name,
const std::string& hostname,
Expand Down
3 changes: 3 additions & 0 deletions components/brave_vpn/brave_vpn_connection_info.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,10 @@ class BraveVPNConnectionInfo {
public:
BraveVPNConnectionInfo();
~BraveVPNConnectionInfo();
BraveVPNConnectionInfo(const BraveVPNConnectionInfo& info);
BraveVPNConnectionInfo& operator=(const BraveVPNConnectionInfo& info);

bool IsValid() const;
void SetConnectionInfo(const std::string& connection_name,
const std::string& hostname,
const std::string& username,
Expand Down
1 change: 1 addition & 0 deletions components/brave_vpn/brave_vpn_os_connection_api.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ class BraveVPNOSConnectionAPI {
};

static BraveVPNOSConnectionAPI* GetInstance();
static BraveVPNOSConnectionAPI* GetInstanceForTest();

BraveVPNOSConnectionAPI(const BraveVPNOSConnectionAPI&) = delete;
BraveVPNOSConnectionAPI& operator=(const BraveVPNOSConnectionAPI&) = delete;
Expand Down
90 changes: 90 additions & 0 deletions components/brave_vpn/brave_vpn_os_connection_api_sim.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/* 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/. */

#include "brave/components/brave_vpn/brave_vpn_os_connection_api_sim.h"

#include "base/logging.h"
#include "base/notreached.h"
#include "base/threading/sequenced_task_runner_handle.h"

namespace brave_vpn {

// static
BraveVPNOSConnectionAPI* BraveVPNOSConnectionAPI::GetInstanceForTest() {
static base::NoDestructor<BraveVPNOSConnectionAPISim> s_manager;
return s_manager.get();
}

BraveVPNOSConnectionAPISim::BraveVPNOSConnectionAPISim() = default;
BraveVPNOSConnectionAPISim::~BraveVPNOSConnectionAPISim() = default;

void BraveVPNOSConnectionAPISim::CreateVPNConnection(
const BraveVPNConnectionInfo& info) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE,
base::BindOnce(&BraveVPNOSConnectionAPISim::OnCreated,
weak_factory_.GetWeakPtr(), info.connection_name(), true));
}

void BraveVPNOSConnectionAPISim::UpdateVPNConnection(
const BraveVPNConnectionInfo& info) {
NOTIMPLEMENTED();
}

void BraveVPNOSConnectionAPISim::Connect(const std::string& name) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&BraveVPNOSConnectionAPISim::OnConnected,
weak_factory_.GetWeakPtr(), name, true));
}

void BraveVPNOSConnectionAPISim::Disconnect(const std::string& name) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&BraveVPNOSConnectionAPISim::OnDisconnected,
weak_factory_.GetWeakPtr(), name, true));
}

void BraveVPNOSConnectionAPISim::RemoveVPNConnection(const std::string& name) {
base::SequencedTaskRunnerHandle::Get()->PostTask(
FROM_HERE, base::BindOnce(&BraveVPNOSConnectionAPISim::OnRemoved,
weak_factory_.GetWeakPtr(), name, true));
}

void BraveVPNOSConnectionAPISim::OnCreated(const std::string& name,
bool success) {
if (!success)
return;

for (Observer& obs : observers_)
obs.OnCreated(name);
}

void BraveVPNOSConnectionAPISim::OnConnected(const std::string& name,
bool success) {
if (!success)
return;

for (Observer& obs : observers_)
obs.OnConnected(name);
}

void BraveVPNOSConnectionAPISim::OnDisconnected(const std::string& name,
bool success) {
if (!success)
return;

for (Observer& obs : observers_)
obs.OnDisconnected(name);
}

void BraveVPNOSConnectionAPISim::OnRemoved(const std::string& name,
bool success) {
if (!success)
return;

for (Observer& obs : observers_)
obs.OnRemoved(name);
}

} // namespace brave_vpn
Loading

0 comments on commit 9a6dc74

Please sign in to comment.