Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -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_instance.h" |
| 6 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 7 | #include <base/values.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 8 | #include <chromeos/errors/error.h> |
| 9 | #include <chromeos/errors/error_codes.h> |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 10 | |
| 11 | #include "buffet/commands/command_definition.h" |
| 12 | #include "buffet/commands/command_dictionary.h" |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 13 | #include "buffet/commands/command_proxy_interface.h" |
| 14 | #include "buffet/commands/command_queue.h" |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 15 | #include "buffet/commands/schema_constants.h" |
| 16 | #include "buffet/commands/schema_utils.h" |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 17 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 18 | namespace buffet { |
| 19 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 20 | const char CommandInstance::kStatusQueued[] = "queued"; |
| 21 | const char CommandInstance::kStatusInProgress[] = "inProgress"; |
| 22 | const char CommandInstance::kStatusPaused[] = "paused"; |
| 23 | const char CommandInstance::kStatusError[] = "error"; |
| 24 | const char CommandInstance::kStatusDone[] = "done"; |
| 25 | const char CommandInstance::kStatusCanceled[] = "canceled"; |
| 26 | const char CommandInstance::kStatusAborted[] = "aborted"; |
| 27 | const char CommandInstance::kStatusExpired[] = "expired"; |
| 28 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 29 | CommandInstance::CommandInstance( |
| 30 | const std::string& name, |
| 31 | const std::shared_ptr<const CommandDefinition>& command_definition, |
| 32 | const native_types::Object& parameters) |
| 33 | : name_(name), |
| 34 | command_definition_(command_definition), |
| 35 | parameters_(parameters) { |
| 36 | CHECK(command_definition_.get()); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 37 | } |
| 38 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 39 | CommandInstance::~CommandInstance() = default; |
| 40 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 41 | const std::string& CommandInstance::GetCategory() const { |
| 42 | return command_definition_->GetCategory(); |
| 43 | } |
| 44 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 45 | std::shared_ptr<const PropValue> CommandInstance::FindParameter( |
| 46 | const std::string& name) const { |
| 47 | std::shared_ptr<const PropValue> value; |
| 48 | auto p = parameters_.find(name); |
| 49 | if (p != parameters_.end()) |
| 50 | value = p->second; |
| 51 | return value; |
| 52 | } |
| 53 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 54 | namespace { |
| 55 | |
| 56 | // Helper method to retrieve command parameters from the command definition |
| 57 | // object passed in as |json| and corresponding command definition schema |
| 58 | // specified in |command_def|. |
| 59 | // On success, returns |true| and the validated parameters and values through |
| 60 | // |parameters|. Otherwise returns |false| and additional error information in |
| 61 | // |error|. |
| 62 | bool GetCommandParameters(const base::DictionaryValue* json, |
| 63 | const CommandDefinition* command_def, |
| 64 | native_types::Object* parameters, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 65 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 66 | // Get the command parameters from 'parameters' property. |
| 67 | base::DictionaryValue no_params; // Placeholder when no params are specified. |
| 68 | const base::DictionaryValue* params = nullptr; |
| 69 | const base::Value* params_value = nullptr; |
| 70 | if (json->GetWithoutPathExpansion(commands::attributes::kCommand_Parameters, |
| 71 | ¶ms_value)) { |
| 72 | // Make sure the "parameters" property is actually an object. |
| 73 | if (!params_value->GetAsDictionary(¶ms)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 74 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 75 | chromeos::errors::json::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 76 | chromeos::errors::json::kObjectExpected, |
| 77 | "Property '%s' must be a JSON object", |
| 78 | commands::attributes::kCommand_Parameters); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 79 | return false; |
| 80 | } |
| 81 | } else { |
| 82 | // "parameters" are not specified. Assume empty param list. |
| 83 | params = &no_params; |
| 84 | } |
| 85 | |
| 86 | // Now read in the parameters and validate their values against the command |
| 87 | // definition schema. |
| 88 | if (!TypedValueFromJson(params, command_def->GetParameters().get(), |
| 89 | parameters, error)) { |
| 90 | return false; |
| 91 | } |
| 92 | return true; |
| 93 | } |
| 94 | |
| 95 | } // anonymous namespace |
| 96 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 97 | std::unique_ptr<CommandInstance> CommandInstance::FromJson( |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 98 | const base::Value* value, |
| 99 | const CommandDictionary& dictionary, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 100 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 101 | std::unique_ptr<CommandInstance> instance; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 102 | // Get the command JSON object from the value. |
| 103 | const base::DictionaryValue* json = nullptr; |
| 104 | if (!value->GetAsDictionary(&json)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 105 | chromeos::Error::AddTo(error, FROM_HERE, chromeos::errors::json::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 106 | chromeos::errors::json::kObjectExpected, |
| 107 | "Command instance is not a JSON object"); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 108 | return instance; |
| 109 | } |
| 110 | |
| 111 | // Get the command name from 'name' property. |
| 112 | std::string command_name; |
| 113 | if (!json->GetStringWithoutPathExpansion(commands::attributes::kCommand_Name, |
| 114 | &command_name)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 115 | chromeos::Error::AddTo(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 116 | errors::commands::kPropertyMissing, |
| 117 | "Command name is missing"); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 118 | return instance; |
| 119 | } |
| 120 | // Make sure we know how to handle the command with this name. |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 121 | auto command_def = dictionary.FindCommand(command_name); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 122 | if (!command_def) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 123 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 124 | errors::commands::kInvalidCommandName, |
| 125 | "Unknown command received: %s", |
| 126 | command_name.c_str()); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 127 | return instance; |
| 128 | } |
| 129 | |
| 130 | native_types::Object parameters; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 131 | if (!GetCommandParameters(json, command_def.get(), ¶meters, error)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 132 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 133 | errors::commands::kCommandFailed, |
| 134 | "Failed to validate command '%s'", |
| 135 | command_name.c_str()); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 136 | return instance; |
| 137 | } |
| 138 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 139 | instance.reset(new CommandInstance(command_name, command_def, parameters)); |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 140 | // TODO(antonm): Move command_id to ctor and remove setter. |
| 141 | std::string command_id; |
| 142 | if (json->GetStringWithoutPathExpansion(commands::attributes::kCommand_Id, |
| 143 | &command_id)) { |
| 144 | instance->SetID(command_id); |
| 145 | } |
| 146 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 147 | return instance; |
| 148 | } |
| 149 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 150 | void CommandInstance::AddProxy(std::unique_ptr<CommandProxyInterface> proxy) { |
| 151 | proxies_.push_back(std::move(proxy)); |
| 152 | } |
| 153 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 154 | bool CommandInstance::SetResults(const native_types::Object& results) { |
| 155 | // TODO(antonm): Add validation. |
| 156 | if (results != results_) { |
| 157 | results_ = results; |
| 158 | for (auto& proxy : proxies_) { |
| 159 | proxy->OnResultsChanged(results_); |
| 160 | } |
| 161 | } |
| 162 | return true; |
| 163 | } |
| 164 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 165 | bool CommandInstance::SetProgress(int progress) { |
| 166 | if (progress < 0 || progress > 100) |
| 167 | return false; |
| 168 | if (progress != progress_) { |
| 169 | progress_ = progress; |
| 170 | SetStatus(kStatusInProgress); |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 171 | for (auto& proxy : proxies_) { |
| 172 | proxy->OnProgressChanged(progress_); |
| 173 | } |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 174 | } |
| 175 | return true; |
| 176 | } |
| 177 | |
| 178 | void CommandInstance::Abort() { |
| 179 | SetStatus(kStatusAborted); |
| 180 | RemoveFromQueue(); |
| 181 | // The command will be destroyed after that, so do not access any members. |
| 182 | } |
| 183 | |
| 184 | void CommandInstance::Cancel() { |
| 185 | SetStatus(kStatusCanceled); |
| 186 | RemoveFromQueue(); |
| 187 | // The command will be destroyed after that, so do not access any members. |
| 188 | } |
| 189 | |
| 190 | void CommandInstance::Done() { |
| 191 | SetProgress(100); |
| 192 | SetStatus(kStatusDone); |
| 193 | RemoveFromQueue(); |
| 194 | // The command will be destroyed after that, so do not access any members. |
| 195 | } |
| 196 | |
| 197 | void CommandInstance::SetStatus(const std::string& status) { |
| 198 | if (status != status_) { |
| 199 | status_ = status; |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 200 | for (auto& proxy : proxies_) { |
| 201 | proxy->OnStatusChanged(status_); |
| 202 | } |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 203 | } |
| 204 | } |
| 205 | |
| 206 | void CommandInstance::RemoveFromQueue() { |
| 207 | if (queue_) { |
| 208 | // Store this instance in unique_ptr until the end of this method, |
| 209 | // otherwise it will be destroyed as soon as CommandQueue::Remove() returns. |
| 210 | std::unique_ptr<CommandInstance> this_instance = queue_->Remove(GetID()); |
| 211 | // The command instance will survive till the end of this scope. |
| 212 | } |
| 213 | } |
| 214 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 215 | |
| 216 | } // namespace buffet |