| 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/string_number_conversions.h" | 7 #include "base/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 "chrome/common/extensions/manifest.h" | 11 #include "chrome/common/extensions/manifest.h" |
| 12 #include "extensions/common/error_utils.h" | 12 #include "extensions/common/error_utils.h" |
| 13 | 13 |
| 14 namespace keys = extension_manifest_keys; |
| 15 |
| 14 namespace extensions { | 16 namespace extensions { |
| 15 | 17 |
| 16 namespace { | 18 namespace { |
| 17 // The maximum number of commands (including page action/browser actions) an | 19 // The maximum number of commands (including page action/browser actions) an |
| 18 // extension can have. | 20 // extension can have. |
| 19 const size_t kMaxCommandsPerExtension = 4; | 21 const size_t kMaxCommandsPerExtension = 4; |
| 20 } // namespace | 22 } // namespace |
| 21 | 23 |
| 22 CommandsInfo::CommandsInfo() { | 24 CommandsInfo::CommandsInfo() { |
| 23 } | 25 } |
| 24 | 26 |
| 25 CommandsInfo::~CommandsInfo() { | 27 CommandsInfo::~CommandsInfo() { |
| 26 } | 28 } |
| 27 | 29 |
| 28 // static | 30 // static |
| 29 const Command* CommandsInfo::GetBrowserActionCommand( | 31 const Command* CommandsInfo::GetBrowserActionCommand( |
| 30 const Extension* extension) { | 32 const Extension* extension) { |
| 31 CommandsInfo* info = static_cast<CommandsInfo*>( | 33 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 32 extension->GetManifestData(extension_manifest_keys::kCommands)); | 34 extension->GetManifestData(keys::kCommands)); |
| 33 return info ? info->browser_action_command.get() : NULL; | 35 return info ? info->browser_action_command.get() : NULL; |
| 34 } | 36 } |
| 35 | 37 |
| 36 // static | 38 // static |
| 37 const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) { | 39 const Command* CommandsInfo::GetPageActionCommand(const Extension* extension) { |
| 38 CommandsInfo* info = static_cast<CommandsInfo*>( | 40 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 39 extension->GetManifestData(extension_manifest_keys::kCommands)); | 41 extension->GetManifestData(keys::kCommands)); |
| 40 return info ? info->page_action_command.get() : NULL; | 42 return info ? info->page_action_command.get() : NULL; |
| 41 } | 43 } |
| 42 | 44 |
| 43 // static | 45 // static |
| 44 const Command* CommandsInfo::GetScriptBadgeCommand(const Extension* extension) { | 46 const Command* CommandsInfo::GetScriptBadgeCommand(const Extension* extension) { |
| 45 CommandsInfo* info = static_cast<CommandsInfo*>( | 47 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 46 extension->GetManifestData(extension_manifest_keys::kCommands)); | 48 extension->GetManifestData(keys::kCommands)); |
| 47 return info ? info->script_badge_command.get() : NULL; | 49 return info ? info->script_badge_command.get() : NULL; |
| 48 } | 50 } |
| 49 | 51 |
| 50 // static | 52 // static |
| 51 const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) { | 53 const CommandMap* CommandsInfo::GetNamedCommands(const Extension* extension) { |
| 52 CommandsInfo* info = static_cast<CommandsInfo*>( | 54 CommandsInfo* info = static_cast<CommandsInfo*>( |
| 53 extension->GetManifestData(extension_manifest_keys::kCommands)); | 55 extension->GetManifestData(keys::kCommands)); |
| 54 return info ? &info->named_commands : NULL; | 56 return info ? &info->named_commands : NULL; |
| 55 } | 57 } |
| 56 | 58 |
| 57 CommandsHandler::CommandsHandler() { | 59 CommandsHandler::CommandsHandler() { |
| 58 } | 60 } |
| 59 | 61 |
| 60 CommandsHandler::~CommandsHandler() { | 62 CommandsHandler::~CommandsHandler() { |
| 61 } | 63 } |
| 62 | 64 |
| 63 bool CommandsHandler::Parse(const base::Value* value, | 65 bool CommandsHandler::Parse(Extension* extension, string16* error) { |
| 64 Extension* extension, | 66 if (!extension->manifest()->HasKey(keys::kCommands)) { |
| 65 string16* error) { | 67 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); |
| 68 MaybeSetBrowserActionDefault(extension, commands_info.get()); |
| 69 extension->SetManifestData(keys::kCommands, |
| 70 commands_info.release()); |
| 71 return true; |
| 72 } |
| 73 |
| 66 const base::DictionaryValue* dict = NULL; | 74 const base::DictionaryValue* dict = NULL; |
| 67 if (!value->GetAsDictionary(&dict)) { | 75 if (!extension->manifest()->GetDictionary(keys::kCommands, &dict)) { |
| 68 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidCommandsKey); | 76 *error = ASCIIToUTF16(extension_manifest_errors::kInvalidCommandsKey); |
| 69 return false; | 77 return false; |
| 70 } | 78 } |
| 71 | 79 |
| 72 if (dict->size() > kMaxCommandsPerExtension) { | 80 if (dict->size() > kMaxCommandsPerExtension) { |
| 73 *error = ErrorUtils::FormatErrorMessageUTF16( | 81 *error = ErrorUtils::FormatErrorMessageUTF16( |
| 74 extension_manifest_errors::kInvalidKeyBindingTooMany, | 82 extension_manifest_errors::kInvalidKeyBindingTooMany, |
| 75 base::IntToString(kMaxCommandsPerExtension)); | 83 base::IntToString(kMaxCommandsPerExtension)); |
| 76 return false; | 84 return false; |
| 77 } | 85 } |
| (...skipping 27 matching lines...) Expand all Loading... |
| 105 extension_manifest_values::kScriptBadgeCommandEvent) { | 113 extension_manifest_values::kScriptBadgeCommandEvent) { |
| 106 commands_info->script_badge_command.reset(binding.release()); | 114 commands_info->script_badge_command.reset(binding.release()); |
| 107 } else { | 115 } else { |
| 108 if (command_name[0] != '_') // All commands w/underscore are reserved. | 116 if (command_name[0] != '_') // All commands w/underscore are reserved. |
| 109 commands_info->named_commands[command_name] = *binding.get(); | 117 commands_info->named_commands[command_name] = *binding.get(); |
| 110 } | 118 } |
| 111 } | 119 } |
| 112 | 120 |
| 113 MaybeSetBrowserActionDefault(extension, commands_info.get()); | 121 MaybeSetBrowserActionDefault(extension, commands_info.get()); |
| 114 | 122 |
| 115 extension->SetManifestData(extension_manifest_keys::kCommands, | 123 extension->SetManifestData(keys::kCommands, |
| 116 commands_info.release()); | 124 commands_info.release()); |
| 117 return true; | 125 return true; |
| 118 } | 126 } |
| 119 | 127 |
| 120 bool CommandsHandler::HasNoKey(Extension* extension, | 128 bool CommandsHandler::AlwaysParseForType(Extension::Type type) { |
| 121 string16* error) { | 129 return type == Extension::TYPE_EXTENSION || |
| 122 scoped_ptr<CommandsInfo> commands_info(new CommandsInfo); | 130 type == Extension::TYPE_LEGACY_PACKAGED_APP || |
| 123 MaybeSetBrowserActionDefault(extension, commands_info.get()); | 131 type == Extension::TYPE_PLATFORM_APP; |
| 124 extension->SetManifestData(extension_manifest_keys::kCommands, | |
| 125 commands_info.release()); | |
| 126 return true; | |
| 127 } | 132 } |
| 128 | 133 |
| 129 void CommandsHandler::MaybeSetBrowserActionDefault(const Extension* extension, | 134 void CommandsHandler::MaybeSetBrowserActionDefault(const Extension* extension, |
| 130 CommandsInfo* info) { | 135 CommandsInfo* info) { |
| 131 if (extension->manifest()->HasKey(extension_manifest_keys::kBrowserAction) && | 136 if (extension->manifest()->HasKey(keys::kBrowserAction) && |
| 132 !info->browser_action_command.get()) { | 137 !info->browser_action_command.get()) { |
| 133 info->browser_action_command.reset(new Command( | 138 info->browser_action_command.reset(new Command( |
| 134 extension_manifest_values::kBrowserActionCommandEvent, string16(), "")); | 139 extension_manifest_values::kBrowserActionCommandEvent, string16(), "")); |
| 135 } | 140 } |
| 136 } | 141 } |
| 137 | 142 |
| 138 } // namespace extensions | 143 } // namespace extensions |
| OLD | NEW |