Bug 1795723 - Unified extensions UI should support High Contrast Mode. r=ayeddi,deskt...
[gecko.git] / dom / canvas / CanvasUtils.h
blob67313b033a7fa01db1daf2871df7cc16f101e389
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
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 "CanvasRenderingContextHelper.h"
10 #include "mozilla/CheckedInt.h"
11 #include "mozilla/dom/ToJSValue.h"
12 #include "jsapi.h"
13 #include "js/Array.h" // JS::GetArrayLength
14 #include "js/PropertyAndElement.h" // JS_GetElement
15 #include "mozilla/FloatingPoint.h"
17 class nsIPrincipal;
19 namespace mozilla {
21 namespace dom {
22 class Document;
23 class HTMLCanvasElement;
24 class OffscreenCanvas;
25 } // namespace dom
27 namespace CanvasUtils {
29 bool GetCanvasContextType(const nsAString& str,
30 dom::CanvasContextType* const out_type);
32 // Check that the rectangle [x,y,w,h] is a subrectangle of
33 // [0,0,realWidth,realHeight]
35 inline bool CheckSaneSubrectSize(int32_t x, int32_t y, int32_t w, int32_t h,
36 int32_t realWidth, int32_t realHeight) {
37 CheckedInt32 checked_xmost = CheckedInt32(x) + w;
38 CheckedInt32 checked_ymost = CheckedInt32(y) + h;
40 return w >= 0 && h >= 0 && x >= 0 && y >= 0 && checked_xmost.isValid() &&
41 checked_xmost.value() <= realWidth && checked_ymost.isValid() &&
42 checked_ymost.value() <= realHeight;
45 // Flag aCanvasElement as write-only if drawing an image with aPrincipal
46 // onto it would make it such.
48 void DoDrawImageSecurityCheck(dom::HTMLCanvasElement* aCanvasElement,
49 nsIPrincipal* aPrincipal, bool forceWriteOnly,
50 bool CORSUsed);
52 void DoDrawImageSecurityCheck(dom::OffscreenCanvas* aOffscreenCanvas,
53 nsIPrincipal* aPrincipal, bool forceWriteOnly,
54 bool CORSUsed);
56 // Check if the context is chrome or has the permission to drawWindow
57 bool HasDrawWindowPrivilege(JSContext* aCx, JSObject* aObj);
59 // Check if the context has permission to use OffscreenCanvas.
60 bool IsOffscreenCanvasEnabled(JSContext* aCx, JSObject* aObj);
62 // Check site-specific permission and display prompt if appropriate.
63 bool IsImageExtractionAllowed(dom::Document* aDocument, JSContext* aCx,
64 Maybe<nsIPrincipal*> aPrincipal);
66 // Make a double out of |v|, treating undefined values as 0.0 (for
67 // the sake of sparse arrays). Return true iff coercion
68 // succeeded.
69 bool CoerceDouble(const JS::Value& v, double* d);
71 /* Float validation stuff */
72 #define VALIDATE(_f) \
73 if (!IsFinite(_f)) return false
75 inline bool FloatValidate(double f1) {
76 VALIDATE(f1);
77 return true;
80 inline bool FloatValidate(double f1, double f2) {
81 VALIDATE(f1);
82 VALIDATE(f2);
83 return true;
86 inline bool FloatValidate(double f1, double f2, double f3) {
87 VALIDATE(f1);
88 VALIDATE(f2);
89 VALIDATE(f3);
90 return true;
93 inline bool FloatValidate(double f1, double f2, double f3, double f4) {
94 VALIDATE(f1);
95 VALIDATE(f2);
96 VALIDATE(f3);
97 VALIDATE(f4);
98 return true;
101 inline bool FloatValidate(double f1, double f2, double f3, double f4,
102 double f5) {
103 VALIDATE(f1);
104 VALIDATE(f2);
105 VALIDATE(f3);
106 VALIDATE(f4);
107 VALIDATE(f5);
108 return true;
111 inline bool FloatValidate(double f1, double f2, double f3, double f4, double f5,
112 double f6) {
113 VALIDATE(f1);
114 VALIDATE(f2);
115 VALIDATE(f3);
116 VALIDATE(f4);
117 VALIDATE(f5);
118 VALIDATE(f6);
119 return true;
122 #undef VALIDATE
124 template <typename T>
125 nsresult JSValToDashArray(JSContext* cx, const JS::Value& patternArray,
126 nsTArray<T>& dashes) {
127 // The cap is pretty arbitrary. 16k should be enough for
128 // anybody...
129 static const uint32_t MAX_NUM_DASHES = 1 << 14;
131 if (!patternArray.isPrimitive()) {
132 JS::Rooted<JSObject*> obj(cx, patternArray.toObjectOrNull());
133 uint32_t length;
134 if (!JS::GetArrayLength(cx, obj, &length)) {
135 // Not an array-like thing
136 return NS_ERROR_INVALID_ARG;
137 } else if (length > MAX_NUM_DASHES) {
138 // Too many dashes in the pattern
139 return NS_ERROR_ILLEGAL_VALUE;
142 bool haveNonzeroElement = false;
143 for (uint32_t i = 0; i < length; ++i) {
144 JS::Rooted<JS::Value> elt(cx);
145 double d;
146 if (!JS_GetElement(cx, obj, i, &elt)) {
147 return NS_ERROR_FAILURE;
149 if (!(CoerceDouble(elt, &d) && FloatValidate(d) && d >= 0.0)) {
150 // Pattern elements must be finite "numbers" >= 0.
151 return NS_ERROR_INVALID_ARG;
152 } else if (d > 0.0) {
153 haveNonzeroElement = true;
155 if (!dashes.AppendElement(d, mozilla::fallible)) {
156 return NS_ERROR_OUT_OF_MEMORY;
160 if (dashes.Length() > 0 && !haveNonzeroElement) {
161 // An all-zero pattern makes no sense.
162 return NS_ERROR_ILLEGAL_VALUE;
164 } else if (!(patternArray.isUndefined() || patternArray.isNull())) {
165 // undefined and null mean "reset to no dash". Any other
166 // random garbage is a type error.
167 return NS_ERROR_INVALID_ARG;
170 return NS_OK;
173 template <typename T>
174 void DashArrayToJSVal(nsTArray<T>& dashes, JSContext* cx,
175 JS::MutableHandle<JS::Value> retval,
176 mozilla::ErrorResult& rv) {
177 if (dashes.IsEmpty()) {
178 retval.setNull();
179 return;
181 JS::Rooted<JS::Value> val(cx);
182 if (!mozilla::dom::ToJSValue(cx, dashes, retval)) {
183 rv.Throw(NS_ERROR_OUT_OF_MEMORY);
187 // returns true if write-only mode must used for this principal based on
188 // the incumbent global.
189 bool CheckWriteOnlySecurity(bool aCORSUsed, nsIPrincipal* aPrincipal,
190 bool aHadCrossOriginRedirects);
192 } // namespace CanvasUtils
193 } // namespace mozilla
195 #endif /* _CANVASUTILS_H_ */