Worker.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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" };
  25. String credentials { "same-origin" };
  26. String name { "" };
  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(FlyString const& script_url, WorkerOptions const options, DOM::Document& document);
  33. static WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> construct_impl(JS::Realm& realm, FlyString 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(FlyString 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 void visit_edges(Cell::Visitor&) override;
  57. FlyString 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. NonnullOwnPtr<JS::Interpreter> m_interpreter;
  63. JS::GCPtr<WorkerEnvironmentSettingsObject> m_inner_settings;
  64. JS::VM::InterpreterExecutionScope m_interpreter_scope;
  65. RefPtr<WorkerDebugConsoleClient> m_console;
  66. JS::NonnullGCPtr<MessagePort> m_implicit_port;
  67. JS::GCPtr<MessagePort> m_outside_port;
  68. // NOTE: These are inside the worker VM.
  69. JS::GCPtr<JS::Realm> m_worker_realm;
  70. JS::GCPtr<JS::Object> m_worker_scope;
  71. // FIXME: This is a mega-hack but necessary because HTML::Window holds all the prototypes and constructors.
  72. // There should be *no* Window object in a Worker context.
  73. JS::GCPtr<HTML::Window> m_worker_window;
  74. void run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_settings, MessagePort& outside_port, WorkerOptions const& options);
  75. };
  76. }