From 75e2ab431ff908a1fd1f37a448e5db68982bda65 Mon Sep 17 00:00:00 2001 From: Simon Hong Date: Wed, 30 Oct 2024 18:18:59 +0900 Subject: [PATCH] Added BraveVPNController to BrowserWindowFeatures fix https://github.com/brave/brave-browser/issues/41973 BraveVPNController will control vpn behavior related with browser window. --- browser/ui/BUILD.gn | 5 +++ browser/ui/brave_browser_window.h | 2 -- browser/ui/brave_vpn/brave_vpn_controller.cc | 35 +++++++++++++++++++ browser/ui/brave_vpn/brave_vpn_controller.h | 30 ++++++++++++++++ browser/ui/browser_commands.cc | 6 ++-- browser/ui/views/frame/brave_browser_view.h | 3 +- .../browser_window/browser_window_features.cc | 33 +++++++++++++++++ .../public/browser_window_features.h | 25 +++++++++++++ 8 files changed, 134 insertions(+), 5 deletions(-) create mode 100644 browser/ui/brave_vpn/brave_vpn_controller.cc create mode 100644 browser/ui/brave_vpn/brave_vpn_controller.h create mode 100644 chromium_src/chrome/browser/ui/browser_window/public/browser_window_features.h diff --git a/browser/ui/BUILD.gn b/browser/ui/BUILD.gn index 4530823438f3..8c0ca8400703 100644 --- a/browser/ui/BUILD.gn +++ b/browser/ui/BUILD.gn @@ -640,6 +640,11 @@ source_set("ui") { deps += [ "//brave/browser/brave_vpn/win:wireguard_utils" ] } + + sources += [ + "brave_vpn/brave_vpn_controller.cc", + "brave_vpn/brave_vpn_controller.h", + ] } if (enable_ai_chat) { sources += [ diff --git a/browser/ui/brave_browser_window.h b/browser/ui/brave_browser_window.h index d6b7d5fc768c..21dbbe518b7c 100644 --- a/browser/ui/brave_browser_window.h +++ b/browser/ui/brave_browser_window.h @@ -70,8 +70,6 @@ class BraveBrowserWindow : public BrowserWindow { virtual void ShowWaybackMachineBubble() {} #endif - virtual void ShowBraveVPNBubble() {} - // Returns true if all tabs in this window is being dragged. virtual bool IsInTabDragging() const; }; diff --git a/browser/ui/brave_vpn/brave_vpn_controller.cc b/browser/ui/brave_vpn/brave_vpn_controller.cc new file mode 100644 index 000000000000..02c813530bfe --- /dev/null +++ b/browser/ui/brave_vpn/brave_vpn_controller.cc @@ -0,0 +1,35 @@ +/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ + +#include "brave/browser/ui/brave_vpn/brave_vpn_controller.h" + +#include "brave/browser/brave_vpn/brave_vpn_service_factory.h" +#include "brave/browser/ui/views/frame/brave_browser_view.h" +#include "brave/components/brave_vpn/browser/brave_vpn_service.h" +#include "brave/components/brave_vpn/common/brave_vpn_utils.h" +#include "chrome/browser/ui/singleton_tabs.h" +#include "url/gurl.h" + +BraveVPNController::BraveVPNController(BrowserView* browser_view) + : browser_view_(browser_view) {} + +BraveVPNController::~BraveVPNController() = default; + +void BraveVPNController::ShowBraveVPNBubble() { + GetBraveBrowserView()->ShowBraveVPNBubble(); +} + +void BraveVPNController::OpenVPNAccountPage() { + auto* browser = browser_view_->browser(); + auto* profile = browser->profile(); + auto* vpn_service = brave_vpn::BraveVpnServiceFactory::GetForProfile(profile); + const auto url = + brave_vpn::GetManageUrl(vpn_service->GetCurrentEnvironment()); + ShowSingletonTab(browser, GURL(url)); +} + +BraveBrowserView* BraveVPNController::GetBraveBrowserView() { + return static_cast(browser_view_); +} diff --git a/browser/ui/brave_vpn/brave_vpn_controller.h b/browser/ui/brave_vpn/brave_vpn_controller.h new file mode 100644 index 000000000000..79b7fff83be1 --- /dev/null +++ b/browser/ui/brave_vpn/brave_vpn_controller.h @@ -0,0 +1,30 @@ +/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_BROWSER_UI_BRAVE_VPN_BRAVE_VPN_CONTROLLER_H_ +#define BRAVE_BROWSER_UI_BRAVE_VPN_BRAVE_VPN_CONTROLLER_H_ + +#include "base/memory/raw_ptr.h" + +class BraveBrowserView; +class BrowserView; + +class BraveVPNController { + public: + explicit BraveVPNController(BrowserView* browser_view); + ~BraveVPNController(); + BraveVPNController(const BraveVPNController&) = delete; + BraveVPNController& operator=(const BraveVPNController&) = delete; + + void ShowBraveVPNBubble(); + void OpenVPNAccountPage(); + + private: + BraveBrowserView* GetBraveBrowserView(); + + raw_ptr browser_view_ = nullptr; +}; + +#endif // BRAVE_BROWSER_UI_BRAVE_VPN_BRAVE_VPN_CONTROLLER_H_ diff --git a/browser/ui/browser_commands.cc b/browser/ui/browser_commands.cc index 12a3b2fbbb45..9f1ac7b2e443 100644 --- a/browser/ui/browser_commands.cc +++ b/browser/ui/browser_commands.cc @@ -84,6 +84,7 @@ #if BUILDFLAG(ENABLE_BRAVE_VPN) #include "brave/browser/brave_vpn/brave_vpn_service_factory.h" +#include "brave/browser/ui/brave_vpn/brave_vpn_controller.h" #include "brave/components/brave_vpn/browser/brave_vpn_service.h" #include "brave/components/brave_vpn/common/brave_vpn_constants.h" #include "brave/components/brave_vpn/common/brave_vpn_utils.h" @@ -181,8 +182,9 @@ void MaybeDistillAndShowSpeedreaderBubble(Browser* browser) { } void ShowBraveVPNBubble(Browser* browser) { - // Ask to browser view. - static_cast(browser->window())->ShowBraveVPNBubble(); +#if BUILDFLAG(ENABLE_BRAVE_VPN) + browser->GetFeatures().GetBraveVPNController()->ShowBraveVPNBubble(); +#endif } void ToggleBraveVPNTrayIcon() { diff --git a/browser/ui/views/frame/brave_browser_view.h b/browser/ui/views/frame/brave_browser_view.h index 9460756d2e89..e173c35d5522 100644 --- a/browser/ui/views/frame/brave_browser_view.h +++ b/browser/ui/views/frame/brave_browser_view.h @@ -76,6 +76,8 @@ class BraveBrowserView : public BrowserView, void SetStarredState(bool is_starred) override; void ShowUpdateChromeDialog() override; + + void ShowBraveVPNBubble(); void CreateWalletBubble(); void CreateApproveWalletBubble(); void CloseWalletBubble(); @@ -163,7 +165,6 @@ class BraveBrowserView : public BrowserView, TabStripModel* tab_strip_model, const TabStripModelChange& change, const TabStripSelectionChange& selection) override; - void ShowBraveVPNBubble() override; views::CloseRequestResult OnWindowCloseRequested() override; void ConfirmBrowserCloseWithPendingDownloads( int download_count, diff --git a/chromium_src/chrome/browser/ui/browser_window/browser_window_features.cc b/chromium_src/chrome/browser/ui/browser_window/browser_window_features.cc index d8035010dd18..9b127cdc7c7c 100644 --- a/chromium_src/chrome/browser/ui/browser_window/browser_window_features.cc +++ b/chromium_src/chrome/browser/ui/browser_window/browser_window_features.cc @@ -5,8 +5,41 @@ #include "chrome/browser/ui/browser_window/public/browser_window_features.h" +#include "base/notreached.h" #include "brave/browser/ui/views/side_panel/brave_side_panel_coordinator.h" +#include "brave/components/brave_vpn/common/buildflags/buildflags.h" + +#if BUILDFLAG(ENABLE_BRAVE_VPN) +#include "brave/browser/ui/brave_vpn/brave_vpn_controller.h" +#endif + +#if !BUILDFLAG(ENABLE_BRAVE_VPN) +// Use stub class to avoid incomplete type build error. +class BraveVPNController {}; +#endif #define SidePanelCoordinator BraveSidePanelCoordinator +#define InitPostBrowserViewConstruction \ + InitPostBrowserViewConstruction_ChromiumImpl + #include "src/chrome/browser/ui/browser_window/browser_window_features.cc" + +#undef InitPostBrowserViewConstruction #undef SidePanelCoordinator + +void BrowserWindowFeatures::InitPostBrowserViewConstruction( + BrowserView* browser_view) { + InitPostBrowserViewConstruction_ChromiumImpl(browser_view); + +#if BUILDFLAG(ENABLE_BRAVE_VPN) + brave_vpn_controller_ = std::make_unique(browser_view); +#endif +} + +BraveVPNController* BrowserWindowFeatures::GetBraveVPNController() { +#if BUILDFLAG(ENABLE_BRAVE_VPN) + return brave_vpn_controller_.get(); +#else + NOTREACHED_NORETURN(); +#endif +} diff --git a/chromium_src/chrome/browser/ui/browser_window/public/browser_window_features.h b/chromium_src/chrome/browser/ui/browser_window/public/browser_window_features.h new file mode 100644 index 000000000000..a40146e6e2bc --- /dev/null +++ b/chromium_src/chrome/browser/ui/browser_window/public/browser_window_features.h @@ -0,0 +1,25 @@ +/* Copyright (c) 2024 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 https://mozilla.org/MPL/2.0/. */ + +#ifndef BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_BROWSER_WINDOW_PUBLIC_BROWSER_WINDOW_FEATURES_H_ +#define BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_BROWSER_WINDOW_PUBLIC_BROWSER_WINDOW_FEATURES_H_ + +class BraveVPNController; + +#define InitPostBrowserViewConstruction \ + InitPostBrowserViewConstruction_ChromiumImpl(BrowserView* browser_view); \ + BraveVPNController* GetBraveVPNController(); \ + \ + private: \ + std::unique_ptr brave_vpn_controller_; \ + \ + public: \ + void InitPostBrowserViewConstruction + +#include "src/chrome/browser/ui/browser_window/public/browser_window_features.h" // IWYU pragma: export + +#undef InitPostBrowserViewConstruction + +#endif // BRAVE_CHROMIUM_SRC_CHROME_BROWSER_UI_BROWSER_WINDOW_PUBLIC_BROWSER_WINDOW_FEATURES_H_