blob: f8bde2a9f0dd79805a1f8ce8dacfec6d8ad8a373 [file] [log] [blame]
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07001// 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 Vakulenko132617a2014-09-04 08:59:43 -070012#include <base/macros.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070013#include <chromeos/errors/error.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070014
15#include "buffet/commands/prop_values.h"
16#include "buffet/commands/schema_utils.h"
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070017
18namespace base {
19class Value;
20} // namespace base
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070021
22namespace buffet {
23
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070024class CommandDictionary;
Alex Vakulenkof6b38712014-09-03 16:23:38 -070025class CommandProxyInterface;
26class CommandQueue;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070027
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070028class 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 Vakulenko8dc69af2014-08-07 10:29:42 -070033 CommandInstance(const std::string& name,
34 const std::string& category,
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070035 const native_types::Object& parameters);
36
Alex Vakulenkofedc4872014-08-20 12:38:43 -070037 // Returns the full command ID.
38 const std::string& GetID() const { return id_; }
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070039 // 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 Vakulenko8dc69af2014-08-07 10:29:42 -070049 // 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 Vakulenkofedc4872014-08-20 12:38:43 -070053 static std::unique_ptr<CommandInstance> FromJson(
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070054 const base::Value* value,
55 const CommandDictionary& dictionary,
Alex Vakulenko5f472062014-08-14 17:54:04 -070056 chromeos::ErrorPtr* error);
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070057
Alex Vakulenkofedc4872014-08-20 12:38:43 -070058 // 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 Vakulenkof6b38712014-09-03 16:23:38 -070061 // 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 Vakulenkofedc4872014-08-20 12:38:43 -070090
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070091 private:
Alex Vakulenkof6b38712014-09-03 16:23:38 -070092 // 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 Vakulenkofedc4872014-08-20 12:38:43 -0700100 // Unique command ID within a command queue.
101 std::string id_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700102 // 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 Vakulenkof6b38712014-09-03 16:23:38 -0700110 // 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 Vakulenkoaa3a5592014-08-07 07:24:06 -0700121
122 DISALLOW_COPY_AND_ASSIGN(CommandInstance);
123};
124
125} // namespace buffet
126
127#endif // BUFFET_COMMANDS_COMMAND_INSTANCE_H_