1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 20:17:44 +00:00

LibWeb: Rename Web::Frame to Web::BrowsingContext

Our "frame" concept very closely matches what the web specs call a
"browsing context", so let's rename it to that. :^)

The "main frame" becomes the "top-level browsing context",
and "sub-frames" are now "nested browsing contexts".
This commit is contained in:
Andreas Kling 2021-05-30 12:36:53 +02:00
parent 8be98af77c
commit 4190fd2199
43 changed files with 241 additions and 241 deletions

View file

@ -13,14 +13,14 @@
#include <LibWeb/Layout/BreakNode.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Layout/TextNode.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/UIEvents/EventNames.h>
namespace Web {
Frame::Frame(DOM::Element& host_element, Frame& main_frame)
: m_page(*main_frame.page())
, m_main_frame(main_frame)
BrowsingContext::BrowsingContext(DOM::Element& host_element, BrowsingContext& top_level_browsing_context)
: m_page(*top_level_browsing_context.page())
, m_top_level_browsing_context(top_level_browsing_context)
, m_loader(*this)
, m_event_handler({}, *this)
, m_host_element(host_element)
@ -28,23 +28,23 @@ Frame::Frame(DOM::Element& host_element, Frame& main_frame)
setup();
}
Frame::Frame(Page& page)
BrowsingContext::BrowsingContext(Page& page)
: m_page(page)
, m_main_frame(*this)
, m_top_level_browsing_context(*this)
, m_loader(*this)
, m_event_handler({}, *this)
{
setup();
}
Frame::~Frame()
BrowsingContext::~BrowsingContext()
{
}
void Frame::setup()
void BrowsingContext::setup()
{
m_cursor_blink_timer = Core::Timer::construct(500, [this] {
if (!is_focused_frame())
if (!is_focused_context())
return;
if (m_cursor_position.node() && m_cursor_position.node()->layout_node()) {
m_cursor_blink_state = !m_cursor_blink_state;
@ -53,24 +53,24 @@ void Frame::setup()
});
}
void Frame::did_edit(Badge<EditEventHandler>)
void BrowsingContext::did_edit(Badge<EditEventHandler>)
{
reset_cursor_blink_cycle();
}
void Frame::reset_cursor_blink_cycle()
void BrowsingContext::reset_cursor_blink_cycle()
{
m_cursor_blink_state = true;
m_cursor_blink_timer->restart();
m_cursor_position.node()->layout_node()->set_needs_display();
}
bool Frame::is_focused_frame() const
bool BrowsingContext::is_focused_context() const
{
return m_page && &m_page->focused_frame() == this;
return m_page && &m_page->focused_context() == this;
}
void Frame::set_document(DOM::Document* document)
void BrowsingContext::set_document(DOM::Document* document)
{
if (m_document == document)
return;
@ -78,21 +78,21 @@ void Frame::set_document(DOM::Document* document)
m_cursor_position = {};
if (m_document)
m_document->detach_from_frame({}, *this);
m_document->detach_from_browsing_context({}, *this);
m_document = document;
if (m_document) {
m_document->attach_to_frame({}, *this);
if (m_page && is_main_frame())
m_document->attach_to_browsing_context({}, *this);
if (m_page && is_top_level())
m_page->client().page_did_change_title(m_document->title());
}
if (m_page)
m_page->client().page_did_set_document_in_main_frame(m_document);
m_page->client().page_did_set_document_in_top_level_browsing_context(m_document);
}
void Frame::set_viewport_rect(const Gfx::IntRect& rect)
void BrowsingContext::set_viewport_rect(const Gfx::IntRect& rect)
{
bool did_change = false;
@ -116,7 +116,7 @@ void Frame::set_viewport_rect(const Gfx::IntRect& rect)
}
}
void Frame::set_size(const Gfx::IntSize& size)
void BrowsingContext::set_size(const Gfx::IntSize& size)
{
if (m_size == size)
return;
@ -130,7 +130,7 @@ void Frame::set_size(const Gfx::IntSize& size)
client->frame_did_set_viewport_rect(viewport_rect());
}
void Frame::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
void BrowsingContext::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
{
if (m_viewport_scroll_offset == offset)
return;
@ -140,14 +140,14 @@ void Frame::set_viewport_scroll_offset(const Gfx::IntPoint& offset)
client->frame_did_set_viewport_rect(viewport_rect());
}
void Frame::set_needs_display(const Gfx::IntRect& rect)
void BrowsingContext::set_needs_display(const Gfx::IntRect& rect)
{
if (!viewport_rect().intersects(rect))
return;
if (is_main_frame()) {
if (is_top_level()) {
if (m_page)
m_page->client().page_did_invalidate(to_main_frame_rect(rect));
m_page->client().page_did_invalidate(to_top_level_rect(rect));
return;
}
@ -155,7 +155,7 @@ void Frame::set_needs_display(const Gfx::IntRect& rect)
host_element()->layout_node()->set_needs_display();
}
void Frame::scroll_to_anchor(const String& fragment)
void BrowsingContext::scroll_to_anchor(const String& fragment)
{
if (!document())
return;
@ -190,18 +190,18 @@ void Frame::scroll_to_anchor(const String& fragment)
m_page->client().page_did_request_scroll_into_view(enclosing_int_rect(float_rect));
}
Gfx::IntRect Frame::to_main_frame_rect(const Gfx::IntRect& a_rect)
Gfx::IntRect BrowsingContext::to_top_level_rect(const Gfx::IntRect& a_rect)
{
auto rect = a_rect;
rect.set_location(to_main_frame_position(a_rect.location()));
rect.set_location(to_top_level_position(a_rect.location()));
return rect;
}
Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
Gfx::IntPoint BrowsingContext::to_top_level_position(const Gfx::IntPoint& a_position)
{
auto position = a_position;
for (auto* ancestor = parent(); ancestor; ancestor = ancestor->parent()) {
if (ancestor->is_main_frame())
if (ancestor->is_top_level())
break;
if (!ancestor->host_element())
return {};
@ -212,7 +212,7 @@ Gfx::IntPoint Frame::to_main_frame_position(const Gfx::IntPoint& a_position)
return position;
}
void Frame::set_cursor_position(DOM::Position position)
void BrowsingContext::set_cursor_position(DOM::Position position)
{
if (m_cursor_position == position)
return;
@ -228,7 +228,7 @@ void Frame::set_cursor_position(DOM::Position position)
reset_cursor_blink_cycle();
}
String Frame::selected_text() const
String BrowsingContext::selected_text() const
{
StringBuilder builder;
if (!m_document)
@ -275,29 +275,29 @@ String Frame::selected_text() const
return builder.to_string();
}
void Frame::register_viewport_client(ViewportClient& client)
void BrowsingContext::register_viewport_client(ViewportClient& client)
{
auto result = m_viewport_clients.set(&client);
VERIFY(result == AK::HashSetResult::InsertedNewEntry);
}
void Frame::unregister_viewport_client(ViewportClient& client)
void BrowsingContext::unregister_viewport_client(ViewportClient& client)
{
bool was_removed = m_viewport_clients.remove(&client);
VERIFY(was_removed);
}
void Frame::register_frame_nesting(URL const& url)
void BrowsingContext::register_frame_nesting(URL const& url)
{
m_frame_nesting_levels.ensure(url)++;
}
bool Frame::is_frame_nesting_allowed(URL const& url) const
bool BrowsingContext::is_frame_nesting_allowed(URL const& url) const
{
return m_frame_nesting_levels.get(url).value_or(0) < 3;
}
bool Frame::increment_cursor_position_offset()
bool BrowsingContext::increment_cursor_position_offset()
{
if (!m_cursor_position.increment_offset())
return false;
@ -305,7 +305,7 @@ bool Frame::increment_cursor_position_offset()
return true;
}
bool Frame::decrement_cursor_position_offset()
bool BrowsingContext::decrement_cursor_position_offset()
{
if (!m_cursor_position.decrement_offset())
return false;

View file

@ -21,11 +21,11 @@
namespace Web {
class Frame : public TreeNode<Frame> {
class BrowsingContext : public TreeNode<BrowsingContext> {
public:
static NonnullRefPtr<Frame> create_subframe(DOM::Element& host_element, Frame& main_frame) { return adopt_ref(*new Frame(host_element, main_frame)); }
static NonnullRefPtr<Frame> create(Page& page) { return adopt_ref(*new Frame(page)); }
~Frame();
static NonnullRefPtr<BrowsingContext> create_nested(DOM::Element& host_element, BrowsingContext& top_level_browsing_context) { return adopt_ref(*new BrowsingContext(host_element, top_level_browsing_context)); }
static NonnullRefPtr<BrowsingContext> create(Page& page) { return adopt_ref(*new BrowsingContext(page)); }
~BrowsingContext();
class ViewportClient {
public:
@ -35,8 +35,8 @@ public:
void register_viewport_client(ViewportClient&);
void unregister_viewport_client(ViewportClient&);
bool is_main_frame() const { return this == &m_main_frame; }
bool is_focused_frame() const;
bool is_top_level() const { return this == &m_top_level_browsing_context; }
bool is_focused_context() const;
const DOM::Document* document() const { return m_document; }
DOM::Document* document() { return m_document; }
@ -63,14 +63,14 @@ public:
void scroll_to_anchor(const String&);
Frame& main_frame() { return m_main_frame; }
const Frame& main_frame() const { return m_main_frame; }
BrowsingContext& top_level_browsing_context() { return m_top_level_browsing_context; }
BrowsingContext const& top_level_browsing_context() const { return m_top_level_browsing_context; }
DOM::Element* host_element() { return m_host_element; }
const DOM::Element* host_element() const { return m_host_element; }
Gfx::IntPoint to_main_frame_position(const Gfx::IntPoint&);
Gfx::IntRect to_main_frame_rect(const Gfx::IntRect&);
Gfx::IntPoint to_top_level_position(const Gfx::IntPoint&);
Gfx::IntRect to_top_level_rect(const Gfx::IntRect&);
const DOM::Position& cursor_position() const { return m_cursor_position; }
void set_cursor_position(DOM::Position);
@ -90,15 +90,15 @@ public:
HashMap<URL, size_t> const& frame_nesting_levels() const { return m_frame_nesting_levels; }
private:
explicit Frame(DOM::Element& host_element, Frame& main_frame);
explicit Frame(Page&);
explicit BrowsingContext(DOM::Element& host_element, BrowsingContext& top_level_browsing_context);
explicit BrowsingContext(Page&);
void reset_cursor_blink_cycle();
void setup();
WeakPtr<Page> m_page;
Frame& m_main_frame;
BrowsingContext& m_top_level_browsing_context;
FrameLoader m_loader;
EventHandler m_event_handler;

View file

@ -12,8 +12,8 @@
#include <LibWeb/DOM/Text.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Layout/LayoutPosition.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/EditEventHandler.h>
#include <LibWeb/Page/Frame.h>
namespace Web {

View file

@ -13,7 +13,7 @@ namespace Web {
class EditEventHandler {
public:
explicit EditEventHandler(Frame& frame)
explicit EditEventHandler(BrowsingContext& frame)
: m_frame(frame)
{
}
@ -25,7 +25,7 @@ public:
virtual void handle_insert(DOM::Position, u32 code_point);
private:
Frame& m_frame;
BrowsingContext& m_frame;
};
}

View file

@ -14,8 +14,8 @@
#include <LibWeb/HTML/HTMLImageElement.h>
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Layout/InitialContainingBlockBox.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/EventHandler.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/UIEvents/EventNames.h>
#include <LibWeb/UIEvents/MouseEvent.h>
@ -87,7 +87,7 @@ static Gfx::IntPoint compute_mouse_event_offset(const Gfx::IntPoint& position, c
};
}
EventHandler::EventHandler(Badge<Frame>, Frame& frame)
EventHandler::EventHandler(Badge<BrowsingContext>, BrowsingContext& frame)
: m_frame(frame)
, m_edit_event_handler(make<EditEventHandler>(frame))
{
@ -158,7 +158,7 @@ bool EventHandler::handle_mouseup(const Gfx::IntPoint& position, unsigned button
if (result.layout_node && result.layout_node->dom_node()) {
RefPtr<DOM::Node> node = result.layout_node->dom_node();
if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
return subframe->event_handler().handle_mouseup(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
return false;
}
@ -202,13 +202,13 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
return false;
if (is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
return subframe->event_handler().handle_mousedown(position.translated(compute_mouse_event_offset({}, *result.layout_node)), button, modifiers);
return false;
}
if (auto* page = m_frame.page())
page->set_focused_frame({}, m_frame);
page->set_focused_browsing_context({}, m_frame);
auto offset = compute_mouse_event_offset(position, *result.layout_node);
node->dispatch_event(UIEvents::MouseEvent::create(UIEvents::EventNames::mousedown, offset.x(), offset.y(), position.x(), position.y()));
@ -222,7 +222,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
auto& image_element = downcast<HTML::HTMLImageElement>(*node);
auto image_url = image_element.document().complete_url(image_element.src());
if (auto* page = m_frame.page())
page->client().page_did_request_image_context_menu(m_frame.to_main_frame_position(position), image_url, "", modifiers, image_element.bitmap());
page->client().page_did_request_image_context_menu(m_frame.to_top_level_position(position), image_url, "", modifiers, image_element.bitmap());
return true;
}
@ -237,7 +237,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
auto anchor = href.substring_view(1, href.length() - 1);
m_frame.scroll_to_anchor(anchor);
} else {
if (m_frame.is_main_frame()) {
if (m_frame.is_top_level()) {
if (auto* page = m_frame.page())
page->client().page_did_click_link(url, link->target(), modifiers);
} else {
@ -247,7 +247,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
}
} else if (button == GUI::MouseButton::Right) {
if (auto* page = m_frame.page())
page->client().page_did_request_link_context_menu(m_frame.to_main_frame_position(position), url, link->target(), modifiers);
page->client().page_did_request_link_context_menu(m_frame.to_top_level_position(position), url, link->target(), modifiers);
} else if (button == GUI::MouseButton::Middle) {
if (auto* page = m_frame.page())
page->client().page_did_middle_click_link(url, link->target(), modifiers);
@ -262,7 +262,7 @@ bool EventHandler::handle_mousedown(const Gfx::IntPoint& position, unsigned butt
}
} else if (button == GUI::MouseButton::Right) {
if (auto* page = m_frame.page())
page->client().page_did_request_context_menu(m_frame.to_main_frame_position(position));
page->client().page_did_request_context_menu(m_frame.to_top_level_position(position));
}
}
return true;
@ -299,7 +299,7 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
RefPtr<DOM::Node> node = result.layout_node->dom_node();
if (node && is<HTML::HTMLIFrameElement>(*node)) {
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).content_frame())
if (auto* subframe = downcast<HTML::HTMLIFrameElement>(*node).nested_browsing_context())
return subframe->event_handler().handle_mousemove(position.translated(compute_mouse_event_offset({}, *result.layout_node)), buttons, modifiers);
return false;
}
@ -340,7 +340,7 @@ bool EventHandler::handle_mousemove(const Gfx::IntPoint& position, unsigned butt
if (hovered_node_changed) {
RefPtr<HTML::HTMLElement> 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_frame.to_main_frame_position(position), hovered_html_element->title());
page->client().page_did_enter_tooltip_area(m_frame.to_top_level_position(position), hovered_html_element->title());
} else {
page->client().page_did_leave_tooltip_area();
}

View file

@ -17,11 +17,11 @@
namespace Web {
class Frame;
class BrowsingContext;
class EventHandler {
public:
explicit EventHandler(Badge<Frame>, Frame&);
explicit EventHandler(Badge<BrowsingContext>, BrowsingContext&);
~EventHandler();
bool handle_mouseup(const Gfx::IntPoint&, unsigned button, unsigned modifiers);
@ -42,7 +42,7 @@ private:
Layout::InitialContainingBlockBox* layout_root();
const Layout::InitialContainingBlockBox* layout_root() const;
Frame& m_frame;
BrowsingContext& m_frame;
bool m_in_mouse_selection { false };

View file

@ -5,7 +5,7 @@
*/
#include <LibWeb/InProcessWebView.h>
#include <LibWeb/Page/Frame.h>
#include <LibWeb/Page/BrowsingContext.h>
#include <LibWeb/Page/Page.h>
namespace Web {
@ -13,38 +13,38 @@ namespace Web {
Page::Page(PageClient& client)
: m_client(client)
{
m_main_frame = Frame::create(*this);
m_top_level_browsing_context = BrowsingContext::create(*this);
}
Page::~Page()
{
}
Frame& Page::focused_frame()
BrowsingContext& Page::focused_context()
{
if (m_focused_frame)
return *m_focused_frame;
return main_frame();
if (m_focused_context)
return *m_focused_context;
return top_level_browsing_context();
}
void Page::set_focused_frame(Badge<EventHandler>, Frame& frame)
void Page::set_focused_browsing_context(Badge<EventHandler>, BrowsingContext& browsing_context)
{
m_focused_frame = frame.make_weak_ptr();
m_focused_context = browsing_context.make_weak_ptr();
}
void Page::load(const URL& url)
{
main_frame().loader().load(url, FrameLoader::Type::Navigation);
top_level_browsing_context().loader().load(url, FrameLoader::Type::Navigation);
}
void Page::load(const LoadRequest& request)
{
main_frame().loader().load(request, FrameLoader::Type::Navigation);
top_level_browsing_context().loader().load(request, FrameLoader::Type::Navigation);
}
void Page::load_html(const StringView& html, const URL& url)
{
main_frame().loader().load_html(html, url);
top_level_browsing_context().loader().load_html(html, url);
}
Gfx::Palette Page::palette() const
@ -59,27 +59,27 @@ Gfx::IntRect Page::screen_rect() const
bool Page::handle_mousewheel(const Gfx::IntPoint& position, unsigned button, unsigned modifiers, int wheel_delta)
{
return main_frame().event_handler().handle_mousewheel(position, button, modifiers, wheel_delta);
return top_level_browsing_context().event_handler().handle_mousewheel(position, button, modifiers, wheel_delta);
}
bool Page::handle_mouseup(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
return main_frame().event_handler().handle_mouseup(position, button, modifiers);
return top_level_browsing_context().event_handler().handle_mouseup(position, button, modifiers);
}
bool Page::handle_mousedown(const Gfx::IntPoint& position, unsigned button, unsigned modifiers)
{
return main_frame().event_handler().handle_mousedown(position, button, modifiers);
return top_level_browsing_context().event_handler().handle_mousedown(position, button, modifiers);
}
bool Page::handle_mousemove(const Gfx::IntPoint& position, unsigned buttons, unsigned modifiers)
{
return main_frame().event_handler().handle_mousemove(position, buttons, modifiers);
return top_level_browsing_context().event_handler().handle_mousemove(position, buttons, modifiers);
}
bool Page::handle_keydown(KeyCode key, unsigned modifiers, u32 code_point)
{
return focused_frame().event_handler().handle_keydown(key, modifiers, code_point);
return focused_context().event_handler().handle_keydown(key, modifiers, code_point);
}
}

View file

@ -33,13 +33,13 @@ public:
PageClient& client() { return m_client; }
const PageClient& client() const { return m_client; }
Web::Frame& main_frame() { return *m_main_frame; }
const Web::Frame& main_frame() const { return *m_main_frame; }
Web::BrowsingContext& top_level_browsing_context() { return *m_top_level_browsing_context; }
const Web::BrowsingContext& top_level_browsing_context() const { return *m_top_level_browsing_context; }
Web::Frame& focused_frame();
const Web::Frame& focused_frame() const { return const_cast<Page*>(this)->focused_frame(); }
Web::BrowsingContext& focused_context();
const Web::BrowsingContext& focused_context() const { return const_cast<Page*>(this)->focused_context(); }
void set_focused_frame(Badge<EventHandler>, Frame&);
void set_focused_browsing_context(Badge<EventHandler>, BrowsingContext&);
void load(const URL&);
void load(const LoadRequest&);
@ -59,8 +59,8 @@ public:
private:
PageClient& m_client;
RefPtr<Frame> m_main_frame;
WeakPtr<Frame> m_focused_frame;
RefPtr<BrowsingContext> m_top_level_browsing_context;
WeakPtr<BrowsingContext> m_focused_context;
};
class PageClient {
@ -68,7 +68,7 @@ public:
virtual bool is_multi_process() const = 0;
virtual Gfx::Palette palette() const = 0;
virtual Gfx::IntRect screen_rect() const = 0;
virtual void page_did_set_document_in_main_frame(DOM::Document*) { }
virtual void page_did_set_document_in_top_level_browsing_context(DOM::Document*) { }
virtual void page_did_change_title(const String&) { }
virtual void page_did_start_loading(const URL&) { }
virtual void page_did_finish_loading(const URL&) { }