blob: 7cc8907a38cd3db4fd132a8e3bea9f23b4713b89 [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_MANAGER_H_
6#define LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_
Alex Vakulenko7c36b672014-07-16 14:50:58 -07007
Alex Vakulenko95110752014-09-03 16:27:21 -07008#include <memory>
Alex Vakulenkofd448692014-07-22 07:46:53 -07009#include <string>
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070010#include <vector>
Alex Vakulenkofd448692014-07-22 07:46:53 -070011
Alex Vakulenko9ea5a322015-04-17 15:35:34 -070012#include <base/callback.h>
Alex Vakulenko95110752014-09-03 16:27:21 -070013#include <base/macros.h>
14#include <base/memory/weak_ptr.h>
Alex Vakulenko7c36b672014-07-16 14:50:58 -070015
Stefan Sauer2d16dfa2015-09-25 17:08:35 +020016#include "src/commands/command_dictionary.h"
17#include "src/commands/command_queue.h"
Alex Vakulenko95110752014-09-03 16:27:21 -070018
Vitaly Bukab6f015a2015-07-09 14:59:23 -070019namespace weave {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070020
Alex Vakulenko95110752014-09-03 16:27:21 -070021class CommandInstance;
Vitaly Buka1e363672015-09-25 14:01:16 -070022
Alex Vakulenko7c36b672014-07-16 14:50:58 -070023// CommandManager class that will have a list of all the device command
24// schemas as well as the live command queue of pending command instances
25// dispatched to the device.
Vitaly Bukaa8ece8f2015-10-05 13:30:23 -070026class CommandManager final {
Alex Vakulenko7c36b672014-07-16 14:50:58 -070027 public:
Alex Vakulenko95110752014-09-03 16:27:21 -070028 CommandManager();
Alex Vakulenko7c36b672014-07-16 14:50:58 -070029
Vitaly Bukaa8ece8f2015-10-05 13:30:23 -070030 ~CommandManager();
Vitaly Buka8b4ab962015-07-14 19:19:39 -070031
Vitaly Buka8b4ab962015-07-14 19:19:39 -070032 bool AddCommand(const base::DictionaryValue& command,
Vitaly Buka8b4ab962015-07-14 19:19:39 -070033 std::string* id,
Vitaly Bukaa8ece8f2015-10-05 13:30:23 -070034 ErrorPtr* error);
35 CommandInstance* FindCommand(const std::string& id);
Vitaly Buka695a5fb2015-10-06 16:26:08 -070036 void AddCommandAddedCallback(const CommandQueue::CommandCallback& callback);
37 void AddCommandRemovedCallback(const CommandQueue::CommandCallback& callback);
38 void AddCommandHandler(const std::string& command_name,
Vitaly Bukac6029262015-10-07 09:29:13 -070039 const Device::CommandHandlerCallback& callback);
Vitaly Buka8b4ab962015-07-14 19:19:39 -070040
Vitaly Bukaaabadee2015-03-18 23:33:44 -070041 // Sets callback which is called when command definitions is changed.
Vitaly Buka553a7622015-10-05 13:53:20 -070042 void AddCommandDefChanged(const base::Closure& callback);
Vitaly Bukaaabadee2015-03-18 23:33:44 -070043
44 // Returns the command definitions for the device.
Alex Vakulenko7c36b672014-07-16 14:50:58 -070045 const CommandDictionary& GetCommandDictionary() const;
46
Alex Vakulenkofd448692014-07-22 07:46:53 -070047 // Same as the overload above, but takes a path to a json file to read
48 // the base command definitions from.
Vitaly Buka0464f262015-10-27 12:55:38 -070049 bool LoadStandardCommands(const std::string& json, ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070050
Vitaly Buka453c4dd2015-10-04 18:01:50 -070051 // Loads device command schema.
Vitaly Bukae2810e02015-08-16 23:31:55 -070052 bool LoadCommands(const base::DictionaryValue& dict,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070053 ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070054
55 // Same as the overload above, but takes a path to a json file to read
Vitaly Buka453c4dd2015-10-04 18:01:50 -070056 // the base command definitions from.
Vitaly Bukae2810e02015-08-16 23:31:55 -070057 bool LoadCommands(const std::string& json,
Vitaly Bukae2810e02015-08-16 23:31:55 -070058 ErrorPtr* error);
Alex Vakulenkofd448692014-07-22 07:46:53 -070059
Anton Muhin5191e812014-10-30 17:49:48 +040060 // Adds a new command to the command queue.
61 void AddCommand(std::unique_ptr<CommandInstance> command_instance);
Alex Vakulenko95110752014-09-03 16:27:21 -070062
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070063 // Changes the visibility of commands.
64 bool SetCommandVisibility(const std::vector<std::string>& command_names,
65 CommandDefinition::Visibility visibility,
Vitaly Buka0801a1f2015-08-14 10:03:46 -070066 ErrorPtr* error);
Alex Vakulenkoe03af6d2015-04-20 11:00:54 -070067
Vitaly Bukac4c67af2015-10-02 01:08:12 -070068 bool AddCommand(const base::DictionaryValue& command,
69 UserRole role,
70 std::string* id,
71 ErrorPtr* error);
72
Alex Vakulenko7c36b672014-07-16 14:50:58 -070073 private:
Vitaly Buka80d66532015-10-27 23:06:11 -070074 CommandDictionary dictionary_; // Registered definitions.
Vitaly Bukaae0f3a12015-05-11 16:27:30 -070075 CommandQueue command_queue_;
Vitaly Buka5e6ff6c2015-05-11 15:41:33 -070076 std::vector<base::Callback<void()>> on_command_changed_;
Vitaly Buka4547afa2015-06-09 09:46:53 -070077 uint32_t next_command_id_{0};
Alex Vakulenkoe4efaaf2014-07-22 08:08:44 -070078
Alex Vakulenko7c36b672014-07-16 14:50:58 -070079 DISALLOW_COPY_AND_ASSIGN(CommandManager);
80};
81
Vitaly Bukab6f015a2015-07-09 14:59:23 -070082} // namespace weave
Alex Vakulenko7c36b672014-07-16 14:50:58 -070083
Vitaly Buka912b6982015-07-06 11:13:03 -070084#endif // LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_