Worker.h 3.3 KB

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