Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/bindings/StructuredClone.cpp
Line
Count
Source (jump to first uncovered line)
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
ReadStructuredCloneImageData(JSContext* aCx, JSStructuredCloneReader* aReader)
18
0
{
19
0
  // Read the information out of the stream.
20
0
  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
0
  }
26
0
  MOZ_ASSERT(dataArray.isObject());
27
0
28
0
  // Protect the result from a moving GC in ~nsRefPtr.
29
0
  JS::Rooted<JSObject*> result(aCx);
30
0
  {
31
0
    // Construct the ImageData.
32
0
    RefPtr<ImageData> imageData = new ImageData(width, height,
33
0
                                                  dataArray.toObject());
34
0
    // Wrap it in a JS::Value.
35
0
    if (!imageData->WrapObject(aCx, nullptr, &result)) {
36
0
      return nullptr;
37
0
    }
38
0
  }
39
0
  return result;
40
0
}
41
42
bool
43
WriteStructuredCloneImageData(JSContext* aCx, JSStructuredCloneWriter* aWriter,
44
                              ImageData* aImageData)
45
0
{
46
0
  uint32_t width = aImageData->Width();
47
0
  uint32_t height = aImageData->Height();
48
0
49
0
  JS::Rooted<JSObject*> dataArray(aCx, aImageData->GetDataObject());
50
0
  if (!JS_WrapObject(aCx, &dataArray)) {
51
0
    return false;
52
0
  }
53
0
54
0
  JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*dataArray));
55
0
  return JS_WriteUint32Pair(aWriter, SCTAG_DOM_IMAGEDATA, 0) &&
56
0
         JS_WriteUint32Pair(aWriter, width, height) &&
57
0
         JS_WriteTypedArray(aWriter, arrayValue);
58
0
}
59
60
} // namespace dom
61
} // namespace mozilla