ladybird/Libraries/LibWebView/Settings.h
Timothy Flynn e084a86861 LibWebView+UI: Introduce a persistent settings object
This adds a WebView::Settings class to own persistent browser settings.
In this first pass, it now owns the new tab page URL and search engine
settings.

For simplicitly, we currently use a JSON format for these settings. They
are stored alongside the cookie database. As of this commit, the saved
JSON will have the form:

    {
        "newTabPageURL": "about:blank",
        "searchEngine": {
            "name": "Google"
        }
    }

(The search engine is an object to allow room for a future patch to
implement custom search engine URLs.)

For Qt, this replaces the management of these particular settings in the
Qt settings UI. We will have an internal browser page to control these
settings instead. In the future, we will want to port all settings to
this new class. We will also want to allow UI-specific settings (such as
whether the hamburger menu is displayed in Qt).
2025-03-22 17:27:45 +01:00

56 lines
1.3 KiB
C++

/*
* Copyright (c) 2025, Tim Flynn <trflynn89@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/Badge.h>
#include <AK/Optional.h>
#include <LibURL/URL.h>
#include <LibWebView/Forward.h>
#include <LibWebView/SearchEngine.h>
namespace WebView {
class SettingsObserver {
public:
SettingsObserver();
virtual ~SettingsObserver();
virtual void new_tab_page_url_changed() { }
virtual void search_engine_changed() { }
};
class Settings {
public:
static Settings create(Badge<Application>);
String serialize_json() const;
void restore_defaults();
URL::URL const& new_tab_page_url() const { return m_new_tab_page_url; }
void set_new_tab_page_url(URL::URL);
Optional<SearchEngine> const& search_engine() const { return m_search_engine; }
void set_search_engine(Optional<StringView> search_engine_name);
static void add_observer(Badge<SettingsObserver>, SettingsObserver&);
static void remove_observer(Badge<SettingsObserver>, SettingsObserver&);
private:
explicit Settings(ByteString settings_path);
void persist_settings();
ByteString m_settings_path;
URL::URL m_new_tab_page_url;
Optional<SearchEngine> m_search_engine;
Vector<SettingsObserver&> m_observers;
};
}