blob: 3bcc07f0a14bfa44fb1ad6ee4fbcdc7f1f6fde85 [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko7c36b672014-07-16 14:50:58 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_
6#define LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_
Alex Vakulenko7c36b672014-07-16 14:50:58 -07007
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
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020013#include "src/commands/object_schema.h"
Alex Vakulenko7c36b672014-07-16 14:50:58 -070014
Vitaly Bukab6f015a2015-07-09 14:59:23 -070015namespace weave {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070016
Vitaly Bukac4c67af2015-10-02 01:08:12 -070017enum class UserRole {
18 kViewer,
19 kUser,
20 kManager,
21 kOwner,
22};
23
Vitaly Buka453c4dd2015-10-04 18:01:50 -070024// A simple GCD command definition. This class contains the full object schema
25// describing the command parameter types and constraints.
Alex Vakulenko534a3122015-05-22 15:48:53 -070026class CommandDefinition final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070027 public:
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070028 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 Buka0801a1f2015-08-14 10:03:46 -070037 bool FromString(const std::string& str, ErrorPtr* error);
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070038
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 Buka453c4dd2015-10-04 18:01:50 -070051 CommandDefinition(std::unique_ptr<const ObjectSchema> parameters,
Vitaly Buka4129dfa2015-04-29 12:16:58 -070052 std::unique_ptr<const ObjectSchema> progress,
Alex Vakulenko5ef75792015-03-19 15:50:44 -070053 std::unique_ptr<const ObjectSchema> results);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070054
Alex Vakulenko7c36b672014-07-16 14:50:58 -070055 // Gets the object schema for command parameters.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070056 const ObjectSchema* GetParameters() const { return parameters_.get(); }
Vitaly Buka4129dfa2015-04-29 12:16:58 -070057 // Gets the object schema for command progress.
58 const ObjectSchema* GetProgress() const { return progress_.get(); }
Anton Muhin71fb9d52014-11-21 22:22:39 +040059 // Gets the object schema for command results.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070060 const ObjectSchema* GetResults() const { return results_.get(); }
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070061 // Returns the command visibility.
62 const Visibility& GetVisibility() const { return visibility_; }
63 // Changes the command visibility.
64 void SetVisibility(const Visibility& visibility);
Vitaly Buka6fed0532015-05-14 16:57:23 -070065 // 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 Vakulenko7c36b672014-07-16 14:50:58 -070069
70 private:
Alex Vakulenko5ef75792015-03-19 15:50:44 -070071 std::unique_ptr<const ObjectSchema> parameters_; // Command parameters def.
Vitaly Buka4129dfa2015-04-29 12:16:58 -070072 std::unique_ptr<const ObjectSchema> progress_; // Command progress def.
Vitaly Bukaa647c852015-07-06 14:51:01 -070073 std::unique_ptr<const ObjectSchema> results_; // Command results def.
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070074 Visibility visibility_; // Available to all by default.
Vitaly Buka6fed0532015-05-14 16:57:23 -070075 // Minimal role required to execute command.
76 UserRole minimal_role_{UserRole::kUser};
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070077
Alex Vakulenko7c36b672014-07-16 14:50:58 -070078 DISALLOW_COPY_AND_ASSIGN(CommandDefinition);
79};
80
Vitaly Bukab6f015a2015-07-09 14:59:23 -070081} // namespace weave
Alex Vakulenko7c36b672014-07-16 14:50:58 -070082
Vitaly Buka912b6982015-07-06 11:13:03 -070083#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_DEFINITION_H_