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 | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 7 | #include <base/file_util.h> |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 8 | #include <base/files/file_enumerator.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 9 | #include <base/json/json_reader.h> |
Alex Vakulenko | 5841c30 | 2014-07-23 10:49:49 -0700 | [diff] [blame] | 10 | #include <base/values.h> |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 11 | #include <chromeos/error.h> |
| 12 | #include <chromeos/error_codes.h> |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 13 | |
| 14 | #include "buffet/commands/schema_constants.h" |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 15 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 16 | namespace buffet { |
| 17 | |
| 18 | const CommandDictionary& CommandManager::GetCommandDictionary() const { |
| 19 | return dictionary_; |
| 20 | } |
| 21 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 22 | bool CommandManager::LoadBaseCommands(const base::DictionaryValue& json, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 23 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 24 | return base_dictionary_.LoadCommands(json, "", nullptr, error); |
| 25 | } |
| 26 | |
| 27 | bool CommandManager::LoadBaseCommands(const base::FilePath& json_file_path, |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 28 | chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 29 | std::unique_ptr<const base::DictionaryValue> json = LoadJsonDict( |
| 30 | json_file_path, error); |
| 31 | if (!json) |
| 32 | return false; |
| 33 | return LoadBaseCommands(*json, error); |
| 34 | } |
| 35 | |
| 36 | bool CommandManager::LoadCommands(const base::DictionaryValue& json, |
| 37 | const std::string& category, |
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 dictionary_.LoadCommands(json, category, &base_dictionary_, error); |
| 40 | } |
| 41 | |
| 42 | bool CommandManager::LoadCommands(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 | std::string category = json_file_path.BaseName().RemoveExtension().value(); |
| 49 | return LoadCommands(*json, category, error); |
| 50 | } |
| 51 | |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 52 | void CommandManager::Startup() { |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 53 | LOG(INFO) << "Initializing CommandManager."; |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 54 | // Load global standard GCD command dictionary. |
| 55 | base::FilePath base_command_file("/etc/buffet/gcd.json"); |
| 56 | LOG(INFO) << "Loading standard commands from " << base_command_file.value(); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 57 | CHECK(LoadBaseCommands(base_command_file, nullptr)) |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 58 | << "Failed to load the standard command definitions."; |
| 59 | |
| 60 | // Load static device command definitions. |
| 61 | base::FilePath device_command_dir("/etc/buffet/commands"); |
| 62 | base::FileEnumerator enumerator(device_command_dir, false, |
| 63 | base::FileEnumerator::FILES, |
| 64 | FILE_PATH_LITERAL("*.json")); |
| 65 | base::FilePath json_file_path = enumerator.Next(); |
| 66 | while (!json_file_path.empty()) { |
| 67 | LOG(INFO) << "Loading command schema from " << json_file_path.value(); |
Alex Vakulenko | c2bc9a4 | 2014-07-23 10:57:58 -0700 | [diff] [blame] | 68 | CHECK(LoadCommands(json_file_path, nullptr)) |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 69 | << "Failed to load the command definition file."; |
| 70 | json_file_path = enumerator.Next(); |
| 71 | } |
Alex Vakulenko | e4efaaf | 2014-07-22 08:08:44 -0700 | [diff] [blame] | 72 | } |
| 73 | |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 74 | std::unique_ptr<const base::DictionaryValue> CommandManager::LoadJsonDict( |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 75 | const base::FilePath& json_file_path, chromeos::ErrorPtr* error) { |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 76 | std::string json_string; |
| 77 | if (!base::ReadFileToString(json_file_path, &json_string)) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 78 | chromeos::Error::AddToPrintf(error, chromeos::errors::file_system::kDomain, |
| 79 | chromeos::errors::file_system::kFileReadError, |
| 80 | "Failed to read file '%s'", |
| 81 | json_file_path.value().c_str()); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 82 | return std::unique_ptr<const base::DictionaryValue>(); |
| 83 | } |
| 84 | std::string error_message; |
| 85 | base::Value* value = base::JSONReader::ReadAndReturnError( |
| 86 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 87 | if (!value) { |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 88 | chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, |
| 89 | chromeos::errors::json::kParseError, |
| 90 | "Error parsing content of JSON file '%s': %s", |
| 91 | json_file_path.value().c_str(), |
| 92 | error_message.c_str()); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 93 | return std::unique_ptr<const base::DictionaryValue>(); |
| 94 | } |
| 95 | const base::DictionaryValue* dict_value = nullptr; |
| 96 | if (!value->GetAsDictionary(&dict_value)) { |
| 97 | delete value; |
Alex Vakulenko | 5f47206 | 2014-08-14 17:54:04 -0700 | [diff] [blame] | 98 | chromeos::Error::AddToPrintf(error, chromeos::errors::json::kDomain, |
| 99 | chromeos::errors::json::kObjectExpected, |
| 100 | "Content of file '%s' is not a JSON object", |
| 101 | json_file_path.value().c_str()); |
Alex Vakulenko | fd44869 | 2014-07-22 07:46:53 -0700 | [diff] [blame] | 102 | return std::unique_ptr<const base::DictionaryValue>(); |
| 103 | } |
| 104 | return std::unique_ptr<const base::DictionaryValue>(dict_value); |
| 105 | } |
| 106 | |
Alex Vakulenko | 7c36b67 | 2014-07-16 14:50:58 -0700 | [diff] [blame] | 107 | } // namespace buffet |