Bug 1610775 [wpt PR 21336] - Update urllib3 to 1.25.8, a=testonly
[gecko.git] / dom / canvas / ImageData.cpp
blobaafecc27d4c4459aef124b4c34585870aaa79450
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 "mozilla/CheckedInt.h"
10 #include "mozilla/HoldDropJSObjects.h"
11 #include "mozilla/dom/ImageDataBinding.h"
13 #include "jsapi.h"
15 namespace mozilla {
16 namespace dom {
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
21 NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
24 NS_INTERFACE_MAP_ENTRY(nsISupports)
25 NS_INTERFACE_MAP_END
27 NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(ImageData)
28 NS_IMPL_CYCLE_COLLECTION_TRACE_JS_MEMBER_CALLBACK(mData)
29 NS_IMPL_CYCLE_COLLECTION_TRACE_END
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
34 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
35 tmp->DropData();
36 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
38 // static
39 already_AddRefed<ImageData> ImageData::Constructor(const GlobalObject& aGlobal,
40 const uint32_t aWidth,
41 const uint32_t aHeight,
42 ErrorResult& aRv) {
43 if (aWidth == 0 || aHeight == 0) {
44 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
45 return nullptr;
47 CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
48 if (!length.isValid()) {
49 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
50 return nullptr;
52 js::AssertSameCompartment(aGlobal.Context(), aGlobal.Get());
53 JSObject* data = Uint8ClampedArray::Create(aGlobal.Context(), length.value());
54 if (!data) {
55 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
56 return nullptr;
58 RefPtr<ImageData> imageData = new ImageData(aWidth, aHeight, *data);
59 return imageData.forget();
62 // static
63 already_AddRefed<ImageData> ImageData::Constructor(
64 const GlobalObject& aGlobal, const Uint8ClampedArray& aData,
65 const uint32_t aWidth, const Optional<uint32_t>& aHeight,
66 ErrorResult& aRv) {
67 aData.ComputeState();
69 uint32_t length = aData.Length();
70 if (length == 0 || length % 4) {
71 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
72 return nullptr;
74 length /= 4;
75 if (aWidth == 0) {
76 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
77 return nullptr;
79 uint32_t height = length / aWidth;
80 if (length != aWidth * height ||
81 (aHeight.WasPassed() && aHeight.Value() != height)) {
82 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
83 return nullptr;
85 RefPtr<ImageData> imageData = new ImageData(aWidth, height, *aData.Obj());
86 return imageData.forget();
89 void ImageData::HoldData() { mozilla::HoldJSObjects(this); }
91 void ImageData::DropData() {
92 if (mData) {
93 mData = nullptr;
94 mozilla::DropJSObjects(this);
98 bool ImageData::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto,
99 JS::MutableHandle<JSObject*> aReflector) {
100 return ImageData_Binding::Wrap(aCx, this, aGivenProto, aReflector);
103 // static
104 already_AddRefed<ImageData> ImageData::ReadStructuredClone(
105 JSContext* aCx, nsIGlobalObject* aGlobal,
106 JSStructuredCloneReader* aReader) {
107 // Read the information out of the stream.
108 uint32_t width, height;
109 JS::Rooted<JS::Value> dataArray(aCx);
110 if (!JS_ReadUint32Pair(aReader, &width, &height) ||
111 !JS_ReadTypedArray(aReader, &dataArray)) {
112 return nullptr;
114 MOZ_ASSERT(dataArray.isObject());
116 RefPtr<ImageData> imageData =
117 new ImageData(width, height, dataArray.toObject());
118 return imageData.forget();
121 bool ImageData::WriteStructuredClone(JSContext* aCx,
122 JSStructuredCloneWriter* aWriter) const {
123 JS::Rooted<JS::Value> arrayValue(aCx, JS::ObjectValue(*GetDataObject()));
124 if (!JS_WrapValue(aCx, &arrayValue)) {
125 return false;
128 return JS_WriteUint32Pair(aWriter, Width(), Height()) &&
129 JS_WriteTypedArray(aWriter, arrayValue);
132 } // namespace dom
133 } // namespace mozilla