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"
13 #include "js/Array.h" // JS::GetArrayLength
14 #include "js/PropertyAndElement.h" // JS_GetElement
15 #include "mozilla/FloatingPoint.h"
23 class HTMLCanvasElement
;
24 class OffscreenCanvas
;
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
,
52 void DoDrawImageSecurityCheck(dom::OffscreenCanvas
* aOffscreenCanvas
,
53 nsIPrincipal
* aPrincipal
, bool forceWriteOnly
,
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
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
) {
80 inline bool FloatValidate(double f1
, double f2
) {
86 inline bool FloatValidate(double f1
, double f2
, double f3
) {
93 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
) {
101 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
,
111 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
, double f5
,
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
129 static const uint32_t MAX_NUM_DASHES
= 1 << 14;
131 if (!patternArray
.isPrimitive()) {
132 JS::Rooted
<JSObject
*> obj(cx
, patternArray
.toObjectOrNull());
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
);
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
;
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()) {
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_ */