WorkerGlobalScope.cpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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_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(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#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. WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> const& urls, PerformTheFetchHook perform_fetch)
  68. {
  69. // The algorithm may optionally be customized by supplying custom perform the fetch hooks,
  70. // which if provided will be used when invoking fetch a classic worker-imported script.
  71. // NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
  72. // FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
  73. // 2. Let settings object be the current settings object.
  74. auto& settings_object = HTML::current_settings_object();
  75. // 3. If urls is empty, return.
  76. if (urls.is_empty())
  77. return {};
  78. // 4. Let urlRecords be « ».
  79. Vector<URL::URL> url_records;
  80. url_records.ensure_capacity(urls.size());
  81. // 5. For each url of urls:
  82. for (auto const& url : urls) {
  83. // 1. Let urlRecord be the result of encoding-parsing a URL given url, relative to settings object.
  84. auto url_record = settings_object.parse_url(url);
  85. // 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
  86. if (!url_record.is_valid())
  87. return WebIDL::SyntaxError::create(realm(), "Invalid URL"_string);
  88. // 3. Append urlRecord to urlRecords.
  89. url_records.unchecked_append(url_record);
  90. }
  91. // 6. For each urlRecord of urlRecords:
  92. for (auto const& url_record : url_records) {
  93. // 1. Fetch a classic worker-imported script given urlRecord and settings object, passing along performFetch if provided.
  94. // If this succeeds, let script be the result. Otherwise, rethrow the exception.
  95. auto classic_script = TRY(HTML::fetch_a_classic_worker_imported_script(url_record, settings_object, perform_fetch));
  96. // 2. Run the classic script script, with the rethrow errors argument set to true.
  97. // NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
  98. // or gets prematurely aborted by the terminate a worker algorithm defined above.
  99. // If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
  100. // letting the exception or aborting continue to be processed by the calling script.
  101. TRY(classic_script->run(ClassicScript::RethrowErrors::Yes));
  102. }
  103. return {};
  104. }
  105. // https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
  106. JS::NonnullGCPtr<WorkerLocation> WorkerGlobalScope::location() const
  107. {
  108. // The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
  109. return *m_location;
  110. }
  111. // https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
  112. JS::NonnullGCPtr<WorkerNavigator> WorkerGlobalScope::navigator() const
  113. {
  114. // The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
  115. // which represents the identity and state of the user agent (the client).
  116. return *m_navigator;
  117. }
  118. #undef __ENUMERATE
  119. #define __ENUMERATE(attribute_name, event_name) \
  120. void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
  121. { \
  122. set_event_handler_attribute(event_name, move(value)); \
  123. } \
  124. WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
  125. { \
  126. return event_handler_attribute(event_name); \
  127. }
  128. ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
  129. #undef __ENUMERATE
  130. JS::NonnullGCPtr<CSS::FontFaceSet> WorkerGlobalScope::fonts()
  131. {
  132. if (!m_fonts)
  133. m_fonts = CSS::FontFaceSet::create(realm());
  134. return *m_fonts;
  135. }
  136. }