ConnectionFromClient.cpp 2.5 KB

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