buffet: Add command visibility option to command schema
Add the ability to specify command visibility by using 'visibility'
property in command definition JSON file.
This CL adds parsing code for command visibility as well as the storage
of the property inside CommandDefinition class. The actual usage of this
property will be implemented in subsequent CLs.
BUG=brillo:797
TEST=`FEATURES=test emerge-link buffet`
Change-Id: I3f9b49f8b57f6b63e7c4ae73d042e414c9b6b801
Reviewed-on: https://chromium-review.googlesource.com/266208
Trybot-Ready: Alex Vakulenko <avakulenko@chromium.org>
Tested-by: Alex Vakulenko <avakulenko@chromium.org>
Reviewed-by: Vitaly Buka <vitalybuka@chromium.org>
Commit-Queue: Vitaly Buka <vitalybuka@chromium.org>
diff --git a/buffet/commands/command_definition.cc b/buffet/commands/command_definition.cc
index c5854c2..02b7722 100644
--- a/buffet/commands/command_definition.cc
+++ b/buffet/commands/command_definition.cc
@@ -4,14 +4,70 @@
#include "buffet/commands/command_definition.h"
+#include <vector>
+
+#include <chromeos/errors/error.h>
+#include <chromeos/strings/string_utils.h>
+
+#include "buffet/commands/schema_constants.h"
+
namespace buffet {
+bool CommandDefinition::Visibility::FromString(const std::string& str,
+ chromeos::ErrorPtr* error) {
+ // This special case is useful for places where we want to make a command
+ // to ALL clients, even if new clients are added in the future.
+ if (str == commands::attributes::kCommand_Visibility_All) {
+ local = true;
+ cloud = true;
+ return true;
+ }
+
+ // Clear any bits first.
+ local = false;
+ cloud = false;
+ if (str == commands::attributes::kCommand_Visibility_None)
+ return true;
+
+ for (const std::string& value : chromeos::string_utils::Split(str, ",")) {
+ if (value == commands::attributes::kCommand_Visibility_Local) {
+ local = true;
+ } else if (value == commands::attributes::kCommand_Visibility_Cloud) {
+ cloud = true;
+ } else {
+ chromeos::Error::AddToPrintf(
+ error, FROM_HERE, errors::commands::kDomain,
+ errors::commands::kInvalidPropValue,
+ "Invalid command visibility value '%s'", value.c_str());
+ return false;
+ }
+ }
+ return true;
+}
+
+std::string CommandDefinition::Visibility::ToString() const {
+ if (local && cloud)
+ return commands::attributes::kCommand_Visibility_All;
+ if (!local && !cloud)
+ return commands::attributes::kCommand_Visibility_None;
+ if (local)
+ return commands::attributes::kCommand_Visibility_Local;
+ return commands::attributes::kCommand_Visibility_Cloud;
+}
+
CommandDefinition::CommandDefinition(
const std::string& category,
std::unique_ptr<const ObjectSchema> parameters,
std::unique_ptr<const ObjectSchema> results)
: category_{category},
parameters_{std::move(parameters)},
- results_{std::move(results)} {}
+ results_{std::move(results)} {
+ // Set to be available to all clients by default.
+ visibility_ = Visibility::GetAll();
+}
+
+void CommandDefinition::SetVisibility(const Visibility& visibility) {
+ visibility_ = visibility;
+}
} // namespace buffet