Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 1 | // 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_MANAGER_H_ |
| 6 | #define BUFFET_COMMANDS_COMMAND_MANAGER_H_ |
| 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> |
| 13 | #include <base/callback_list.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 14 | #include <base/files/file_path.h> |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 15 | #include <base/macros.h> |
| 16 | #include <base/memory/weak_ptr.h> |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 17 | |
| 18 | #include "buffet/commands/command_dictionary.h" |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 19 | #include "buffet/commands/command_queue.h" |
| 20 | #include "buffet/commands/dbus_command_dispatcher.h" |
| 21 | |
| 22 | namespace chromeos { |
| 23 | namespace dbus_utils { |
| 24 | class ExportedObjectManager; |
| 25 | } // namespace dbus_utils |
| 26 | } // namespace chromeos |
| 27 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 28 | |
| 29 | namespace buffet { |
| 30 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 31 | class CommandInstance; |
| 32 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 33 | // CommandManager class that will have a list of all the device command |
| 34 | // schemas as well as the live command queue of pending command instances |
| 35 | // dispatched to the device. |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 36 | class CommandManager final { |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 37 | public: |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 38 | // A token given by CommandManager in response to AddOnCommandDefChanged(). |
| 39 | // When the CallbackToken is destroyed, the registered notification |
| 40 | // callback associated with it will automatically be removed from the command |
| 41 | // manager's callback list. |
| 42 | using CallbackToken = |
| 43 | std::unique_ptr<base::CallbackList<void()>::Subscription>; |
| 44 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 45 | CommandManager(); |
| 46 | explicit CommandManager( |
| 47 | const base::WeakPtr<chromeos::dbus_utils::ExportedObjectManager>& |
| 48 | object_manager); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 49 | // Special constructor to help mock out command dispatcher for testing. |
| 50 | explicit CommandManager(CommandDispachInterface* dispatch_interface); |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 51 | |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 52 | // Sets callback which is called when command definitions is changed. |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame^] | 53 | CallbackToken AddOnCommandDefChanged( |
| 54 | const base::Closure& callback) WARN_UNUSED_RESULT { |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 55 | return CallbackToken{on_command_changed_.Add(callback).release()}; |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | // Returns the command definitions for the device. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 59 | const CommandDictionary& GetCommandDictionary() const; |
| 60 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 61 | // Loads base/standard GCD command definitions. |
| 62 | // |json| is the full JSON schema of standard GCD commands. These commands |
| 63 | // are not necessarily supported by a particular device but rather |
| 64 | // all the standard commands defined by GCD standard for all known/supported |
| 65 | // device kinds. |
| 66 | // On success, returns true. Otherwise, |error| contains additional |
| 67 | // error information. |
| 68 | bool LoadBaseCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 69 | chromeos::ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 70 | |
| 71 | // Same as the overload above, but takes a path to a json file to read |
| 72 | // the base command definitions from. |
| 73 | bool LoadBaseCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 74 | chromeos::ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 75 | |
| 76 | // Loads device command schema for particular category. |
| 77 | // See CommandDictionary::LoadCommands for detailed description of the |
| 78 | // parameters. |
| 79 | bool LoadCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 80 | const std::string& category, |
| 81 | chromeos::ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 82 | |
| 83 | // Same as the overload above, but takes a path to a json file to read |
| 84 | // the base command definitions from. Also, the command category is |
| 85 | // derived from file name (without extension). So, if the path points to |
| 86 | // "power_manager.json", the command category used will be "power_manager". |
| 87 | bool LoadCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 88 | chromeos::ErrorPtr* error); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 89 | |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 90 | // Startup method to be called by buffet daemon at startup. |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 91 | // Initializes the object and reads files in |definitions_path| to load |
| 92 | // 1) the standard GCD command dictionary |
| 93 | // 2) static vendor-provided command definitions |
| 94 | // If |test_definitions_path| is not empty, we'll also look there for |
| 95 | // additional commands. |
| 96 | void Startup(const base::FilePath& definitions_path, |
| 97 | const base::FilePath& test_definitions_path); |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 98 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 99 | // Adds a new command to the command queue. |
| 100 | void AddCommand(std::unique_ptr<CommandInstance> command_instance); |
| 101 | |
| 102 | // Finds a command by the command |id|. Returns nullptr if the command with |
| 103 | // the given |id| is not found. The returned pointer should not be persisted |
| 104 | // for a long period of time. |
| 105 | CommandInstance* FindCommand(const std::string& id) const; |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 106 | |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame^] | 107 | // Changes the visibility of commands. |
| 108 | bool SetCommandVisibility(const std::vector<std::string>& command_names, |
| 109 | CommandDefinition::Visibility visibility, |
| 110 | chromeos::ErrorPtr* error); |
| 111 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 112 | private: |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 113 | CommandDictionary base_dictionary_; // Base/std command definitions/schemas. |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 114 | CommandDictionary dictionary_; // Command definitions/schemas. |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 115 | CommandQueue command_queue_; |
| 116 | DBusCommandDispacher command_dispatcher_; |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 117 | base::CallbackList<void()> on_command_changed_; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 118 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 119 | DISALLOW_COPY_AND_ASSIGN(CommandManager); |
| 120 | }; |
| 121 | |
| 122 | } // namespace buffet |
| 123 | |
| 124 | #endif // BUFFET_COMMANDS_COMMAND_MANAGER_H_ |