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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
|
// Copyright 2013 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "extensions/common/api/commands/commands_handler.h"
#include <memory>
#include <utility>
#include <vector>
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/command.h"
#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 {
namespace keys = manifest_keys;
namespace {
// The maximum number of commands (including page action/browser actions) with a
// keybinding an extension can have.
const int kMaxCommandsWithKeybindingPerExtension = 4;
} // namespace
CommandsInfo::CommandsInfo() = default;
CommandsInfo::~CommandsInfo() = default;
// static
const Command* CommandsInfo::GetBrowserActionCommand(
const Extension* extension) {
auto* info =
static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
return info ? info->browser_action_command.get() : nullptr;
}
// static
const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) {
auto* info =
static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
return info ? info->page_action_command.get() : nullptr;
}
// static
const Command* CommandsInfo::GetActionCommand(const Extension* extension) {
auto* info =
static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
return info ? info->action_command.get() : nullptr;
}
// static
const ui::CommandMap* CommandsInfo::GetNamedCommands(
const Extension* extension) {
auto* info =
static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
return info ? &info->named_commands : nullptr;
}
CommandsHandler::CommandsHandler() = default;
CommandsHandler::~CommandsHandler() = default;
bool CommandsHandler::Parse(Extension* extension, std::u16string* error) {
if (!extension->manifest()->FindKey(keys::kCommands)) {
std::unique_ptr<CommandsInfo> commands_info(new CommandsInfo);
MaybeSetActionDefault(extension, commands_info.get());
extension->SetManifestData(keys::kCommands, std::move(commands_info));
return true;
}
const base::Value::Dict* dict =
extension->manifest()->available_values().FindDict(keys::kCommands);
if (!dict) {
*error = manifest_errors::kInvalidCommandsKey;
return false;
}
std::unique_ptr<CommandsInfo> commands_info(new CommandsInfo);
bool invalid_action_command_specified = false;
int command_index = 0;
int keybindings_found = 0;
for (const auto item : *dict) {
++command_index;
const base::Value::Dict* command = item.second.GetIfDict();
if (!command) {
*error = ErrorUtils::FormatErrorMessageUTF16(
manifest_errors::kInvalidKeyBindingDictionary,
base::NumberToString(command_index));
return false;
}
std::unique_ptr<extensions::Command> binding(new Command());
if (!binding->Parse(*command, item.first, command_index, error))
return false; // |error| already set.
if (binding->accelerator().key_code() != ui::VKEY_UNKNOWN) {
// Only media keys are allowed to work without modifiers, and because
// media keys aren't registered exclusively they should not count towards
// the max of four shortcuts per extension.
if (!binding->accelerator().IsMediaKey()) {
++keybindings_found;
}
if (keybindings_found > kMaxCommandsWithKeybindingPerExtension &&
!PermissionsParser::HasAPIPermission(
extension, mojom::APIPermissionID::kCommandsAccessibility)) {
*error = ErrorUtils::FormatErrorMessageUTF16(
manifest_errors::kInvalidKeyBindingTooMany,
base::NumberToString(kMaxCommandsWithKeybindingPerExtension));
return false;
}
}
std::string command_name = binding->command_name();
// Set the command only if it's correct for the manifest's action type. This
// relies on the fact that manifests cannot have multiple action types.
if (command_name == manifest_values::kActionCommandEvent) {
if (extension->manifest()->FindKey(keys::kAction))
commands_info->action_command = std::move(binding);
else
invalid_action_command_specified = true;
} else if (command_name == manifest_values::kBrowserActionCommandEvent) {
if (extension->manifest()->FindKey(keys::kBrowserAction))
commands_info->browser_action_command = std::move(binding);
else
invalid_action_command_specified = true;
} else if (command_name == manifest_values::kPageActionCommandEvent) {
if (extension->manifest()->FindKey(keys::kPageAction))
commands_info->page_action_command = std::move(binding);
else
invalid_action_command_specified = true;
} else if (command_name[0] != '_') { // Commands w/underscore are reserved.
commands_info->named_commands[command_name] = *binding;
}
}
if (invalid_action_command_specified) {
extension->AddInstallWarning(InstallWarning(
manifest_errors::kCommandActionIncorrectForManifestActionType,
manifest_keys::kCommands));
}
MaybeSetActionDefault(extension, commands_info.get());
extension->SetManifestData(keys::kCommands, std::move(commands_info));
return true;
}
bool CommandsHandler::AlwaysParseForType(Manifest::Type type) const {
return type == Manifest::TYPE_EXTENSION ||
type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
type == Manifest::TYPE_PLATFORM_APP;
}
void CommandsHandler::MaybeSetActionDefault(const Extension* extension,
CommandsInfo* info) {
if (extension->manifest()->FindKey(keys::kAction) &&
!info->action_command.get()) {
info->action_command =
std::make_unique<Command>(manifest_values::kActionCommandEvent,
std::u16string(), std::string(), false);
} else if (extension->manifest()->FindKey(keys::kBrowserAction) &&
!info->browser_action_command.get()) {
info->browser_action_command =
std::make_unique<Command>(manifest_values::kBrowserActionCommandEvent,
std::u16string(), std::string(), false);
}
}
base::span<const char* const> CommandsHandler::Keys() const {
static constexpr const char* kKeys[] = {keys::kCommands};
return kKeys;
}
} // namespace extensions
|