Worker.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #pragma once
  7. #include <AK/RefCounted.h>
  8. #include <AK/URLParser.h>
  9. #include <LibWeb/Bindings/MainThreadVM.h>
  10. #include <LibWeb/Forward.h>
  11. #include <LibWeb/HTML/MessageEvent.h>
  12. #include <LibWeb/HTML/MessagePort.h>
  13. #include <LibWeb/HTML/Scripting/ClassicScript.h>
  14. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  15. #include <LibWeb/HTML/Scripting/WorkerEnvironmentSettingsObject.h>
  16. #include <LibWeb/HTML/WorkerDebugConsoleClient.h>
  17. #include <LibWeb/Loader/ResourceLoader.h>
  18. #define ENUMERATE_WORKER_EVENT_HANDLERS(E) \
  19. E(onmessage, HTML::EventNames::message) \
  20. E(onmessageerror, HTML::EventNames::messageerror)
  21. namespace Web::HTML {
  22. struct WorkerOptions {
  23. String type { "classic"_string };
  24. String credentials { "same-origin"_string };
  25. String name { String {} };
  26. };
  27. // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
  28. class Worker : public DOM::EventTarget {
  29. WEB_PLATFORM_OBJECT(Worker, DOM::EventTarget);
  30. public:
  31. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> create(String const& script_url, WorkerOptions const options, DOM::Document& document);
  32. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> construct_impl(JS::Realm& realm, String const& script_url, WorkerOptions const options)
  33. {
  34. auto& window = verify_cast<HTML::Window>(realm.global_object());
  35. return Worker::create(script_url, options, window.associated_document());
  36. }
  37. WebIDL::ExceptionOr<void> terminate();
  38. void post_message(JS::Value message, JS::Value transfer);
  39. virtual ~Worker() = default;
  40. MessagePort* implicit_message_port() { return m_implicit_port.ptr(); }
  41. JS::GCPtr<MessagePort> outside_message_port() { return m_outside_port; }
  42. #undef __ENUMERATE
  43. #define __ENUMERATE(attribute_name, event_name) \
  44. void set_##attribute_name(WebIDL::CallbackType*); \
  45. WebIDL::CallbackType* attribute_name();
  46. ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE)
  47. #undef __ENUMERATE
  48. protected:
  49. Worker(String const&, const WorkerOptions, DOM::Document&);
  50. private:
  51. static HTML::EventLoop& get_vm_event_loop(JS::VM& target_vm)
  52. {
  53. return static_cast<Bindings::WebEngineCustomData*>(target_vm.custom_data())->event_loop;
  54. }
  55. virtual void initialize(JS::Realm&) override;
  56. virtual void visit_edges(Cell::Visitor&) override;
  57. String m_script_url;
  58. WorkerOptions m_options;
  59. JS::GCPtr<DOM::Document> m_document;
  60. Bindings::WebEngineCustomData m_custom_data;
  61. NonnullRefPtr<JS::VM> m_worker_vm;
  62. JS::GCPtr<WorkerEnvironmentSettingsObject> m_inner_settings;
  63. RefPtr<WorkerDebugConsoleClient> m_console;
  64. JS::NonnullGCPtr<MessagePort> m_implicit_port;
  65. JS::GCPtr<MessagePort> m_outside_port;
  66. // NOTE: These are inside the worker VM.
  67. JS::GCPtr<JS::Realm> m_worker_realm;
  68. JS::GCPtr<JS::Object> m_worker_scope;
  69. void run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_settings, MessagePort& outside_port, WorkerOptions const& options);
  70. };
  71. }