Vitaly Buka | 4615e0d | 2015-10-14 15:35:12 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Weave Authors. All rights reserved. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #ifndef LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_ |
| 6 | #define LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_ |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 7 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 8 | #include <memory> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 9 | #include <string> |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 10 | #include <vector> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 11 | |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 12 | #include <base/callback.h> |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 13 | #include <base/macros.h> |
| 14 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 15 | |
Stefan Sauer | 2d16dfa | 2015-09-25 17:08:35 +0200 | [diff] [blame] | 16 | #include "src/commands/command_dictionary.h" |
| 17 | #include "src/commands/command_queue.h" |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 18 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 19 | namespace weave { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 20 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 21 | class CommandInstance; |
Vitaly Buka | 1e36367 | 2015-09-25 14:01:16 -0700 | [diff] [blame] | 22 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 23 | // 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 Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 26 | class CommandManager final { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 27 | public: |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 28 | CommandManager(); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 29 | |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 30 | ~CommandManager(); |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 31 | |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 32 | bool AddCommand(const base::DictionaryValue& command, |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 33 | std::string* id, |
Vitaly Buka | a8ece8f | 2015-10-05 13:30:23 -0700 | [diff] [blame] | 34 | ErrorPtr* error); |
| 35 | CommandInstance* FindCommand(const std::string& id); |
Vitaly Buka | 695a5fb | 2015-10-06 16:26:08 -0700 | [diff] [blame] | 36 | void AddCommandAddedCallback(const CommandQueue::CommandCallback& callback); |
| 37 | void AddCommandRemovedCallback(const CommandQueue::CommandCallback& callback); |
| 38 | void AddCommandHandler(const std::string& command_name, |
Vitaly Buka | c602926 | 2015-10-07 09:29:13 -0700 | [diff] [blame] | 39 | const Device::CommandHandlerCallback& callback); |
Vitaly Buka | 8b4ab96 | 2015-07-14 19:19:39 -0700 | [diff] [blame] | 40 | |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 41 | // Sets callback which is called when command definitions is changed. |
Vitaly Buka | 553a762 | 2015-10-05 13:53:20 -0700 | [diff] [blame] | 42 | void AddCommandDefChanged(const base::Closure& callback); |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 43 | |
| 44 | // Returns the command definitions for the device. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 45 | const CommandDictionary& GetCommandDictionary() const; |
| 46 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 47 | // Same as the overload above, but takes a path to a json file to read |
| 48 | // the base command definitions from. |
Vitaly Buka | 0464f26 | 2015-10-27 12:55:38 -0700 | [diff] [blame] | 49 | bool LoadStandardCommands(const std::string& json, ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 50 | |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 51 | // Loads device command schema. |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 52 | bool LoadCommands(const base::DictionaryValue& dict, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 53 | ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 54 | |
| 55 | // Same as the overload above, but takes a path to a json file to read |
Vitaly Buka | 453c4dd | 2015-10-04 18:01:50 -0700 | [diff] [blame] | 56 | // the base command definitions from. |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 57 | bool LoadCommands(const std::string& json, |
Vitaly Buka | e2810e0 | 2015-08-16 23:31:55 -0700 | [diff] [blame] | 58 | ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 59 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 60 | // Adds a new command to the command queue. |
| 61 | void AddCommand(std::unique_ptr<CommandInstance> command_instance); |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 62 | |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 63 | // Changes the visibility of commands. |
| 64 | bool SetCommandVisibility(const std::vector<std::string>& command_names, |
| 65 | CommandDefinition::Visibility visibility, |
Vitaly Buka | 0801a1f | 2015-08-14 10:03:46 -0700 | [diff] [blame] | 66 | ErrorPtr* error); |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 67 | |
Vitaly Buka | c4c67af | 2015-10-02 01:08:12 -0700 | [diff] [blame] | 68 | bool AddCommand(const base::DictionaryValue& command, |
| 69 | UserRole role, |
| 70 | std::string* id, |
| 71 | ErrorPtr* error); |
| 72 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 73 | private: |
Vitaly Buka | 80d6653 | 2015-10-27 23:06:11 -0700 | [diff] [blame] | 74 | CommandDictionary dictionary_; // Registered definitions. |
Vitaly Buka | ae0f3a1 | 2015-05-11 16:27:30 -0700 | [diff] [blame] | 75 | CommandQueue command_queue_; |
Vitaly Buka | 5e6ff6c | 2015-05-11 15:41:33 -0700 | [diff] [blame] | 76 | std::vector<base::Callback<void()>> on_command_changed_; |
Vitaly Buka | 4547afa | 2015-06-09 09:46:53 -0700 | [diff] [blame] | 77 | uint32_t next_command_id_{0}; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 78 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 79 | DISALLOW_COPY_AND_ASSIGN(CommandManager); |
| 80 | }; |
| 81 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 82 | } // namespace weave |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 83 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 84 | #endif // LIBWEAVE_SRC_COMMANDS_COMMAND_MANAGER_H_ |