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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
| 10 | #include <string> |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 11 | #include <vector> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 12 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 14 | #include <base/observer_list.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 15 | #include <weave/error.h> |
Vitaly Buka | 7b382ac | 2015-08-03 13:50:01 -0700 | [diff] [blame] | 16 | #include <weave/command.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 17 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 18 | #include "src/commands/prop_values.h" |
| 19 | #include "src/commands/schema_utils.h" |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 20 | |
| 21 | namespace base { |
| 22 | class Value; |
| 23 | } // namespace base |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 24 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 25 | namespace weave { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 26 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 27 | class CommandDefinition; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 28 | class CommandDictionary; |
Vitaly Buka | 0d377a4 | 2015-07-21 10:26:08 -0700 | [diff] [blame] | 29 | class CommandObserver; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 30 | class CommandQueue; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 31 | |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 32 | class CommandInstance final : public Command { |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 33 | public: |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 34 | class Observer { |
| 35 | public: |
Vitaly Buka | 375f328 | 2015-10-07 18:34:15 -0700 | [diff] [blame] | 36 | virtual void OnCommandDestroyed() = 0; |
| 37 | virtual void OnErrorChanged() = 0; |
| 38 | virtual void OnProgressChanged() = 0; |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 39 | virtual void OnResultsChanged() = 0; |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 40 | virtual void OnStateChanged() = 0; |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 41 | |
| 42 | protected: |
| 43 | virtual ~Observer() = default; |
| 44 | }; |
| 45 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 46 | // Construct a command instance given the full command |name| which must |
| 47 | // be in format "<package_name>.<command_name>", a command |category| and |
| 48 | // a list of parameters and their values specified in |parameters|. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 49 | CommandInstance(const std::string& name, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 50 | Command::Origin origin, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 51 | const CommandDefinition* command_definition, |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 52 | const ValueMap& parameters); |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 53 | ~CommandInstance() override; |
| 54 | |
| 55 | // Command overrides. |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 56 | const std::string& GetID() const override; |
| 57 | const std::string& GetName() const override; |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 58 | Command::State GetState() const override; |
| 59 | Command::Origin GetOrigin() const override; |
Vitaly Buka | 8d8d219 | 2015-07-21 22:25:09 -0700 | [diff] [blame] | 60 | std::unique_ptr<base::DictionaryValue> GetParameters() const override; |
| 61 | std::unique_ptr<base::DictionaryValue> GetProgress() const override; |
| 62 | std::unique_ptr<base::DictionaryValue> GetResults() const override; |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 63 | const Error* GetError() const override; |
Vitaly Buka | 4f4e228 | 2015-07-23 17:50:07 -0700 | [diff] [blame] | 64 | bool SetProgress(const base::DictionaryValue& progress, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 65 | ErrorPtr* error) override; |
Vitaly Buka | 2f54897 | 2015-10-08 19:34:49 -0700 | [diff] [blame] | 66 | bool Complete(const base::DictionaryValue& results, ErrorPtr* error) override; |
Vitaly Buka | 47a1f6f | 2015-10-07 18:09:57 -0700 | [diff] [blame] | 67 | bool Pause(ErrorPtr* error) override; |
| 68 | bool SetError(const Error* command_error, ErrorPtr* error) override; |
| 69 | bool Abort(const Error* command_error, ErrorPtr* error) override; |
| 70 | bool Cancel(ErrorPtr* error) override; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 71 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 72 | // Returns command definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 73 | const CommandDefinition* GetCommandDefinition() const { |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 74 | return command_definition_; |
| 75 | } |
| 76 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 77 | // Parses a command instance JSON definition and constructs a CommandInstance |
| 78 | // object, checking the JSON |value| against the command definition schema |
| 79 | // found in command |dictionary|. On error, returns null unique_ptr and |
| 80 | // fills in error details in |error|. |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 81 | // |command_id| is the ID of the command returned, as parsed from the |value|. |
| 82 | // The command ID extracted (if present in the JSON object) even if other |
| 83 | // parsing/validation error occurs and command instance is not constructed. |
| 84 | // This is used to report parse failures back to the server. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 85 | static std::unique_ptr<CommandInstance> FromJson( |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 86 | const base::Value* value, |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 87 | Command::Origin origin, |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 88 | const CommandDictionary& dictionary, |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 89 | std::string* command_id, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 90 | ErrorPtr* error); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 91 | |
Vitaly Buka | ef213d7 | 2015-10-07 15:54:58 -0700 | [diff] [blame] | 92 | std::unique_ptr<base::DictionaryValue> ToJson() const; |
| 93 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 94 | // Sets the command ID (normally done by CommandQueue when the command |
| 95 | // instance is added to it). |
| 96 | void SetID(const std::string& id) { id_ = id; } |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 97 | |
| 98 | void AddObserver(Observer* observer); |
| 99 | void RemoveObserver(Observer* observer); |
| 100 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 101 | // Sets the pointer to queue this command is part of. |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 102 | void AttachToQueue(CommandQueue* queue) { queue_ = queue; } |
| 103 | void DetachFromQueue() { |
| 104 | observers_.Clear(); |
| 105 | queue_ = nullptr; |
| 106 | command_definition_ = nullptr; |
| 107 | } |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 108 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 109 | private: |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 110 | // Helper function to update the command status. |
| 111 | // Used by Abort(), Cancel(), Done() methods. |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 112 | bool SetStatus(Command::State status, ErrorPtr* error); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 113 | // Helper method that removes this command from the command queue. |
| 114 | // Note that since the command queue owns the lifetime of the command instance |
| 115 | // object, removing a command from the queue will also destroy it. |
| 116 | void RemoveFromQueue(); |
| 117 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 118 | // Unique command ID within a command queue. |
| 119 | std::string id_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 120 | // Full command name as "<package_name>.<command_name>". |
| 121 | std::string name_; |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 122 | // The origin of the command, either "local" or "cloud". |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 123 | Command::Origin origin_ = Command::Origin::kLocal; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 124 | // Command definition. |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 125 | const CommandDefinition* command_definition_{nullptr}; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 126 | // Command parameters and their values. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 127 | ValueMap parameters_; |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 128 | // Current command execution progress. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 129 | ValueMap progress_; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 130 | // Command results. |
Vitaly Buka | 774cdf5 | 2015-07-21 13:55:00 -0700 | [diff] [blame] | 131 | ValueMap results_; |
Vitaly Buka | 0209da4 | 2015-10-08 00:07:18 -0700 | [diff] [blame] | 132 | // Current command state. |
| 133 | Command::State state_ = Command::State::kQueued; |
Vitaly Buka | 70f77d9 | 2015-10-07 15:42:40 -0700 | [diff] [blame] | 134 | // Error encountered during execution of the command. |
| 135 | ErrorPtr error_; |
Vitaly Buka | 157b16a | 2015-07-31 16:20:48 -0700 | [diff] [blame] | 136 | // Command observers. |
| 137 | base::ObserverList<Observer> observers_; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 138 | // Pointer to the command queue this command instance is added to. |
| 139 | // The queue owns the command instance, so it outlives this object. |
| 140 | CommandQueue* queue_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 141 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 142 | DISALLOW_COPY_AND_ASSIGN(CommandInstance); |
| 143 | }; |
| 144 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 145 | } // namespace weave |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 146 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 147 | #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ |