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/Document.h"
12 #include "mozilla/dom/ToJSValue.h"
14 #include "js/Array.h" // JS::GetArrayLength
15 #include "mozilla/FloatingPoint.h"
22 class HTMLCanvasElement
;
25 namespace CanvasUtils
{
27 bool GetCanvasContextType(const nsAString
& str
,
28 dom::CanvasContextType
* const out_type
);
30 // Check that the rectangle [x,y,w,h] is a subrectangle of
31 // [0,0,realWidth,realHeight]
33 inline bool CheckSaneSubrectSize(int32_t x
, int32_t y
, int32_t w
, int32_t h
,
34 int32_t realWidth
, int32_t realHeight
) {
35 CheckedInt32 checked_xmost
= CheckedInt32(x
) + w
;
36 CheckedInt32 checked_ymost
= CheckedInt32(y
) + h
;
38 return w
>= 0 && h
>= 0 && x
>= 0 && y
>= 0 && checked_xmost
.isValid() &&
39 checked_xmost
.value() <= realWidth
&& 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
, bool forceWriteOnly
,
50 // Check if the context is chrome or has the permission to drawWindow
51 bool HasDrawWindowPrivilege(JSContext
* aCx
, JSObject
* aObj
);
53 // Check site-specific permission and display prompt if appropriate.
54 bool IsImageExtractionAllowed(dom::Document
* aDocument
, JSContext
* aCx
,
55 nsIPrincipal
& aPrincipal
);
57 // Make a double out of |v|, treating undefined values as 0.0 (for
58 // the sake of sparse arrays). Return true iff coercion
60 bool CoerceDouble(const JS::Value
& v
, double* d
);
62 /* Float validation stuff */
63 #define VALIDATE(_f) \
64 if (!IsFinite(_f)) return false
66 inline bool FloatValidate(double f1
) {
71 inline bool FloatValidate(double f1
, double f2
) {
77 inline bool FloatValidate(double f1
, double f2
, double f3
) {
84 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
) {
92 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
,
102 inline bool FloatValidate(double f1
, double f2
, double f3
, double f4
, double f5
,
115 template <typename T
>
116 nsresult
JSValToDashArray(JSContext
* cx
, const JS::Value
& patternArray
,
117 nsTArray
<T
>& dashes
) {
118 // The cap is pretty arbitrary. 16k should be enough for
120 static const uint32_t MAX_NUM_DASHES
= 1 << 14;
122 if (!patternArray
.isPrimitive()) {
123 JS::Rooted
<JSObject
*> obj(cx
, patternArray
.toObjectOrNull());
125 if (!JS::GetArrayLength(cx
, obj
, &length
)) {
126 // Not an array-like thing
127 return NS_ERROR_INVALID_ARG
;
128 } else if (length
> MAX_NUM_DASHES
) {
129 // Too many dashes in the pattern
130 return NS_ERROR_ILLEGAL_VALUE
;
133 bool haveNonzeroElement
= false;
134 for (uint32_t i
= 0; i
< length
; ++i
) {
135 JS::Rooted
<JS::Value
> elt(cx
);
137 if (!JS_GetElement(cx
, obj
, i
, &elt
)) {
138 return NS_ERROR_FAILURE
;
140 if (!(CoerceDouble(elt
, &d
) && FloatValidate(d
) && d
>= 0.0)) {
141 // Pattern elements must be finite "numbers" >= 0.
142 return NS_ERROR_INVALID_ARG
;
143 } else if (d
> 0.0) {
144 haveNonzeroElement
= true;
146 if (!dashes
.AppendElement(d
, mozilla::fallible
)) {
147 return NS_ERROR_OUT_OF_MEMORY
;
151 if (dashes
.Length() > 0 && !haveNonzeroElement
) {
152 // An all-zero pattern makes no sense.
153 return NS_ERROR_ILLEGAL_VALUE
;
155 } else if (!(patternArray
.isUndefined() || patternArray
.isNull())) {
156 // undefined and null mean "reset to no dash". Any other
157 // random garbage is a type error.
158 return NS_ERROR_INVALID_ARG
;
164 template <typename T
>
165 void DashArrayToJSVal(nsTArray
<T
>& dashes
, JSContext
* cx
,
166 JS::MutableHandle
<JS::Value
> retval
,
167 mozilla::ErrorResult
& rv
) {
168 if (dashes
.IsEmpty()) {
172 JS::Rooted
<JS::Value
> val(cx
);
173 if (!mozilla::dom::ToJSValue(cx
, dashes
, retval
)) {
174 rv
.Throw(NS_ERROR_OUT_OF_MEMORY
);
178 // returns true if write-only mode must used for this principal based on
179 // the incumbent global.
180 bool CheckWriteOnlySecurity(bool aCORSUsed
, nsIPrincipal
* aPrincipal
,
181 bool aHadCrossOriginRedirects
);
183 } // namespace CanvasUtils
184 } // namespace mozilla
186 #endif /* _CANVASUTILS_H_ */