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|. |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 35 | CommandInstance( |
| 36 | const std::string& name, |
| 37 | const std::shared_ptr<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 |
| 52 | // with given name does not exist, returns null shared_ptr. |
| 53 | std::shared_ptr<const PropValue> FindParameter(const std::string& name) const; |
| 54 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 55 | // Returns command definition. |
| 56 | std::shared_ptr<const CommandDefinition> GetCommandDefinition() const { |
| 57 | return command_definition_; |
| 58 | } |
| 59 | |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 60 | // Parses a command instance JSON definition and constructs a CommandInstance |
| 61 | // object, checking the JSON |value| against the command definition schema |
| 62 | // found in command |dictionary|. On error, returns null unique_ptr and |
| 63 | // fills in error details in |error|. |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 64 | static std::unique_ptr<CommandInstance> FromJson( |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 65 | const base::Value* value, |
| 66 | const CommandDictionary& dictionary, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 67 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 8dc69af | 2014-08-07 10:29:42 -0700 | [diff] [blame] | 68 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 69 | // Sets the command ID (normally done by CommandQueue when the command |
| 70 | // instance is added to it). |
| 71 | void SetID(const std::string& id) { id_ = id; } |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 72 | // Adds a proxy for this command. |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 73 | // The proxy object is not owned by this class. |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 74 | void AddProxy(std::unique_ptr<CommandProxyInterface> proxy); |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 75 | // Sets the pointer to queue this command is part of. |
| 76 | void SetCommandQueue(CommandQueue* queue) { queue_ = queue; } |
| 77 | |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 78 | // Updates the command results. The |results| should match the schema. |
| 79 | // Returns false if |results| value is incorrect. |
| 80 | bool SetResults(const native_types::Object& results); |
| 81 | |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 82 | // Updates the command execution progress. The |progress| must be between |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 83 | // 0 and 100. Returns false if |progress| value is incorrect. |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 84 | bool SetProgress(int progress); |
| 85 | // Aborts command execution. |
| 86 | void Abort(); |
| 87 | // Cancels command execution. |
| 88 | void Cancel(); |
| 89 | // Marks the command as completed successfully. |
| 90 | void Done(); |
| 91 | |
| 92 | // Command state getters. |
| 93 | int GetProgress() const { return progress_; } |
| 94 | const std::string& GetStatus() const { return status_; } |
| 95 | |
| 96 | // Values for command execution status. |
| 97 | static const char kStatusQueued[]; |
| 98 | static const char kStatusInProgress[]; |
| 99 | static const char kStatusPaused[]; |
| 100 | static const char kStatusError[]; |
| 101 | static const char kStatusDone[]; |
Alex Vakulenko | db22124 | 2015-03-13 14:02:46 -0700 | [diff] [blame^] | 102 | static const char kStatusCancelled[]; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 103 | static const char kStatusAborted[]; |
| 104 | static const char kStatusExpired[]; |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 105 | |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 106 | private: |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 107 | // Helper function to update the command status. |
| 108 | // Used by Abort(), Cancel(), Done() methods. |
| 109 | void SetStatus(const std::string& status); |
| 110 | // Helper method that removes this command from the command queue. |
| 111 | // Note that since the command queue owns the lifetime of the command instance |
| 112 | // object, removing a command from the queue will also destroy it. |
| 113 | void RemoveFromQueue(); |
| 114 | |
Alex Vakulenko | fedc487 | 2014-08-20 12:38:43 -0700 | [diff] [blame] | 115 | // Unique command ID within a command queue. |
| 116 | std::string id_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 117 | // Full command name as "<package_name>.<command_name>". |
| 118 | std::string name_; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 119 | // Command definition. |
| 120 | std::shared_ptr<const CommandDefinition> command_definition_; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 121 | // Command parameters and their values. |
| 122 | native_types::Object parameters_; |
Anton Muhin | cfde869 | 2014-11-25 03:36:59 +0400 | [diff] [blame] | 123 | // Command results. |
| 124 | native_types::Object results_; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 125 | // Current command status. |
| 126 | std::string status_ = kStatusQueued; |
| 127 | // Current command execution progress. |
| 128 | int progress_ = 0; |
Anton Muhin | 4dc3785 | 2014-10-30 22:17:25 +0400 | [diff] [blame] | 129 | // Command proxies for the command. |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 130 | std::vector<std::unique_ptr<CommandProxyInterface>> proxies_; |
Alex Vakulenko | f6b3871 | 2014-09-03 16:23:38 -0700 | [diff] [blame] | 131 | // Pointer to the command queue this command instance is added to. |
| 132 | // The queue owns the command instance, so it outlives this object. |
| 133 | CommandQueue* queue_ = nullptr; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 134 | |
Anton Muhin | b66a930 | 2014-11-10 22:15:22 +0400 | [diff] [blame] | 135 | friend class DBusCommandDispacherTest; |
| 136 | friend class DBusCommandProxyTest; |
Alex Vakulenko | aa3a559 | 2014-08-07 07:24:06 -0700 | [diff] [blame] | 137 | DISALLOW_COPY_AND_ASSIGN(CommandInstance); |
| 138 | }; |
| 139 | |
| 140 | } // namespace buffet |
| 141 | |
| 142 | #endif // BUFFET_COMMANDS_COMMAND_INSTANCE_H_ |