Skip to content

Commit

Permalink
LibWeb: Make BrowsingContex::page() return a Page&
Browse files Browse the repository at this point in the history
This exposed a whole slew of now-unnecessary null checks. :^)
  • Loading branch information
awesomekling committed Jun 26, 2023
1 parent f87c59e commit 6fcea38
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 67 deletions.
7 changes: 3 additions & 4 deletions Userland/Libraries/LibWeb/DOM/Document.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ static JS::NonnullGCPtr<HTML::BrowsingContext> obtain_a_browsing_context_to_use_
}

// 3. Let newBrowsingContext be the result of creating a new top-level browsing context.
VERIFY(browsing_context.page());
auto new_browsing_context = HTML::BrowsingContext::create_a_new_top_level_browsing_context(*browsing_context.page());
auto new_browsing_context = HTML::BrowsingContext::create_a_new_top_level_browsing_context(browsing_context.page());

// 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.
Expand Down Expand Up @@ -1705,12 +1704,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)
Expand Down
8 changes: 3 additions & 5 deletions Userland/Libraries/LibWeb/DOM/Element.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,9 +1371,7 @@ static ErrorOr<void> scroll_an_element_into_view(DOM::Element& element, Bindings
if (!element.document().browsing_context())
return Error::from_string_view("Element has no browsing context."sv);

auto* page = element.document().browsing_context()->page();
if (!page)
return Error::from_string_view("Element has no page."sv);
auto& page = element.document().browsing_context()->page();

// If this element doesn't have a layout node, we can't scroll it into view.
element.document().update_layout();
Expand All @@ -1388,8 +1386,8 @@ static ErrorOr<void> scroll_an_element_into_view(DOM::Element& element, Bindings
if (!layout_node)
return Error::from_string_view("Element has no parent layout node that is a box."sv);

if (element.document().browsing_context() == &page->top_level_browsing_context())
page->client().page_did_request_scroll_into_view(verify_cast<Layout::Box>(*layout_node).paintable_box()->absolute_padding_box_rect());
if (element.document().browsing_context() == &page.top_level_browsing_context())
page.client().page_did_request_scroll_into_view(verify_cast<Layout::Box>(*layout_node).paintable_box()->absolute_padding_box_rect());

return {};
}
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/BrowsingContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1222,7 +1222,7 @@ WebIDL::ExceptionOr<void> BrowsingContext::navigate(
(void)process_response_end_of_body;

// AD-HOC:
auto request = LoadRequest::create_for_url_on_page(resource->url(), page());
auto request = LoadRequest::create_for_url_on_page(resource->url(), &page());
request.set_method(DeprecatedString { resource->method() });
for (auto& header : *resource->header_list()) {
request.set_header(DeprecatedString { header.name.bytes() }, DeprecatedString { header.value.bytes() });
Expand Down
4 changes: 2 additions & 2 deletions Userland/Libraries/LibWeb/HTML/BrowsingContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ class BrowsingContext final
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; }

CSSPixelSize size() const { return m_size; }
void set_size(CSSPixelSize);
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/HTMLInputElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -201,7 +201,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;
}

Expand Down
4 changes: 1 addition & 3 deletions Userland/Libraries/LibWeb/HTML/Navigable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,9 +137,7 @@ void Navigable::activate_history_entry(JS::GCPtr<SessionHistoryEntry> entry)

// Not in the spec:
if (is<TraversableNavigable>(*this) && parent() == nullptr) {
if (auto* page = active_browsing_context()->page()) {
page->client().page_did_start_loading(entry->url, false);
}
active_browsing_context()->page().client().page_did_start_loading(entry->url, false);
}
}

Expand Down
28 changes: 12 additions & 16 deletions Userland/Libraries/LibWeb/Loader/FrameLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ bool FrameLoader::load(LoadRequest& request, Type type)
auto& url = request.url();

if (type == Type::Navigation || type == Type::Reload || type == Type::Redirect) {
if (auto* page = browsing_context().page()) {
if (&page->top_level_browsing_context() == m_browsing_context)
page->client().page_did_start_loading(url, type == Type::Redirect);
}
auto& page = browsing_context().page();
if (&page.top_level_browsing_context() == m_browsing_context)
page.client().page_did_start_loading(url, type == Type::Redirect);
}

// https://fetch.spec.whatwg.org/#concept-fetch
Expand Down Expand Up @@ -133,7 +132,7 @@ bool FrameLoader::load(const AK::URL& url, Type type)
return false;
}

auto request = LoadRequest::create_for_url_on_page(url, browsing_context().page());
auto request = LoadRequest::create_for_url_on_page(url, &browsing_context().page());
return load(request, type);
}

