blob: b1028d0371d9000b47907995d9b7eed80fafd2c1 [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 Buka7b382ac2015-08-03 13:50:01 -070015#include <weave/command.h>
Vitaly Buka34668e72015-12-15 14:46:47 -080016#include <weave/error.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;
Alex Vakulenko88f55d82015-12-03 15:30:27 -080053 const std::string& GetComponent() const override;
Vitaly Buka0209da42015-10-08 00:07:18 -070054 Command::State GetState() const override;
55 Command::Origin GetOrigin() const override;
Vitaly Bukac4305602015-11-24 23:33:09 -080056 const base::DictionaryValue& GetParameters() const override;
57 const base::DictionaryValue& GetProgress() const override;
58 const base::DictionaryValue& GetResults() const override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070059 const Error* GetError() const override;
Vitaly Buka4f4e2282015-07-23 17:50:07 -070060 bool SetProgress(const base::DictionaryValue& progress,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070061 ErrorPtr* error) override;
Vitaly Buka2f548972015-10-08 19:34:49 -070062 bool Complete(const base::DictionaryValue& results, ErrorPtr* error) override;
Vitaly Buka47a1f6f2015-10-07 18:09:57 -070063 bool Pause(ErrorPtr* error) override;
64 bool SetError(const Error* command_error, ErrorPtr* error) override;
65 bool Abort(const Error* command_error, ErrorPtr* error) override;
66 bool Cancel(ErrorPtr* error) override;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070067
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070068 // Parses a command instance JSON definition and constructs a CommandInstance
Alex Vakulenko88f55d82015-12-03 15:30:27 -080069 // object.
70 // On error, returns null unique_ptr and 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.
Vitaly Buka34668e72015-12-15 14:46:47 -080075 static std::unique_ptr<CommandInstance> FromJson(const base::Value* value,
76 Command::Origin origin,
77 std::string* command_id,
78 ErrorPtr* error);
Alex Vakulenko8dc69af2014-08-07 10:29:42 -070079
Vitaly Bukaef213d72015-10-07 15:54:58 -070080 std::unique_ptr<base::DictionaryValue> ToJson() const;
81
Alex Vakulenkofedc4872014-08-20 12:38:43 -070082 // Sets the command ID (normally done by CommandQueue when the command
83 // instance is added to it).
84 void SetID(const std::string& id) { id_ = id; }
Alex Vakulenko88f55d82015-12-03 15:30:27 -080085 void SetComponent(const std::string& component) { component_ = component; }
Vitaly Bukac6029262015-10-07 09:29:13 -070086
87 void AddObserver(Observer* observer);
88 void RemoveObserver(Observer* observer);
89
Alex Vakulenkof6b38712014-09-03 16:23:38 -070090 // Sets the pointer to queue this command is part of.
Vitaly Bukac6029262015-10-07 09:29:13 -070091 void AttachToQueue(CommandQueue* queue) { queue_ = queue; }
92 void DetachFromQueue() {
93 observers_.Clear();
94 queue_ = nullptr;
Vitaly Bukac6029262015-10-07 09:29:13 -070095 }
Alex Vakulenkof6b38712014-09-03 16:23:38 -070096
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -070097 private:
Alex Vakulenkof6b38712014-09-03 16:23:38 -070098 // Helper function to update the command status.
99 // Used by Abort(), Cancel(), Done() methods.
Vitaly Buka0209da42015-10-08 00:07:18 -0700100 bool SetStatus(Command::State status, ErrorPtr* error);
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700101 // Helper method that removes this command from the command queue.
102 // Note that since the command queue owns the lifetime of the command instance
103 // object, removing a command from the queue will also destroy it.
104 void RemoveFromQueue();
105
Alex Vakulenkofedc4872014-08-20 12:38:43 -0700106 // Unique command ID within a command queue.
107 std::string id_;
Alex Vakulenko88f55d82015-12-03 15:30:27 -0800108 // Full command name as "<trait_name>.<command_name>".
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700109 std::string name_;
Alex Vakulenko88f55d82015-12-03 15:30:27 -0800110 // Full path to the component this command is intended for.
111 std::string component_;
Alex Vakulenkof784e212015-04-20 12:33:52 -0700112 // The origin of the command, either "local" or "cloud".
Vitaly Buka0209da42015-10-08 00:07:18 -0700113 Command::Origin origin_ = Command::Origin::kLocal;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700114 // Command parameters and their values.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800115 base::DictionaryValue parameters_;
Vitaly Buka4129dfa2015-04-29 12:16:58 -0700116 // Current command execution progress.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800117 base::DictionaryValue progress_;
Anton Muhincfde8692014-11-25 03:36:59 +0400118 // Command results.
Alex Vakulenko36bf1b52015-11-23 09:35:37 -0800119 base::DictionaryValue results_;
Vitaly Buka0209da42015-10-08 00:07:18 -0700120 // Current command state.
121 Command::State state_ = Command::State::kQueued;
Vitaly Buka70f77d92015-10-07 15:42:40 -0700122 // Error encountered during execution of the command.
123 ErrorPtr error_;
Vitaly Buka157b16a2015-07-31 16:20:48 -0700124 // Command observers.
125 base::ObserverList<Observer> observers_;
Alex Vakulenkof6b38712014-09-03 16:23:38 -0700126 // Pointer to the command queue this command instance is added to.
127 // The queue owns the command instance, so it outlives this object.
128 CommandQueue* queue_ = nullptr;
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700129
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700130 DISALLOW_COPY_AND_ASSIGN(CommandInstance);
131};
132
Vitaly Bukab6f015a2015-07-09 14:59:23 -0700133} // namespace weave
Alex Vakulenkoaa3a5592014-08-07 07:24:06 -0700134
Vitaly Buka912b6982015-07-06 11:13:03 -0700135#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_