Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 5 | #include "src/commands/command_instance.h" |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 7 | #include <base/values.h> |
Vitaly Buka | 7b382ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 8 | #include <weave/enum_to_string.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 9 | #include <weave/error.h> |
Alex Vakulenko | 4e4dfd4 | 2015-08-06 14:01:42 -0700 | [diff] [blame] | 10 | #include <weave/export.h> |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 11 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 12 | #include "src/commands/command_queue.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 13 | #include "src/commands/schema_constants.h" |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 14 | #include "src/json_error_codes.h" |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 15 | #include "src/utils.h" |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 17 | namespace weave { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 19 | namespace { |
| 20 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 21 | const EnumToStringMap<Command::State>::Map kMapStatus[] = { |
| 22 | {Command::State::kQueued, "queued"}, |
| 23 | {Command::State::kInProgress, "inProgress"}, |
| 24 | {Command::State::kPaused, "paused"}, |
| 25 | {Command::State::kError, "error"}, |
| 26 | {Command::State::kDone, "done"}, |
| 27 | {Command::State::kCancelled, "cancelled"}, |
| 28 | {Command::State::kAborted, "aborted"}, |
| 29 | {Command::State::kExpired, "expired"}, |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 30 | }; |
| 31 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 32 | const EnumToStringMap<Command::Origin>::Map kMapOrigin[] = { |
| 33 | {Command::Origin::kLocal, "local"}, |
| 34 | {Command::Origin::kCloud, "cloud"}, |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 37 | bool ReportInvalidStateTransition(ErrorPtr* error, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 38 | Command::State from, |
| 39 | Command::State to) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 40 | return Error::AddToPrintf(error, FROM_HERE, errors::commands::kInvalidState, |
| 41 | "State switch impossible: '%s' -> '%s'", |
| 42 | EnumToString(from).c_str(), |
| 43 | EnumToString(to).c_str()); |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 44 | } |
| 45 | |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 46 | } // namespace |
| 47 | |
| 48 | template <> |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 49 | LIBWEAVE_EXPORT EnumToStringMap<Command::State>::EnumToStringMap() |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 50 | : EnumToStringMap(kMapStatus) {} |
| 51 | |
| 52 | template <> |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 53 | LIBWEAVE_EXPORT EnumToStringMap<Command::Origin>::EnumToStringMap() |
Vitaly Buka | 15f5909 | 2015-07-24 16:54:32 -0700 | [diff] [blame] | 54 | : EnumToStringMap(kMapOrigin) {} |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 55 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 56 | CommandInstance::CommandInstance(const std::string& name, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 57 | Command::Origin origin, |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 58 | const base::DictionaryValue& parameters) |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 59 | : name_{name}, origin_{origin} { |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 60 | parameters_.MergeDictionary(¶meters); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 61 | } |
| 62 | |
Vitaly Buka | c3d4e97 | 2015-07-21 09:55:25 -0700 | [diff] [blame] | 63 | CommandInstance::~CommandInstance() { |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 64 | FOR_EACH_OBSERVER(Observer, observers_, OnCommandDestroyed()); |
Vitaly Buka | c3d4e97 | 2015-07-21 09:55:25 -0700 | [diff] [blame] | 65 | } |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 66 | |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 67 | const std::string& CommandInstance::GetID() const { |
| 68 | return id_; |
| 69 | } |
| 70 | |
| 71 | const std::string& CommandInstance::GetName() const { |
| 72 | return name_; |
| 73 | } |
| 74 | |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 75 | const std::string& CommandInstance::GetComponent() const { |
| 76 | return component_; |
| 77 | } |
| 78 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 79 | Command::State CommandInstance::GetState() const { |
| 80 | return state_; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 83 | Command::Origin CommandInstance::GetOrigin() const { |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 84 | return origin_; |
| 85 | } |
| 86 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 87 | const base::DictionaryValue& CommandInstance::GetParameters() const { |
| 88 | return parameters_; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 89 | } |
| 90 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 91 | const base::DictionaryValue& CommandInstance::GetProgress() const { |
| 92 | return progress_; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Vitaly Buka | c430560 | 2015-11-24 23:33:09 -0800 | [diff] [blame] | 95 | const base::DictionaryValue& CommandInstance::GetResults() const { |
| 96 | return results_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 97 | } |
| 98 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 99 | const Error* CommandInstance::GetError() const { |
| 100 | return error_.get(); |
| 101 | } |
| 102 | |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 103 | bool CommandInstance::SetProgress(const base::DictionaryValue& progress, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 104 | ErrorPtr* error) { |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 105 | // Change status even if progress unchanged, e.g. 0% -> 0%. |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 106 | if (!SetStatus(State::kInProgress, error)) |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 107 | return false; |
| 108 | |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 109 | if (!progress_.Equals(&progress)) { |
| 110 | progress_.Clear(); |
| 111 | progress_.MergeDictionary(&progress); |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 112 | FOR_EACH_OBSERVER(Observer, observers_, OnProgressChanged()); |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 113 | } |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 114 | |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 115 | return true; |
| 116 | } |
| 117 | |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 118 | bool CommandInstance::Complete(const base::DictionaryValue& results, |
| 119 | ErrorPtr* error) { |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 120 | if (!results_.Equals(&results)) { |
| 121 | results_.Clear(); |
| 122 | results_.MergeDictionary(&results); |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 123 | FOR_EACH_OBSERVER(Observer, observers_, OnResultsChanged()); |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 124 | } |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 125 | // Change status even if result is unchanged. |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 126 | bool result = SetStatus(State::kDone, error); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 127 | RemoveFromQueue(); |
| 128 | // The command will be destroyed after that, so do not access any members. |
| 129 | return result; |
| 130 | } |
| 131 | |
| 132 | bool CommandInstance::SetError(const Error* command_error, ErrorPtr* error) { |
| 133 | error_ = command_error ? command_error->Clone() : nullptr; |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 134 | FOR_EACH_OBSERVER(Observer, observers_, OnErrorChanged()); |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 135 | return SetStatus(State::kError, error); |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 136 | } |
| 137 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 138 | namespace { |
| 139 | |
| 140 | // Helper method to retrieve command parameters from the command definition |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 141 | // object passed in as |json|. |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 142 | // On success, returns |true| and the validated parameters and values through |
| 143 | // |parameters|. Otherwise returns |false| and additional error information in |
| 144 | // |error|. |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 145 | std::unique_ptr<base::DictionaryValue> GetCommandParameters( |
| 146 | const base::DictionaryValue* json, |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 147 | ErrorPtr* error) { |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 148 | // Get the command parameters from 'parameters' property. |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 149 | std::unique_ptr<base::DictionaryValue> params; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 150 | const base::Value* params_value = nullptr; |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 151 | if (json->Get(commands::attributes::kCommand_Parameters, ¶ms_value)) { |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 152 | // Make sure the "parameters" property is actually an object. |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 153 | const base::DictionaryValue* params_dict = nullptr; |
| 154 | if (!params_value->GetAsDictionary(¶ms_dict)) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 155 | return Error::AddToPrintf(error, FROM_HERE, errors::json::kObjectExpected, |
| 156 | "Property '%s' must be a JSON object", |
| 157 | commands::attributes::kCommand_Parameters); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 158 | } |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 159 | params.reset(params_dict->DeepCopy()); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 160 | } else { |
| 161 | // "parameters" are not specified. Assume empty param list. |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 162 | params.reset(new base::DictionaryValue); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 163 | } |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 164 | return params; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | } // anonymous namespace |
| 168 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 169 | std::unique_ptr<CommandInstance> CommandInstance::FromJson( |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 170 | const base::Value* value, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 171 | Command::Origin origin, |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 172 | std::string* command_id, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 173 | ErrorPtr* error) { |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 174 | std::unique_ptr<CommandInstance> instance; |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 175 | std::string command_id_buffer; // used if |command_id| was nullptr. |
| 176 | if (!command_id) |
| 177 | command_id = &command_id_buffer; |
| 178 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 179 | // Get the command JSON object from the value. |
| 180 | const base::DictionaryValue* json = nullptr; |
| 181 | if (!value->GetAsDictionary(&json)) { |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 182 | command_id->clear(); |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 183 | return Error::AddTo(error, FROM_HERE, errors::json::kObjectExpected, |
| 184 | "Command instance is not a JSON object"); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 185 | } |
| 186 | |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 187 | // Get the command ID from 'id' property. |
| 188 | if (!json->GetString(commands::attributes::kCommand_Id, command_id)) |
| 189 | command_id->clear(); |
| 190 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 191 | // Get the command name from 'name' property. |
| 192 | std::string command_name; |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 193 | if (!json->GetString(commands::attributes::kCommand_Name, &command_name)) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 194 | return Error::AddTo(error, FROM_HERE, errors::commands::kPropertyMissing, |
| 195 | "Command name is missing"); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 196 | } |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 197 | |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 198 | auto parameters = GetCommandParameters(json, error); |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 199 | if (!parameters) { |
Vitaly Buka | 0dbbf60 | 2016-01-22 11:38:37 -0800 | [diff] [blame] | 200 | return Error::AddToPrintf( |
| 201 | error, FROM_HERE, errors::commands::kCommandFailed, |
| 202 | "Failed to validate command '%s'", command_name.c_str()); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Alex Vakulenko | 2c7740a | 2015-11-30 08:51:29 -0800 | [diff] [blame] | 205 | instance.reset(new CommandInstance{command_name, origin, *parameters}); |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 206 | |
| 207 | if (!command_id->empty()) |
| 208 | instance->SetID(*command_id); |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 209 | |
Alex Vakulenko | 88f55d8 | 2015-12-03 15:30:27 -0800 | [diff] [blame] | 210 | // Get the component name this command is for. |
| 211 | std::string component; |
| 212 | if (json->GetString(commands::attributes::kCommand_Component, &component)) |
| 213 | instance->SetComponent(component); |
| 214 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 215 | return instance; |
| 216 | } |
| 217 | |
Vitaly Buka | 906d39e | 2015-03-24 10:08:26 -0700 | [diff] [blame] | 218 | std::unique_ptr<base::DictionaryValue> CommandInstance::ToJson() const { |
| 219 | std::unique_ptr<base::DictionaryValue> json{new base::DictionaryValue}; |
| 220 | |
| 221 | json->SetString(commands::attributes::kCommand_Id, id_); |
| 222 | json->SetString(commands::attributes::kCommand_Name, name_); |
Vitaly Buka | 5ddd991 | 2016-03-09 10:44:48 -0800 | [diff] [blame] | 223 | json->SetString(commands::attributes::kCommand_Component, component_); |
Alex Vakulenko | 36bf1b5 | 2015-11-23 09:35:37 -0800 | [diff] [blame] | 224 | json->Set(commands::attributes::kCommand_Parameters, parameters_.DeepCopy()); |
| 225 | json->Set(commands::attributes::kCommand_Progress, progress_.DeepCopy()); |
| 226 | json->Set(commands::attributes::kCommand_Results, results_.DeepCopy()); |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 227 | json->SetString(commands::attributes::kCommand_State, EnumToString(state_)); |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 228 | if (error_) { |
| 229 | json->Set(commands::attributes::kCommand_Error, |
| 230 | ErrorInfoToJson(*error_).release()); |
| 231 | } |
Vitaly Buka | 906d39e | 2015-03-24 10:08:26 -0700 | [diff] [blame] | 232 | |
| 233 | return json; |
| 234 | } |
| 235 | |
Vitaly Buka | 67b5355 | 2015-07-21 10:36:56 -0700 | [diff] [blame] | 236 | void CommandInstance::AddObserver(Observer* observer) { |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 237 | observers_.AddObserver(observer); |
| 238 | } |
| 239 | |
| 240 | void CommandInstance::RemoveObserver(Observer* observer) { |
| 241 | observers_.RemoveObserver(observer); |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 242 | } |
| 243 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 244 | bool CommandInstance::Pause(ErrorPtr* error) { |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 245 | return SetStatus(State::kPaused, error); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 246 | } |
| 247 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 248 | bool CommandInstance::Abort(const Error* command_error, ErrorPtr* error) { |
| 249 | error_ = command_error ? command_error->Clone() : nullptr; |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 250 | FOR_EACH_OBSERVER(Observer, observers_, OnErrorChanged()); |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 251 | bool result = SetStatus(State::kAborted, error); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 252 | RemoveFromQueue(); |
| 253 | // The command will be destroyed after that, so do not access any members. |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 254 | return result; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 255 | } |
| 256 | |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 257 | bool CommandInstance::Cancel(ErrorPtr* error) { |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 258 | bool result = SetStatus(State::kCancelled, error); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 259 | RemoveFromQueue(); |
| 260 | // The command will be destroyed after that, so do not access any members. |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 261 | return result; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 262 | } |
| 263 | |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 264 | bool CommandInstance::SetStatus(Command::State status, ErrorPtr* error) { |
| 265 | if (status == state_) |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 266 | return true; |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 267 | if (status == State::kQueued) |
| 268 | return ReportInvalidStateTransition(error, state_, status); |
| 269 | switch (state_) { |
| 270 | case State::kDone: |
| 271 | case State::kCancelled: |
| 272 | case State::kAborted: |
| 273 | case State::kExpired: |
| 274 | return ReportInvalidStateTransition(error, state_, status); |
| 275 | case State::kQueued: |
| 276 | case State::kInProgress: |
| 277 | case State::kPaused: |
| 278 | case State::kError: |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 279 | break; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 280 | } |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 281 | state_ = status; |
| 282 | FOR_EACH_OBSERVER(Observer, observers_, OnStateChanged()); |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 283 | return true; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | void CommandInstance::RemoveFromQueue() { |
Vitaly Buka | 2a9b30f | 2015-04-01 10:51:59 -0700 | [diff] [blame] | 287 | if (queue_) |
Alex Vakulenko | 329ad80 | 2016-02-01 12:11:30 -0800 | [diff] [blame] | 288 | queue_->RemoveLater(GetID()); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 289 | } |
| 290 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 291 | } // namespace weave |