Skip to content

Truly independent web browser

LibWeb+WebContent+LibWebView: Add window close preflights

Sam Atkins3fcd77b61d29
diff --git a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
index f89eaa49776..2c300707c34 100644
--- a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
+++ b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
@@ -20,11 +20,13 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(CheckUnloadingCanceledState);
CheckUnloadingCanceledState::CheckUnloadingCanceledState(
+ GC::Ptr<LocalTraversableNavigable> owner,
GC::Ptr<LocalTraversableNavigable> traversable,
Optional<UserNavigationInvolvement> user_involvement,
UnloadPromptShown unload_prompt_shown,
GC::Ref<GC::Function<void(Result, UnloadPromptShown)>> callback)
: m_unload_prompt_shown(unload_prompt_shown)
+ , m_owner(owner)
, m_traversable(traversable)
, m_user_involvement(user_involvement)
, m_callback(callback)
@@ -45,13 +47,23 @@ CheckUnloadingCanceledState::CheckUnloadingCanceledState(
void CheckUnloadingCanceledState::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
- for (auto& doc : m_phase2_documents)
- visitor.visit(doc);
+ visitor.visit(m_phase2_documents);
+ visitor.visit(m_documents_already_fired);
visitor.visit(m_traversable);
+ visitor.visit(m_owner);
visitor.visit(m_callback);
+ visitor.visit(m_close_preflight_callback);
visitor.visit(m_timeout);
}
+void CheckUnloadingCanceledState::adopt_for_close(GC::Ref<GC::Function<void(bool)>> callback)
+{
+ VERIFY(!m_close_preflight_callback);
+ m_close_preflight_callback = callback;
+ m_adopted_for_close = true;
+ ++m_phase2_generation;
+}
+
void CheckUnloadingCanceledState::start(Vector<GC::Root<LocalNavigable>> const& navigables_that_need_before_unload, RefPtr<SessionHistoryEntry> target_entry)
{
// 1. Let documentsToFireBeforeunload be the active document of each item in navigablesThatNeedBeforeUnload.
@@ -97,6 +109,52 @@ void CheckUnloadingCanceledState::start(Vector<GC::Root<LocalNavigable>> const&
start_phase2();
}
+bool CheckUnloadingCanceledState::document_was_already_fired(DOM::Document const& document) const
+{
+ return m_documents_already_fired.find_if([&](auto const& fired_document) {
+ return fired_document.ptr() == &document;
+ }) != m_documents_already_fired.end();
+}
+
+void CheckUnloadingCanceledState::fire_beforeunload(DOM::Document& document, bool unload_prompt_shown, bool& prompt_shown, bool& prompt_canceled)
+{
+ if (!document_was_already_fired(document))
+ m_documents_already_fired.append(document);
+
+ if (m_owner)
+ m_owner->set_active_beforeunload_check(Badge<CheckUnloadingCanceledState> {}, this);
+ auto [did_show_prompt, did_cancel] = document.steps_to_fire_beforeunload(unload_prompt_shown);
+ prompt_shown = did_show_prompt;
+ prompt_canceled = did_cancel;
+ if (m_owner && m_owner->active_beforeunload_check(Badge<CheckUnloadingCanceledState> {}).ptr() == this)
+ m_owner->set_active_beforeunload_check(Badge<CheckUnloadingCanceledState> {}, nullptr);
+}
+
+void CheckUnloadingCanceledState::continue_as_close_after_adoption(bool prompt_shown, bool prompt_canceled)
+{
+ if (prompt_shown)
+ m_unload_prompt_shown = UnloadPromptShown::Yes;
+ if (prompt_canceled) {
+ finish(Result::CanceledByBeforeUnload);
+ return;
+ }
+
+ m_running_adopted_close = true;
+ m_phase2_documents.clear();
+ m_remaining_phase2_tasks = 0;
+ if (m_owner) {
+ if (auto active_document = m_owner->active_document()) {
+ for (auto& navigable : active_document->inclusive_descendant_navigables()) {
+ auto* local_navigable = as_if<LocalNavigable>(navigable.ptr());
+ auto document = local_navigable ? local_navigable->active_document() : GC::Ptr<DOM::Document> {};
+ if (document && !document_was_already_fired(*document))
+ m_phase2_documents.append(*document);
+ }
+ }
+ }
+ start_phase2();
+}
+
bool CheckUnloadingCanceledState::has_pending_before_unload_dialog() const
{
if (m_traversable) {
@@ -117,7 +175,13 @@ void CheckUnloadingCanceledState::start_phase1()
// 1. if needsBeforeunload is true, then:
if (m_needs_beforeunload) {
// 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given traversable's active document and false.
- auto [unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document] = m_traversable->active_document()->steps_to_fire_beforeunload(false);
+ bool unload_prompt_shown_for_this_document;
+ bool unload_prompt_canceled_by_this_document;
+ fire_beforeunload(*m_traversable->active_document(), false, unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document);
+ if (m_adopted_for_close && !m_running_adopted_close) {
+ continue_as_close_after_adoption(unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document);
+ return;
+ }
// 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true.
if (unload_prompt_shown_for_this_document)
@@ -177,19 +241,30 @@ void CheckUnloadingCanceledState::start_phase2()
m_remaining_phase2_tasks = m_phase2_documents.size();
// 7. For each document of documentsToFireBeforeunload, queue a global task on the navigation and traversal task source given document's relevant global object to run the steps:
+ auto generation = m_phase2_generation;
for (auto& document : m_phase2_documents) {
// AD-HOC: Queue with a null document instead of using queue_global_task. Tasks associated with a document
// are only runnable when fully active. In the async state machine, documents can become non
// fully-active between queue and execution time, causing the task to be permanently stuck.
// A null-document task is always runnable; we check validity inside.
- queue_a_task(Task::Source::NavigationAndTraversal, nullptr, nullptr, GC::create_function(heap(), [this, document] {
+ queue_a_task(Task::Source::NavigationAndTraversal, nullptr, nullptr, GC::create_function(heap(), [this, document, generation] {
+ if (generation != m_phase2_generation)
+ return;
if (document->has_been_destroyed() || !document->is_fully_active()) {
did_complete_phase2_task();
return;
}
// 1. Let (unloadPromptShownForThisDocument, unloadPromptCanceledByThisDocument) be the result of running the steps to fire beforeunload given document and unloadPromptShown.
- auto [unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document] = document->steps_to_fire_beforeunload(m_unload_prompt_shown == UnloadPromptShown::Yes);
+ bool unload_prompt_shown_for_this_document;
+ bool unload_prompt_canceled_by_this_document;
+ fire_beforeunload(*document, m_unload_prompt_shown == UnloadPromptShown::Yes, unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document);
+ if (m_adopted_for_close && !m_running_adopted_close) {
+ continue_as_close_after_adoption(unload_prompt_shown_for_this_document, unload_prompt_canceled_by_this_document);
+ return;
+ }
+ if (generation != m_phase2_generation)
+ return;
// 2. If unloadPromptShownForThisDocument is true, then set unloadPromptShown to true.
if (unload_prompt_shown_for_this_document)
@@ -223,6 +298,12 @@ void CheckUnloadingCanceledState::finish(Result final_result)
return;
m_completed = true;
m_timeout->stop();
+ if (m_adopted_for_close) {
+ m_callback->function()(final_result == Result::CanceledByBeforeUnload ? Result::CanceledByBeforeUnload : Result::CanceledByNavigate, m_unload_prompt_shown);
+ VERIFY(m_close_preflight_callback);
+ m_close_preflight_callback->function()(final_result == Result::Continue);
+ return;
+ }
m_callback->function()(final_result, m_unload_prompt_shown);
}
diff --git a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
index 9a71c9c1eb1..ae0df26e5be 100644
--- a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
+++ b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
@@ -12,7 +12,6 @@
#include <LibGC/Cell.h>
#include <LibGC/Function.h>
#include <LibGC/Ptr.h>
-#include <LibGC/RootVector.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/ApplyHistoryStep.h>
#include <LibWeb/HTML/UserNavigationInvolvement.h>
@@ -28,6 +27,7 @@ public:
static constexpr int TIMEOUT_MS = 15000;
CheckUnloadingCanceledState(
+ GC::Ptr<LocalTraversableNavigable> owner,
GC::Ptr<LocalTraversableNavigable> traversable,
Optional<UserNavigationInvolvement> user_involvement,
UnloadPromptShown unload_prompt_shown,
@@ -35,9 +35,13 @@ public:
virtual void visit_edges(Visitor&) override;
+ void adopt_for_close(GC::Ref<GC::Function<void(bool)>> callback);
void start(Vector<GC::Root<LocalNavigable>> const& navigables_that_need_before_unload, RefPtr<SessionHistoryEntry> target_entry);
private:
+ bool document_was_already_fired(DOM::Document const&) const;
+ void fire_beforeunload(DOM::Document&, bool unload_prompt_shown, bool& prompt_shown, bool& prompt_canceled);
+ void continue_as_close_after_adoption(bool prompt_shown, bool prompt_canceled);
bool has_pending_before_unload_dialog() const;
void start_phase1();
void phase1_completed();
@@ -48,14 +52,20 @@ private:
Result m_final_status { Result::Continue };
UnloadPromptShown m_unload_prompt_shown { UnloadPromptShown::No };
bool m_completed { false };
+ GC::Ptr<LocalTraversableNavigable> m_owner;
bool m_needs_beforeunload { false };
size_t m_remaining_phase2_tasks { 0 };
Vector<GC::Ref<DOM::Document>> m_phase2_documents;
+ Vector<GC::Ref<DOM::Document>> m_documents_already_fired;
GC::Ptr<LocalTraversableNavigable> m_traversable;
RefPtr<SessionHistoryEntry> m_target_entry;
Optional<UserNavigationInvolvement> m_user_involvement;
GC::Ref<GC::Function<void(Result, UnloadPromptShown)>> m_callback;
+ GC::Ptr<GC::Function<void(bool)>> m_close_preflight_callback;
GC::Ref<Platform::Timer> m_timeout;
+ u64 m_phase2_generation { 0 };
+ bool m_adopted_for_close { false };
+ bool m_running_adopted_close { false };
};
}
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
index e589672de69..b3c860c1195 100644
--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp
+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
@@ -224,7 +224,7 @@ void HistoryExecutor::run_ui_beforeunload_check(Vector<CrossProcessId> navigable
if (auto navigable = local_navigable_with_id(navigable_id); navigable && !navigable->has_been_destroyed() && navigable->active_document())
navigables.append(*navigable);
}
- check_if_unloading_is_canceled(move(navigables), traversable, move(target_entry), user_involvement_for_navigate_event, unload_prompt_shown, on_complete);
+ check_if_unloading_is_canceled(move(navigables), traversable, traversable, move(target_entry), user_involvement_for_navigate_event, unload_prompt_shown, on_complete);
}
static bool is_same_document_push_or_replace(Optional<Bindings::NavigationType> navigation_type, SessionHistoryEntry const& target_entry, Optional<UniqueNodeID> displayed_document_id)
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 81afff53c24..dfe99dd4431 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -3815,7 +3815,7 @@ void LocalNavigable::run_navigation_unload_check(Utf16String const& navigation_i
// 1. Let unloadPromptCanceled be the result of checking if unloading is user-canceled for navigable's active document's inclusive descendant navigables.
// NB: This page checks the documents it hosts, last: the UI process, which requested the check, ran it in the
// pages hosting the others first, and whether one of them showed the prompt comes with the request.
- check_if_unloading_is_canceled(hosted_inclusive_descendant_navigables(), {}, {}, {}, unload_prompt_shown,
+ check_if_unloading_is_canceled(hosted_inclusive_descendant_navigables(), as_if<LocalTraversableNavigable>(this), {}, {}, {}, unload_prompt_shown,
GC::create_function(heap(), [this, navigation_id, completion_steps](CheckIfUnloadingIsCanceledResult unload_prompt_canceled, UnloadPromptShown) {
if (has_been_destroyed() || !active_window()) {
completion_steps->function()(false);
@@ -4298,6 +4298,7 @@ bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& docu
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#checking-if-unloading-is-canceled
void check_if_unloading_is_canceled(
Vector<GC::Root<LocalNavigable>> navigables_that_need_before_unload,
+ GC::Ptr<LocalTraversableNavigable> owner,
GC::Ptr<LocalTraversableNavigable> traversable,
RefPtr<SessionHistoryEntry> target_entry,
Optional<UserNavigationInvolvement> user_involvement_for_navigate_events,
@@ -4305,6 +4306,7 @@ void check_if_unloading_is_canceled(
GC::Ref<GC::Function<void(CheckIfUnloadingIsCanceledResult, UnloadPromptShown)>> callback)
{
auto state = GC::Heap::the().allocate<CheckUnloadingCanceledState>(
+ owner,
traversable,
user_involvement_for_navigate_events,
unload_prompt_shown,
@@ -4312,6 +4314,35 @@ void check_if_unloading_is_canceled(
state->start(navigables_that_need_before_unload, move(target_entry));
}
+void LocalTraversableNavigable::request_close_preflight(GC::Ref<GC::Function<void(bool)>> callback)
+{
+ if (page().pending_dialog() != Page::PendingDialog::None) {
+ if (page().pending_dialog() == Page::PendingDialog::BeforeUnload && m_active_beforeunload_check) {
+ m_active_beforeunload_check->adopt_for_close(callback);
+ return;
+ }
+ callback->function()(false);
+ return;
+ }
+
+ auto document = active_document();
+ if (!document) {
+ callback->function()(true);
+ return;
+ }
+
+ auto state_callback = GC::create_function(heap(), [callback](CheckIfUnloadingIsCanceledResult result, UnloadPromptShown) {
+ callback->function()(result == CheckIfUnloadingIsCanceledResult::Continue);
+ });
+ auto state = heap().allocate<CheckUnloadingCanceledState>(GC::Ptr<LocalTraversableNavigable> { this }, GC::Ptr<LocalTraversableNavigable> {}, Optional<UserNavigationInvolvement> {}, UnloadPromptShown::No, state_callback);
+ Vector<GC::Root<LocalNavigable>> local_navigables;
+ for (auto& navigable : document->inclusive_descendant_navigables()) {
+ if (auto* local_navigable = as_if<LocalNavigable>(navigable.ptr()))
+ local_navigables.append(*local_navigable);
+ }
+ state->start(local_navigables, {});
+}
+
// https://html.spec.whatwg.org/multipage/browsing-the-web.html#finalize-a-cross-document-navigation
// NB: The UI process appends the steps finalizing the navigation, and runs them, once this process reports that
// populating historyEntry's document is over.
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 92efe5a9f19..8c3886bf598 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -719,7 +719,7 @@ WEB_API GC::Ptr<LocalNavigable> local_navigable_with_id(CrossProcessId);
bool navigation_must_be_a_replace(URL::URL const& url, DOM::Document const& document);
void finalize_a_cross_document_navigation(GC::Ref<LocalNavigable>, HistoryHandlingBehavior, UserNavigationInvolvement, NonnullRefPtr<SessionHistoryEntry>, GC::Ptr<DOM::Document> pending_document, Utf16String navigation_id);
-WEB_API void check_if_unloading_is_canceled(Vector<GC::Root<LocalNavigable>> navigables_that_need_before_unload, GC::Ptr<LocalTraversableNavigable> traversable, RefPtr<SessionHistoryEntry> target_entry, Optional<UserNavigationInvolvement> user_involvement_for_navigate_events, UnloadPromptShown, GC::Ref<GC::Function<void(CheckIfUnloadingIsCanceledResult, UnloadPromptShown)>> callback);
+WEB_API void check_if_unloading_is_canceled(Vector<GC::Root<LocalNavigable>> navigables_that_need_before_unload, GC::Ptr<LocalTraversableNavigable> owner, GC::Ptr<LocalTraversableNavigable> traversable, RefPtr<SessionHistoryEntry> target_entry, Optional<UserNavigationInvolvement> user_involvement_for_navigate_events, UnloadPromptShown, GC::Ref<GC::Function<void(CheckIfUnloadingIsCanceledResult, UnloadPromptShown)>> callback);
void perform_url_and_history_update_steps(DOM::Document& document, URL::URL new_url, Optional<StorageSerializationRecord> = {}, HistoryHandlingBehavior history_handling = HistoryHandlingBehavior::Replace);
}
diff --git a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
index 5ebf152631c..f15bdfaa760 100644
--- a/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalTraversableNavigable.cpp
@@ -12,6 +12,7 @@
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Geolocation/GeolocationCoordinates.h>
#include <LibWeb/HTML/BrowsingContext.h>
+#include <LibWeb/HTML/CheckUnloadingCanceledState.h>
#include <LibWeb/HTML/DocumentState.h>
#include <LibWeb/HTML/History.h>
#include <LibWeb/HTML/HistoryExecutor.h>
@@ -332,4 +333,10 @@ void LocalTraversableNavigable::destroy_top_level_traversable()
remove_from_all_local_navigables();
}
+void LocalTraversableNavigable::visit_edges(Visitor& visitor)
+{
+ Base::visit_edges(visitor);
+ visitor.visit(m_active_beforeunload_check);
+}
+
}
diff --git a/Libraries/LibWeb/HTML/LocalTraversableNavigable.h b/Libraries/LibWeb/HTML/LocalTraversableNavigable.h
index 19fc58080ac..68b0a556cf9 100644
--- a/Libraries/LibWeb/HTML/LocalTraversableNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalTraversableNavigable.h
@@ -8,6 +8,7 @@
#pragma once
+#include <AK/Badge.h>
#include <AK/HashMap.h>
#include <AK/HashTable.h>
#include <AK/Utf16String.h>
@@ -15,6 +16,7 @@
#include <LibWeb/Bindings/NavigationType.h>
#include <LibWeb/Export.h>
#include <LibWeb/HTML/ApplyHistoryStep.h>
+#include <LibWeb/HTML/CheckUnloadingCanceledState.h>
#include <LibWeb/HTML/LocalNavigable.h>
#include <LibWeb/Page/Page.h>
#include <LibWebCommon/HTML/VisibilityState.h>
@@ -48,6 +50,9 @@ public:
Yes,
};
void close_top_level_traversable(PromptToUnload = PromptToUnload::Yes);
+ void request_close_preflight(GC::Ref<GC::Function<void(bool)>> callback);
+ GC::Ptr<CheckUnloadingCanceledState> active_beforeunload_check(Badge<CheckUnloadingCanceledState>) const { return m_active_beforeunload_check; }
+ void set_active_beforeunload_check(Badge<CheckUnloadingCanceledState>, GC::Ptr<CheckUnloadingCanceledState> check) { m_active_beforeunload_check = check; }
void close_top_level_traversable_from_script(Navigable const& source);
void definitely_close_top_level_traversable(PromptToUnload = PromptToUnload::Yes);
void run_ui_traversable_close_unload_task();
@@ -62,6 +67,7 @@ private:
LocalTraversableNavigable(GC::Ref<Page>);
virtual bool is_traversable() const override { return true; }
+ virtual void visit_edges(Visitor&) override;
// WebContent needs the canonical top-level entry count synchronously for is_script_closable().
u64 m_session_history_entry_count { 1 };
@@ -69,6 +75,8 @@ private:
// https://html.spec.whatwg.org/multipage/document-sequences.html#is-created-by-web-content
bool m_is_created_by_web_content { false };
+ GC::Ptr<CheckUnloadingCanceledState> m_active_beforeunload_check;
+
// AD-HOC: A forced close may supersede a prompted close while its beforeunload check is still pending.
bool m_close_steps_have_been_appended { false };
diff --git a/Libraries/LibWebView/ViewImplementation.cpp b/Libraries/LibWebView/ViewImplementation.cpp
index 43519da80b2..1f9fc31dedc 100644
--- a/Libraries/LibWebView/ViewImplementation.cpp
+++ b/Libraries/LibWebView/ViewImplementation.cpp
@@ -2663,6 +2663,17 @@ void ViewImplementation::did_change_display_page(Badge<CanonicalNavigable>, RefP
// The tab is displayed by the page hosting its document from now on.
void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_page)
{
+ auto new_page_id = page().id();
+ Vector<u64> stale_close_preflight_requests;
+ for (auto const& [request_id, request] : m_pending_close_preflight_requests) {
+ if (request.page_id != new_page_id)
+ stale_close_preflight_requests.append(request_id);
+ }
+ for (auto request_id : stale_close_preflight_requests) {
+ if (auto request = m_pending_close_preflight_requests.take(request_id); request.has_value())
+ request->on_complete(false);
+ }
+
// Screenshot and page info requests went to the outgoing page, so only it could answer them.
if (auto pending_screenshot = move(m_pending_screenshot))
pending_screenshot->reject(Error::from_string_literal("The displayed page changed before taking the screenshot"));
@@ -2731,6 +2742,16 @@ void ViewImplementation::display_page_changed(RefPtr<WebContentPage> previous_pa
void ViewImplementation::did_lose_page(Badge<CanonicalTraversable>, WebContentPage& page, WebContentProcessLost process_lost)
{
+ Vector<u64> lost_close_preflight_requests;
+ for (auto const& [request_id, request] : m_pending_close_preflight_requests) {
+ if (request.page_id == page.id())
+ lost_close_preflight_requests.append(request_id);
+ }
+ for (auto request_id : lost_close_preflight_requests) {
+ if (auto request = m_pending_close_preflight_requests.take(request_id); request.has_value())
+ request->on_complete(false);
+ }
+
// NB: The view fails the commands of its own page when it recovers from its process's crash.
if (traversable().display_page() == &page)
return;
@@ -4456,6 +4477,34 @@ void ViewImplementation::force_close()
page().async_force_close();
}
+void ViewImplementation::request_close_preflight(Function<void(bool)> on_complete)
+{
+ auto request_id = m_next_close_preflight_request_id++;
+ auto page_id = page().id();
+ m_pending_close_preflight_requests.set(request_id, { page_id, move(on_complete) });
+ page().async_request_close_preflight(request_id);
+}
+
+void ViewImplementation::did_complete_close_preflight(Badge<WebContentPage>, u64 request_id, bool approved)
+{
+ if (auto request = m_pending_close_preflight_requests.take(request_id); request.has_value())
+ request->on_complete(approved);
+}
+
+Function<void()> ViewImplementation::prepare_for_close_without_prompting()
+{
+ if (m_debugger_paused) {
+ resume_debugger(DebuggerResumeMode::Continue);
+ set_debugger_paused(false);
+ }
+
+ NonnullRefPtr closing_page = page();
+ closing_page->set_detached_close_pending(true);
+ return [closing_page = move(closing_page)] {
+ closing_page->async_request_close_without_prompting();
+ };
+}
+
Function<void()> ViewImplementation::prepare_for_immediate_close()
{
VERIFY(!needs_beforeunload_check());
diff --git a/Libraries/LibWebView/ViewImplementation.h b/Libraries/LibWebView/ViewImplementation.h
index 0ffc3327b7d..ba45fedc4c3 100644
--- a/Libraries/LibWebView/ViewImplementation.h
+++ b/Libraries/LibWebView/ViewImplementation.h
@@ -423,6 +423,9 @@ public:
void set_user_style_sheet(String const& source);
void request_close();
+ void request_close_preflight(Function<void(bool)> on_complete);
+ void did_complete_close_preflight(Badge<WebContentPage>, u64 request_id, bool approved);
+ Function<void()> prepare_for_close_without_prompting();
void force_close();
Function<void()> prepare_for_immediate_close();
bool needs_beforeunload_check() const;
@@ -848,6 +851,12 @@ protected:
u64 m_next_webdriver_user_prompt_request_id { 0 };
HashMap<u64, Function<void(Web::WebDriver::Response)>> m_pending_webdriver_user_prompt_requests;
+ u64 m_next_close_preflight_request_id { 0 };
+ struct PendingClosePreflightRequest {
+ Web::PageId page_id;
+ Function<void(bool)> on_complete;
+ };
+ HashMap<u64, PendingClosePreflightRequest> m_pending_close_preflight_requests;
struct PendingWebDriverCommand {
NonnullRefPtr<WebContentPage> page;
String name;
diff --git a/Libraries/LibWebView/WebContentPage.cpp b/Libraries/LibWebView/WebContentPage.cpp
index 19e126f0c80..c5cc2ad61dd 100644
--- a/Libraries/LibWebView/WebContentPage.cpp
+++ b/Libraries/LibWebView/WebContentPage.cpp
@@ -2573,6 +2573,12 @@ void WebContentPage::did_close_browsing_context()
}
}
+void WebContentPage::did_complete_close_preflight(u64 request_id, bool approved)
+{
+ if (displays_tab())
+ view().did_complete_close_preflight({}, request_id, approved);
+}
+
void WebContentPage::did_request_select_dropdown(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items)
{
if (auto target = view_position(local_root_id, content_position); target.has_value())
diff --git a/Libraries/LibWebView/WebContentPage.h b/Libraries/LibWebView/WebContentPage.h
index dac844357fd..0649b41a3ad 100644
--- a/Libraries/LibWebView/WebContentPage.h
+++ b/Libraries/LibWebView/WebContentPage.h
@@ -330,6 +330,7 @@ private:
virtual void did_request_delete_all_cookies(u64 request_id, URL::URL url) override;
Messages::WebContentClient::DidRequestNewWebViewResponse did_request_new_web_view(Web::HTML::ActivateTab activate_tab, Web::HTML::WebViewHints hints, Optional<Web::HTML::CrossProcessId> opener_navigable_id, Optional<URL::URL> opener_base_url, Utf16String target_name, Web::HTML::SandboxingFlagSet popup_sandboxing_flag_set);
void did_close_browsing_context();
+ virtual void did_complete_close_preflight(u64 request_id, bool approved) override;
virtual void did_request_select_dropdown(Web::HTML::CrossProcessId local_root_id, Gfx::IntPoint content_position, i32 minimum_width, Vector<Web::HTML::SelectItem> items) override;
virtual void did_request_primary_paste() override;
virtual void did_update_primary_selection(String text) override;
diff --git a/Services/WebContent/ConnectionFromClient.cpp b/Services/WebContent/ConnectionFromClient.cpp
index c523a57b425..b63f9da0827 100644
--- a/Services/WebContent/ConnectionFromClient.cpp
+++ b/Services/WebContent/ConnectionFromClient.cpp
@@ -3576,6 +3576,33 @@ void ConnectionFromClient::request_close(Web::PageId page_id)
}
}
+void ConnectionFromClient::request_close_preflight(Web::PageId page_id, u64 request_id)
+{
+ auto page = this->page(page_id);
+ if (!page.has_value()) {
+ async_did_complete_close_preflight(page_id, request_id, false);
+ return;
+ }
+
+ auto* traversable = as_if<Web::HTML::LocalTraversableNavigable>(page->page().top_level_traversable().ptr());
+ if (!traversable) {
+ async_did_complete_close_preflight(page_id, request_id, true);
+ return;
+ }
+
+ traversable->request_close_preflight(GC::create_function(Web::Bindings::main_thread_vm().heap(), [this, page_id, request_id](bool approved) {
+ async_did_complete_close_preflight(page_id, request_id, approved);
+ }));
+}
+
+void ConnectionFromClient::request_close_without_prompting(Web::PageId page_id)
+{
+ if (auto page = this->page(page_id); page.has_value()) {
+ if (auto* traversable = as_if<Web::HTML::LocalTraversableNavigable>(page->page().top_level_traversable().ptr()))
+ traversable->close_top_level_traversable(Web::HTML::LocalTraversableNavigable::PromptToUnload::No);
+ }
+}
+
void ConnectionFromClient::force_close(Web::PageId page_id)
{
if (auto page = this->page(page_id); page.has_value()) {
diff --git a/Services/WebContent/ConnectionFromClient.h b/Services/WebContent/ConnectionFromClient.h
index 49bb1f5862e..358a8d2342e 100644
--- a/Services/WebContent/ConnectionFromClient.h
+++ b/Services/WebContent/ConnectionFromClient.h
@@ -334,6 +334,8 @@ private:
virtual void did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) override;
virtual void request_close(Web::PageId page_id) override;
+ virtual void request_close_preflight(Web::PageId page_id, u64 request_id) override;
+ virtual void request_close_without_prompting(Web::PageId page_id) override;
virtual void force_close(Web::PageId page_id) override;
virtual void exit_fullscreen(Web::PageId page_id) override;
diff --git a/Services/WebContent/WebContentClient.ipc b/Services/WebContent/WebContentClient.ipc
index f83bede3bd0..0742037c164 100644
--- a/Services/WebContent/WebContentClient.ipc
+++ b/Services/WebContent/WebContentClient.ipc
@@ -207,6 +207,7 @@ endpoint WebContentClient routed by Web::PageId page_id
did_update_resource_count(Web::PageId page_id, i32 count_waiting) =|
[NotRouted] did_request_new_web_view(Web::PageId page_id, Web::HTML::ActivateTab activate_tab, Web::HTML::WebViewHints hints, Optional<Web::HTML::CrossProcessId> opener_navigable_id, Optional<URL::URL> opener_base_url, Utf16String target_name, Web::HTML::SandboxingFlagSet popup_sandboxing_flag_set) => (Optional<Web::PageId> new_page_id, Optional<Web::HTML::CrossProcessId> root_navigable_id, Optional<Web::HTML::SessionHistoryEntryDescriptor> initial_history_entry, Optional<Web::HTML::EnvironmentId> initial_environment_id, Optional<Web::HTML::CrossProcessId> initial_document_id, Optional<u64> browsing_context_group_id, Web::HTML::VisibilityState system_visibility_state, String handle)
[NotRouted] did_close_browsing_context(Web::PageId page_id) =|
+ did_complete_close_preflight(Web::PageId page_id, u64 request_id, bool approved) =|
did_change_needs_beforeunload_check(Web::PageId page_id, bool needs_beforeunload_check) =|
did_consume_user_activation(Web::PageId page_id, Web::HTML::UserActivationConsumption consumption) =|
webdriver_user_prompt_handling_complete(Web::PageId page_id, u64 request_id, Web::WebDriver::Response response) =|
diff --git a/Services/WebContent/WebContentServer.ipc b/Services/WebContent/WebContentServer.ipc
index bd9eccc4731..9a317a3fd88 100644
--- a/Services/WebContent/WebContentServer.ipc
+++ b/Services/WebContent/WebContentServer.ipc
@@ -311,6 +311,8 @@ endpoint WebContentServer routed by Web::PageId page_id
did_worker_agent_die(Web::HTML::WorkerAgentOwnerToken owner_token) =|
request_close(Web::PageId page_id) =|
+ request_close_preflight(Web::PageId page_id, u64 request_id) =|
+ request_close_without_prompting(Web::PageId page_id) =|
unload_check_result(Web::PageId page_id, Web::HTML::CrossProcessId check_id, Web::HTML::CheckIfUnloadingIsCanceledResult result) =|
force_close(Web::PageId page_id) =|
diff --git a/Tests/LibWebView/TestBeforeUnload.cpp b/Tests/LibWebView/TestBeforeUnload.cpp
index a0bfa685057..6fa560ed62c 100644
--- a/Tests/LibWebView/TestBeforeUnload.cpp
+++ b/Tests/LibWebView/TestBeforeUnload.cpp
@@ -112,10 +112,17 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
size_t prompt_count = 0;
bool accept_prompt = false;
+ bool adopt_navigation_prompt_for_close = false;
+ Optional<bool> adopted_close_preflight_result;
Optional<String> prompt_source;
view->on_request_before_unload = [&](auto const& source, auto on_complete) {
++prompt_count;
prompt_source = source;
+ if (adopt_navigation_prompt_for_close) {
+ view->request_close_preflight([&](bool approved) {
+ adopted_close_preflight_result = approved;
+ });
+ }
on_complete(accept_prompt);
};
@@ -139,6 +146,53 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)
load_and_wait(destination_url);
VERIFY(prompt_count == 2);
+ auto request_close_preflight = [&] {
+ Optional<bool> result;
+ view->request_close_preflight([&](bool approved) {
+ result = approved;
+ });
+ Core::EventLoop::current().spin_until([&] { return result.has_value(); });
+ return *result;
+ };
+
+ // A close preflight asks without closing the page.
+ load_and_wait(source_url);
+ activate_page();
+ accept_prompt = false;
+ VERIFY(!request_close_preflight());
+ VERIFY(view_is_at(source_url));
+ VERIFY(prompt_count == 3);
+
+ accept_prompt = true;
+ VERIFY(request_close_preflight());
+ VERIFY(view_is_at(source_url));
+ VERIFY(prompt_count == 4);
+
+ // A close preflight adopts an already-open navigation prompt instead of showing another one.
+ adopt_navigation_prompt_for_close = true;
+ accept_prompt = false;
+ auto prompt_count_before_adoption = prompt_count;
+ view->load(destination_url);
+ Core::EventLoop::current().spin_until([&] { return adopted_close_preflight_result.has_value(); });
+ VERIFY(!*adopted_close_preflight_result);
+ VERIFY(view_is_at(source_url));
+ VERIFY(prompt_count == prompt_count_before_adoption + 1);
+
+ adopted_close_preflight_result.clear();
+ accept_prompt = true;
+ prompt_count_before_adoption = prompt_count;
+ view->load(destination_url);
+ Core::EventLoop::current().spin_until([&] { return adopted_close_preflight_result.has_value(); });
+ VERIFY(*adopted_close_preflight_result);
+ VERIFY(view_is_at(source_url));
+ VERIFY(prompt_count == prompt_count_before_adoption + 1);
+
+ bool closed = false;
+ view->on_close = [&] { closed = true; };
+ auto close_without_prompting = view->prepare_for_close_without_prompting();
+ close_without_prompting();
+ Core::EventLoop::current().spin_until([&] { return closed; });
+
outln("PASS: beforeunload prompt handling");
return 0;
}