Bug 1831122 [wpt PR 39823] - Update wpt metadata, a=testonly
[gecko.git] / dom / canvas / ImageData.cpp
blobb4a0a403e5d6e9d533f0c3828af9bce8c9c41d62
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim:set ts=2 sw=2 et tw=78: */
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 file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/dom/ImageData.h"
9 #include "ErrorList.h"
10 #include "js/StructuredClone.h"
11 #include "js/Value.h"
12 #include "jsapi.h"
13 #include "jsfriendapi.h"
14 #include "mozilla/CheckedInt.h"
15 #include "mozilla/ErrorResult.h"
16 #include "mozilla/HoldDropJSObjects.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/dom/BindingDeclarations.h"
19 #include "mozilla/dom/ImageDataBinding.h"
20 #include "nsCycleCollectionNoteChild.h"
22 namespace mozilla::dom {
24 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
25 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
27 NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
29 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
30 NS_INTERFACE_MAP_ENTRY(nsISupports)
31 NS_INTERFACE_MAP_END
33 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData)
34 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
35 NS_IMPL_CYCLE_COLLECTION_TRACE_END
37 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
38 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
40 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
41 tmp->DropData();
42 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
44 // static
45 already_AddRefed<ImageData> ImageData::Constructor(const GlobalObject& aGlobal,
46 const uint32_t aWidth,
47 const uint32_t aHeight,
48 ErrorResult& aRv) {
49 if (aWidth == 0 || aHeight == 0) {
50 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
51 return nullptr;
54 // Restrict the typed array length to INT32_MAX because that's all we support
55 // in dom::TypedArray::ComputeState.
56 CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
57 if (!length.isValid() || length.value() > INT32_MAX) {
58 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
59 return nullptr;
61 js::AssertSameCompartment(aGlobal.Context(), aGlobal.Get());
62 JSObject* data = Uint8ClampedArray::Create(aGlobal.Context(), length.value());
63 if (!data) {
64 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
65 return nullptr;
67 RefPtr<ImageData> imageData = new ImageData(aWidth, aHeight, *data);
68 return imageData.forget();
71 // static
72 already_AddRefed<ImageData> ImageData::Constructor(
73 const GlobalObject& aGlobal, const Uint8ClampedArray& aData,
74 const uint32_t aWidth, const Optional<uint32_t>& aHeight,
75 ErrorResult& aRv) {
76 aData.ComputeState();
78 uint32_t length = aData.Length();
79 if (length == 0 || length % 4) {
80 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
81 return nullptr;
83 length /= 4;
84 if (aWidth == 0) {
85 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
86 return nullptr;
88 uint32_t height = length / aWidth;
89 if (length != aWidth * height ||
90 (aHeight.WasPassed() && aHeight.Value() != height)) {
91 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
92 return nullptr;
94 RefPtr<ImageData> imageData = new ImageData(aWidth, height, *aData.Obj());
95 return imageData.forget();
98 void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
100 void ImageData::DropData() {
101 if (mData) {
102 mozilla::DropJSObjects(this);
106 bool ImageData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
107 JS::MutableHandle<JSObject*> aReflector) {
108 return ImageData_Binding::Wrap(aCx, this, aGivenProto, aReflector);
111 // static
112 already_AddRefed<ImageData> ImageData::ReadStructuredClone(
113 JSContext* aCx, nsIGlobalObject* aGlobal,
114 JSStructuredCloneReader* aReader) {
115 // Read the information out of the stream.
116 uint32_t width, height;
117 JS::Rooted<JS::Value> dataArray(aCx);
118 if (!JS_ReadUint32Pair(aReader, &width, &height) ||
119 !JS_ReadTypedArray(aReader, &dataArray)) {
120 return nullptr;
122 MOZ_ASSERT(dataArray.isObject());
124 RefPtr<ImageData> imageData =
125 new ImageData(width, height, dataArray.toObject());
126 return imageData.forget();
129 bool ImageData::WriteStructuredClone(JSContext* aCx,
130 JSStructuredCloneWriter* aWriter) const {
131 JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*GetDataObject()));
132 if (!JS_WrapValue(aCx, &arrayValue)) {
133 return false;
136 return JS_WriteUint32Pair(aWriter, Width(), Height()) &&
137 JS_WriteTypedArray(aWriter, arrayValue);
140 } // namespace mozilla::dom