LibWeb/HTML: Port Window.postMessage() to IDL

This commit is contained in:
Linus Groh 2023-03-05 17:36:59 +00:00
parent eb4842dfa1
commit c219e6d9c1
Notes: sideshowbarker 2024-07-17 01:51:00 +09:00
3 changed files with 19 additions and 27 deletions
Userland/Libraries/LibWeb/HTML

View file

@ -888,20 +888,6 @@ WindowProxy* Window::parent()
return current->window_proxy();
}
// https://html.spec.whatwg.org/multipage/web-messaging.html#window-post-message-steps
WebIDL::ExceptionOr<void> Window::post_message_impl(JS::Value message, DeprecatedString const&)
{
// FIXME: This is an ad-hoc hack implementation instead, since we don't currently
// have serialization and deserialization of messages.
HTML::queue_global_task(HTML::Task::Source::PostedMessage, *this, [this, message] {
HTML::MessageEventInit event_init {};
event_init.data = message;
event_init.origin = "<origin>"_string.release_value_but_fixme_should_propagate_errors();
dispatch_event(HTML::MessageEvent::create(realm(), String::from_deprecated_string(HTML::EventNames::message).release_value_but_fixme_should_propagate_errors(), event_init).release_value_but_fixme_should_propagate_errors());
});
return {};
}
// https://html.spec.whatwg.org/multipage/structured-data.html#dom-structuredclone
WebIDL::ExceptionOr<JS::Value> Window::structured_clone_impl(JS::VM& vm, JS::Value message)
{
@ -1141,7 +1127,6 @@ WebIDL::ExceptionOr<void> Window::initialize_web_interfaces(Badge<WindowEnvironm
define_native_function(realm, "matchMedia", match_media, 1, attr);
define_native_function(realm, "getSelection", get_selection, 0, attr);
define_native_function(realm, "postMessage", post_message, 1, attr);
define_native_function(realm, "structuredClone", structured_clone, 1, attr);
define_native_function(realm, "fetch", Bindings::fetch, 1, attr);
@ -1257,6 +1242,19 @@ Optional<String> Window::prompt(Optional<String> const& message, Optional<String
return {};
}
// https://html.spec.whatwg.org/multipage/web-messaging.html#dom-window-postmessage
void Window::post_message(JS::Value message, String const&)
{
// FIXME: This is an ad-hoc hack implementation instead, since we don't currently
// have serialization and deserialization of messages.
HTML::queue_global_task(HTML::Task::Source::PostedMessage, *this, [this, message] {
HTML::MessageEventInit event_init {};
event_init.data = message;
event_init.origin = "<origin>"_string.release_value_but_fixme_should_propagate_errors();
dispatch_event(HTML::MessageEvent::create(realm(), String::from_deprecated_string(HTML::EventNames::message).release_value_but_fixme_should_propagate_errors(), event_init).release_value_but_fixme_should_propagate_errors());
});
}
JS_DEFINE_NATIVE_FUNCTION(Window::open)
{
auto* impl = TRY(impl_from(vm));
@ -1866,16 +1864,6 @@ JS_DEFINE_NATIVE_FUNCTION(Window::outer_height_getter)
return JS::Value(impl->outer_height());
}
JS_DEFINE_NATIVE_FUNCTION(Window::post_message)
{
auto* impl = TRY(impl_from(vm));
auto target_origin = TRY(vm.argument(1).to_deprecated_string(vm));
TRY(Bindings::throw_dom_exception_if_needed(vm, [&] {
return impl->post_message_impl(vm.argument(0), target_origin);
}));
return JS::js_undefined();
}
JS_DEFINE_NATIVE_FUNCTION(Window::structured_clone)
{
auto* impl = TRY(impl_from(vm));

View file

@ -119,7 +119,6 @@ public:
// https://html.spec.whatwg.org/multipage/browsers.html#dom-parent
WindowProxy* parent();
WebIDL::ExceptionOr<void> post_message_impl(JS::Value, DeprecatedString const& target_origin);
WebIDL::ExceptionOr<JS::Value> structured_clone_impl(JS::VM& vm, JS::Value);
DeprecatedString name() const;
@ -145,6 +144,8 @@ public:
bool confirm(Optional<String> const& message);
Optional<String> prompt(Optional<String> const& message, Optional<String> const& default_);
void post_message(JS::Value message, String const&);
private:
explicit Window(JS::Realm&);
@ -258,7 +259,6 @@ private:
JS_DECLARE_NATIVE_FUNCTION(outer_width_getter);
JS_DECLARE_NATIVE_FUNCTION(outer_height_getter);
JS_DECLARE_NATIVE_FUNCTION(post_message);
JS_DECLARE_NATIVE_FUNCTION(structured_clone);
JS_DECLARE_NATIVE_FUNCTION(local_storage_getter);

View file

@ -9,6 +9,10 @@ interface Window : EventTarget {
undefined alert(DOMString message);
boolean confirm(optional DOMString message = "");
DOMString? prompt(optional DOMString message = "", optional DOMString default = "");
undefined postMessage(any message, USVString targetOrigin);
// FIXME: undefined postMessage(any message, USVString targetOrigin, optional sequence<object> transfer = []);
// FIXME: undefined postMessage(any message, optional WindowPostMessageOptions options = {});
};
Window includes GlobalEventHandlers;
Window includes WindowEventHandlers;