1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 23:58:11 +00:00

LibWeb: Make BrowsingContex::page() return a Page&

This exposed a whole slew of now-unnecessary null checks. :^)

Co-Authored-By: Andreas Kling <kling@serenityos.org>
This commit is contained in:
Shannon Booth 2023-12-03 20:29:37 +13:00 committed by Andreas Kling
parent af2bcc3b56
commit 88f8ea7c60
9 changed files with 56 additions and 88 deletions

View file

@ -130,8 +130,7 @@ static JS::NonnullGCPtr<HTML::BrowsingContext> obtain_a_browsing_context_to_use_
} }
// 3. Let newBrowsingContext be the first return value of creating a new top-level browsing context and document // 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 // 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. // 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() 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 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) EventTarget* Document::get_parent(Event const& event)

View file

@ -115,8 +115,8 @@ public:
HTML::Window* active_window(); HTML::Window* active_window();
HTML::Window const* active_window() const; HTML::Window const* active_window() const;
Page* page() { return m_page; } Page& page() { return m_page; }
Page const* page() const { return m_page; } Page const& page() const { return m_page; }
Web::EventHandler& event_handler() { return m_event_handler; } Web::EventHandler& event_handler() { return m_event_handler; }
Web::EventHandler const& event_handler() const { return m_event_handler; } Web::EventHandler const& event_handler() const { return m_event_handler; }

View file

@ -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 // 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. // 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; 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 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) { if (element.type_state() == HTMLInputElement::TypeAttributeState::Color) {
auto weak_element = element.make_weak_ptr<HTMLInputElement>(); auto weak_element = element.make_weak_ptr<HTMLInputElement>();
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)));
} }
} }

View file

@ -194,8 +194,7 @@ WebIDL::ExceptionOr<void> History::shared_history_push_replace_state(JS::Value v
auto navigable = document->navigable(); auto navigable = document->navigable();
if (navigable->is_top_level_traversable()) { if (navigable->is_top_level_traversable()) {
if (auto* page = navigable->active_browsing_context()->page()) navigable->active_browsing_context()->page().client().page_did_start_loading(new_url, false);
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 // 10. Run the URL and history update steps given document and newURL, with serializedData set to

View file

@ -193,8 +193,7 @@ void Navigable::activate_history_entry(JS::GCPtr<SessionHistoryEntry> entry)
// Not in the spec: // Not in the spec:
VERIFY(active_browsing_context()); 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 // https://html.spec.whatwg.org/multipage/document-sequences.html#nav-document
@ -1331,8 +1330,7 @@ WebIDL::ExceptionOr<void> Navigable::navigate(NavigateParams params)
} }
if (is_top_level_traversable()) { if (is_top_level_traversable()) {
if (auto* page = active_browsing_context()->page()) active_browsing_context()->page().client().page_did_start_loading(url, false);
page->client().page_did_start_loading(url, false);
} }
// 20. In parallel, run these steps: // 20. In parallel, run these steps:

View file

@ -34,42 +34,36 @@ void Inspector::initialize(JS::Realm& realm)
void Inspector::inspector_loaded() void Inspector::inspector_loaded()
{ {
if (auto* page = global_object().browsing_context()->page()) global_object().browsing_context()->page().client().inspector_did_load();
page->client().inspector_did_load();
} }
void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element) void Inspector::inspect_dom_node(i32 node_id, Optional<i32> const& pseudo_element)
{ {
if (auto* page = global_object().browsing_context()->page()) { auto& page = global_object().browsing_context()->page();
page->client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) { page.client().inspector_did_select_dom_node(node_id, pseudo_element.map([](auto value) {
VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::PseudoElementCount)); VERIFY(value < to_underlying(Web::CSS::Selector::PseudoElement::PseudoElementCount));
return static_cast<Web::CSS::Selector::PseudoElement>(value); return static_cast<Web::CSS::Selector::PseudoElement>(value);
})); }));
}
} }
void Inspector::set_dom_node_text(i32 node_id, String const& text) void Inspector::set_dom_node_text(i32 node_id, String const& text)
{ {
if (auto* page = global_object().browsing_context()->page()) global_object().browsing_context()->page().client().inspector_did_set_dom_node_text(node_id, text);
page->client().inspector_did_set_dom_node_text(node_id, text);
} }
void Inspector::set_dom_node_tag(i32 node_id, String const& tag) void Inspector::set_dom_node_tag(i32 node_id, String const& tag)
{ {
if (auto* page = global_object().browsing_context()->page()) global_object().browsing_context()->page().client().inspector_did_set_dom_node_tag(node_id, tag);
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<DOM::NamedNodeMap> replacement_attributes) void Inspector::replace_dom_node_attribute(i32 node_id, String const& name, JS::NonnullGCPtr<DOM::NamedNodeMap> replacement_attributes)
{ {
if (auto* page = global_object().browsing_context()->page()) global_object().browsing_context()->page().client().inspector_did_replace_dom_node_attribute(node_id, name, replacement_attributes);
page->client().inspector_did_replace_dom_node_attribute(node_id, name, replacement_attributes);
} }
void Inspector::execute_console_script(String const& script) void Inspector::execute_console_script(String const& script)
{ {
if (auto* page = global_object().browsing_context()->page()) global_object().browsing_context()->page().client().inspector_did_execute_console_script(script);
page->client().inspector_did_execute_console_script(script);
} }
} }

