Bumping manifests a=b2g-bump
[gecko.git] / dom / canvas / CanvasUtils.h
blob31347ee651daa369afb7c37abc72e999a819a9e7
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"
12 #include "mozilla/FloatingPoint.h"
14 class nsIPrincipal;
16 namespace mozilla {
18 namespace gfx {
19 class Matrix;
22 namespace dom {
23 class HTMLCanvasElement;
26 namespace CanvasUtils {
29 // Check that the rectangle [x,y,w,h] is a subrectangle of [0,0,realWidth,realHeight]
31 inline bool CheckSaneSubrectSize(int32_t x, int32_t y, int32_t w, int32_t h,
32 int32_t realWidth, int32_t realHeight) {
33 CheckedInt32 checked_xmost = CheckedInt32(x) + w;
34 CheckedInt32 checked_ymost = CheckedInt32(y) + h;
36 return w >= 0 && h >= 0 && x >= 0 && y >= 0 &&
37 checked_xmost.isValid() &&
38 checked_xmost.value() <= realWidth &&
39 checked_ymost.isValid() &&
40 checked_ymost.value() <= realHeight;
43 // Flag aCanvasElement as write-only if drawing an image with aPrincipal
44 // onto it would make it such.
46 void DoDrawImageSecurityCheck(dom::HTMLCanvasElement *aCanvasElement,
47 nsIPrincipal *aPrincipal,
48 bool forceWriteOnly,
49 bool CORSUsed);
51 // Make a double out of |v|, treating undefined values as 0.0 (for
52 // the sake of sparse arrays). Return true iff coercion
53 // succeeded.
54 bool CoerceDouble(JS::Value v, double* d);
56 /* Float validation stuff */
57 #define VALIDATE(_f) if (!IsFinite(_f)) return false
59 inline bool FloatValidate (double f1) {
60 VALIDATE(f1);
61 return true;
64 inline bool FloatValidate (double f1, double f2) {
65 VALIDATE(f1); VALIDATE(f2);
66 return true;
69 inline bool FloatValidate (double f1, double f2, double f3) {
70 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3);
71 return true;
74 inline bool FloatValidate (double f1, double f2, double f3, double f4) {
75 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4);
76 return true;
79 inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5) {
80 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5);
81 return true;
84 inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5, double f6) {
85 VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5); VALIDATE(f6);
86 return true;
89 #undef VALIDATE
91 template<typename T>
92 nsresult
93 JSValToDashArray(JSContext* cx, const JS::Value& val,
94 FallibleTArray<T>& dashArray);
96 template<typename T>
97 JS::Value
98 DashArrayToJSVal(FallibleTArray<T>& dashArray,
99 JSContext* cx, mozilla::ErrorResult& rv);
101 template<typename T>
102 nsresult
103 JSValToDashArray(JSContext* cx, const JS::Value& patternArray,
104 FallibleTArray<T>& dashes)
106 // The cap is pretty arbitrary. 16k should be enough for
107 // anybody...
108 static const uint32_t MAX_NUM_DASHES = 1 << 14;
110 if (!patternArray.isPrimitive()) {
111 JS::Rooted<JSObject*> obj(cx, patternArray.toObjectOrNull());
112 uint32_t length;
113 if (!JS_GetArrayLength(cx, obj, &length)) {
114 // Not an array-like thing
115 return NS_ERROR_INVALID_ARG;
116 } else if (length > MAX_NUM_DASHES) {
117 // Too many dashes in the pattern
118 return NS_ERROR_ILLEGAL_VALUE;
121 bool haveNonzeroElement = false;
122 for (uint32_t i = 0; i < length; ++i) {
123 JS::Rooted<JS::Value> elt(cx);
124 double d;
125 if (!JS_GetElement(cx, obj, i, &elt)) {
126 return NS_ERROR_FAILURE;
128 if (!(CoerceDouble(elt, &d) &&
129 FloatValidate(d) &&
130 d >= 0.0)) {
131 // Pattern elements must be finite "numbers" >= 0.
132 return NS_ERROR_INVALID_ARG;
133 } else if (d > 0.0) {
134 haveNonzeroElement = true;
136 if (!dashes.AppendElement(d)) {
137 return NS_ERROR_OUT_OF_MEMORY;
141 if (dashes.Length() > 0 && !haveNonzeroElement) {
142 // An all-zero pattern makes no sense.
143 return NS_ERROR_ILLEGAL_VALUE;
145 } else if (!(patternArray.isUndefined() || patternArray.isNull())) {
146 // undefined and null mean "reset to no dash". Any other
147 // random garbage is a type error.
148 return NS_ERROR_INVALID_ARG;
151 return NS_OK;
154 template<typename T>
155 void
156 DashArrayToJSVal(FallibleTArray<T>& dashes,
157 JSContext* cx,
158 JS::MutableHandle<JS::Value> retval,
159 mozilla::ErrorResult& rv)
161 if (dashes.IsEmpty()) {
162 retval.setNull();
163 return;
165 JS::Rooted<JS::Value> val(cx);
166 if (!mozilla::dom::ToJSValue(cx, dashes, retval)) {
167 rv.Throw(NS_ERROR_OUT_OF_MEMORY);
174 #endif /* _CANVASUTILS_H_ */