1 // Copyright 2015 The Weave 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 LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ 6 #define LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ 7 8 #include <map> 9 #include <memory> 10 #include <string> 11 #include <vector> 12 13 #include <base/macros.h> 14 #include <base/observer_list.h> 15 #include <weave/command.h> 16 #include <weave/error.h> 17 18 namespace base { 19 class Value; 20 } // namespace base 21 22 namespace weave { 23 24 class CommandDictionary; 25 class CommandObserver; 26 class CommandQueue; 27 28 class CommandInstance final : public Command { 29 public: 30 class Observer { 31 public: 32 virtual void OnCommandDestroyed() = 0; 33 virtual void OnErrorChanged() = 0; 34 virtual void OnProgressChanged() = 0; 35 virtual void OnResultsChanged() = 0; 36 virtual void OnStateChanged() = 0; 37 38 protected: ~Observer()39 virtual ~Observer() {} 40 }; 41 42 // Construct a command instance given the full command |name| which must 43 // be in format "<package_name>.<command_name>" and a list of parameters and 44 // their values specified in |parameters|. 45 CommandInstance(const std::string& name, 46 Command::Origin origin, 47 const base::DictionaryValue& parameters); 48 ~CommandInstance() override; 49 50 // Command overrides. 51 const std::string& GetID() const override; 52 const std::string& GetName() const override; 53 const std::string& GetComponent() const override; 54 Command::State GetState() const override; 55 Command::Origin GetOrigin() const override; 56 const base::DictionaryValue& GetParameters() const override; 57 const base::DictionaryValue& GetProgress() const override; 58 const base::DictionaryValue& GetResults() const override; 59 const Error* GetError() const override; 60 bool SetProgress(const base::DictionaryValue& progress, 61 ErrorPtr* error) override; 62 bool Complete(const base::DictionaryValue& results, ErrorPtr* error) override; 63 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; 67 68 // Parses a command instance JSON definition and constructs a CommandInstance 69 // object. 70 // On error, returns null unique_ptr and fills in error details in |error|. 71 // |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. 75 static std::unique_ptr<CommandInstance> FromJson(const base::Value* value, 76 Command::Origin origin, 77 std::string* command_id, 78 ErrorPtr* error); 79 80 std::unique_ptr<base::DictionaryValue> ToJson() const; 81 82 // Sets the command ID (normally done by CommandQueue when the command 83 // instance is added to it). SetID(const std::string & id)84 void SetID(const std::string& id) { id_ = id; } SetComponent(const std::string & component)85 void SetComponent(const std::string& component) { component_ = component; } 86 87 void AddObserver(Observer* observer); 88 void RemoveObserver(Observer* observer); 89 90 // Sets the pointer to queue this command is part of. AttachToQueue(CommandQueue * queue)91 void AttachToQueue(CommandQueue* queue) { queue_ = queue; } DetachFromQueue()92 void DetachFromQueue() { queue_ = nullptr; } 93 94 private: 95 // Helper function to update the command status. 96 // Used by Abort(), Cancel(), Done() methods. 97 bool SetStatus(Command::State status, ErrorPtr* error); 98 // Helper method that removes this command from the command queue. 99 // Note that since the command queue owns the lifetime of the command instance 100 // object, removing a command from the queue will also destroy it. 101 void RemoveFromQueue(); 102 103 // Unique command ID within a command queue. 104 std::string id_; 105 // Full command name as "<trait_name>.<command_name>". 106 std::string name_; 107 // Full path to the component this command is intended for. 108 std::string component_; 109 // The origin of the command, either "local" or "cloud". 110 Command::Origin origin_ = Command::Origin::kLocal; 111 // Command parameters and their values. 112 base::DictionaryValue parameters_; 113 // Current command execution progress. 114 base::DictionaryValue progress_; 115 // Command results. 116 base::DictionaryValue results_; 117 // Current command state. 118 Command::State state_ = Command::State::kQueued; 119 // Error encountered during execution of the command. 120 ErrorPtr error_; 121 // Command observers. 122 base::ObserverList<Observer> observers_; 123 // Pointer to the command queue this command instance is added to. 124 // The queue owns the command instance, so it outlives this object. 125 CommandQueue* queue_ = nullptr; 126 127 DISALLOW_COPY_AND_ASSIGN(CommandInstance); 128 }; 129 130 } // namespace weave 131 132 #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_INSTANCE_H_ 133