WorkerGlobalScope.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. /*
  2. * Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include <AK/Vector.h>
  7. #include <LibWeb/Bindings/DedicatedWorkerExposedInterfaces.h>
  8. #include <LibWeb/Bindings/Intrinsics.h>
  9. #include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
  10. #include <LibWeb/HTML/EventHandler.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. #include <LibWeb/HTML/MessageEvent.h>
  13. #include <LibWeb/HTML/StructuredSerialize.h>
  14. #include <LibWeb/HTML/WorkerGlobalScope.h>
  15. #include <LibWeb/HTML/WorkerLocation.h>
  16. #include <LibWeb/HTML/WorkerNavigator.h>
  17. #include <LibWeb/Page/Page.h>
  18. namespace Web::HTML {
  19. JS_DEFINE_ALLOCATOR(WorkerGlobalScope);
  20. WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm, JS::NonnullGCPtr<Web::Page> page)
  21. : DOM::EventTarget(realm)
  22. , m_page(page)
  23. {
  24. }
  25. WorkerGlobalScope::~WorkerGlobalScope() = default;
  26. void WorkerGlobalScope::initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>)
  27. {
  28. auto& realm = this->realm();
  29. Base::initialize(realm);
  30. // FIXME: Handle shared worker
  31. add_dedicated_worker_exposed_interfaces(*this);
  32. Object::set_prototype(&Bindings::ensure_web_prototype<Bindings::WorkerGlobalScopePrototype>(realm, "WorkerGlobalScope"_fly_string));
  33. WindowOrWorkerGlobalScopeMixin::initialize(realm);
  34. m_navigator = WorkerNavigator::create(*this);
  35. }
  36. void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
  37. {
  38. Base::visit_edges(visitor);
  39. WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
  40. visitor.visit(m_location);
  41. visitor.visit(m_navigator);
  42. visitor.visit(m_internal_port);
  43. visitor.visit(m_page);
  44. }
  45. void WorkerGlobalScope::finalize()
  46. {
  47. Base::finalize();
  48. WindowOrWorkerGlobalScopeMixin::finalize();
  49. }
  50. void WorkerGlobalScope::set_internal_port(JS::NonnullGCPtr<MessagePort> port)
  51. {
  52. m_internal_port = port;
  53. m_internal_port->set_worker_event_target(*this);
  54. }
  55. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  56. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> urls)
  57. {
  58. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  59. // which if provided will be used when invoking fetch a classic worker-imported script.
  60. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  61. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  62. // FIXME: 2. Let settings object be the current settings object.
  63. // 3. If urls is empty, return.
  64. if (urls.is_empty())
  65. return {};
  66. // FIXME: 4. Parse each value in urls relative to settings object. If any fail, throw a "SyntaxError" DOMException.
  67. // FIXME: 5. For each url in the resulting URL records, run these substeps:
  68. // 1. Fetch a classic worker-imported script given url and settings object, passing along any custom perform the fetch steps provided.
  69. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  70. // 2. Run the classic script script, with the rethrow errors argument set to true.
  71. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  72. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  73. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  74. // letting the exception or aborting continue to be processed by the calling script.
  75. return {};
  76. }
  77. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  78. JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const
  79. {
  80. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  81. return *m_location;
  82. }
  83. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  84. JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
  85. {
  86. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  87. // which represents the identity and state of the user agent (the client).
  88. return *m_navigator;
  89. }
  90. WebIDL::ExceptionOr<void> WorkerGlobalScope::post_message(JS::Value message, StructuredSerializeOptions const& options)
  91. {
  92. return m_internal_port->post_message(message, options);
  93. }
  94. #undef __ENUMERATE
  95. #define __ENUMERATE(attribute_name, event_name) \
  96. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  97. { \
  98. set_event_handler_attribute(event_name, move(value)); \
  99. } \
  100. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  101. { \
  102. return event_handler_attribute(event_name); \
  103. }
  104. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  105. #undef __ENUMERATE
  106. }