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