Worker.cpp 6.0 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/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. JS_DEFINE_ALLOCATOR(Worker);
  16. // https://html.spec.whatwg.org/multipage/workers.html#dedicated-workers-and-the-worker-interface
  17. Worker::Worker(String const& script_url, WorkerOptions const options, DOM::Document& document)
  18. : DOM::EventTarget(document.realm())
  19. , m_script_url(script_url)
  20. , m_options(options)
  21. , m_document(&document)
  22. {
  23. }
  24. void Worker::initialize(JS::Realm& realm)
  25. {
  26. Base::initialize(realm);
  27. set_prototype(&Bindings::ensure_web_prototype<Bindings::WorkerPrototype>(realm, "Worker"));
  28. }
  29. void Worker::visit_edges(Cell::Visitor& visitor)
  30. {
  31. Base::visit_edges(visitor);
  32. visitor.visit(m_document);
  33. visitor.visit(m_outside_port);
  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 = document.relevant_settings_object();
  50. // 3. Parse the scriptURL argument relative to outside settings.
  51. auto url = document.parse_url(script_url.to_deprecated_string());
  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"_fly_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. // 9. Run this step in parallel:
  65. // 1. Run a worker given worker, worker URL, outside settings, outside port, and options.
  66. worker->run_a_worker(url, outside_settings, *outside_port, options);
  67. // 10. Return worker
  68. return worker;
  69. }
  70. // https://html.spec.whatwg.org/multipage/workers.html#run-a-worker
  71. void Worker::run_a_worker(AK::URL& url, EnvironmentSettingsObject& outside_settings, MessagePort&, WorkerOptions const& options)
  72. {
  73. // 1. Let is shared be true if worker is a SharedWorker object, and false otherwise.
  74. // FIXME: SharedWorker support
  75. // 2. Let owner be the relevant owner to add given outside settings.
  76. // FIXME: Support WorkerGlobalScope options
  77. if (!is<HTML::WindowEnvironmentSettingsObject>(outside_settings))
  78. TODO();
  79. // 3. Let parent worker global scope be null.
  80. // 4. If owner is a WorkerGlobalScope object (i.e., we are creating a nested dedicated worker),
  81. // then set parent worker global scope to owner.
  82. // FIXME: Support for nested workers.
  83. // 5. Let unsafeWorkerCreationTime be the unsafe shared current time.
  84. // 6. Let agent be the result of obtaining a dedicated/shared worker agent given outside settings
  85. // and is shared. Run the rest of these steps in that agent.
  86. // Note: This spawns a new process to act as the 'agent' for the worker.
  87. m_agent = heap().allocate_without_realm<WorkerAgent>(url, options);
  88. }
  89. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-terminate
  90. WebIDL::ExceptionOr<void> Worker::terminate()
  91. {
  92. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Terminate");
  93. return {};
  94. }
  95. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-postmessage
  96. void Worker::post_message(JS::Value message, JS::Value)
  97. {
  98. dbgln_if(WEB_WORKER_DEBUG, "WebWorker: Post Message: {}", message.to_string_without_side_effects());
  99. // 1. Let targetPort be the port with which this is entangled, if any; otherwise let it be null.
  100. auto& target_port = m_outside_port;
  101. // 2. Let options be «[ "transfer" → transfer ]».
  102. // 3. Run the message port post message steps providing this, targetPort, message and options.
  103. target_port->post_message(message);
  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