ConnectionFromClient.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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(*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&, IPC::File const& implicit_port)
  44. {
  45. m_worker_host = make_ref_counted<DedicatedWorkerHost>(page(), url, type, implicit_port.take_fd());
  46. m_worker_host->run();
  47. }
  48. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  49. {
  50. auto file_request = m_requested_files.take(request_id);
  51. VERIFY(file_request.has_value());
  52. VERIFY(file_request.value().on_file_request_finish);
  53. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  54. }
  55. }