mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-04-25 17:25:08 +02:00
LibWebView: Move helpers to parse/write JSON files to Utilities.h
These will be re-used for bookmark storage.
This commit is contained in:
Notes:
github-actions[bot]
2026-03-24 16:06:43 +00:00
Author: https://github.com/trflynn89 Commit: https://github.com/LadybirdBrowser/ladybird/commit/c9f89d7a339 Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/8589 Reviewed-by: https://github.com/shannonbooth
@@ -9,15 +9,12 @@
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/StandardPaths.h>
|
||||
#include <LibURL/Parser.h>
|
||||
#include <LibUnicode/Locale.h>
|
||||
#include <LibWebView/Application.h>
|
||||
#include <LibWebView/Settings.h>
|
||||
#include <LibWebView/Utilities.h>
|
||||
|
||||
namespace WebView {
|
||||
|
||||
@@ -50,34 +47,6 @@ static constexpr auto global_privacy_control_key = "globalPrivacyControl"sv;
|
||||
|
||||
static constexpr auto dns_settings_key = "dnsSettings"sv;
|
||||
|
||||
static ErrorOr<JsonObject> read_settings_file(StringView settings_path)
|
||||
{
|
||||
auto settings_file = Core::File::open(settings_path, Core::File::OpenMode::Read);
|
||||
if (settings_file.is_error()) {
|
||||
if (settings_file.error().is_errno() && settings_file.error().code() == ENOENT)
|
||||
return JsonObject {};
|
||||
return settings_file.release_error();
|
||||
}
|
||||
|
||||
auto settings_contents = TRY(settings_file.value()->read_until_eof());
|
||||
auto settings_json = TRY(JsonValue::from_string(settings_contents));
|
||||
|
||||
if (!settings_json.is_object())
|
||||
return Error::from_string_literal("Expected Ladybird settings to be a JSON object");
|
||||
return move(settings_json.as_object());
|
||||
}
|
||||
|
||||
static ErrorOr<void> write_settings_file(StringView settings_path, JsonValue const& contents)
|
||||
{
|
||||
auto settings_directory = LexicalPath { settings_path }.parent();
|
||||
TRY(Core::Directory::create(settings_directory, Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
auto settings_file = TRY(Core::File::open(settings_path, Core::File::OpenMode::Write));
|
||||
TRY(settings_file->write_until_depleted(contents.serialized()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
Settings Settings::create(Badge<Application>)
|
||||
{
|
||||
// FIXME: Move this to a generic "Ladybird config directory" helper.
|
||||
@@ -86,7 +55,7 @@ Settings Settings::create(Badge<Application>)
|
||||
|
||||
Settings settings { move(settings_path) };
|
||||
|
||||
auto settings_json = read_settings_file(settings.m_settings_path);
|
||||
auto settings_json = read_json_file(settings.m_settings_path);
|
||||
if (settings_json.is_error()) {
|
||||
warnln("Unable to read Ladybird settings: {}", settings_json.error());
|
||||
return settings;
|
||||
@@ -505,7 +474,7 @@ void Settings::persist_settings()
|
||||
{
|
||||
auto settings = serialize_json();
|
||||
|
||||
if (auto result = write_settings_file(m_settings_path, settings); result.is_error())
|
||||
if (auto result = write_json_file(m_settings_path, settings); result.is_error())
|
||||
warnln("Unable to persist Ladybird settings: {}", result.error());
|
||||
}
|
||||
|
||||
|
||||
@@ -5,10 +5,13 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
#include <AK/Platform.h>
|
||||
#include <LibCore/Directory.h>
|
||||
#include <LibCore/Environment.h>
|
||||
#include <LibCore/File.h>
|
||||
#include <LibCore/Process.h>
|
||||
#include <LibCore/Resource.h>
|
||||
#include <LibCore/ResourceImplementationFile.h>
|
||||
@@ -142,4 +145,32 @@ ErrorOr<void> handle_attached_debugger()
|
||||
return {};
|
||||
}
|
||||
|
||||
ErrorOr<JsonObject> read_json_file(ByteString const& path)
|
||||
{
|
||||
auto file = Core::File::open(path, Core::File::OpenMode::Read);
|
||||
if (file.is_error()) {
|
||||
if (file.error().is_errno() && file.error().code() == ENOENT)
|
||||
return JsonObject {};
|
||||
return file.release_error();
|
||||
}
|
||||
|
||||
auto contents = TRY(file.value()->read_until_eof());
|
||||
auto json = TRY(JsonValue::from_string(contents));
|
||||
|
||||
if (!json.is_object())
|
||||
return Error::from_string_literal("Expected parsed JSON value to be an object");
|
||||
return move(json.as_object());
|
||||
}
|
||||
|
||||
ErrorOr<void> write_json_file(ByteString const& path, JsonValue const& value)
|
||||
{
|
||||
auto directory = LexicalPath { path }.parent();
|
||||
TRY(Core::Directory::create(directory, Core::Directory::CreateDirectories::Yes));
|
||||
|
||||
auto file = TRY(Core::File::open(path, Core::File::OpenMode::Write));
|
||||
TRY(file->write_until_depleted(value.serialized()));
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -27,4 +27,7 @@ WEBVIEW_API ByteString mach_server_name_for_process(StringView process_name, pid
|
||||
|
||||
WEBVIEW_API ErrorOr<void> handle_attached_debugger();
|
||||
|
||||
ErrorOr<JsonObject> read_json_file(ByteString const& path);
|
||||
ErrorOr<void> write_json_file(ByteString const& path, JsonValue const& value);
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user