Alex Vakulenko | b04936f | 2014-09-19 14:53: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/utils.h" |
| 6 | |
| 7 | #include <map> |
| 8 | #include <string> |
| 9 | |
| 10 | #include <base/files/file_util.h> |
| 11 | #include <base/json/json_reader.h> |
| 12 | #include <chromeos/errors/error_codes.h> |
| 13 | |
| 14 | namespace buffet { |
| 15 | |
| 16 | const char kErrorDomainBuffet[] = "buffet"; |
| 17 | const char kFileReadError[] = "file_read_error"; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 18 | const char kInvalidCategoryError[] = "invalid_category"; |
| 19 | const char kInvalidPackageError[] = "invalid_package"; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 20 | |
| 21 | std::unique_ptr<const base::DictionaryValue> LoadJsonDict( |
| 22 | const base::FilePath& json_file_path, chromeos::ErrorPtr* error) { |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 23 | std::string json_string; |
| 24 | if (!base::ReadFileToString(json_file_path, &json_string)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 25 | chromeos::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 26 | chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 27 | kFileReadError, |
| 28 | "Failed to read file '%s'", |
| 29 | json_file_path.value().c_str()); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 30 | return {}; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 31 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 32 | return LoadJsonDict(json_string, error); |
| 33 | } |
| 34 | |
| 35 | std::unique_ptr<const base::DictionaryValue> LoadJsonDict( |
| 36 | const std::string& json_string, chromeos::ErrorPtr* error) { |
| 37 | std::unique_ptr<const base::DictionaryValue> result; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 38 | std::string error_message; |
| 39 | base::Value* value = base::JSONReader::ReadAndReturnError( |
| 40 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 41 | if (!value) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 42 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 43 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 44 | chromeos::errors::json::kParseError, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 45 | "Error parsing JSON string '%s': %s", |
| 46 | json_string.c_str(), |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 47 | error_message.c_str()); |
| 48 | return result; |
| 49 | } |
| 50 | const base::DictionaryValue* dict_value = nullptr; |
| 51 | if (!value->GetAsDictionary(&dict_value)) { |
| 52 | delete value; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 53 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 54 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 55 | chromeos::errors::json::kObjectExpected, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 56 | "JSON string '%s' is not a JSON object", |
| 57 | json_string.c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 58 | return result; |
| 59 | } |
| 60 | result.reset(dict_value); |
| 61 | return result; |
| 62 | } |
| 63 | |
| 64 | } // namespace buffet |