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

LibWeb: Make Web::PageClient GC-allocated

This is a first step towards simplifying the ownership model of
Web::Page. Soon Web::Page will store its WebClient as a
NonnullGCPtr to help solve lifetime issues of the client being
destroyed before the page.
This commit is contained in:
Shannon Booth 2023-12-04 21:40:33 +13:00 committed by Andreas Kling
parent 5c0cd0f484
commit 6e6f3a9a8f
11 changed files with 45 additions and 17 deletions

View file

@ -22,6 +22,7 @@
#include <LibGfx/Size.h>
#include <LibGfx/StandardCursor.h>
#include <LibIPC/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/CSS/Selector.h>
@ -193,7 +194,9 @@ private:
bool m_pdf_viewer_supported { false };
};
class PageClient {
class PageClient : public JS::Cell {
JS_CELL(PageClient, JS::Cell);
public:
virtual Page& page() = 0;
virtual Page const& page() const = 0;

View file

@ -5,6 +5,7 @@
*/
#include <LibGfx/Bitmap.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/BrowsingContext.h>
@ -23,10 +24,12 @@
namespace Web::SVG {
class SVGDecodedImageData::SVGPageClient final : public PageClient {
JS_CELL(SVGDecodedImageData::SVGPageClient, PageClient);
public:
explicit SVGPageClient(Page& host_page)
: m_host_page(host_page)
static JS::NonnullGCPtr<SVGPageClient> create(JS::VM& vm, Page& page)
{
return vm.heap().allocate_without_realm<SVGPageClient>(page);
}
virtual ~SVGPageClient() override = default;
@ -43,11 +46,17 @@ public:
virtual CSS::PreferredColorScheme preferred_color_scheme() const override { return m_host_page.client().preferred_color_scheme(); }
virtual void request_file(FileRequest) override { }
virtual void paint(DevicePixelRect const&, Gfx::Bitmap&) override { }
private:
explicit SVGPageClient(Page& host_page)
: m_host_page(host_page)
{
}
};
ErrorOr<NonnullRefPtr<SVGDecodedImageData>> SVGDecodedImageData::create(Page& host_page, AK::URL const& url, ByteBuffer data)
{
auto page_client = make<SVGPageClient>(host_page);
auto page_client = SVGPageClient::create(Bindings::main_thread_vm(), host_page);
auto page = make<Page>(*page_client);
page_client->m_svg_page = page.ptr();
page->set_top_level_traversable(MUST(Web::HTML::TraversableNavigable::create_a_fresh_top_level_traversable(*page, AK::URL("about:blank"))));
@ -92,7 +101,7 @@ ErrorOr<NonnullRefPtr<SVGDecodedImageData>> SVGDecodedImageData::create(Page& ho
return adopt_nonnull_ref_or_enomem(new (nothrow) SVGDecodedImageData(move(page), move(page_client), move(document), move(svg_root)));
}
SVGDecodedImageData::SVGDecodedImageData(NonnullOwnPtr<Page> page, NonnullOwnPtr<SVGPageClient> page_client, JS::Handle<DOM::Document> document, JS::Handle<SVG::SVGSVGElement> root_element)
SVGDecodedImageData::SVGDecodedImageData(NonnullOwnPtr<Page> page, JS::Handle<SVGPageClient> page_client, JS::Handle<DOM::Document> document, JS::Handle<SVG::SVGSVGElement> root_element)
: m_page(move(page))
, m_page_client(move(page_client))
, m_document(move(document))

View file

@ -32,13 +32,13 @@ public:
private:
class SVGPageClient;
SVGDecodedImageData(NonnullOwnPtr<Page>, NonnullOwnPtr<SVGPageClient>, JS::Handle<DOM::Document>, JS::Handle<SVG::SVGSVGElement>);
SVGDecodedImageData(NonnullOwnPtr<Page>, JS::Handle<SVGPageClient>, JS::Handle<DOM::Document>, JS::Handle<SVG::SVGSVGElement>);
RefPtr<Gfx::Bitmap> render(Gfx::IntSize) const;
mutable RefPtr<Gfx::ImmutableBitmap> m_immutable_bitmap;
NonnullOwnPtr<Page> m_page;
NonnullOwnPtr<SVGPageClient> m_page_client;
JS::Handle<SVGPageClient> m_page_client;
JS::Handle<DOM::Document> m_document;
JS::Handle<SVG::SVGSVGElement> m_root_element;

View file

@ -39,6 +39,11 @@ void PageClient::set_use_gpu_painter()
s_use_gpu_painter = true;
}
JS::NonnullGCPtr<PageClient> PageClient::create(JS::VM& vm, PageHost& page_host, u64 id)
{
return vm.heap().allocate_without_realm<PageClient>(page_host, id);
}
PageClient::PageClient(PageHost& owner, u64 id)
: m_owner(owner)
, m_page(make<Web::Page>(*this))

View file

@ -17,11 +17,10 @@
namespace WebContent {
class PageClient final : public Web::PageClient {
AK_MAKE_NONCOPYABLE(PageClient);
AK_MAKE_NONMOVABLE(PageClient);
JS_CELL(PageClient, Web::PageClient);
public:
PageClient(PageHost&, u64 id);
static JS::NonnullGCPtr<PageClient> create(JS::VM& vm, PageHost& page_host, u64 id);
static void set_use_gpu_painter();
@ -60,6 +59,8 @@ public:
void set_user_style(String source);
private:
PageClient(PageHost&, u64 id);
// ^PageClient
virtual bool is_connection_open() const override;
virtual Gfx::Palette palette() const override;

View file

@ -6,6 +6,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/HTML/TraversableNavigable.h>
#include <WebContent/ConnectionFromClient.h>
#include <WebContent/PageClient.h>
@ -23,7 +24,7 @@ PageHost::PageHost(ConnectionFromClient& client)
PageClient& PageHost::create_page()
{
m_pages.set(m_next_id, make<PageClient>(*this, m_next_id));
m_pages.set(m_next_id, PageClient::create(Web::Bindings::main_thread_vm(), *this, m_next_id));
++m_next_id;
return *m_pages.get(m_next_id - 1).value();
}

View file

@ -11,6 +11,7 @@
#include <AK/Function.h>
#include <AK/HashMap.h>
#include <AK/NonnullOwnPtr.h>
#include <LibJS/Heap/Handle.h>
#include <WebContent/Forward.h>
namespace WebContent {
@ -34,7 +35,7 @@ private:
explicit PageHost(ConnectionFromClient&);
ConnectionFromClient& m_client;
HashMap<u64, NonnullOwnPtr<PageClient>> m_pages;
HashMap<u64, JS::Handle<PageClient>> m_pages;
u64 m_next_id { 0 };
};

View file

@ -36,7 +36,7 @@ void ConnectionFromClient::request_file(Web::FileRequest request)
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(socket), 1)
, m_page_host(PageHost::create(*this))
, m_page_host(PageHost::create(Web::Bindings::main_thread_vm(), *this))
{
}

View file

@ -9,10 +9,12 @@
#include <AK/HashMap.h>
#include <LibIPC/ConnectionFromClient.h>
#include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Forward.h>
#include <LibWeb/Loader/FileRequest.h>
#include <LibWeb/Worker/WebWorkerClientEndpoint.h>
#include <LibWeb/Worker/WebWorkerServerEndpoint.h>
#include <Services/WebWorker/PageHost.h>
#include <WebWorker/Forward.h>
namespace WebWorker {
@ -40,7 +42,7 @@ private:
virtual void start_dedicated_worker(AK::URL const& url, String const&, String const&, String const&, IPC::File const&) override;
virtual void handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id) override;
NonnullOwnPtr<PageHost> m_page_host;
JS::Handle<PageHost> m_page_host;
// FIXME: Route console messages to the Browser UI using a ConsoleClient

View file

@ -4,11 +4,17 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/VM.h>
#include <WebWorker/ConnectionFromClient.h>
#include <WebWorker/PageHost.h>
namespace WebWorker {
JS::NonnullGCPtr<PageHost> PageHost::create(JS::VM& vm, ConnectionFromClient& client)
{
return vm.heap().allocate_without_realm<PageHost>(client);
}
PageHost::~PageHost() = default;
Web::Page& PageHost::page()

View file

@ -14,11 +14,11 @@
namespace WebWorker {
class PageHost final : public Web::PageClient {
AK_MAKE_NONCOPYABLE(PageHost);
AK_MAKE_NONMOVABLE(PageHost);
JS_CELL(PageHost, Web::PageClient);
public:
static NonnullOwnPtr<PageHost> create(ConnectionFromClient& client) { return adopt_own(*new PageHost(client)); }
static JS::NonnullGCPtr<PageHost> create(JS::VM& vm, ConnectionFromClient& client);
virtual ~PageHost();
virtual Web::Page& page() override;