2022-02-06 19:12:57 -07:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2022, Andrew Kaster <akaster@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <AK/Vector.h>
|
2023-11-08 11:47:41 -07:00
|
|
|
#include <LibWeb/Bindings/Intrinsics.h>
|
2022-08-28 13:42:07 +02:00
|
|
|
#include <LibWeb/Bindings/WorkerGlobalScopePrototype.h>
|
2024-05-07 14:49:31 -06:00
|
|
|
#include <LibWeb/CSS/FontFaceSet.h>
|
2022-02-06 19:12:57 -07:00
|
|
|
#include <LibWeb/HTML/EventHandler.h>
|
|
|
|
#include <LibWeb/HTML/EventNames.h>
|
2023-11-22 09:57:22 -07:00
|
|
|
#include <LibWeb/HTML/MessageEvent.h>
|
2024-10-17 08:46:48 -04:00
|
|
|
#include <LibWeb/HTML/MessagePort.h>
|
2024-05-23 13:57:08 +01:00
|
|
|
#include <LibWeb/HTML/Scripting/ClassicScript.h>
|
2023-11-22 09:57:22 -07:00
|
|
|
#include <LibWeb/HTML/StructuredSerialize.h>
|
2022-02-06 19:12:57 -07:00
|
|
|
#include <LibWeb/HTML/WorkerGlobalScope.h>
|
|
|
|
#include <LibWeb/HTML/WorkerLocation.h>
|
|
|
|
#include <LibWeb/HTML/WorkerNavigator.h>
|
2023-12-20 13:47:01 -07:00
|
|
|
#include <LibWeb/Page/Page.h>
|
2022-02-06 19:12:57 -07:00
|
|
|
|
|
|
|
namespace Web::HTML {
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC_DEFINE_ALLOCATOR(WorkerGlobalScope);
|
2023-11-19 19:47:52 +01:00
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
WorkerGlobalScope::WorkerGlobalScope(JS::Realm& realm, GC::Ref<Web::Page> page)
|
2022-08-28 13:42:07 +02:00
|
|
|
: DOM::EventTarget(realm)
|
2023-11-08 11:47:41 -07:00
|
|
|
, m_page(page)
|
2022-02-06 19:12:57 -07:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-03-14 13:21:51 -06:00
|
|
|
WorkerGlobalScope::~WorkerGlobalScope() = default;
|
2022-02-06 19:12:57 -07:00
|
|
|
|
2024-07-09 15:54:22 -06:00
|
|
|
void WorkerGlobalScope::initialize_web_interfaces_impl()
|
2022-09-04 14:37:49 +02:00
|
|
|
{
|
2023-11-08 11:47:41 -07:00
|
|
|
auto& realm = this->realm();
|
2023-08-07 08:41:28 +02:00
|
|
|
Base::initialize(realm);
|
2023-11-08 11:47:41 -07:00
|
|
|
|
|
|
|
WindowOrWorkerGlobalScopeMixin::initialize(realm);
|
|
|
|
|
2023-08-13 13:05:26 +02:00
|
|
|
m_navigator = WorkerNavigator::create(*this);
|
2022-09-04 14:37:49 +02:00
|
|
|
}
|
|
|
|
|
2022-09-04 14:30:38 +02:00
|
|
|
void WorkerGlobalScope::visit_edges(Cell::Visitor& visitor)
|
|
|
|
{
|
|
|
|
Base::visit_edges(visitor);
|
2023-03-14 06:59:23 -04:00
|
|
|
WindowOrWorkerGlobalScopeMixin::visit_edges(visitor);
|
2024-11-18 22:49:00 +13:00
|
|
|
UniversalGlobalScopeMixin::visit_edges(visitor);
|
2023-03-14 06:59:23 -04:00
|
|
|
|
2023-11-19 16:18:00 +13:00
|
|
|
visitor.visit(m_location);
|
|
|
|
visitor.visit(m_navigator);
|
2023-12-20 13:47:01 -07:00
|
|
|
visitor.visit(m_internal_port);
|
|
|
|
visitor.visit(m_page);
|
2024-05-07 14:49:31 -06:00
|
|
|
visitor.visit(m_fonts);
|
2022-09-04 14:30:38 +02:00
|
|
|
}
|
|
|
|
|
2024-01-03 10:10:47 +01:00
|
|
|
void WorkerGlobalScope::finalize()
|
|
|
|
{
|
|
|
|
Base::finalize();
|
|
|
|
WindowOrWorkerGlobalScopeMixin::finalize();
|
|
|
|
}
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
void WorkerGlobalScope::set_internal_port(GC::Ref<MessagePort> port)
|
2023-11-22 09:57:22 -07:00
|
|
|
{
|
2023-12-20 13:47:01 -07:00
|
|
|
m_internal_port = port;
|
|
|
|
m_internal_port->set_worker_event_target(*this);
|
2023-11-22 09:57:22 -07:00
|
|
|
}
|
|
|
|
|
2024-07-09 03:02:22 -06:00
|
|
|
// https://html.spec.whatwg.org/multipage/workers.html#close-a-worker
|
2024-07-09 15:54:22 -06:00
|
|
|
void WorkerGlobalScope::close_a_worker()
|
2024-07-09 03:02:22 -06:00
|
|
|
{
|
|
|
|
// 1. Discard any tasks that have been added to workerGlobal's relevant agent's event loop's task queues.
|
2024-10-06 22:41:11 +01:00
|
|
|
relevant_settings_object(*this).responsible_event_loop().task_queue().remove_tasks_matching([](HTML::Task const& task) {
|
|
|
|
// NOTE: We don't discard tasks with the PostedMessage source, as the spec expects PostMessage() to act as if it is invoked immediately
|
|
|
|
return task.source() != HTML::Task::Source::PostedMessage;
|
2024-07-09 03:02:22 -06:00
|
|
|
});
|
|
|
|
|
|
|
|
// 2. Set workerGlobal's closing flag to true. (This prevents any further tasks from being queued.)
|
|
|
|
m_closing = true;
|
|
|
|
}
|
|
|
|
|
2022-02-06 19:12:57 -07:00
|
|
|
// https://html.spec.whatwg.org/multipage/workers.html#importing-scripts-and-libraries
|
2024-10-21 13:35:24 +13:00
|
|
|
// https://whatpr.org/html/9893/workers.html#importing-scripts-and-libraries
|
2024-05-23 13:57:08 +01:00
|
|
|
WebIDL::ExceptionOr<void> WorkerGlobalScope::import_scripts(Vector<String> const& urls, PerformTheFetchHook perform_fetch)
|
2022-02-06 19:12:57 -07:00
|
|
|
{
|
|
|
|
// The algorithm may optionally be customized by supplying custom perform the fetch hooks,
|
|
|
|
// which if provided will be used when invoking fetch a classic worker-imported script.
|
|
|
|
// NOTE: Service Workers is an example of a specification that runs this algorithm with its own options for the perform the fetch hook.
|
|
|
|
|
2022-04-20 19:48:48 +02:00
|
|
|
// FIXME: 1. If worker global scope's type is "module", throw a TypeError exception.
|
2024-05-23 13:57:08 +01:00
|
|
|
|
2024-10-21 13:35:24 +13:00
|
|
|
// 2. Let settings object be the current principal settings object.
|
|
|
|
auto& settings_object = HTML::current_principal_settings_object();
|
2022-02-06 19:12:57 -07:00
|
|
|
|
|
|
|
// 3. If urls is empty, return.
|
|
|
|
if (urls.is_empty())
|
|
|
|
return {};
|
|
|
|
|
2024-05-23 13:57:08 +01:00
|
|
|
// 4. Let urlRecords be « ».
|
|
|
|
Vector<URL::URL> url_records;
|
|
|
|
url_records.ensure_capacity(urls.size());
|
|
|
|
|
|
|
|
// 5. For each url of urls:
|
|
|
|
for (auto const& url : urls) {
|
|
|
|
// 1. Let urlRecord be the result of encoding-parsing a URL given url, relative to settings object.
|
|
|
|
auto url_record = settings_object.parse_url(url);
|
|
|
|
|
|
|
|
// 2. If urlRecord is failure, then throw a "SyntaxError" DOMException.
|
|
|
|
if (!url_record.is_valid())
|
2024-10-12 20:56:21 +02:00
|
|
|
return WebIDL::SyntaxError::create(realm(), "Invalid URL"_string);
|
2024-05-23 13:57:08 +01:00
|
|
|
|
|
|
|
// 3. Append urlRecord to urlRecords.
|
|
|
|
url_records.unchecked_append(url_record);
|
|
|
|
}
|
|
|
|
|
|
|
|
// 6. For each urlRecord of urlRecords:
|
|
|
|
for (auto const& url_record : url_records) {
|
|
|
|
// 1. Fetch a classic worker-imported script given urlRecord and settings object, passing along performFetch if provided.
|
|
|
|
// If this succeeds, let script be the result. Otherwise, rethrow the exception.
|
|
|
|
auto classic_script = TRY(HTML::fetch_a_classic_worker_imported_script(url_record, settings_object, perform_fetch));
|
|
|
|
|
|
|
|
// 2. Run the classic script script, with the rethrow errors argument set to true.
|
|
|
|
// NOTE: script will run until it either returns, fails to parse, fails to catch an exception,
|
|
|
|
// or gets prematurely aborted by the terminate a worker algorithm defined above.
|
|
|
|
// If an exception was thrown or if the script was prematurely aborted, then abort all these steps,
|
|
|
|
// letting the exception or aborting continue to be processed by the calling script.
|
|
|
|
TRY(classic_script->run(ClassicScript::RethrowErrors::Yes));
|
|
|
|
}
|
2022-02-06 19:12:57 -07:00
|
|
|
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/workers.html#dom-workerglobalscope-location
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<WorkerLocation> WorkerGlobalScope::location() const
|
2022-02-06 19:12:57 -07:00
|
|
|
{
|
|
|
|
// The location attribute must return the WorkerLocation object whose associated WorkerGlobalScope object is the WorkerGlobalScope object.
|
|
|
|
return *m_location;
|
|
|
|
}
|
|
|
|
|
|
|
|
// https://html.spec.whatwg.org/multipage/workers.html#dom-worker-navigator
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<WorkerNavigator> WorkerGlobalScope::navigator() const
|
2022-02-06 19:12:57 -07:00
|
|
|
{
|
|
|
|
// The navigator attribute of the WorkerGlobalScope interface must return an instance of the WorkerNavigator interface,
|
|
|
|
// which represents the identity and state of the user agent (the client).
|
|
|
|
return *m_navigator;
|
|
|
|
}
|
|
|
|
|
|
|
|
#undef __ENUMERATE
|
2022-09-24 16:02:41 +01:00
|
|
|
#define __ENUMERATE(attribute_name, event_name) \
|
|
|
|
void WorkerGlobalScope::set_##attribute_name(WebIDL::CallbackType* value) \
|
|
|
|
{ \
|
|
|
|
set_event_handler_attribute(event_name, move(value)); \
|
|
|
|
} \
|
|
|
|
WebIDL::CallbackType* WorkerGlobalScope::attribute_name() \
|
|
|
|
{ \
|
|
|
|
return event_handler_attribute(event_name); \
|
2022-02-06 19:12:57 -07:00
|
|
|
}
|
|
|
|
ENUMERATE_WORKER_GLOBAL_SCOPE_EVENT_HANDLERS(__ENUMERATE)
|
|
|
|
#undef __ENUMERATE
|
|
|
|
|
2024-11-15 04:01:23 +13:00
|
|
|
GC::Ref<CSS::FontFaceSet> WorkerGlobalScope::fonts()
|
2024-05-07 14:49:31 -06:00
|
|
|
{
|
|
|
|
if (!m_fonts)
|
|
|
|
m_fonts = CSS::FontFaceSet::create(realm());
|
|
|
|
return *m_fonts;
|
|
|
|
}
|
|
|
|
|
2022-02-06 19:12:57 -07:00
|
|
|
}
|