Skip to content

Commit

Permalink
LibWeb: Add partially functioning Worker API
Browse files Browse the repository at this point in the history
Add a partial implementation of HTML5 Worker API.
Messages can be sent from the inner context externally.
  • Loading branch information
ben-abraham authored and awesomekling committed Feb 17, 2022
1 parent b6c3fad commit ae346cf
Show file tree
Hide file tree
Showing 15 changed files with 659 additions and 3 deletions.
4 changes: 4 additions & 0 deletions AK/Debug.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,10 @@
#cmakedefine01 WEBSERVER_DEBUG
#endif

#ifndef WEB_WORKER_DEBUG
#cmakedefine01 WEB_WORKER_DEBUG
#endif

#ifndef WINDOWMANAGER_DEBUG
#cmakedefine01 WINDOWMANAGER_DEBUG
#endif
Expand Down
1 change: 1 addition & 0 deletions Meta/CMake/all_the_debug_macros.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ set(WASM_BINPARSER_DEBUG ON)
set(WASM_TRACE_DEBUG ON)
set(WASM_VALIDATOR_DEBUG ON)
set(WEBSERVER_DEBUG ON)
set(WEB_WORKER_DEBUG ON)
set(WINDOWMANAGER_DEBUG ON)
set(WSMESSAGELOOP_DEBUG ON)
set(WSSCREEN_DEBUG ON)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2613,8 +2613,13 @@ void generate_prototype_implementation(IDL::Interface const& interface)
#include <LibWeb/Bindings/ExceptionOrUtils.h>
#include <LibWeb/Bindings/LocationObject.h>
#include <LibWeb/Bindings/WindowObject.h>
#include <LibWeb/Bindings/WorkerLocationWrapper.h>
#include <LibWeb/Bindings/WorkerNavigatorWrapper.h>
#include <LibWeb/Bindings/WorkerWrapper.h>
#include <LibWeb/DOM/Element.h>
#include <LibWeb/DOM/Event.h>
#include <LibWeb/DOM/IDLEventListener.h>
#include <LibWeb/DOM/Range.h>
#include <LibWeb/DOM/Window.h>
#include <LibWeb/HTML/Scripting/Environments.h>
#include <LibWeb/Origin.h>
Expand Down
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/Bindings/WindowObjectHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@
#include <LibWeb/Bindings/URLSearchParamsPrototype.h>
#include <LibWeb/Bindings/WebSocketConstructor.h>
#include <LibWeb/Bindings/WebSocketPrototype.h>
#include <LibWeb/Bindings/WorkerConstructor.h>
#include <LibWeb/Bindings/WorkerPrototype.h>
#include <LibWeb/Bindings/XMLHttpRequestConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestEventTargetConstructor.h>
#include <LibWeb/Bindings/XMLHttpRequestEventTargetPrototype.h>
Expand Down Expand Up @@ -467,6 +469,7 @@
ADD_WINDOW_OBJECT_INTERFACE(URLSearchParams) \
ADD_WINDOW_OBJECT_INTERFACE(URL) \
ADD_WINDOW_OBJECT_INTERFACE(WebSocket) \
ADD_WINDOW_OBJECT_INTERFACE(Worker) \
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequest) \
ADD_WINDOW_OBJECT_INTERFACE(XMLHttpRequestEventTarget) \
ADD_WINDOW_OBJECT_CONSTRUCTOR_AND_PROTOTYPE(Image, ImageConstructor, HTMLImageElementPrototype)
3 changes: 3 additions & 0 deletions Userland/Libraries/LibWeb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ set(SOURCES
HTML/TagNames.cpp
HTML/TextMetrics.cpp
HTML/WebSocket.cpp
HTML/Worker.cpp
HTML/WorkerDebugConsoleClient.cpp
HTML/WorkerGlobalScope.cpp
HTML/WorkerLocation.cpp
HighResolutionTime/Performance.cpp
Expand Down Expand Up @@ -518,6 +520,7 @@ libweb_js_wrapper(HTML/Storage)
libweb_js_wrapper(HTML/SubmitEvent)
libweb_js_wrapper(HTML/TextMetrics)
libweb_js_wrapper(HTML/WebSocket)
libweb_js_wrapper(HTML/Worker)
libweb_js_wrapper(HTML/WorkerGlobalScope)
libweb_js_wrapper(HTML/WorkerLocation)
libweb_js_wrapper(HTML/WorkerNavigator)
Expand Down
4 changes: 4 additions & 0 deletions Userland/Libraries/LibWeb/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,14 @@ class MessageEvent;
class MessagePort;
class PageTransitionEvent;
class PromiseRejectionEvent;
class WorkerDebugConsoleClient;
class Storage;
class SubmitEvent;
class TextMetrics;
class WebSocket;
class WindowEnvironmentSettingsObject;
class Worker;
class WorkerEnvironmentSettingsObject;
class WorkerGlobalScope;
class WorkerLocation;
class WorkerNavigator;
Expand Down Expand Up @@ -488,6 +491,7 @@ class URLSearchParamsWrapper;
class URLWrapper;
class WebSocketWrapper;
class WindowObject;
class WorkerWrapper;
class WorkerGlobalScopeWrapper;
class WorkerLocationWrapper;
class WorkerNavigatorWrapper;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/MessageChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ MessageChannel::MessageChannel()
m_port2 = MessagePort::create();

// 3. Entangle this's port 1 and this's port 2.
m_port1->entangle_with({}, *m_port2);
m_port1->entangle_with(*m_port2);
}

