content/canvas/src/ImageData.cpp

branch
TOR_BUG_9701
changeset 11
deefc01c0e14
equal deleted inserted replaced
-1:000000000000 0:d771314e02f9
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/. */
6
7 #include "mozilla/dom/ImageData.h"
8
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/HoldDropJSObjects.h"
11 #include "mozilla/dom/ImageDataBinding.h"
12
13 #include "jsapi.h"
14
15 namespace mozilla {
16 namespace dom {
17
18 NS_IMPL_CYCLE_COLLECTING_ADDREF(ImageData)
19 NS_IMPL_CYCLE_COLLECTING_RELEASE(ImageData)
20
21 NS_IMPL_CYCLE_COLLECTION_CLASS(ImageData)
22
23 NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(ImageData)
24 NS_INTERFACE_MAP_ENTRY(nsISupports)
25 NS_INTERFACE_MAP_END
26
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
30
31 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(ImageData)
32 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_SCRIPT_OBJECTS
33 NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END
34
35 NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(ImageData)
36 tmp->DropData();
37 NS_IMPL_CYCLE_COLLECTION_UNLINK_END
38
39 //static
40 ImageData*
41 ImageData::Constructor(const GlobalObject& aGlobal,
42 const uint32_t aWidth,
43 const uint32_t aHeight,
44 ErrorResult& aRv)
45 {
46 if (aWidth == 0 || aHeight == 0) {
47 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
48 return nullptr;
49 }
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;
54 }
55 js::AssertSameCompartment(aGlobal.GetContext(), aGlobal.Get());
56 JSObject* data = Uint8ClampedArray::Create(aGlobal.GetContext(),
57 length.value());
58 if (!data) {
59 aRv.Throw(NS_ERROR_OUT_OF_MEMORY);
60 return nullptr;
61 }
62 return new ImageData(aWidth, aHeight, *data);
63 }
64
65 //static
66 ImageData*
67 ImageData::Constructor(const GlobalObject& aGlobal,
68 const Uint8ClampedArray& aData,
69 const uint32_t aWidth,
70 const Optional<uint32_t>& aHeight,
71 ErrorResult& aRv)
72 {
73 aData.ComputeLengthAndData();
74
75 uint32_t length = aData.Length();
76 if (length == 0 || length % 4) {
77 aRv.Throw(NS_ERROR_DOM_INVALID_STATE_ERR);
78 return nullptr;
79 }
80 length /= 4;
81 if (aWidth == 0) {
82 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
83 return nullptr;
84 }
85 uint32_t height = length / aWidth;
86 if (length != aWidth * height ||
87 (aHeight.WasPassed() && aHeight.Value() != height)) {
88 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR);
89 return nullptr;
90 }
91 return new ImageData(aWidth, height, *aData.Obj());
92 }
93
94 void
95 ImageData::HoldData()
96 {
97 mozilla::HoldJSObjects(this);
98 }
99
100 void
101 ImageData::DropData()
102 {
103 if (mData) {
104 mData = nullptr;
105 mozilla::DropJSObjects(this);
106 }
107 }
108
109 JSObject*
110 ImageData::WrapObject(JSContext* cx)
111 {
112 return ImageDataBinding::Wrap(cx, this);
113 }
114
115 } // namespace dom
116 } // namespace mozilla

mercurial