| OLD | NEW |
| (Empty) | |
| 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/string_number_conversions.h" |
| 8 #include "base/utf_string_conversions.h" |
| 9 #include "base/values.h" |
| 10 #include "chrome/common/extensions/extension_manifest_constants.h" |
| 11 #include "chrome/common/extensions/manifest.h" |
| 12 #include "extensions/common/error_utils.h" |
| 13 |
| 14 namespace extensions { |
| 15 |
| 16 namespace { |
| 17 // The maximum number of commands (including page action/browser actions) an |
| 18 // extension can have. |
| 19 const size_t kMaxCommandsPerExtension = 4; |
| 20 } // namespace |
| 21 |
| 22 CommandsInfo::CommandsInfo() { |
| 23 } |
| 24 |
| 25 CommandsInfo::~CommandsInfo() { |
| 26 } |
| 27 |
| 28 // static |
| 29 const Command* CommandsInfo::GetBrowserActionCommand( |
| 30 const Extension* extension) { |
| 31 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 32 extension->GetManifestData(extension_manifest_keys::kCommands)); |
| 33 return info ? info->browser_action_command.get() : NULL; |
| 34 } |
| 35 |
| 36 // static |
| 37 const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) { |
| 38 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 39 extension->GetManifestData(extension_manifest_keys::kCommands)); |
| 40 return info ? info->page_action_command.get() : NULL; |
| 41 } |
| 42 |
| 43 // static |
| 44 const Command* CommandsInfo::GetScriptBadgeCommand(const Extension* extension) { |
| 45 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 46 extension->GetManifestData(extension_manifest_keys::kCommands)); |
| 47 return info ? info->script_badge_command.get() : NULL; |
| 48 } |
| 49 |
| 50 // static |
| 51 const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) { |
| 52 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 53 extension->GetManifestData(extension_manifest_keys::kCommands)); |
| 54 return info ? &info->named_commands : NULL; |
| 55 } |
| 56 |
| 57 CommandsHandler::CommandsHandler() { |
| 58 } |
| 59 |
| 60 CommandsHandler::~CommandsHandler() { |
| 61 } |
| 62 |
| 63 bool CommandsHandler::Parse(const base::Value* value, |
| 64 Extension* extension, |
| 65 string16* error) { |
| 66 const base::DictionaryValue* dict = NULL; |
| 67 if (!value->GetAsDictionary(&dict)) { |
| 68 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidCommandsKey); |
| 69 return false; |
| 70 } |
| 71 |
| 72 if (dict->size() > kMaxCommandsPerExtension) { |
| 73 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 74 extension_manifest_errors::kInvalidKeyBindingTooMany, |
| 75 base::IntToString(kMaxCommandsPerExtension)); |
| 76 return false; |
| 77 } |
| 78 |
| 79 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); |
| 80 |
| 81 int command_index = 0; |
| 82 for (DictionaryValue::key_iterator iter = dict->begin_keys(); |
| 83 iter != dict->end_keys(); ++iter) { |
| 84 ++command_index; |
| 85 |
| 86 const DictionaryValue* command = NULL; |
| 87 if (!dict->GetDictionary(*iter, &command)) { |
| 88 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 89 extension_manifest_errors::kInvalidKeyBindingDictionary, |
| 90 base::IntToString(command_index)); |
| 91 return false; |
| 92 } |
| 93 |
| 94 scoped_ptr<extensions::Command> binding(new Command()); |
| 95 if (!binding->Parse(command, *iter, command_index, error)) |
| 96 return false; // |error| already set. |
| 97 |
| 98 std::string command_name = binding->command_name(); |
| 99 if (command_name == extension_manifest_values::kBrowserActionCommandEvent) { |
| 100 commands_info->browser_action_command.reset(binding.release()); |
| 101 } else if (command_name == |
| 102 extension_manifest_values::kPageActionCommandEvent) { |
| 103 commands_info->page_action_command.reset(binding.release()); |
| 104 } else if (command_name == |
| 105 extension_manifest_values::kScriptBadgeCommandEvent) { |
| 106 commands_info->script_badge_command.reset(binding.release()); |
| 107 } else { |
| 108 if (command_name[0] != '_') // All commands w/underscore are reserved. |
| 109 commands_info->named_commands[command_name] = *binding.get(); |
| 110 } |
| 111 } |
| 112 |
| 113 MaybeSetBrowserActionDefault(extension, commands_info.get()); |
| 114 |
| 115 extension->SetManifestData(extension_manifest_keys::kCommands, |
| 116 commands_info.release()); |
| 117 return true; |
| 118 } |
| 119 |
| 120 bool CommandsHandler::HasNoKey(Extension* extension, |
| 121 string16* error) { |
| 122 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); |
| 123 MaybeSetBrowserActionDefault(extension, commands_info.get()); |
| 124 extension->SetManifestData(extension_manifest_keys::kCommands, |
| 125 commands_info.release()); |
| 126 return true; |
| 127 } |
| 128 |
| 129 void CommandsHandler::MaybeSetBrowserActionDefault(const Extension* extension, |
| 130 CommandsInfo* info) { |
| 131 if (extension->manifest()->HasKey(extension_manifest_keys::kBrowserAction) && |
| 132 !info->browser_action_command.get()) { |
| 133 info->browser_action_command.reset(new Command( |
| 134 extension_manifest_values::kBrowserActionCommandEvent, string16(), "")); |
| 135 } |
| 136 } |
| 137 |
| 138 } // namespace extensions |
| OLD | NEW |