LibWeb: Add stub of ServiceWorker interface

(cherry picked from commit a0c07d1bb27cdab33a12e1b1ad03a9452750503a)
This commit is contained in:
Andrew Kaster 2024-09-09 23:29:41 -06:00 committed by Nico Weber
parent 345143a5d0
commit 98663d09c6
6 changed files with 125 additions and 0 deletions

View file

@ -330,6 +330,7 @@ SVGUseElement
Screen
ScreenOrientation
Selection
ServiceWorker
ServiceWorkerContainer
ServiceWorkerRegistration
Set

View file

@ -448,6 +448,7 @@ set(SOURCES
HTML/Scripting/SerializedEnvironmentSettingsObject.cpp
HTML/SelectedFile.cpp
HTML/SelectItem.cpp
HTML/ServiceWorker.cpp
HTML/ServiceWorkerContainer.cpp
HTML/ServiceWorkerRegistration.cpp
HTML/SessionHistoryEntry.cpp

View file

@ -0,0 +1,47 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibJS/Heap/Heap.h>
#include <LibJS/Runtime/Realm.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/HTML/EventNames.h>
#include <LibWeb/HTML/ServiceWorker.h>
namespace Web::HTML {
ServiceWorker::ServiceWorker(JS::Realm& realm, String script_url)
: DOM::EventTarget(realm)
, m_script_url(move(script_url))
{
}
ServiceWorker::~ServiceWorker() = default;
JS::NonnullGCPtr<ServiceWorker> ServiceWorker::create(JS::Realm& realm)
{
return realm.heap().allocate<ServiceWorker>(realm, realm, ""_string);
}
void ServiceWorker::initialize(JS::Realm& realm)
{
Base::initialize(realm);
WEB_SET_PROTOTYPE_FOR_INTERFACE(ServiceWorker);
}
#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \
void ServiceWorker::set_##attribute_name(WebIDL::CallbackType* value) \
{ \
set_event_handler_attribute(event_name, value); \
} \
WebIDL::CallbackType* ServiceWorker::attribute_name() \
{ \
return event_handler_attribute(event_name); \
}
ENUMERATE_SERVICE_WORKER_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE
}

View file

@ -0,0 +1,46 @@
/*
* Copyright (c) 2024, Andrew Kaster <andrew@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/Bindings/ServiceWorkerPrototype.h>
#include <LibWeb/DOM/EventTarget.h>
#include <LibWeb/HTML/MessagePort.h>
#define ENUMERATE_SERVICE_WORKER_EVENT_HANDLERS(E) \
E(onstatechange, HTML::EventNames::statechange) \
E(onerror, HTML::EventNames::error)
namespace Web::HTML {
class ServiceWorker : public DOM::EventTarget {
WEB_PLATFORM_OBJECT(ServiceWorker, DOM::EventTarget);
public:
[[nodiscard]] static JS::NonnullGCPtr<ServiceWorker> create(JS::Realm& realm);
virtual ~ServiceWorker() override;
String script_url() const { return m_script_url; }
Bindings::ServiceWorkerState service_worker_state() const { return m_state; }
#undef __ENUMERATE
#define __ENUMERATE(attribute_name, event_name) \
void set_##attribute_name(WebIDL::CallbackType*); \
WebIDL::CallbackType* attribute_name();
ENUMERATE_SERVICE_WORKER_EVENT_HANDLERS(__ENUMERATE)
#undef __ENUMERATE
private:
ServiceWorker(JS::Realm&, String script_url);
virtual void initialize(JS::Realm&) override;
String m_script_url;
Bindings::ServiceWorkerState m_state { Bindings::ServiceWorkerState::Parsed };
};
}

View file

@ -0,0 +1,29 @@
#import <DOM/EventTarget.idl>
#import <DOM/EventHandler.idl>
#import <HTML/AbstractWorker.idl>
#import <HTML/MessagePort.idl>
// https://w3c.github.io/ServiceWorker/#serviceworker-interface
[SecureContext, Exposed=(Window,Worker)]
interface ServiceWorker : EventTarget {
readonly attribute USVString scriptURL;
[ImplementedAs=service_worker_state] readonly attribute ServiceWorkerState state;
// FIXME: IDL overload issue here
// FIXME: undefined postMessage(any message, sequence<object> transfer);
[FIXME] undefined postMessage(any message, optional StructuredSerializeOptions options = {});
// event
attribute EventHandler onstatechange;
};
ServiceWorker includes AbstractWorker;
enum ServiceWorkerState {
"parsed",
"installing",
"installed",
"activating",
"activated",
"redundant"
};

View file

@ -216,6 +216,7 @@ libweb_js_bindings(HTML/Plugin)
libweb_js_bindings(HTML/PluginArray)
libweb_js_bindings(HTML/PopStateEvent)
libweb_js_bindings(HTML/PromiseRejectionEvent)
libweb_js_bindings(HTML/ServiceWorker)
libweb_js_bindings(HTML/ServiceWorkerContainer)
libweb_js_bindings(HTML/ServiceWorkerRegistration)
libweb_js_bindings(HTML/Storage)