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_DICTIONARY_H_ |
| 6 | #define BUFFET_COMMANDS_COMMAND_DICTIONARY_H_ |
| 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> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 15 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 16 | |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 17 | #include "buffet/commands/command_definition.h" |
| 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 | |
| 24 | namespace buffet { |
| 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. |
| 33 | class CommandDictionary { |
| 34 | public: |
| 35 | CommandDictionary() = default; |
| 36 | |
| 37 | // Gets the list of names of commands that belong to the given category. |
| 38 | std::vector<std::string> GetCommandNamesByCategory( |
| 39 | const std::string& category) const; |
| 40 | |
| 41 | // Loads command definitions from a JSON object. This is done at the daemon |
| 42 | // startup and whenever a device daemon decides to update its command list. |
| 43 | // |json| is a JSON dictionary that describes the complete commands contained |
| 44 | // in a particular |category|. Usually, |categories| are 1:1 with daemons on |
| 45 | // a device. For instance, the power manager daemon might provide a category |
| 46 | // "power_man" that provides the "base.reboot" and "base.shutdown" commands. |
| 47 | // However, nothing prohibits a daemon providing commands in two categories. |
| 48 | // When LoadCommands is called, all previous definitions of commands from the |
| 49 | // same category are removed, effectively replacing all the commands in the |
| 50 | // given category. |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 51 | // Optional |base_commands| parameter specifies the definition of standard |
| 52 | // GCD commands for parameter schema validation. Can be set to nullptr if |
| 53 | // no validation is needed. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 54 | // Returns false on failure and |error| provides additional error information |
| 55 | // when provided. |
| 56 | bool LoadCommands(const base::DictionaryValue& json, |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 57 | const std::string& category, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 58 | const CommandDictionary* base_commands, |
| 59 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 60 | // Converts all the command definitions to a JSON object for CDD/Device |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 61 | // draft. |
| 62 | // |filter| is a predicate used to filter out the command definitions to |
| 63 | // be returned by this method. Only command definitions for which the |
| 64 | // predicate returns true will be included in the resulting JSON. |
| 65 | // |full_schema| specifies whether full command definitions must be generated |
| 66 | // (true) for CDD or only overrides from the base schema (false). |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 67 | // Returns empty unique_ptr in case of an error and fills in the additional |
| 68 | // error details in |error|. |
| 69 | std::unique_ptr<base::DictionaryValue> GetCommandsAsJson( |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 70 | const std::function<bool(const CommandDefinition*)>& filter, |
| 71 | bool full_schema, |
| 72 | chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 73 | // Returns the number of command definitions in the dictionary. |
| 74 | size_t GetSize() const { return definitions_.size(); } |
| 75 | // Checks if the dictionary has no command definitions. |
| 76 | bool IsEmpty() const { return definitions_.empty(); } |
| 77 | // Remove all the command definitions from the dictionary. |
| 78 | void Clear(); |
| 79 | // Finds a definition for the given command. |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 80 | const CommandDefinition* FindCommand(const std::string& command_name) const; |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 81 | CommandDefinition* FindCommand(const std::string& command_name); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 82 | |
| 83 | private: |
Alex Vakulenko | 5e86fee | 2015-04-17 08:47:45 -0700 | [diff] [blame] | 84 | using CommandMap = std::map<std::string, std::unique_ptr<CommandDefinition>>; |
Alex Vakulenko | 5ef7579 | 2015-03-19 15:50:44 -0700 | [diff] [blame] | 85 | |
| 86 | std::unique_ptr<ObjectSchema> BuildObjectSchema( |
Anton Muhin | 71fb9d5 | 2014-11-21 22:22:39 +0400 | [diff] [blame] | 87 | const base::DictionaryValue* command_def_json, |
| 88 | const char* property_name, |
| 89 | const ObjectSchema* base_def, |
| 90 | const std::string& command_name, |
| 91 | chromeos::ErrorPtr* error); |
| 92 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 93 | CommandMap definitions_; // List of all available command definitions. |
| 94 | DISALLOW_COPY_AND_ASSIGN(CommandDictionary); |
| 95 | }; |
| 96 | |
| 97 | } // namespace buffet |
| 98 | |
| 99 | #endif // BUFFET_COMMANDS_COMMAND_DICTIONARY_H_ |