0

Move extensions::Command to ui::Command

This is part of the effort to move GlobalShortcutListener to
//ui/base. We need the API for this class to reference general
concepts rather than extensions specific concepts. So this moves
the extensions concepts out into a subclass.

This will ultimately be replaced by more of a builder/utils pattern
but this is the first step that is needed to unblock the rest of the
migration.

Bug: 378487333
Change-Id: I15d3420eb8a2f1beb3bd89e5cb99f1f1f9f4f775
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/6081681
Code-Coverage: findit-for-me@appspot.gserviceaccount.com <findit-for-me@appspot.gserviceaccount.com>
Reviewed-by: Emilia Paz <emiliapaz@chromium.org>
Reviewed-by: Dana Fried <dfried@chromium.org>
Commit-Queue: Alison Gale <agale@chromium.org>
Cr-Commit-Position: refs/heads/main@{#1395168}
This commit is contained in:
Alison Gale
2024-12-11 16:21:57 -08:00
committed by Chromium LUCI CQ
parent 6cf1bb3488
commit ad77162f7e
20 changed files with 169 additions and 101 deletions

@ -10,11 +10,11 @@
#include "chrome/browser/extensions/commands/command_service.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/common/api/extension_action/action_info.h"
#include "ui/base/accelerators/command.h"
namespace {
base::Value::Dict CreateCommandValue(const extensions::Command& command,
bool active) {
base::Value::Dict CreateCommandValue(const ui::Command& command, bool active) {
base::Value::Dict result;
result.Set("name", command.command_name());
result.Set("description", command.description());
@ -56,13 +56,13 @@ ExtensionFunction::ResponseAction GetAllCommandsFunction::Run() {
command_list.Append(CreateCommandValue(page_action, active));
}
extensions::CommandMap named_commands;
ui::CommandMap named_commands;
command_service->GetNamedCommands(extension_->id(),
extensions::CommandService::ALL,
extensions::CommandService::ANY_SCOPE,
&named_commands);
for (extensions::CommandMap::const_iterator iter = named_commands.begin();
for (ui::CommandMap::const_iterator iter = named_commands.begin();
iter != named_commands.end(); ++iter) {
extensions::Command command = command_service->FindCommandByName(
extension_->id(), iter->second.command_name());

@ -72,6 +72,7 @@
#include "extensions/common/permissions/permissions_data.h"
#include "extensions/grit/extensions_browser_resources.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/base/accelerators/command.h"
#include "ui/base/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/codec/png_codec.h"
@ -193,7 +194,7 @@ developer::RuntimeError ConstructRuntimeError(const RuntimeError& error) {
void ConstructCommands(CommandService* command_service,
const ExtensionId& extension_id,
std::vector<developer::Command>* commands) {
auto construct_command = [](const Command& command, bool active,
auto construct_command = [](const ui::Command& command, bool active,
bool is_extension_action) {
developer::Command command_value;
command_value.description =
@ -223,13 +224,13 @@ void ConstructCommands(CommandService* command_service,
}
}
CommandMap named_commands;
ui::CommandMap named_commands;
if (command_service->GetNamedCommands(extension_id,
CommandService::ALL,
CommandService::ANY_SCOPE,
&named_commands)) {
for (auto& pair : named_commands) {
Command& command_to_use = pair.second;
ui::Command& command_to_use = pair.second;
// TODO(devlin): For some reason beyond my knowledge, FindCommandByName
// returns different data than GetNamedCommands, including the
// accelerators, but not the descriptions - and even then, only if the

@ -32,6 +32,7 @@
#include "extensions/common/feature_switch.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
namespace {
@ -126,7 +127,7 @@ CommandService* CommandService::Get(content::BrowserContext* context) {
bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
QueryType type,
CommandScope scope,
CommandMap* command_map) const {
ui::CommandMap* command_map) const {
const Extension* extension =
GetExtensionInEnabledOrDisabledExtensions(extension_id);
if (!extension) {
@ -134,7 +135,7 @@ bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
}
command_map->clear();
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (!commands)
return false;
@ -147,7 +148,7 @@ bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
if (type == ACTIVE && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
continue;
Command command = named_command.second;
ui::Command command = named_command.second;
if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global()))
continue;
@ -354,14 +355,14 @@ void CommandService::UpdateKeybindings(const Extension* extension) {
void CommandService::RemoveRelinquishedKeybindings(const Extension* extension) {
// Remove keybindings if they have been removed by the extension and the user
// has not modified them.
CommandMap existing_command_map;
ui::CommandMap existing_command_map;
if (GetNamedCommands(extension->id(),
CommandService::ACTIVE,
CommandService::REGULAR,
&existing_command_map)) {
const CommandMap* new_command_map =
const ui::CommandMap* new_command_map =
CommandsInfo::GetNamedCommands(extension);
for (CommandMap::const_iterator it = existing_command_map.begin();
for (ui::CommandMap::const_iterator it = existing_command_map.begin();
it != existing_command_map.end(); ++it) {
std::string command_name = it->first;
if (new_command_map->find(command_name) == new_command_map->end() &&
@ -437,12 +438,12 @@ void CommandService::RemoveRelinquishedKeybindings(const Extension* extension) {
}
void CommandService::AssignKeybindings(const Extension* extension) {
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (!commands)
return;
for (const auto& named_command : *commands) {
const Command command = named_command.second;
const ui::Command command = named_command.second;
if (CanAutoAssign(command, extension)) {
AddKeybindingPref(command.accelerator(),
extension->id(),
@ -482,7 +483,7 @@ void CommandService::AssignKeybindings(const Extension* extension) {
}
}
bool CommandService::CanAutoAssign(const Command &command,
bool CommandService::CanAutoAssign(const ui::Command& command,
const Extension* extension) {
// Extensions are allowed to auto-assign updated keys if the user has not
// changed from the previous value.
@ -524,10 +525,10 @@ void CommandService::UpdateExtensionSuggestedCommandPrefs(
const Extension* extension) {
base::Value::Dict suggested_key_prefs;
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (commands) {
for (const auto& named_command : *commands) {
const Command command = named_command.second;
const ui::Command command = named_command.second;
base::Value::Dict command_keys;
command_keys.Set(kSuggestedKey,
Command::AcceleratorToString(command.accelerator()));
@ -573,7 +574,7 @@ void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs(
if (current_prefs) {
base::Value::Dict suggested_key_prefs = current_prefs->Clone();
const CommandMap* named_commands =
const ui::CommandMap* named_commands =
CommandsInfo::GetNamedCommands(extension);
const Command* browser_action_command =

@ -17,6 +17,7 @@
#include "extensions/common/command.h"
#include "extensions/common/extension.h"
#include "extensions/common/extension_id.h"
#include "ui/base/accelerators/command.h"
class Profile;
@ -123,7 +124,7 @@ class CommandService : public BrowserContextKeyedAPI,
bool GetNamedCommands(const ExtensionId& extension_id,
QueryType type,
CommandScope scope,
CommandMap* command_map) const;
ui::CommandMap* command_map) const;
// Records a keybinding |accelerator| as active for an extension with id
// |extension_id| and command with the name |command_name|. If
@ -217,8 +218,7 @@ class CommandService : public BrowserContextKeyedAPI,
// Checks if |extension| is permitted to automatically assign the
// |accelerator| key.
bool CanAutoAssign(const Command &command,
const Extension* extension);
bool CanAutoAssign(const ui::Command& command, const Extension* extension);
// Updates the record of |extension|'s most recent suggested command shortcut
// keys in the preferences.

@ -2,6 +2,8 @@
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/extensions/commands/command_service.h"
#include <memory>
#include <utility>
@ -10,7 +12,6 @@
#include "base/values.h"
#include "build/build_config.h"
#include "build/chromeos_buildflags.h"
#include "chrome/browser/extensions/commands/command_service.h"
#include "chrome/browser/extensions/extension_apitest.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
@ -23,6 +24,7 @@
#include "extensions/common/manifest_constants.h"
#include "extensions/test/test_extension_dir.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/command.h"
namespace {
const char kBasicBrowserActionKeybinding[] = "Ctrl+Shift+F";
@ -592,13 +594,13 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest,
CommandService* command_service = CommandService::Get(browser()->profile());
{
CommandMap command_map;
ui::CommandMap command_map;
EXPECT_TRUE(command_service->GetNamedCommands(
extension->id(), CommandService::ALL, CommandService::ANY_SCOPE,
&command_map));
ASSERT_EQ(1u, command_map.count(kBasicNamedCommand));
Command command = command_map[kBasicNamedCommand];
ui::Command command = command_map[kBasicNamedCommand];
EXPECT_EQ(kBasicNamedKeybinding,
Command::AcceleratorToString(command.accelerator()));
}
@ -607,13 +609,13 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest,
extension->id(), kBasicNamedCommand, kBasicAlternateKeybinding);
{
CommandMap command_map;
ui::CommandMap command_map;
EXPECT_TRUE(command_service->GetNamedCommands(
extension->id(), CommandService::ALL, CommandService::ANY_SCOPE,
&command_map));
ASSERT_EQ(1u, command_map.count(kBasicNamedCommand));
Command command = command_map[kBasicNamedCommand];
ui::Command command = command_map[kBasicNamedCommand];
EXPECT_EQ(kBasicAlternateKeybinding,
Command::AcceleratorToString(command.accelerator()));
}
@ -621,13 +623,13 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest,
command_service->RemoveKeybindingPrefs(extension->id(), kBasicNamedCommand);
{
CommandMap command_map;
ui::CommandMap command_map;
EXPECT_TRUE(command_service->GetNamedCommands(
extension->id(), CommandService::ALL, CommandService::ANY_SCOPE,
&command_map));
ASSERT_EQ(1u, command_map.count(kBasicNamedCommand));
Command command = command_map[kBasicNamedCommand];
ui::Command command = command_map[kBasicNamedCommand];
EXPECT_EQ(kBasicNamedKeybinding,
Command::AcceleratorToString(command.accelerator()));
}
@ -642,13 +644,13 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest, GetNamedCommandsQueryActive) {
CommandService* command_service = CommandService::Get(browser()->profile());
{
CommandMap command_map;
ui::CommandMap command_map;
EXPECT_TRUE(command_service->GetNamedCommands(
extension->id(), CommandService::ACTIVE, CommandService::ANY_SCOPE,
&command_map));
ASSERT_EQ(1u, command_map.count(kBasicNamedCommand));
Command command = command_map[kBasicNamedCommand];
ui::Command command = command_map[kBasicNamedCommand];
EXPECT_EQ(kBasicNamedKeybinding,
Command::AcceleratorToString(command.accelerator()));
}
@ -657,13 +659,13 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest, GetNamedCommandsQueryActive) {
extension->id(), kBasicNamedCommand, kBasicAlternateKeybinding);
{
CommandMap command_map;
ui::CommandMap command_map;
EXPECT_TRUE(command_service->GetNamedCommands(
extension->id(), CommandService::ACTIVE, CommandService::ANY_SCOPE,
&command_map));
ASSERT_EQ(1u, command_map.count(kBasicNamedCommand));
Command command = command_map[kBasicNamedCommand];
ui::Command command = command_map[kBasicNamedCommand];
EXPECT_EQ(kBasicAlternateKeybinding,
Command::AcceleratorToString(command.accelerator()));
}
@ -671,7 +673,7 @@ IN_PROC_BROWSER_TEST_F(CommandServiceTest, GetNamedCommandsQueryActive) {
command_service->RemoveKeybindingPrefs(extension->id(), kBasicNamedCommand);
{
CommandMap command_map;
ui::CommandMap command_map;
command_service->GetNamedCommands(
extension->id(), CommandService::ACTIVE, CommandService::ANY_SCOPE,
&command_map);

@ -12,6 +12,7 @@
#include "content/public/browser/browser_context.h"
#include "extensions/browser/pref_names.h"
#include "extensions/common/extension.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
@ -81,7 +82,7 @@ void ExtensionCommandsGlobalRegistry::AddExtensionKeybindings(
}
extensions::CommandService* command_service =
extensions::CommandService::Get(browser_context_);
extensions::CommandMap commands;
ui::CommandMap commands;
if (instance->IsRegistrationHandledExternally()) {
if (!command_service->GetNamedCommands(
extension->id(), extensions::CommandService::ALL,

@ -9,6 +9,7 @@
#include "base/notreached.h"
#include "content/public/browser/browser_thread.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
using content::BrowserThread;
@ -110,11 +111,10 @@ bool GlobalShortcutListener::IsRegistrationHandledExternally() const {
return false;
}
void GlobalShortcutListener::OnCommandsChanged(
const ExtensionId& extension_id,
const std::string& profile_id,
const extensions::CommandMap& commands,
Observer* observer) {}
void GlobalShortcutListener::OnCommandsChanged(const ExtensionId& extension_id,
const std::string& profile_id,
const ui::CommandMap& commands,
Observer* observer) {}
void GlobalShortcutListener::NotifyKeyPressed(
const ui::Accelerator& accelerator) {

@ -8,8 +8,8 @@
#include <map>
#include "base/memory/raw_ptr.h"
#include "extensions/common/command.h"
#include "extensions/common/extension_id.h"
#include "ui/base/accelerators/command.h"
namespace ui {
class Accelerator;
@ -75,7 +75,7 @@ class GlobalShortcutListener {
// Called when an extension's commands are registered.
virtual void OnCommandsChanged(const ExtensionId& extension_id,
const std::string& profile_id,
const extensions::CommandMap& commands,
const ui::CommandMap& commands,
Observer* observer);
protected:

@ -23,6 +23,7 @@
#include "dbus/message.h"
#include "dbus/object_path.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
#include "ui/base/linux/xdg_shortcut.h"
namespace extensions {
@ -152,7 +153,7 @@ bool GlobalShortcutListenerLinux::IsRegistrationHandledExternally() const {
void GlobalShortcutListenerLinux::OnCommandsChanged(
const ExtensionId& extension_id,
const std::string& profile_id,
const CommandMap& commands,
const ui::CommandMap& commands,
Observer* observer) {
// If starting the service failed, there's no need to add the command list.
if (!service_started_.value_or(true)) {
@ -363,7 +364,7 @@ std::string GlobalShortcutListenerLinux::SessionKey::GetTokenKey() const {
GlobalShortcutListenerLinux::SessionContext::SessionContext(
Observer* observer,
const CommandMap& commands)
const ui::CommandMap& commands)
: observer(observer), commands(commands) {}
GlobalShortcutListenerLinux::SessionContext::~SessionContext() {

@ -17,6 +17,7 @@
#include "components/dbus/xdg/request.h"
#include "dbus/bus.h"
#include "dbus/object_proxy.h"
#include "ui/base/accelerators/command.h"
namespace dbus_xdg {
class Request;
@ -69,13 +70,13 @@ class GlobalShortcutListenerLinux : public GlobalShortcutListener {
};
struct SessionContext {
SessionContext(Observer* observer, const CommandMap& commands);
SessionContext(Observer* observer, const ui::CommandMap& commands);
~SessionContext();
scoped_refptr<dbus::Bus> bus;
raw_ptr<dbus::ObjectProxy> session_proxy;
const raw_ptr<Observer> observer;
CommandMap commands;
ui::CommandMap commands;
bool bind_shortcuts_called = false;
std::unique_ptr<dbus_xdg::Request> request;
};
@ -93,7 +94,7 @@ class GlobalShortcutListenerLinux : public GlobalShortcutListener {
bool IsRegistrationHandledExternally() const override;
void OnCommandsChanged(const ExtensionId& extension_id,
const std::string& profile_id,
const CommandMap& commands,
const ui::CommandMap& commands,
Observer* observer) override;
void OnCreateSession(

@ -13,9 +13,11 @@
#include "dbus/message.h"
#include "dbus/mock_bus.h"
#include "dbus/mock_object_proxy.h"
#include "extensions/common/command.h"
#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
using ::testing::_;
using ::testing::AtLeast;
@ -334,7 +336,7 @@ TEST(GlobalShortcutListenerLinuxTest, OnCommandsChanged) {
std::move(*callback).Run(response.get());
}));
CommandMap commands;
ui::CommandMap commands;
commands[kCommandName] = Command(kCommandName, kShortcutDescription,
Command::AcceleratorToString(ui::Accelerator(
ui::VKEY_A, ui::EF_CONTROL_DOWN)),

@ -9,6 +9,7 @@
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/extensions/accelerator_priority.h"
#include "extensions/common/extension.h"
#include "ui/base/accelerators/command.h"
#include "ui/views/focus/focus_manager.h"
ExtensionKeybindingRegistryViews::ExtensionKeybindingRegistryViews(
@ -37,14 +38,14 @@ void ExtensionKeybindingRegistryViews::AddExtensionKeybindings(
extensions::CommandService::Get(profile_);
// Add all the active keybindings (except page actions and browser actions,
// which are handled elsewhere).
extensions::CommandMap commands;
ui::CommandMap commands;
if (!command_service->GetNamedCommands(
extension->id(),
extensions::CommandService::ACTIVE,
extensions::CommandService::REGULAR,
&commands))
return;
extensions::CommandMap::const_iterator iter = commands.begin();
ui::CommandMap::const_iterator iter = commands.begin();
for (; iter != commands.end(); ++iter) {
if (!command_name.empty() && (iter->second.command_name() != command_name))
continue;

@ -15,6 +15,7 @@
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest_constants.h"
#include "extensions/common/manifest_handlers/permissions_parser.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
@ -52,7 +53,8 @@ const Command* CommandsInfo::GetActionCommand(const Extension* extension) {
}
// static
const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) {
const ui::CommandMap* CommandsInfo::GetNamedCommands(
const Extension* extension) {
auto* info =
static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
return info ? &info->named_commands : nullptr;

@ -12,6 +12,7 @@
#include "extensions/common/extension.h"
#include "extensions/common/manifest.h"
#include "extensions/common/manifest_handler.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
@ -26,12 +27,12 @@ struct CommandsInfo : public Extension::ManifestData {
std::unique_ptr<Command> browser_action_command;
std::unique_ptr<Command> page_action_command;
std::unique_ptr<Command> action_command;
CommandMap named_commands;
ui::CommandMap named_commands;
static const Command* GetBrowserActionCommand(const Extension* extension);
static const Command* GetPageActionCommand(const Extension* extension);
static const Command* GetActionCommand(const Extension* extension);
static const CommandMap* GetNamedCommands(const Extension* extension);
static const ui::CommandMap* GetNamedCommands(const Extension* extension);
};
// Parses the "commands" manifest key.

@ -12,6 +12,7 @@
#include "extensions/common/manifest_test.h"
#include "extensions/common/warnings_test_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
@ -48,11 +49,12 @@ TEST_F(CommandsManifestTest, CommandManifestParseCommandsBrowserAction) {
LoadAndExpectSuccess(ManifestData::FromJSON(kManifest));
ASSERT_TRUE(extension.get());
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
const ui::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension.get());
ASSERT_TRUE(commands);
EXPECT_EQ(1u, commands->size());
auto iter = commands->begin();
const Command* named_command = &(*iter).second;
const ui::Command* named_command = &(*iter).second;
EXPECT_EQ("feature1", named_command->command_name());
EXPECT_EQ(u"desc", named_command->description());
const ui::Accelerator ctrl_shift_f =
@ -96,11 +98,12 @@ TEST_F(CommandsManifestTest, CommandManifestParseCommandsPageAction) {
LoadAndExpectSuccess(ManifestData::FromJSON(kManifest));
ASSERT_TRUE(extension.get());
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
const ui::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension.get());
ASSERT_TRUE(commands);
EXPECT_EQ(1u, commands->size());
auto iter = commands->begin();
const Command* named_command = &(*iter).second;
const ui::Command* named_command = &(*iter).second;
EXPECT_EQ("feature1", named_command->command_name());
EXPECT_EQ(u"desc", named_command->description());
@ -140,11 +143,12 @@ TEST_F(CommandsManifestTest, CommandManifestParseCommandsAction) {
LoadAndExpectSuccess(ManifestData::FromJSON(kManifest));
ASSERT_TRUE(extension.get());
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
const ui::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension.get());
ASSERT_TRUE(commands);
EXPECT_EQ(1u, commands->size());
auto iter = commands->begin();
const Command* named_command = &(*iter).second;
const ui::Command* named_command = &(*iter).second;
EXPECT_EQ("feature1", named_command->command_name());
EXPECT_EQ(u"desc", named_command->description());
@ -182,11 +186,12 @@ TEST_F(CommandsManifestTest,
LoadAndExpectSuccess(ManifestData::FromJSON(kManifest));
ASSERT_TRUE(extension.get());
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
const ui::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension.get());
ASSERT_TRUE(commands);
EXPECT_EQ(1u, commands->size());
auto iter = commands->begin();
const Command* named_command = &(*iter).second;
const ui::Command* named_command = &(*iter).second;
EXPECT_EQ("feature1", named_command->command_name());
EXPECT_EQ(u"desc", named_command->description());
@ -223,11 +228,12 @@ TEST_F(CommandsManifestTest,
LoadAndExpectSuccess(ManifestData::FromJSON(kManifest));
ASSERT_TRUE(extension.get());
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension.get());
const ui::CommandMap* commands =
CommandsInfo::GetNamedCommands(extension.get());
ASSERT_TRUE(commands);
EXPECT_EQ(1u, commands->size());
auto iter = commands->begin();
const Command* named_command = &(*iter).second;
const ui::Command* named_command = &(*iter).second;
EXPECT_EQ("feature1", named_command->command_name());
EXPECT_EQ(u"desc", named_command->description());

@ -6,6 +6,8 @@
#include <stddef.h>
#include <memory>
#include "base/check.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_split.h"
@ -17,6 +19,7 @@
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
#include "extensions/common/manifest_constants.h"
#include "ui/base/accelerators/command.h"
#include "ui/base/accelerators/media_keys_listener.h"
namespace extensions {
@ -258,24 +261,18 @@ std::string NormalizeShortcutSuggestion(std::string_view suggestion,
} // namespace
Command::Command() : global_(false) {}
Command::Command(std::string_view command_name,
std::u16string_view description,
std::string_view accelerator,
bool global)
: command_name_(command_name), description_(description), global_(global) {
: ui::Command(command_name, description, global) {
if (!accelerator.empty()) {
std::u16string error;
accelerator_ = ParseImpl(accelerator, CommandPlatform(), 0,
!IsActionRelatedCommand(command_name), &error);
set_accelerator(ParseImpl(accelerator, CommandPlatform(), 0,
!IsActionRelatedCommand(command_name), &error));
}
}
Command::Command(const Command& other) = default;
Command::~Command() = default;
// static
std::string Command::CommandPlatform() {
#if BUILDFLAG(IS_WIN)
@ -522,10 +519,10 @@ bool Command::Parse(const base::Value::Dict& command,
if (iter->first == key) {
// This platform is our platform, so grab this key.
accelerator_ = accelerator;
command_name_ = command_name;
description_ = description;
global_ = global;
set_accelerator(accelerator);
set_command_name(command_name);
set_description(description);
set_global(global);
}
}
return true;

@ -11,18 +11,19 @@
#include "base/values.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
namespace extensions {
class Command {
class Command : public ui::Command {
public:
Command();
Command() = default;
Command(std::string_view command_name,
std::u16string_view description,
std::string_view accelerator,
bool global);
Command(const Command& other);
~Command();
Command(const Command& other) = default;
~Command() override = default;
// The platform value for the Command.
static std::string CommandPlatform();
@ -46,29 +47,8 @@ class Command {
std::string_view command_name,
int index,
std::u16string* error);
// Accessors:
const std::string& command_name() const { return command_name_; }
const ui::Accelerator& accelerator() const { return accelerator_; }
const std::u16string& description() const { return description_; }
bool global() const { return global_; }
// Setter:
void set_accelerator(const ui::Accelerator& accelerator) {
accelerator_ = accelerator;
}
void set_global(bool global) { global_ = global; }
private:
std::string command_name_;
ui::Accelerator accelerator_;
std::u16string description_;
bool global_;
};
// A mapping of command name (std::string) to a command object.
using CommandMap = std::map<std::string, Command>;
} // namespace extensions
#endif // EXTENSIONS_COMMON_COMMAND_H_

@ -117,6 +117,8 @@ component("base") {
sources = [
"accelerators/accelerator.cc",
"accelerators/accelerator.h",
"accelerators/command.cc",
"accelerators/command.h",
"class_property.cc",
"class_property.h",
"default_style.h",

@ -0,0 +1,14 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "ui/base/accelerators/command.h"
namespace ui {
Command::Command(std::string_view command_name,
std::u16string_view description,
bool global)
: command_name_(command_name), description_(description), global_(global) {}
} // namespace ui

@ -0,0 +1,56 @@
// Copyright 2024 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef UI_BASE_ACCELERATORS_COMMAND_H_
#define UI_BASE_ACCELERATORS_COMMAND_H_
#include <map>
#include <string>
#include <string_view>
#include "base/values.h"
#include "ui/base/accelerators/accelerator.h"
namespace ui {
class COMPONENT_EXPORT(UI_BASE) Command {
public:
Command() = default;
Command(std::string_view command_name,
std::u16string_view description,
bool global);
Command(const Command& other) = default;
virtual ~Command() = default;
// Accessors:
const std::string& command_name() const { return command_name_; }
const ui::Accelerator& accelerator() const { return accelerator_; }
const std::u16string& description() const { return description_; }
bool global() const { return global_; }
// Setter:
void set_command_name(std::string_view command_name) {
command_name_ = command_name;
}
void set_accelerator(const ui::Accelerator& accelerator) {
accelerator_ = accelerator;
}
void set_description(std::u16string_view description) {
description_ = description;
}
void set_global(bool global) { global_ = global; }
private:
std::string command_name_;
ui::Accelerator accelerator_;
std::u16string description_;
bool global_ = false;
};
// A mapping of command name (std::string) to a command object.
using CommandMap = std::map<std::string, Command>;
} // namespace ui
#endif // UI_BASE_ACCELERATORS_COMMAND_H_