1 // Copyright (c) 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "chrome/common/extensions/api/commands/commands_handler.h"
6 
7 #include <memory>
8 #include <utility>
9 #include <vector>
10 
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/values.h"
14 #include "chrome/common/extensions/command.h"
15 #include "extensions/common/error_utils.h"
16 #include "extensions/common/manifest_constants.h"
17 #include "extensions/common/manifest_handlers/permissions_parser.h"
18 
19 namespace extensions {
20 
21 namespace keys = manifest_keys;
22 
23 namespace {
24 // The maximum number of commands (including page action/browser actions) with a
25 // keybinding an extension can have.
26 const int kMaxCommandsWithKeybindingPerExtension = 4;
27 }  // namespace
28 
CommandsInfo()29 CommandsInfo::CommandsInfo() {
30 }
31 
~CommandsInfo()32 CommandsInfo::~CommandsInfo() {
33 }
34 
35 // static
GetBrowserActionCommand(const Extension * extension)36 const Command* CommandsInfo::GetBrowserActionCommand(
37     const Extension* extension) {
38   CommandsInfo* info = static_cast<CommandsInfo*>(
39       extension->GetManifestData(keys::kCommands));
40   return info ? info->browser_action_command.get() : NULL;
41 }
42 
43 // static
GetPageActionCommand(const Extension * extension)44 const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) {
45   CommandsInfo* info = static_cast<CommandsInfo*>(
46       extension->GetManifestData(keys::kCommands));
47   return info ? info->page_action_command.get() : NULL;
48 }
49 
50 // static
GetActionCommand(const Extension * extension)51 const Command* CommandsInfo::GetActionCommand(const Extension* extension) {
52   CommandsInfo* info =
53       static_cast<CommandsInfo*>(extension->GetManifestData(keys::kCommands));
54   return info ? info->action_command.get() : nullptr;
55 }
56 
57 // static
GetNamedCommands(const Extension * extension)58 const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) {
59   CommandsInfo* info = static_cast<CommandsInfo*>(
60       extension->GetManifestData(keys::kCommands));
61   return info ? &info->named_commands : NULL;
62 }
63 
CommandsHandler()64 CommandsHandler::CommandsHandler() {
65 }
66 
~CommandsHandler()67 CommandsHandler::~CommandsHandler() {
68 }
69 
Parse(Extension * extension,base::string16 * error)70 bool CommandsHandler::Parse(Extension* extension, base::string16* error) {
71   if (!extension->manifest()->HasKey(keys::kCommands)) {
72     std::unique_ptr<CommandsInfo> commands_info(new CommandsInfo);
73     MaybeSetBrowserActionDefault(extension, commands_info.get());
74     extension->SetManifestData(keys::kCommands, std::move(commands_info));
75     return true;
76   }
77 
78   const base::DictionaryValue* dict = NULL;
79   if (!extension->manifest()->GetDictionary(keys::kCommands, &dict)) {
80     *error = base::ASCIIToUTF16(manifest_errors::kInvalidCommandsKey);
81     return false;
82   }
83 
84   std::unique_ptr<CommandsInfo> commands_info(new CommandsInfo);
85 
86   int command_index = 0;
87   int keybindings_found = 0;
88   for (base::DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd();
89        iter.Advance()) {
90     ++command_index;
91 
92     const base::DictionaryValue* command = NULL;
93     if (!iter.value().GetAsDictionary(&command)) {
94       *error = ErrorUtils::FormatErrorMessageUTF16(
95           manifest_errors::kInvalidKeyBindingDictionary,
96           base::NumberToString(command_index));
97       return false;
98     }
99 
100     std::unique_ptr<extensions::Command> binding(new Command());
101     if (!binding->Parse(command, iter.key(), command_index, error))
102       return false;  // |error| already set.
103 
104     if (binding->accelerator().key_code() != ui::VKEY_UNKNOWN) {
105       // Only media keys are allowed to work without modifiers, and because
106       // media keys aren't registered exclusively they should not count towards
107       // the max of four shortcuts per extension.
108       if (!Command::IsMediaKey(binding->accelerator()))
109         ++keybindings_found;
110 
111       if (keybindings_found > kMaxCommandsWithKeybindingPerExtension &&
112           !PermissionsParser::HasAPIPermission(
113               extension, APIPermission::kCommandsAccessibility)) {
114         *error = ErrorUtils::FormatErrorMessageUTF16(
115             manifest_errors::kInvalidKeyBindingTooMany,
116             base::NumberToString(kMaxCommandsWithKeybindingPerExtension));
117         return false;
118       }
119     }
120 
121     std::string command_name = binding->command_name();
122     if (command_name == manifest_values::kBrowserActionCommandEvent) {
123       commands_info->browser_action_command = std::move(binding);
124     } else if (command_name ==
125                    manifest_values::kPageActionCommandEvent) {
126       commands_info->page_action_command = std::move(binding);
127     } else if (command_name == manifest_values::kActionCommandEvent) {
128       commands_info->action_command = std::move(binding);
129     } else {
130       if (command_name[0] != '_')  // All commands w/underscore are reserved.
131         commands_info->named_commands[command_name] = *binding;
132     }
133   }
134 
135   MaybeSetBrowserActionDefault(extension, commands_info.get());
136 
137   extension->SetManifestData(keys::kCommands, std::move(commands_info));
138   return true;
139 }
140 
AlwaysParseForType(Manifest::Type type) const141 bool CommandsHandler::AlwaysParseForType(Manifest::Type type) const {
142   return type == Manifest::TYPE_EXTENSION ||
143       type == Manifest::TYPE_LEGACY_PACKAGED_APP ||
144       type == Manifest::TYPE_PLATFORM_APP;
145 }
146 
MaybeSetBrowserActionDefault(const Extension * extension,CommandsInfo * info)147 void CommandsHandler::MaybeSetBrowserActionDefault(const Extension* extension,
148                                                    CommandsInfo* info) {
149   // TODO(devlin): Synthesize a command for the "action" key, too?
150   if (extension->manifest()->HasKey(keys::kBrowserAction) &&
151       !info->browser_action_command.get()) {
152     info->browser_action_command.reset(
153         new Command(manifest_values::kBrowserActionCommandEvent,
154                     base::string16(),
155                     std::string(),
156                     false));
157   }
158 }
159 
Keys() const160 base::span<const char* const> CommandsHandler::Keys() const {
161   static constexpr const char* kKeys[] = {keys::kCommands};
162   return kKeys;
163 }
164 
165 }  // namespace extensions
166