blob: 7668fbae409334e8bcbc01ee14c9d1893333313e [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>
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070010#include <set>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070011#include <string>
12#include <vector>
13
Alex Vakulenko132617a2014-09-04 08:59:43 -070014#include <base/macros.h>
Alex Vakulenkoa8b95bc2014-08-27 11:00:57 -070015#include <chromeos/errors/error.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070016
Alex Vakulenko5ef75792015-03-19 15:50:44 -070017#include "buffet/commands/command_definition.h"
18
Alex Vakulenko7c36b672014-07-16 14:50:58 -070019namespace base {
20class Value;
21class DictionaryValue;
22} // namespace base
23
24namespace buffet {
25
Anton Muhin71fb9d52014-11-21 22:22:39 +040026class ObjectSchema;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070027
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.
33class 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 Vakulenkofd448692014-07-22 07:46:53 -070051 // 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 Vakulenko7c36b672014-07-16 14:50:58 -070054 // Returns false on failure and |error| provides additional error information
55 // when provided.
56 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenkofd448692014-07-22 07:46:53 -070057 const std::string& category,
Alex Vakulenko5f472062014-08-14 17:54:04 -070058 const CommandDictionary* base_commands,
59 chromeos::ErrorPtr* error);
Alex Vakulenko45109442014-07-29 11:07:10 -070060 // Converts all the command definitions to a JSON object for CDD/Device
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070061 // 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 Vakulenko45109442014-07-29 11:07:10 -070067 // 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 Vakulenko9ea5a322015-04-17 15:35:34 -070070 const std::function<bool(const CommandDefinition*)>& filter,
71 bool full_schema,
72 chromeos::ErrorPtr* error) const;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070073 // 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 Vakulenko5ef75792015-03-19 15:50:44 -070080 const CommandDefinition* FindCommand(const std::string& command_name) const;
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070081 CommandDefinition* FindCommand(const std::string& command_name);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070082
83 private:
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070084 using CommandMap = std::map<std::string, std::unique_ptr<CommandDefinition>>;
Alex Vakulenko5ef75792015-03-19 15:50:44 -070085
86 std::unique_ptr<ObjectSchema> BuildObjectSchema(
Anton Muhin71fb9d52014-11-21 22:22:39 +040087 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 Vakulenko7c36b672014-07-16 14:50:58 -070093 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_