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/manager.cc b/buffet/manager.cc
index 93d3029..860e880 100644
--- a/buffet/manager.cc
+++ b/buffet/manager.cc
@@ -5,6 +5,7 @@
 #include "buffet/manager.h"
 
 #include <map>
+#include <set>
 #include <string>
 
 #include <base/bind.h>
@@ -21,7 +22,6 @@
 #include <dbus/values_util.h>
 
 #include "buffet/commands/command_instance.h"
-#include "buffet/commands/command_manager.h"
 #include "buffet/states/state_change_queue.h"
 #include "buffet/states/state_manager.h"
 #include "buffet/storage_impls.h"
@@ -52,7 +52,7 @@
                     const AsyncEventSequencer::CompletionAction& cb) {
   command_manager_ =
       std::make_shared<CommandManager>(dbus_object_.GetObjectManager());
-  command_manager_->SetOnCommandDefChanged(
+  command_changed_callback_token_ = command_manager_->AddOnCommandDefChanged(
       base::Bind(&Manager::OnCommandDefsChanged, base::Unretained(this)));
   command_manager_->Startup(base::FilePath{"/etc/buffet"},
                             test_definitions_path);
@@ -222,8 +222,11 @@
 
 void Manager::OnCommandDefsChanged() {
   chromeos::ErrorPtr error;
-  std::unique_ptr<base::DictionaryValue> commands =
-      command_manager_->GetCommandDictionary().GetCommandsAsJson(true, &error);
+  // Limit only to commands that are visible to the local clients.
+  auto commands = command_manager_->GetCommandDictionary().GetCommandsAsJson(
+      [](const buffet::CommandDefinition* def) {
+        return def->GetVisibility().local;
+      }, true, &error);
   CHECK(commands);
   std::string json;
   base::JSONWriter::WriteWithOptions(commands.get(),