summaryrefslogtreecommitdiff
path: root/chromium/fuchsia/base/config_reader.cc
blob: f3ac3d51c5e02e585bcc9dd48450e9c68197a23f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// Copyright 2020 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "fuchsia/base/config_reader.h"

#include "base/files/file_util.h"
#include "base/json/json_reader.h"

namespace cr_fuchsia {

base::Optional<base::Value> LoadPackageConfig() {
  constexpr char kConfigPath[] = "/config/data/config.json";

  base::FilePath path(kConfigPath);
  if (!base::PathExists(path)) {
    LOG(WARNING) << "Config file doesn't exist: " << path.value();
    return base::nullopt;
  }

  std::string file_content;
  bool loaded = base::ReadFileToString(path, &file_content);
  if (!loaded) {
    LOG(WARNING) << "Couldn't read config file: " << path.value();
    return base::nullopt;
  }

  base::JSONReader reader;
  base::Optional<base::Value> parsed = reader.Read(file_content);
  CHECK(parsed) << "Failed to parse " << path.value() << ": "
                << reader.GetErrorMessage();
  CHECK(parsed->is_dict()) << "Config is not a JSON dictinary: "
                           << path.value();

  return std::move(parsed.value());
}

}  // namespace cr_fuchsia