blob: 260cb3d2cd9a696b9efcb4b49bebb4091732fc15 [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,
Vitaly Buka4129dfa2015-04-29 12:16:58 -070061 std::unique_ptr<const ObjectSchema> progress,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070062 std::unique_ptr<const ObjectSchema> results)
Vitaly Buka4129dfa2015-04-29 12:16:58 -070063 : category_{category},
64 parameters_{std::move(parameters)},
65 progress_{std::move(progress)},
66 results_{std::move(results)} {
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070067 // Set to be available to all clients by default.
68 visibility_ = Visibility::GetAll();
69}
70
71void CommandDefinition::SetVisibility(const Visibility& visibility) {
72 visibility_ = visibility;
73}
Alex Vakulenko7c36b672014-07-16 14:50:58 -070074
75} // namespace buffet