blob: 6ce7036765d48ab34af0fc3a2d42ce4c61c0f9b3 [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>
Anton Muhin4dc37852014-10-30 22:17:25 +040011#include <vector>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070012
Alex Vakulenko132617a2014-09-04 08:59:43 -070013#include <base/macros.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070014#include <chromeos/errors/error.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070015
16#include "buffet/commands/prop_values.h"
17#include "buffet/commands/schema_utils.h"
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070018
19namespace base {
20class Value;
21} // namespace base
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070022
23namespace buffet {
24
Anton Muhincfde8692014-11-25 03:36:59 +040025class CommandDefinition;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070026class CommandDictionary;
Alex Vakulenkof6b38712014-09-03 16:23:38 -070027class CommandProxyInterface;
28class CommandQueue;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070029
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070030class 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|.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070035 CommandInstance(const std::string& name,
Alex Vakulenkof784e212015-04-20 12:33:52 -070036 const std::string& origin,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070037 const CommandDefinition* command_definition,
38 const native_types::Object& parameters);
Anton Muhinb66a9302014-11-10 22:15:22 +040039 ~CommandInstance();
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070040
Alex Vakulenkofedc4872014-08-20 12:38:43 -070041 // Returns the full command ID.
42 const std::string& GetID() const { return id_; }
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070043 // Returns the full name of the command.
44 const std::string& GetName() const { return name_; }
45 // Returns the command category.
Anton Muhincfde8692014-11-25 03:36:59 +040046 const std::string& GetCategory() const;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070047 // Returns the command parameters and their values.
48 const native_types::Object& GetParameters() const { return parameters_; }
Anton Muhincfde8692014-11-25 03:36:59 +040049 // Returns the command results and their values.
50 const native_types::Object& GetResults() const { return results_; }
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070051 // Finds a command parameter value by parameter |name|. If the parameter
Alex Vakulenko5ef75792015-03-19 15:50:44 -070052 // with given name does not exist, returns nullptr.
53 const PropValue* FindParameter(const std::string& name) const;
Alex Vakulenkof784e212015-04-20 12:33:52 -070054 // Returns the full name of the command.
55 const std::string& GetOrigin() const { return origin_; }
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070056
Anton Muhincfde8692014-11-25 03:36:59 +040057 // Returns command definition.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070058 const CommandDefinition* GetCommandDefinition() const {
Anton Muhincfde8692014-11-25 03:36:59 +040059 return command_definition_;
60 }
61
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070062 // Parses a command instance JSON definition and constructs a CommandInstance
63 // object, checking the JSON |value| against the command definition schema
64 // found in command |dictionary|. On error, returns null unique_ptr and
65 // fills in error details in |error|.
Alex Vakulenkod1978d32015-04-29 17:33:26 -070066 // |command_id| is the ID of the command returned, as parsed from the |value|.
67 // The command ID extracted (if present in the JSON object) even if other
68 // parsing/validation error occurs and command instance is not constructed.
69 // This is used to report parse failures back to the server.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070070 static std::unique_ptr<CommandInstance> FromJson(
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070071 const base::Value* value,
Alex Vakulenkof784e212015-04-20 12:33:52 -070072 const std::string& origin,
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070073 const CommandDictionary& dictionary,
Alex Vakulenkod1978d32015-04-29 17:33:26 -070074 std::string* command_id,
Alex Vakulenko5f472062014-08-14 17:54:04 -070075 chromeos::ErrorPtr* error);
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070076
Vitaly Buka906d39e2015-03-24 10:08:26 -070077 // Returns JSON representation of the command.
78 std::unique_ptr<base::DictionaryValue> ToJson() const;
79
Alex Vakulenkofedc4872014-08-20 12:38:43 -070080 // Sets the command ID (normally done by CommandQueue when the command
81 // instance is added to it).
82 void SetID(const std::string& id) { id_ = id; }
Anton Muhin4dc37852014-10-30 22:17:25 +040083 // Adds a proxy for this command.
Alex Vakulenkof6b38712014-09-03 16:23:38 -070084 // The proxy object is not owned by this class.
Anton Muhinb66a9302014-11-10 22:15:22 +040085 void AddProxy(std::unique_ptr<CommandProxyInterface> proxy);
Alex Vakulenkof6b38712014-09-03 16:23:38 -070086 // Sets the pointer to queue this command is part of.
87 void SetCommandQueue(CommandQueue* queue) { queue_ = queue; }
88
Vitaly Buka4129dfa2015-04-29 12:16:58 -070089 // Updates the command progress. The |progress| should match the schema.
90 // Returns false if |results| value is incorrect.
91 bool SetProgress(const native_types::Object& progress);
92
Anton Muhincfde8692014-11-25 03:36:59 +040093 // Updates the command results. The |results| should match the schema.
94 // Returns false if |results| value is incorrect.
95 bool SetResults(const native_types::Object& results);
96
Alex Vakulenkof6b38712014-09-03 16:23:38 -070097 // Aborts command execution.
98 void Abort();
99 // Cancels command execution.
100 void Cancel();
101 // Marks the command as completed successfully.
102 void Done();
103
104 // Command state getters.
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700105 const native_types::Object& GetProgress() const { return progress_; }
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700106 const std::string& GetStatus() const { return status_; }
107
108 // Values for command execution status.
109 static const char kStatusQueued[];
110 static const char kStatusInProgress[];
111 static const char kStatusPaused[];
112 static const char kStatusError[];
113 static const char kStatusDone[];
Alex Vakulenkodb221242015-03-13 14:02:46 -0700114 static const char kStatusCancelled[];
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700115 static const char kStatusAborted[];
116 static const char kStatusExpired[];
Alex Vakulenkofedc4872014-08-20 12:38:43 -0700117
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700118 private:
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700119 // Helper function to update the command status.
120 // Used by Abort(), Cancel(), Done() methods.
121 void SetStatus(const std::string& status);
122 // Helper method that removes this command from the command queue.
123 // Note that since the command queue owns the lifetime of the command instance
124 // object, removing a command from the queue will also destroy it.
125 void RemoveFromQueue();
126
Alex Vakulenkofedc4872014-08-20 12:38:43 -0700127 // Unique command ID within a command queue.
128 std::string id_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700129 // Full command name as "<package_name>.<command_name>".
130 std::string name_;
Alex Vakulenkof784e212015-04-20 12:33:52 -0700131 // The origin of the command, either "local" or "cloud".
132 std::string origin_;
Anton Muhincfde8692014-11-25 03:36:59 +0400133 // Command definition.
Alex Vakulenko5ef75792015-03-19 15:50:44 -0700134 const CommandDefinition* command_definition_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700135 // Command parameters and their values.
136 native_types::Object parameters_;
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700137 // Current command execution progress.
138 native_types::Object progress_;
Anton Muhincfde8692014-11-25 03:36:59 +0400139 // Command results.
140 native_types::Object results_;
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700141 // Current command status.
142 std::string status_ = kStatusQueued;
Anton Muhin4dc37852014-10-30 22:17:25 +0400143 // Command proxies for the command.
Anton Muhinb66a9302014-11-10 22:15:22 +0400144 std::vector<std::unique_ptr<CommandProxyInterface>> proxies_;
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700145 // Pointer to the command queue this command instance is added to.
146 // The queue owns the command instance, so it outlives this object.
147 CommandQueue* queue_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700148
Anton Muhinb66a9302014-11-10 22:15:22 +0400149 friend class DBusCommandDispacherTest;
150 friend class DBusCommandProxyTest;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700151 DISALLOW_COPY_AND_ASSIGN(CommandInstance);
152};
153
154} // namespace buffet
155
156#endif // BUFFET_COMMANDS_COMMAND_INSTANCE_H_