Truly independent web browser
LibWebView+UI/Qt: Say when a tab was stopped for its memory use
Andreas Klingdf0be72d3de3
diff --git a/Libraries/LibWebView/Application.cpp b/Libraries/LibWebView/Application.cppindex 75a48ba0f0a..372c8c0f077 100644--- a/Libraries/LibWebView/Application.cpp+++ b/Libraries/LibWebView/Application.cpp@@ -1883,6 +1883,12 @@ ErrorOr<void> Application::launch_services()m_process_manager->on_process_exited = [this](Process&& process, Optional<int> exit_status) {process_did_exit(move(process), exit_status);};+ m_process_manager->on_process_exceeded_memory_limit = [](Process& process, u64) {+ if (process.type() != ProcessType::WebContent)+ return;+ if (auto client = process.client<WebContentClient>())+ client->did_exceed_memory_limit();+ };m_process_manager->set_web_content_memory_limit(m_browser_options.web_content_memory_limit_in_bytes.value_or(ProcessManager::default_web_content_memory_limit()));if (m_browser_options.disable_sql_database == DisableSQLDatabase::No) {@@ -2520,6 +2526,13 @@ static void load_page_for_info_and_exit(Core::EventLoop& event_loop, HeadlessWebevent_loop.quit(1);});};+ // NB: A page that was ended for its memory use is not loaded again, so its load never finishes.+ view.on_web_content_crashed = [&event_loop](auto crash_reason) {+ if (crash_reason != ViewImplementation::WebContentCrashReason::ExceededMemoryLimit)+ return;+ warnln("Unable to get page info: the page was ended for its memory use");+ event_loop.quit(1);+ };view.load(url);}diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cppindex 5b03fa47dc6..8484b8cfe99 100644--- a/Libraries/LibWebView/ViewImplementation.cpp+++ b/Libraries/LibWebView/ViewImplementation.cpp@@ -4373,7 +4373,7 @@ void ViewImplementation::dump_session_history(StringView reason, SessionHistoryDhistory_log_entries(traversable().session_history()));}-void ViewImplementation::handle_web_content_process_crash()+void ViewImplementation::handle_web_content_process_crash(WebContentCrashReason crash_reason){auto failed_url = url();if (auto browser_ui_load_url = this->browser_ui_load_url(); browser_ui_load_url.has_value())@@ -4426,8 +4426,12 @@ void ViewImplementation::handle_web_content_process_crash()if (auto const& headless_mode = Application::browser_options().headless_mode; headless_mode.has_value())recovery_mode = *headless_mode == HeadlessMode::Test ? RecoveryMode::PrepareForNextNavigation : RecoveryMode::Restore;- if (recovery_mode == RecoveryMode::ShowOverlay)- dbgln("\033[31;1mWebContent process crashed!\033[0m Last page loaded: {}", failed_url);+ if (recovery_mode == RecoveryMode::ShowOverlay) {+ if (crash_reason == WebContentCrashReason::ExceededMemoryLimit)+ dbgln("\033[31;1mWebContent process was ended for its memory use!\033[0m Last page loaded: {}", failed_url);+ else+ dbgln("\033[31;1mWebContent process crashed!\033[0m Last page loaded: {}", failed_url);+ }reset_page_media_state();@@ -4445,14 +4449,17 @@ void ViewImplementation::handle_web_content_process_crash()respawn_web_content_process_after_crash();// A repeatedly crashing headless page is left dormant so the next WebDriver test or explicit navigation can- // proceed without feeding an automatic restore loop.- if (recovery_mode == RecoveryMode::Restore && !crashed_repeatedly) {+ // proceed without feeding an automatic restore loop. So is a page that was ended for its memory use, because+ // the restored page would grow to the limit again.+ auto restores_page = !crashed_repeatedly && crash_reason != WebContentCrashReason::ExceededMemoryLimit;+ if (recovery_mode == RecoveryMode::Restore && restores_page) {recover_current_session_history_entry_with_history_operation();} else if (recovery_mode == RecoveryMode::ShowOverlay) {traversable().abandon_history_operations();set_crash_state(CrashState {.failed_url = move(failed_url),.navigation_to_retry = move(navigation_to_retry),+ .exceeded_memory_limit = crash_reason == WebContentCrashReason::ExceededMemoryLimit,});} else {traversable().abandon_history_operations();diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.hindex f71dce3491d..815ccd8e5e1 100644--- a/Libraries/LibWebView/ViewImplementation.h+++ b/Libraries/LibWebView/ViewImplementation.h@@ -266,6 +266,7 @@ public:bool crash_overlay_active() const { return m_crash_state.has_value(); }String crash_overlay_failed_url() const;+ bool crash_overlay_is_for_memory_limit() const { return m_crash_state.has_value() && m_crash_state->exceeded_memory_limit; }Optional<ByteString> crash_report_name() const;Optional<String> crash_report_website() const;@@ -712,6 +713,7 @@ public:enum class WebContentCrashReason {ProcessCrash,RejectedIPC,+ ExceededMemoryLimit,};Function<void(WebContentCrashReason)> on_web_content_crashed;Function<void(bool)> on_crash_overlay_state_change;@@ -890,7 +892,7 @@ protected:void retry_navigation(NavigationToRetry, Web::Bindings::NavigationHistoryBehavior);struct CrashState;- void handle_web_content_process_crash();+ void handle_web_content_process_crash(WebContentCrashReason);void did_save_crash_report(ByteString report_name);void respawn_web_content_process_after_crash();void prepare_for_navigation_after_crash(Optional<NavigationToRetry> navigation_to_retry = {});@@ -943,6 +945,7 @@ protected:Optional<NavigationToRetry> navigation_to_retry;bool recovery_started { false };ByteString report_name {};+ bool exceeded_memory_limit { false };};Optional<CrashState> m_crash_state;diff --git a/Libraries/LibWebView/WebContentClient.cpp b/Libraries/LibWebView/WebContentClient.cppindex 97c9bc0cdb6..8d20947addd 100644--- a/Libraries/LibWebView/WebContentClient.cpp+++ b/Libraries/LibWebView/WebContentClient.cpp@@ -634,7 +634,11 @@ void WebContentClient::did_lose_process()return;// The views are told deferred, and looked up by ID, in case they are destroyed before then.- auto crash_reason = m_rejected_ipc ? ViewImplementation::WebContentCrashReason::RejectedIPC : ViewImplementation::WebContentCrashReason::ProcessCrash;+ auto crash_reason = ViewImplementation::WebContentCrashReason::ProcessCrash;+ if (m_rejected_ipc)+ crash_reason = ViewImplementation::WebContentCrashReason::RejectedIPC;+ else if (m_exceeded_memory_limit)+ crash_reason = ViewImplementation::WebContentCrashReason::ExceededMemoryLimit;for (auto const& lost : lost_pages) {if (!lost.view_id.has_value())continue;@@ -645,7 +649,7 @@ void WebContentClient::did_lose_process()if (!view.has_value())return;if (shows_crash_screen)- view->handle_web_content_process_crash();+ view->handle_web_content_process_crash(crash_reason);if (view->on_web_content_crashed)view->on_web_content_crashed(crash_reason);});diff --git a/Libraries/LibWebView/WebContentClient.h b/Libraries/LibWebView/WebContentClient.hindex 507ab8858ff..d362f09c08f 100644--- a/Libraries/LibWebView/WebContentClient.h+++ b/Libraries/LibWebView/WebContentClient.h@@ -122,6 +122,8 @@ public:Optional<i32> compositor_connection_id(Badge<Application>) const { return m_compositor_connection_id; }virtual void did_misbehave(StringView message_name, StringView reason) override;+ // The process is about to be ended for using more memory than the limit.+ void did_exceed_memory_limit() { m_exceeded_memory_limit = true; }enum class HTTPCookieAccess : u8 {Allowed,// A WebDriver cookie command runs, but for another document, which the renderer may not have heard of yet.@@ -272,6 +274,7 @@ private:RequestServerSiteBindings m_request_server_site_bindings;bool m_requested_close { false };bool m_rejected_ipc { false };+ bool m_exceeded_memory_limit { false };// The site of the documents this process hosts, if it has only hosted those of one.Optional<Utf16String> m_site_of_hosted_documents;Optional<u64> m_image_decoder_client_generation;diff --git a/Services/Hub/HubClient.ipc b/Services/Hub/HubClient.ipcindex 87d96588789..d294704f8e4 100644--- a/Services/Hub/HubClient.ipc+++ b/Services/Hub/HubClient.ipc@@ -206,6 +206,7 @@ struct Hub::ViewState {bool is_loading;bool crash_overlay_active;String crash_overlay_failed_url;+ bool crash_overlay_is_for_memory_limit;Optional<ByteString> crash_report_name;Optional<String> crash_report_website;double zoom_level;diff --git a/Services/Hub/View.cpp b/Services/Hub/View.cppindex c8185ffb351..058db096322 100644--- a/Services/Hub/View.cpp+++ b/Services/Hub/View.cpp@@ -663,6 +663,7 @@ ViewState View::current_state() conststate.crash_report_name = crash_report_name();state.crash_report_website = crash_report_website();state.crash_overlay_failed_url = crash_overlay_failed_url();+ state.crash_overlay_is_for_memory_limit = crash_overlay_is_for_memory_limit();state.host_for_settings = current_host_for_settings();state.can_report_website_issue = can_report_website_issue();state.can_undo = can_undo();diff --git a/UI/Qt/RemoteView.h b/UI/Qt/RemoteView.hindex 0baef7c6a89..06707cef17f 100644--- a/UI/Qt/RemoteView.h+++ b/UI/Qt/RemoteView.h@@ -118,6 +118,7 @@ public:bool is_loading() const { return m_state.is_loading; }bool crash_overlay_active() const { return m_state.crash_overlay_active; }String crash_overlay_failed_url() const { return m_state.crash_overlay_failed_url; }+ bool crash_overlay_is_for_memory_limit() const { return m_state.crash_overlay_is_for_memory_limit; }Optional<ByteString> crash_report_name() const { return m_state.crash_report_name; }Optional<String> crash_report_website() const { return m_state.crash_report_website; }void traverse_the_history_to_step(i32 step) { HubConnection::the().async_view_traverse_the_history_to_step(view_id(), step); }diff --git a/UI/Qt/WebContentView.cpp b/UI/Qt/WebContentView.cppindex 1f1e64cccb1..5d873c2d1c0 100644--- a/UI/Qt/WebContentView.cpp+++ b/UI/Qt/WebContentView.cpp@@ -1520,7 +1520,7 @@ void WebContentView::set_crash_overlay_visible(bool visible)heading_layout->setContentsMargins(0, 0, 0, 0);heading_layout->setSpacing(ReportReview::item_spacing);- auto* title = ReportReview::create_title(tr("This tab crashed"), heading);+ m_crash_overlay_title = ReportReview::create_title({}, heading);m_crash_overlay_url = new CrashOverlayUrlLabel(heading);@@ -1542,7 +1542,7 @@ void WebContentView::set_crash_overlay_visible(bool visible)m_crash_overlay_reload_shortcut->setContext(Qt::WidgetWithChildrenShortcut);QObject::connect(m_crash_overlay_reload_shortcut, &QShortcut::activated, m_crash_overlay_reload_button, &QPushButton::click);- heading_layout->addWidget(title);+ heading_layout->addWidget(m_crash_overlay_title);heading_layout->addWidget(m_crash_overlay_url);heading_layout->addWidget(m_crash_overlay_message);@@ -1574,7 +1574,13 @@ void WebContentView::set_crash_overlay_visible(bool visible)if (m_crash_report_container)m_crash_report_container->hide();m_crash_overlay_url->show();- m_crash_overlay_message->setText(crash_screen_message());+ if (crash_overlay_is_for_memory_limit()) {+ m_crash_overlay_title->setText(tr("This tab ran out of memory"));+ m_crash_overlay_message->setText(tr("This page used too much memory, so it was stopped."));+ } else {+ m_crash_overlay_title->setText(tr("This tab crashed"));+ m_crash_overlay_message->setText(crash_screen_message());+ }m_crash_overlay_reload_button->show();m_crash_overlay_reload_shortcut->setEnabled(true);diff --git a/UI/Qt/WebContentView.h b/UI/Qt/WebContentView.hindex 5eb4f846287..7181fc40b30 100644--- a/UI/Qt/WebContentView.h+++ b/UI/Qt/WebContentView.h@@ -342,6 +342,7 @@ private:PageOverlay* m_crash_overlay { nullptr };CrashOverlayUrlLabel* m_crash_overlay_url { nullptr };+ QLabel* m_crash_overlay_title { nullptr };QLabel* m_crash_overlay_message { nullptr };QPushButton* m_crash_overlay_reload_button { nullptr };QShortcut* m_crash_overlay_reload_shortcut { nullptr };