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