2023-11-08 18:47:41 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
2024-06-28 12:24:49 +00:00
|
|
|
#include <LibCore/EventLoop.h>
|
2023-11-08 18:47:41 +00:00
|
|
|
#include <WebWorker/ConnectionFromClient.h>
|
|
|
|
#include <WebWorker/DedicatedWorkerHost.h>
|
|
|
|
#include <WebWorker/PageHost.h>
|
|
|
|
|
|
|
|
namespace WebWorker {
|
|
|
|
|
2024-07-09 09:00:06 +00:00
|
|
|
void ConnectionFromClient::close_worker()
|
|
|
|
{
|
|
|
|
async_did_close_worker();
|
|
|
|
|
|
|
|
// FIXME: Invoke a worker shutdown operation that implements the spec
|
|
|
|
m_worker_host = nullptr;
|
|
|
|
|
|
|
|
die();
|
|
|
|
}
|
|
|
|
|
2023-11-08 18:47:41 +00:00
|
|
|
void ConnectionFromClient::die()
|
|
|
|
{
|
2023-11-22 16:49:10 +00:00
|
|
|
// 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);
|
2023-11-08 18:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2024-04-16 19:46:30 +00:00
|
|
|
handle_file_return(0, IPC::File::adopt_file(file.release_value()), request_id);
|
2023-11-08 18:47:41 +00:00
|
|
|
}
|
|
|
|
|
2024-10-22 21:47:33 +00:00
|
|
|
ConnectionFromClient::ConnectionFromClient(IPC::Transport transport)
|
|
|
|
: IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(transport), 1)
|
2023-12-04 08:40:33 +00:00
|
|
|
, m_page_host(PageHost::create(Web::Bindings::main_thread_vm(), *this))
|
2023-11-08 18:47:41 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
ConnectionFromClient::~ConnectionFromClient() = default;
|
|
|
|
|
|
|
|
Web::Page& ConnectionFromClient::page()
|
|
|
|
{
|
|
|
|
return m_page_host->page();
|
|
|
|
}
|
|
|
|
|
|
|
|
Web::Page const& ConnectionFromClient::page() const
|
|
|
|
{
|
|
|
|
return m_page_host->page();
|
|
|
|
}
|
|
|
|
|
2024-09-11 02:57:48 +00:00
|
|
|
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)
|
2023-11-08 18:47:41 +00:00
|
|
|
{
|
2024-07-09 21:54:22 +00:00
|
|
|
m_worker_host = make_ref_counted<DedicatedWorkerHost>(url, type, name);
|
2023-12-20 20:47:01 +00:00
|
|
|
// FIXME: Yikes, const_cast to move? Feels like a LibIPC bug.
|
|
|
|
// We should be able to move non-copyable types from a Message type.
|
2024-03-05 16:42:10 +00:00
|
|
|
m_worker_host->run(page(), move(const_cast<Web::HTML::TransferDataHolder&>(implicit_port)), outside_settings);
|
2023-11-08 18:47:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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() });
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|