blob: 8d3d45cd3ea683f5383b376fb6a7abf132ea946e [file] [log] [blame]
Vitaly Buka4615e0d2015-10-14 15:35:12 -07001// Copyright 2015 The Weave Authors. All rights reserved.
Alex Vakulenko7c36b672014-07-16 14:50:58 -07002// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
Vitaly Buka912b6982015-07-06 11:13:03 -07005#ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_
6#define LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_
Alex Vakulenko7c36b672014-07-16 14:50:58 -07007
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>
Vitaly Buka0801a1f2015-08-14 10:03:46 -070015#include <weave/error.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070016
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020017#include "src/commands/command_definition.h"
Alex Vakulenko5ef75792015-03-19 15:50:44 -070018
Alex Vakulenko7c36b672014-07-16 14:50:58 -070019namespace base {
20class Value;
21class DictionaryValue;
22} // namespace base
23
Vitaly Bukab6f015a2015-07-09 14:59:23 -070024namespace weave {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070025
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.
Alex Vakulenko534a3122015-05-22 15:48:53 -070033class CommandDictionary final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070034 public:
35 CommandDictionary() = default;
36
Alex Vakulenko7c36b672014-07-16 14:50:58 -070037 // Loads command definitions from a JSON object. This is done at the daemon
38 // startup and whenever a device daemon decides to update its command list.
Vitaly Buka453c4dd2015-10-04 18:01:50 -070039 // |json| is a JSON dictionary that describes the complete commands. Optional
40 // |base_commands| parameter specifies the definition of standard GCD commands
41 // for parameter schema validation. Can be set to nullptr if no validation is
42 // needed. Returns false on failure and |error| provides additional error
43 // information when provided.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070044 bool LoadCommands(const base::DictionaryValue& json,
Alex Vakulenko5f472062014-08-14 17:54:04 -070045 const CommandDictionary* base_commands,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070046 ErrorPtr* error);
Alex Vakulenko45109442014-07-29 11:07:10 -070047 // Converts all the command definitions to a JSON object for CDD/Device
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070048 // draft.
49 // |filter| is a predicate used to filter out the command definitions to
50 // be returned by this method. Only command definitions for which the
51 // predicate returns true will be included in the resulting JSON.
52 // |full_schema| specifies whether full command definitions must be generated
53 // (true) for CDD or only overrides from the base schema (false).
Alex Vakulenko45109442014-07-29 11:07:10 -070054 // Returns empty unique_ptr in case of an error and fills in the additional
55 // error details in |error|.
56 std::unique_ptr<base::DictionaryValue> GetCommandsAsJson(
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070057 const std::function<bool(const CommandDefinition*)>& filter,
58 bool full_schema,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070059 ErrorPtr* error) const;
Alex Vakulenko7c36b672014-07-16 14:50:58 -070060 // Returns the number of command definitions in the dictionary.
61 size_t GetSize() const { return definitions_.size(); }
62 // Checks if the dictionary has no command definitions.
63 bool IsEmpty() const { return definitions_.empty(); }
64 // Remove all the command definitions from the dictionary.
65 void Clear();
66 // Finds a definition for the given command.
Alex Vakulenko5ef75792015-03-19 15:50:44 -070067 const CommandDefinition* FindCommand(const std::string& command_name) const;
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070068 CommandDefinition* FindCommand(const std::string& command_name);
Alex Vakulenko7c36b672014-07-16 14:50:58 -070069
70 private:
Alex Vakulenko5e86fee2015-04-17 08:47:45 -070071 using CommandMap = std::map<std::string, std::unique_ptr<CommandDefinition>>;
Alex Vakulenko5ef75792015-03-19 15:50:44 -070072
73 std::unique_ptr<ObjectSchema> BuildObjectSchema(
Anton Muhin71fb9d52014-11-21 22:22:39 +040074 const base::DictionaryValue* command_def_json,
75 const char* property_name,
76 const ObjectSchema* base_def,
77 const std::string& command_name,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070078 ErrorPtr* error);
Anton Muhin71fb9d52014-11-21 22:22:39 +040079
Alex Vakulenko7c36b672014-07-16 14:50:58 -070080 CommandMap definitions_; // List of all available command definitions.
81 DISALLOW_COPY_AND_ASSIGN(CommandDictionary);
82};
83
Vitaly Bukab6f015a2015-07-09 14:59:23 -070084} // namespace weave
Alex Vakulenko7c36b672014-07-16 14:50:58 -070085
Vitaly Buka912b6982015-07-06 11:13:03 -070086#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_DICTIONARY_H_