Add uninstall handler for vpn status tray icon (#20274)

* Add uninstall handler for vpn status tray icon

* Remove optional type

* Rename and fix message id
This commit is contained in:
Cepera
2023-09-26 04:15:38 +09:00
committed by GitHub
parent e3063dd881
commit a24fab76cc
15 changed files with 127 additions and 25 deletions
@@ -18,6 +18,7 @@
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/service/install_utils.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/service/wireguard_service_runner.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/service/wireguard_tunnel_service.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/install_utils.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_tray_runner.h"
#include "brave/components/brave_vpn/common/wireguard/win/service_constants.h"
#include "chrome/install_static/product_install_details.h"
@@ -142,7 +143,8 @@ int APIENTRY wWinMain(HINSTANCE instance, HINSTANCE prev, wchar_t*, int) {
// windows service and removes stored data. Used by the uninstaller.
if (command_line->HasSwitch(
brave_vpn::kBraveVpnWireguardServiceUnnstallSwitchName)) {
auto success = brave_vpn::UninstallBraveWireguardService();
auto success = brave_vpn::UninstallBraveWireguardService() &&
brave_vpn::UninstallStatusTrayIcon();
return success ? 0 : 1;
}
@@ -8,6 +8,8 @@ import("//build/toolchain/gcc_toolchain.gni")
source_set("status_tray") {
sources = [
"brave_vpn_tray_command_ids.h",
"install_utils.cc",
"install_utils.h",
"status_tray_runner.cc",
"status_tray_runner.h",
]
@@ -15,6 +17,7 @@ source_set("status_tray") {
deps = [
"ras",
"resources",
"status_icon:utils",
"wireguard",
"//base",
"//brave/components/brave_vpn/common",
@@ -7,11 +7,12 @@
#define BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_BRAVE_VPN_TRAY_COMMAND_IDS_H_
// First command id must be higher than last command.
#define IDC_BRAVE_VPN_TRAY_EXIT_ICON 1000
#define IDC_BRAVE_VPN_TRAY_HIDE_ICON 1000
#define IDC_BRAVE_VPN_TRAY_CONNECT_VPN_ITEM 1001
#define IDC_BRAVE_VPN_TRAY_DISCONNECT_VPN_ITEM 1002
#define IDC_BRAVE_VPN_TRAY_STATUS_ITEM 1003
#define IDC_BRAVE_VPN_TRAY_MANAGE_ACCOUNT_ITEM 1004
#define IDC_BRAVE_VPN_TRAY_ABOUT_ITEM 1005
#define IDC_BRAVE_VPN_TRAY_EXIT 1006
#endif // BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_BRAVE_VPN_TRAY_COMMAND_IDS_H_
@@ -0,0 +1,27 @@
/* Copyright (c) 2023 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/brave_vpn/win/brave_vpn_wireguard_service/status_tray/install_utils.h"
#include <windows.h> // needed for WM_MENUCOMMAND
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/brave_vpn_tray_command_ids.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/constants.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/icon_utils.h"
namespace brave_vpn {
bool UninstallStatusTrayIcon() {
auto* hWnd = GetBraveVpnStatusTrayIconHWND();
if (!hWnd) {
return true;
}
return SendMessage(hWnd,
RegisterWindowMessage(kBraveVpnStatusTrayMessageName),
IDC_BRAVE_VPN_TRAY_EXIT, 0) == TRUE;
}
} // namespace brave_vpn
@@ -0,0 +1,17 @@
/* Copyright (c) 2023 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_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_INSTALL_UTILS_H_
#define BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_INSTALL_UTILS_H_
namespace brave_vpn {
// Uninstallation actions for status tray icons, called from browser
// uninstaller.
bool UninstallStatusTrayIcon();
} // namespace brave_vpn
#endif // BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_INSTALL_UTILS_H_
@@ -3,10 +3,20 @@
# 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/.
source_set("status_icon") {
source_set("utils") {
sources = [
"constants.h",
"icon_utils.cc",
"icon_utils.h",
]
deps = [
"//base",
"//ui/gfx",
]
}
source_set("status_icon") {
sources = [
"native_popup_menu.cc",
"native_popup_menu.h",
"scoped_hmenu.h",
@@ -20,6 +30,7 @@ source_set("status_icon") {
]
public_deps = [ "//ui/base" ]
deps = [
":utils",
"//base",
"//ui/gfx",
]
@@ -0,0 +1,22 @@
/* Copyright (c) 2023 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_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_CONSTANTS_H_
#define BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_CONSTANTS_H_
#include "base/files/file_path.h"
namespace brave_vpn {
// Status tray icon window name and class.
constexpr base::FilePath::CharType kStatusTrayWindowName[] =
FILE_PATH_LITERAL("BraveVpn_StatusTrayWindow");
constexpr base::FilePath::CharType kStatusTrayWindowClass[] =
FILE_PATH_LITERAL("BraveVpn_StatusTraydowClass");
const base::FilePath::CharType kBraveVpnStatusTrayMessageName[] =
FILE_PATH_LITERAL("BraveVpn_CustomTrayMessage");
} // namespace brave_vpn
#endif // BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_CONSTANTS_H_
@@ -7,6 +7,7 @@
#include <memory>
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/constants.h"
#include "ui/gfx/icon_util.h"
#include "ui/gfx/image/image_family.h"
#include "ui/gfx/image/image_skia.h"
@@ -33,4 +34,13 @@ gfx::ImageSkia GetIconFromResources(int icon_id, const gfx::Size& size) {
return family->CreateExact(size).AsImageSkia();
}
HWND GetBraveVpnStatusTrayIconHWND() {
return FindWindowEx(nullptr, nullptr, kStatusTrayWindowClass,
kStatusTrayWindowName);
}
bool IsBraveVpnTrayIconRunning() {
return GetBraveVpnStatusTrayIconHWND() != NULL;
}
} // namespace brave_vpn
@@ -6,6 +6,8 @@
#ifndef BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_ICON_UTILS_H_
#define BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_ICON_UTILS_H_
#include "base/win/windows_types.h"
namespace gfx {
class ImageSkia;
class Size;
@@ -13,6 +15,8 @@ class Size;
namespace brave_vpn {
gfx::ImageSkia GetIconFromResources(int icon_id, const gfx::Size& size);
bool IsBraveVpnTrayIconRunning();
HWND GetBraveVpnStatusTrayIconHWND();
} // namespace brave_vpn
#endif // BRAVE_BROWSER_BRAVE_VPN_WIN_BRAVE_VPN_WIREGUARD_SERVICE_STATUS_TRAY_STATUS_ICON_ICON_UTILS_H_
@@ -73,7 +73,6 @@ void NativePopupMenu::AddMenuItemAt(size_t menu_index) {
mii.dwTypeData = base::as_writable_wcstr(items_[menu_index]->label);
// MIIM_STATE
mii.fState = model_->IsEnabledAt(menu_index) ? MFS_ENABLED : MFS_DISABLED;
;
InsertMenuItem(GetWeakMenuHandle(), menu_index, TRUE, &mii);
}
@@ -14,7 +14,6 @@
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/native_popup_menu.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/tray_menu_model.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/geometry/rect.h"
#include "ui/gfx/icon_util.h"
namespace brave_vpn {
@@ -69,7 +68,11 @@ void StatusIcon::OnMenuCommand(int index, int event_flags) {
if (!menu_model_->delegate()) {
return;
}
menu_model_->ExecuteCommand(menu_model_->GetCommandIdAt(index), event_flags);
ExecuteCommand(menu_model_->GetCommandIdAt(index), event_flags);
}
void StatusIcon::ExecuteCommand(int command_id, int event_flags) {
menu_model_->ExecuteCommand(command_id, event_flags);
}
void StatusIcon::ResetIcon() {
@@ -39,6 +39,7 @@ class StatusIcon {
void UpdateState(const gfx::ImageSkia& image, const std::u16string& tool_tip);
void SetContextMenu(std::unique_ptr<TrayMenuModel> menu);
void OnMenuCommand(int index, int event_flags);
void ExecuteCommand(int command_id, int event_flags);
// Re-creates the status tray icon.
void ResetIcon();
@@ -13,6 +13,7 @@
#include "base/files/file_path.h"
#include "base/win/windows_types.h"
#include "base/win/wrapped_window_proc.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/constants.h"
#include "brave/browser/brave_vpn/win/brave_vpn_wireguard_service/status_tray/status_icon/status_icon.h"
#include "ui/gfx/geometry/point.h"
#include "ui/gfx/win/hwnd_util.h"
@@ -22,11 +23,6 @@ namespace brave_vpn {
static const UINT kStatusIconMessage = WM_APP + 1;
namespace {
constexpr base::FilePath::CharType kStatusTraydowName[] =
FILE_PATH_LITERAL("BraveVpn_StatusTraydow");
constexpr base::FilePath::CharType kStatusTraydowClass[] =
FILE_PATH_LITERAL("BraveVpn_StatusTraydowClass");
const base::FilePath::CharType kBraveVpnTaskbarMessageName[] =
FILE_PATH_LITERAL("TaskbarCreated");
@@ -42,7 +38,7 @@ StatusTray::StatusTray() : atom_(0), instance_(NULL) {
// Register our window class
WNDCLASSEX window_class;
base::win::InitializeWindowClass(
kStatusTraydowClass,
kStatusTrayWindowClass,
&base::win::WrappedWindowProc<StatusTray::WndProcStatic>, 0, 0, 0, NULL,
NULL, NULL, NULL, NULL, &window_class);
instance_ = window_class.hInstance;
@@ -52,13 +48,14 @@ StatusTray::StatusTray() : atom_(0), instance_(NULL) {
// If the taskbar is re-created after we start up, we have to rebuild all of
// our icons.
taskbar_created_message_ = RegisterWindowMessage(kBraveVpnTaskbarMessageName);
custom_tray_message_ = RegisterWindowMessage(kBraveVpnStatusTrayMessageName);
// Create an offscreen window for handling messages for the status icons. We
// create a hidden WS_POPUP window instead of an HWND_MESSAGE window, because
// only top-level windows such as popups can receive broadcast messages like
// "TaskbarCreated".
window_.reset(CreateWindow(MAKEINTATOM(atom_), kStatusTraydowName, WS_POPUP,
0, 0, 0, 0, 0, 0, instance_, 0));
window_.reset(CreateWindow(MAKEINTATOM(atom_), kStatusTrayWindowName,
WS_POPUP, 0, 0, 0, 0, 0, 0, instance_, 0));
gfx::CheckWindowCreated(window_.get(), ::GetLastError());
gfx::SetWindowUserData(window_.get(), this);
}
@@ -70,11 +67,6 @@ StatusTray::~StatusTray() {
}
}
bool StatusTray::IconWindowExists() {
return FindWindowEx(nullptr, nullptr, kStatusTraydowClass,
kStatusTraydowName) != NULL;
}
StatusIcon* StatusTray::GetStatusIcon() {
return status_icon_.get();
}
@@ -103,7 +95,12 @@ LRESULT CALLBACK StatusTray::WndProc(HWND hwnd,
UINT message,
WPARAM wparam,
LPARAM lparam) {
if (message == taskbar_created_message_) {
if (message == custom_tray_message_) {
if (status_icon_) {
status_icon_->ExecuteCommand(wparam, lparam);
}
return TRUE;
} else if (message == taskbar_created_message_) {
// We need to reset an icon because the taskbar went away.
if (status_icon_) {
status_icon_->ResetIcon();
@@ -27,8 +27,6 @@ class StatusTray {
~StatusTray();
static bool IconWindowExists();
void CreateStatusIcon(const gfx::ImageSkia& image,
const std::u16string& tool_tip);
StatusIcon* GetStatusIcon();
@@ -57,6 +55,10 @@ class StatusTray {
// reset our status icons.
UINT taskbar_created_message_;
// The message ID of the "CustomTrayMessage" message, sent to us when we need
// to execute status icon commands.
UINT custom_tray_message_;
std::unique_ptr<StatusIcon> status_icon_;
};
@@ -151,7 +151,10 @@ void StatusTrayRunner::SetupStatusIcon() {
void StatusTrayRunner::ExecuteCommand(int command_id, int event_flags) {
switch (command_id) {
case IDC_BRAVE_VPN_TRAY_EXIT_ICON:
case IDC_BRAVE_VPN_TRAY_EXIT:
SignalExit();
break;
case IDC_BRAVE_VPN_TRAY_HIDE_ICON:
EnableVPNTrayIcon(false);
SignalExit();
break;
@@ -193,7 +196,7 @@ void StatusTrayRunner::OnMenuWillShow(ui::SimpleMenuModel* source) {
l10n_util::GetStringUTF16(IDS_BRAVE_VPN_WIREGUARD_TRAY_ABOUT_ITEM));
source->AddSeparator(ui::NORMAL_SEPARATOR);
source->AddItem(
IDC_BRAVE_VPN_TRAY_EXIT_ICON,
IDC_BRAVE_VPN_TRAY_HIDE_ICON,
l10n_util::GetStringUTF16(IDS_BRAVE_VPN_WIREGUARD_TRAY_REMOVE_ICON_ITEM));
}
@@ -331,7 +334,7 @@ HRESULT StatusTrayRunner::Run() {
return S_OK;
}
if (StatusTray::IconWindowExists()) {
if (brave_vpn::IsBraveVpnTrayIconRunning()) {
VLOG(1) << "Tray icon is already visible.";
return S_OK;
}