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"
10 #include "js/StructuredClone.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
)
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
)
42 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
45 already_AddRefed
<ImageData
> ImageData::Constructor(const GlobalObject
& aGlobal
,
46 const uint32_t aWidth
,
47 const uint32_t aHeight
,
49 if (aWidth
== 0 || aHeight
== 0) {
50 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
54 // Restrict the typed array length to INT32_MAX because that's all we support.
55 CheckedInt
<uint32_t> length
= CheckedInt
<uint32_t>(aWidth
) * aHeight
* 4;
56 if (!length
.isValid() || length
.value() > INT32_MAX
) {
57 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
60 js::AssertSameCompartment(aGlobal
.Context(), aGlobal
.Get());
62 Uint8ClampedArray::Create(aGlobal
.Context(), length
.value(), aRv
);
66 RefPtr
<ImageData
> imageData
= new ImageData(aWidth
, aHeight
, *data
);
67 return imageData
.forget();
71 already_AddRefed
<ImageData
> ImageData::Constructor(
72 const GlobalObject
& aGlobal
, const Uint8ClampedArray
& aData
,
73 const uint32_t aWidth
, const Optional
<uint32_t>& aHeight
,
75 Maybe
<uint32_t> maybeLength
= aData
.ProcessData(
76 [&](const Span
<uint8_t>& aData
, JS::AutoCheckCannotGC
&& nogc
) {
77 return Some(aData
.Length());
79 uint32_t length
= maybeLength
.valueOr(0);
80 if (length
== 0 || length
% 4) {
81 aRv
.Throw(NS_ERROR_DOM_INVALID_STATE_ERR
);
86 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
89 uint32_t height
= length
/ aWidth
;
90 if (length
!= aWidth
* height
||
91 (aHeight
.WasPassed() && aHeight
.Value() != height
)) {
92 aRv
.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR
);
95 RefPtr
<ImageData
> imageData
= new ImageData(aWidth
, height
, *aData
.Obj());
96 return imageData
.forget();
99 void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
101 void ImageData::DropData() {
103 mozilla::DropJSObjects(this);
107 bool ImageData::WrapObject(JSContext
* aCx
, JS::Handle
<JSObject
*> aGivenProto
,
108 JS::MutableHandle
<JSObject
*> aReflector
) {
109 return ImageData_Binding::Wrap(aCx
, this, aGivenProto
, aReflector
);
113 already_AddRefed
<ImageData
> ImageData::ReadStructuredClone(
114 JSContext
* aCx
, nsIGlobalObject
* aGlobal
,
115 JSStructuredCloneReader
* aReader
) {
116 // Read the information out of the stream.
117 uint32_t width
, height
;
118 JS::Rooted
<JS::Value
> dataArray(aCx
);
119 if (!JS_ReadUint32Pair(aReader
, &width
, &height
) ||
120 !JS_ReadTypedArray(aReader
, &dataArray
)) {
123 MOZ_ASSERT(dataArray
.isObject());
125 RefPtr
<ImageData
> imageData
=
126 new ImageData(width
, height
, dataArray
.toObject());
127 return imageData
.forget();
130 bool ImageData::WriteStructuredClone(JSContext
* aCx
,
131 JSStructuredCloneWriter
* aWriter
) const {
132 JS::Rooted
<JS::Value
> arrayValue(aCx
, JS::ObjectValue(*GetDataObject()));
133 if (!JS_WrapValue(aCx
, &arrayValue
)) {
137 return JS_WriteUint32Pair(aWriter
, Width(), Height()) &&
138 JS_WriteTypedArray(aWriter
, arrayValue
);
141 } // namespace mozilla::dom