blob: 02b7722b6b7482bf061f5670924dc7936cebbb15 [file] [log] [blame]
Alex Vakulenko7c36b672014-07-16 14:50:58 -07001// 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 Vakulenko5e86fee2015-04-17 08:47:45 -07007#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 Vakulenko7c36b672014-07-16 14:50:58 -070014namespace buffet {
15
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070016bool 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
48std::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 Vakulenko7c36b672014-07-16 14:50:58 -070058CommandDefinition::CommandDefinition(
59 const std::string& category,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070060 std::unique_ptr<const ObjectSchema> parameters,
61 std::unique_ptr<const ObjectSchema> results)
62 : category_{category},
63 parameters_{std::move(parameters)},
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070064 results_{std::move(results)} {
65 // Set to be available to all clients by default.
66 visibility_ = Visibility::GetAll();
67}
68
69void CommandDefinition::SetVisibility(const Visibility& visibility) {
70 visibility_ = visibility;
71}
Alex Vakulenko7c36b672014-07-16 14:50:58 -070072
73} // namespace buffet