Everywhere: Hoist the Services folder to the top-level

This commit is contained in:
Timothy Flynn 2024-11-09 12:13:18 -05:00 committed by Andreas Kling
commit 22e0eeada2
Notes: github-actions[bot] 2024-11-10 11:52:06 +00:00
68 changed files with 41 additions and 41 deletions

View file

@ -0,0 +1,42 @@
include(fontconfig)
set(WEBWORKER_SOURCES
${LADYBIRD_SOURCE_DIR}/Ladybird/FontPlugin.cpp
${LADYBIRD_SOURCE_DIR}/Ladybird/HelperProcess.cpp
${LADYBIRD_SOURCE_DIR}/Ladybird/Utilities.cpp
ConnectionFromClient.cpp
DedicatedWorkerHost.cpp
PageHost.cpp
)
# FIXME: Add Android service
add_library(webworkerservice STATIC ${WEBWORKER_SOURCES})
set_target_properties(webworkerservice PROPERTIES AUTOMOC OFF AUTORCC OFF AUTOUIC OFF)
target_include_directories(webworkerservice PRIVATE ${CMAKE_CURRENT_BINARY_DIR}/../..)
target_include_directories(webworkerservice PRIVATE ${LADYBIRD_SOURCE_DIR}/Services/)
target_include_directories(webworkerservice PRIVATE ${LADYBIRD_SOURCE_DIR}/Userland/)
target_link_libraries(webworkerservice PUBLIC LibCore LibFileSystem LibGfx LibIPC LibJS LibRequests LibWeb LibWebView LibUnicode LibImageDecoderClient LibMain LibURL)
if (HAS_FONTCONFIG)
target_link_libraries(webworkerservice PRIVATE Fontconfig::Fontconfig)
endif()
if (ENABLE_QT)
qt_add_executable(WebWorker
${LADYBIRD_SOURCE_DIR}/Ladybird/Qt/EventLoopImplementationQt.cpp
${LADYBIRD_SOURCE_DIR}/Ladybird/Qt/EventLoopImplementationQtEventTarget.cpp
${LADYBIRD_SOURCE_DIR}/Ladybird/Qt/StringUtils.cpp
main.cpp
)
target_link_libraries(WebWorker PRIVATE Qt::Core)
target_link_libraries(WebWorker PRIVATE webworkerservice LibWebSocket)
target_compile_definitions(WebWorker PRIVATE HAVE_QT=1)
else()
add_executable(WebWorker main.cpp)
endif()
target_include_directories(WebWorker PRIVATE ${LADYBIRD_SOURCE_DIR}/Userland/)
target_link_libraries(WebWorker PRIVATE webworkerservice)

View file

@ -0,0 +1,84 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibCore/EventLoop.h>
#include <WebWorker/ConnectionFromClient.h>
#include <WebWorker/DedicatedWorkerHost.h>
#include <WebWorker/PageHost.h>
namespace WebWorker {
void ConnectionFromClient::close_worker()
{
async_did_close_worker();
// FIXME: Invoke a worker shutdown operation that implements the spec
m_worker_host = nullptr;
die();
}
void ConnectionFromClient::die()
{
// FIXME: When handling multiple workers in the same process,
// this logic needs to be smarter (only when all workers are dead, etc).
Core::EventLoop::current().quit(0);
}
void ConnectionFromClient::request_file(Web::FileRequest request)
{
// FIXME: Route this to FSAS or Brower chrome as appropriate instead of allowing
// the WebWorker process filesystem access
auto path = request.path();
auto request_id = ++last_id;
m_requested_files.set(request_id, move(request));
auto file = Core::File::open(path, Core::File::OpenMode::Read);
if (file.is_error())
handle_file_return(file.error().code(), {}, request_id);
else
handle_file_return(0, IPC::File::adopt_file(file.release_value()), request_id);
}
ConnectionFromClient::ConnectionFromClient(IPC::Transport transport)
: IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(transport), 1)
, m_page_host(PageHost::create(Web::Bindings::main_thread_vm(), *this))
{
}
ConnectionFromClient::~ConnectionFromClient() = default;
Web::Page& ConnectionFromClient::page()
{
return m_page_host->page();
}
Web::Page const& ConnectionFromClient::page() const
{
return m_page_host->page();
}
void ConnectionFromClient::start_dedicated_worker(URL::URL const& url, Web::Bindings::WorkerType const& type, Web::Bindings::RequestCredentials const&, String const& name, Web::HTML::TransferDataHolder const& implicit_port, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings)
{
m_worker_host = make_ref_counted<DedicatedWorkerHost>(url, type, name);
// FIXME: Yikes, const_cast to move? Feels like a LibIPC bug.
// We should be able to move non-copyable types from a Message type.
m_worker_host->run(page(), move(const_cast<Web::HTML::TransferDataHolder&>(implicit_port)), outside_settings);
}
void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
{
auto file_request = m_requested_files.take(request_id);
VERIFY(file_request.has_value());
VERIFY(file_request.value().on_file_request_finish);
file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
}
}

