ConnectionFromClient.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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::close_worker()
  12. {
  13. async_did_close_worker();
  14. // FIXME: Invoke a worker shutdown operation that implements the spec
  15. m_worker_host = nullptr;
  16. die();
  17. }
  18. void ConnectionFromClient::die()
  19. {
  20. // FIXME: When handling multiple workers in the same process,
  21. // this logic needs to be smarter (only when all workers are dead, etc).
  22. Core::EventLoop::current().quit(0);
  23. }
  24. void ConnectionFromClient::request_file(Web::FileRequest request)
  25. {
  26. // FIXME: Route this to FSAS or Brower chrome as appropriate instead of allowing
  27. // the WebWorker process filesystem access
  28. auto path = request.path();
  29. auto request_id = ++last_id;
  30. m_requested_files.set(request_id, move(request));
  31. auto file = Core::File::open(path, Core::File::OpenMode::Read);
  32. if (file.is_error())
  33. handle_file_return(file.error().code(), {}, request_id);
  34. else
  35. handle_file_return(0, IPC::File::adopt_file(file.release_value()), request_id);
  36. }
  37. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  38. : IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(socket), 1)
  39. , m_page_host(PageHost::create(Web::Bindings::main_thread_vm(), *this))
  40. {
  41. }
  42. ConnectionFromClient::~ConnectionFromClient() = default;
  43. Web::Page& ConnectionFromClient::page()
  44. {
  45. return m_page_host->page();
  46. }
  47. Web::Page const& ConnectionFromClient::page() const
  48. {
  49. return m_page_host->page();
  50. }
  51. 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)
  52. {
  53. m_worker_host = make_ref_counted<DedicatedWorkerHost>(url, type);
  54. // FIXME: Yikes, const_cast to move? Feels like a LibIPC bug.
  55. // We should be able to move non-copyable types from a Message type.
  56. m_worker_host->run(page(), move(const_cast<Web::HTML::TransferDataHolder&>(implicit_port)), outside_settings);
  57. }
  58. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  59. {
  60. auto file_request = m_requested_files.take(request_id);
  61. VERIFY(file_request.has_value());
  62. VERIFY(file_request.value().on_file_request_finish);
  63. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  64. }
  65. }