blob: a5e2916ddbf825c45fae6901bc43439a1e1b16f2 [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#ifndef BUFFET_COMMANDS_COMMAND_DEFINITION_H_
6#define BUFFET_COMMANDS_COMMAND_DEFINITION_H_
7
8#include <memory>
9#include <string>
10
Alex Vakulenko132617a2014-09-04 08:59:43 -070011#include <base/macros.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070012
13#include "buffet/commands/object_schema.h"
14
15namespace buffet {
16
17// A simple GCD command definition. This class contains the command category
18// and a full object schema describing the command parameter types and
19// constraints. See comments for CommandDefinitions::LoadCommands for the
20// detailed description of what command categories are and what they are used
21// for.
Alex Vakulenko534a3122015-05-22 15:48:53 -070022class CommandDefinition final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070023 public:
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070024 struct Visibility {
25 Visibility() = default;
26 Visibility(bool is_local, bool is_cloud)
27 : local{is_local}, cloud{is_cloud} {}
28
29 // Converts a comma-separated string of visibility identifiers into the
30 // Visibility bitset (|str| is a string like "local,cloud").
31 // Special string value "all" is treated as a list of every possible
32 // visibility values and "none" to have all the bits cleared.
33 bool FromString(const std::string& str, chromeos::ErrorPtr* error);
34
35 // Converts the visibility bitset to a string.
36 std::string ToString() const;
37
38 static Visibility GetAll() { return Visibility{true, true}; }
39 static Visibility GetLocal() { return Visibility{true, false}; }
40 static Visibility GetCloud() { return Visibility{false, true}; }
41 static Visibility GetNone() { return Visibility{false, false}; }
42
43 bool local{false}; // Command is available to local clients.
44 bool cloud{false}; // Command is available to cloud clients.
45 };
46
Alex Vakulenko7c36b672014-07-16 14:50:58 -070047 CommandDefinition(const std::string& category,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070048 std::unique_ptr<const ObjectSchema> parameters,
Vitaly Buka4129dfa2015-04-29 12:16:58 -070049 std::unique_ptr<const ObjectSchema> progress,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070050 std::unique_ptr<const ObjectSchema> results);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070051
52 // Gets the category this command belongs to.
53 const std::string& GetCategory() const { return category_; }
54 // Gets the object schema for command parameters.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070055 const ObjectSchema* GetParameters() const { return parameters_.get(); }
Vitaly Buka4129dfa2015-04-29 12:16:58 -070056 // Gets the object schema for command progress.
57 const ObjectSchema* GetProgress() const { return progress_.get(); }
Anton Muhin71fb9d52014-11-21 22:22:39 +040058 // Gets the object schema for command results.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070059 const ObjectSchema* GetResults() const { return results_.get(); }
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070060 // Returns the command visibility.
61 const Visibility& GetVisibility() const { return visibility_; }
62 // Changes the command visibility.
63 void SetVisibility(const Visibility& visibility);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070064
65 private:
66 std::string category_; // Cmd category. Could be "powerd" for "base.reboot".
Alex Vakulenko5ef75792015-03-19 15:50:44 -070067 std::unique_ptr<const ObjectSchema> parameters_; // Command parameters def.
Vitaly Buka4129dfa2015-04-29 12:16:58 -070068 std::unique_ptr<const ObjectSchema> progress_; // Command progress def.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070069 std::unique_ptr<const ObjectSchema> results_; // Command results def.
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070070 Visibility visibility_; // Available to all by default.
71
Alex Vakulenko7c36b672014-07-16 14:50:58 -070072 DISALLOW_COPY_AND_ASSIGN(CommandDefinition);
73};
74
75} // namespace buffet
76
77#endif // BUFFET_COMMANDS_COMMAND_DEFINITION_H_