diff --git a/browser/brave_vpn/brave_vpn_service_factory.cc b/browser/brave_vpn/brave_vpn_service_factory.cc index 5d2acc7d9e5d..e37cb527df98 100644 --- a/browser/brave_vpn/brave_vpn_service_factory.cc +++ b/browser/brave_vpn/brave_vpn_service_factory.cc @@ -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::get(); } +#if defined(OS_WIN) || defined(OS_MAC) +// static +BraveVpnServiceDesktop* BraveVpnServiceFactory::GetForProfile( + Profile* profile) { + return static_cast( + GetInstance()->GetServiceForBrowserContext(profile, true)); +} +#endif + +#if defined(OS_ANDROID) // static BraveVpnService* BraveVpnServiceFactory::GetForProfile(Profile* profile) { return static_cast( GetInstance()->GetServiceForBrowserContext(profile, true)); } +#endif BraveVpnServiceFactory::BraveVpnServiceFactory() : BrowserContextKeyedServiceFactory( @@ -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( diff --git a/browser/brave_vpn/brave_vpn_service_factory.h b/browser/brave_vpn/brave_vpn_service_factory.h index 739f9d8ddba0..c49c8a7609f7 100644 --- a/browser/brave_vpn/brave_vpn_service_factory.h +++ b/browser/brave_vpn/brave_vpn_service_factory.h @@ -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; diff --git a/browser/ui/views/toolbar/brave_vpn_button.cc b/browser/ui/views/toolbar/brave_vpn_button.cc index bbb679c2d272..f169218728e9 100644 --- a/browser/ui/views/toolbar/brave_vpn_button.cc +++ b/browser/ui/views/toolbar/brave_vpn_button.cc @@ -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 = diff --git a/browser/ui/views/toolbar/brave_vpn_button.h b/browser/ui/views/toolbar/brave_vpn_button.h index a09c65de7e69..e5f47862e18b 100644 --- a/browser/ui/views/toolbar/brave_vpn_button.h +++ b/browser/ui/views/toolbar/brave_vpn_button.h @@ -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); @@ -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; @@ -35,8 +39,9 @@ class BraveVPNButton : public ToolbarButton, public BraveVpnService::Observer { void OnButtonPressed(const ui::Event& event); void ShowBraveVPNPanel(); - BraveVpnService* service_ = nullptr; - base::ScopedObservation + BraveVpnServiceDesktop* service_ = nullptr; + base::ScopedObservation observation_{this}; WebUIBubbleManagerT webui_bubble_manager_; }; diff --git a/browser/ui/webui/brave_vpn/vpn_panel_handler.cc b/browser/ui/webui/brave_vpn/vpn_panel_handler.cc index 6269ebcdb47a..38189072aa48 100644 --- a/browser/ui/webui/brave_vpn/vpn_panel_handler.cc +++ b/browser/ui/webui/brave_vpn/vpn_panel_handler.cc @@ -7,10 +7,12 @@ #include +#include "brave/components/brave_vpn/brave_vpn_service_desktop.h" + VPNPanelHandler::VPNPanelHandler( mojo::PendingReceiver receiver, ui::MojoBubbleWebUIController* webui_controller, - BraveVpnService* service) + BraveVpnServiceDesktop* service) : receiver_(this, std::move(receiver)), webui_controller_(webui_controller), service_(service) {} diff --git a/browser/ui/webui/brave_vpn/vpn_panel_handler.h b/browser/ui/webui/brave_vpn/vpn_panel_handler.h index 716546e0ef9c..095b2de6b969 100644 --- a/browser/ui/webui/brave_vpn/vpn_panel_handler.h +++ b/browser/ui/webui/brave_vpn/vpn_panel_handler.h @@ -10,7 +10,7 @@ #include #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" @@ -19,6 +19,8 @@ namespace content { class WebUI; } // namespace content +class BraveVpnServiceDesktop; + class VPNPanelHandler : public brave_vpn::mojom::PanelHandler { public: using GetWebContentsForTabCallback = @@ -27,7 +29,7 @@ class VPNPanelHandler : public brave_vpn::mojom::PanelHandler { VPNPanelHandler( mojo::PendingReceiver receiver, ui::MojoBubbleWebUIController* webui_controller, - BraveVpnService* service); + BraveVpnServiceDesktop* service); VPNPanelHandler(const VPNPanelHandler&) = delete; VPNPanelHandler& operator=(const VPNPanelHandler&) = delete; @@ -41,7 +43,7 @@ class VPNPanelHandler : public brave_vpn::mojom::PanelHandler { private: mojo::Receiver receiver_; ui::MojoBubbleWebUIController* const webui_controller_; - BraveVpnService* service_; + BraveVpnServiceDesktop* service_; }; #endif // BRAVE_BROWSER_UI_WEBUI_BRAVE_VPN_VPN_PANEL_HANDLER_H_ diff --git a/components/brave_vpn/BUILD.gn b/components/brave_vpn/BUILD.gn index e47e40229b02..2e0c4ca14467 100644 --- a/components/brave_vpn/BUILD.gn +++ b/components/brave_vpn/BUILD.gn @@ -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", @@ -25,12 +21,9 @@ 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", @@ -38,23 +31,42 @@ static_library("brave_vpn") { 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", + ] } } diff --git a/components/brave_vpn/brave_vpn_connection_info.cc b/components/brave_vpn/brave_vpn_connection_info.cc index f4d16826f026..9b81157778d0 100644 --- a/components/brave_vpn/brave_vpn_connection_info.cc +++ b/components/brave_vpn/brave_vpn_connection_info.cc @@ -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, diff --git a/components/brave_vpn/brave_vpn_connection_info.h b/components/brave_vpn/brave_vpn_connection_info.h index 6162e5d7bb06..25ecd9b796c2 100644 --- a/components/brave_vpn/brave_vpn_connection_info.h +++ b/components/brave_vpn/brave_vpn_connection_info.h @@ -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, diff --git a/components/brave_vpn/brave_vpn_os_connection_api.h b/components/brave_vpn/brave_vpn_os_connection_api.h index 35b4509066f9..0f870bcb712e 100644 --- a/components/brave_vpn/brave_vpn_os_connection_api.h +++ b/components/brave_vpn/brave_vpn_os_connection_api.h @@ -30,6 +30,7 @@ class BraveVPNOSConnectionAPI { }; static BraveVPNOSConnectionAPI* GetInstance(); + static BraveVPNOSConnectionAPI* GetInstanceForTest(); BraveVPNOSConnectionAPI(const BraveVPNOSConnectionAPI&) = delete; BraveVPNOSConnectionAPI& operator=(const BraveVPNOSConnectionAPI&) = delete; diff --git a/components/brave_vpn/brave_vpn_os_connection_api_sim.cc b/components/brave_vpn/brave_vpn_os_connection_api_sim.cc new file mode 100644 index 000000000000..a5429ce0a76f --- /dev/null +++ b/components/brave_vpn/brave_vpn_os_connection_api_sim.cc @@ -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 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 diff --git a/components/brave_vpn/brave_vpn_os_connection_api_sim.h b/components/brave_vpn/brave_vpn_os_connection_api_sim.h new file mode 100644 index 000000000000..f8939e9613d4 --- /dev/null +++ b/components/brave_vpn/brave_vpn_os_connection_api_sim.h @@ -0,0 +1,47 @@ +/* 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_OS_CONNECTION_API_SIM_H_ +#define BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_OS_CONNECTION_API_SIM_H_ + +#include + +#include "base/memory/weak_ptr.h" +#include "base/no_destructor.h" +#include "brave/components/brave_vpn/brave_vpn_os_connection_api.h" + +namespace brave_vpn { + +class BraveVPNOSConnectionAPISim : public BraveVPNOSConnectionAPI { + public: + BraveVPNOSConnectionAPISim(const BraveVPNOSConnectionAPISim&) = delete; + BraveVPNOSConnectionAPISim& operator=(const BraveVPNOSConnectionAPISim&) = + delete; + + protected: + friend class base::NoDestructor; + + BraveVPNOSConnectionAPISim(); + ~BraveVPNOSConnectionAPISim() override; + + // BraveVPNOSConnectionAPI overrides: + void CreateVPNConnection(const BraveVPNConnectionInfo& info) override; + void UpdateVPNConnection(const BraveVPNConnectionInfo& info) override; + void RemoveVPNConnection(const std::string& name) override; + void Connect(const std::string& name) override; + void Disconnect(const std::string& name) override; + + private: + void OnCreated(const std::string& name, bool success); + void OnConnected(const std::string& name, bool success); + void OnDisconnected(const std::string& name, bool success); + void OnRemoved(const std::string& name, bool success); + + base::WeakPtrFactory weak_factory_{this}; +}; + +} // namespace brave_vpn + +#endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_OS_CONNECTION_API_SIM_H_ diff --git a/components/brave_vpn/brave_vpn_service.cc b/components/brave_vpn/brave_vpn_service.cc index 34b6d56a865a..9368f4badb14 100644 --- a/components/brave_vpn/brave_vpn_service.cc +++ b/components/brave_vpn/brave_vpn_service.cc @@ -5,9 +5,10 @@ #include "brave/components/brave_vpn/brave_vpn_service.h" +#include + #include "base/json/json_reader.h" #include "base/json/json_writer.h" -#include "base/notreached.h" #include "third_party/abseil-cpp/absl/types/optional.h" namespace { @@ -77,9 +78,7 @@ BraveVpnService::BraveVpnService( BraveVpnService::~BraveVpnService() = default; -void BraveVpnService::Shutdown() { - observers_.Clear(); -} +void BraveVpnService::Shutdown() {} void BraveVpnService::OAuthRequest(const GURL& url, const std::string& method, @@ -141,37 +140,6 @@ void BraveVpnService::VerifyPurchaseToken(ResponseCallback callback, std::move(internal_callback)); } -void BraveVpnService::Connect() { - NOTIMPLEMENTED(); - - is_connected_ = true; - for (Observer& obs : observers_) - obs.OnConnectionStateChanged(true); -} - -void BraveVpnService::Disconnect() { - NOTIMPLEMENTED(); - - is_connected_ = false; - for (Observer& obs : observers_) - obs.OnConnectionStateChanged(false); -} - -bool BraveVpnService::IsConnected() const { - NOTIMPLEMENTED(); - - // Just return fake statue now. - return is_connected_; -} - -void BraveVpnService::AddObserver(Observer* observer) { - observers_.AddObserver(observer); -} - -void BraveVpnService::RemoveObserver(Observer* observer) { - observers_.RemoveObserver(observer); -} - void BraveVpnService::OnGetResponse( ResponseCallback callback, int status, diff --git a/components/brave_vpn/brave_vpn_service.h b/components/brave_vpn/brave_vpn_service.h index 56088ba78df7..a717f264ba7d 100644 --- a/components/brave_vpn/brave_vpn_service.h +++ b/components/brave_vpn/brave_vpn_service.h @@ -6,15 +6,11 @@ #ifndef BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_H_ #define BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_H_ -#include -#include #include -#include #include "base/bind.h" #include "base/callback_forward.h" #include "base/memory/scoped_refptr.h" -#include "base/observer_list.h" #include "brave/components/api_request_helper/api_request_helper.h" #include "components/keyed_service/core/keyed_service.h" #include "services/network/public/cpp/shared_url_loader_factory.h" @@ -26,14 +22,6 @@ class SharedURLLoaderFactory; class BraveVpnService : public KeyedService { public: - class Observer : public base::CheckedObserver { - public: - virtual void OnConnectionStateChanged(bool connected) = 0; - - protected: - ~Observer() override = default; - }; - explicit BraveVpnService( scoped_refptr url_loader_factory); ~BraveVpnService() override; @@ -61,13 +49,6 @@ class BraveVpnService : public KeyedService { const std::string& product_id, const std::string& product_type); - void Connect(); - void Disconnect(); - bool IsConnected() const; - - void AddObserver(Observer* observer); - void RemoveObserver(Observer* observer); - private: using URLRequestCallback = base::OnceCallback& headers); - bool is_connected_ = false; - base::ObserverList observers_; api_request_helper::APIRequestHelper api_request_helper_; }; diff --git a/components/brave_vpn/brave_vpn_service_desktop.cc b/components/brave_vpn/brave_vpn_service_desktop.cc new file mode 100644 index 000000000000..a31ca7319b96 --- /dev/null +++ b/components/brave_vpn/brave_vpn_service_desktop.cc @@ -0,0 +1,121 @@ +/* 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_service_desktop.h" + +#include + +#include "base/command_line.h" +#include "base/logging.h" +#include "base/notreached.h" +#include "base/strings/string_split.h" +#include "brave/components/brave_vpn/switches.h" + +namespace { + +bool GetVPNCredentialsFromSwitch(brave_vpn::BraveVPNConnectionInfo* info) { + DCHECK(info); + auto* cmd = base::CommandLine::ForCurrentProcess(); + if (!cmd->HasSwitch(brave_vpn::switches::kBraveVPNTestCredentials)) + return false; + + std::string value = + cmd->GetSwitchValueASCII(brave_vpn::switches::kBraveVPNTestCredentials); + std::vector tokens = base::SplitString( + value, ":", base::KEEP_WHITESPACE, base::SPLIT_WANT_ALL); + if (tokens.size() == 4) { + info->SetConnectionInfo(tokens[0], tokens[1], tokens[2], tokens[3]); + return true; + } + + LOG(ERROR) << __func__ << ": Invalid credentials"; + return false; +} + +brave_vpn::BraveVPNOSConnectionAPI* GetBraveVPNConnectionAPI() { + auto* cmd = base::CommandLine::ForCurrentProcess(); + if (cmd->HasSwitch(brave_vpn::switches::kBraveVPNSimulation)) + return brave_vpn::BraveVPNOSConnectionAPI::GetInstanceForTest(); + return brave_vpn::BraveVPNOSConnectionAPI::GetInstance(); +} + +} // namespace + +BraveVpnServiceDesktop::BraveVpnServiceDesktop( + scoped_refptr url_loader_factory) + : BraveVpnService(url_loader_factory) { + observed_.Observe(GetBraveVPNConnectionAPI()); +} + +BraveVpnServiceDesktop::~BraveVpnServiceDesktop() = default; + +void BraveVpnServiceDesktop::Shutdown() { + BraveVpnService::Shutdown(); + + observed_.Reset(); + observers_.Clear(); +} + +void BraveVpnServiceDesktop::OnCreated(const std::string& name) { + for (Observer& obs : observers_) + obs.OnConnectionCreated(); +} + +void BraveVpnServiceDesktop::OnRemoved(const std::string& name) { + for (Observer& obs : observers_) + obs.OnConnectionRemoved(); +} + +void BraveVpnServiceDesktop::OnConnected(const std::string& name) { + is_connected_ = true; + + for (Observer& obs : observers_) + obs.OnConnectionStateChanged(true); +} + +void BraveVpnServiceDesktop::OnDisconnected(const std::string& name) { + is_connected_ = false; + + for (Observer& obs : observers_) + obs.OnConnectionStateChanged(false); +} + +void BraveVpnServiceDesktop::CreateVPNConnection() { + GetBraveVPNConnectionAPI()->CreateVPNConnection(GetConnectionInfo()); +} + +void BraveVpnServiceDesktop::RemoveVPNConnnection() { + GetBraveVPNConnectionAPI()->RemoveVPNConnection( + GetConnectionInfo().connection_name()); +} + +void BraveVpnServiceDesktop::Connect() { + GetBraveVPNConnectionAPI()->Connect(GetConnectionInfo().connection_name()); +} + +void BraveVpnServiceDesktop::Disconnect() { + GetBraveVPNConnectionAPI()->Disconnect(GetConnectionInfo().connection_name()); +} + +bool BraveVpnServiceDesktop::IsConnected() const { + return is_connected_; +} + +void BraveVpnServiceDesktop::AddObserver(Observer* observer) { + observers_.AddObserver(observer); +} + +void BraveVpnServiceDesktop::RemoveObserver(Observer* observer) { + observers_.RemoveObserver(observer); +} + +brave_vpn::BraveVPNConnectionInfo BraveVpnServiceDesktop::GetConnectionInfo() { + brave_vpn::BraveVPNConnectionInfo info; + if (!GetVPNCredentialsFromSwitch(&info)) { + // TODO(simonhong): Get real credentials from payment service. + NOTIMPLEMENTED(); + } + return info; +} diff --git a/components/brave_vpn/brave_vpn_service_desktop.h b/components/brave_vpn/brave_vpn_service_desktop.h new file mode 100644 index 000000000000..94d4640f6f46 --- /dev/null +++ b/components/brave_vpn/brave_vpn_service_desktop.h @@ -0,0 +1,66 @@ +/* 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_SERVICE_DESKTOP_H_ +#define BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_DESKTOP_H_ + +#include + +#include "base/observer_list.h" +#include "base/scoped_observation.h" +#include "brave/components/brave_vpn/brave_vpn_connection_info.h" +#include "brave/components/brave_vpn/brave_vpn_os_connection_api.h" +#include "brave/components/brave_vpn/brave_vpn_service.h" + +class BraveVpnServiceDesktop + : public BraveVpnService, + public brave_vpn::BraveVPNOSConnectionAPI::Observer { + public: + class Observer : public base::CheckedObserver { + public: + virtual void OnConnectionStateChanged(bool connected) = 0; + virtual void OnConnectionCreated() = 0; + virtual void OnConnectionRemoved() = 0; + + protected: + ~Observer() override = default; + }; + + explicit BraveVpnServiceDesktop( + scoped_refptr url_loader_factory); + ~BraveVpnServiceDesktop() override; + + BraveVpnServiceDesktop(const BraveVpnServiceDesktop&) = delete; + BraveVpnServiceDesktop& operator=(const BraveVpnServiceDesktop&) = delete; + + void Connect(); + void Disconnect(); + bool IsConnected() const; + void CreateVPNConnection(); + void RemoveVPNConnnection(); + + void AddObserver(Observer* observer); + void RemoveObserver(Observer* observer); + + private: + // BraveVpnService overrides: + void Shutdown() override; + + // brave_vpn::BraveVPNOSConnectionAPI::Observer overrides: + void OnCreated(const std::string& name) override; + void OnRemoved(const std::string& name) override; + void OnConnected(const std::string& name) override; + void OnDisconnected(const std::string& name) override; + + brave_vpn::BraveVPNConnectionInfo GetConnectionInfo(); + + bool is_connected_ = false; + base::ObserverList observers_; + base::ScopedObservation + observed_{this}; +}; + +#endif // BRAVE_COMPONENTS_BRAVE_VPN_BRAVE_VPN_SERVICE_DESKTOP_H_ diff --git a/components/brave_vpn/switches.h b/components/brave_vpn/switches.h new file mode 100644 index 000000000000..d2d601cf1a10 --- /dev/null +++ b/components/brave_vpn/switches.h @@ -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_SWITCHES_H_ +#define BRAVE_COMPONENTS_BRAVE_VPN_SWITCHES_H_ + +namespace brave_vpn { + +namespace switches { + +// Value should be "connection-name:host-name:user-name:password". +constexpr char kBraveVPNTestCredentials[] = "brave-vpn-test-credentials"; +// Use for simulation instead of calling os platform apis. +constexpr char kBraveVPNSimulation[] = "brave-vpn-simulate"; + +} // namespace switches + +} // namespace brave_vpn + +#endif // BRAVE_COMPONENTS_BRAVE_VPN_SWITCHES_H_