1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 21:17:44 +00:00

LibWeb: Remove unecessary dependence on Window from WebGL and WebSocket

These classes only needed Window to get at its realm. Pass a realm
directly to construct WebGL and WebSocket classes.
This commit is contained in:
Andrew Kaster 2022-09-25 18:12:50 -06:00 committed by Linus Groh
parent 4bb6345b2f
commit beb3519a49
9 changed files with 42 additions and 42 deletions

View file

@ -97,7 +97,7 @@ JS::ThrowCompletionOr<HTMLCanvasElement::HasOrCreatedContext> HTMLCanvasElement:
if (!m_context.has<Empty>())
return m_context.has<JS::NonnullGCPtr<WebGL::WebGLRenderingContext>>() ? HasOrCreatedContext::Yes : HasOrCreatedContext::No;
auto maybe_context = TRY(WebGL::WebGLRenderingContext::create(window(), *this, options));
auto maybe_context = TRY(WebGL::WebGLRenderingContext::create(realm(), *this, options));
if (!maybe_context)
return HasOrCreatedContext::No;

View file

@ -4,26 +4,26 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/HTML/Window.h>
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/WebGL/WebGLContextEvent.h>
namespace Web::WebGL {
WebGLContextEvent* WebGLContextEvent::create(HTML::Window& window_object, FlyString const& event_name, WebGLContextEventInit const& event_init)
WebGLContextEvent* WebGLContextEvent::create(JS::Realm& realm, FlyString const& event_name, WebGLContextEventInit const& event_init)
{
return window_object.heap().allocate<WebGLContextEvent>(window_object.realm(), window_object, event_name, event_init);
return realm.heap().allocate<WebGLContextEvent>(realm, realm, event_name, event_init);
}
WebGLContextEvent* WebGLContextEvent::create_with_global_object(HTML::Window& window_object, FlyString const& event_name, WebGLContextEventInit const& event_init)
WebGLContextEvent* WebGLContextEvent::construct_impl(JS::Realm& realm, FlyString const& event_name, WebGLContextEventInit const& event_init)
{
return create(window_object, event_name, event_init);
return create(realm, event_name, event_init);
}
WebGLContextEvent::WebGLContextEvent(HTML::Window& window_object, FlyString const& type, WebGLContextEventInit const& event_init)
: DOM::Event(window_object, type, event_init)
WebGLContextEvent::WebGLContextEvent(JS::Realm& realm, FlyString const& type, WebGLContextEventInit const& event_init)
: DOM::Event(realm, type, event_init)
, m_status_message(event_init.status_message)
{
set_prototype(&window_object.cached_web_prototype("WebGLContextEvent"));
set_prototype(&Bindings::cached_web_prototype(realm, "WebGLContextEvent"));
}
WebGLContextEvent::~WebGLContextEvent() = default;

View file

@ -19,16 +19,16 @@ class WebGLContextEvent final : public DOM::Event {
WEB_PLATFORM_OBJECT(WebGLContextEvent, DOM::Event);
public:
static WebGLContextEvent* create(HTML::Window&, FlyString const& type, WebGLContextEventInit const& event_init);
static WebGLContextEvent* create_with_global_object(HTML::Window&, FlyString const& type, WebGLContextEventInit const& event_init);
WebGLContextEvent(HTML::Window&, FlyString const& type, WebGLContextEventInit const& event_init);
static WebGLContextEvent* create(JS::Realm&, FlyString const& type, WebGLContextEventInit const& event_init);
static WebGLContextEvent* construct_impl(JS::Realm&, FlyString const& type, WebGLContextEventInit const& event_init);
virtual ~WebGLContextEvent() override;
String const& status_message() const { return m_status_message; }
private:
WebGLContextEvent(JS::Realm&, FlyString const& type, WebGLContextEventInit const& event_init);
String m_status_message { String::empty() };
};

View file

@ -4,9 +4,9 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibWeb/Bindings/Intrinsics.h>
#include <LibWeb/DOM/Document.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebGL/WebGLContextEvent.h>
#include <LibWeb/WebGL/WebGLRenderingContext.h>
@ -17,7 +17,7 @@ static void fire_webgl_context_event(HTML::HTMLCanvasElement& canvas_element, Fl
{
// To fire a WebGL context event named e means that an event using the WebGLContextEvent interface, with its type attribute [DOM4] initialized to e, its cancelable attribute initialized to true, and its isTrusted attribute [DOM4] initialized to true, is to be dispatched at the given object.
// FIXME: Consider setting a status message.
auto event = WebGLContextEvent::create(canvas_element.document().window(), type, WebGLContextEventInit {});
auto event = WebGLContextEvent::create(canvas_element.realm(), type, WebGLContextEventInit {});
event->set_is_trusted(true);
event->set_cancelable(true);
canvas_element.dispatch_event(*event);
@ -30,7 +30,7 @@ static void fire_webgl_context_creation_error(HTML::HTMLCanvasElement& canvas_el
fire_webgl_context_event(canvas_element, "webglcontextcreationerror"sv);
}
JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> WebGLRenderingContext::create(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> WebGLRenderingContext::create(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, JS::Value options)
{
// We should be coming here from getContext being called on a wrapped <canvas> element.
auto context_attributes = TRY(convert_value_to_context_attributes_dictionary(canvas_element.vm(), options));
@ -46,13 +46,13 @@ JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> WebGLRenderingContext::c
fire_webgl_context_creation_error(canvas_element);
return JS::GCPtr<WebGLRenderingContext> { nullptr };
}
return window.heap().allocate<WebGLRenderingContext>(window.realm(), window, canvas_element, context_or_error.release_value(), context_attributes, context_attributes);
return realm.heap().allocate<WebGLRenderingContext>(realm, realm, canvas_element, context_or_error.release_value(), context_attributes, context_attributes);
}
WebGLRenderingContext::WebGLRenderingContext(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
: WebGLRenderingContextBase(window, canvas_element, move(context), move(context_creation_parameters), move(actual_context_parameters))
WebGLRenderingContext::WebGLRenderingContext(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
: WebGLRenderingContextBase(realm, canvas_element, move(context), move(context_creation_parameters), move(actual_context_parameters))
{
set_prototype(&window.cached_web_prototype("WebGLRenderingContext"));
set_prototype(&Bindings::cached_web_prototype(realm, "WebGLRenderingContext"));
}
WebGLRenderingContext::~WebGLRenderingContext() = default;

View file

@ -16,12 +16,12 @@ class WebGLRenderingContext final : public WebGLRenderingContextBase {
WEB_PLATFORM_OBJECT(WebGLRenderingContext, WebGLRenderingContextBase);
public:
static JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> create(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, JS::Value options);
static JS::ThrowCompletionOr<JS::GCPtr<WebGLRenderingContext>> create(JS::Realm&, HTML::HTMLCanvasElement& canvas_element, JS::Value options);
virtual ~WebGLRenderingContext() override;
private:
WebGLRenderingContext(HTML::Window&, HTML::HTMLCanvasElement&, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
WebGLRenderingContext(JS::Realm&, HTML::HTMLCanvasElement&, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
};
}

View file

@ -7,13 +7,12 @@
#include <AK/Debug.h>
#include <LibGL/GLContext.h>
#include <LibWeb/HTML/HTMLCanvasElement.h>
#include <LibWeb/HTML/Window.h>
#include <LibWeb/WebGL/WebGLRenderingContextBase.h>
namespace Web::WebGL {
WebGLRenderingContextBase::WebGLRenderingContextBase(HTML::Window& window, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
: PlatformObject(window.realm())
WebGLRenderingContextBase::WebGLRenderingContextBase(JS::Realm& realm, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters)
: PlatformObject(realm)
, m_canvas_element(canvas_element)
, m_context(move(context))
, m_context_creation_parameters(move(context_creation_parameters))

View file

@ -64,7 +64,7 @@ public:
void viewport(GLint x, GLint y, GLsizei width, GLsizei height);
protected:
WebGLRenderingContextBase(HTML::Window&, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
WebGLRenderingContextBase(JS::Realm&, HTML::HTMLCanvasElement& canvas_element, NonnullOwnPtr<GL::GLContext> context, WebGLContextAttributes context_creation_parameters, WebGLContextAttributes actual_context_parameters);
private:
virtual void visit_edges(Cell::Visitor&) override;

View file

@ -47,25 +47,26 @@ WebSocketClientSocket::~WebSocketClientSocket() = default;
WebSocketClientManager::WebSocketClientManager() = default;
// https://websockets.spec.whatwg.org/#dom-websocket-websocket
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> WebSocket::create_with_global_object(HTML::Window& window, String const& url)
WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> WebSocket::construct_impl(JS::Realm& realm, String const& url)
{
auto& window = verify_cast<HTML::Window>(realm.global_object());
AK::URL url_record(url);
if (!url_record.is_valid())
return WebIDL::SyntaxError::create(window, "Invalid URL");
return WebIDL::SyntaxError::create(realm, "Invalid URL");
if (!url_record.scheme().is_one_of("ws", "wss"))
return WebIDL::SyntaxError::create(window, "Invalid protocol");
return WebIDL::SyntaxError::create(realm, "Invalid protocol");
if (!url_record.fragment().is_empty())
return WebIDL::SyntaxError::create(window, "Presence of URL fragment is invalid");
return WebIDL::SyntaxError::create(realm, "Presence of URL fragment is invalid");
// 5. If `protocols` is a string, set `protocols` to a sequence consisting of just that string
// 6. If any of the values in `protocols` occur more than once or otherwise fail to match the requirements, throw SyntaxError
return JS::NonnullGCPtr(*window.heap().allocate<WebSocket>(window.realm(), window, url_record));
return JS::NonnullGCPtr(*realm.heap().allocate<WebSocket>(realm, window, url_record));
}
WebSocket::WebSocket(HTML::Window& window, AK::URL& url)
: EventTarget(window.realm())
, m_window(window)
{
set_prototype(&window.cached_web_prototype("WebSocket"));
set_prototype(&Bindings::cached_web_prototype(window.realm(), "WebSocket"));
// FIXME: Integrate properly with FETCH as per https://fetch.spec.whatwg.org/#websocket-opening-handshake
auto origin_string = m_window->associated_document().origin().serialize();
@ -137,13 +138,13 @@ WebIDL::ExceptionOr<void> WebSocket::close(Optional<u16> code, Optional<String>
{
// 1. If code is present, but is neither an integer equal to 1000 nor an integer in the range 3000 to 4999, inclusive, throw an "InvalidAccessError" DOMException.
if (code.has_value() && *code != 1000 && (*code < 3000 || *code > 4099))
return WebIDL::InvalidAccessError::create(global_object(), "The close error code is invalid");
return WebIDL::InvalidAccessError::create(realm(), "The close error code is invalid");
// 2. If reason is present, then run these substeps:
if (reason.has_value()) {
// 1. Let reasonBytes be the result of encoding reason.
// 2. If reasonBytes is longer than 123 bytes, then throw a "SyntaxError" DOMException.
if (reason->bytes().size() > 123)
return WebIDL::SyntaxError::create(global_object(), "The close reason is longer than 123 bytes");
return WebIDL::SyntaxError::create(realm(), "The close reason is longer than 123 bytes");
}
// 3. Run the first matching steps from the following list:
auto state = ready_state();
@ -164,7 +165,7 @@ WebIDL::ExceptionOr<void> WebSocket::send(String const& data)
{
auto state = ready_state();
if (state == WebSocket::ReadyState::Connecting)
return WebIDL::InvalidStateError::create(global_object(), "Websocket is still CONNECTING");
return WebIDL::InvalidStateError::create(realm(), "Websocket is still CONNECTING");
if (state == WebSocket::ReadyState::Open) {
m_websocket->send(data);
// TODO : If the data cannot be sent, e.g. because it would need to be buffered but the buffer is full, the user agent must flag the WebSocket as full and then close the WebSocket connection.
@ -179,13 +180,13 @@ void WebSocket::on_open()
// 1. Change the readyState attribute's value to OPEN (1).
// 2. Change the extensions attribute's value to the extensions in use, if it is not the null value. [WSP]
// 3. Change the protocol attribute's value to the subprotocol in use, if it is not the null value. [WSP]
dispatch_event(*DOM::Event::create(*m_window, HTML::EventNames::open));
dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::open));
}
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
void WebSocket::on_error()
{
dispatch_event(*DOM::Event::create(*m_window, HTML::EventNames::error));
dispatch_event(*DOM::Event::create(realm(), HTML::EventNames::error));
}
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
@ -197,7 +198,7 @@ void WebSocket::on_close(u16 code, String reason, bool was_clean)
event_init.was_clean = was_clean;
event_init.code = code;
event_init.reason = move(reason);
dispatch_event(*HTML::CloseEvent::create(*m_window, HTML::EventNames::close, event_init));
dispatch_event(*HTML::CloseEvent::create(realm(), HTML::EventNames::close, event_init));
}
// https://websockets.spec.whatwg.org/#feedback-from-the-protocol
@ -210,7 +211,7 @@ void WebSocket::on_message(ByteBuffer message, bool is_text)
HTML::MessageEventInit event_init;
event_init.data = JS::js_string(vm(), text_message);
event_init.origin = url();
dispatch_event(*HTML::MessageEvent::create(*m_window, HTML::EventNames::message, event_init));
dispatch_event(*HTML::MessageEvent::create(realm(), HTML::EventNames::message, event_init));
return;
}
@ -222,7 +223,7 @@ void WebSocket::on_message(ByteBuffer message, bool is_text)
HTML::MessageEventInit event_init;
event_init.data = JS::ArrayBuffer::create(realm(), message);
event_init.origin = url();
dispatch_event(*HTML::MessageEvent::create(*m_window, HTML::EventNames::message, event_init));
dispatch_event(*HTML::MessageEvent::create(realm(), HTML::EventNames::message, event_init));
return;
}

View file

@ -37,7 +37,7 @@ public:
Closed = 3,
};
static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> create_with_global_object(HTML::Window&, String const& url);
static WebIDL::ExceptionOr<JS::NonnullGCPtr<WebSocket>> construct_impl(JS::Realm&, String const& url);
virtual ~WebSocket() override;
@ -66,7 +66,7 @@ private:
void on_error();
void on_close(u16 code, String reason, bool was_clean);
explicit WebSocket(HTML::Window&, AK::URL&);
WebSocket(HTML::Window&, AK::URL&);
virtual void visit_edges(Cell::Visitor&) override;