Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 "buffet/commands/command_dictionary.h" |
| 6 | |
| 7 | #include <base/values.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 8 | #include <chromeos/strings/string_utils.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 9 | |
| 10 | #include "buffet/commands/command_definition.h" |
| 11 | #include "buffet/commands/schema_constants.h" |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 12 | |
| 13 | namespace buffet { |
| 14 | |
| 15 | std::vector<std::string> CommandDictionary::GetCommandNamesByCategory( |
| 16 | const std::string& category) const { |
| 17 | std::vector<std::string> names; |
| 18 | for (const auto& pair : definitions_) { |
| 19 | if (pair.second->GetCategory() == category) |
| 20 | names.push_back(pair.first); |
| 21 | } |
| 22 | return names; |
| 23 | } |
| 24 | |
| 25 | bool CommandDictionary::LoadCommands(const base::DictionaryValue& json, |
| 26 | const std::string& category, |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 27 | const CommandDictionary* base_commands, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 28 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 29 | CommandMap new_defs; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 30 | |
| 31 | // |json| contains a list of nested objects with the following structure: |
| 32 | // {"<pkg_name>": {"<cmd_name>": {"parameters": {object_schema}}, ...}, ...} |
| 33 | // Iterate over packages |
| 34 | base::DictionaryValue::Iterator package_iter(json); |
| 35 | while (!package_iter.IsAtEnd()) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 36 | std::string package_name = package_iter.key(); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 37 | const base::DictionaryValue* package_value = nullptr; |
| 38 | if (!package_iter.value().GetAsDictionary(&package_value)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 39 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 40 | errors::commands::kTypeMismatch, |
| 41 | "Expecting an object for package '%s'", |
| 42 | package_name.c_str()); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 43 | return false; |
| 44 | } |
| 45 | // Iterate over command definitions within the current package. |
| 46 | base::DictionaryValue::Iterator command_iter(*package_value); |
| 47 | while (!command_iter.IsAtEnd()) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 48 | std::string command_name = command_iter.key(); |
| 49 | if (command_name.empty()) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 50 | chromeos::Error::AddToPrintf( |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 51 | error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 52 | errors::commands::kInvalidCommandName, |
| 53 | "Unnamed command encountered in package '%s'", |
| 54 | package_name.c_str()); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 55 | return false; |
| 56 | } |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 57 | const base::DictionaryValue* command_def_json = nullptr; |
| 58 | if (!command_iter.value().GetAsDictionary(&command_def_json)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 59 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 60 | errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 61 | errors::commands::kTypeMismatch, |
| 62 | "Expecting an object for command '%s'", |
| 63 | command_name.c_str()); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 64 | return false; |
| 65 | } |
| 66 | // Construct the compound command name as "pkg_name.cmd_name". |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 67 | std::string full_command_name = |
| 68 | chromeos::string_utils::Join(".", package_name, command_name); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 69 | |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 70 | const ObjectSchema* base_parameters_def = nullptr; |
| 71 | const ObjectSchema* base_results_def = nullptr; |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 72 | if (base_commands) { |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 73 | auto cmd = base_commands->FindCommand(full_command_name); |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 74 | if (cmd) { |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 75 | base_parameters_def = cmd->GetParameters(); |
| 76 | base_results_def = cmd->GetResults(); |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 77 | } |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 78 | |
| 79 | // If the base command dictionary was provided but the command was not |
| 80 | // found in it, this must be a custom (vendor) command. GCD spec states |
| 81 | // that all custom command names must begin with "_". Let's enforce |
| 82 | // this rule here. |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 83 | if (!cmd) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 84 | if (command_name.front() != '_') { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 85 | chromeos::Error::AddToPrintf( |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 86 | error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 87 | errors::commands::kInvalidCommandName, |
| 88 | "The name of custom command '%s' in package '%s'" |
| 89 | " must start with '_'", |
| 90 | command_name.c_str(), package_name.c_str()); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 91 | return false; |
| 92 | } |
| 93 | } |
| 94 | } |
| 95 | |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 96 | auto parameters_schema = BuildObjectSchema( |
| 97 | command_def_json, |
| 98 | commands::attributes::kCommand_Parameters, |
| 99 | base_parameters_def, |
| 100 | full_command_name, |
| 101 | error); |
| 102 | if (!parameters_schema) |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 103 | return false; |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 104 | |
| 105 | auto results_schema = BuildObjectSchema( |
| 106 | command_def_json, |
| 107 | commands::attributes::kCommand_Results, |
| 108 | base_results_def, |
| 109 | full_command_name, |
| 110 | error); |
| 111 | if (!results_schema) |
| 112 | return false; |
| 113 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 114 | std::unique_ptr<CommandDefinition> command_def{ |
| 115 | new CommandDefinition{category, std::move(parameters_schema), |
| 116 | std::move(results_schema)} |
| 117 | }; |
| 118 | new_defs.emplace(full_command_name, std::move(command_def)); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 119 | |
| 120 | command_iter.Advance(); |
| 121 | } |
| 122 | package_iter.Advance(); |
| 123 | } |
| 124 | |
| 125 | // Verify that newly loaded command definitions do not override existing |
| 126 | // definitions in another category. This is unlikely, but we don't want to let |
| 127 | // one vendor daemon to define the same commands already handled by another |
| 128 | // daemon on the same device. |
| 129 | for (const auto& pair : new_defs) { |
| 130 | auto iter = definitions_.find(pair.first); |
Christopher Wiley | 13fca9d | 2015-01-14 09:56:34 -0800 | [diff] [blame] | 131 | CHECK(iter == definitions_.end()) |
| 132 | << "Definition for command '" << pair.first |
| 133 | << "' overrides an earlier definition in category '" |
| 134 | << iter->second->GetCategory().c_str() << "'"; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 135 | } |
| 136 | |
| 137 | // Now that we successfully loaded all the command definitions, |
| 138 | // remove previous definitions of commands from the same category. |
| 139 | std::vector<std::string> names = GetCommandNamesByCategory(category); |
| 140 | for (const std::string& name : names) |
| 141 | definitions_.erase(name); |
| 142 | |
| 143 | // Insert new definitions into the global map. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 144 | for (auto& pair : new_defs) |
| 145 | definitions_.emplace(pair.first, std::move(pair.second)); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 146 | return true; |
| 147 | } |
| 148 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 149 | std::unique_ptr<ObjectSchema> CommandDictionary::BuildObjectSchema( |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 150 | const base::DictionaryValue* command_def_json, |
| 151 | const char* property_name, |
| 152 | const ObjectSchema* base_def, |
| 153 | const std::string& command_name, |
| 154 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 155 | auto object_schema = ObjectSchema::Create(); |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 156 | |
| 157 | const base::DictionaryValue* schema_def = nullptr; |
| 158 | if (!command_def_json->GetDictionaryWithoutPathExpansion(property_name, |
| 159 | &schema_def)) { |
| 160 | chromeos::Error::AddToPrintf( |
| 161 | error, FROM_HERE, errors::commands::kDomain, |
| 162 | errors::commands::kPropertyMissing, |
| 163 | "Command definition '%s' is missing property '%s'", |
| 164 | command_name.c_str(), |
| 165 | property_name); |
| 166 | return {}; |
| 167 | } |
| 168 | |
| 169 | if (!object_schema->FromJson(schema_def, base_def, error)) { |
| 170 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 171 | errors::commands::kDomain, |
| 172 | errors::commands::kInvalidObjectSchema, |
| 173 | "Invalid definition for command '%s'", |
| 174 | command_name.c_str()); |
| 175 | return {}; |
| 176 | } |
| 177 | |
| 178 | return object_schema; |
| 179 | } |
| 180 | |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 181 | std::unique_ptr<base::DictionaryValue> CommandDictionary::GetCommandsAsJson( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 182 | bool full_schema, chromeos::ErrorPtr* error) const { |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 183 | std::unique_ptr<base::DictionaryValue> dict(new base::DictionaryValue); |
| 184 | for (const auto& pair : definitions_) { |
| 185 | std::unique_ptr<base::DictionaryValue> definition = |
| 186 | pair.second->GetParameters()->ToJson(full_schema, error); |
| 187 | if (!definition) { |
| 188 | dict.reset(); |
| 189 | return dict; |
| 190 | } |
Vitaly Buka | db770e7 | 2015-03-10 19:33:33 -0700 | [diff] [blame] | 191 | auto cmd_name_parts = chromeos::string_utils::SplitAtFirst(pair.first, "."); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 192 | std::string package_name = cmd_name_parts.first; |
| 193 | std::string command_name = cmd_name_parts.second; |
| 194 | base::DictionaryValue* package = nullptr; |
| 195 | if (!dict->GetDictionaryWithoutPathExpansion(package_name, &package)) { |
| 196 | // If this is the first time we encounter this package, create a JSON |
| 197 | // object for it. |
| 198 | package = new base::DictionaryValue; |
| 199 | dict->SetWithoutPathExpansion(package_name, package); |
| 200 | } |
| 201 | base::DictionaryValue* command_def = new base::DictionaryValue; |
| 202 | command_def->SetWithoutPathExpansion( |
| 203 | commands::attributes::kCommand_Parameters, definition.release()); |
| 204 | package->SetWithoutPathExpansion(command_name, command_def); |
| 205 | } |
| 206 | return dict; |
| 207 | } |
| 208 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 209 | const CommandDefinition* CommandDictionary::FindCommand( |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 210 | const std::string& command_name) const { |
| 211 | auto pair = definitions_.find(command_name); |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 212 | return (pair != definitions_.end()) ? pair->second.get() : nullptr; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | void CommandDictionary::Clear() { |
| 216 | definitions_.clear(); |
| 217 | } |
| 218 | |
| 219 | } // namespace buffet |