[Customize Chrome Panel] Observe brave specific actions visibility from customize toolbar handler (#29879)

In order to update web ui's toggle buttons' state correctly based 
on the visibility of brave specific actions, we need to observe 
corresponding preferences and update the state accordingly.

<!-- Add brave-browser issue below that this PR will resolve -->
Resolves https://github.com/brave/brave-browser/issues/47359
This commit is contained in:
Sangwoo Ko
2025-07-03 21:00:19 +09:00
committed by GitHub
parent 1146b13477
commit 7d59eefe38
5 changed files with 142 additions and 14 deletions
@@ -53,7 +53,10 @@ source_set("browser_tests") {
source_set("unit_tests") {
testonly = true
sources = [ "customize_toolbar/list_action_modifiers_unittest.cc" ]
sources = [
"customize_toolbar/customize_toolbar_handler_unittest.cc",
"customize_toolbar/list_action_modifiers_unittest.cc",
]
deps = [
":customize_chrome",
@@ -6,6 +6,7 @@
#ifndef BRAVE_BROWSER_UI_WEBUI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_TOOLBAR_BRAVE_ACTION_H_
#define BRAVE_BROWSER_UI_WEBUI_SIDE_PANEL_CUSTOMIZE_CHROME_CUSTOMIZE_TOOLBAR_BRAVE_ACTION_H_
#include "base/containers/fixed_flat_map.h"
#include "brave/components/ai_chat/core/common/pref_names.h"
#include "brave/components/brave_vpn/common/buildflags/buildflags.h"
#include "brave/components/brave_wallet/browser/pref_names.h"
@@ -69,14 +70,16 @@ inline constexpr BraveAction kShowVPNAction = {
.icon = kLeoProductVpnIcon};
#endif // BUILDFLAG(ENABLE_BRAVE_VPN)
inline constexpr auto kBraveActions = std::to_array<BraveAction>({
kShowSidePanelAction,
kShowWalletAction,
kShowAIChatAction,
inline constexpr auto kBraveActions =
base::MakeFixedFlatMap<side_panel::customize_chrome::mojom::ActionId,
const BraveAction*>({
{kShowSidePanelAction.id, &kShowSidePanelAction},
{kShowWalletAction.id, &kShowWalletAction},
{kShowAIChatAction.id, &kShowAIChatAction},
#if BUILDFLAG(ENABLE_BRAVE_VPN)
kShowVPNAction,
{kShowVPNAction.id, &kShowVPNAction},
#endif // BUILDFLAG(ENABLE_BRAVE_VPN)
});
});
} // namespace customize_chrome
@@ -0,0 +1,92 @@
// Copyright (c) 2025 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 "chrome/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/customize_toolbar_handler.h"
#include "brave/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/brave_action.h"
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/customize_toolbar.mojom.h"
#include "chrome/test/base/testing_profile.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/browser/web_contents.h"
#include "content/public/test/browser_task_environment.h"
#include "content/public/test/test_web_contents_factory.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace {
class MockPage
: public side_panel::customize_chrome::mojom::CustomizeToolbarClient {
public:
MockPage() = default;
~MockPage() override = default;
mojo::PendingRemote<
side_panel::customize_chrome::mojom::CustomizeToolbarClient>
BindAndGetRemote() {
DCHECK(!receiver_.is_bound());
return receiver_.BindNewPipeAndPassRemote();
}
MOCK_METHOD(void,
SetActionPinned,
(side_panel::customize_chrome::mojom::ActionId action_id,
bool pinned));
MOCK_METHOD(void, NotifyActionsUpdated, ());
mojo::Receiver<side_panel::customize_chrome::mojom::CustomizeToolbarClient>
receiver_{this};
};
} // namespace
namespace customize_chrome {
class CustomizeToolbarHandlerUnitTest : public testing::Test {
public:
CustomizeToolbarHandlerUnitTest() = default;
~CustomizeToolbarHandlerUnitTest() override = default;
// testing::Test:
void SetUp() override {
web_contents_ =
test_web_contents_factory_.CreateWebContents(&testing_profile_);
handler_ = std::make_unique<CustomizeToolbarHandler>(
mojo::PendingReceiver<
side_panel::customize_chrome::mojom::CustomizeToolbarHandler>(),
mock_page_.BindAndGetRemote(), web_contents_);
}
void TearDown() override {
handler_.reset();
web_contents_ = nullptr;
}
protected:
sync_preferences::TestingPrefServiceSyncable* GetTestingPrefService() {
return testing_profile_.GetTestingPrefService();
}
content::BrowserTaskEnvironment task_environment_;
TestingProfile testing_profile_;
testing::NiceMock<MockPage> mock_page_;
content::TestWebContentsFactory test_web_contents_factory_;
raw_ptr<content::WebContents> web_contents_;
std::unique_ptr<CustomizeToolbarHandler> handler_;
};
TEST_F(CustomizeToolbarHandlerUnitTest,
OnBraveActionPinnedChanged_ShouldBeCalledWhenPrefsChanged) {
for (const auto& [id, brave_action] : kBraveActions) {
const bool pinned =
GetTestingPrefService()->GetBoolean(brave_action->pref_name);
EXPECT_CALL(mock_page_, SetActionPinned(id, !pinned));
GetTestingPrefService()->SetBoolean(brave_action->pref_name, !pinned);
}
}
} // namespace customize_chrome
@@ -5,15 +5,24 @@
#include "chrome/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/customize_toolbar_handler.h"
#include "base/containers/map_util.h"
#include "base/memory/raw_ref.h"
#include "base/notreached.h"
#include "brave/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/brave_action.h"
#include "brave/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/list_action_modifiers.h"
#include "chrome/browser/ui/views/frame/browser_view.h"
#define ListActions ListActionsChromium
#define PinAction PinActionChromium
// pref_change_registrar_.Init() in constructor
#define Init(...) \
Init(__VA_ARGS__); \
ObserveBraveActions()
#include "src/chrome/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/customize_toolbar_handler.cc"
#undef Init
#undef PinAction
#undef ListActions
@@ -30,14 +39,32 @@ void CustomizeToolbarHandler::ListActions(ListActionsCallback callback) {
void CustomizeToolbarHandler::PinAction(
side_panel::customize_chrome::mojom::ActionId action_id,
bool pin) {
for (const auto& brave_action : customize_chrome::kBraveActions) {
if (action_id == brave_action.id) {
// Brave specific actions are handled here.
prefs()->SetBoolean(brave_action.pref_name,
!prefs()->GetBoolean(brave_action.pref_name));
return;
}
if (const auto* brave_action =
base::FindPtrOrNull(customize_chrome::kBraveActions, action_id)) {
// Brave specific actions are handled here.
prefs()->SetBoolean(brave_action->pref_name,
!prefs()->GetBoolean(brave_action->pref_name));
return;
}
PinActionChromium(action_id, pin);
}
void CustomizeToolbarHandler::ObserveBraveActions() {
for (const auto& [id, brave_action] : customize_chrome::kBraveActions) {
pref_change_registrar_.Add(
brave_action->pref_name,
base::BindRepeating(
&CustomizeToolbarHandler::OnBraveActionPinnedChanged,
base::Unretained(this), id));
}
}
void CustomizeToolbarHandler::OnBraveActionPinnedChanged(
side_panel::customize_chrome::mojom::ActionId action_id) {
const auto* brave_action =
base::FindPtrOrNull(customize_chrome::kBraveActions, action_id);
CHECK(brave_action);
client_->SetActionPinned(action_id,
prefs()->GetBoolean(brave_action->pref_name));
}
@@ -15,6 +15,9 @@
#define PinAction \
PinActionChromium(side_panel::customize_chrome::mojom::ActionId action_id, \
bool pin); \
void ObserveBraveActions(); \
void OnBraveActionPinnedChanged( \
side_panel::customize_chrome::mojom::ActionId action_id); \
void PinAction
#include "src/chrome/browser/ui/webui/side_panel/customize_chrome/customize_toolbar/customize_toolbar_handler.h" // IWYU pragma: export