MessageChannel::~MessageChannel()
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/MessagePort.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ void MessagePort::disentangle()
}

// https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
void MessagePort::entangle_with(Badge<MessageChannel>, MessagePort& remote_port)
void MessagePort::entangle_with(MessagePort& remote_port)
{
if (m_remote_port == &remote_port)
return;
Expand Down
2 changes: 1 addition & 1 deletion Userland/Libraries/LibWeb/HTML/MessagePort.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class MessagePort final
virtual JS::Object* create_wrapper(JS::GlobalObject&) override;

// https://html.spec.whatwg.org/multipage/web-messaging.html#entangle
void entangle_with(Badge<MessageChannel>, MessagePort&);
void entangle_with(MessagePort&);

// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-messageport-postmessage
void post_message(JS::Value);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
/*
* Copyright (c) 2022, Ben Abraham <[email protected]>
*
* SPDX-License-Identifier: BSD-2-Clause
*/

#pragma once

#include <AK/URL.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/Forward.h>

namespace Web::HTML {

// FIXME: This is a bit ugly, this implementation is basically a 1:1 copy of what is in ESO
// just modified to use DOM::Document instead of DOM::Window since workers have no window
class WorkerEnvironmentSettingsObject final
: public EnvironmentSettingsObject
, public Weakable<WorkerEnvironmentSettingsObject> {
public:
WorkerEnvironmentSettingsObject(DOM::Document& document, JS::ExecutionContext& execution_context)
: EnvironmentSettingsObject(execution_context)
, m_document(document)
{
}

static WeakPtr<WorkerEnvironmentSettingsObject> setup(DOM::Document& document, JS::ExecutionContext& execution_context /* FIXME: null or an environment reservedEnvironment, a URL topLevelCreationURL, and an origin topLevelOrigin */)
{
auto* realm = execution_context.realm;
VERIFY(realm);
auto settings_object = adopt_own(*new WorkerEnvironmentSettingsObject(document, execution_context));
settings_object->target_browsing_context = nullptr;
realm->set_host_defined(move(settings_object));

return static_cast<WorkerEnvironmentSettingsObject*>(realm->host_defined());
}

virtual ~WorkerEnvironmentSettingsObject() override = default;

RefPtr<DOM::Document> responsible_document() override { return m_document; }
String api_url_character_encoding() override { return m_document->encoding_or_default(); }
AK::URL api_base_url() override { return m_document->url(); }
Origin origin() override { return m_document->origin(); }
CanUseCrossOriginIsolatedAPIs cross_origin_isolated_capability() override { TODO(); }

private:
NonnullRefPtr<DOM::Document> m_document;
};

}
Loading

0 comments on commit ae346cf

Please sign in to comment.