ConnectionFromClient.cpp 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * Copyright (c) 2023, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <WebWorker/ConnectionFromClient.h>
  7. #include <WebWorker/DedicatedWorkerHost.h>
  8. #include <WebWorker/PageHost.h>
  9. namespace WebWorker {
  10. void ConnectionFromClient::die()
  11. {
  12. // FIXME: When handling multiple workers in the same process,
  13. // this logic needs to be smarter (only when all workers are dead, etc).
  14. Core::EventLoop::current().quit(0);
  15. }
  16. void ConnectionFromClient::request_file(Web::FileRequest request)
  17. {
  18. // FIXME: Route this to FSAS or Brower chrome as appropriate instead of allowing
  19. // the WebWorker process filesystem access
  20. auto path = request.path();
  21. auto request_id = ++last_id;
  22. m_requested_files.set(request_id, move(request));
  23. auto file = Core::File::open(path, Core::File::OpenMode::Read);
  24. if (file.is_error())
  25. handle_file_return(file.error().code(), {}, request_id);
  26. else
  27. handle_file_return(0, IPC::File(*file.value()), request_id);
  28. }
  29. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  30. : IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(socket), 1)
  31. , m_page_host(PageHost::create(Web::Bindings::main_thread_vm(), *this))
  32. {
  33. }
  34. ConnectionFromClient::~ConnectionFromClient() = default;
  35. Web::Page& ConnectionFromClient::page()
  36. {
  37. return m_page_host->page();
  38. }
  39. Web::Page const& ConnectionFromClient::page() const
  40. {
  41. return m_page_host->page();
  42. }
  43. void ConnectionFromClient::start_dedicated_worker(AK::URL const& url, String const& type, String const&, String const&, Web::HTML::TransferDataHolder const& implicit_port)
  44. {
  45. m_worker_host = make_ref_counted<DedicatedWorkerHost>(url, type);
  46. // FIXME: Yikes, const_cast to move? Feels like a LibIPC bug.
  47. // We should be able to move non-copyable types from a Message type.
  48. m_worker_host->run(page(), move(const_cast<Web::HTML::TransferDataHolder&>(implicit_port)));
  49. }
  50. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  51. {
  52. auto file_request = m_requested_files.take(request_id);
  53. VERIFY(file_request.has_value());
  54. VERIFY(file_request.value().on_file_request_finish);
  55. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  56. }
  57. }