From 4174037d8a39b465ccd8afd91632596dc26a9a1d Mon Sep 17 00:00:00 2001 From: FranklinDM Date: Fri, 7 Apr 2023 16:02:34 +0800 Subject: [PATCH] Issue #2197 - Part 2d: Implement PostMessageOptions for Window Based on https://bugzilla.mozilla.org/show_bug.cgi?id=1502802 --- dom/base/nsGlobalWindow.cpp | 11 +++++++++++ dom/base/nsGlobalWindow.h | 6 ++++++ .../mochitest/whatwg/test_postMessage_origin.xhtml | 7 ------- dom/webidl/Window.webidl | 6 ++++++ 4 files changed, 23 insertions(+), 7 deletions(-) diff --git a/dom/base/nsGlobalWindow.cpp b/dom/base/nsGlobalWindow.cpp index 7c0256a2b7..c2aa8137f6 100644 --- a/dom/base/nsGlobalWindow.cpp +++ b/dom/base/nsGlobalWindow.cpp @@ -8904,6 +8904,17 @@ nsGlobalWindow::PostMessageMoz(JSContext* aCx, aSubjectPrincipal, aRv); } +void +nsGlobalWindow::PostMessageMoz(JSContext* aCx, + JS::Handle aMessage, + const WindowPostMessageOptions& aOptions, + nsIPrincipal& aSubjectPrincipal, + ErrorResult& aRv) +{ + PostMessageMoz(aCx, aMessage, aOptions.mTargetOrigin, aOptions.mTransfer, + aSubjectPrincipal, aRv); +} + class nsCloseEvent : public Runnable { RefPtr mWindow; diff --git a/dom/base/nsGlobalWindow.h b/dom/base/nsGlobalWindow.h index 8c961e2014..9a97a5e92a 100644 --- a/dom/base/nsGlobalWindow.h +++ b/dom/base/nsGlobalWindow.h @@ -135,6 +135,7 @@ class TabGroup; class Timeout; class U2F; class WakeLock; +struct WindowPostMessageOptions; class Worklet; namespace cache { class CacheStorage; @@ -936,6 +937,11 @@ public: const mozilla::dom::Sequence& aTransfer, nsIPrincipal& aSubjectPrincipal, mozilla::ErrorResult& aRv); + void PostMessageMoz(JSContext* aCx, + JS::Handle aMessage, + const mozilla::dom::WindowPostMessageOptions& aOptions, + nsIPrincipal& aSubjectPrincipal, + mozilla::ErrorResult& aError); int32_t SetTimeout(JSContext* aCx, mozilla::dom::Function& aFunction, int32_t aTimeout, const mozilla::dom::Sequence& aArguments, diff --git a/dom/tests/mochitest/whatwg/test_postMessage_origin.xhtml b/dom/tests/mochitest/whatwg/test_postMessage_origin.xhtml index f6a9198968..e48bee136d 100644 --- a/dom/tests/mochitest/whatwg/test_postMessage_origin.xhtml +++ b/dom/tests/mochitest/whatwg/test_postMessage_origin.xhtml @@ -383,13 +383,6 @@ var tests = hasWrongReturnOriginBug: true }, - // 55 - { - args: ["NOT-RECEIVED", undefined], - source: "sameDomain", - name: "SyntaxError", - code: DOMException.SYNTAX_ERR - }, ]; function allTests(callback) diff --git a/dom/webidl/Window.webidl b/dom/webidl/Window.webidl index 73d5843a16..59cc9689c2 100644 --- a/dom/webidl/Window.webidl +++ b/dom/webidl/Window.webidl @@ -80,6 +80,8 @@ interface nsIDOMCrypto; [Throws, CrossOriginCallable, NeedsSubjectPrincipal] void postMessage(any message, DOMString targetOrigin, optional sequence transfer = []); + [Throws, CrossOriginCallable, NeedsSubjectPrincipal] + void postMessage(any message, optional WindowPostMessageOptions options); // also has obsolete members }; @@ -485,3 +487,7 @@ callback IdleRequestCallback = void (IdleDeadline deadline); partial interface Window { [ChromeOnly] readonly attribute boolean isSecureContextIfOpenerIgnored; }; + +dictionary WindowPostMessageOptions : StructuredSerializeOptions { + USVString targetOrigin = "/"; +};