StructuredClone.cpp 1.8 KB

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