Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 1 | // Copyright 2014 The Chromium OS 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 | #include "buffet/commands/command_definition.h" |
| 6 | |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 7 | #include <vector> |
| 8 | |
| 9 | #include <chromeos/errors/error.h> |
| 10 | #include <chromeos/strings/string_utils.h> |
| 11 | |
| 12 | #include "buffet/commands/schema_constants.h" |
| 13 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 14 | namespace buffet { |
| 15 | |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 16 | bool CommandDefinition::Visibility::FromString(const std::string& str, |
| 17 | chromeos::ErrorPtr* error) { |
| 18 | // This special case is useful for places where we want to make a command |
| 19 | // to ALL clients, even if new clients are added in the future. |
| 20 | if (str == commands::attributes::kCommand_Visibility_All) { |
| 21 | local = true; |
| 22 | cloud = true; |
| 23 | return true; |
| 24 | } |
| 25 | |
| 26 | // Clear any bits first. |
| 27 | local = false; |
| 28 | cloud = false; |
| 29 | if (str == commands::attributes::kCommand_Visibility_None) |
| 30 | return true; |
| 31 | |
| 32 | for (const std::string& value : chromeos::string_utils::Split(str, ",")) { |
| 33 | if (value == commands::attributes::kCommand_Visibility_Local) { |
| 34 | local = true; |
| 35 | } else if (value == commands::attributes::kCommand_Visibility_Cloud) { |
| 36 | cloud = true; |
| 37 | } else { |
| 38 | chromeos::Error::AddToPrintf( |
| 39 | error, FROM_HERE, errors::commands::kDomain, |
| 40 | errors::commands::kInvalidPropValue, |
| 41 | "Invalid command visibility value '%s'", value.c_str()); |
| 42 | return false; |
| 43 | } |
| 44 | } |
| 45 | return true; |
| 46 | } |
| 47 | |
| 48 | std::string CommandDefinition::Visibility::ToString() const { |
| 49 | if (local && cloud) |
| 50 | return commands::attributes::kCommand_Visibility_All; |
| 51 | if (!local && !cloud) |
| 52 | return commands::attributes::kCommand_Visibility_None; |
| 53 | if (local) |
| 54 | return commands::attributes::kCommand_Visibility_Local; |
| 55 | return commands::attributes::kCommand_Visibility_Cloud; |
| 56 | } |
| 57 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 58 | CommandDefinition::CommandDefinition( |
| 59 | const std::string& category, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 60 | std::unique_ptr<const ObjectSchema> parameters, |
| 61 | std::unique_ptr<const ObjectSchema> results) |
| 62 | : category_{category}, |
| 63 | parameters_{std::move(parameters)}, |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 64 | results_{std::move(results)} { |
| 65 | // Set to be available to all clients by default. |
| 66 | visibility_ = Visibility::GetAll(); |
| 67 | } |
| 68 | |
| 69 | void CommandDefinition::SetVisibility(const Visibility& visibility) { |
| 70 | visibility_ = visibility; |
| 71 | } |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 72 | |
| 73 | } // namespace buffet |