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 | #include "buffet/commands/command_manager.h" |
| 6 | |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 7 | #include <base/files/file_enumerator.h> |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 8 | #include <base/values.h> |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 9 | #include <chromeos/dbus/exported_object_manager.h> |
Alex Vakulenko | a8b95bc | 2014-08-27 11:00:57 -0700 | [diff] [blame] | 10 | #include <chromeos/errors/error.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 11 | |
| 12 | #include "buffet/commands/schema_constants.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 13 | #include "buffet/utils.h" |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 15 | using chromeos::dbus_utils::ExportedObjectManager; |
| 16 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 17 | namespace buffet { |
| 18 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 19 | CommandManager::CommandManager() { |
| 20 | command_queue_.SetCommandDispachInterface(&command_dispatcher_); |
| 21 | } |
| 22 | |
| 23 | CommandManager::CommandManager( |
| 24 | const base::WeakPtr<ExportedObjectManager>& object_manager) |
| 25 | : command_dispatcher_(object_manager->GetBus(), object_manager.get()) { |
| 26 | command_queue_.SetCommandDispachInterface(&command_dispatcher_); |
| 27 | } |
| 28 | |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 29 | CommandManager::CommandManager(CommandDispachInterface* dispatch_interface) { |
| 30 | command_queue_.SetCommandDispachInterface(dispatch_interface); |
| 31 | } |
| 32 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 33 | const CommandDictionary& CommandManager::GetCommandDictionary() const { |
| 34 | return dictionary_; |
| 35 | } |
| 36 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 37 | bool CommandManager::LoadBaseCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 38 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 39 | return base_dictionary_.LoadCommands(json, "", nullptr, error); |
| 40 | } |
| 41 | |
| 42 | bool CommandManager::LoadBaseCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 43 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 44 | std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict( |
| 45 | json_file_path, error); |
| 46 | if (!json) |
| 47 | return false; |
| 48 | return LoadBaseCommands(*json, error); |
| 49 | } |
| 50 | |
| 51 | bool CommandManager::LoadCommands(const base::DictionaryValue& json, |
| 52 | const std::string& category, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 53 | chromeos::ErrorPtr* error) { |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 54 | bool result = |
| 55 | dictionary_.LoadCommands(json, category, &base_dictionary_, error); |
Alex Vakulenko | 9ea5a32 | 2015-04-17 15:35:34 -0700 | [diff] [blame] | 56 | on_command_changed_.Notify(); |
Vitaly Buka | aabadee | 2015-03-18 23:33:44 -0700 | [diff] [blame] | 57 | return result; |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 58 | } |
| 59 | |
| 60 | bool CommandManager::LoadCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 61 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 62 | std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict( |
| 63 | json_file_path, error); |
| 64 | if (!json) |
| 65 | return false; |
| 66 | std::string category = json_file_path.BaseName().RemoveExtension().value(); |
| 67 | return LoadCommands(*json, category, error); |
| 68 | } |
| 69 | |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 70 | void CommandManager::Startup(const base::FilePath& definitions_path, |
| 71 | const base::FilePath& test_definitions_path) { |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 72 | LOG(INFO) << "Initializing CommandManager."; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 73 | // Load global standard GCD command dictionary. |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 74 | base::FilePath base_command_file{definitions_path.Append("gcd.json")}; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 75 | LOG(INFO) << "Loading standard commands from " << base_command_file.value(); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 76 | CHECK(LoadBaseCommands(base_command_file, nullptr)) |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 77 | << "Failed to load the standard command definitions."; |
| 78 | |
Christopher Wiley | bb5b848 | 2015-02-12 13:42:16 -0800 | [diff] [blame] | 79 | auto LoadPackages = [this](const base::FilePath& root, |
| 80 | const base::FilePath::StringType& pattern) { |
| 81 | base::FilePath device_command_dir{root.Append("commands")}; |
| 82 | VLOG(2) << "Looking for commands in " << root.value(); |
| 83 | base::FileEnumerator enumerator(device_command_dir, false, |
| 84 | base::FileEnumerator::FILES, |
| 85 | pattern); |
| 86 | base::FilePath json_file_path = enumerator.Next(); |
| 87 | while (!json_file_path.empty()) { |
| 88 | LOG(INFO) << "Loading command schema from " << json_file_path.value(); |
| 89 | CHECK(this->LoadCommands(json_file_path, nullptr)) |
| 90 | << "Failed to load the command definition file."; |
| 91 | json_file_path = enumerator.Next(); |
| 92 | } |
| 93 | }; |
| 94 | LoadPackages(definitions_path, FILE_PATH_LITERAL("*.json")); |
| 95 | LoadPackages(test_definitions_path, FILE_PATH_LITERAL("*test.json")); |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 96 | } |
| 97 | |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 98 | void CommandManager::AddCommand( |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 99 | std::unique_ptr<CommandInstance> command_instance) { |
Anton Muhin | 5191e81 | 2014-10-30 17:49:48 +0400 | [diff] [blame] | 100 | command_queue_.Add(std::move(command_instance)); |
| 101 | } |
| 102 | |
| 103 | CommandInstance* CommandManager::FindCommand(const std::string& id) const { |
| 104 | return command_queue_.Find(id); |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 105 | } |
| 106 | |
Alex Vakulenko | e03af6d | 2015-04-20 11:00:54 -0700 | [diff] [blame] | 107 | bool CommandManager::SetCommandVisibility( |
| 108 | const std::vector<std::string>& command_names, |
| 109 | CommandDefinition::Visibility visibility, |
| 110 | chromeos::ErrorPtr* error) { |
| 111 | if (command_names.empty()) |
| 112 | return true; |
| 113 | |
| 114 | std::vector<CommandDefinition*> definitions; |
| 115 | definitions.reserve(command_names.size()); |
| 116 | |
| 117 | // Find/validate command definitions first. |
| 118 | for (const std::string& name : command_names) { |
| 119 | CommandDefinition* def = dictionary_.FindCommand(name); |
| 120 | if (!def) { |
| 121 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::commands::kDomain, |
| 122 | errors::commands::kInvalidCommandName, |
| 123 | "Command '%s' is unknown", name.c_str()); |
| 124 | return false; |
| 125 | } |
| 126 | definitions.push_back(def); |
| 127 | } |
| 128 | |
| 129 | // Now that we know that all the command names were valid, |
| 130 | // update the respective commands' visibility. |
| 131 | for (CommandDefinition* def : definitions) { |
| 132 | def->SetVisibility(visibility); |
| 133 | } |
| 134 | on_command_changed_.Notify(); |
| 135 | return true; |
| 136 | } |
| 137 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 138 | } // namespace buffet |