diff --git a/Userland/Libraries/LibWeb/DOM/Document.cpp b/Userland/Libraries/LibWeb/DOM/Document.cpp index 7f963926f4..5bad8a87ce 100644 --- a/Userland/Libraries/LibWeb/DOM/Document.cpp +++ b/Userland/Libraries/LibWeb/DOM/Document.cpp @@ -130,8 +130,7 @@ static JS::NonnullGCPtr obtain_a_browsing_context_to_use_ } // 3. Let newBrowsingContext be the first return value of creating a new top-level browsing context and document - VERIFY(browsing_context.page()); - auto new_browsing_context = HTML::create_a_new_top_level_browsing_context_and_document(*browsing_context.page()).release_value_but_fixme_should_propagate_errors().browsing_context; + auto new_browsing_context = HTML::create_a_new_top_level_browsing_context_and_document(browsing_context.page()).release_value_but_fixme_should_propagate_errors().browsing_context; // FIXME: 4. If navigationCOOP's value is "same-origin-plurs-COEP", then set newBrowsingContext's group's // cross-origin isolation mode to either "logical" or "concrete". The choice of which is implementation-defined. @@ -1910,12 +1909,12 @@ void Document::update_readiness(HTML::DocumentReadyState readiness_value) Page* Document::page() { - return m_browsing_context ? m_browsing_context->page() : nullptr; + return m_browsing_context ? &m_browsing_context->page() : nullptr; } Page const* Document::page() const { - return m_browsing_context ? m_browsing_context->page() : nullptr; + return m_browsing_context ? &m_browsing_context->page() : nullptr; } EventTarget* Document::get_parent(Event const& event) diff --git a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h index 3d76021621..6017aa6e18 100644 --- a/Userland/Libraries/LibWeb/HTML/BrowsingContext.h +++ b/Userland/Libraries/LibWeb/HTML/BrowsingContext.h @@ -115,8 +115,8 @@ public: HTML::Window* active_window(); HTML::Window const* active_window() const; - Page* page() { return m_page; } - Page const* page() const { return m_page; } + Page& page() { return m_page; } + Page const& page() const { return m_page; } Web::EventHandler& event_handler() { return m_event_handler; } Web::EventHandler const& event_handler() const { return m_event_handler; } diff --git a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp index 20030374a8..ececdff88e 100644 --- a/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp +++ b/Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp @@ -199,7 +199,7 @@ static void show_the_picker_if_applicable(HTMLInputElement& element) // FIXME: Pass along accept attribute information https://html.spec.whatwg.org/multipage/input.html#attr-input-accept // The accept attribute may be specified to provide user agents with a hint of what file types will be accepted. - element.document().browsing_context()->top_level_browsing_context()->page()->client().page_did_request_file_picker(weak_element, multiple); + element.document().browsing_context()->top_level_browsing_context()->page().client().page_did_request_file_picker(weak_element, multiple); return; } @@ -211,7 +211,7 @@ static void show_the_picker_if_applicable(HTMLInputElement& element) // (If this closes a file selection picker, then per the above that will lead to firing either input and change events, or a cancel event.) if (element.type_state() == HTMLInputElement::TypeAttributeState::Color) { auto weak_element = element.make_weak_ptr(); - element.document().browsing_context()->top_level_browsing_context()->page()->did_request_color_picker(weak_element, Color::from_string(element.value()).value_or(Color(0, 0, 0))); + element.document().browsing_context()->top_level_browsing_context()->page().did_request_color_picker(weak_element, Color::from_string(element.value()).value_or(Color(0, 0, 0))); } } diff --git a/Userland/Libraries/LibWeb/HTML/History.cpp b/Userland/Libraries/LibWeb/HTML/History.cpp index 274260c9cf..5ada1f616b 100644 --- a/Userland/Libraries/LibWeb/HTML/History.cpp +++ b/Userland/Libraries/LibWeb/HTML/History.cpp @@ -194,8 +194,7 @@ WebIDL::ExceptionOr History::shared_history_push_replace_state(JS::Value v auto navigable = document->navigable(); if (navigable->is_top_level_traversable()) { - if (auto* page = navigable->active_browsing_context()->page()) - page->client().page_did_start_loading(new_url, false); + navigable->active_browsing_context()->page().client().page_did_start_loading(new_url, false); } // 10. Run the URL and history update steps given document and newURL, with serializedData set to diff --git a/Userland/Libraries/LibWeb/HTML/Navigable.cpp b/Userland/Libraries/LibWeb/HTML/Navigable.cpp index f983d35aa8..4e5939e04c 100644 --- a/Userland/Libraries/LibWeb/HTML/Navigable.cpp +++ b/Userland/Libraries/LibWeb/HTML/Navigable.cpp @@ -193,8 +193,7 @@ void Navigable::activate_history_entry(JS::GCPtr entry) // Not in the spec: VERIFY(active_browsing_context()); - VERIFY(active_browsing_context()->page()); - active_browsing_context()->page()->client().page_did_create_new_document(*new_document); + active_browsing_context()->page().client().page_did_create_new_document(*new_document); } // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document @@ -1331,8 +1330,7 @@ WebIDL::ExceptionOr Navigable::navigate(NavigateParams params) } if (is_top_level_traversable()) { - if (auto* page = active_browsing_context()->page()) - page->client().page_did_start_loading(url, false); + active_browsing_context()->page().client().page_did_start_loading(url, false); } // 20. In parallel, run these steps: diff --git a/Userland/Libraries/LibWeb/Internals/Inspector.cpp b/Userland/Libraries/LibWeb/Internals/Inspector.cpp index d59acc1a08..5abc4ed2fe 100644 --- a/Userland/Libraries/LibWeb/Internals/Inspector.cpp +++ b/Userland/Libraries/LibWeb/Internals/Inspector.cpp @@ -34,42 +34,36 @@ void Inspector::initialize(JS::Realm& realm) void Inspector::inspector_loaded() { - if (auto* page = global_object().browsing_context()->page()) - page->client().inspector_did_load(); + global_object().browsing_context()->page().client().inspector_did_load(); } void Inspector::inspect_dom_node(i32 node_id, Optional const& pseudo_element) { - if (auto* page = global_object().browsing_context()->page()) { - page->client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) { - VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::PseudoElementCount)); - return static_cast(value); - })); - } + auto& page = global_object().browsing_context()->page(); + page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) { + VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::PseudoElementCount)); + return static_cast(value); + })); } void Inspector::set_dom_node_text(i32 node_id, String const& text) { - if (auto* page = global_object().browsing_context()->page()) - page->client().inspector_did_set_dom_node_text(node_id, text); + global_object().browsing_context()->page().client().inspector_did_set_dom_node_text(node_id, text); } void Inspector::set_dom_node_tag(i32 node_id, String const& tag) { - if (auto* page = global_object().browsing_context()->page()) - page->client().inspector_did_set_dom_node_tag(node_id, tag); + global_object().browsing_context()->page().client().inspector_did_set_dom_node_tag(node_id, tag); } void Inspector::replace_dom_node_attribute(i32 node_id, String const& name, JS::NonnullGCPtr replacement_attributes) { - if (auto* page = global_object().browsing_context()->page()) - page->client().inspector_did_replace_dom_node_attribute(node_id, name, replacement_attributes); + global_object().browsing_context()->page().client().inspector_did_replace_dom_node_attribute(node_id, name, replacement_attributes); } void Inspector::execute_console_script(String const& script) { - if (auto* page = global_object().browsing_context()->page()) - page->client().inspector_did_execute_console_script(script); + global_object().browsing_context()->page().client().inspector_did_execute_console_script(script); } } diff --git a/Userland/Libraries/LibWeb/Internals/Internals.cpp b/Userland/Libraries/LibWeb/Internals/Internals.cpp index 15a10d4044..03d269fd11 100644 --- a/Userland/Libraries/LibWeb/Internals/Internals.cpp +++ b/Userland/Libraries/LibWeb/Internals/Internals.cpp @@ -36,9 +36,7 @@ void Internals::initialize(JS::Realm& realm) void Internals::signal_text_test_is_done() { - if (auto* page = global_object().browsing_context()->page()) { - page->client().page_did_finish_text_test(); - } + global_object().browsing_context()->page().client().page_did_finish_text_test(); } void Internals::gc() @@ -65,33 +63,23 @@ JS::Object* Internals::hit_test(double x, double y) void Internals::send_text(HTML::HTMLElement& target, String const& text) { - auto* page = global_object().browsing_context()->page(); - if (!page) - return; - + auto& page = global_object().browsing_context()->page(); target.focus(); for (auto code_point : text.code_points()) - page->handle_keydown(code_point_to_key_code(code_point), 0, code_point); + page.handle_keydown(code_point_to_key_code(code_point), 0, code_point); } void Internals::commit_text() { - auto* page = global_object().browsing_context()->page(); - if (!page) - return; - - page->handle_keydown(Key_Return, 0, 0); + global_object().browsing_context()->page().handle_keydown(Key_Return, 0, 0); } void Internals::click(double x, double y) { - auto* page = global_object().browsing_context()->page(); - if (!page) - return; - - page->handle_mousedown({ x, y }, { x, y }, 1, 0, 0); - page->handle_mouseup({ x, y }, { x, y }, 1, 0, 0); + auto& page = global_object().browsing_context()->page(); + page.handle_mousedown({ x, y }, { x, y }, 1, 0, 0); + page.handle_mouseup({ x, y }, { x, y }, 1, 0, 0); } WebIDL::ExceptionOr Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event) diff --git a/Userland/Libraries/LibWeb/Page/EventHandler.cpp b/Userland/Libraries/LibWeb/Page/EventHandler.cpp index 591e67af86..385f9fdd75 100644 --- a/Userland/Libraries/LibWeb/Page/EventHandler.cpp +++ b/Userland/Libraries/LibWeb/Page/EventHandler.cpp @@ -195,10 +195,9 @@ bool EventHandler::handle_mousewheel(CSSPixelPoint position, CSSPixelPoint scree auto client_offset = compute_mouse_event_client_offset(position); auto page_offset = compute_mouse_event_page_offset(client_offset); if (node->dispatch_event(UIEvents::WheelEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::wheel, screen_position, page_offset, client_offset, offset, wheel_delta_x, wheel_delta_y, button, buttons, modifiers).release_value_but_fixme_should_propagate_errors())) { - if (auto* page = m_browsing_context->page()) { - if (m_browsing_context == &page->top_level_browsing_context()) - page->client().page_did_request_scroll(wheel_delta_x, wheel_delta_y); - } + auto& page = m_browsing_context->page(); + if (m_browsing_context == &page.top_level_browsing_context()) + page.client().page_did_request_scroll(wheel_delta_x, wheel_delta_y); } handled_event = true; @@ -286,18 +285,15 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, CSSPixelPoint screen_p auto url = document->parse_url(href); dbgln("Web::EventHandler: Clicking on a link to {}", url); if (button == GUI::MouseButton::Middle) { - if (auto* page = m_browsing_context->page()) - page->client().page_did_middle_click_link(url, link->target(), modifiers); + m_browsing_context->page().client().page_did_middle_click_link(url, link->target(), modifiers); } else if (button == GUI::MouseButton::Secondary) { - if (auto* page = m_browsing_context->page()) - page->client().page_did_request_link_context_menu(m_browsing_context->to_top_level_position(position), url, link->target(), modifiers); + m_browsing_context->page().client().page_did_request_link_context_menu(m_browsing_context->to_top_level_position(position), url, link->target(), modifiers); } } else if (button == GUI::MouseButton::Secondary) { if (is(*node)) { auto& image_element = verify_cast(*node); auto image_url = image_element.document().parse_url(image_element.src()); - if (auto* page = m_browsing_context->page()) - page->client().page_did_request_image_context_menu(m_browsing_context->to_top_level_position(position), image_url, "", modifiers, image_element.bitmap()); + m_browsing_context->page().client().page_did_request_image_context_menu(m_browsing_context->to_top_level_position(position), image_url, "", modifiers, image_element.bitmap()); } else if (is(*node)) { auto& media_element = verify_cast(*node); @@ -310,10 +306,9 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, CSSPixelPoint screen_p .is_looping = media_element.has_attribute(HTML::AttributeNames::loop), }; - if (auto* page = m_browsing_context->page()) - page->did_request_media_context_menu(media_element.unique_id(), m_browsing_context->to_top_level_position(position), "", modifiers, move(menu)); - } else if (auto* page = m_browsing_context->page()) { - page->client().page_did_request_context_menu(m_browsing_context->to_top_level_position(position)); + m_browsing_context->page().did_request_media_context_menu(media_element.unique_id(), m_browsing_context->to_top_level_position(position), "", modifiers, move(menu)); + } else { + m_browsing_context->page().client().page_did_request_context_menu(m_browsing_context->to_top_level_position(position)); } } } @@ -369,8 +364,7 @@ bool EventHandler::handle_mousedown(CSSPixelPoint position, CSSPixelPoint screen return false; } - if (auto* page = m_browsing_context->page()) - page->set_focused_browsing_context({}, m_browsing_context); + m_browsing_context->page().set_focused_browsing_context({}, m_browsing_context); // Search for the first parent of the hit target that's an element. // "The click event type MUST be dispatched on the topmost event target indicated by the pointer." (https://www.w3.org/TR/uievents/#event-type-click) @@ -460,8 +454,7 @@ bool EventHandler::handle_mousemove(CSSPixelPoint position, CSSPixelPoint screen return false; // FIXME: It feels a bit aggressive to always update the cursor like this. - if (auto* page = m_browsing_context->page()) - page->client().page_did_request_cursor_change(Gfx::StandardCursor::None); + m_browsing_context->page().client().page_did_request_cursor_change(Gfx::StandardCursor::None); } auto node = dom_node_for_event_dispatch(*paintable); @@ -530,27 +523,27 @@ bool EventHandler::handle_mousemove(CSSPixelPoint position, CSSPixelPoint screen } document.navigable()->set_needs_display(); } - if (auto* page = m_browsing_context->page()) - page->client().page_did_change_selection(); + m_browsing_context->page().client().page_did_change_selection(); } } - if (auto* page = m_browsing_context->page()) { - page->client().page_did_request_cursor_change(hovered_node_cursor); + auto& page = m_browsing_context->page(); - if (hovered_node_changed) { - JS::GCPtr hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr; - if (hovered_html_element && hovered_html_element->title().has_value()) { - page->client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title()->to_deprecated_string()); - } else { - page->client().page_did_leave_tooltip_area(); - } - if (is_hovering_link) - page->client().page_did_hover_link(document.parse_url(hovered_link_element->href())); - else - page->client().page_did_unhover_link(); + page.client().page_did_request_cursor_change(hovered_node_cursor); + + if (hovered_node_changed) { + JS::GCPtr hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr; + if (hovered_html_element && hovered_html_element->title().has_value()) { + page.client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title()->to_deprecated_string()); + } else { + page.client().page_did_leave_tooltip_area(); } + if (is_hovering_link) + page.client().page_did_hover_link(document.parse_url(hovered_link_element->href())); + else + page.client().page_did_unhover_link(); } + return true; } diff --git a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp index 7174aa4abb..d7836e0306 100644 --- a/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp +++ b/Userland/Libraries/LibWeb/Painting/MediaPaintable.cpp @@ -302,8 +302,7 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badgeclient().page_did_leave_tooltip_area(); + browsing_context().page().client().page_did_leave_tooltip_area(); break; } @@ -351,10 +350,8 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mousemove(Badge(media_element.volume() * 100.0); - page->client().page_did_enter_tooltip_area(position, DeprecatedString::formatted("{}%", volume)); - } + auto volume = static_cast(media_element.volume() * 100.0); + browsing_context().page().client().page_did_enter_tooltip_area(position, DeprecatedString::formatted("{}%", volume)); } break;