1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/StructuredClone.h"
9 #include "js/StructuredClone.h"
10 #include "mozilla/dom/ImageData.h"
11 #include "mozilla/dom/StructuredCloneTags.h"
17 ReadStructuredCloneImageData(JSContext
* aCx
, JSStructuredCloneReader
* aReader
)
19 // Read the information out of the stream.
20 uint32_t width
, height
;
21 JS::Rooted
<JS::Value
> dataArray(aCx
);
22 if (!JS_ReadUint32Pair(aReader
, &width
, &height
) ||
23 !JS_ReadTypedArray(aReader
, &dataArray
)) {
26 MOZ_ASSERT(dataArray
.isObject());
28 // Protect the result from a moving GC in ~nsRefPtr.
29 JS::Rooted
<JSObject
*> result(aCx
);
31 // Construct the ImageData.
32 nsRefPtr
<ImageData
> imageData
= new ImageData(width
, height
,
33 dataArray
.toObject());
34 // Wrap it in a JS::Value.
35 result
= imageData
->WrapObject(aCx
);
41 WriteStructuredCloneImageData(JSContext
* aCx
, JSStructuredCloneWriter
* aWriter
,
42 ImageData
* aImageData
)
44 uint32_t width
= aImageData
->Width();
45 uint32_t height
= aImageData
->Height();
46 JS::Rooted
<JSObject
*> dataArray(aCx
, aImageData
->GetDataObject());
48 JSAutoCompartment
ac(aCx
, dataArray
);
49 JS::Rooted
<JS::Value
> arrayValue(aCx
, JS::ObjectValue(*dataArray
));
50 return JS_WriteUint32Pair(aWriter
, SCTAG_DOM_IMAGEDATA
, 0) &&
51 JS_WriteUint32Pair(aWriter
, width
, height
) &&
52 JS_WriteTypedArray(aWriter
, arrayValue
);
56 } // namespace mozilla