1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100
|
// Copyright 2025 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ui/android/extensions/extension_keybinding_registry_android.h"
#include "base/containers/contains.h"
#include "base/functional/bind.h"
#include "chrome/browser/extensions/commands/command_service.h"
#include "chrome/browser/extensions/extension_keybinding_registry.h"
#include "chrome/browser/profiles/profile.h"
#include "extensions/browser/extension_system.h"
#include "extensions/common/extension.h"
#include "third_party/jni_zero/jni_zero.h"
#include "ui/base/accelerators/accelerator.h"
#include "ui/events/android/key_event_android.h"
#include "ui/events/event.h"
#include "ui/events/keycodes/keyboard_codes.h"
#include "ui/events/platform_event.h"
namespace extensions {
ExtensionKeybindingRegistryAndroid::ExtensionKeybindingRegistryAndroid(
content::BrowserContext* context)
: ExtensionKeybindingRegistry(context,
ExtensionFilter::ALL_EXTENSIONS,
nullptr) {}
ExtensionKeybindingRegistryAndroid::~ExtensionKeybindingRegistryAndroid() =
default;
bool ExtensionKeybindingRegistryAndroid::PopulateCommands(
const Extension* extension,
ui::CommandMap* commands) {
CommandService* command_service = CommandService::Get(browser_context());
bool populated_named_commands =
command_service->GetNamedCommands(extension->id(), CommandService::ACTIVE,
CommandService::REGULAR, commands);
Command cmd;
bool populated_action_command = command_service->GetExtensionActionCommand(
extension->id(), ActionInfo::Type::kAction,
CommandService::QueryType::ACTIVE, &cmd, /*active=*/nullptr);
if (populated_action_command) {
(*commands)[cmd.command_name()] = cmd;
}
return populated_named_commands || populated_action_command;
}
bool ExtensionKeybindingRegistryAndroid::RegisterAccelerator(
const ui::Accelerator& accelerator,
const ExtensionId& extension_id,
const std::string& command_name) {
active_accelerators_.insert(accelerator);
if (Command::IsActionRelatedCommand(command_name)) {
active_action_accelerators_[accelerator] = extension_id;
}
return true;
}
void ExtensionKeybindingRegistryAndroid::UnregisterAccelerator(
const ui::Accelerator& accelerator) {
active_accelerators_.erase(accelerator);
active_action_accelerators_.erase(accelerator);
}
void ExtensionKeybindingRegistryAndroid::OnShortcutHandlingSuspended(
bool suspended) {
is_shortcut_handling_suspended_ = suspended;
}
bool ExtensionKeybindingRegistryAndroid::ShouldIgnoreCommand(
const std::string& command) const {
// This class supports action related commands, so ignore nothing.
return false;
}
std::variant<bool, std::string>
ExtensionKeybindingRegistryAndroid::HandleKeyDownEvent(
const ui::KeyEventAndroid& key_event) {
if (is_shortcut_handling_suspended_) {
return false;
}
ui::PlatformEvent native_event(key_event);
ui::Accelerator accelerator((ui::KeyEvent(native_event)));
if (!active_accelerators_.contains(accelerator)) {
return false;
}
auto it = active_action_accelerators_.find(accelerator);
if (it != active_action_accelerators_.end()) {
return it->second;
}
return NotifyEventTargets(accelerator);
}
} // namespace extensions
|