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 | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 29 | const CommandDictionary& CommandManager::GetCommandDictionary() const { |
| 30 | return dictionary_; |
| 31 | } |
| 32 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 33 | bool CommandManager::LoadBaseCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 34 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 35 | return base_dictionary_.LoadCommands(json, "", nullptr, error); |
| 36 | } |
| 37 | |
| 38 | bool CommandManager::LoadBaseCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 39 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 40 | std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict( |
| 41 | json_file_path, error); |
| 42 | if (!json) |
| 43 | return false; |
| 44 | return LoadBaseCommands(*json, error); |
| 45 | } |
| 46 | |
| 47 | bool CommandManager::LoadCommands(const base::DictionaryValue& json, |
| 48 | const std::string& category, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 49 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 50 | return dictionary_.LoadCommands(json, category, &base_dictionary_, error); |
| 51 | } |
| 52 | |
| 53 | bool CommandManager::LoadCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 54 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 55 | std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict( |
| 56 | json_file_path, error); |
| 57 | if (!json) |
| 58 | return false; |
| 59 | std::string category = json_file_path.BaseName().RemoveExtension().value(); |
| 60 | return LoadCommands(*json, category, error); |
| 61 | } |
| 62 | |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 63 | void CommandManager::Startup() { |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 64 | LOG(INFO) << "Initializing CommandManager."; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 65 | // Load global standard GCD command dictionary. |
| 66 | base::FilePath base_command_file("/etc/buffet/gcd.json"); |
| 67 | LOG(INFO) << "Loading standard commands from " << base_command_file.value(); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 68 | CHECK(LoadBaseCommands(base_command_file, nullptr)) |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 69 | << "Failed to load the standard command definitions."; |
| 70 | |
| 71 | // Load static device command definitions. |
| 72 | base::FilePath device_command_dir("/etc/buffet/commands"); |
| 73 | base::FileEnumerator enumerator(device_command_dir, false, |
| 74 | base::FileEnumerator::FILES, |
| 75 | FILE_PATH_LITERAL("*.json")); |
| 76 | base::FilePath json_file_path = enumerator.Next(); |
| 77 | while (!json_file_path.empty()) { |
| 78 | LOG(INFO) << "Loading command schema from " << json_file_path.value(); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 79 | CHECK(LoadCommands(json_file_path, nullptr)) |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 80 | << "Failed to load the command definition file."; |
| 81 | json_file_path = enumerator.Next(); |
| 82 | } |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 83 | } |
| 84 | |
Alex Vakulenko | 9511075 | 2014-09-03 16:27:21 -0700 | [diff] [blame] | 85 | std::string CommandManager::AddCommand( |
| 86 | std::unique_ptr<CommandInstance> command_instance) { |
| 87 | return command_queue_.Add(std::move(command_instance)); |
| 88 | } |
| 89 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 90 | } // namespace buffet |