OLD | NEW |
1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/common/extensions/api/commands/commands_handler.h" | 5 #include "chrome/common/extensions/api/commands/commands_handler.h" |
6 | 6 |
7 #include "base/strings/string_number_conversions.h" | 7 #include "base/strings/string_number_conversions.h" |
8 #include "base/utf_string_conversions.h" | 8 #include "base/utf_string_conversions.h" |
9 #include "base/values.h" | 9 #include "base/values.h" |
10 #include "chrome/common/extensions/extension_manifest_constants.h" | 10 #include "chrome/common/extensions/extension_manifest_constants.h" |
11 #include "extensions/common/error_utils.h" | 11 #include "extensions/common/error_utils.h" |
12 | 12 |
13 namespace keys = extension_manifest_keys; | 13 namespace keys = extension_manifest_keys; |
14 | 14 |
15 namespace extensions { | 15 namespace extensions { |
16 | 16 |
17 namespace { | 17 namespace { |
18 // The maximum number of commands (including page action/browser actions) an | 18 // The maximum number of commands (including page action/browser actions) with a |
19 // extension can have. | 19 // keybinding an extension can have. |
20 const size_t kMaxCommandsPerExtension = 4; | 20 const int kMaxCommandsWithKeybindingPerExtension = 4; |
21 } // namespace | 21 } // namespace |
22 | 22 |
23 CommandsInfo::CommandsInfo() { | 23 CommandsInfo::CommandsInfo() { |
24 } | 24 } |
25 | 25 |
26 CommandsInfo::~CommandsInfo() { | 26 CommandsInfo::~CommandsInfo() { |
27 } | 27 } |
28 | 28 |
29 // static | 29 // static |
30 const Command* CommandsInfo::GetBrowserActionCommand( | 30 const Command* CommandsInfo::GetBrowserActionCommand( |
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 commands_info.release()); | 69 commands_info.release()); |
70 return true; | 70 return true; |
71 } | 71 } |
72 | 72 |
73 const base::DictionaryValue* dict = NULL; | 73 const base::DictionaryValue* dict = NULL; |
74 if (!extension->manifest()->GetDictionary(keys::kCommands, &dict)) { | 74 if (!extension->manifest()->GetDictionary(keys::kCommands, &dict)) { |
75 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidCommandsKey); | 75 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidCommandsKey); |
76 return false; | 76 return false; |
77 } | 77 } |
78 | 78 |
79 if (dict->size() > kMaxCommandsPerExtension) { | |
80 *error = ErrorUtils::FormatErrorMessageUTF16( | |
81 extension_manifest_errors::kInvalidKeyBindingTooMany, | |
82 base::IntToString(kMaxCommandsPerExtension)); | |
83 return false; | |
84 } | |
85 | |
86 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); | 79 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); |
87 | 80 |
88 int command_index = 0; | 81 int command_index = 0; |
| 82 int keybindings_found = 0; |
89 for (DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd(); | 83 for (DictionaryValue::Iterator iter(*dict); !iter.IsAtEnd(); |
90 iter.Advance()) { | 84 iter.Advance()) { |
91 ++command_index; | 85 ++command_index; |
92 | 86 |
93 const DictionaryValue* command = NULL; | 87 const DictionaryValue* command = NULL; |
94 if (!iter.value().GetAsDictionary(&command)) { | 88 if (!iter.value().GetAsDictionary(&command)) { |
95 *error = ErrorUtils::FormatErrorMessageUTF16( | 89 *error = ErrorUtils::FormatErrorMessageUTF16( |
96 extension_manifest_errors::kInvalidKeyBindingDictionary, | 90 extension_manifest_errors::kInvalidKeyBindingDictionary, |
97 base::IntToString(command_index)); | 91 base::IntToString(command_index)); |
98 return false; | 92 return false; |
99 } | 93 } |
100 | 94 |
101 scoped_ptr<extensions::Command> binding(new Command()); | 95 scoped_ptr<extensions::Command> binding(new Command()); |
102 if (!binding->Parse(command, iter.key(), command_index, error)) | 96 if (!binding->Parse(command, iter.key(), command_index, error)) |
103 return false; // |error| already set. | 97 return false; // |error| already set. |
104 | 98 |
| 99 if (binding->accelerator().key_code() != ui::VKEY_UNKNOWN) { |
| 100 if (++keybindings_found > kMaxCommandsWithKeybindingPerExtension) { |
| 101 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 102 extension_manifest_errors::kInvalidKeyBindingTooMany, |
| 103 base::IntToString(kMaxCommandsWithKeybindingPerExtension)); |
| 104 return false; |
| 105 } |
| 106 } |
| 107 |
105 std::string command_name = binding->command_name(); | 108 std::string command_name = binding->command_name(); |
106 if (command_name == extension_manifest_values::kBrowserActionCommandEvent) { | 109 if (command_name == extension_manifest_values::kBrowserActionCommandEvent) { |
107 commands_info->browser_action_command.reset(binding.release()); | 110 commands_info->browser_action_command.reset(binding.release()); |
108 } else if (command_name == | 111 } else if (command_name == |
109 extension_manifest_values::kPageActionCommandEvent) { | 112 extension_manifest_values::kPageActionCommandEvent) { |
110 commands_info->page_action_command.reset(binding.release()); | 113 commands_info->page_action_command.reset(binding.release()); |
111 } else if (command_name == | 114 } else if (command_name == |
112 extension_manifest_values::kScriptBadgeCommandEvent) { | 115 extension_manifest_values::kScriptBadgeCommandEvent) { |
113 commands_info->script_badge_command.reset(binding.release()); | 116 commands_info->script_badge_command.reset(binding.release()); |
114 } else { | 117 } else { |
(...skipping 22 matching lines...) Expand all Loading... |
137 info->browser_action_command.reset(new Command( | 140 info->browser_action_command.reset(new Command( |
138 extension_manifest_values::kBrowserActionCommandEvent, string16(), "")); | 141 extension_manifest_values::kBrowserActionCommandEvent, string16(), "")); |
139 } | 142 } |
140 } | 143 } |
141 | 144 |
142 const std::vector<std::string> CommandsHandler::Keys() const { | 145 const std::vector<std::string> CommandsHandler::Keys() const { |
143 return SingleKey(keys::kCommands); | 146 return SingleKey(keys::kCommands); |
144 } | 147 } |
145 | 148 |
146 } // namespace extensions | 149 } // namespace extensions |
OLD | NEW |