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