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> |
| 10 | #include <string> |
| 11 | #include <vector> |
| 12 | |
Alex Vakulenko | 132617a | 2014-09-04 08:59:43 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 14 | #include <chromeos/errors/error.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 15 | |
| 16 | namespace base { |
| 17 | class Value; |
| 18 | class DictionaryValue; |
| 19 | } // namespace base |
| 20 | |
| 21 | namespace buffet { |
| 22 | |
| 23 | class CommandDefinition; |
| 24 | |
| 25 | // CommandDictionary is a wrapper around a map of command name and the |
| 26 | // corresponding command definition schema. The command name (the key in |
| 27 | // the map) is a compound name in a form of "package_name.command_name", |
| 28 | // where "package_name" is a name of command package such as "base", "printers", |
| 29 | // and others. So the full command name could be "base.reboot", for example. |
| 30 | class CommandDictionary { |
| 31 | public: |
| 32 | CommandDictionary() = default; |
| 33 | |
| 34 | // Gets the list of names of commands that belong to the given category. |
| 35 | std::vector<std::string> GetCommandNamesByCategory( |
| 36 | const std::string& category) const; |
| 37 | |
| 38 | // Loads command definitions from a JSON object. This is done at the daemon |
| 39 | // startup and whenever a device daemon decides to update its command list. |
| 40 | // |json| is a JSON dictionary that describes the complete commands contained |
| 41 | // in a particular |category|. Usually, |categories| are 1:1 with daemons on |
| 42 | // a device. For instance, the power manager daemon might provide a category |
| 43 | // "power_man" that provides the "base.reboot" and "base.shutdown" commands. |
| 44 | // However, nothing prohibits a daemon providing commands in two categories. |
| 45 | // When LoadCommands is called, all previous definitions of commands from the |
| 46 | // same category are removed, effectively replacing all the commands in the |
| 47 | // given category. |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 48 | // Optional |base_commands| parameter specifies the definition of standard |
| 49 | // GCD commands for parameter schema validation. Can be set to nullptr if |
| 50 | // no validation is needed. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 51 | // Returns false on failure and |error| provides additional error information |
| 52 | // when provided. |
| 53 | bool LoadCommands(const base::DictionaryValue& json, |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 54 | const std::string& category, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 55 | const CommandDictionary* base_commands, |
| 56 | chromeos::ErrorPtr* error); |
Alex Vakulenko | 4510944 | 2014-07-29 11:07:10 -0700 | [diff] [blame] | 57 | // Converts all the command definitions to a JSON object for CDD/Device |
| 58 | // draft. |full_schema| specifies whether full command definitions must |
| 59 | // be generated (true) for CDD or only overrides from the base schema (false). |
| 60 | // Returns empty unique_ptr in case of an error and fills in the additional |
| 61 | // error details in |error|. |
| 62 | std::unique_ptr<base::DictionaryValue> GetCommandsAsJson( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 63 | bool full_schema, chromeos::ErrorPtr* error) const; |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 64 | // Returns the number of command definitions in the dictionary. |
| 65 | size_t GetSize() const { return definitions_.size(); } |
| 66 | // Checks if the dictionary has no command definitions. |
| 67 | bool IsEmpty() const { return definitions_.empty(); } |
| 68 | // Remove all the command definitions from the dictionary. |
| 69 | void Clear(); |
| 70 | // Finds a definition for the given command. |
| 71 | const CommandDefinition* FindCommand(const std::string& command_name) const; |
| 72 | |
| 73 | private: |
| 74 | using CommandMap = std::map<std::string, |
| 75 | std::shared_ptr<const CommandDefinition>>; |
| 76 | |
| 77 | CommandMap definitions_; // List of all available command definitions. |
| 78 | DISALLOW_COPY_AND_ASSIGN(CommandDictionary); |
| 79 | }; |
| 80 | |
| 81 | } // namespace buffet |
| 82 | |
| 83 | #endif // BUFFET_COMMANDS_COMMAND_DICTIONARY_H_ |