Files
brave-core/browser/ui/commander/simple_command_source.cc
Claudio DeSouza cffb249298 [cr149] FindLastActiveWithProfile returning BWI
This change allows a couple of more places to be migrated to
`BrowserWindowInterface`.

Chromium changes:
https://chromium.googlesource.com/chromium/src/+/e8d0df41d25900497716dd3a4b070c6436fa1a97

commit e8d0df41d25900497716dd3a4b070c6436fa1a97
Author: Kun Wang <kunwang@microsoft.com>
Date:   Wed Apr 15 23:35:39 2026 -0700

    [bedrock] Migrate FindLastActiveWithProfile Step 1

    Bug: 494010890
    Change-Id: Ie0385deeda2e2f28baf75a810ada0becf5807c78
    Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/7753884
    Reviewed-by: Thomas Lukaszewicz <tluk@chromium.org>
    Reviewed-by: Qikai Zhong <qikaizhong@microsoft.com>
    Commit-Queue: Kun Wang <kunwang@microsoft.com>
    Cr-Commit-Position: refs/heads/main@{#1615659}
2026-05-22 16:24:59 -04:00

112 lines
3.6 KiB
C++

// 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/ui/commander/simple_command_source.h"
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/strings/utf_string_conversions.h"
#include "brave/app/brave_command_ids.h"
#include "brave/app/command_utils.h"
#include "brave/browser/misc_metrics/profile_misc_metrics_service.h"
#include "brave/browser/misc_metrics/profile_misc_metrics_service_factory.h"
#include "brave/browser/ui/commander/command_source.h"
#include "brave/browser/ui/commander/fuzzy_finder.h"
#include "brave/components/ai_chat/core/common/buildflags/buildflags.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/accelerator_utils.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/browser/ui/browser_commands.h"
#include "chrome/browser/ui/browser_window/public/browser_window_features.h"
#include "chrome/browser/ui/browser_window/public/browser_window_interface.h"
#include "ui/base/accelerators/accelerator.h"
#if BUILDFLAG(ENABLE_AI_CHAT)
#include "brave/components/ai_chat/core/browser/ai_chat_metrics.h"
#endif // BUILDFLAG(ENABLE_AI_CHAT)
namespace commander {
namespace {
void MaybeReportCommandExecution(BrowserWindowInterface* browser,
int command_id) {
#if BUILDFLAG(ENABLE_AI_CHAT)
if (command_id == IDC_TOGGLE_AI_CHAT) {
auto* profile_metrics =
misc_metrics::ProfileMiscMetricsServiceFactory::GetServiceForContext(
browser->GetProfile());
if (!profile_metrics) {
return;
}
auto* ai_chat_metrics = profile_metrics->GetAIChatMetrics();
if (!ai_chat_metrics) {
return;
}
ai_chat_metrics->HandleOpenViaEntryPoint(
ai_chat::EntryPoint::kOmniboxCommand);
}
#endif // BUILDFLAG(ENABLE_AI_CHAT)
}
} // namespace
SimpleCommandSource::SimpleCommandSource() = default;
SimpleCommandSource::~SimpleCommandSource() = default;
CommandSource::CommandResults SimpleCommandSource::GetCommands(
const std::u16string& input,
BrowserWindowInterface* browser) const {
CommandSource::CommandResults results;
if (!browser || !browser->GetFeatures().browser_command_controller()) {
return results;
}
auto commands = commands::GetCommands();
FuzzyFinder finder(input);
std::vector<gfx::Range> ranges;
for (const int command_id : commands) {
if (!chrome::IsCommandEnabled(browser, command_id)) {
continue;
}
std::u16string name =
base::UTF8ToUTF16(commands::GetCommandName(command_id));
double score = finder.Find(name, ranges);
if (score == 0) {
continue;
}
auto item = std::make_unique<CommandItem>(name, score, ranges);
ui::Accelerator accelerator;
ui::AcceleratorProvider* provider =
AcceleratorProviderForBrowser(browser->GetBrowserForMigrationOnly());
if (provider->GetAcceleratorForCommandId(command_id, &accelerator)) {
item->annotation = accelerator.GetShortcutText();
}
item->command = base::BindOnce(
[](BrowserWindowInterface* browser, int command_id) {
MaybeReportCommandExecution(browser, command_id);
chrome::ExecuteCommand(browser, command_id);
},
// Unretained is safe here, as the commands will be reset if the browser
// is closed.
base::Unretained(browser), command_id);
results.push_back(std::move(item));
}
return results;
}
} // namespace commander