blob: 5a2ebf705ca8a5b27a89a95b9f982c03ac4a720c [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_
6#define LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -07007
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>
Vitaly Buka157b16a2015-07-31 16:20:48 -070014#include <base/observer_list.h>
Vitaly Buka0801a1f2015-08-14 10:03:46 -070015#include <weave/error.h>
Vitaly Buka7b382ac2015-08-03 13:50:01 -070016#include <weave/command.h>
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070017
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020018#include "src/commands/prop_values.h"
19#include "src/commands/schema_utils.h"
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070020
21namespace base {
22class Value;
23} // namespace base
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070024
Vitaly Bukab6f015a2015-07-09 14:59:23 -070025namespace weave {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070026
Anton Muhincfde8692014-11-25 03:36:59 +040027class CommandDefinition;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070028class CommandDictionary;
Vitaly Buka0d377a42015-07-21 10:26:08 -070029class CommandObserver;
Alex Vakulenkof6b38712014-09-03 16:23:38 -070030class CommandQueue;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070031
Vitaly Buka8b4ab962015-07-14 19:19:39 -070032class CommandInstance final : public Command {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070033 public:
Vitaly Bukac6029262015-10-07 09:29:13 -070034 class Observer {
35 public:
Vitaly Buka375f3282015-10-07 18:34:15 -070036 virtual void OnCommandDestroyed() = 0;
37 virtual void OnErrorChanged() = 0;
38 virtual void OnProgressChanged() = 0;
Vitaly Bukac6029262015-10-07 09:29:13 -070039 virtual void OnResultsChanged() = 0;
Vitaly Buka0209da42015-10-08 00:07:18 -070040 virtual void OnStateChanged() = 0;
Vitaly Bukac6029262015-10-07 09:29:13 -070041
42 protected:
43 virtual ~Observer() = default;
44 };
45
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070046 // Construct a command instance given the full command |name| which must
47 // be in format "<package_name>.<command_name>", a command |category| and
48 // a list of parameters and their values specified in |parameters|.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070049 CommandInstance(const std::string& name,
Vitaly Buka0209da42015-10-08 00:07:18 -070050 Command::Origin origin,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070051 const CommandDefinition* command_definition,
Vitaly Buka774cdf52015-07-21 13:55:00 -070052 const ValueMap& parameters);
Vitaly Buka8b4ab962015-07-14 19:19:39 -070053 ~CommandInstance() override;
54
55 // Command overrides.
Vitaly Buka8d8d2192015-07-21 22:25:09 -070056 const std::string& GetID() const override;
57 const std::string& GetName() const override;
Vitaly Buka0209da42015-10-08 00:07:18 -070058 Command::State GetState() const override;
59 Command::Origin GetOrigin() const override;
Vitaly Buka8d8d2192015-07-21 22:25:09 -070060 std::unique_ptr<base::DictionaryValue> GetParameters() const override;
61 std::unique_ptr<base::DictionaryValue> GetProgress() const override;
62 std::unique_ptr<base::DictionaryValue> GetResults() const override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070063 const Error* GetError() const override;
Vitaly Buka4f4e2282015-07-23 17:50:07 -070064 bool SetProgress(const base::DictionaryValue& progress,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070065 ErrorPtr* error) override;
Vitaly Buka2f548972015-10-08 19:34:49 -070066 bool Complete(const base::DictionaryValue& results, ErrorPtr* error) override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070067 bool Pause(ErrorPtr* error) override;
68 bool SetError(const Error* command_error, ErrorPtr* error) override;
69 bool Abort(const Error* command_error, ErrorPtr* error) override;
70 bool Cancel(ErrorPtr* error) override;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070071
Anton Muhincfde8692014-11-25 03:36:59 +040072 // Returns command definition.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070073 const CommandDefinition* GetCommandDefinition() const {
Anton Muhincfde8692014-11-25 03:36:59 +040074 return command_definition_;
75 }
76
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070077 // Parses a command instance JSON definition and constructs a CommandInstance
78 // object, checking the JSON |value| against the command definition schema
79 // found in command |dictionary|. On error, returns null unique_ptr and
80 // fills in error details in |error|.
Alex Vakulenkod1978d32015-04-29 17:33:26 -070081 // |command_id| is the ID of the command returned, as parsed from the |value|.
82 // The command ID extracted (if present in the JSON object) even if other
83 // parsing/validation error occurs and command instance is not constructed.
84 // This is used to report parse failures back to the server.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070085 static std::unique_ptr<CommandInstance> FromJson(
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070086 const base::Value* value,
Vitaly Buka0209da42015-10-08 00:07:18 -070087 Command::Origin origin,
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070088 const CommandDictionary& dictionary,
Alex Vakulenkod1978d32015-04-29 17:33:26 -070089 std::string* command_id,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070090 ErrorPtr* error);
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070091
Vitaly Bukaef213d72015-10-07 15:54:58 -070092 std::unique_ptr<base::DictionaryValue> ToJson() const;
93
Alex Vakulenkofedc4872014-08-20 12:38:43 -070094 // Sets the command ID (normally done by CommandQueue when the command
95 // instance is added to it).
96 void SetID(const std::string& id) { id_ = id; }
Vitaly Bukac6029262015-10-07 09:29:13 -070097
98 void AddObserver(Observer* observer);
99 void RemoveObserver(Observer* observer);
100
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700101 // Sets the pointer to queue this command is part of.
Vitaly Bukac6029262015-10-07 09:29:13 -0700102 void AttachToQueue(CommandQueue* queue) { queue_ = queue; }
103 void DetachFromQueue() {
104 observers_.Clear();
105 queue_ = nullptr;
106 command_definition_ = nullptr;
107 }
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700108
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700109 private:
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700110 // Helper function to update the command status.
111 // Used by Abort(), Cancel(), Done() methods.
Vitaly Buka0209da42015-10-08 00:07:18 -0700112 bool SetStatus(Command::State status, ErrorPtr* error);
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700113 // Helper method that removes this command from the command queue.
114 // Note that since the command queue owns the lifetime of the command instance
115 // object, removing a command from the queue will also destroy it.
116 void RemoveFromQueue();
117
Alex Vakulenkofedc4872014-08-20 12:38:43 -0700118 // Unique command ID within a command queue.
119 std::string id_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700120 // Full command name as "<package_name>.<command_name>".
121 std::string name_;
Alex Vakulenkof784e212015-04-20 12:33:52 -0700122 // The origin of the command, either "local" or "cloud".
Vitaly Buka0209da42015-10-08 00:07:18 -0700123 Command::Origin origin_ = Command::Origin::kLocal;
Anton Muhincfde8692014-11-25 03:36:59 +0400124 // Command definition.
Vitaly Bukac6029262015-10-07 09:29:13 -0700125 const CommandDefinition* command_definition_{nullptr};
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700126 // Command parameters and their values.
Vitaly Buka774cdf52015-07-21 13:55:00 -0700127 ValueMap parameters_;
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700128 // Current command execution progress.
Vitaly Buka774cdf52015-07-21 13:55:00 -0700129 ValueMap progress_;
Anton Muhincfde8692014-11-25 03:36:59 +0400130 // Command results.
Vitaly Buka774cdf52015-07-21 13:55:00 -0700131 ValueMap results_;
Vitaly Buka0209da42015-10-08 00:07:18 -0700132 // Current command state.
133 Command::State state_ = Command::State::kQueued;
Vitaly Buka70f77d92015-10-07 15:42:40 -0700134 // Error encountered during execution of the command.
135 ErrorPtr error_;
Vitaly Buka157b16a2015-07-31 16:20:48 -0700136 // Command observers.
137 base::ObserverList<Observer> observers_;
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700138 // Pointer to the command queue this command instance is added to.
139 // The queue owns the command instance, so it outlives this object.
140 CommandQueue* queue_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700141
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700142 DISALLOW_COPY_AND_ASSIGN(CommandInstance);
143};
144
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700145} // namespace weave
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700146
Vitaly Buka912b6982015-07-06 11:13:03 -0700147#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_