blob: b65668488d035fdc8cfa198e82b474e2bdd11130 [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
13#include <base/basictypes.h>
14
15#include "buffet/error.h"
16
17namespace base {
18class Value;
19class DictionaryValue;
20} // namespace base
21
22namespace buffet {
23
24class 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.
31class 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 Vakulenkofd448692014-07-22 07:46:53 -070049 // 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 Vakulenko7c36b672014-07-16 14:50:58 -070052 // Returns false on failure and |error| provides additional error information
53 // when provided.
54 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenkofd448692014-07-22 07:46:53 -070055 const std::string& category,
56 const CommandDictionary* base_commands, 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(
63 bool full_schema, 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_