Call command handler with weak_ptr
Command handler should keep only weak_ptr to command and check on every
access. Long async commands will need to keep reference to the command.
BUG:24267885
Change-Id: I3ca3be9e31b9e9a942eca001ed21f1133973f0ea
Reviewed-on: https://weave-review.googlesource.com/1255
Reviewed-by: Alex Vakulenko <avakulenko@google.com>
diff --git a/libweave/src/commands/command_queue.h b/libweave/src/commands/command_queue.h
index 888c04f..99da0b2 100644
--- a/libweave/src/commands/command_queue.h
+++ b/libweave/src/commands/command_queue.h
@@ -25,7 +25,8 @@
public:
CommandQueue() = default;
- using CommandCallback = Device::CommandHandlerCallback;
+ // TODO: Remove AddCommandAddedCallback and AddCommandRemovedCallback.
+ using CommandCallback = base::Callback<void(Command* command)>;
// Adds notifications callback for a new command is added to the queue.
void AddCommandAddedCallback(const CommandCallback& callback);
@@ -34,7 +35,7 @@
void AddCommandRemovedCallback(const CommandCallback& callback);
void AddCommandHandler(const std::string& command_name,
- const CommandCallback& callback);
+ const Device::CommandHandlerCallback& callback);
// Checks if the command queue is empty.
bool IsEmpty() const { return map_.empty(); }
@@ -75,7 +76,7 @@
base::Time test_now_;
// ID-to-CommandInstance map.
- std::map<std::string, std::unique_ptr<CommandInstance>> map_;
+ std::map<std::string, std::shared_ptr<CommandInstance>> map_;
// Queue of commands to be removed.
std::queue<std::pair<base::Time, std::string>> remove_queue_;
@@ -83,8 +84,8 @@
using CallbackList = std::vector<CommandCallback>;
CallbackList on_command_added_;
CallbackList on_command_removed_;
- std::map<std::string, CommandCallback> command_callbacks_;
- CommandCallback default_command_callback_;
+ std::map<std::string, Device::CommandHandlerCallback> command_callbacks_;
+ Device::CommandHandlerCallback default_command_callback_;
DISALLOW_COPY_AND_ASSIGN(CommandQueue);
};