WorkerGlobalScope.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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/Intrinsics.h>
  8. #include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
  9. #include <LibWeb/CSS/FontFaceSet.h>
  10. #include <LibWeb/HTML/EventHandler.h>
  11. #include <LibWeb/HTML/EventNames.h>
  12. #include <LibWeb/HTML/MessageEvent.h>
  13. #include <LibWeb/HTML/MessagePort.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. GC_DEFINE_ALLOCATOR(WorkerGlobalScope);
  22. WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm, GC::Ref<Web::Page> page)
  23. : DOM::EventTarget(realm)
  24. , m_page(page)
  25. {
  26. }
  27. WorkerGlobalScope::~WorkerGlobalScope() = default;
  28. void WorkerGlobalScope::initialize_web_interfaces_impl()
  29. {
  30. auto& realm = this->realm();
  31. Base::initialize(realm);
  32. WindowOrWorkerGlobalScopeMixin::initialize(realm);
  33. m_navigator = WorkerNavigator::create(*this);
  34. }
  35. void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
  36. {
  37. Base::visit_edges(visitor);
  38. WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
  39. UniversalGlobalScopeMixin::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. visitor.visit(m_fonts);
  45. }
  46. void WorkerGlobalScope::finalize()
  47. {
  48. Base::finalize();
  49. WindowOrWorkerGlobalScopeMixin::finalize();
  50. }
  51. void WorkerGlobalScope::set_internal_port(GC::Ref<MessagePort> port)
  52. {
  53. m_internal_port = port;
  54. m_internal_port->set_worker_event_target(*this);
  55. }
  56. // https://html.spec.whatwg.org/multipage/workers.html#close-a-worker
  57. void WorkerGlobalScope::close_a_worker()
  58. {
  59. // 1. Discard any tasks that have been added to workerGlobal's relevant agent's event loop's task queues.
  60. relevant_settings_object(*this).responsible_event_loop().task_queue().remove_tasks_matching([](HTML::Task const& task) {
  61. // NOTE: We don't discard tasks with the PostedMessage source, as the spec expects PostMessage() to act as if it is invoked immediately
  62. return task.source() != HTML::Task::Source::PostedMessage;
  63. });
  64. // 2. Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)
  65. m_closing = true;
  66. }
  67. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  68. // https://whatpr.org/html/9893/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 principal settings object.
  76. auto& settings_object = HTML::current_principal_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.encoding_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"_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. GC::Ref<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. GC::Ref<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. #undef __ENUMERATE
  121. #define __ENUMERATE(attribute_name, event_name) \
  122. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  123. { \
  124. set_event_handler_attribute(event_name, move(value)); \
  125. } \
  126. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  127. { \
  128. return event_handler_attribute(event_name); \
  129. }
  130. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  131. #undef __ENUMERATE
  132. GC::Ref<CSS::FontFaceSet> WorkerGlobalScope::fonts()
  133. {
  134. if (!m_fonts)
  135. m_fonts = CSS::FontFaceSet::create(realm());
  136. return *m_fonts;
  137. }
  138. }