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 | |
Vitaly Buka | 912b698 | 2015-07-06 11:13:03 -0700 | [diff] [blame] | 5 | #include "libweave/src/utils.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 6 | |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 7 | #include <base/bind_helpers.h> |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 8 | #include <base/files/file_util.h> |
| 9 | #include <base/json/json_reader.h> |
Vitaly Buka | ea2f15f | 2015-08-13 15:26:20 -0700 | [diff] [blame^] | 10 | |
| 11 | #include "libweave/src/json_error_codes.h" |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 12 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 13 | namespace weave { |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 14 | |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 15 | namespace { |
| 16 | |
| 17 | // Truncates a string if it is too long. Used for error reporting with really |
| 18 | // long JSON strings. |
| 19 | std::string LimitString(const std::string& text, size_t max_len) { |
| 20 | if (text.size() <= max_len) |
| 21 | return text; |
| 22 | return text.substr(0, max_len - 3) + "..."; |
| 23 | } |
| 24 | |
| 25 | const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars. |
| 26 | |
| 27 | } // anonymous namespace |
| 28 | |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 29 | const char kErrorDomain[] = "weave"; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 30 | const char kFileReadError[] = "file_read_error"; |
Alex Vakulenko | 07216fe | 2014-09-19 15:31:09 -0700 | [diff] [blame] | 31 | const char kInvalidCategoryError[] = "invalid_category"; |
| 32 | const char kInvalidPackageError[] = "invalid_package"; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 33 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 34 | std::unique_ptr<base::DictionaryValue> LoadJsonDict( |
| 35 | const base::FilePath& json_file_path, |
| 36 | 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)) { |
Vitaly Buka | 0fa51e5 | 2015-07-10 00:12:25 -0700 | [diff] [blame] | 39 | chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError, |
| 40 | "Failed to read file '%s'", |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 41 | json_file_path.value().c_str()); |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 42 | return {}; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 43 | } |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 44 | return LoadJsonDict(json_string, error); |
| 45 | } |
| 46 | |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 47 | std::unique_ptr<base::DictionaryValue> LoadJsonDict( |
| 48 | const std::string& json_string, |
| 49 | chromeos::ErrorPtr* error) { |
| 50 | std::unique_ptr<base::DictionaryValue> result; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 51 | std::string error_message; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 52 | auto value = base::JSONReader::ReadAndReturnError( |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 53 | json_string, base::JSON_PARSE_RFC, nullptr, &error_message); |
| 54 | if (!value) { |
Vitaly Buka | ea2f15f | 2015-08-13 15:26:20 -0700 | [diff] [blame^] | 55 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain, |
| 56 | errors::json::kParseError, |
| 57 | "Error parsing JSON string '%s' (%zu): %s", |
| 58 | LimitString(json_string, kMaxStrLen).c_str(), |
| 59 | json_string.size(), error_message.c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 60 | return result; |
| 61 | } |
Vitaly Buka | 207c1cb | 2015-05-14 17:06:18 -0700 | [diff] [blame] | 62 | base::DictionaryValue* dict_value = nullptr; |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 63 | if (!value->GetAsDictionary(&dict_value)) { |
Vitaly Buka | ea2f15f | 2015-08-13 15:26:20 -0700 | [diff] [blame^] | 64 | chromeos::Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain, |
| 65 | errors::json::kObjectExpected, |
Alex Vakulenko | 9e2f8cd | 2015-04-07 16:28:09 -0700 | [diff] [blame] | 66 | "JSON string '%s' is not a JSON object", |
Alex Vakulenko | 36c85aa | 2015-04-09 09:06:39 -0700 | [diff] [blame] | 67 | LimitString(json_string, kMaxStrLen).c_str()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 68 | return result; |
Alex Vakulenko | ae1ffbc | 2015-06-15 12:53:22 -0700 | [diff] [blame] | 69 | } else { |
| 70 | // |value| is now owned by |dict_value|. |
| 71 | base::IgnoreResult(value.release()); |
Alex Vakulenko | b04936f | 2014-09-19 14:53:58 -0700 | [diff] [blame] | 72 | } |
| 73 | result.reset(dict_value); |
| 74 | return result; |
| 75 | } |
| 76 | |
Vitaly Buka | b6f015a | 2015-07-09 14:59:23 -0700 | [diff] [blame] | 77 | } // namespace weave |