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 | #ifndef BUFFET_COMMANDS_COMMAND_INSTANCE_H_ |
| 6 | #define BUFFET_COMMANDS_COMMAND_INSTANCE_H_ |
| 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> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 14 | #include <chromeos/errors/error.h> |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 15 | |
| 16 | #include "buffet/commands/prop_values.h" |
| 17 | #include "buffet/commands/schema_utils.h" |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 18 | |
| 19 | namespace base { |
| 20 | class Value; |
| 21 | } // namespace base |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 22 | |
| 23 | namespace buffet { |
| 24 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 25 | class CommandDefinition; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 26 | class CommandDictionary; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 27 | class CommandProxyInterface; |
| 28 | class CommandQueue; |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 29 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 30 | class CommandInstance final { |
| 31 | public: |
| 32 | // Construct a command instance given the full command |name| which must |
| 33 | // be in format "<package_name>.<command_name>", a command |category| and |
| 34 | // a list of parameters and their values specified in |parameters|. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 35 | CommandInstance(const std::string& name, |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 36 | const std::string& origin, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 37 | const CommandDefinition* command_definition, |
| 38 | const native_types::Object& parameters); |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 39 | ~CommandInstance(); |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 40 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 41 | // Returns the full command ID. |
| 42 | const std::string& GetID() const { return id_; } |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 43 | // Returns the full name of the command. |
| 44 | const std::string& GetName() const { return name_; } |
| 45 | // Returns the command category. |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 46 | const std::string& GetCategory() const; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 47 | // Returns the command parameters and their values. |
| 48 | const native_types::Object& GetParameters() const { return parameters_; } |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 49 | // Returns the command results and their values. |
| 50 | const native_types::Object& GetResults() const { return results_; } |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 51 | // Finds a command parameter value by parameter |name|. If the parameter |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 52 | // with given name does not exist, returns nullptr. |
| 53 | const PropValue* FindParameter(const std::string& name) const; |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 54 | // Returns the full name of the command. |
| 55 | const std::string& GetOrigin() const { return origin_; } |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 56 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 57 | // Returns command definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 58 | const CommandDefinition* GetCommandDefinition() const { |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 59 | return command_definition_; |
| 60 | } |
| 61 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 62 | // Parses a command instance JSON definition and constructs a CommandInstance |
| 63 | // object, checking the JSON |value| against the command definition schema |
| 64 | // found in command |dictionary|. On error, returns null unique_ptr and |
| 65 | // fills in error details in |error|. |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 66 | // |command_id| is the ID of the command returned, as parsed from the |value|. |
| 67 | // The command ID extracted (if present in the JSON object) even if other |
| 68 | // parsing/validation error occurs and command instance is not constructed. |
| 69 | // This is used to report parse failures back to the server. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 70 | static std::unique_ptr<CommandInstance> FromJson( |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 71 | const base::Value* value, |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 72 | const std::string& origin, |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 73 | const CommandDictionary& dictionary, |
Alex Vakulenko | d1978d3 | 2015-04-29 17:33:26 -0700 | [diff] [blame] | 74 | std::string* command_id, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 75 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 76 | |
Vitaly Buka | 906d39e | 2015-03-24 10:08:26 -0700 | [diff] [blame] | 77 | // Returns JSON representation of the command. |
| 78 | std::unique_ptr<base::DictionaryValue> ToJson() const; |
| 79 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 80 | // Sets the command ID (normally done by CommandQueue when the command |
| 81 | // instance is added to it). |
| 82 | void SetID(const std::string& id) { id_ = id; } |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 83 | // Adds a proxy for this command. |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 84 | // The proxy object is not owned by this class. |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 85 | void AddProxy(std::unique_ptr<CommandProxyInterface> proxy); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 86 | // Sets the pointer to queue this command is part of. |
| 87 | void SetCommandQueue(CommandQueue* queue) { queue_ = queue; } |
| 88 | |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 89 | // Updates the command progress. The |progress| should match the schema. |
| 90 | // Returns false if |results| value is incorrect. |
| 91 | bool SetProgress(const native_types::Object& progress); |
| 92 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 93 | // Updates the command results. The |results| should match the schema. |
| 94 | // Returns false if |results| value is incorrect. |
| 95 | bool SetResults(const native_types::Object& results); |
| 96 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 97 | // Aborts command execution. |
| 98 | void Abort(); |
| 99 | // Cancels command execution. |
| 100 | void Cancel(); |
| 101 | // Marks the command as completed successfully. |
| 102 | void Done(); |
| 103 | |
| 104 | // Command state getters. |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 105 | const native_types::Object& GetProgress() const { return progress_; } |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 106 | const std::string& GetStatus() const { return status_; } |
| 107 | |
| 108 | // Values for command execution status. |
| 109 | static const char kStatusQueued[]; |
| 110 | static const char kStatusInProgress[]; |
| 111 | static const char kStatusPaused[]; |
| 112 | static const char kStatusError[]; |
| 113 | static const char kStatusDone[]; |
Alex Vakulenko | db22124 | 2015-03-13 14:02:46 -0700 | [diff] [blame] | 114 | static const char kStatusCancelled[]; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 115 | static const char kStatusAborted[]; |
| 116 | static const char kStatusExpired[]; |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 117 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 118 | private: |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 119 | // Helper function to update the command status. |
| 120 | // Used by Abort(), Cancel(), Done() methods. |
| 121 | void SetStatus(const std::string& status); |
| 122 | // Helper method that removes this command from the command queue. |
| 123 | // Note that since the command queue owns the lifetime of the command instance |
| 124 | // object, removing a command from the queue will also destroy it. |
| 125 | void RemoveFromQueue(); |
| 126 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 127 | // Unique command ID within a command queue. |
| 128 | std::string id_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 129 | // Full command name as "<package_name>.<command_name>". |
| 130 | std::string name_; |
Alex Vakulenko | f784e21 | 2015-04-20 12:33:52 -0700 | [diff] [blame] | 131 | // The origin of the command, either "local" or "cloud". |
| 132 | std::string origin_; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 133 | // Command definition. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 134 | const CommandDefinition* command_definition_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 135 | // Command parameters and their values. |
| 136 | native_types::Object parameters_; |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 137 | // Current command execution progress. |
| 138 | native_types::Object progress_; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 139 | // Command results. |
| 140 | native_types::Object results_; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 141 | // Current command status. |
| 142 | std::string status_ = kStatusQueued; |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 143 | // Command proxies for the command. |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 144 | std::vector<std::unique_ptr<CommandProxyInterface>> proxies_; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 145 | // Pointer to the command queue this command instance is added to. |
| 146 | // The queue owns the command instance, so it outlives this object. |
| 147 | CommandQueue* queue_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 148 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 149 | friend class DBusCommandDispacherTest; |
| 150 | friend class DBusCommandProxyTest; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 151 | DISALLOW_COPY_AND_ASSIGN(CommandInstance); |
| 152 | }; |
| 153 | |
| 154 | } // namespace buffet |
| 155 | |
| 156 | #endif // BUFFET_COMMANDS_COMMAND_INSTANCE_H_ |