mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-08-21 01:40:46 +00:00
LibWebView: Add autoplay settings to about:settings
The idea with the UI here is that it will serve as a generic component for all site settings, such as autoplay, notifications, etc. When the site settings dialog is opened, it is filled with the requested setting data, and messages sent to the browser process are based on the setting. This patch only implements the UI and persisted settings. It does not apply autoplay changes to the WebContent process.
This commit is contained in:
parent
c7979a8bd6
commit
223b04f087
Notes:
github-actions[bot]
2025-03-30 15:20:14 +00:00
Author: https://github.com/trflynn89
Commit: 223b04f087
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/4146
Reviewed-by: https://github.com/tcl3 ✅
6 changed files with 614 additions and 10 deletions
|
@ -5,6 +5,7 @@
|
|||
*/
|
||||
|
||||
#include <AK/ByteString.h>
|
||||
#include <AK/JsonArray.h>
|
||||
#include <AK/JsonObject.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/LexicalPath.h>
|
||||
|
@ -19,9 +20,15 @@
|
|||
namespace WebView {
|
||||
|
||||
static constexpr auto new_tab_page_url_key = "newTabPageURL"sv;
|
||||
|
||||
static constexpr auto search_engine_key = "searchEngine"sv;
|
||||
static constexpr auto search_engine_name_key = "name"sv;
|
||||
|
||||
static constexpr auto site_setting_enabled_globally_key = "enabledGlobally"sv;
|
||||
static constexpr auto site_setting_site_filters_key = "siteFilters"sv;
|
||||
|
||||
static constexpr auto autoplay_key = "autoplay"sv;
|
||||
|
||||
static ErrorOr<JsonObject> read_settings_file(StringView settings_path)
|
||||
{
|
||||
auto settings_file = Core::File::open(settings_path, Core::File::OpenMode::Read);
|
||||
|
@ -74,6 +81,26 @@ Settings Settings::create(Badge<Application>)
|
|||
settings.m_search_engine = find_search_engine_by_name(*search_engine_name);
|
||||
}
|
||||
|
||||
auto load_site_setting = [&](SiteSetting& site_setting, StringView key) {
|
||||
auto saved_settings = settings_json.value().get_object(key);
|
||||
if (!saved_settings.has_value())
|
||||
return;
|
||||
|
||||
if (auto enabled_globally = saved_settings->get_bool(site_setting_enabled_globally_key); enabled_globally.has_value())
|
||||
site_setting.enabled_globally = *enabled_globally;
|
||||
|
||||
if (auto site_filters = saved_settings->get_array(site_setting_site_filters_key); site_filters.has_value()) {
|
||||
site_setting.site_filters.clear();
|
||||
|
||||
site_filters->for_each([&](auto const& site_filter) {
|
||||
if (site_filter.is_string())
|
||||
site_setting.site_filters.set(site_filter.as_string());
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
load_site_setting(settings.m_autoplay, autoplay_key);
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
@ -95,6 +122,22 @@ JsonValue Settings::serialize_json() const
|
|||
settings.set(search_engine_key, move(search_engine));
|
||||
}
|
||||
|
||||
auto save_site_setting = [&](SiteSetting const& site_setting, StringView key) {
|
||||
JsonArray site_filters;
|
||||
site_filters.ensure_capacity(site_setting.site_filters.size());
|
||||
|
||||
for (auto const& site_filter : site_setting.site_filters)
|
||||
site_filters.must_append(site_filter);
|
||||
|
||||
JsonObject setting;
|
||||
setting.set("enabledGlobally"sv, site_setting.enabled_globally);
|
||||
setting.set("siteFilters"sv, move(site_filters));
|
||||
|
||||
settings.set(key, move(setting));
|
||||
};
|
||||
|
||||
save_site_setting(m_autoplay, autoplay_key);
|
||||
|
||||
return settings;
|
||||
}
|
||||
|
||||
|
@ -102,6 +145,7 @@ void Settings::restore_defaults()
|
|||
{
|
||||
m_new_tab_page_url = URL::about_newtab();
|
||||
m_search_engine.clear();
|
||||
m_autoplay = SiteSetting {};
|
||||
|
||||
persist_settings();
|
||||
|
||||
|
@ -131,6 +175,46 @@ void Settings::set_search_engine(Optional<StringView> search_engine_name)
|
|||
observer.search_engine_changed();
|
||||
}
|
||||
|
||||
void Settings::set_autoplay_enabled_globally(bool enabled_globally)
|
||||
{
|
||||
m_autoplay.enabled_globally = enabled_globally;
|
||||
persist_settings();
|
||||
|
||||
for (auto& observer : m_observers)
|
||||
observer.autoplay_settings_changed();
|
||||
}
|
||||
|
||||
void Settings::add_autoplay_site_filter(String const& site_filter)
|
||||
{
|
||||
auto trimmed_site_filter = MUST(site_filter.trim_whitespace());
|
||||
if (trimmed_site_filter.is_empty())
|
||||
return;
|
||||
|
||||
m_autoplay.site_filters.set(move(trimmed_site_filter));
|
||||
persist_settings();
|
||||
|
||||
for (auto& observer : m_observers)
|
||||
observer.autoplay_settings_changed();
|
||||
}
|
||||
|
||||
void Settings::remove_autoplay_site_filter(String const& site_filter)
|
||||
{
|
||||
m_autoplay.site_filters.remove(site_filter);
|
||||
persist_settings();
|
||||
|
||||
for (auto& observer : m_observers)
|
||||
observer.autoplay_settings_changed();
|
||||
}
|
||||
|
||||
void Settings::remove_all_autoplay_site_filters()
|
||||
{
|
||||
m_autoplay.site_filters.clear();
|
||||
persist_settings();
|
||||
|
||||
for (auto& observer : m_observers)
|
||||
observer.autoplay_settings_changed();
|
||||
}
|
||||
|
||||
void Settings::persist_settings()
|
||||
{
|
||||
auto settings = serialize_json();
|
||||
|
@ -162,4 +246,9 @@ SettingsObserver::~SettingsObserver()
|
|||
Settings::remove_observer({}, *this);
|
||||
}
|
||||
|
||||
SiteSetting::SiteSetting()
|
||||
{
|
||||
site_filters.set("file://"_string);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,6 +7,7 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Badge.h>
|
||||
#include <AK/HashTable.h>
|
||||
#include <AK/JsonValue.h>
|
||||
#include <AK/Optional.h>
|
||||
#include <LibURL/URL.h>
|
||||
|
@ -15,6 +16,13 @@
|
|||
|
||||
namespace WebView {
|
||||
|
||||
struct SiteSetting {
|
||||
SiteSetting();
|
||||
|
||||
bool enabled_globally { false };
|
||||
OrderedHashTable<String> site_filters;
|
||||
};
|
||||
|
||||
class SettingsObserver {
|
||||
public:
|
||||
SettingsObserver();
|
||||
|
@ -22,6 +30,7 @@ public:
|
|||
|
||||
virtual void new_tab_page_url_changed() { }
|
||||
virtual void search_engine_changed() { }
|
||||
virtual void autoplay_settings_changed() { }
|
||||
};
|
||||
|
||||
class Settings {
|
||||
|
@ -38,6 +47,12 @@ public:
|
|||
Optional<SearchEngine> const& search_engine() const { return m_search_engine; }
|
||||
void set_search_engine(Optional<StringView> search_engine_name);
|
||||
|
||||
SiteSetting const& autoplay_settings() const { return m_autoplay; }
|
||||
void set_autoplay_enabled_globally(bool);
|
||||
void add_autoplay_site_filter(String const&);
|
||||
void remove_autoplay_site_filter(String const&);
|
||||
void remove_all_autoplay_site_filters();
|
||||
|
||||
static void add_observer(Badge<SettingsObserver>, SettingsObserver&);
|
||||
static void remove_observer(Badge<SettingsObserver>, SettingsObserver&);
|
||||
|
||||
|
@ -50,6 +65,7 @@ private:
|
|||
|
||||
URL::URL m_new_tab_page_url;
|
||||
Optional<SearchEngine> m_search_engine;
|
||||
SiteSetting m_autoplay;
|
||||
|
||||
Vector<SettingsObserver&> m_observers;
|
||||
};
|
||||
|
|
|
@ -29,6 +29,21 @@ void SettingsUI::register_interfaces()
|
|||
register_interface("setSearchEngine"sv, [this](auto const& data) {
|
||||
set_search_engine(data);
|
||||
});
|
||||
register_interface("loadForciblyEnabledSiteSettings"sv, [this](auto const&) {
|
||||
load_forcibly_enabled_site_settings();
|
||||
});
|
||||
register_interface("setSiteSettingEnabledGlobally"sv, [this](auto const& data) {
|
||||
set_site_setting_enabled_globally(data);
|
||||
});
|
||||
register_interface("addSiteSettingFilter"sv, [this](auto const& data) {
|
||||
add_site_setting_filter(data);
|
||||
});
|
||||
register_interface("removeSiteSettingFilter"sv, [this](auto const& data) {
|
||||
remove_site_setting_filter(data);
|
||||
});
|
||||
register_interface("removeAllSiteSettingFilters"sv, [this](auto const& data) {
|
||||
remove_all_site_setting_filters(data);
|
||||
});
|
||||
}
|
||||
|
||||
void SettingsUI::load_current_settings()
|
||||
|
@ -72,4 +87,113 @@ void SettingsUI::set_search_engine(JsonValue const& search_engine)
|
|||
WebView::Application::settings().set_search_engine(search_engine.as_string());
|
||||
}
|
||||
|
||||
enum class SiteSettingType {
|
||||
Autoplay,
|
||||
};
|
||||
|
||||
static constexpr StringView site_setting_type_to_string(SiteSettingType setting)
|
||||
{
|
||||
switch (setting) {
|
||||
case SiteSettingType::Autoplay:
|
||||
return "autoplay"sv;
|
||||
}
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
|
||||
static Optional<SiteSettingType> site_setting_type(JsonValue const& settings)
|
||||
{
|
||||
if (!settings.is_object())
|
||||
return {};
|
||||
|
||||
auto setting_type = settings.as_object().get_string("setting"sv);
|
||||
if (!setting_type.has_value())
|
||||
return {};
|
||||
|
||||
if (*setting_type == "autoplay"sv)
|
||||
return SiteSettingType::Autoplay;
|
||||
return {};
|
||||
}
|
||||
|
||||
void SettingsUI::load_forcibly_enabled_site_settings()
|
||||
{
|
||||
JsonArray site_settings;
|
||||
|
||||
if (Application::web_content_options().enable_autoplay == EnableAutoplay::Yes)
|
||||
site_settings.must_append(site_setting_type_to_string(SiteSettingType::Autoplay));
|
||||
|
||||
async_send_message("forciblyEnableSiteSettings"sv, move(site_settings));
|
||||
}
|
||||
|
||||
void SettingsUI::set_site_setting_enabled_globally(JsonValue const& site_setting)
|
||||
{
|
||||
auto setting = site_setting_type(site_setting);
|
||||
if (!setting.has_value())
|
||||
return;
|
||||
|
||||
auto enabled = site_setting.as_object().get_bool("enabled"sv);
|
||||
if (!enabled.has_value())
|
||||
return;
|
||||
|
||||
switch (*setting) {
|
||||
case SiteSettingType::Autoplay:
|
||||
WebView::Application::settings().set_autoplay_enabled_globally(*enabled);
|
||||
break;
|
||||
}
|
||||
|
||||
load_current_settings();
|
||||
}
|
||||
|
||||
void SettingsUI::add_site_setting_filter(JsonValue const& site_setting)
|
||||
{
|
||||
auto setting = site_setting_type(site_setting);
|
||||
if (!setting.has_value())
|
||||
return;
|
||||
|
||||
auto filter = site_setting.as_object().get_string("filter"sv);
|
||||
if (!filter.has_value())
|
||||
return;
|
||||
|
||||
switch (*setting) {
|
||||
case SiteSettingType::Autoplay:
|
||||
WebView::Application::settings().add_autoplay_site_filter(*filter);
|
||||
break;
|
||||
}
|
||||
|
||||
load_current_settings();
|
||||
}
|
||||
|
||||
void SettingsUI::remove_site_setting_filter(JsonValue const& site_setting)
|
||||
{
|
||||
auto setting = site_setting_type(site_setting);
|
||||
if (!setting.has_value())
|
||||
return;
|
||||
|
||||
auto filter = site_setting.as_object().get_string("filter"sv);
|
||||
if (!filter.has_value())
|
||||
return;
|
||||
|
||||
switch (*setting) {
|
||||
case SiteSettingType::Autoplay:
|
||||
WebView::Application::settings().remove_autoplay_site_filter(*filter);
|
||||
break;
|
||||
}
|
||||
|
||||
load_current_settings();
|
||||
}
|
||||
|
||||
void SettingsUI::remove_all_site_setting_filters(JsonValue const& site_setting)
|
||||
{
|
||||
auto setting = site_setting_type(site_setting);
|
||||
if (!setting.has_value())
|
||||
return;
|
||||
|
||||
switch (*setting) {
|
||||
case SiteSettingType::Autoplay:
|
||||
WebView::Application::settings().remove_all_autoplay_site_filters();
|
||||
break;
|
||||
}
|
||||
|
||||
load_current_settings();
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -21,6 +21,11 @@ private:
|
|||
void set_new_tab_page_url(JsonValue const&);
|
||||
void load_available_search_engines();
|
||||
void set_search_engine(JsonValue const&);
|
||||
void load_forcibly_enabled_site_settings();
|
||||
void set_site_setting_enabled_globally(JsonValue const&);
|
||||
void add_site_setting_filter(JsonValue const&);
|
||||
void remove_site_setting_filter(JsonValue const&);
|
||||
void remove_all_site_setting_filters(JsonValue const&);
|
||||
};
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue