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 | #ifndef BUFFET_COMMANDS_COMMAND_DEFINITION_H_ |
| 6 | #define BUFFET_COMMANDS_COMMAND_DEFINITION_H_ |
| 7 | |
| 8 | #include <memory> |
| 9 | #include <string> |
| 10 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 11 | #include <base/macros.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 12 | |
| 13 | #include "buffet/commands/object_schema.h" |
| 14 | |
| 15 | namespace 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 Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 22 | class CommandDefinition final { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 23 | public: |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 24 | 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 Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 47 | CommandDefinition(const std::string& category, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 48 | std::unique_ptr<const ObjectSchema> parameters, |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 49 | std::unique_ptr<const ObjectSchema> progress, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 50 | std::unique_ptr<const ObjectSchema> results); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 51 | |
| 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 Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 55 | const ObjectSchema* GetParameters() const { return parameters_.get(); } |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 56 | // Gets the object schema for command progress. |
| 57 | const ObjectSchema* GetProgress() const { return progress_.get(); } |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 58 | // Gets the object schema for command results. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 59 | const ObjectSchema* GetResults() const { return results_.get(); } |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 60 | // Returns the command visibility. |
| 61 | const Visibility& GetVisibility() const { return visibility_; } |
| 62 | // Changes the command visibility. |
| 63 | void SetVisibility(const Visibility& visibility); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 64 | |
| 65 | private: |
| 66 | std::string category_; // Cmd category. Could be "powerd" for "base.reboot". |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 67 | std::unique_ptr<const ObjectSchema> parameters_; // Command parameters def. |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 68 | std::unique_ptr<const ObjectSchema> progress_; // Command progress def. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 69 | std::unique_ptr<const ObjectSchema> results_; // Command results def. |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 70 | Visibility visibility_; // Available to all by default. |
| 71 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 72 | DISALLOW_COPY_AND_ASSIGN(CommandDefinition); |
| 73 | }; |
| 74 | |
| 75 | } // namespace buffet |
| 76 | |
| 77 | #endif // BUFFET_COMMANDS_COMMAND_DEFINITION_H_ |