Worker.h 3.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  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>> create_with_global_object(HTML::Window& window, FlyString const& script_url, WorkerOptions const options)
  34. {
  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(FlyString 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 visit_edges(Cell::Visitor&) override;
  56. FlyString m_script_url;
  57. WorkerOptions m_options;
  58. JS::GCPtr<DOM::Document> m_document;
  59. Bindings::WebEngineCustomData m_custom_data;
  60. NonnullRefPtr<JS::VM> m_worker_vm;
  61. NonnullOwnPtr<JS::Interpreter> m_interpreter;
  62. WeakPtr<WorkerEnvironmentSettingsObject> m_inner_settings;
  63. JS::VM::InterpreterExecutionScope m_interpreter_scope;
  64. RefPtr<WorkerDebugConsoleClient> m_console;
  65. JS::NonnullGCPtr<MessagePort> m_implicit_port;
  66. JS::GCPtr<MessagePort> m_outside_port;
  67. // NOTE: These are inside the worker VM.
  68. JS::GCPtr<JS::Realm> m_worker_realm;
  69. JS::GCPtr<JS::Object> m_worker_scope;
  70. // FIXME: This is a mega-hack but necessary because HTML::Window holds all the prototypes and constructors.
  71. // There should be *no* Window object in a Worker context.
  72. JS::GCPtr<HTML::Window> m_worker_window;
  73. void run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_settings, MessagePort& outside_port, WorkerOptions const options);
  74. };
  75. }