Skip to content

Truly independent web browser

LibWeb: Keep a running unloading-canceled check alive from its page

Shannon Booth66367f5ac024
diff --git a/Libraries/LibWeb/Forward.h b/Libraries/LibWeb/Forward.h
index 88d968cf2d2..774a9f8cc13 100644
--- a/Libraries/LibWeb/Forward.h
+++ b/Libraries/LibWeb/Forward.h
@@ -681,6 +681,7 @@ class BrowsingContext;
class Canvas2DContextBase;
class CanvasHost;
class CanvasRenderingContext2D;
+class CheckUnloadingCanceledState;
class RemoteCanvas2DTransport;
class ClassicScript;
class CloseEvent;
diff --git a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
index bc42e41b75a..5ad9bcd8f33 100644
--- a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
+++ b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.cpp
@@ -20,12 +20,14 @@ namespace Web::HTML {
GC_DEFINE_ALLOCATOR(CheckUnloadingCanceledState);
CheckUnloadingCanceledState::CheckUnloadingCanceledState(
+ GC::Ref<Page> page,
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_page(page)
, m_owner(owner)
, m_traversable(traversable)
, m_user_involvement(user_involvement)
@@ -41,12 +43,14 @@ CheckUnloadingCanceledState::CheckUnloadingCanceledState(
}
})))
{
+ m_page->add_unloading_canceled_check({}, *this);
m_timeout->start();
}
void CheckUnloadingCanceledState::visit_edges(Visitor& visitor)
{
Base::visit_edges(visitor);
+ visitor.visit(m_page);
visitor.visit(m_phase2_documents);
visitor.visit(m_documents_already_fired);
visitor.visit(m_traversable);
@@ -298,6 +302,7 @@ void CheckUnloadingCanceledState::finish(Result final_result)
return;
m_completed = true;
m_timeout->stop();
+ m_page->remove_unloading_canceled_check({}, *this);
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);
diff --git a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
index ae0df26e5be..f2f072dcde0 100644
--- a/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
+++ b/Libraries/LibWeb/HTML/CheckUnloadingCanceledState.h
@@ -27,6 +27,7 @@ public:
static constexpr int TIMEOUT_MS = 15000;
CheckUnloadingCanceledState(
+ GC::Ref<Page>,
GC::Ptr<LocalTraversableNavigable> owner,
GC::Ptr<LocalTraversableNavigable> traversable,
Optional<UserNavigationInvolvement> user_involvement,
@@ -52,6 +53,7 @@ private:
Result m_final_status { Result::Continue };
UnloadPromptShown m_unload_prompt_shown { UnloadPromptShown::No };
bool m_completed { false };
+ GC::Ref<Page> m_page;
GC::Ptr<LocalTraversableNavigable> m_owner;
bool m_needs_beforeunload { false };
size_t m_remaining_phase2_tasks { 0 };
diff --git a/Libraries/LibWeb/HTML/HistoryExecutor.cpp b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
index 45364e8babc..8f038f672be 100644
--- a/Libraries/LibWeb/HTML/HistoryExecutor.cpp
+++ b/Libraries/LibWeb/HTML/HistoryExecutor.cpp
@@ -231,7 +231,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, traversable, move(target_entry), user_involvement_for_navigate_event, unload_prompt_shown, on_complete);
+ check_if_unloading_is_canceled(*m_page, 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, DOM::Document const& displayed_document)
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.cpp b/Libraries/LibWeb/HTML/LocalNavigable.cpp
index 09e9b385425..dedc92d0639 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.cpp
+++ b/Libraries/LibWeb/HTML/LocalNavigable.cpp
@@ -3836,7 +3836,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(), as_if<LocalTraversableNavigable>(this), {}, {}, {}, unload_prompt_shown,
+ check_if_unloading_is_canceled(page(), 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);
@@ -4367,6 +4367,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(
+ Page& page,
Vector<GC::Root<LocalNavigable>> navigables_that_need_before_unload,
GC::Ptr<LocalTraversableNavigable> owner,
GC::Ptr<LocalTraversableNavigable> traversable,
@@ -4376,6 +4377,7 @@ void check_if_unloading_is_canceled(
GC::Ref<GC::Function<void(CheckIfUnloadingIsCanceledResult, UnloadPromptShown)>> callback)
{
auto state = GC::Heap::the().allocate<CheckUnloadingCanceledState>(
+ page,
owner,
traversable,
user_involvement_for_navigate_events,
@@ -4404,7 +4406,7 @@ void LocalTraversableNavigable::request_close_preflight(GC::Ref<GC::Function<voi
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);
+ auto state = heap().allocate<CheckUnloadingCanceledState>(page(), 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()))
diff --git a/Libraries/LibWeb/HTML/LocalNavigable.h b/Libraries/LibWeb/HTML/LocalNavigable.h
index 310104ca147..25b5258ec99 100644
--- a/Libraries/LibWeb/HTML/LocalNavigable.h
+++ b/Libraries/LibWeb/HTML/LocalNavigable.h
@@ -815,7 +815,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> 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);
+WEB_API void check_if_unloading_is_canceled(Page&, 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/Page/Page.cpp b/Libraries/LibWeb/Page/Page.cpp
index 03610ab9cc2..701a42bc248 100644
--- a/Libraries/LibWeb/Page/Page.cpp
+++ b/Libraries/LibWeb/Page/Page.cpp
@@ -23,6 +23,7 @@
#include <LibWeb/Editing/SpellingMarkers.h>
#include <LibWeb/Editing/WritingSuggestionController.h>
#include <LibWeb/HTML/BrowsingContext.h>
+#include <LibWeb/HTML/CheckUnloadingCanceledState.h>
#include <LibWeb/HTML/EventLoop/EventLoop.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/Focus.h>
@@ -163,6 +164,7 @@ void Page::visit_edges(JS::Cell::Visitor& visitor)
visitor.visit(request.value.callback);
visitor.visit(m_pending_permission_requests);
visitor.visit(m_notifications_list);
+ visitor.visit(m_unloading_canceled_checks);
m_pending_fullscreen_operations.for_each([&](auto const& operation) {
operation.visit([&](PendingFullscreenEnter const& enter_operation) {
visitor.visit(enter_operation.element);
@@ -1637,6 +1639,16 @@ void Page::permission_request_completed(u64 request_id, Web::Permissions::Effect
callback.value()->function()(state);
}
+void Page::add_unloading_canceled_check(Badge<HTML::CheckUnloadingCanceledState>, GC::Ref<HTML::CheckUnloadingCanceledState> check)
+{
+ m_unloading_canceled_checks.set(check);
+}
+
+void Page::remove_unloading_canceled_check(Badge<HTML::CheckUnloadingCanceledState>, GC::Ref<HTML::CheckUnloadingCanceledState> check)
+{
+ m_unloading_canceled_checks.remove(check);
+}
+
void Page::register_showing_notification(u64 notification_id, GC::Ref<NotificationsAPI::Notification> notification)
{
m_notifications_list.set(notification_id, notification);
diff --git a/Libraries/LibWeb/Page/Page.h b/Libraries/LibWeb/Page/Page.h
index dda565ce251..06085530bbb 100644
--- a/Libraries/LibWeb/Page/Page.h
+++ b/Libraries/LibWeb/Page/Page.h
@@ -305,6 +305,9 @@ public:
void dismiss_dialog(GC::Ref<GC::Function<void()>> on_dialog_closed);
void accept_dialog(GC::Ref<GC::Function<void()>> on_dialog_closed);
+ void add_unloading_canceled_check(Badge<HTML::CheckUnloadingCanceledState>, GC::Ref<HTML::CheckUnloadingCanceledState>);
+ void remove_unloading_canceled_check(Badge<HTML::CheckUnloadingCanceledState>, GC::Ref<HTML::CheckUnloadingCanceledState>);
+
void did_request_color_picker(GC::Weak<HTML::HTMLInputElement> target, Color current_color);
void color_picker_update(Optional<Color> picked_color, HTML::ColorPickerUpdateState state);
@@ -579,6 +582,10 @@ private:
Optional<Optional<Utf16String>> m_pending_prompt_response;
GC::Ptr<GC::Function<void()>> m_on_pending_dialog_closed;
+ // The checks if unloading is canceled that are still running. They wait on tasks and dialogs that may never come
+ // back, so this page keeps them alive until they finish, if need be by timing out.
+ HashTable<GC::Ref<HTML::CheckUnloadingCanceledState>> m_unloading_canceled_checks;
+
PendingNonBlockingDialog m_pending_non_blocking_dialog { PendingNonBlockingDialog::None };
GC::Weak<HTML::HTMLElement> m_pending_non_blocking_dialog_target;
Utf16String m_pending_date_picker_input_type;