WorkerGlobalScope.cpp 5.3 KB

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