Commit 85f22351 authored by Wez's avatar Wez Committed by Commit Bot

[fuchsia] Cache the package configuration in LoadPackageConfig().

Package configurations do not change at run-time, so cache it the first
time it is read, to avoid the cost of repeatedly re-reading it from the
filesystem.

Change-Id: I53c6ea55be5fca48bf3c81f189e251d8a11a55a5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2135607
Commit-Queue: Wez <wez@chromium.org>
Auto-Submit: Wez <wez@chromium.org>
Reviewed-by: default avatarSergey Ulanov <sergeyu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#756349}
parent f542d13a
...@@ -6,10 +6,13 @@ ...@@ -6,10 +6,13 @@
#include "base/files/file_util.h" #include "base/files/file_util.h"
#include "base/json/json_reader.h" #include "base/json/json_reader.h"
#include "base/no_destructor.h"
namespace cr_fuchsia { namespace cr_fuchsia {
base::Optional<base::Value> LoadPackageConfig() { namespace {
base::Optional<base::Value> ReadPackageConfig() {
constexpr char kConfigPath[] = "/config/data/config.json"; constexpr char kConfigPath[] = "/config/data/config.json";
base::FilePath path(kConfigPath); base::FilePath path(kConfigPath);
...@@ -35,4 +38,14 @@ base::Optional<base::Value> LoadPackageConfig() { ...@@ -35,4 +38,14 @@ base::Optional<base::Value> LoadPackageConfig() {
return std::move(parsed.value()); return std::move(parsed.value());
} }
} // namespace
const base::Optional<base::Value>& LoadPackageConfig() {
// Package configurations do not change at run-time, so read the configuration
// on the first call and cache the result.
static base::NoDestructor<base::Optional<base::Value>> config(
ReadPackageConfig());
return *config;
}
} // namespace cr_fuchsia } // namespace cr_fuchsia
...@@ -13,7 +13,7 @@ namespace cr_fuchsia { ...@@ -13,7 +13,7 @@ namespace cr_fuchsia {
// Loads and parses configuration data from the environment. // Loads and parses configuration data from the environment.
// Returns a null value if the file(s) do not exist. // Returns a null value if the file(s) do not exist.
// CHECK-fails if the file(s) are present but not parseable. // CHECK-fails if the file(s) are present but not parseable.
base::Optional<base::Value> LoadPackageConfig(); const base::Optional<base::Value>& LoadPackageConfig();
} // namespace cr_fuchsia } // namespace cr_fuchsia
......
...@@ -255,9 +255,10 @@ const uint32_t ContextProviderImpl::kContextRequestHandleId = ...@@ -255,9 +255,10 @@ const uint32_t ContextProviderImpl::kContextRequestHandleId =
PA_HND(PA_USER0, 0); PA_HND(PA_USER0, 0);
ContextProviderImpl::ContextProviderImpl() { ContextProviderImpl::ContextProviderImpl() {
base::Optional<base::Value> default_config = cr_fuchsia::LoadPackageConfig(); const base::Optional<base::Value>& default_config =
cr_fuchsia::LoadPackageConfig();
if (default_config) { if (default_config) {
config_default_ = std::move(default_config.value()); config_default_ = default_config->Clone();
} else { } else {
config_default_ = base::Value(base::Value::Type::DICTIONARY); config_default_ = base::Value(base::Value::Type::DICTIONARY);
} }
...@@ -571,14 +572,14 @@ base::Value ContextProviderImpl::LoadConfig() { ...@@ -571,14 +572,14 @@ base::Value ContextProviderImpl::LoadConfig() {
if (!config_override_.is_none()) if (!config_override_.is_none())
return config_override_.Clone(); return config_override_.Clone();
base::Optional<base::Value> config = cr_fuchsia::LoadPackageConfig(); const base::Optional<base::Value>& config = cr_fuchsia::LoadPackageConfig();
if (!config) { if (!config) {
DLOG(WARNING) << "Configuration data not found. Using default " DLOG(WARNING) << "Configuration data not found. Using default "
"WebEngine configuration."; "WebEngine configuration.";
return base::Value(base::Value::Type::DICTIONARY); return base::Value(base::Value::Type::DICTIONARY);
} }
return std::move(*config); return config->Clone();
} }
void ContextProviderImpl::EnableDevTools( void ContextProviderImpl::EnableDevTools(
......
...@@ -24,7 +24,7 @@ namespace { ...@@ -24,7 +24,7 @@ namespace {
bool IsHeadless() { bool IsHeadless() {
constexpr char kHeadlessConfigKey[] = "headless"; constexpr char kHeadlessConfigKey[] = "headless";
base::Optional<base::Value> config = cr_fuchsia::LoadPackageConfig(); const base::Optional<base::Value>& config = cr_fuchsia::LoadPackageConfig();
if (config) { if (config) {
base::Optional<bool> headless = config->FindBoolPath(kHeadlessConfigKey); base::Optional<bool> headless = config->FindBoolPath(kHeadlessConfigKey);
return headless && *headless; return headless && *headless;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment