Worker.cpp 6.0 KB

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