diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp index db0aee4847..8e88543733 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.cpp @@ -95,6 +95,8 @@ void WindowObject::initialize_global_object() define_native_function("matchMedia", match_media, 1, attr); define_native_function("getSelection", get_selection, 0, attr); + define_native_function("postMessage", post_message, 1, attr); + // FIXME: These properties should be [Replaceable] according to the spec, but [Writable+Configurable] is the closest we have. define_native_accessor("scrollX", scroll_x_getter, {}, attr); define_native_accessor("pageXOffset", scroll_x_getter, {}, attr); @@ -641,6 +643,14 @@ JS_DEFINE_NATIVE_FUNCTION(WindowObject::screen_y_getter) return JS::Value(impl->screen_y()); } +JS_DEFINE_NATIVE_FUNCTION(WindowObject::post_message) +{ + auto* impl = TRY(impl_from(vm, global_object)); + auto target_origin = TRY(vm.argument(1).to_string(global_object)); + impl->post_message(vm.argument(0), target_origin); + return JS::js_undefined(); +} + JS_DEFINE_NATIVE_FUNCTION(WindowObject::local_storage_getter) { auto* impl = TRY(impl_from(vm, global_object)); diff --git a/Userland/Libraries/LibWeb/Bindings/WindowObject.h b/Userland/Libraries/LibWeb/Bindings/WindowObject.h index 866d89a407..645d2992f0 100644 --- a/Userland/Libraries/LibWeb/Bindings/WindowObject.h +++ b/Userland/Libraries/LibWeb/Bindings/WindowObject.h @@ -94,6 +94,8 @@ private: JS_DECLARE_NATIVE_FUNCTION(screen_left_getter); JS_DECLARE_NATIVE_FUNCTION(screen_top_getter); + JS_DECLARE_NATIVE_FUNCTION(post_message); + JS_DECLARE_NATIVE_FUNCTION(local_storage_getter); JS_DECLARE_NATIVE_FUNCTION(alert); diff --git a/Userland/Libraries/LibWeb/DOM/Window.cpp b/Userland/Libraries/LibWeb/DOM/Window.cpp index 711c1b8ac8..6312df3e3a 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.cpp +++ b/Userland/Libraries/LibWeb/DOM/Window.cpp @@ -19,6 +19,7 @@ #include #include #include +#include #include #include #include @@ -476,4 +477,18 @@ Window* Window::parent() return ¤t->active_document()->window(); } +// https://html.spec.whatwg.org/multipage/web-messaging.html#window-post-message-steps +DOM::ExceptionOr 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, *wrapper(), [strong_this = NonnullRefPtr(*this), message]() mutable { + HTML::MessageEventInit event_init {}; + event_init.data = message; + event_init.origin = ""; + strong_this->dispatch_event(HTML::MessageEvent::create(HTML::EventNames::message, event_init)); + }); + return {}; +} + } diff --git a/Userland/Libraries/LibWeb/DOM/Window.h b/Userland/Libraries/LibWeb/DOM/Window.h index 21cc82abbd..d9e0bc9c33 100644 --- a/Userland/Libraries/LibWeb/DOM/Window.h +++ b/Userland/Libraries/LibWeb/DOM/Window.h @@ -102,6 +102,8 @@ public: Window* parent(); + DOM::ExceptionOr post_message(JS::Value, String const& target_origin); + private: explicit Window(Document&);