WorkerGlobalScope.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  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. visitor.visit(m_location);
  40. visitor.visit(m_navigator);
  41. visitor.visit(m_internal_port);
  42. visitor.visit(m_page);
  43. visitor.visit(m_fonts);
  44. }
  45. void WorkerGlobalScope::finalize()
  46. {
  47. Base::finalize();
  48. WindowOrWorkerGlobalScopeMixin::finalize();
  49. }
  50. void WorkerGlobalScope::set_internal_port(GC::Ref<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#close-a-worker
  56. void WorkerGlobalScope::close_a_worker()
  57. {
  58. // 1. Discard any tasks that have been added to workerGlobal's relevant agent's event loop's task queues.
  59. relevant_settings_object(*this).responsible_event_loop().task_queue().remove_tasks_matching([](HTML::Task const& task) {
  60. // NOTE: We don't discard tasks with the PostedMessage source, as the spec expects PostMessage() to act as if it is invoked immediately
  61. return task.source() != HTML::Task::Source::PostedMessage;
  62. });
  63. // 2. Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)
  64. m_closing = true;
  65. }
  66. // https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
  67. // https://whatpr.org/html/9893/workers.html#importing-scripts-and-libraries
  68. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> const& urls, PerformTheFetchHook perform_fetch)
  69. {
  70. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  71. // which if provided will be used when invoking fetch a classic worker-imported script.
  72. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  73. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  74. // 2. Let settings object be the current principal settings object.
  75. auto& settings_object = HTML::current_principal_settings_object();
  76. // 3. If urls is empty, return.
  77. if (urls.is_empty())
  78. return {};
  79. // 4. Let urlRecords be « ».
  80. Vector<URL::URL> url_records;
  81. url_records.ensure_capacity(urls.size());
  82. // 5. For each url of urls:
  83. for (auto const& url : urls) {
  84. // 1. Let urlRecord be the result of encoding-parsing a URL given url, relative to settings object.
  85. auto url_record = settings_object.parse_url(url);
  86. // 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
  87. if (!url_record.is_valid())
  88. return WebIDL::SyntaxError::create(realm(), "Invalid URL"_string);
  89. // 3. Append urlRecord to urlRecords.
  90. url_records.unchecked_append(url_record);
  91. }
  92. // 6. For each urlRecord of urlRecords:
  93. for (auto const& url_record : url_records) {
  94. // 1. Fetch a classic worker-imported script given urlRecord and settings object, passing along performFetch if provided.
  95. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  96. auto classic_script = TRY(HTML::fetch_a_classic_worker_imported_script(url_record, settings_object, perform_fetch));
  97. // 2. Run the classic script script, with the rethrow errors argument set to true.
  98. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  99. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  100. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  101. // letting the exception or aborting continue to be processed by the calling script.
  102. TRY(classic_script->run(ClassicScript::RethrowErrors::Yes));
  103. }
  104. return {};
  105. }
  106. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  107. GC::Ref<WorkerLocation> WorkerGlobalScope::location() const
  108. {
  109. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  110. return *m_location;
  111. }
  112. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  113. GC::Ref<WorkerNavigator> WorkerGlobalScope::navigator() const
  114. {
  115. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  116. // which represents the identity and state of the user agent (the client).
  117. return *m_navigator;
  118. }
  119. #undef __ENUMERATE
  120. #define __ENUMERATE(attribute_name, event_name) \
  121. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  122. { \
  123. set_event_handler_attribute(event_name, move(value)); \
  124. } \
  125. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  126. { \
  127. return event_handler_attribute(event_name); \
  128. }
  129. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  130. #undef __ENUMERATE
  131. GC::Ref<CSS::FontFaceSet> WorkerGlobalScope::fonts()
  132. {
  133. if (!m_fonts)
  134. m_fonts = CSS::FontFaceSet::create(realm());
  135. return *m_fonts;
  136. }
  137. }