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_DICTIONARY_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_ |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 7 | |
| 8 | #include <map> |
| 9 | #include <memory> |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 10 | #include <set> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 11 | #include <string> |
| 12 | #include <vector> |
| 13 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 14 | #include <base/macros.h> |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 15 | #include <weave/error.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 16 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 17 | #include "src/commands/command_definition.h" |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 18 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 19 | namespace base { |
| 20 | class Value; |
| 21 | class DictionaryValue; |
| 22 | } // namespace base |
| 23 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 24 | namespace weave { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 25 | |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 26 | class ObjectSchema; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 27 | |
| 28 | // CommandDictionary is a wrapper around a map of command name and the |
| 29 | // corresponding command definition schema. The command name (the key in |
| 30 | // the map) is a compound name in a form of "package_name.command_name", |
| 31 | // where "package_name" is a name of command package such as "base", "printers", |
| 32 | // and others. So the full command name could be "base.reboot", for example. |
Alex Vakulenko | 534a312 | 2015-05-22 15:48:53 -0700 | [diff] [blame] | 33 | class CommandDictionary final { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 34 | public: |
| 35 | CommandDictionary() = default; |
| 36 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 37 | // Loads command definitions from a JSON object. This is done at the daemon |
| 38 | // startup and whenever a device daemon decides to update its command list. |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 39 | // |json| is a JSON dictionary that describes the complete commands. Optional |
| 40 | // |base_commands| parameter specifies the definition of standard GCD commands |
| 41 | // for parameter schema validation. Can be set to nullptr if no validation is |
| 42 | // needed. Returns false on failure and |error| provides additional error |
| 43 | // information when provided. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 44 | bool LoadCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 45 | const CommandDictionary* base_commands, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 46 | ErrorPtr* error); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 47 | // Converts all the command definitions to a JSON object for CDD/Device |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 48 | // draft. |
| 49 | // |filter| is a predicate used to filter out the command definitions to |
| 50 | // be returned by this method. Only command definitions for which the |
| 51 | // predicate returns true will be included in the resulting JSON. |
| 52 | // |full_schema| specifies whether full command definitions must be generated |
| 53 | // (true) for CDD or only overrides from the base schema (false). |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 54 | // Returns empty unique_ptr in case of an error and fills in the additional |
| 55 | // error details in |error|. |
| 56 | std::unique_ptr<base::DictionaryValue> GetCommandsAsJson( |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 57 | const std::function<bool(const CommandDefinition*)>& filter, |
| 58 | bool full_schema, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 59 | ErrorPtr* error) const; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 60 | // Returns the number of command definitions in the dictionary. |
| 61 | size_t GetSize() const { return definitions_.size(); } |
| 62 | // Checks if the dictionary has no command definitions. |
| 63 | bool IsEmpty() const { return definitions_.empty(); } |
| 64 | // Remove all the command definitions from the dictionary. |
| 65 | void Clear(); |
| 66 | // Finds a definition for the given command. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 67 | const CommandDefinition* FindCommand(const std::string& command_name) const; |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 68 | CommandDefinition* FindCommand(const std::string& command_name); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 69 | |
| 70 | private: |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 71 | using CommandMap = std::map<std::string, std::unique_ptr<CommandDefinition>>; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 72 | |
| 73 | std::unique_ptr<ObjectSchema> BuildObjectSchema( |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 74 | const base::DictionaryValue* command_def_json, |
| 75 | const char* property_name, |
| 76 | const ObjectSchema* base_def, |
| 77 | const std::string& command_name, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 78 | ErrorPtr* error); |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 79 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 80 | CommandMap definitions_; // List of all available command definitions. |
| 81 | DISALLOW_COPY_AND_ASSIGN(CommandDictionary); |
| 82 | }; |
| 83 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 84 | } // namespace weave |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 85 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 86 | #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_ |