diff --git a/dom/base/DOMPoint.cpp b/dom/base/DOMPoint.cpp index 7d45925044e6..846bd8b1dcfb 100644 --- a/dom/base/DOMPoint.cpp +++ b/dom/base/DOMPoint.cpp @@ -39,7 +39,7 @@ JSObject* DOMPointReadOnly::WrapObject(JSContext* aCx, // https://drafts.fxtf.org/geometry/#structured-serialization bool DOMPointReadOnly::WriteStructuredClone( - JSStructuredCloneWriter* aWriter) const { + JSContext* aCx, JSStructuredCloneWriter* aWriter) const { #define WriteDouble(d) \ JS_WriteUint32Pair(aWriter, (BitwiseCast(d) >> 32) & 0xffffffff, \ BitwiseCast(d) & 0xffffffff) @@ -50,6 +50,18 @@ bool DOMPointReadOnly::WriteStructuredClone( #undef WriteDouble } +// static +already_AddRefed DOMPointReadOnly::ReadStructuredClone( + JSContext* aCx, nsIGlobalObject* aGlobal, + JSStructuredCloneReader* aReader) { + RefPtr retval = new DOMPointReadOnly(aGlobal); + if (!retval->ReadStructuredClone(aReader)) { + return nullptr; + } + return retval.forget(); + ; +} + bool DOMPointReadOnly::ReadStructuredClone(JSStructuredCloneReader* aReader) { uint32_t high; uint32_t low; @@ -66,7 +78,6 @@ bool DOMPointReadOnly::ReadStructuredClone(JSStructuredCloneReader* aReader) { ReadDouble(&mW); return true; - #undef ReadDouble } @@ -89,3 +100,15 @@ JSObject* DOMPoint::WrapObject(JSContext* aCx, JS::Handle aGivenProto) { return DOMPoint_Binding::Wrap(aCx, this, aGivenProto); } + +// static +already_AddRefed DOMPoint::ReadStructuredClone( + JSContext* aCx, nsIGlobalObject* aGlobal, + JSStructuredCloneReader* aReader) { + RefPtr retval = new DOMPoint(aGlobal); + if (!retval->ReadStructuredClone(aReader)) { + return nullptr; + } + return retval.forget(); + ; +} diff --git a/dom/base/DOMPoint.h b/dom/base/DOMPoint.h index 13bafac4f83b..0064f1db0410 100644 --- a/dom/base/DOMPoint.h +++ b/dom/base/DOMPoint.h @@ -16,6 +16,8 @@ #include "nsCOMPtr.h" #include "mozilla/dom/BindingDeclarations.h" +class nsIGlobalObject; + namespace mozilla { namespace dom { @@ -46,13 +48,20 @@ class DOMPointReadOnly : public nsWrapperCache { virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; - bool WriteStructuredClone(JSStructuredCloneWriter* aWriter) const; + bool WriteStructuredClone(JSContext* aCx, + JSStructuredCloneWriter* aWriter) const; - bool ReadStructuredClone(JSStructuredCloneReader* aReader); + static already_AddRefed ReadStructuredClone( + JSContext* aCx, nsIGlobalObject* aGlobal, + JSStructuredCloneReader* aReader); protected: virtual ~DOMPointReadOnly() {} + // Shared implementation of ReadStructuredClone for DOMPoint and + // DOMPointReadOnly. + bool ReadStructuredClone(JSStructuredCloneReader* aReader); + nsCOMPtr mParent; double mX, mY, mZ, mW; }; @@ -72,6 +81,11 @@ class DOMPoint final : public DOMPointReadOnly { virtual JSObject* WrapObject(JSContext* aCx, JS::Handle aGivenProto) override; + static already_AddRefed ReadStructuredClone( + JSContext* aCx, nsIGlobalObject* aGlobal, + JSStructuredCloneReader* aReader); + using DOMPointReadOnly::ReadStructuredClone; + void SetX(double aX) { mX = aX; } void SetY(double aY) { mY = aY; } void SetZ(double aZ) { mZ = aZ; } diff --git a/dom/base/DOMQuad.cpp b/dom/base/DOMQuad.cpp index 2592de8cb1ea..856a46a5eca8 100644 --- a/dom/base/DOMQuad.cpp +++ b/dom/base/DOMQuad.cpp @@ -117,7 +117,7 @@ void DOMQuad::ToJSON(DOMQuadJSON& aInit) { bool DOMQuad::WriteStructuredClone(JSContext* aCx, JSStructuredCloneWriter* aWriter) const { for (const auto& point : mPoints) { - if (!point->WriteStructuredClone(aWriter)) { + if (!point->WriteStructuredClone(aCx, aWriter)) { return false; } } @@ -130,8 +130,8 @@ already_AddRefed DOMQuad::ReadStructuredClone( JSStructuredCloneReader* aReader) { RefPtr quad = new DOMQuad(aGlobal); for (auto& point : quad->mPoints) { - point = new DOMPoint(aGlobal); - if (!point->ReadStructuredClone(aReader)) { + point = DOMPoint::ReadStructuredClone(aCx, aGlobal, aReader); + if (!point) { return nullptr; } } diff --git a/dom/base/StructuredCloneHolder.cpp b/dom/base/StructuredCloneHolder.cpp index e4cc693faef5..e4fbefd0eaab 100644 --- a/dom/base/StructuredCloneHolder.cpp +++ b/dom/base/StructuredCloneHolder.cpp @@ -16,8 +16,6 @@ #include "mozilla/dom/DirectoryBinding.h" #include "mozilla/dom/DOMMatrix.h" #include "mozilla/dom/DOMMatrixBinding.h" -#include "mozilla/dom/DOMPoint.h" -#include "mozilla/dom/DOMPointBinding.h" #include "mozilla/dom/DOMRect.h" #include "mozilla/dom/DOMRectBinding.h" #include "mozilla/dom/File.h" @@ -128,7 +126,7 @@ void StructuredCloneCallbacksError(JSContext* aCx, uint32_t aErrorId) { void AssertTagValues() { static_assert(SCTAG_DOM_IMAGEDATA == 0xffff8007 && SCTAG_DOM_DOMPOINT == 0xffff8008 && - SCTAG_DOM_DOMPOINT_READONLY == 0xffff8009 && + SCTAG_DOM_DOMPOINTREADONLY == 0xffff8009 && SCTAG_DOM_WEBCRYPTO_KEY == 0xffff800a && SCTAG_DOM_NULL_PRINCIPAL == 0xffff800b && SCTAG_DOM_SYSTEM_PRINCIPAL == 0xffff800c && @@ -358,7 +356,6 @@ JSObject* StructuredCloneHolder::ReadFullySerializableObjects( } if (aTag == SCTAG_DOM_WEBCRYPTO_KEY || aTag == SCTAG_DOM_URLSEARCHPARAMS || - aTag == SCTAG_DOM_DOMPOINT || aTag == SCTAG_DOM_DOMPOINT_READONLY || aTag == SCTAG_DOM_DOMRECT || aTag == SCTAG_DOM_DOMRECT_READONLY || aTag == SCTAG_DOM_DOMMATRIX || aTag == SCTAG_DOM_DOMMATRIX_READONLY) { // Prevent the return value from being trashed by a GC during ~nsRefPtr. @@ -378,20 +375,6 @@ JSObject* StructuredCloneHolder::ReadFullySerializableObjects( } else { result = usp->WrapObject(aCx, nullptr); } - } else if (aTag == SCTAG_DOM_DOMPOINT) { - RefPtr domPoint = new DOMPoint(global); - if (!domPoint->ReadStructuredClone(aReader)) { - result = nullptr; - } else { - result = domPoint->WrapObject(aCx, nullptr); - } - } else if (aTag == SCTAG_DOM_DOMPOINT_READONLY) { - RefPtr domPoint = new DOMPointReadOnly(global); - if (!domPoint->ReadStructuredClone(aReader)) { - result = nullptr; - } else { - result = domPoint->WrapObject(aCx, nullptr); - } } else if (aTag == SCTAG_DOM_DOMRECT) { RefPtr domRect = new DOMRect(global); if (!domRect->ReadStructuredClone(aReader)) { @@ -533,26 +516,6 @@ bool StructuredCloneHolder::WriteFullySerializableObjects( } #endif - // Handle DOMPoint cloning - // Should be done before DOMPointeReadOnly check - // because every DOMPoint is also a DOMPointReadOnly - { - DOMPoint* domPoint = nullptr; - if (NS_SUCCEEDED(UNWRAP_OBJECT(DOMPoint, &obj, domPoint))) { - return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DOMPOINT, 0) && - domPoint->WriteStructuredClone(aWriter); - } - } - - // Handle DOMPointReadOnly cloning - { - DOMPointReadOnly* domPoint = nullptr; - if (NS_SUCCEEDED(UNWRAP_OBJECT(DOMPointReadOnly, &obj, domPoint))) { - return JS_WriteUint32Pair(aWriter, SCTAG_DOM_DOMPOINT_READONLY, 0) && - domPoint->WriteStructuredClone(aWriter); - } - } - // Handle DOMRect cloning // Should be done before DOMRecteReadOnly check // because every DOMRect is also a DOMRectReadOnly diff --git a/dom/base/StructuredCloneTags.h b/dom/base/StructuredCloneTags.h index 91dfae871cc2..21a530060f05 100644 --- a/dom/base/StructuredCloneTags.h +++ b/dom/base/StructuredCloneTags.h @@ -46,7 +46,7 @@ enum StructuredCloneTags { SCTAG_DOM_IMAGEDATA, SCTAG_DOM_DOMPOINT, - SCTAG_DOM_DOMPOINT_READONLY, + SCTAG_DOM_DOMPOINTREADONLY, // IMPORTANT: Don't change the order of these enum values. You could break // IDB. diff --git a/dom/webidl/DOMPoint.webidl b/dom/webidl/DOMPoint.webidl index 1603253a696f..b2959579f2fc 100644 --- a/dom/webidl/DOMPoint.webidl +++ b/dom/webidl/DOMPoint.webidl @@ -13,7 +13,8 @@ [Pref="layout.css.DOMPoint.enabled", Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1), - Exposed=(Window,Worker)] + Exposed=(Window,Worker), + Serializable] interface DOMPointReadOnly { [NewObject] static DOMPointReadOnly fromPoint(optional DOMPointInit other); @@ -28,7 +29,8 @@ interface DOMPointReadOnly { [Pref="layout.css.DOMPoint.enabled", Constructor(optional unrestricted double x = 0, optional unrestricted double y = 0, optional unrestricted double z = 0, optional unrestricted double w = 1), - Exposed=(Window,Worker)] + Exposed=(Window,Worker), + Serializable] interface DOMPoint : DOMPointReadOnly { [NewObject] static DOMPoint fromPoint(optional DOMPointInit other);