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

@@ -72,6 +72,7 @@
#include "extensions/common/permissions/permissions_data.h" #include "extensions/common/permissions/permissions_data.h"
#include "extensions/grit/extensions_browser_resources.h" #include "extensions/grit/extensions_browser_resources.h"
#include "third_party/skia/include/core/SkBitmap.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/l10n/l10n_util.h"
#include "ui/base/resource/resource_bundle.h" #include "ui/base/resource/resource_bundle.h"
#include "ui/gfx/codec/png_codec.h" #include "ui/gfx/codec/png_codec.h"
@@ -193,7 +194,7 @@ developer::RuntimeError ConstructRuntimeError(const RuntimeError& error) {
void ConstructCommands(CommandService* command_service, void ConstructCommands(CommandService* command_service,
const ExtensionId& extension_id, const ExtensionId& extension_id,
std::vector<developer::Command>* commands) { 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) { bool is_extension_action) {
developer::Command command_value; developer::Command command_value;
command_value.description = 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, if (command_service->GetNamedCommands(extension_id,
CommandService::ALL, CommandService::ALL,
CommandService::ANY_SCOPE, CommandService::ANY_SCOPE,
&named_commands)) { &named_commands)) {
for (auto& pair : 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 // TODO(devlin): For some reason beyond my knowledge, FindCommandByName
// returns different data than GetNamedCommands, including the // returns different data than GetNamedCommands, including the
// accelerators, but not the descriptions - and even then, only if 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/feature_switch.h"
#include "extensions/common/manifest_constants.h" #include "extensions/common/manifest_constants.h"
#include "extensions/common/permissions/permissions_data.h" #include "extensions/common/permissions/permissions_data.h"
#include "ui/base/accelerators/command.h"
namespace extensions { namespace extensions {
namespace { namespace {
@@ -126,7 +127,7 @@ CommandService* CommandService::Get(content::BrowserContext* context) {
bool CommandService::GetNamedCommands(const ExtensionId& extension_id, bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
QueryType type, QueryType type,
CommandScope scope, CommandScope scope,
CommandMap* command_map) const { ui::CommandMap* command_map) const {
const Extension* extension = const Extension* extension =
GetExtensionInEnabledOrDisabledExtensions(extension_id); GetExtensionInEnabledOrDisabledExtensions(extension_id);
if (!extension) { if (!extension) {
@@ -134,7 +135,7 @@ bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
} }
command_map->clear(); command_map->clear();
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (!commands) if (!commands)
return false; return false;
@@ -147,7 +148,7 @@ bool CommandService::GetNamedCommands(const ExtensionId& extension_id,
if (type == ACTIVE && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN) if (type == ACTIVE && shortcut_assigned.key_code() == ui::VKEY_UNKNOWN)
continue; continue;
Command command = named_command.second; ui::Command command = named_command.second;
if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global())) if (scope != ANY_SCOPE && ((scope == GLOBAL) != saved_command.global()))
continue; continue;
@@ -354,14 +355,14 @@ void CommandService::UpdateKeybindings(const Extension* extension) {
void CommandService::RemoveRelinquishedKeybindings(const Extension* extension) { void CommandService::RemoveRelinquishedKeybindings(const Extension* extension) {
// Remove keybindings if they have been removed by the extension and the user // Remove keybindings if they have been removed by the extension and the user
// has not modified them. // has not modified them.
CommandMap existing_command_map; ui::CommandMap existing_command_map;
if (GetNamedCommands(extension->id(), if (GetNamedCommands(extension->id(),
CommandService::ACTIVE, CommandService::ACTIVE,
CommandService::REGULAR, CommandService::REGULAR,
&existing_command_map)) { &existing_command_map)) {
const CommandMap* new_command_map = const ui::CommandMap* new_command_map =
CommandsInfo::GetNamedCommands(extension); 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) { it != existing_command_map.end(); ++it) {
std::string command_name = it->first; std::string command_name = it->first;
if (new_command_map->find(command_name) == new_command_map->end() && 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) { void CommandService::AssignKeybindings(const Extension* extension) {
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (!commands) if (!commands)
return; return;
for (const auto& named_command : *commands) { for (const auto& named_command : *commands) {
const Command command = named_command.second; const ui::Command command = named_command.second;
if (CanAutoAssign(command, extension)) { if (CanAutoAssign(command, extension)) {
AddKeybindingPref(command.accelerator(), AddKeybindingPref(command.accelerator(),
extension->id(), 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) { const Extension* extension) {
// Extensions are allowed to auto-assign updated keys if the user has not // Extensions are allowed to auto-assign updated keys if the user has not
// changed from the previous value. // changed from the previous value.
@@ -524,10 +525,10 @@ void CommandService::UpdateExtensionSuggestedCommandPrefs(
const Extension* extension) { const Extension* extension) {
base::Value::Dict suggested_key_prefs; base::Value::Dict suggested_key_prefs;
const CommandMap* commands = CommandsInfo::GetNamedCommands(extension); const ui::CommandMap* commands = CommandsInfo::GetNamedCommands(extension);
if (commands) { if (commands) {
for (const auto& named_command : *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; base::Value::Dict command_keys;
command_keys.Set(kSuggestedKey, command_keys.Set(kSuggestedKey,
Command::AcceleratorToString(command.accelerator())); Command::AcceleratorToString(command.accelerator()));
@@ -573,7 +574,7 @@ void CommandService::RemoveDefunctExtensionSuggestedCommandPrefs(
if (current_prefs) { if (current_prefs) {
base::Value::Dict suggested_key_prefs = current_prefs->Clone(); base::Value::Dict suggested_key_prefs = current_prefs->Clone();
const CommandMap* named_commands = const ui::CommandMap* named_commands =
CommandsInfo::GetNamedCommands(extension); CommandsInfo::GetNamedCommands(extension);
const Command* browser_action_command = const Command* browser_action_command =

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

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

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

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

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

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

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

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

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

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

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

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

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

@@ -11,18 +11,19 @@
#include "base/values.h" #include "base/values.h"
#include "ui/base/accelerators/accelerator.h" #include "ui/base/accelerators/accelerator.h"
#include "ui/base/accelerators/command.h"
namespace extensions { namespace extensions {
class Command { class Command : public ui::Command {
public: public:
Command(); Command() = default;
Command(std::string_view command_name, Command(std::string_view command_name,
std::u16string_view description, std::u16string_view description,
std::string_view accelerator, std::string_view accelerator,
bool global); bool global);
Command(const Command& other); Command(const Command& other) = default;
~Command(); ~Command() override = default;
// The platform value for the Command. // The platform value for the Command.
static std::string CommandPlatform(); static std::string CommandPlatform();
@@ -46,29 +47,8 @@ class Command {
std::string_view command_name, std::string_view command_name,
int index, int index,
std::u16string* error); 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 } // namespace extensions
#endif // EXTENSIONS_COMMON_COMMAND_H_ #endif // EXTENSIONS_COMMON_COMMAND_H_

@@ -117,6 +117,8 @@ component("base") {
sources = [ sources = [
"accelerators/accelerator.cc", "accelerators/accelerator.cc",
"accelerators/accelerator.h", "accelerators/accelerator.h",
"accelerators/command.cc",
"accelerators/command.h",
"class_property.cc", "class_property.cc",
"class_property.h", "class_property.h",
"default_style.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_