LibWebView: Add do-not-track setting to about:settings

This commit is contained in:
Timothy Flynn 2025-04-02 09:47:44 -04:00
parent 7be3ed559e
commit cc4420c7d8
7 changed files with 66 additions and 0 deletions

View file

@ -334,6 +334,16 @@
</div>
</div>
<div class="card">
<div class="card-header">Privacy</div>
<div class="card-body">
<div class="card-group toggle-container">
<label for="do-not-track-toggle">Send web sites a "Do Not Track" request</label>
<input id="do-not-track-toggle" type="checkbox" switch />
</div>
</div>
</div>
<div class="button-container">
<button id="restore-defaults" class="primary-button">Restore&nbsp;Defaults</button>
</div>
@ -378,6 +388,7 @@
const siteSettingsInput = document.querySelector("#site-settings-input");
const siteSettingsRemoveAll = document.querySelector("#site-settings-remove-all");
const siteSettingsTitle = document.querySelector("#site-settings-title");
const doNotTrackToggle = document.querySelector("#do-not-track-toggle");
const restoreDefaults = document.querySelector("#restore-defaults");
window.settings = {};
@ -412,6 +423,8 @@
if (siteSetting === "autoplay") {
showSiteSettings("Autoplay", window.settings.autoplay);
}
doNotTrackToggle.checked = window.settings.doNotTrack;
};
newTabPageURL.addEventListener("change", () => {
@ -609,6 +622,10 @@
event.stopPropagation();
});
doNotTrackToggle.addEventListener("change", () => {
ladybird.sendMessage("setDoNotTrack", doNotTrackToggle.checked);
});
restoreDefaults.addEventListener("click", () => {
ladybird.sendMessage("restoreDefaultSettings");
});

View file

@ -32,6 +32,8 @@ static constexpr auto site_setting_site_filters_key = "siteFilters"sv;
static constexpr auto autoplay_key = "autoplay"sv;
static constexpr auto do_not_track_key = "doNotTrack"sv;
static ErrorOr<JsonObject> read_settings_file(StringView settings_path)
{
auto settings_file = Core::File::open(settings_path, Core::File::OpenMode::Read);
@ -109,6 +111,9 @@ Settings Settings::create(Badge<Application>)
load_site_setting(settings.m_autoplay, autoplay_key);
if (auto do_not_track = settings_json.value().get_bool(do_not_track_key); do_not_track.has_value())
settings.m_do_not_track = *do_not_track ? DoNotTrack::Yes : DoNotTrack::No;
return settings;
}
@ -153,6 +158,8 @@ JsonValue Settings::serialize_json() const
save_site_setting(m_autoplay, autoplay_key);
settings.set(do_not_track_key, m_do_not_track == DoNotTrack::Yes);
return settings;
}
@ -162,6 +169,7 @@ void Settings::restore_defaults()
m_search_engine.clear();
m_autocomplete_engine.clear();
m_autoplay = SiteSetting {};
m_do_not_track = DoNotTrack::No;
persist_settings();
@ -244,6 +252,15 @@ void Settings::remove_all_autoplay_site_filters()
observer.autoplay_settings_changed();
}
void Settings::set_do_not_track(DoNotTrack do_not_track)
{
m_do_not_track = do_not_track;
persist_settings();
for (auto& observer : m_observers)
observer.do_not_track_changed();
}
void Settings::persist_settings()
{
auto settings = serialize_json();

View file

@ -24,6 +24,11 @@ struct SiteSetting {
OrderedHashTable<String> site_filters;
};
enum class DoNotTrack {
No,
Yes,
};
class SettingsObserver {
public:
SettingsObserver();
@ -33,6 +38,7 @@ public:
virtual void search_engine_changed() { }
virtual void autocomplete_engine_changed() { }
virtual void autoplay_settings_changed() { }
virtual void do_not_track_changed() { }
};
class Settings {
@ -58,6 +64,9 @@ public:
void remove_autoplay_site_filter(String const&);
void remove_all_autoplay_site_filters();
DoNotTrack do_not_track() const { return m_do_not_track; }
void set_do_not_track(DoNotTrack);
static void add_observer(Badge<SettingsObserver>, SettingsObserver&);
static void remove_observer(Badge<SettingsObserver>, SettingsObserver&);
@ -72,6 +81,7 @@ private:
Optional<SearchEngine> m_search_engine;
Optional<AutocompleteEngine> m_autocomplete_engine;
SiteSetting m_autoplay;
DoNotTrack m_do_not_track { DoNotTrack::No };
Vector<SettingsObserver&> m_observers;
};

View file

@ -605,6 +605,7 @@ void ViewImplementation::initialize_client(CreateNewClient create_new_client)
client().async_debug_request(m_client_state.page_index, "spoof-user-agent"sv, *user_agents.get(*user_agent_preset));
autoplay_settings_changed();
do_not_track_changed();
}
void ViewImplementation::handle_web_content_process_crash(LoadErrorPage load_error_page)
@ -663,6 +664,12 @@ void ViewImplementation::autoplay_settings_changed()
client().async_set_autoplay_allowlist(page_id(), autoplay_settings.site_filters.values());
}
void ViewImplementation::do_not_track_changed()
{
auto do_not_track = Application::settings().do_not_track();
client().async_set_enable_do_not_track(page_id(), do_not_track == DoNotTrack::Yes);
}
static ErrorOr<LexicalPath> save_screenshot(Gfx::ShareableBitmap const& bitmap)
{
if (!bitmap.is_valid())

View file

@ -265,6 +265,7 @@ protected:
void handle_web_content_process_crash(LoadErrorPage = LoadErrorPage::Yes);
virtual void autoplay_settings_changed() override;
virtual void do_not_track_changed() override;
struct SharedBitmap {
i32 id { -1 };

View file

@ -50,6 +50,10 @@ void SettingsUI::register_interfaces()
register_interface("removeAllSiteSettingFilters"sv, [this](auto const& data) {
remove_all_site_setting_filters(data);
});
register_interface("setDoNotTrack"sv, [this](auto const& data) {
set_do_not_track(data);
});
}
void SettingsUI::load_current_settings()
@ -218,4 +222,12 @@ void SettingsUI::remove_all_site_setting_filters(JsonValue const& site_setting)
load_current_settings();
}
void SettingsUI::set_do_not_track(JsonValue const& do_not_track)
{
if (!do_not_track.is_bool())
return;
WebView::Application::settings().set_do_not_track(do_not_track.as_bool() ? DoNotTrack::Yes : DoNotTrack::No);
}
}

View file

@ -30,6 +30,8 @@ private:
void add_site_setting_filter(JsonValue const&);
void remove_site_setting_filter(JsonValue const&);
void remove_all_site_setting_filters(JsonValue const&);
void set_do_not_track(JsonValue const&);
};
}