Expand Down Expand Up @@ -174,7 +173,7 @@ void FrameLoader::set_error_page_url(DeprecatedString error_page_url)

void FrameLoader::load_error_page(const AK::URL& failed_url, DeprecatedString const& error)
{
LoadRequest request = LoadRequest::create_for_url_on_page(s_error_page_url, browsing_context().page());
LoadRequest request = LoadRequest::create_for_url_on_page(s_error_page_url, &browsing_context().page());

ResourceLoader::the().load(
request,
Expand All @@ -195,12 +194,11 @@ void FrameLoader::load_error_page(const AK::URL& failed_url, DeprecatedString co

void FrameLoader::load_favicon(RefPtr<Gfx::Bitmap> bitmap)
{
if (auto* page = browsing_context().page()) {
if (bitmap)
page->client().page_did_change_favicon(*bitmap);
else if (s_default_favicon_bitmap)
page->client().page_did_change_favicon(*s_default_favicon_bitmap);
}
auto& page = browsing_context().page();
if (bitmap)
page.client().page_did_change_favicon(*bitmap);
else if (s_default_favicon_bitmap)
page.client().page_did_change_favicon(*s_default_favicon_bitmap);
}

void FrameLoader::resource_did_load()
Expand Down Expand Up @@ -287,8 +285,7 @@ void FrameLoader::resource_did_load()
document->set_content_type(resource()->mime_type());

browsing_context().set_active_document(document);
if (auto* page = browsing_context().page())
page->client().page_did_create_main_document();
browsing_context().page().client().page_did_create_main_document();

if (!parse_document(*document, resource()->encoded_data())) {
load_error_page(url, "Failed to parse content.");
Expand All @@ -300,8 +297,7 @@ void FrameLoader::resource_did_load()
else
browsing_context().scroll_to({ 0, 0 });

if (auto* page = browsing_context().page())
page->client().page_did_finish_loading(url);
browsing_context().page().client().page_did_finish_loading(url);
}

void FrameLoader::resource_did_fail()
Expand Down
61 changes: 26 additions & 35 deletions Userland/Libraries/LibWeb/Page/EventHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,9 @@ bool EventHandler::handle_mousewheel(CSSPixelPoint position, unsigned button, un

auto offset = compute_mouse_event_offset(position, *layout_node);
if (node->dispatch_event(UIEvents::WheelEvent::create_from_platform_event(node->realm(), UIEvents::EventNames::wheel, offset.x(), offset.y(), position.x(), position.y(), wheel_delta_x, wheel_delta_y, buttons, button).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 * 20, wheel_delta_y * 20);
}
auto& page = m_browsing_context->page();
if (m_browsing_context == &page.top_level_browsing_context())
page.client().page_did_request_scroll(wheel_delta_x * 20, wheel_delta_y * 20);
}

handled_event = true;
Expand Down Expand Up @@ -291,23 +290,19 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, unsigned button, unsig
m_browsing_context->scroll_to_anchor(url.fragment());
} else {
if (m_browsing_context->is_top_level()) {
if (auto* page = m_browsing_context->page())
page->client().page_did_click_link(url, link->target(), modifiers);
m_browsing_context->page().client().page_did_click_link(url, link->target(), modifiers);
}
}
} else 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<HTML::HTMLImageElement>(*node)) {
auto& image_element = verify_cast<HTML::HTMLImageElement>(*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<HTML::HTMLMediaElement>(*node)) {
auto& media_element = verify_cast<HTML::HTMLMediaElement>(*node);

Expand All @@ -320,10 +315,9 @@ bool EventHandler::handle_mouseup(CSSPixelPoint position, unsigned button, unsig
.is_looping = media_element.has_attribute(HTML::AttributeNames::loop),
};

if (auto* page = m_browsing_context->page())
page->did_request_media_context_menu(media_element.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.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));
}
}
}
Expand Down Expand Up @@ -375,8 +369,7 @@ bool EventHandler::handle_mousedown(CSSPixelPoint position, unsigned button, uns
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)
Expand Down Expand Up @@ -457,8 +450,7 @@ bool EventHandler::handle_mousemove(CSSPixelPoint position, unsigned buttons, un
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);
Expand Down Expand Up @@ -520,27 +512,26 @@ bool EventHandler::handle_mousemove(CSSPixelPoint position, unsigned buttons, un
}
m_browsing_context->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();
page.client().page_did_request_cursor_change(hovered_node_cursor);

if (hovered_node_changed) {
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().is_null()) {
page->client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title());
} 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();
if (hovered_node_changed) {
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().is_null()) {
page.client().page_did_enter_tooltip_area(m_browsing_context->to_top_level_position(position), hovered_html_element->title());
} 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;
}

Expand Down

0 comments on commit 6fcea38

Please sign in to comment.