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