Bumping manifests a=b2g-bump
[gecko.git] / dom / canvas / CanvasUtils.h
blobba72c0da8ecb2cc311489f0c5b6bc89d119b556d
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 #ifndef _CANVASUTILS_H_
7 #define _CANVASUTILS_H_
9 #include "mozilla/CheckedInt.h"
10 #include "mozilla/dom/ToJSValue.h"
11 #include "jsapi.h"
13 class nsIPrincipal;
15 namespace mozilla {
17 namespace gfx {
18 class Matrix;
21 namespace dom {
22 class HTMLCanvasElement;
25 namespace CanvasUtils {
28 // Check that the rectangle [x,y,w,h] is a subrectangle of [0,0,realWidth,realHeight]
30 inline bool CheckSaneSubrectSize(int32_t x, int32_t y, int32_t w, int32_t h,
31 int32_t realWidth, int32_t realHeight) {
32 CheckedInt32 checked_xmost = CheckedInt32(x) + w;
33 CheckedInt32 checked_ymost = CheckedInt32(y) + h;
35 return w >= 0 && h >= 0 && x >= 0 && y >= 0 &&
36 checked_xmost.isValid() &&
37 checked_xmost.value() <= realWidth &&
38 checked_ymost.isValid() &&
39 checked_ymost.value() <= realHeight;
42 // Flag aCanvasElement as write-only if drawing an image with aPrincipal
43 // onto it would make it such.
45 void DoDrawImageSecurityCheck(dom::HTMLCanvasElement *aCanvasElement,
46 nsIPrincipal *aPrincipal,
47 bool forceWriteOnly,
48 bool CORSUsed);
50 // Make a double out of |v|, treating undefined values as 0.0 (for
51 // the sake of sparse arrays). Return true iff coercion
52 // succeeded.
53 bool CoerceDouble(JS::Value v, double* d);
55 /* Float validation stuff */
56 #define VALIDATE(_f) if (!NS_finite(_f)) return false
58 inline bool FloatValidate (double f1) {
59 VALIDATE(f1);
60 return true;
63 inline bool FloatValidate (double f1, double f2) {
64 VALIDATE(f1); VALIDATE(f2);
65 return true;
68 inline bool FloatValidate (double f1, double f2, double f3) {
69 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3);
70 return true;
73 inline bool FloatValidate (double f1, double f2, double f3, double f4) {
74 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4);
75 return true;
78 inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5) {
79 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5);
80 return true;
83 inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5, double f6) {
84 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5); VALIDATE(f6);
85 return true;
88 #undef VALIDATE
90 template<typename T>
91 nsresult
92 JSValToDashArray(JSContext* cx, const JS::Value& val,
93 FallibleTArray<T>& dashArray);
95 template<typename T>
96 JS::Value
97 DashArrayToJSVal(FallibleTArray<T>& dashArray,
98 JSContext* cx, mozilla::ErrorResult& rv);
100 template<typename T>
101 nsresult
102 JSValToDashArray(JSContext* cx, const JS::Value& patternArray,
103 FallibleTArray<T>& dashes)
105 // The cap is pretty arbitrary. 16k should be enough for
106 // anybody...
107 static const uint32_t MAX_NUM_DASHES = 1 << 14;
109 if (!patternArray.isPrimitive()) {
110 JS::Rooted<JSObject*> obj(cx, patternArray.toObjectOrNull());
111 uint32_t length;
112 if (!JS_GetArrayLength(cx, obj, &length)) {
113 // Not an array-like thing
114 return NS_ERROR_INVALID_ARG;
115 } else if (length > MAX_NUM_DASHES) {
116 // Too many dashes in the pattern
117 return NS_ERROR_ILLEGAL_VALUE;
120 bool haveNonzeroElement = false;
121 for (uint32_t i = 0; i < length; ++i) {
122 JS::Rooted<JS::Value> elt(cx);
123 double d;
124 if (!JS_GetElement(cx, obj, i, &elt)) {
125 return NS_ERROR_FAILURE;
127 if (!(CoerceDouble(elt, &d) &&
128 FloatValidate(d) &&
129 d >= 0.0)) {
130 // Pattern elements must be finite "numbers" >= 0.
131 return NS_ERROR_INVALID_ARG;
132 } else if (d > 0.0) {
133 haveNonzeroElement = true;
135 if (!dashes.AppendElement(d)) {
136 return NS_ERROR_OUT_OF_MEMORY;
140 if (dashes.Length() > 0 && !haveNonzeroElement) {
141 // An all-zero pattern makes no sense.
142 return NS_ERROR_ILLEGAL_VALUE;
144 } else if (!(patternArray.isUndefined() || patternArray.isNull())) {
145 // undefined and null mean "reset to no dash". Any other
146 // random garbage is a type error.
147 return NS_ERROR_INVALID_ARG;
150 return NS_OK;
153 template<typename T>
154 void
155 DashArrayToJSVal(FallibleTArray<T>& dashes,
156 JSContext* cx,
157 JS::MutableHandle<JS::Value> retval,
158 mozilla::ErrorResult& rv)
160 if (dashes.IsEmpty()) {
161 retval.setNull();
162 return;
164 JS::Rooted<JS::Value> val(cx);
165 if (!mozilla::dom::ToJSValue(cx, dashes, retval)) {
166 rv.Throw(NS_ERROR_OUT_OF_MEMORY);
173 #endif /* _CANVASUTILS_H_ */