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