Worker.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2022, Ben Abraham <ben.d.abraham@gmail.com>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Debug.h>
  7. #include <LibJS/Runtime/Realm.h>
  8. #include <LibWeb/Bindings/WorkerPrototype.h>
  9. #include <LibWeb/HTML/MessagePort.h>
  10. #include <LibWeb/HTML/Scripting/Environments.h>
  11. #include <LibWeb/HTML/Scripting/WindowEnvironmentSettingsObject.h>
  12. #include <LibWeb/HTML/Worker.h>
  13. namespace Web::HTML {
  14. GC_DEFINE_ALLOCATOR(Worker);
  15. // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
  16. Worker::Worker(String const& script_url, WorkerOptions const& options, DOM::Document& document)
  17. : DOM::EventTarget(document.realm())
  18. , m_script_url(script_url)
  19. , m_options(options)
  20. , m_document(&document)
  21. {
  22. }
  23. void Worker::initialize(JS::Realm& realm)
  24. {
  25. Base::initialize(realm);
  26. WEB_SET_PROTOTYPE_FOR_INTERFACE(Worker);
  27. }
  28. void Worker::visit_edges(Cell::Visitor& visitor)
  29. {
  30. Base::visit_edges(visitor);
  31. visitor.visit(m_document);
  32. visitor.visit(m_outside_port);
  33. visitor.visit(m_agent);
  34. }
  35. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker
  36. // https://whatpr.org/html/9893/workers.html#dom-worker
  37. WebIDL::ExceptionOr<GC::Ref<Worker>> Worker::create(String const& script_url, WorkerOptions const& options, DOM::Document& document)
  38. {
  39. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Creating worker with script_url = {}", script_url);
  40. // Returns a new Worker object. scriptURL will be fetched and executed in the background,
  41. // creating a new global environment for which worker represents the communication channel.
  42. // options can be used to define the name of that global environment via the name option,
  43. // primarily for debugging purposes. It can also ensure this new global environment supports
  44. // JavaScript modules (specify type: "module"), and if that is specified, can also be used
  45. // to specify how scriptURL is fetched through the credentials option.
  46. // FIXME: 1. The user agent may throw a "SecurityError" DOMException if the request violates
  47. // a policy decision (e.g. if the user agent is configured to not allow the page to start dedicated workers).
  48. // Technically not a fixme if our policy is not to throw errors :^)
  49. // 2. Let outside settings be the current principal settings object.
  50. auto& outside_settings = current_principal_settings_object();
  51. // 3. Parse the scriptURL argument relative to outside settings.
  52. auto url = outside_settings.parse_url(script_url);
  53. // 4. If this fails, throw a "SyntaxError" DOMException.
  54. if (!url.is_valid()) {
  55. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", script_url);
  56. return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_string);
  57. }
  58. // 5. Let worker URL be the resulting URL record.
  59. // 6. Let worker be a new Worker object.
  60. auto worker = document.realm().create<Worker>(script_url, options, document);
  61. // 7. Let outside port be a new MessagePort in outside settings's Realm.
  62. auto outside_port = MessagePort::create(outside_settings.realm());
  63. // 8. Associate the outside port with worker
  64. worker->m_outside_port = outside_port;
  65. worker->m_outside_port->set_worker_event_target(worker);
  66. // 9. Run this step in parallel:
  67. // 1. Run a worker given worker, worker URL, outside settings, outside port, and options.
  68. worker->run_a_worker(url, outside_settings, *outside_port, options);
  69. // 10. Return worker
  70. return worker;
  71. }
  72. // https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
  73. void Worker::run_a_worker(URL::URL& url, EnvironmentSettingsObject& outside_settings, GC::Ptr<MessagePort> port, WorkerOptions const& options)
  74. {
  75. // 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
  76. // FIXME: SharedWorker support
  77. // 2. Let owner be the relevant owner to add given outside settings.
  78. // FIXME: Support WorkerGlobalScope options
  79. if (!is<HTML::WindowEnvironmentSettingsObject>(outside_settings))
  80. TODO();
  81. // 3. Let parent worker global scope be null.
  82. // 4. If owner is a WorkerGlobalScope object (i.e., we are creating a nested dedicated worker),
  83. // then set parent worker global scope to owner.
  84. // FIXME: Support for nested workers.
  85. // 5. Let unsafeWorkerCreationTime be the unsafe shared current time.
  86. // 6. Let agent be the result of obtaining a dedicated/shared worker agent given outside settings
  87. // and is shared. Run the rest of these steps in that agent.
  88. // Note: This spawns a new process to act as the 'agent' for the worker.
  89. m_agent = outside_settings.realm().create<WorkerAgent>(url, options, port, outside_settings);
  90. }
  91. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate
  92. WebIDL::ExceptionOr<void> Worker::terminate()
  93. {
  94. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Terminate");
  95. return {};
  96. }
  97. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
  98. WebIDL::ExceptionOr<void> Worker::post_message(JS::Value message, StructuredSerializeOptions const& options)
  99. {
  100. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Post Message: {}", message.to_string_without_side_effects());
  101. // The postMessage(message, transfer) and postMessage(message, options) methods on Worker objects act as if,
  102. // when invoked, they immediately invoked the respective postMessage(message, transfer) and
  103. // postMessage(message, options) on the port, with the same arguments, and returned the same return value.
  104. return m_outside_port->post_message(message, options);
  105. }
  106. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
  107. WebIDL::ExceptionOr<void> Worker::post_message(JS::Value message, Vector<GC::Root<JS::Object>> const& transfer)
  108. {
  109. // The postMessage(message, transfer) and postMessage(message, options) methods on Worker objects act as if,
  110. // when invoked, they immediately invoked the respective postMessage(message, transfer) and
  111. // postMessage(message, options) on the port, with the same arguments, and returned the same return value.
  112. return m_outside_port->post_message(message, transfer);
  113. }
  114. #undef __ENUMERATE
  115. #define __ENUMERATE(attribute_name, event_name) \
  116. void Worker::set_##attribute_name(WebIDL::CallbackType* value) \
  117. { \
  118. set_event_handler_attribute(event_name, move(value)); \
  119. } \
  120. WebIDL::CallbackType* Worker::attribute_name() \
  121. { \
  122. return event_handler_attribute(event_name); \
  123. }
  124. ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE)
  125. #undef __ENUMERATE
  126. } // namespace Web::HTML