Worker.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. JS_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. WebIDL::ExceptionOr<JS::NonnullGCPtr<Worker>> Worker::create(String const& script_url, WorkerOptions const& options, DOM::Document& document)
  37. {
  38. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Creating worker with script_url = {}", script_url);
  39. // Returns a new Worker object. scriptURL will be fetched and executed in the background,
  40. // creating a new global environment for which worker represents the communication channel.
  41. // options can be used to define the name of that global environment via the name option,
  42. // primarily for debugging purposes. It can also ensure this new global environment supports
  43. // JavaScript modules (specify type: "module"), and if that is specified, can also be used
  44. // to specify how scriptURL is fetched through the credentials option.
  45. // FIXME: 1. The user agent may throw a "SecurityError" DOMException if the request violates
  46. // a policy decision (e.g. if the user agent is configured to not allow the page to start dedicated workers).
  47. // Technically not a fixme if our policy is not to throw errors :^)
  48. // 2. Let outside settings be the current settings object.
  49. auto& outside_settings = current_settings_object();
  50. // 3. Parse the scriptURL argument relative to outside settings.
  51. auto url = document.parse_url(script_url);
  52. // 4. If this fails, throw a "SyntaxError" DOMException.
  53. if (!url.is_valid()) {
  54. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Invalid URL loaded '{}'.", script_url);
  55. return WebIDL::SyntaxError::create(document.realm(), "url is not valid"_string);
  56. }
  57. // 5. Let worker URL be the resulting URL record.
  58. // 6. Let worker be a new Worker object.
  59. auto worker = document.heap().allocate<Worker>(document.realm(), script_url, options, document);
  60. // 7. Let outside port be a new MessagePort in outside settings's Realm.
  61. auto outside_port = MessagePort::create(outside_settings.realm());
  62. // 8. Associate the outside port with worker
  63. worker->m_outside_port = outside_port;
  64. worker->m_outside_port->set_worker_event_target(worker);
  65. // 9. Run this step in parallel:
  66. // 1. Run a worker given worker, worker URL, outside settings, outside port, and options.
  67. worker->run_a_worker(url, outside_settings, *outside_port, options);
  68. // 10. Return worker
  69. return worker;
  70. }
  71. // https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
  72. void Worker::run_a_worker(URL::URL& url, EnvironmentSettingsObject& outside_settings, JS::GCPtr<MessagePort> port, WorkerOptions const& options)
  73. {
  74. // 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
  75. // FIXME: SharedWorker support
  76. // 2. Let owner be the relevant owner to add given outside settings.
  77. // FIXME: Support WorkerGlobalScope options
  78. if (!is<HTML::WindowEnvironmentSettingsObject>(outside_settings))
  79. TODO();
  80. // 3. Let parent worker global scope be null.
  81. // 4. If owner is a WorkerGlobalScope object (i.e., we are creating a nested dedicated worker),
  82. // then set parent worker global scope to owner.
  83. // FIXME: Support for nested workers.
  84. // 5. Let unsafeWorkerCreationTime be the unsafe shared current time.
  85. // 6. Let agent be the result of obtaining a dedicated/shared worker agent given outside settings
  86. // and is shared. Run the rest of these steps in that agent.
  87. // Note: This spawns a new process to act as the 'agent' for the worker.
  88. m_agent = heap().allocate<WorkerAgent>(outside_settings.realm(), url, options, port, outside_settings);
  89. }
  90. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate
  91. WebIDL::ExceptionOr<void> Worker::terminate()
  92. {
  93. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Terminate");
  94. return {};
  95. }
  96. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
  97. WebIDL::ExceptionOr<void> Worker::post_message(JS::Value message, StructuredSerializeOptions const& options)
  98. {
  99. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Post Message: {}", message.to_string_without_side_effects());
  100. // The postMessage(message, transfer) and postMessage(message, options) methods on Worker objects act as if,
  101. // when invoked, they immediately invoked the respective postMessage(message, transfer) and
  102. // postMessage(message, options) on the port, with the same arguments, and returned the same return value.
  103. return m_outside_port->post_message(message, options);
  104. }
  105. #undef __ENUMERATE
  106. #define __ENUMERATE(attribute_name, event_name) \
  107. void Worker::set_##attribute_name(WebIDL::CallbackType* value) \
  108. { \
  109. set_event_handler_attribute(event_name, move(value)); \
  110. } \
  111. WebIDL::CallbackType* Worker::attribute_name() \
  112. { \
  113. return event_handler_attribute(event_name); \
  114. }
  115. ENUMERATE_WORKER_EVENT_HANDLERS(__ENUMERATE)
  116. #undef __ENUMERATE
  117. } // namespace Web::HTML