blob: bd6098631b0566bc273372374ea3636d644775f8 [file] [log] [blame]
Alex Vakulenko7c36b672014-07-16 14:50:58 -07001// 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 Vakulenko132617a2014-09-04 08:59:43 -070013#include <base/macros.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070014#include <chromeos/errors/error.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070015
16namespace base {
17class Value;
18class DictionaryValue;
19} // namespace base
20
21namespace buffet {
22
23class 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.
30class 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 Vakulenkofd448692014-07-22 07:46:53 -070048 // 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 Vakulenko7c36b672014-07-16 14:50:58 -070051 // Returns false on failure and |error| provides additional error information
52 // when provided.
53 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenkofd448692014-07-22 07:46:53 -070054 const std::string& category,
Alex Vakulenko5f472062014-08-14 17:54:04 -070055 const CommandDictionary* base_commands,
56 chromeos::ErrorPtr* error);
Alex Vakulenko45109442014-07-29 11:07:10 -070057 // 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 Vakulenko5f472062014-08-14 17:54:04 -070063 bool full_schema, chromeos::ErrorPtr* error) const;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070064 // 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_