Bumping manifests a=b2g-bump
[gecko.git] / dom / canvas / ImageData.cpp
blob1f640028d579326d9ab1c79bb494bb6195665656
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_SCRIPT_OBJECTS
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
36 tmp->DropData();
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
39 //static
40 already_AddRefed<ImageData>
41 ImageData::Constructor(const GlobalObject& aGlobal,
42 const uint32_t aWidth,
43 const uint32_t aHeight,
44 ErrorResult& aRv)
46 if (aWidth == 0 || aHeight == 0) {
47 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
48 return nullptr;
50 CheckedInt<uint32_t> length = CheckedInt<uint32_t>(aWidth) * aHeight * 4;
51 if (!length.isValid()) {
52 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
53 return nullptr;
55 js::AssertSameCompartment(aGlobal.Context(), aGlobal.Get());
56 JSObject* data = Uint8ClampedArray::Create(aGlobal.Context(),
57 length.value());
58 if (!data) {
59 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
60 return nullptr;
62 nsRefPtr<ImageData> imageData = new ImageData(aWidth, aHeight, *data);
63 return imageData.forget();
66 //static
67 already_AddRefed<ImageData>
68 ImageData::Constructor(const GlobalObject& aGlobal,
69 const Uint8ClampedArray& aData,
70 const uint32_t aWidth,
71 const Optional<uint32_t>& aHeight,
72 ErrorResult& aRv)
74 aData.ComputeLengthAndData();
76 uint32_t length = aData.Length();
77 if (length == 0 || length % 4) {
78 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
79 return nullptr;
81 length /= 4;
82 if (aWidth == 0) {
83 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
84 return nullptr;
86 uint32_t height = length / aWidth;
87 if (length != aWidth * height ||
88 (aHeight.WasPassed() && aHeight.Value() != height)) {
89 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
90 return nullptr;
92 nsRefPtr<ImageData> imageData = new ImageData(aWidth, height, *aData.Obj());
93 return imageData.forget();
96 void
97 ImageData::HoldData()
99 mozilla::HoldJSObjects(this);
102 void
103 ImageData::DropData()
105 if (mData) {
106 mData = nullptr;
107 mozilla::DropJSObjects(this);
111 JSObject*
112 ImageData::WrapObject(JSContext* cx)
114 return ImageDataBinding::Wrap(cx, this);
117 } // namespace dom
118 } // namespace mozilla