buffet: Move command state into CommandInstance

The command state is now stored in CommandInstance and not in
DBusCommandProxy. CommandInstance can now notify the proxy of
command state changes via CommandProxyInterface.

Moved command status strings from dbus_constants.h into the
CommandInstance class, as members.

Added a property on DBusCommandProxy to expose the command
parameters to command handlers, so they can get the parameter
values over D-Bus.

BUG=chromium:374864
TEST=FEATURES=test emerge-link buffet

Change-Id: Ief3397ef09644772ffc3b1b01ed972a8b6779df4
Reviewed-on: https://chromium-review.googlesource.com/216296
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Christopher Wiley <wiley@chromium.org>
Commit-Queue: Alex Vakulenko <avakulenko@chromium.org>
diff --git a/buffet/commands/command_instance.h b/buffet/commands/command_instance.h
index 6e482ad..4b71149 100644
--- a/buffet/commands/command_instance.h
+++ b/buffet/commands/command_instance.h
@@ -22,6 +22,8 @@
 namespace buffet {
 
 class CommandDictionary;
+class CommandProxyInterface;
+class CommandQueue;
 
 class CommandInstance final {
  public:
@@ -56,8 +58,45 @@
   // Sets the command ID (normally done by CommandQueue when the command
   // instance is added to it).
   void SetID(const std::string& id) { id_ = id; }
+  // Sets the command proxy for the dispatch technology used.
+  // The proxy object is not owned by this class.
+  void SetProxy(CommandProxyInterface* proxy) { proxy_ = proxy; }
+  // Sets the pointer to queue this command is part of.
+  void SetCommandQueue(CommandQueue* queue) { queue_ = queue; }
+
+  // Updates the command execution progress. The |progress| must be between
+  // 0 and 100. Returns false if the progress value is incorrect.
+  bool SetProgress(int progress);
+  // Aborts command execution.
+  void Abort();
+  // Cancels command execution.
+  void Cancel();
+  // Marks the command as completed successfully.
+  void Done();
+
+  // Command state getters.
+  int GetProgress() const { return progress_; }
+  const std::string& GetStatus() const { return status_; }
+
+  // Values for command execution status.
+  static const char kStatusQueued[];
+  static const char kStatusInProgress[];
+  static const char kStatusPaused[];
+  static const char kStatusError[];
+  static const char kStatusDone[];
+  static const char kStatusCanceled[];
+  static const char kStatusAborted[];
+  static const char kStatusExpired[];
 
  private:
+  // Helper function to update the command status.
+  // Used by Abort(), Cancel(), Done() methods.
+  void SetStatus(const std::string& status);
+  // Helper method that removes this command from the command queue.
+  // Note that since the command queue owns the lifetime of the command instance
+  // object, removing a command from the queue will also destroy it.
+  void RemoveFromQueue();
+
   // Unique command ID within a command queue.
   std::string id_;
   // Full command name as "<package_name>.<command_name>".
@@ -68,6 +107,17 @@
   std::string category_;
   // Command parameters and their values.
   native_types::Object parameters_;
+  // Current command status.
+  std::string status_ = kStatusQueued;
+  // Current command execution progress.
+  int progress_ = 0;
+  // Command proxy class for the current dispatch technology (e.g. D-Bus).
+  // This is a weak pointer. The proxy's lifetime is managed by the command
+  // dispatcher.
+  CommandProxyInterface* proxy_ = nullptr;
+  // Pointer to the command queue this command instance is added to.
+  // The queue owns the command instance, so it outlives this object.
+  CommandQueue* queue_ = nullptr;
 
   DISALLOW_COPY_AND_ASSIGN(CommandInstance);
 };