blob: ea31476782bd25a58ae8d24dc0540065dff24b4a [file] [log] [blame]
Alex Vakulenkob04936f2014-09-19 14:53:58 -07001// 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 Buka912b6982015-07-06 11:13:03 -07005#include "libweave/src/utils.h"
Alex Vakulenkob04936f2014-09-19 14:53:58 -07006
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -07007#include <base/bind_helpers.h>
Alex Vakulenkob04936f2014-09-19 14:53:58 -07008#include <base/files/file_util.h>
9#include <base/json/json_reader.h>
Vitaly Bukaea2f15f2015-08-13 15:26:20 -070010
11#include "libweave/src/json_error_codes.h"
Alex Vakulenkob04936f2014-09-19 14:53:58 -070012
Vitaly Bukab6f015a2015-07-09 14:59:23 -070013namespace weave {
Alex Vakulenkob04936f2014-09-19 14:53:58 -070014
Alex Vakulenko36c85aa2015-04-09 09:06:39 -070015namespace {
16
17// Truncates a string if it is too long. Used for error reporting with really
18// long JSON strings.
19std::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
25const size_t kMaxStrLen = 1700; // Log messages are limited to 2000 chars.
26
27} // anonymous namespace
28
Vitaly Buka0fa51e52015-07-10 00:12:25 -070029const char kErrorDomain[] = "weave";
Alex Vakulenkob04936f2014-09-19 14:53:58 -070030const char kFileReadError[] = "file_read_error";
Alex Vakulenko07216fe2014-09-19 15:31:09 -070031const char kInvalidCategoryError[] = "invalid_category";
32const char kInvalidPackageError[] = "invalid_package";
Alex Vakulenkob04936f2014-09-19 14:53:58 -070033
Vitaly Buka207c1cb2015-05-14 17:06:18 -070034std::unique_ptr<base::DictionaryValue> LoadJsonDict(
35 const base::FilePath& json_file_path,
36 chromeos::ErrorPtr* error) {
Alex Vakulenkob04936f2014-09-19 14:53:58 -070037 std::string json_string;
38 if (!base::ReadFileToString(json_file_path, &json_string)) {
Vitaly Buka0fa51e52015-07-10 00:12:25 -070039 chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomain, kFileReadError,
40 "Failed to read file '%s'",
Alex Vakulenkob04936f2014-09-19 14:53:58 -070041 json_file_path.value().c_str());
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070042 return {};
Alex Vakulenkob04936f2014-09-19 14:53:58 -070043 }
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070044 return LoadJsonDict(json_string, error);
45}
46
Vitaly Buka207c1cb2015-05-14 17:06:18 -070047std::unique_ptr<base::DictionaryValue> LoadJsonDict(
48 const std::string& json_string,
49 chromeos::ErrorPtr* error) {
50 std::unique_ptr<base::DictionaryValue> result;
Alex Vakulenkob04936f2014-09-19 14:53:58 -070051 std::string error_message;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -070052 auto value = base::JSONReader::ReadAndReturnError(
Alex Vakulenkob04936f2014-09-19 14:53:58 -070053 json_string, base::JSON_PARSE_RFC, nullptr, &error_message);
54 if (!value) {
Vitaly Bukaea2f15f2015-08-13 15:26:20 -070055 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 Vakulenkob04936f2014-09-19 14:53:58 -070060 return result;
61 }
Vitaly Buka207c1cb2015-05-14 17:06:18 -070062 base::DictionaryValue* dict_value = nullptr;
Alex Vakulenkob04936f2014-09-19 14:53:58 -070063 if (!value->GetAsDictionary(&dict_value)) {
Vitaly Bukaea2f15f2015-08-13 15:26:20 -070064 chromeos::Error::AddToPrintf(error, FROM_HERE, errors::json::kDomain,
65 errors::json::kObjectExpected,
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070066 "JSON string '%s' is not a JSON object",
Alex Vakulenko36c85aa2015-04-09 09:06:39 -070067 LimitString(json_string, kMaxStrLen).c_str());
Alex Vakulenkob04936f2014-09-19 14:53:58 -070068 return result;
Alex Vakulenkoae1ffbc2015-06-15 12:53:22 -070069 } else {
70 // |value| is now owned by |dict_value|.
71 base::IgnoreResult(value.release());
Alex Vakulenkob04936f2014-09-19 14:53:58 -070072 }
73 result.reset(dict_value);
74 return result;
75}
76
Vitaly Bukab6f015a2015-07-09 14:59:23 -070077} // namespace weave