Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_ |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 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 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 13 | #include "src/commands/object_schema.h" |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 14 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 15 | namespace weave { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 16 | |
Vitaly Buka | c4c67af | 2015-10-02 01:08:12 -0700 | [diff] [blame] | 17 | enum class UserRole { |
| 18 | kViewer, |
| 19 | kUser, |
| 20 | kManager, |
| 21 | kOwner, |
| 22 | }; |
| 23 | |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 24 | // A simple GCD command definition. This class contains the full object schema |
| 25 | // describing the command parameter types and constraints. |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 26 | class CommandDefinition final { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 27 | public: |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 28 | struct Visibility { |
| 29 | Visibility() = default; |
| 30 | Visibility(bool is_local, bool is_cloud) |
| 31 | : local{is_local}, cloud{is_cloud} {} |
| 32 | |
| 33 | // Converts a comma-separated string of visibility identifiers into the |
| 34 | // Visibility bitset (|str| is a string like "local,cloud"). |
| 35 | // Special string value "all" is treated as a list of every possible |
| 36 | // visibility values and "none" to have all the bits cleared. |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 37 | bool FromString(const std::string& str, ErrorPtr* error); |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 38 | |
| 39 | // Converts the visibility bitset to a string. |
| 40 | std::string ToString() const; |
| 41 | |
| 42 | static Visibility GetAll() { return Visibility{true, true}; } |
| 43 | static Visibility GetLocal() { return Visibility{true, false}; } |
| 44 | static Visibility GetCloud() { return Visibility{false, true}; } |
| 45 | static Visibility GetNone() { return Visibility{false, false}; } |
| 46 | |
| 47 | bool local{false}; // Command is available to local clients. |
| 48 | bool cloud{false}; // Command is available to cloud clients. |
| 49 | }; |
| 50 | |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 51 | CommandDefinition(std::unique_ptr<const ObjectSchema> parameters, |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 52 | std::unique_ptr<const ObjectSchema> progress, |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 53 | std::unique_ptr<const ObjectSchema> results); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 54 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 55 | // Gets the object schema for command parameters. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 56 | const ObjectSchema* GetParameters() const { return parameters_.get(); } |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 57 | // Gets the object schema for command progress. |
| 58 | const ObjectSchema* GetProgress() const { return progress_.get(); } |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 59 | // Gets the object schema for command results. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 60 | const ObjectSchema* GetResults() const { return results_.get(); } |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 61 | // Returns the command visibility. |
| 62 | const Visibility& GetVisibility() const { return visibility_; } |
| 63 | // Changes the command visibility. |
| 64 | void SetVisibility(const Visibility& visibility); |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 65 | // Returns the role required to execute command. |
| 66 | UserRole GetMinimalRole() const { return minimal_role_; } |
| 67 | // Changes the role required to execute command. |
| 68 | void SetMinimalRole(UserRole minimal_role) { minimal_role_ = minimal_role; } |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 71 | std::unique_ptr<const ObjectSchema> parameters_; // Command parameters def. |
Vitaly Buka | 4129dfa | 2015-04-29 12:16:58 -0700 | [diff] [blame] | 72 | std::unique_ptr<const ObjectSchema> progress_; // Command progress def. |
Vitaly Buka | a647c85 | 2015-07-06 14:51:01 -0700 | [diff] [blame] | 73 | std::unique_ptr<const ObjectSchema> results_; // Command results def. |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 74 | Visibility visibility_; // Available to all by default. |
Vitaly Buka | 6fed053 | 2015-05-14 16:57:23 -0700 | [diff] [blame] | 75 | // Minimal role required to execute command. |
| 76 | UserRole minimal_role_{UserRole::kUser}; |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 77 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 78 | DISALLOW_COPY_AND_ASSIGN(CommandDefinition); |
| 79 | }; |
| 80 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 81 | } // namespace weave |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 82 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 83 | #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_ |