1 // Copyright 2020 The Chromium 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 "fuchsia/base/config_reader.h" 6 7 #include "base/files/file_util.h" 8 #include "base/json/json_reader.h" 9 #include "base/logging.h" 10 #include "base/no_destructor.h" 11 12 namespace cr_fuchsia { 13 14 namespace { 15 ReadPackageConfig()16base::Optional<base::Value> ReadPackageConfig() { 17 constexpr char kConfigPath[] = "/config/data/config.json"; 18 19 base::FilePath path(kConfigPath); 20 if (!base::PathExists(path)) { 21 LOG(WARNING) << "Config file doesn't exist: " << path.value(); 22 return base::nullopt; 23 } 24 25 std::string file_content; 26 bool loaded = base::ReadFileToString(path, &file_content); 27 if (!loaded) { 28 LOG(WARNING) << "Couldn't read config file: " << path.value(); 29 return base::nullopt; 30 } 31 32 base::JSONReader::ValueWithError parsed = 33 base::JSONReader::ReadAndReturnValueWithError(file_content); 34 CHECK(parsed.value) << "Failed to parse " << path.value() << ": " 35 << parsed.error_message; 36 CHECK(parsed.value->is_dict()) 37 << "Config is not a JSON dictionary: " << path.value(); 38 39 return std::move(parsed.value); 40 } 41 42 } // namespace 43 LoadPackageConfig()44const base::Optional<base::Value>& LoadPackageConfig() { 45 // Package configurations do not change at run-time, so read the configuration 46 // on the first call and cache the result. 47 static base::NoDestructor<base::Optional<base::Value>> config( 48 ReadPackageConfig()); 49 return *config; 50 } 51 52 } // namespace cr_fuchsia 53