1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-05-31 13:48:12 +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/Size.h>
#include <LibGfx/StandardCursor.h> #include <LibGfx/StandardCursor.h>
#include <LibIPC/Forward.h> #include <LibIPC/Forward.h>
#include <LibJS/Heap/Cell.h>
#include <LibJS/Heap/Handle.h> #include <LibJS/Heap/Handle.h>
#include <LibWeb/CSS/PreferredColorScheme.h> #include <LibWeb/CSS/PreferredColorScheme.h>
#include <LibWeb/CSS/Selector.h> #include <LibWeb/CSS/Selector.h>
@ -193,7 +194,9 @@ private:
bool m_pdf_viewer_supported { false }; bool m_pdf_viewer_supported { false };
}; };
class PageClient { class PageClient : public JS::Cell {
JS_CELL(PageClient, JS::Cell);
public: public:
virtual Page& page() = 0; virtual Page& page() = 0;
virtual Page const& page() const = 0; virtual Page const& page() const = 0;

View file

@ -5,6 +5,7 @@
*/ */
#include <LibGfx/Bitmap.h> #include <LibGfx/Bitmap.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/DOM/Document.h> #include <LibWeb/DOM/Document.h>
#include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h> #include <LibWeb/Fetch/Infrastructure/HTTP/Responses.h>
#include <LibWeb/HTML/BrowsingContext.h> #include <LibWeb/HTML/BrowsingContext.h>
@ -23,10 +24,12 @@
namespace Web::SVG { namespace Web::SVG {
class SVGDecodedImageData::SVGPageClient final : public PageClient { class SVGDecodedImageData::SVGPageClient final : public PageClient {
JS_CELL(SVGDecodedImageData::SVGPageClient, PageClient);
public: public:
explicit SVGPageClient(Page& host_page) static JS::NonnullGCPtr<SVGPageClient> create(JS::VM& vm, Page& page)
: m_host_page(host_page)
{ {
return vm.heap().allocate_without_realm<SVGPageClient>(page);
} }
virtual ~SVGPageClient() override = default; 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 CSS::PreferredColorScheme preferred_color_scheme() const override { return m_host_page.client().preferred_color_scheme(); }
virtual void request_file(FileRequest) override { } virtual void request_file(FileRequest) override { }
virtual void paint(DevicePixelRect const&, Gfx::Bitmap&) 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) 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); auto page = make<Page>(*page_client);
page_client->m_svg_page = page.ptr(); 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")))); 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))); 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(move(page))
, m_page_client(move(page_client)) , m_page_client(move(page_client))
, m_document(move(document)) , m_document(move(document))

View file

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

View file

@ -39,6 +39,11 @@ void PageClient::set_use_gpu_painter()
s_use_gpu_painter = true; 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) PageClient::PageClient(PageHost& owner, u64 id)
: m_owner(owner) : m_owner(owner)
, m_page(make<Web::Page>(*this)) , m_page(make<Web::Page>(*this))

View file

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

View file

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

View file

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

View file

@ -36,7 +36,7 @@ void ConnectionFromClient::request_file(Web::FileRequest request)
ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket) ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
: IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(socket), 1) : 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 <AK/HashMap.h>
#include <LibIPC/ConnectionFromClient.h> #include <LibIPC/ConnectionFromClient.h>
#include <LibJS/Forward.h> #include <LibJS/Forward.h>
#include <LibJS/Heap/Handle.h>
#include <LibWeb/Forward.h> #include <LibWeb/Forward.h>
#include <LibWeb/Loader/FileRequest.h> #include <LibWeb/Loader/FileRequest.h>
#include <LibWeb/Worker/WebWorkerClientEndpoint.h> #include <LibWeb/Worker/WebWorkerClientEndpoint.h>
#include <LibWeb/Worker/WebWorkerServerEndpoint.h> #include <LibWeb/Worker/WebWorkerServerEndpoint.h>
#include <Services/WebWorker/PageHost.h>
#include <WebWorker/Forward.h> #include <WebWorker/Forward.h>
namespace WebWorker { 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 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; 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 // FIXME: Route console messages to the Browser UI using a ConsoleClient

View file

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

View file

@ -14,11 +14,11 @@
namespace WebWorker { namespace WebWorker {
class PageHost final : public Web::PageClient { class PageHost final : public Web::PageClient {
AK_MAKE_NONCOPYABLE(PageHost); JS_CELL(PageHost, Web::PageClient);
AK_MAKE_NONMOVABLE(PageHost);
public: 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 ~PageHost();
virtual Web::Page& page() override; virtual Web::Page& page() override;