Truly independent web browser
LibWebView: Use temporary profiles in test mode
Timothy Flynn3a8ec48b8d6d
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 202e0fe5a82..6b400deb8a9 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -642,10 +642,17 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)}// Test results must not depend on the languages of the machine running the tests.- auto platform_languages = enable_test_mode ? Vector<String> {} : platform_preferred_languages();+ auto platform_languages = m_web_content_options.is_test_mode == IsTestMode::Yes ? Vector<String> {} : platform_preferred_languages();+m_settings = make<Settings>(Settings::create(LexicalPath::join(profile().paths().config, "Settings.json"sv).string(), move(platform_languages)));m_bookmark_store = make<BookmarkStore>(BookmarkStore::create(LexicalPath::join(profile().paths().config, "Bookmarks.json"sv).string()));+ if (m_web_content_options.is_test_mode == IsTestMode::Yes) {+ TRY(m_settings->complete_first_run());+ m_settings->set_background_networking_enabled(false);+ m_settings->set_content_blocker_enabled(true);+ }+m_browser_options.urls = sanitize_urls(m_browser_options.raw_urls);m_request_server_options.cache_path = profile().paths().cache;m_web_content_options.cache_path = profile().paths().cache;@@ -654,7 +661,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)rebuild_content_blocker_list_paths();if (!m_settings->first_run_complete() && !m_browser_options.headless_mode.has_value()- && !m_browser_options.webdriver_browser_endpoint.has_value() && m_web_content_options.is_test_mode == IsTestMode::No) {+ && !m_browser_options.webdriver_browser_endpoint.has_value()) {if (m_browser_options.raw_urls.is_empty())m_browser_options.urls = { URL::about_welcome() };else if (!m_browser_options.urls.contains_slow(URL::about_welcome()))@@ -668,10 +675,6 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)m_web_content_options.expose_experimental_interfaces = ExposeExperimentalInterfaces::Yes;m_web_content_options.expose_internals_object = ExposeInternalsObject::Yes;m_web_content_options.force_cpu_painting = ForceCPUPainting::Yes;-- // Test results must not depend on the content blocking the developer running the tests happens to have- // configured for normal browsing, and compiling those lists into every renderer is not cheap.- m_browser_options.content_blocker_list_paths.clear();}if (m_web_content_options.file_scheme_urls_have_tuple_origins == FileSchemeUrlsHaveTupleOrigins::Yes)@@ -694,7 +697,7 @@ ErrorOr<void> Application::initialize(Main::Arguments const& arguments)m_stats_log->start_sampling();TRY(launch_services());- if (m_web_content_options.is_test_mode == IsTestMode::No && m_browser_options.enable_content_blocker == EnableContentBlocker::Yes) {+ if (m_browser_options.enable_content_blocker == EnableContentBlocker::Yes) {m_content_blocker_list_update_timer = Core::Timer::create_repeating(AK::Duration::from_seconds(24 * 60 * 60), [this] {start_content_blocker_list_update(ContentBlockerListUpdateTrigger::Automatic);});@@ -724,9 +727,11 @@ ErrorOr<void> Application::create_profile([[maybe_unused]] Optional<StringView>if (profile_path.has_value())profile_selection.path = profile_path->to_byte_string();- // Naming a profile is an explicit choice and wins; otherwise the app decides whether it wants a throwaway one.- if (!profile_selection.temporary && !profile_name.has_value() && !profile_path.has_value())- profile_selection.temporary = should_use_temporary_profile_by_default();+ if (m_web_content_options.is_test_mode == IsTestMode::Yes) {+ if (profile_name.has_value() || profile_path.has_value())+ return Error::from_string_literal("Test mode cannot be combined with a persistent profile selector");+ profile_selection.temporary = true;+ }#endifProfileRoots profile_roots {@@ -813,9 +818,7 @@ void Application::rebuild_content_blocker_list_paths()ErrorOr<bool> Application::load_content_blocker_lists(){- StringView custom_filters;- if (m_web_content_options.is_test_mode == IsTestMode::No)- custom_filters = m_settings->custom_content_blocker_filters();+ auto custom_filters = m_settings->custom_content_blocker_filters();if (m_browser_options.content_blocker_list_paths.is_empty() && custom_filters.is_empty()) {if (m_content_blocker_list_buffer.has_value() && m_content_blocker_list_buffer->size() == 0)return false;@@ -860,8 +863,6 @@ ErrorOr<bool> Application::load_content_blocker_lists()void Application::start_content_blocker_list_update(ContentBlockerListUpdateTrigger trigger, Optional<StringView> requested_identifier){- if (m_web_content_options.is_test_mode == IsTestMode::Yes)- return;if (trigger == ContentBlockerListUpdateTrigger::Automatic && !m_settings->automatic_filter_list_updates_allowed())return;if (content_blocker_list_update_in_progress() && !requested_identifier.has_value())@@ -2978,9 +2979,7 @@ void Application::background_networking_settings_changed(Badge<ApplicationSettinbool Application::content_blocking_enabled() const{- // Test mode supplies its own filter lists independently of profile preferences.- return m_browser_options.enable_content_blocker == EnableContentBlocker::Yes- && (m_web_content_options.is_test_mode == IsTestMode::Yes || m_settings->content_blocker_enabled());+ return m_browser_options.enable_content_blocker == EnableContentBlocker::Yes && m_settings->content_blocker_enabled();}bool Application::has_content_blocking_rules() const@@ -2990,7 +2989,7 @@ bool Application::has_content_blocking_rules() constvoid Application::apply_content_blocking_policy(WebContentPage& page){- auto disabled_sites = m_web_content_options.is_test_mode == IsTestMode::Yes ? Vector<String> {} : m_settings->content_blocker_disabled_sites().values();+ auto disabled_sites = m_settings->content_blocker_disabled_sites().values();page.async_set_content_blocking_policy(content_blocking_enabled(), disabled_sites);}@@ -3001,17 +3000,16 @@ void Application::content_blocker_settings_changed(Badge<ApplicationSettingsObsevoid Application::apply_content_blocker_settings(){+ rebuild_content_blocker_list_paths();bool rules_changed = false;- if (m_web_content_options.is_test_mode == IsTestMode::No) {- rebuild_content_blocker_list_paths();- if (auto result = load_content_blocker_lists(); result.is_error())- warnln("Unable to apply content blocker settings: {}", result.error());- else- rules_changed = result.release_value();- }++ if (auto result = load_content_blocker_lists(); result.is_error())+ warnln("Unable to apply content blocker settings: {}", result.error());+ else+ rules_changed = result.release_value();auto enabled = content_blocking_enabled();- auto disabled_sites = m_web_content_options.is_test_mode == IsTestMode::Yes ? Vector<String> {} : m_settings->content_blocker_disabled_sites().values();+ auto disabled_sites = m_settings->content_blocker_disabled_sites().values();WebContentClient::for_each_client([&](WebContentClient& client) {client.for_each_page([&](WebContentPage& page) {diff --git a/Libraries/LibWebView/Application.h b/Libraries/LibWebView/Application.hindex af2e25e38f9..745d65fbdd1 100644--- a/Libraries/LibWebView/Application.h+++ b/Libraries/LibWebView/Application.h@@ -424,8 +424,6 @@ protected:virtual Core::EventLoop& create_platform_event_loop();virtual bool should_coordinate_browser_process() const { return true; }- // An application whose state must not leak between runs — or into a developer's own browsing state.- virtual bool should_use_temporary_profile_by_default() const { return false; }virtual SiteIsolationMode default_site_isolation_mode() const { return SiteIsolationMode::IFrame; }virtual Optional<ByteString> ask_user_for_download_path([[maybe_unused]] ByteString const& file) const { return {}; }diff --git a/Tests/LibWeb/test-web/Application.h b/Tests/LibWeb/test-web/Application.hindex 3930c30111b..0a87bfa51b4 100644--- a/Tests/LibWeb/test-web/Application.h+++ b/Tests/LibWeb/test-web/Application.h@@ -26,8 +26,6 @@ public:virtual void create_platform_arguments(Core::ArgsParser&) override;virtual void create_platform_options(WebView::BrowserOptions&, WebView::RequestServerOptions&, WebView::WebContentOptions&) override;- // Tests must not read, or write, the browser settings of whoever is running them.- virtual bool should_use_temporary_profile_by_default() const override { return true; }virtual bool should_coordinate_browser_process() const override { return false; }virtual bool should_capture_web_content_output() const override { return true; }// Tests cover elastic overscroll on every platform, whichever of them the browser enables it on.diff --git a/Tests/LibWebView/TestApplicationDestruction.cpp b/Tests/LibWebView/TestApplicationDestruction.cppindex ec84f141aae..235480fcafb 100644--- a/Tests/LibWebView/TestApplicationDestruction.cpp+++ b/Tests/LibWebView/TestApplicationDestruction.cpp@@ -6,8 +6,6 @@#include <LibTest/TestCase.h>-#include <LibCore/Directory.h>-#include <LibCore/Environment.h>#include <LibCore/EventLoop.h>#include <LibCore/StandardPaths.h>#include <LibCore/ThreadEventQueue.h>@@ -16,6 +14,7 @@#include <LibWebView/Application.h>#include <LibWebView/HSTSStore.h>#include <LibWebView/HeadlessWebView.h>+#include <LibWebView/Settings.h>#include <LibWebView/Utilities.h>#include <LibWebView/WebContentClient.h>#include <LibWebView/WebContentPage.h>@@ -30,11 +29,10 @@ public:{}- virtual void create_platform_options(WebView::BrowserOptions& browser_options, WebView::RequestServerOptions&, WebView::WebContentOptions& web_content_options) override+ virtual void create_platform_options(WebView::BrowserOptions& browser_options, WebView::RequestServerOptions&, WebView::WebContentOptions&) override{browser_options.headless_mode.clear();browser_options.disable_sql_database = WebView::DisableSQLDatabase::No;- web_content_options.is_test_mode = WebView::IsTestMode::Yes;}virtual Core::EventLoop& create_platform_event_loop() override { return Core::EventLoop::current(); }@@ -55,22 +53,22 @@ public:(void)FileSystem::remove(m_test_directory, FileSystem::RecursionMode::Allowed);}- ErrorOr<NonnullOwnPtr<TestApplication>> create_application()+ ErrorOr<NonnullOwnPtr<TestApplication>> create_application(bool test_mode = true){- auto make_test_dir = [&](auto dir, auto env) -> ErrorOr<void> {- auto path = ByteString::formatted("{}/{}", m_test_directory, dir);- TRY(Core::Directory::create(path, Core::Directory::CreateDirectories::Yes));- TRY(Core::Environment::set(env, path, Core::Environment::Overwrite::Yes));- return {};- };-- TRY(make_test_dir("config"sv, "XDG_CONFIG_HOME"sv));- TRY(make_test_dir("data"sv, "XDG_DATA_HOME"sv));+ auto profile_path = LexicalPath::join(m_test_directory, "profile"sv).string();+ Vector<StringView> argument_strings { "ladybird"sv };++ if (test_mode) {+ argument_strings.append("--test-mode"sv);+ } else {+ argument_strings.append("--profile-path"sv);+ argument_strings.append(profile_path);+ }Main::Arguments arguments {.argc = 0,.argv = nullptr,- .strings = Span<StringView> { &m_executable, 1 }+ .strings = argument_strings,};#if defined(LADYBIRD_BINARY_PATH)@@ -82,7 +80,6 @@ public:private:Core::EventLoop m_event_loop;- StringView m_executable { "ladybird"sv };ByteString m_test_directory;};@@ -97,7 +94,7 @@ TEST_CASE(hsts_store_synchronized_on_destruction)TestEnvironment environment;ByteString database_path;{- auto app = TRY_OR_FAIL(environment.create_application());+ auto app = TRY_OR_FAIL(environment.create_application(false));auto view = WebView::HeadlessWebView::create(TRY_OR_FAIL(load_test_theme()), { 1000, 700 });database_path = app->profile().paths().data;@@ -170,3 +167,54 @@ TEST_CASE(application_destruction_with_pending_web_ui_callbacks)EXPECT(!lifetime.web_ui);}}++TEST_CASE(test_mode_uses_a_fresh_profile_and_removes_it_on_destruction)+{+ TestEnvironment environment;+ ByteString user_config_path;+ {+ auto app = TRY_OR_FAIL(environment.create_application(false));+ EXPECT(!app->profile().is_temporary());+ user_config_path = app->profile().paths().config;+ app->settings().set_languages({ "fr"_string });+ app->settings().set_content_blocker_enabled(false);+ app->settings().set_custom_content_blocker_filters("example.com##.advertisement"_string);+ }++ ByteString first_test_root;+ {+ auto app = TRY_OR_FAIL(environment.create_application());+ EXPECT(app->profile().is_temporary());+ EXPECT_NE(app->profile().paths().config, user_config_path);+ first_test_root = app->profile().paths().identity;+ EXPECT(FileSystem::is_directory(first_test_root));+ EXPECT_NE(app->settings().languages().first(), "fr"sv);+ EXPECT(app->settings().content_blocker_enabled());+ EXPECT(app->settings().custom_content_blocker_filters().is_empty());+ EXPECT(!app->has_content_blocking_rules());++ // Changes made during a test must not affect the next run or the user's profile.+ app->settings().set_languages({ "de"_string });+ app->settings().set_content_blocker_enabled(false);+ EXPECT(!app->content_blocking_enabled());+ app->settings().set_custom_content_blocker_filters("example.com##.test-advertisement"_string);+ EXPECT(app->has_content_blocking_rules());+ }+ EXPECT(!FileSystem::exists(first_test_root));++ ByteString second_test_root;+ {+ auto app = TRY_OR_FAIL(environment.create_application());+ second_test_root = app->profile().paths().identity;+ EXPECT_NE(first_test_root, second_test_root);+ EXPECT_NE(app->settings().languages().first(), "de"sv);+ EXPECT(app->settings().content_blocker_enabled());+ EXPECT(!app->has_content_blocking_rules());+ }+ EXPECT(!FileSystem::exists(second_test_root));++ auto user_settings = WebView::Settings::create(LexicalPath::join(user_config_path, "Settings.json"sv).string());+ EXPECT_EQ(user_settings.languages().first(), "fr"sv);+ EXPECT(!user_settings.content_blocker_enabled());+ EXPECT_EQ(user_settings.custom_content_blocker_filters(), "example.com##.advertisement"sv);+}