blob: 82df190a4601613bab5f9c931e908824fd5ae7fd [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
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070018namespace base {
19class Value;
20} // namespace base
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070021
Vitaly Bukab6f015a2015-07-09 14:59:23 -070022namespace weave {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070023
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070024class CommandDictionary;
Vitaly Buka0d377a42015-07-21 10:26:08 -070025class CommandObserver;
Alex Vakulenkof6b38712014-09-03 16:23:38 -070026class CommandQueue;
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070027
Vitaly Buka8b4ab962015-07-14 19:19:39 -070028class CommandInstance final : public Command {
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070029 public:
Vitaly Bukac6029262015-10-07 09:29:13 -070030 class Observer {
31 public:
Vitaly Buka375f3282015-10-07 18:34:15 -070032 virtual void OnCommandDestroyed() = 0;
33 virtual void OnErrorChanged() = 0;
34 virtual void OnProgressChanged() = 0;
Vitaly Bukac6029262015-10-07 09:29:13 -070035 virtual void OnResultsChanged() = 0;
Vitaly Buka0209da42015-10-08 00:07:18 -070036 virtual void OnStateChanged() = 0;
Vitaly Bukac6029262015-10-07 09:29:13 -070037
38 protected:
Vitaly Buka3bfb13d2015-11-24 14:46:13 -080039 virtual ~Observer() {}
Vitaly Bukac6029262015-10-07 09:29:13 -070040 };
41
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070042 // Construct a command instance given the full command |name| which must
Alex Vakulenko36bf1b52015-11-23 09:35:37 -080043 // be in format "<package_name>.<command_name>" and a list of parameters and
44 // their values specified in |parameters|.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070045 CommandInstance(const std::string& name,
Vitaly Buka0209da42015-10-08 00:07:18 -070046 Command::Origin origin,
Alex Vakulenko36bf1b52015-11-23 09:35:37 -080047 const base::DictionaryValue& parameters);
Vitaly Buka8b4ab962015-07-14 19:19:39 -070048 ~CommandInstance() override;
49
50 // Command overrides.
Vitaly Buka8d8d2192015-07-21 22:25:09 -070051 const std::string& GetID() const override;
52 const std::string& GetName() const override;
Vitaly Buka0209da42015-10-08 00:07:18 -070053 Command::State GetState() const override;
54 Command::Origin GetOrigin() const override;
Vitaly Bukac4305602015-11-24 23:33:09 -080055 const base::DictionaryValue& GetParameters() const override;
56 const base::DictionaryValue& GetProgress() const override;
57 const base::DictionaryValue& GetResults() const override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070058 const Error* GetError() const override;
Vitaly Buka4f4e2282015-07-23 17:50:07 -070059 bool SetProgress(const base::DictionaryValue& progress,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070060 ErrorPtr* error) override;
Vitaly Buka2f548972015-10-08 19:34:49 -070061 bool Complete(const base::DictionaryValue& results, ErrorPtr* error) override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070062 bool Pause(ErrorPtr* error) override;
63 bool SetError(const Error* command_error, ErrorPtr* error) override;
64 bool Abort(const Error* command_error, ErrorPtr* error) override;
65 bool Cancel(ErrorPtr* error) override;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070066
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070067 // Parses a command instance JSON definition and constructs a CommandInstance
68 // object, checking the JSON |value| against the command definition schema
69 // found in command |dictionary|. On error, returns null unique_ptr and
70 // fills in error details in |error|.
Alex Vakulenkod1978d32015-04-29 17:33:26 -070071 // |command_id| is the ID of the command returned, as parsed from the |value|.
72 // The command ID extracted (if present in the JSON object) even if other
73 // parsing/validation error occurs and command instance is not constructed.
74 // This is used to report parse failures back to the server.
Alex Vakulenkofedc4872014-08-20 12:38:43 -070075 static std::unique_ptr<CommandInstance> FromJson(
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070076 const base::Value* value,
Vitaly Buka0209da42015-10-08 00:07:18 -070077 Command::Origin origin,
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070078 const CommandDictionary& dictionary,
Alex Vakulenkod1978d32015-04-29 17:33:26 -070079 std::string* command_id,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070080 ErrorPtr* error);
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070081
Vitaly Bukaef213d72015-10-07 15:54:58 -070082 std::unique_ptr<base::DictionaryValue> ToJson() const;
83
Alex Vakulenkofedc4872014-08-20 12:38:43 -070084 // Sets the command ID (normally done by CommandQueue when the command
85 // instance is added to it).
86 void SetID(const std::string& id) { id_ = id; }
Vitaly Bukac6029262015-10-07 09:29:13 -070087
88 void AddObserver(Observer* observer);
89 void RemoveObserver(Observer* observer);
90
Alex Vakulenkof6b38712014-09-03 16:23:38 -070091 // Sets the pointer to queue this command is part of.
Vitaly Bukac6029262015-10-07 09:29:13 -070092 void AttachToQueue(CommandQueue* queue) { queue_ = queue; }
93 void DetachFromQueue() {
94 observers_.Clear();
95 queue_ = nullptr;
Vitaly Bukac6029262015-10-07 09:29:13 -070096 }
Alex Vakulenkof6b38712014-09-03 16:23:38 -070097
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070098 private:
Alex Vakulenkof6b38712014-09-03 16:23:38 -070099 // Helper function to update the command status.
100 // Used by Abort(), Cancel(), Done() methods.
Vitaly Buka0209da42015-10-08 00:07:18 -0700101 bool SetStatus(Command::State status, ErrorPtr* error);
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700102 // Helper method that removes this command from the command queue.
103 // Note that since the command queue owns the lifetime of the command instance
104 // object, removing a command from the queue will also destroy it.
105 void RemoveFromQueue();
106
Alex Vakulenkofedc4872014-08-20 12:38:43 -0700107 // Unique command ID within a command queue.
108 std::string id_;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700109 // Full command name as "<package_name>.<command_name>".
110 std::string name_;
Alex Vakulenkof784e212015-04-20 12:33:52 -0700111 // The origin of the command, either "local" or "cloud".
Vitaly Buka0209da42015-10-08 00:07:18 -0700112 Command::Origin origin_ = Command::Origin::kLocal;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700113 // Command parameters and their values.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800114 base::DictionaryValue parameters_;
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700115 // Current command execution progress.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800116 base::DictionaryValue progress_;
Anton Muhincfde8692014-11-25 03:36:59 +0400117 // Command results.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800118 base::DictionaryValue results_;
Vitaly Buka0209da42015-10-08 00:07:18 -0700119 // Current command state.
120 Command::State state_ = Command::State::kQueued;
Vitaly Buka70f77d92015-10-07 15:42:40 -0700121 // Error encountered during execution of the command.
122 ErrorPtr error_;
Vitaly Buka157b16a2015-07-31 16:20:48 -0700123 // Command observers.
124 base::ObserverList<Observer> observers_;
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700125 // Pointer to the command queue this command instance is added to.
126 // The queue owns the command instance, so it outlives this object.
127 CommandQueue* queue_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700128
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700129 DISALLOW_COPY_AND_ASSIGN(CommandInstance);
130};
131
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700132} // namespace weave
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700133
Vitaly Buka912b6982015-07-06 11:13:03 -0700134#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_