Line data Source code
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/. */
6 :
7 : #include "mozilla/dom/StructuredClone.h"
8 :
9 : #include "js/StructuredClone.h"
10 : #include "mozilla/dom/ImageData.h"
11 : #include "mozilla/dom/StructuredCloneTags.h"
12 :
13 : namespace mozilla {
14 : namespace dom {
15 :
16 : JSObject*
17 0 : ReadStructuredCloneImageData(JSContext* aCx, JSStructuredCloneReader* aReader)
18 : {
19 : // Read the information out of the stream.
20 : uint32_t width, height;
21 0 : JS::Rooted<JS::Value> dataArray(aCx);
22 0 : if (!JS_ReadUint32Pair(aReader, &width, &height) ||
23 0 : !JS_ReadTypedArray(aReader, &dataArray)) {
24 0 : return nullptr;
25 : }
26 0 : MOZ_ASSERT(dataArray.isObject());
27 :
28 : // Protect the result from a moving GC in ~nsRefPtr.
29 0 : JS::Rooted<JSObject*> result(aCx);
30 : {
31 : // Construct the ImageData.
32 : RefPtr<ImageData> imageData = new ImageData(width, height,
33 0 : dataArray.toObject());
34 : // Wrap it in a JS::Value.
35 0 : if (!imageData->WrapObject(aCx, nullptr, &result)) {
36 0 : return nullptr;
37 : }
38 : }
39 0 : return result;
40 : }
41 :
42 : bool
43 0 : WriteStructuredCloneImageData(JSContext* aCx, JSStructuredCloneWriter* aWriter,
44 : ImageData* aImageData)
45 : {
46 0 : uint32_t width = aImageData->Width();
47 0 : uint32_t height = aImageData->Height();
48 0 : JS::Rooted<JSObject*> dataArray(aCx, aImageData->GetDataObject());
49 :
50 0 : JSAutoCompartment ac(aCx, dataArray);
51 0 : JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*dataArray));
52 0 : return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) &&
53 0 : JS_WriteUint32Pair(aWriter, width, height) &&
54 0 : JS_WriteTypedArray(aWriter, arrayValue);
55 : }
56 :
57 : } // namespace dom
58 : } // namespace mozilla
|