View file

@ -0,0 +1,57 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#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 <WebWorker/Forward.h>
#include <WebWorker/PageHost.h>
namespace WebWorker {
class ConnectionFromClient final
: public IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint> {
C_OBJECT(ConnectionFromClient);
public:
virtual ~ConnectionFromClient() override;
virtual void die() override;
virtual void close_worker() override;
void request_file(Web::FileRequest);
PageHost& page_host() { return *m_page_host; }
PageHost const& page_host() const { return *m_page_host; }
private:
explicit ConnectionFromClient(IPC::Transport);
Web::Page& page();
Web::Page const& page() const;
virtual void start_dedicated_worker(URL::URL const& url, Web::Bindings::WorkerType const& type, Web::Bindings::RequestCredentials const& credentials, String const& name, Web::HTML::TransferDataHolder const&, Web::HTML::SerializedEnvironmentSettingsObject const&) override;
virtual void handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id) override;
JS::Handle<PageHost> m_page_host;
// FIXME: Route console messages to the Browser UI using a ConsoleClient
HashMap<int, Web::FileRequest> m_requested_files {};
int last_id { 0 };
RefPtr<DedicatedWorkerHost> m_worker_host;
};
}

View file

@ -0,0 +1,240 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibIPC/File.h>
#include <LibJS/Runtime/ConsoleObject.h>
#include <LibWeb/Fetch/Fetching/Fetching.h>
#include <LibWeb/Fetch/Infrastructure/FetchAlgorithms.h>
#include <LibWeb/HTML/DedicatedWorkerGlobalScope.h>
#include <LibWeb/HTML/MessagePort.h>
#include <LibWeb/HTML/Scripting/ClassicScript.h>
#include <LibWeb/HTML/Scripting/EnvironmentSettingsSnapshot.h>
#include <LibWeb/HTML/Scripting/Fetching.h>
#include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
#include <LibWeb/HTML/WorkerDebugConsoleClient.h>
#include <LibWeb/HTML/WorkerGlobalScope.h>
#include <LibWeb/HighResolutionTime/TimeOrigin.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <WebWorker/DedicatedWorkerHost.h>
namespace WebWorker {
DedicatedWorkerHost::DedicatedWorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name)
: m_url(move(url))
, m_type(type)
, m_name(move(name))
{
}
DedicatedWorkerHost::~DedicatedWorkerHost() = default;
// https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
// FIXME: Extract out into a helper for both shared and dedicated workers
void DedicatedWorkerHost::run(JS::NonnullGCPtr<Web::Page> page, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const& outside_settings_snapshot)
{
bool const is_shared = false;
// 3. Let unsafeWorkerCreationTime be the unsafe shared current time.
auto unsafe_worker_creation_time = Web::HighResolutionTime::unsafe_shared_current_time();
// 7. Let realm execution context be the result of creating a new JavaScript realm given agent and the following customizations:
auto realm_execution_context = Web::Bindings::create_a_new_javascript_realm(
Web::Bindings::main_thread_vm(),
[page](JS::Realm& realm) -> JS::Object* {
// 7a. For the global object, if is shared is true, create a new SharedWorkerGlobalScope object.
// 7b. Otherwise, create a new DedicatedWorkerGlobalScope object.
// FIXME: Proper support for both SharedWorkerGlobalScope and DedicatedWorkerGlobalScope
if (is_shared)
TODO();
return Web::Bindings::main_thread_vm().heap().allocate_without_realm<Web::HTML::DedicatedWorkerGlobalScope>(realm, page);
},
nullptr);
// 8. Let worker global scope be the global object of realm execution context's Realm component.
// NOTE: This is the DedicatedWorkerGlobalScope or SharedWorkerGlobalScope object created in the previous step.
JS::NonnullGCPtr<Web::HTML::WorkerGlobalScope> worker_global_scope = verify_cast<Web::HTML::WorkerGlobalScope>(realm_execution_context->realm->global_object());
// 9. Set up a worker environment settings object with realm execution context,
// outside settings, and unsafeWorkerCreationTime, and let inside settings be the result.
auto inner_settings = Web::HTML::WorkerEnvironmentSettingsObject::setup(page, move(realm_execution_context), outside_settings_snapshot, unsafe_worker_creation_time);
auto& console_object = *inner_settings->realm().intrinsics().console_object();
m_console = console_object.heap().allocate_without_realm<Web::HTML::WorkerDebugConsoleClient>(console_object.console());
VERIFY(m_console);
console_object.console().set_client(*m_console);
// 10. Set worker global scope's name to the value of options's name member.
if (is_shared)
TODO();
else
static_cast<Web::HTML::DedicatedWorkerGlobalScope&>(*worker_global_scope).set_name(m_name);
// 11. Append owner to worker global scope's owner set.
// FIXME: support for 'owner' set on WorkerGlobalScope
// IMPLEMENTATION DEFINED: We need an object to represent the fetch response's client
auto outside_settings = inner_settings->heap().allocate<Web::HTML::EnvironmentSettingsSnapshot>(inner_settings->realm(), inner_settings->realm_execution_context().copy(), outside_settings_snapshot);
// 12. If is shared is true, then:
if (is_shared) {
// FIXME: Shared worker support
// 1. Set worker global scope's constructor origin to outside settings's origin.
// 2. Set worker global scope's constructor url to url.
// 3. Set worker global scope's type to the value of options's type member.
// 4. Set worker global scope's credentials to the value of options's credentials member.
}
// 13. Let destination be "sharedworker" if is shared is true, and "worker" otherwise.
auto destination = is_shared ? Web::Fetch::Infrastructure::Request::Destination::SharedWorker
: Web::Fetch::Infrastructure::Request::Destination::Worker;
// In both cases, let performFetch be the following perform the fetch hook given request, isTopLevel and processCustomFetchResponse:
auto perform_fetch_function = [inner_settings, worker_global_scope](JS::NonnullGCPtr<Web::Fetch::Infrastructure::Request> request, Web::HTML::TopLevelModule is_top_level, Web::Fetch::Infrastructure::FetchAlgorithms::ProcessResponseConsumeBodyFunction process_custom_fetch_response) -> Web::WebIDL::ExceptionOr<void> {
auto& realm = inner_settings->realm();
auto& vm = realm.vm();
Web::Fetch::Infrastructure::FetchAlgorithms::Input fetch_algorithms_input {};
// 1. If isTopLevel is false, fetch request with processResponseConsumeBody set to processCustomFetchResponse, and abort these steps.
if (is_top_level == Web::HTML::TopLevelModule::No) {
fetch_algorithms_input.process_response_consume_body = move(process_custom_fetch_response);
TRY(Web::Fetch::Fetching::fetch(realm, request, Web::Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
return {};
}
// 2. Set request's reserved client to inside settings.
request->set_reserved_client(JS::GCPtr<Web::HTML::EnvironmentSettingsObject>(inner_settings));
// We need to store the process custom fetch response function on the heap here, because we're storing it in another heap function
auto process_custom_fetch_response_function = JS::create_heap_function(vm.heap(), move(process_custom_fetch_response));
// 3. Fetch request with processResponseConsumeBody set to the following steps given response response and null, failure, or a byte sequence bodyBytes:
fetch_algorithms_input.process_response_consume_body = [worker_global_scope, process_custom_fetch_response_function](auto response, auto body_bytes) {
// 1. Set worker global scope's url to response's url.
worker_global_scope->set_url(response->url().value_or({}));
// FIXME: 2. Initialize worker global scope's policy container given worker global scope, response, and inside settings.
// FIXME: 3. If the Run CSP initialization for a global object algorithm returns "Blocked" when executed upon worker
// global scope, set response to a network error. [CSP]
// FIXME: 4. If worker global scope's embedder policy's value is compatible with cross-origin isolation and is shared is true,
// then set agent's agent cluster's cross-origin isolation mode to "logical" or "concrete".
// The one chosen is implementation-defined.
// FIXME: 5. If the result of checking a global object's embedder policy with worker global scope, outside settings,
// and response is false, then set response to a network error.
// FIXME: 6. Set worker global scope's cross-origin isolated capability to true if agent's agent cluster's cross-origin
// isolation mode is "concrete".
if (!is_shared) {
// 7. If is shared is false and owner's cross-origin isolated capability is false, then set worker
// global scope's cross-origin isolated capability to false.
// 8. If is shared is false and response's url's scheme is "data", then set worker global scope's
// cross-origin isolated capability to false.
}
// 9. Run processCustomFetchResponse with response and bodyBytes.
process_custom_fetch_response_function->function()(response, body_bytes);
};
TRY(Web::Fetch::Fetching::fetch(realm, request, Web::Fetch::Infrastructure::FetchAlgorithms::create(vm, move(fetch_algorithms_input))));
return {};
};
auto perform_fetch = Web::HTML::create_perform_the_fetch_hook(inner_settings->heap(), move(perform_fetch_function));
auto on_complete_function = [inner_settings, worker_global_scope, message_port_data = move(message_port_data), url = m_url](JS::GCPtr<Web::HTML::Script> script) mutable {
auto& realm = inner_settings->realm();
// 1. If script is null or if script's error to rethrow is non-null, then:
if (!script || !script->error_to_rethrow().is_null()) {
// FIXME: 1. Queue a global task on the DOM manipulation task source given worker's relevant global object to fire an event named error at worker.
// FIXME: Notify Worker parent through IPC to fire an error event at Worker
// FIXME 2. Run the environment discarding steps for inside settings.
// 3. Abort these steps.
dbgln("DedicatedWorkerHost: Unable to fetch script {} because {}", url, script ? script->error_to_rethrow().to_string_without_side_effects() : "script was null"_string);
return;
}
// FIXME: 2. Associate worker with worker global scope.
// What does this even mean?
// 3. Let inside port be a new MessagePort object in inside settings's Realm.
auto inside_port = Web::HTML::MessagePort::create(realm);
// 4. Associate inside port with worker global scope.
worker_global_scope->set_internal_port(inside_port);
// 5. Entangle outside port and inside port.
MUST(inside_port->transfer_receiving_steps(message_port_data));
// 6. Create a new WorkerLocation object and associate it with worker global scope.
worker_global_scope->set_location(realm.heap().allocate<Web::HTML::WorkerLocation>(realm, *worker_global_scope));
// FIXME: 7. Closing orphan workers: Start monitoring the worker such that no sooner than it
// stops being a protected worker, and no later than it stops being a permissible worker,
// worker global scope's closing flag is set to true.
// FIXME: 8. Suspending workers: Start monitoring the worker, such that whenever worker global scope's
// closing flag is false and the worker is a suspendable worker, the user agent suspends
// execution of script in that worker until such time as either the closing flag switches to
// true or the worker stops being a suspendable worker
// 9. Set inside settings's execution ready flag.
inner_settings->execution_ready = true;
// 10. If script is a classic script, then run the classic script script.
// Otherwise, it is a module script; run the module script script.
if (is<Web::HTML::ClassicScript>(*script))
(void)static_cast<Web::HTML::ClassicScript&>(*script).run();
else
(void)verify_cast<Web::HTML::JavaScriptModuleScript>(*script).run();
// FIXME: 11. Enable outside port's port message queue.
// 12. If is shared is false, enable the port message queue of the worker's implicit port.
if (!is_shared) {
inside_port->start();
}
// FIXME: 13. If is shared is true, then queue a global task on DOM manipulation task source given worker
// global scope to fire an event named connect at worker global scope, using MessageEvent,
// with the data attribute initialized to the empty string, the ports attribute initialized
// to a new frozen array containing inside port, and the source attribute initialized to inside port.
// FIXME: 14. Enable the client message queue of the ServiceWorkerContainer object whose associated service
// worker client is worker global scope's relevant settings object.
// 15. Event loop: Run the responsible event loop specified by inside settings until it is destroyed.
inner_settings->responsible_event_loop().schedule();
// FIXME: We need to react to the closing flag being set on the responsible event loop
// And use that to shutdown the WorkerHost
// FIXME: 16. Clear the worker global scope's map of active timers.
// FIXME: 17. Disentangle all the ports in the list of the worker's ports.
// FIXME: 18. Empty worker global scope's owner set.
};
auto on_complete = Web::HTML::create_on_fetch_script_complete(inner_settings->vm().heap(), move(on_complete_function));
// 14. Obtain script by switching on the value of options's type member:
// classic: Fetch a classic worker script given url, outside settings, destination, inside settings,
// and with onComplete and performFetch as defined below.
// module: Fetch a module worker script graph given url, outside settings, destination, the value of the credentials member of options, inside settings,
// and with onComplete and performFetch as defined below.
if (m_type == Web::Bindings::WorkerType::Classic) {
if (auto err = Web::HTML::fetch_classic_worker_script(m_url, outside_settings, destination, inner_settings, perform_fetch, on_complete); err.is_error()) {
dbgln("Failed to run worker script");
// FIXME: Abort the worker properly
TODO();
}
} else {
VERIFY(m_type == Web::Bindings::WorkerType::Module);
// FIXME: Pass credentials
if (auto err = Web::HTML::fetch_module_worker_script_graph(m_url, outside_settings, destination, inner_settings, perform_fetch, on_complete); err.is_error()) {
dbgln("Failed to run worker script");
// FIXME: Abort the worker properly
TODO();
}
}
}
}

View file

@ -0,0 +1,34 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/RefCounted.h>
#include <LibURL/URL.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Bindings/WorkerPrototype.h>
#include <LibWeb/Forward.h>
#include <LibWeb/HTML/Scripting/SerializedEnvironmentSettingsObject.h>
#include <LibWeb/HTML/StructuredSerialize.h>
namespace WebWorker {
class DedicatedWorkerHost : public RefCounted<DedicatedWorkerHost> {
public:
explicit DedicatedWorkerHost(URL::URL url, Web::Bindings::WorkerType type, String name);
~DedicatedWorkerHost();
void run(JS::NonnullGCPtr<Web::Page>, Web::HTML::TransferDataHolder message_port_data, Web::HTML::SerializedEnvironmentSettingsObject const&);
private:
JS::Handle<Web::HTML::WorkerDebugConsoleClient> m_console;
URL::URL m_url;
Web::Bindings::WorkerType m_type;
String m_name;
};
}

View file

@ -0,0 +1,15 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
namespace WebWorker {
class ConnectionFromClient;
class DedicatedWorkerHost;
class PageHost;
}

View file

@ -0,0 +1,102 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Runtime/VM.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <WebWorker/ConnectionFromClient.h>
#include <WebWorker/PageHost.h>
namespace WebWorker {
JS_DEFINE_ALLOCATOR(PageHost);
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()
{
return *m_page;
}
Web::Page const& PageHost::page() const
{
return *m_page;
}
Gfx::Palette PageHost::palette() const
{
return Gfx::Palette(*m_palette_impl);
}
void PageHost::setup_palette()
{
// FIXME: We don't actually need a palette :thonk:
auto buffer_or_error = Core::AnonymousBuffer::create_with_size(sizeof(Gfx::SystemTheme));
VERIFY(!buffer_or_error.is_error());
auto buffer = buffer_or_error.release_value();
auto* theme = buffer.data<Gfx::SystemTheme>();
theme->color[to_underlying(Gfx::ColorRole::Window)] = Color(Color::Magenta).value();
theme->color[to_underlying(Gfx::ColorRole::WindowText)] = Color(Color::Cyan).value();
m_palette_impl = Gfx::PaletteImpl::create_with_anonymous_buffer(buffer);
}
bool PageHost::is_connection_open() const
{
return m_client.is_open();
}
Web::DevicePixelRect PageHost::screen_rect() const
{
return {};
}
double PageHost::device_pixels_per_css_pixel() const
{
return 1.0;
}
Web::CSS::PreferredColorScheme PageHost::preferred_color_scheme() const
{
return Web::CSS::PreferredColorScheme::Auto;
}
Web::CSS::PreferredContrast PageHost::preferred_contrast() const
{
return Web::CSS::PreferredContrast::Auto;
}
Web::CSS::PreferredMotion PageHost::preferred_motion() const
{
return Web::CSS::PreferredMotion::Auto;
}
void PageHost::paint(Web::DevicePixelRect const&, Web::Painting::BackingStore&, Web::PaintOptions)
{
}
void PageHost::request_file(Web::FileRequest request)
{
m_client.request_file(move(request));
}
PageHost::PageHost(ConnectionFromClient& client)
: m_client(client)
, m_page(Web::Page::create(Web::Bindings::main_thread_vm(), *this))
{
setup_palette();
}
void PageHost::visit_edges(JS::Cell::Visitor& visitor)
{
Base::visit_edges(visitor);
visitor.visit(m_page);
}
}

View file

@ -0,0 +1,52 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibGfx/Rect.h>
#include <LibWeb/Page/Page.h>
#include <LibWeb/PixelUnits.h>
#include <WebWorker/Forward.h>
namespace WebWorker {
class PageHost final : public Web::PageClient {
JS_CELL(PageHost, Web::PageClient);
JS_DECLARE_ALLOCATOR(PageHost);
public:
static JS::NonnullGCPtr<PageHost> create(JS::VM& vm, ConnectionFromClient& client);
virtual ~PageHost();
virtual Web::Page& page() override;
virtual Web::Page const& page() const override;
virtual bool is_connection_open() const override;
virtual Gfx::Palette palette() const override;
virtual Web::DevicePixelRect screen_rect() const override;
virtual double device_pixels_per_css_pixel() const override;
virtual Web::CSS::PreferredColorScheme preferred_color_scheme() const override;
virtual Web::CSS::PreferredContrast preferred_contrast() const override;
virtual Web::CSS::PreferredMotion preferred_motion() const override;
virtual void paint_next_frame() override {};
virtual void process_screenshot_requests() override {};
virtual void paint(Web::DevicePixelRect const&, Web::Painting::BackingStore&, Web::PaintOptions = {}) override;
virtual void request_file(Web::FileRequest) override;
virtual bool is_ready_to_paint() const override { return true; }
virtual Web::DisplayListPlayerType display_list_player_type() const override { VERIFY_NOT_REACHED(); }
private:
explicit PageHost(ConnectionFromClient&);
virtual void visit_edges(JS::Cell::Visitor&) override;
void setup_palette();
ConnectionFromClient& m_client;
JS::NonnullGCPtr<Web::Page> m_page;
RefPtr<Gfx::PaletteImpl> m_palette_impl;
};
}

View file

@ -0,0 +1,85 @@
/*
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <Ladybird/FontPlugin.h>
#include <Ladybird/HelperProcess.h>
#include <Ladybird/Utilities.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/EventLoop.h>
#include <LibCore/LocalServer.h>
#include <LibCore/Process.h>
#include <LibCore/StandardPaths.h>
#include <LibCore/System.h>
#include <LibFileSystem/FileSystem.h>
#include <LibIPC/SingleServer.h>
#include <LibMain/Main.h>
#include <LibWeb/Bindings/MainThreadVM.h>
#include <LibWeb/Loader/GeneratedPagesLoader.h>
#include <LibWeb/Loader/ResourceLoader.h>
#include <LibWeb/Platform/EventLoopPlugin.h>
#include <LibWeb/Platform/EventLoopPluginSerenity.h>
#include <LibWeb/WebSockets/WebSocket.h>
#include <WebWorker/ConnectionFromClient.h>
#if defined(HAVE_QT)
# include <Ladybird/Qt/EventLoopImplementationQt.h>
# include <QCoreApplication>
#endif
static ErrorOr<void> initialize_resource_loader(JS::Heap&, int request_server_socket);
ErrorOr<int> serenity_main(Main::Arguments arguments)
{
AK::set_rich_debug_enabled(true);
int request_server_socket { -1 };
StringView serenity_resource_root;
Vector<ByteString> certificates;
bool wait_for_debugger = false;
Core::ArgsParser args_parser;
args_parser.add_option(request_server_socket, "File descriptor of the request server socket", "request-server-socket", 's', "request-server-socket");
args_parser.add_option(serenity_resource_root, "Absolute path to directory for serenity resources", "serenity-resource-root", 'r', "serenity-resource-root");
args_parser.add_option(certificates, "Path to a certificate file", "certificate", 'C', "certificate");
args_parser.add_option(wait_for_debugger, "Wait for debugger", "wait-for-debugger");
args_parser.parse(arguments);
if (wait_for_debugger)
Core::Process::wait_for_debugger_and_break();
#if defined(HAVE_QT)
QCoreApplication app(arguments.argc, arguments.argv);
Core::EventLoopManager::install(*new Ladybird::EventLoopManagerQt);
#endif
Core::EventLoop event_loop;
platform_init();
Web::Platform::EventLoopPlugin::install(*new Web::Platform::EventLoopPluginSerenity);
Web::Platform::FontPlugin::install(*new Ladybird::FontPlugin(false));
TRY(Web::Bindings::initialize_main_thread_vm(Web::HTML::EventLoop::Type::Worker));
TRY(initialize_resource_loader(Web::Bindings::main_thread_vm().heap(), request_server_socket));
auto client = TRY(IPC::take_over_accepted_client_from_system_server<WebWorker::ConnectionFromClient>());
return event_loop.exec();
}
static ErrorOr<void> initialize_resource_loader(JS::Heap& heap, int request_server_socket)
{
static_assert(IsSame<IPC::Transport, IPC::TransportSocket>, "Need to handle other IPC transports here");
auto socket = TRY(Core::LocalSocket::adopt_fd(request_server_socket));
TRY(socket->set_blocking(true));
auto request_client = TRY(try_make_ref_counted<Requests::RequestClient>(IPC::Transport(move(socket))));
Web::ResourceLoader::initialize(heap, move(request_client));
return {};
}