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 | |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | // Truncates a string if it is too long. Used for error reporting with really |
| 19 | // long JSON strings. |
| 20 | std::string LimitString(const std::string& text, size_t max_len) { |
| 21 | if (text.size() <= max_len) |
| 22 | return text; |
| 23 | return text.substr(0, max_len - 3) + "..."; |
| 24 | } |
| 25 | |
| 26 | const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars. |
| 27 | |
| 28 | } // anonymous namespace |
| 29 | |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 30 | const char kErrorDomainBuffet[] = "buffet"; |
| 31 | const char kFileReadError[] = "file_read_error"; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 32 | const char kInvalidCategoryError[] = "invalid_category"; |
| 33 | const char kInvalidPackageError[] = "invalid_package"; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 34 | |
| 35 | std::unique_ptr<const base::DictionaryValue> LoadJsonDict( |
| 36 | const base::FilePath& json_file_path, chromeos::ErrorPtr* error) { |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 37 | std::string json_string; |
| 38 | if (!base::ReadFileToString(json_file_path, &json_string)) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 39 | chromeos::errors::system::AddSystemError(error, FROM_HERE, errno); |
| 40 | chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 41 | kFileReadError, |
| 42 | "Failed to read file '%s'", |
| 43 | json_file_path.value().c_str()); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 44 | return {}; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 45 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 46 | return LoadJsonDict(json_string, error); |
| 47 | } |
| 48 | |
| 49 | std::unique_ptr<const base::DictionaryValue> LoadJsonDict( |
| 50 | const std::string& json_string, chromeos::ErrorPtr* error) { |
| 51 | std::unique_ptr<const base::DictionaryValue> result; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 52 | std::string error_message; |
| 53 | base::Value* value = base::JSONReader::ReadAndReturnError( |
| 54 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 55 | if (!value) { |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 56 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 57 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 58 | chromeos::errors::json::kParseError, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 59 | "Error parsing JSON string '%s': %s", |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 60 | LimitString(json_string, kMaxStrLen).c_str(), |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 61 | error_message.c_str()); |
| 62 | return result; |
| 63 | } |
| 64 | const base::DictionaryValue* dict_value = nullptr; |
| 65 | if (!value->GetAsDictionary(&dict_value)) { |
| 66 | delete value; |
Alex Vakulenko | ac8037d | 2014-11-11 11:42:05 -0800 | [diff] [blame] | 67 | chromeos::Error::AddToPrintf(error, FROM_HERE, |
| 68 | chromeos::errors::json::kDomain, |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 69 | chromeos::errors::json::kObjectExpected, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 70 | "JSON string '%s' is not a JSON object", |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 71 | LimitString(json_string, kMaxStrLen).c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 72 | return result; |
| 73 | } |
| 74 | result.reset(dict_value); |
| 75 | return result; |
| 76 | } |
| 77 | |
| 78 | } // namespace buffet |