blob: 8e29ebeae83fef20a129c991a36218de2e08b65f [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
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
14namespace buffet {
15
16const char kErrorDomainBuffet[] = "buffet";
17const char kFileReadError[] = "file_read_error";
Alex Vakulenko07216fe2014-09-19 15:31:09 -070018const char kInvalidCategoryError[] = "invalid_category";
19const char kInvalidPackageError[] = "invalid_package";
Alex Vakulenkob04936f2014-09-19 14:53:58 -070020
21std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
22 const base::FilePath& json_file_path, chromeos::ErrorPtr* error) {
Alex Vakulenkob04936f2014-09-19 14:53:58 -070023 std::string json_string;
24 if (!base::ReadFileToString(json_file_path, &json_string)) {
Alex Vakulenkoac8037d2014-11-11 11:42:05 -080025 chromeos::errors::system::AddSystemError(error, FROM_HERE, errno);
26 chromeos::Error::AddToPrintf(error, FROM_HERE, kErrorDomainBuffet,
Alex Vakulenkob04936f2014-09-19 14:53:58 -070027 kFileReadError,
28 "Failed to read file '%s'",
29 json_file_path.value().c_str());
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070030 return {};
Alex Vakulenkob04936f2014-09-19 14:53:58 -070031 }
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070032 return LoadJsonDict(json_string, error);
33}
34
35std::unique_ptr<const base::DictionaryValue> LoadJsonDict(
36 const std::string& json_string, chromeos::ErrorPtr* error) {
37 std::unique_ptr<const base::DictionaryValue> result;
Alex Vakulenkob04936f2014-09-19 14:53:58 -070038 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 Vakulenkoac8037d2014-11-11 11:42:05 -080042 chromeos::Error::AddToPrintf(error, FROM_HERE,
43 chromeos::errors::json::kDomain,
Alex Vakulenkob04936f2014-09-19 14:53:58 -070044 chromeos::errors::json::kParseError,
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070045 "Error parsing JSON string '%s': %s",
46 json_string.c_str(),
Alex Vakulenkob04936f2014-09-19 14:53:58 -070047 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 Vakulenkoac8037d2014-11-11 11:42:05 -080053 chromeos::Error::AddToPrintf(error, FROM_HERE,
54 chromeos::errors::json::kDomain,
Alex Vakulenkob04936f2014-09-19 14:53:58 -070055 chromeos::errors::json::kObjectExpected,
Alex Vakulenko9e2f8cd2015-04-07 16:28:09 -070056 "JSON string '%s' is not a JSON object",
57 json_string.c_str());
Alex Vakulenkob04936f2014-09-19 14:53:58 -070058 return result;
59 }
60 result.reset(dict_value);
61 return result;
62}
63
64} // namespace buffet