ConnectionFromClient.cpp 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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: Do something here (shutdown process/script gracefully?)
  13. }
  14. void ConnectionFromClient::request_file(Web::FileRequest request)
  15. {
  16. // FIXME: Route this to FSAS or Brower chrome as appropriate instead of allowing
  17. // the WebWorker process filesystem access
  18. auto path = request.path();
  19. auto request_id = ++last_id;
  20. m_requested_files.set(request_id, move(request));
  21. auto file = Core::File::open(path, Core::File::OpenMode::Read);
  22. if (file.is_error())
  23. handle_file_return(file.error().code(), {}, request_id);
  24. else
  25. handle_file_return(0, IPC::File(*file.value()), request_id);
  26. }
  27. ConnectionFromClient::ConnectionFromClient(NonnullOwnPtr<Core::LocalSocket> socket)
  28. : IPC::ConnectionFromClient<WebWorkerClientEndpoint, WebWorkerServerEndpoint>(*this, move(socket), 1)
  29. , m_page_host(PageHost::create(*this))
  30. {
  31. }
  32. ConnectionFromClient::~ConnectionFromClient() = default;
  33. Web::Page& ConnectionFromClient::page()
  34. {
  35. return m_page_host->page();
  36. }
  37. Web::Page const& ConnectionFromClient::page() const
  38. {
  39. return m_page_host->page();
  40. }
  41. void ConnectionFromClient::start_dedicated_worker(AK::URL const& url, String const& type, String const&, String const&, IPC::File const&)
  42. {
  43. m_worker_host = make_ref_counted<DedicatedWorkerHost>(page(), url, type);
  44. m_worker_host->run();
  45. }
  46. void ConnectionFromClient::handle_file_return(i32 error, Optional<IPC::File> const& file, i32 request_id)
  47. {
  48. auto file_request = m_requested_files.take(request_id);
  49. VERIFY(file_request.has_value());
  50. VERIFY(file_request.value().on_file_request_finish);
  51. file_request.value().on_file_request_finish(error != 0 ? Error::from_errno(error) : ErrorOr<i32> { file->take_fd() });
  52. }
  53. }