View file

@ -36,9 +36,7 @@ void Internals::initialize(JS::Realm& realm)
void Internals::signal_text_test_is_done() void Internals::signal_text_test_is_done()
{ {
if (auto* page = global_object().browsing_context()->page()) { global_object().browsing_context()->page().client().page_did_finish_text_test();
page->client().page_did_finish_text_test();
}
} }
void Internals::gc() 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) void Internals::send_text(HTML::HTMLElement& target, String const& text)
{ {
auto* page = global_object().browsing_context()->page(); auto& page = global_object().browsing_context()->page();
if (!page)
return;
target.focus(); target.focus();
for (auto code_point : text.code_points()) 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() void Internals::commit_text()
{ {
auto* page = global_object().browsing_context()->page(); global_object().browsing_context()->page().handle_keydown(Key_Return, 0, 0);
if (!page)
return;
page->handle_keydown(Key_Return, 0, 0);
} }
void Internals::click(double x, double y) void Internals::click(double x, double y)
{ {
auto* page = global_object().browsing_context()->page(); auto& page = global_object().browsing_context()->page();
if (!page) page.handle_mousedown({ x, y }, { x, y }, 1, 0, 0);
return; page.handle_mouseup({ x, y }, { x, y }, 1, 0, 0);
page->handle_mousedown({ x, y }, { x, y }, 1, 0, 0);
page->handle_mouseup({ x, y }, { x, y }, 1, 0, 0);
} }
WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event) WebIDL::ExceptionOr<bool> Internals::dispatch_user_activated_event(DOM::EventTarget& target, DOM::Event& event)

View file

@ -195,10 +195,9 @@ bool EventHandler::handle_mousewheel(CSSPixelPoint position, CSSPixelPoint scree
auto client_offset = compute_mouse_event_client_offset(position); auto client_offset = compute_mouse_event_client_offset(position);
auto page_offset = compute_mouse_event_page_offset(client_offset); 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 (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()) { auto& page = m_browsing_context->page();
if (m_browsing_context == &page->top_level_browsing_context()) if (m_browsing_context == &page.top_level_browsing_context())
page->client().page_did_request_scroll(wheel_delta_x, wheel_delta_y); page.client().page_did_request_scroll(wheel_delta_x, wheel_delta_y);
}
} }
handled_event = true; handled_event = true;
@ -286,18 +285,15 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, CSSPixelPoint screen_p
auto url = document->parse_url(href); auto url = document->parse_url(href);
dbgln("Web::EventHandler: Clicking on a link to {}", url); dbgln("Web::EventHandler: Clicking on a link to {}", url);
if (button == GUI::MouseButton::Middle) { if (button == GUI::MouseButton::Middle) {
if (auto* page = m_browsing_context->page()) m_browsing_context->page().client().page_did_middle_click_link(url, link->target(), modifiers);
page->client().page_did_middle_click_link(url, link->target(), modifiers);
} else if (button == GUI::MouseButton::Secondary) { } else if (button == GUI::MouseButton::Secondary) {
if (auto* page = m_browsing_context->page()) m_browsing_context->page().client().page_did_request_link_context_menu(m_browsing_context->to_top_level_position(position), url, link->target(), modifiers);
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) { } else if (button == GUI::MouseButton::Secondary) {
if (is<HTML::HTMLImageElement>(*node)) { if (is<HTML::HTMLImageElement>(*node)) {
auto& image_element = verify_cast<HTML::HTMLImageElement>(*node); auto& image_element = verify_cast<HTML::HTMLImageElement>(*node);
auto image_url = image_element.document().parse_url(image_element.src()); auto image_url = image_element.document().parse_url(image_element.src());
if (auto* page = m_browsing_context->page()) 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());
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<HTML::HTMLMediaElement>(*node)) { } else if (is<HTML::HTMLMediaElement>(*node)) {
auto& media_element = verify_cast<HTML::HTMLMediaElement>(*node); auto& media_element = verify_cast<HTML::HTMLMediaElement>(*node);
@ -310,10 +306,9 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, CSSPixelPoint screen_p
.is_looping = media_element.has_attribute(HTML::AttributeNames::loop), .is_looping = media_element.has_attribute(HTML::AttributeNames::loop),
}; };
if (auto* page = m_browsing_context->page()) m_browsing_context->page().did_request_media_context_menu(media_element.unique_id(), m_browsing_context->to_top_level_position(position), "", modifiers, move(menu));
page->did_request_media_context_menu(media_element.unique_id(), m_browsing_context->to_top_level_position(position), "", modifiers, move(menu)); } else {
} else if (auto* page = m_browsing_context->page()) { m_browsing_context->page().client().page_did_request_context_menu(m_browsing_context->to_top_level_position(position));
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; return false;
} }
if (auto* page = m_browsing_context->page()) m_browsing_context->page().set_focused_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. // 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) // "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; return false;
// FIXME: It feels a bit aggressive to always update the cursor like this. // FIXME: It feels a bit aggressive to always update the cursor like this.
if (auto* page = m_browsing_context->page()) m_browsing_context->page().client().page_did_request_cursor_change(Gfx::StandardCursor::None);
page->client().page_did_request_cursor_change(Gfx::StandardCursor::None);
} }
auto node = dom_node_for_event_dispatch(*paintable); 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(); document.navigable()->set_needs_display();
} }
if (auto* page = m_browsing_context->page()) m_browsing_context->page().client().page_did_change_selection();
page->client().page_did_change_selection();
} }
} }
if (auto* page = m_browsing_context->page()) { auto& page = m_browsing_context->page();
page->client().page_did_request_cursor_change(hovered_node_cursor);
if (hovered_node_changed) { page.client().page_did_request_cursor_change(hovered_node_cursor);
JS::GCPtr<HTML::HTMLElement const> 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()) { if (hovered_node_changed) {
page->client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title()->to_deprecated_string()); JS::GCPtr<HTML::HTMLElement const> hovered_html_element = document.hovered_node() ? document.hovered_node()->enclosing_html_element_with_attribute(HTML::AttributeNames::title) : nullptr;
} else { if (hovered_html_element && hovered_html_element->title().has_value()) {
page->client().page_did_leave_tooltip_area(); page.client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title()->to_deprecated_string());
} } else {
if (is_hovering_link) page.client().page_did_leave_tooltip_area();
page->client().page_did_hover_link(document.parse_url(hovered_link_element->href()));
else
page->client().page_did_unhover_link();
} }
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; return true;
} }

View file

@ -302,8 +302,7 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mouseup(Badge<Eve
break; break;
case HTML::HTMLMediaElement::MouseTrackingComponent::Volume: case HTML::HTMLMediaElement::MouseTrackingComponent::Volume:
if (auto* page = browsing_context().page()) browsing_context().page().client().page_did_leave_tooltip_area();
page->client().page_did_leave_tooltip_area();
break; break;
} }
@ -351,10 +350,8 @@ MediaPaintable::DispatchEventOfSameName MediaPaintable::handle_mousemove(Badge<E
if (cached_layout_boxes.volume_rect.has_value()) { if (cached_layout_boxes.volume_rect.has_value()) {
set_volume(media_element, *cached_layout_boxes.volume_scrub_rect, position); set_volume(media_element, *cached_layout_boxes.volume_scrub_rect, position);
if (auto* page = browsing_context().page()) { auto volume = static_cast<u8>(media_element.volume() * 100.0);
auto volume = static_cast<u8>(media_element.volume() * 100.0); browsing_context().page().client().page_did_enter_tooltip_area(position, DeprecatedString::formatted("{}%", volume));
page->client().page_did_enter_tooltip_area(position, DeprecatedString::formatted("{}%", volume));
}
} }
break; break;