WorkerGlobalScope.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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/Scripting/ClassicScript.h>
  15. #include <LibWeb/HTML/StructuredSerialize.h>
  16. #include <LibWeb/HTML/WorkerGlobalScope.h>
  17. #include <LibWeb/HTML/WorkerLocation.h>
  18. #include <LibWeb/HTML/WorkerNavigator.h>
  19. #include <LibWeb/Page/Page.h>
  20. namespace Web::HTML {
  21. JS_DEFINE_ALLOCATOR(WorkerGlobalScope);
  22. WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm, JS::NonnullGCPtr<Web::Page> page)
  23. : DOM::EventTarget(realm)
  24. , m_page(page)
  25. {
  26. }
  27. WorkerGlobalScope::~WorkerGlobalScope() = default;
  28. void WorkerGlobalScope::initialize_web_interfaces(Badge<WorkerEnvironmentSettingsObject>)
  29. {
  30. auto& realm = this->realm();
  31. Base::initialize(realm);
  32. // FIXME: Handle shared worker
  33. add_dedicated_worker_exposed_interfaces(*this);
  34. WEB_SET_PROTOTYPE_FOR_INTERFACE(WorkerGlobalScope);
  35. WindowOrWorkerGlobalScopeMixin::initialize(realm);
  36. m_navigator = WorkerNavigator::create(*this);
  37. }
  38. void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
  39. {
  40. Base::visit_edges(visitor);
  41. WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
  42. visitor.visit(m_location);
  43. visitor.visit(m_navigator);
  44. visitor.visit(m_internal_port);
  45. visitor.visit(m_page);
  46. visitor.visit(m_fonts);
  47. }
  48. void WorkerGlobalScope::finalize()
  49. {
  50. Base::finalize();
  51. WindowOrWorkerGlobalScopeMixin::finalize();
  52. }
  53. void WorkerGlobalScope::set_internal_port(JS::NonnullGCPtr<MessagePort> port)
  54. {
  55. m_internal_port = port;
  56. m_internal_port->set_worker_event_target(*this);
  57. }
  58. // https://html.spec.whatwg.org/multipage/workers.html#close-a-worker
  59. void WorkerGlobalScope::close()
  60. {
  61. // 1. Discard any tasks that have been added to workerGlobal's relevant agent's event loop's task queues.
  62. relevant_settings_object(*this).responsible_event_loop().task_queue().remove_tasks_matching([](HTML::Task const&) {
  63. return true;
  64. });
  65. // 2. Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)
  66. m_closing = true;
  67. }
  68. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  69. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> const& urls, PerformTheFetchHook perform_fetch)
  70. {
  71. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  72. // which if provided will be used when invoking fetch a classic worker-imported script.
  73. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  74. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  75. // 2. Let settings object be the current settings object.
  76. auto& settings_object = HTML::current_settings_object();
  77. // 3. If urls is empty, return.
  78. if (urls.is_empty())
  79. return {};
  80. // 4. Let urlRecords be « ».
  81. Vector<URL::URL> url_records;
  82. url_records.ensure_capacity(urls.size());
  83. // 5. For each url of urls:
  84. for (auto const& url : urls) {
  85. // 1. Let urlRecord be the result of encoding-parsing a URL given url, relative to settings object.
  86. auto url_record = settings_object.parse_url(url);
  87. // 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
  88. if (!url_record.is_valid())
  89. return WebIDL::SyntaxError::create(realm(), "Invalid URL"_fly_string);
  90. // 3. Append urlRecord to urlRecords.
  91. url_records.unchecked_append(url_record);
  92. }
  93. // 6. For each urlRecord of urlRecords:
  94. for (auto const& url_record : url_records) {
  95. // 1. Fetch a classic worker-imported script given urlRecord and settings object, passing along performFetch if provided.
  96. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  97. auto classic_script = TRY(HTML::fetch_a_classic_worker_imported_script(url_record, settings_object, perform_fetch));
  98. // 2. Run the classic script script, with the rethrow errors argument set to true.
  99. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  100. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  101. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  102. // letting the exception or aborting continue to be processed by the calling script.
  103. TRY(classic_script->run(ClassicScript::RethrowErrors::Yes));
  104. }
  105. return {};
  106. }
  107. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  108. JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const
  109. {
  110. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  111. return *m_location;
  112. }
  113. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  114. JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
  115. {
  116. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  117. // which represents the identity and state of the user agent (the client).
  118. return *m_navigator;
  119. }
  120. WebIDL::ExceptionOr<void> WorkerGlobalScope::post_message(JS::Value message, StructuredSerializeOptions const& options)
  121. {
  122. return m_internal_port->post_message(message, options);
  123. }
  124. #undef __ENUMERATE
  125. #define __ENUMERATE(attribute_name, event_name) \
  126. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  127. { \
  128. set_event_handler_attribute(event_name, move(value)); \
  129. } \
  130. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  131. { \
  132. return event_handler_attribute(event_name); \
  133. }
  134. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  135. #undef __ENUMERATE
  136. JS::NonnullGCPtr<CSS::FontFaceSet> WorkerGlobalScope::fonts()
  137. {
  138. if (!m_fonts)
  139. m_fonts = CSS::FontFaceSet::create(realm());
  140. return *m_fonts;
  141. }
  142. }