Skip to content

Commit

Permalink
Added BraveVPNController to BrowserWindowFeatures
Browse files Browse the repository at this point in the history
fix brave/brave-browser#41973

BraveVPNController will control vpn behavior related with browser window.
  • Loading branch information
simonhong committed Oct 30, 2024
1 parent f98f048 commit 483f3f1
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 5 deletions.
5 changes: 5 additions & 0 deletions browser/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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 += [
Expand Down
2 changes: 0 additions & 2 deletions browser/ui/brave_browser_window.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
};
Expand Down
35 changes: 35 additions & 0 deletions browser/ui/brave_vpn/brave_vpn_controller.cc
Original file line number Diff line number Diff line change
@@ -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<BraveBrowserView*>(browser_view_);
}
30 changes: 30 additions & 0 deletions browser/ui/brave_vpn/brave_vpn_controller.h
Original file line number Diff line number Diff line change
@@ -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<BrowserView, DanglingUntriaged> browser_view_ = nullptr;
};

#endif // BRAVE_BROWSER_UI_BRAVE_VPN_BRAVE_VPN_CONTROLLER_H_
6 changes: 4 additions & 2 deletions browser/ui/browser_commands.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -181,8 +182,9 @@ void MaybeDistillAndShowSpeedreaderBubble(Browser* browser) {
}

void ShowBraveVPNBubble(Browser* browser) {
// Ask to browser view.
static_cast<BraveBrowserWindow*>(browser->window())->ShowBraveVPNBubble();
#if BUILDFLAG(ENABLE_BRAVE_VPN)
browser->GetFeatures().GetBraveVPNController()->ShowBraveVPNBubble();
#endif
}

void ToggleBraveVPNTrayIcon() {
Expand Down
3 changes: 2 additions & 1 deletion browser/ui/views/frame/brave_browser_view.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,36 @@

#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

#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<BraveVPNController>(browser_view);
#endif
}

BraveVPNController* BrowserWindowFeatures::GetBraveVPNController() {
#if BUILDFLAG(ENABLE_BRAVE_VPN)
return brave_vpn_controller_.get();
#else
NOTREACHED();
#endif
}
Original file line number Diff line number Diff line change
@@ -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<BraveVPNController> 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_

0 comments on commit 483f3f1

Please sign in to comment.