buffet: Add command_def filtering by command visibility
When updating cloud or local client with the device's CDD make sure
to send only the command definitions that are available to particular
client (local vs cloud).
Also made it possible to subscribe to command definition change notifications
for more than one listener (using base::CallbackList) so that both the local
and cloud adapters can notify the respective clients about command visibility
changes (the actual API to change the command visibility is coming in a
follow-up CL).
BUG=brillo:797
TEST=`FEATURES=test emerge-link buffet`
Change-Id: I6bec36633ababcb534012abad2c37a3502d8faf4
Reviewed-on: https://chromium-review.googlesource.com/266209
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/commands/command_manager.h b/buffet/commands/command_manager.h
index a703ab3..3e23cbf 100644
--- a/buffet/commands/command_manager.h
+++ b/buffet/commands/command_manager.h
@@ -8,6 +8,8 @@
#include <memory>
#include <string>
+#include <base/callback.h>
+#include <base/callback_list.h>
#include <base/files/file_path.h>
#include <base/macros.h>
#include <base/memory/weak_ptr.h>
@@ -32,6 +34,13 @@
// dispatched to the device.
class CommandManager final {
public:
+ // A token given by CommandManager in response to AddOnCommandDefChanged().
+ // When the CallbackToken is destroyed, the registered notification
+ // callback associated with it will automatically be removed from the command
+ // manager's callback list.
+ using CallbackToken =
+ std::unique_ptr<base::CallbackList<void()>::Subscription>;
+
CommandManager();
explicit CommandManager(
const base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager>&
@@ -40,8 +49,8 @@
explicit CommandManager(CommandDispachInterface* dispatch_interface);
// Sets callback which is called when command definitions is changed.
- void SetOnCommandDefChanged(const base::Closure& on_command_defs_changed) {
- on_command_defs_changed_ = on_command_defs_changed;
+ CallbackToken AddOnCommandDefChanged(const base::Closure& callback) {
+ return CallbackToken{on_command_changed_.Add(callback).release()};
}
// Returns the command definitions for the device.
@@ -98,7 +107,7 @@
CommandDictionary dictionary_; // Command definitions/schemas.
CommandQueue command_queue_;
DBusCommandDispacher command_dispatcher_;
- base::Closure on_command_defs_changed_;
+ base::CallbackList<void()> on_command_changed_;
DISALLOW_COPY_AND_ASSIGN(CommandManager);
};