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