Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef _CANVASUTILS_H_ |
michael@0 | 7 | #define _CANVASUTILS_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "mozilla/CheckedInt.h" |
michael@0 | 10 | #include "mozilla/dom/ToJSValue.h" |
michael@0 | 11 | #include "jsapi.h" |
michael@0 | 12 | |
michael@0 | 13 | class nsIPrincipal; |
michael@0 | 14 | |
michael@0 | 15 | namespace mozilla { |
michael@0 | 16 | |
michael@0 | 17 | namespace gfx { |
michael@0 | 18 | class Matrix; |
michael@0 | 19 | } |
michael@0 | 20 | |
michael@0 | 21 | namespace dom { |
michael@0 | 22 | class HTMLCanvasElement; |
michael@0 | 23 | } |
michael@0 | 24 | |
michael@0 | 25 | namespace CanvasUtils { |
michael@0 | 26 | |
michael@0 | 27 | |
michael@0 | 28 | // Check that the rectangle [x,y,w,h] is a subrectangle of [0,0,realWidth,realHeight] |
michael@0 | 29 | |
michael@0 | 30 | inline bool CheckSaneSubrectSize(int32_t x, int32_t y, int32_t w, int32_t h, |
michael@0 | 31 | int32_t realWidth, int32_t realHeight) { |
michael@0 | 32 | CheckedInt32 checked_xmost = CheckedInt32(x) + w; |
michael@0 | 33 | CheckedInt32 checked_ymost = CheckedInt32(y) + h; |
michael@0 | 34 | |
michael@0 | 35 | return w >= 0 && h >= 0 && x >= 0 && y >= 0 && |
michael@0 | 36 | checked_xmost.isValid() && |
michael@0 | 37 | checked_xmost.value() <= realWidth && |
michael@0 | 38 | checked_ymost.isValid() && |
michael@0 | 39 | checked_ymost.value() <= realHeight; |
michael@0 | 40 | } |
michael@0 | 41 | |
michael@0 | 42 | // Flag aCanvasElement as write-only if drawing an image with aPrincipal |
michael@0 | 43 | // onto it would make it such. |
michael@0 | 44 | |
michael@0 | 45 | void DoDrawImageSecurityCheck(dom::HTMLCanvasElement *aCanvasElement, |
michael@0 | 46 | nsIPrincipal *aPrincipal, |
michael@0 | 47 | bool forceWriteOnly, |
michael@0 | 48 | bool CORSUsed); |
michael@0 | 49 | |
michael@0 | 50 | bool IsImageExtractionAllowed(nsIDocument *aDocument, JSContext *aCx); |
michael@0 | 51 | |
michael@0 | 52 | // Make a double out of |v|, treating undefined values as 0.0 (for |
michael@0 | 53 | // the sake of sparse arrays). Return true iff coercion |
michael@0 | 54 | // succeeded. |
michael@0 | 55 | bool CoerceDouble(JS::Value v, double* d); |
michael@0 | 56 | |
michael@0 | 57 | /* Float validation stuff */ |
michael@0 | 58 | #define VALIDATE(_f) if (!NS_finite(_f)) return false |
michael@0 | 59 | |
michael@0 | 60 | inline bool FloatValidate (double f1) { |
michael@0 | 61 | VALIDATE(f1); |
michael@0 | 62 | return true; |
michael@0 | 63 | } |
michael@0 | 64 | |
michael@0 | 65 | inline bool FloatValidate (double f1, double f2) { |
michael@0 | 66 | VALIDATE(f1); VALIDATE(f2); |
michael@0 | 67 | return true; |
michael@0 | 68 | } |
michael@0 | 69 | |
michael@0 | 70 | inline bool FloatValidate (double f1, double f2, double f3) { |
michael@0 | 71 | VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); |
michael@0 | 72 | return true; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | inline bool FloatValidate (double f1, double f2, double f3, double f4) { |
michael@0 | 76 | VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); |
michael@0 | 77 | return true; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5) { |
michael@0 | 81 | VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5); |
michael@0 | 82 | return true; |
michael@0 | 83 | } |
michael@0 | 84 | |
michael@0 | 85 | inline bool FloatValidate (double f1, double f2, double f3, double f4, double f5, double f6) { |
michael@0 | 86 | VALIDATE(f1); VALIDATE(f2); VALIDATE(f3); VALIDATE(f4); VALIDATE(f5); VALIDATE(f6); |
michael@0 | 87 | return true; |
michael@0 | 88 | } |
michael@0 | 89 | |
michael@0 | 90 | #undef VALIDATE |
michael@0 | 91 | |
michael@0 | 92 | template<typename T> |
michael@0 | 93 | nsresult |
michael@0 | 94 | JSValToDashArray(JSContext* cx, const JS::Value& val, |
michael@0 | 95 | FallibleTArray<T>& dashArray); |
michael@0 | 96 | |
michael@0 | 97 | template<typename T> |
michael@0 | 98 | JS::Value |
michael@0 | 99 | DashArrayToJSVal(FallibleTArray<T>& dashArray, |
michael@0 | 100 | JSContext* cx, mozilla::ErrorResult& rv); |
michael@0 | 101 | |
michael@0 | 102 | template<typename T> |
michael@0 | 103 | nsresult |
michael@0 | 104 | JSValToDashArray(JSContext* cx, const JS::Value& patternArray, |
michael@0 | 105 | FallibleTArray<T>& dashes) |
michael@0 | 106 | { |
michael@0 | 107 | // The cap is pretty arbitrary. 16k should be enough for |
michael@0 | 108 | // anybody... |
michael@0 | 109 | static const uint32_t MAX_NUM_DASHES = 1 << 14; |
michael@0 | 110 | |
michael@0 | 111 | if (!JSVAL_IS_PRIMITIVE(patternArray)) { |
michael@0 | 112 | JS::Rooted<JSObject*> obj(cx, JSVAL_TO_OBJECT(patternArray)); |
michael@0 | 113 | uint32_t length; |
michael@0 | 114 | if (!JS_GetArrayLength(cx, obj, &length)) { |
michael@0 | 115 | // Not an array-like thing |
michael@0 | 116 | return NS_ERROR_INVALID_ARG; |
michael@0 | 117 | } else if (length > MAX_NUM_DASHES) { |
michael@0 | 118 | // Too many dashes in the pattern |
michael@0 | 119 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 120 | } |
michael@0 | 121 | |
michael@0 | 122 | bool haveNonzeroElement = false; |
michael@0 | 123 | for (uint32_t i = 0; i < length; ++i) { |
michael@0 | 124 | JS::Rooted<JS::Value> elt(cx); |
michael@0 | 125 | double d; |
michael@0 | 126 | if (!JS_GetElement(cx, obj, i, &elt)) { |
michael@0 | 127 | return NS_ERROR_FAILURE; |
michael@0 | 128 | } |
michael@0 | 129 | if (!(CoerceDouble(elt, &d) && |
michael@0 | 130 | FloatValidate(d) && |
michael@0 | 131 | d >= 0.0)) { |
michael@0 | 132 | // Pattern elements must be finite "numbers" >= 0. |
michael@0 | 133 | return NS_ERROR_INVALID_ARG; |
michael@0 | 134 | } else if (d > 0.0) { |
michael@0 | 135 | haveNonzeroElement = true; |
michael@0 | 136 | } |
michael@0 | 137 | if (!dashes.AppendElement(d)) { |
michael@0 | 138 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 139 | } |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | if (dashes.Length() > 0 && !haveNonzeroElement) { |
michael@0 | 143 | // An all-zero pattern makes no sense. |
michael@0 | 144 | return NS_ERROR_ILLEGAL_VALUE; |
michael@0 | 145 | } |
michael@0 | 146 | } else if (!(JSVAL_IS_VOID(patternArray) || JSVAL_IS_NULL(patternArray))) { |
michael@0 | 147 | // undefined and null mean "reset to no dash". Any other |
michael@0 | 148 | // random garbage is a type error. |
michael@0 | 149 | return NS_ERROR_INVALID_ARG; |
michael@0 | 150 | } |
michael@0 | 151 | |
michael@0 | 152 | return NS_OK; |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | template<typename T> |
michael@0 | 156 | void |
michael@0 | 157 | DashArrayToJSVal(FallibleTArray<T>& dashes, |
michael@0 | 158 | JSContext* cx, |
michael@0 | 159 | JS::MutableHandle<JS::Value> retval, |
michael@0 | 160 | mozilla::ErrorResult& rv) |
michael@0 | 161 | { |
michael@0 | 162 | if (dashes.IsEmpty()) { |
michael@0 | 163 | retval.setNull(); |
michael@0 | 164 | return; |
michael@0 | 165 | } |
michael@0 | 166 | JS::Rooted<JS::Value> val(cx); |
michael@0 | 167 | if (!mozilla::dom::ToJSValue(cx, dashes, retval)) { |
michael@0 | 168 | rv.Throw(NS_ERROR_OUT_OF_MEMORY); |
michael@0 | 169 | } |
michael@0 | 170 | } |
michael@0 | 171 | |
michael@0 | 172 | } |
michael@0 | 173 | } |
michael@0 | 174 | |
michael@0 | 175 | #endif /* _CANVASUTILS_H_ */ |