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
|
// Copyright (c) 2013 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/common/extensions/api/commands/commands_handler.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/utf_string_conversions.h"
#include "base/values.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/manifest_constants.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() {
}
CommandsInfo::~CommandsInfo() {
}
// static
const Command* CommandsInfo::GetBrowserActionCommand(
const Extension* extension) {
CommandsInfo* info = static_cast<CommandsInfo*>(
extension->GetManifestData(keys::kCommands));
return info ? info->browser_action_command.get() : NULL;
}
// static
const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) {
CommandsInfo* info = static_cast<CommandsInfo*>(
extension->GetManifestData(keys::kCommands));
return info ? info->page_action_command.get() : NULL;
}
// static
const Command* CommandsInfo::GetScriptBadgeCommand(const Extension* extension) {
CommandsInfo* info = static_cast<CommandsInfo*>(
extension->GetManifestData(keys::kCommands));
return info ? info->script_badge_command.get() : NULL;
}
// static
const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) {
CommandsInfo* info = static_cast<CommandsInfo*>(
extension->GetManifestData(keys::kCommands));
return info ? &info->named_commands : NULL;
}
CommandsHandler::CommandsHandler() {
}
CommandsHandler::~CommandsHandler() {
}
bool CommandsHandler::Parse(Extension* extension, string16* error) {
if (!extension->manifest()->HasKey(keys::kCommands)) {
scoped_ptr<CommandsInfo> commands_info(new CommandsInfo);
MaybeSetBrowserActionDefault(extension, commands_info.get());
extension->SetManifestData(keys::kCommands,
commands_info.release());
return true;
}
const base::DictionaryValue* dict = NULL;
if (!extension->manifest()->GetDictionary(keys::kCommands, &dict)) {
*error = ASCIIToUTF16(manifest_errors::kInvalidCommandsKey);
return false;
}
scoped_ptr<CommandsInfo> commands_info(new CommandsInfo);
int command_index = 0;
int keybindings_found = 0;
for (DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd();
iter.Advance()) {
++command_index;
const DictionaryValue* command = NULL;
if (!iter.value().GetAsDictionary(&command)) {
*error = ErrorUtils::FormatErrorMessageUTF16(
manifest_errors::kInvalidKeyBindingDictionary,
base::IntToString(command_index));
return false;
}
scoped_ptr<extensions::Command> binding(new Command());
if (!binding->Parse(command, iter.key(), command_index, error))
return false; // |error| already set.
if (binding->accelerator().key_code() != ui::VKEY_UNKNOWN) {
if (++keybindings_found > kMaxCommandsWithKeybindingPerExtension) {
*error = ErrorUtils::FormatErrorMessageUTF16(
manifest_errors::kInvalidKeyBindingTooMany,
base::IntToString(kMaxCommandsWithKeybindingPerExtension));
return false;
}
}
std::string command_name = binding->command_name();
if (command_name == manifest_values::kBrowserActionCommandEvent) {
commands_info->browser_action_command.reset(binding.release());
} else if (command_name ==
manifest_values::kPageActionCommandEvent) {
commands_info->page_action_command.reset(binding.release());
} else if (command_name ==
manifest_values::kScriptBadgeCommandEvent) {
commands_info->script_badge_command.reset(binding.release());
} else {
if (command_name[0] != '_') // All commands w/underscore are reserved.
commands_info->named_commands[command_name] = *binding.get();
}
}
MaybeSetBrowserActionDefault(extension, commands_info.get());
extension->SetManifestData(keys::kCommands,
commands_info.release());
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::MaybeSetBrowserActionDefault(const Extension* extension,
CommandsInfo* info) {
if (extension->manifest()->HasKey(keys::kBrowserAction) &&
!info->browser_action_command.get()) {
info->browser_action_command.reset(
new Command(manifest_values::kBrowserActionCommandEvent,
string16(),
std::string()));
}
}
const std::vector<std::string> CommandsHandler::Keys() const {
return SingleKey(keys::kCommands);
}
} // namespace extensions
|