1 : /* -*- Mode: C++; tab-width: 40; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /* ***** BEGIN LICENSE BLOCK *****
3 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
4 : *
5 : * The contents of this file are subject to the Mozilla Public License Version
6 : * 1.1 (the "License"); you may not use this file except in compliance with
7 : * the License. You may obtain a copy of the License at
8 : * http://www.mozilla.org/MPL/
9 : *
10 : * Software distributed under the License is distributed on an "AS IS" basis,
11 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 : * for the specific language governing rights and limitations under the
13 : * License.
14 : *
15 : * The Original Code is Gecko code.
16 : *
17 : * The Initial Developer of the Original Code is
18 : * the Mozilla Foundation.
19 : * Portions created by the Initial Developer are Copyright (C) 2010
20 : * the Initial Developer. All Rights Reserved.
21 : *
22 : * Contributor(s):
23 : * Vladimir Vukicevic <vladimir@pobox.com> (original author)
24 : * Ms2ger <ms2ger@gmail.com>
25 : * Yury <async.processingjs@yahoo.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either of the GNU General Public License Version 2 or later (the "GPL"),
29 : * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #include "nsDOMError.h"
42 : #include "nsIDOMCanvasRenderingContext2D.h"
43 : #include "CheckedInt.h"
44 : #include "nsMathUtils.h"
45 :
46 : #include "jsapi.h"
47 :
48 : typedef NS_STDCALL_FUNCPROTO(nsresult, CanvasStyleSetterType, nsIDOMCanvasRenderingContext2D,
49 : SetStrokeStyle_multi, (const nsAString &, nsISupports *));
50 : typedef NS_STDCALL_FUNCPROTO(nsresult, CanvasStyleGetterType, nsIDOMCanvasRenderingContext2D,
51 : GetStrokeStyle_multi, (nsAString &, nsISupports **, PRInt32 *));
52 :
53 : static JSBool
54 0 : Canvas2D_SetStyleHelper(JSContext *cx, JSObject *obj, jsid id, jsval *vp,
55 : CanvasStyleSetterType setfunc)
56 : {
57 0 : XPC_QS_ASSERT_CONTEXT_OK(cx);
58 : nsIDOMCanvasRenderingContext2D *self;
59 0 : xpc_qsSelfRef selfref;
60 0 : JS::AutoValueRooter tvr(cx);
61 0 : if (!xpc_qsUnwrapThis(cx, obj, nsnull, &self, &selfref.ptr, tvr.jsval_addr(), nsnull))
62 0 : return JS_FALSE;
63 :
64 0 : nsresult rv = NS_OK;
65 0 : if (JSVAL_IS_STRING(*vp)) {
66 : xpc_qsDOMString arg0(cx, *vp, vp,
67 : xpc_qsDOMString::eDefaultNullBehavior,
68 0 : xpc_qsDOMString::eDefaultUndefinedBehavior);
69 0 : if (!arg0.IsValid())
70 0 : return JS_FALSE;
71 :
72 0 : rv = (self->*setfunc)(arg0, nsnull);
73 : } else {
74 : nsISupports *arg0;
75 0 : xpc_qsSelfRef arg0ref;
76 0 : rv = xpc_qsUnwrapArg<nsISupports>(cx, *vp, &arg0, &arg0ref.ptr, vp);
77 0 : if (NS_FAILED(rv)) {
78 0 : xpc_qsThrowBadSetterValue(cx, rv, JSVAL_TO_OBJECT(*tvr.jsval_addr()), id);
79 0 : return JS_FALSE;
80 : }
81 :
82 0 : rv = (self->*setfunc)(NullString(), arg0);
83 : }
84 :
85 0 : if (NS_FAILED(rv))
86 0 : return xpc_qsThrowGetterSetterFailed(cx, rv, JSVAL_TO_OBJECT(*tvr.jsval_addr()), id);
87 :
88 0 : return JS_TRUE;
89 : }
90 :
91 : static JSBool
92 0 : Canvas2D_GetStyleHelper(JSContext *cx, JSObject *obj, jsid id, jsval *vp,
93 : CanvasStyleGetterType getfunc)
94 : {
95 0 : XPC_QS_ASSERT_CONTEXT_OK(cx);
96 : nsIDOMCanvasRenderingContext2D *self;
97 0 : xpc_qsSelfRef selfref;
98 0 : XPCLazyCallContext lccx(JS_CALLER, cx, obj);
99 0 : if (!xpc_qsUnwrapThis(cx, obj, nsnull, &self, &selfref.ptr, vp, &lccx))
100 0 : return JS_FALSE;
101 : nsresult rv;
102 :
103 0 : nsString resultString;
104 0 : nsCOMPtr<nsISupports> resultInterface;
105 : PRInt32 resultType;
106 0 : rv = (self->*getfunc)(resultString, getter_AddRefs(resultInterface), &resultType);
107 0 : if (NS_FAILED(rv))
108 0 : return xpc_qsThrowGetterSetterFailed(cx, rv, JSVAL_TO_OBJECT(*vp), id);
109 :
110 0 : switch (resultType) {
111 : case nsIDOMCanvasRenderingContext2D::CMG_STYLE_STRING:
112 0 : return xpc::StringToJsval(cx, resultString, vp);
113 :
114 : case nsIDOMCanvasRenderingContext2D::CMG_STYLE_PATTERN:
115 : {
116 : qsObjectHelper helper(resultInterface,
117 0 : xpc_qsGetWrapperCache(resultInterface));
118 : return xpc_qsXPCOMObjectToJsval(lccx, helper,
119 : &NS_GET_IID(nsIDOMCanvasPattern),
120 0 : &interfaces[k_nsIDOMCanvasPattern], vp);
121 : }
122 : case nsIDOMCanvasRenderingContext2D::CMG_STYLE_GRADIENT:
123 : {
124 : qsObjectHelper helper(resultInterface,
125 0 : xpc_qsGetWrapperCache(resultInterface));
126 : return xpc_qsXPCOMObjectToJsval(lccx, helper,
127 : &NS_GET_IID(nsIDOMCanvasGradient),
128 0 : &interfaces[k_nsIDOMCanvasGradient], vp);
129 : }
130 : default:
131 0 : return xpc_qsThrowGetterSetterFailed(cx, NS_ERROR_FAILURE, JSVAL_TO_OBJECT(*vp), id);
132 : }
133 : }
134 :
135 : static JSBool
136 0 : nsIDOMCanvasRenderingContext2D_SetStrokeStyle(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
137 : {
138 0 : return Canvas2D_SetStyleHelper(cx, obj, id, vp, &nsIDOMCanvasRenderingContext2D::SetStrokeStyle_multi);
139 : }
140 :
141 : static JSBool
142 0 : nsIDOMCanvasRenderingContext2D_GetStrokeStyle(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
143 : {
144 0 : return Canvas2D_GetStyleHelper(cx, obj, id, vp, &nsIDOMCanvasRenderingContext2D::GetStrokeStyle_multi);
145 : }
146 :
147 : static JSBool
148 0 : nsIDOMCanvasRenderingContext2D_SetFillStyle(JSContext *cx, JSObject *obj, jsid id, JSBool strict, jsval *vp)
149 : {
150 0 : return Canvas2D_SetStyleHelper(cx, obj, id, vp, &nsIDOMCanvasRenderingContext2D::SetFillStyle_multi);
151 : }
152 :
153 : static JSBool
154 0 : nsIDOMCanvasRenderingContext2D_GetFillStyle(JSContext *cx, JSObject *obj, jsid id, jsval *vp)
155 : {
156 0 : return Canvas2D_GetStyleHelper(cx, obj, id, vp, &nsIDOMCanvasRenderingContext2D::GetFillStyle_multi);
157 : }
158 :
159 : static bool
160 0 : CreateImageData(JSContext* cx,
161 : uint32_t w,
162 : uint32_t h,
163 : nsIDOMCanvasRenderingContext2D* self,
164 : int32_t x,
165 : int32_t y,
166 : jsval* vp)
167 : {
168 : using mozilla::CheckedInt;
169 :
170 0 : if (w == 0)
171 0 : w = 1;
172 0 : if (h == 0)
173 0 : h = 1;
174 :
175 0 : CheckedInt<uint32_t> len = CheckedInt<uint32_t>(w) * h * 4;
176 0 : if (!len.valid()) {
177 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
178 : }
179 :
180 : // Create the fast typed array; it's initialized to 0 by default.
181 : JSObject* darray =
182 0 : js_CreateTypedArray(cx, js::TypedArray::TYPE_UINT8_CLAMPED, len.value());
183 0 : JS::AutoObjectRooter rd(cx, darray);
184 0 : if (!darray) {
185 0 : return false;
186 : }
187 :
188 0 : if (self) {
189 0 : JSObject *tdest = js::TypedArray::getTypedArray(darray);
190 :
191 : // make the call
192 : nsresult rv =
193 : self->GetImageData_explicit(x, y, w, h,
194 0 : static_cast<PRUint8*>(JS_GetTypedArrayData(tdest)),
195 0 : JS_GetTypedArrayByteLength(tdest));
196 0 : if (NS_FAILED(rv)) {
197 0 : return xpc_qsThrowMethodFailed(cx, rv, vp);
198 : }
199 : }
200 :
201 : // Do JS_NewObject after CreateTypedArray, so that gc will get
202 : // triggered here if necessary
203 0 : JSObject* result = JS_NewObject(cx, NULL, NULL, NULL);
204 0 : JS::AutoObjectRooter rr(cx, result);
205 0 : if (!result) {
206 0 : return false;
207 : }
208 :
209 0 : if (!JS_DefineProperty(cx, result, "width", INT_TO_JSVAL(w), NULL, NULL,
210 0 : JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT) ||
211 0 : !JS_DefineProperty(cx, result, "height", INT_TO_JSVAL(h), NULL, NULL,
212 0 : JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT) ||
213 : !JS_DefineProperty(cx, result, "data", OBJECT_TO_JSVAL(darray), NULL, NULL,
214 0 : JSPROP_ENUMERATE | JSPROP_READONLY | JSPROP_PERMANENT)) {
215 0 : return false;
216 : }
217 :
218 0 : *vp = OBJECT_TO_JSVAL(result);
219 0 : return true;
220 : }
221 :
222 : static bool
223 0 : GetImageDataDimensions(JSContext *cx, JSObject *dataObject, uint32_t *width, uint32_t *height)
224 : {
225 : jsval temp;
226 : int32_t wi, hi;
227 :
228 : // Need to check that dataObject is ImageData object. That's hard for the moment
229 : // because they're just vanilla objects in our implementation.
230 : // Let's guess, if the object has valid width and height then it's suitable
231 : // for this operation.
232 0 : if (!JS_GetProperty(cx, dataObject, "width", &temp) ||
233 0 : !JS_ValueToECMAInt32(cx, temp, &wi))
234 0 : return false;
235 :
236 0 : if (!JS_GetProperty(cx, dataObject, "height", &temp) ||
237 0 : !JS_ValueToECMAInt32(cx, temp, &hi))
238 0 : return false;
239 :
240 0 : if (wi <= 0 || hi <= 0)
241 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
242 :
243 0 : *width = uint32_t(wi);
244 0 : *height = uint32_t(hi);
245 0 : return true;
246 : }
247 :
248 : static JSBool
249 0 : nsIDOMCanvasRenderingContext2D_CreateImageData(JSContext *cx, unsigned argc, jsval *vp)
250 : {
251 0 : XPC_QS_ASSERT_CONTEXT_OK(cx);
252 :
253 : /* Note: this doesn't need JS_THIS_OBJECT */
254 :
255 0 : if (argc < 1)
256 0 : return xpc_qsThrow(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
257 :
258 0 : jsval *argv = JS_ARGV(cx, vp);
259 :
260 0 : if (argc == 1) {
261 : // The specification asks to throw NOT_SUPPORTED if first argument is NULL,
262 : // An object is expected, so throw an exception for all primitives.
263 0 : if (JSVAL_IS_PRIMITIVE(argv[0]))
264 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
265 :
266 0 : JSObject *dataObject = JSVAL_TO_OBJECT(argv[0]);
267 :
268 : uint32_t data_width, data_height;
269 0 : if (!GetImageDataDimensions(cx, dataObject, &data_width, &data_height))
270 0 : return false;
271 :
272 0 : return CreateImageData(cx, data_width, data_height, NULL, 0, 0, vp);
273 : }
274 :
275 : double width, height;
276 0 : if (!JS_ValueToNumber(cx, argv[0], &width) ||
277 0 : !JS_ValueToNumber(cx, argv[1], &height))
278 0 : return false;
279 :
280 0 : if (!NS_finite(width) || !NS_finite(height))
281 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
282 :
283 0 : if (!width || !height)
284 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
285 :
286 0 : int32_t wi = JS_DoubleToInt32(width);
287 0 : int32_t hi = JS_DoubleToInt32(height);
288 :
289 0 : uint32_t w = NS_ABS(wi);
290 0 : uint32_t h = NS_ABS(hi);
291 0 : return CreateImageData(cx, w, h, NULL, 0, 0, vp);
292 : }
293 :
294 : static JSBool
295 0 : nsIDOMCanvasRenderingContext2D_GetImageData(JSContext *cx, unsigned argc, jsval *vp)
296 : {
297 0 : XPC_QS_ASSERT_CONTEXT_OK(cx);
298 :
299 0 : JSObject *obj = JS_THIS_OBJECT(cx, vp);
300 0 : if (!obj)
301 0 : return JS_FALSE;
302 :
303 : nsIDOMCanvasRenderingContext2D *self;
304 0 : xpc_qsSelfRef selfref;
305 0 : JS::AutoValueRooter tvr(cx);
306 0 : if (!xpc_qsUnwrapThis(cx, obj, nsnull, &self, &selfref.ptr, tvr.jsval_addr(), nsnull))
307 0 : return JS_FALSE;
308 :
309 0 : if (argc < 4)
310 0 : return xpc_qsThrow(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
311 :
312 0 : jsval *argv = JS_ARGV(cx, vp);
313 :
314 : double xd, yd, width, height;
315 0 : if (!JS_ValueToNumber(cx, argv[0], &xd) ||
316 0 : !JS_ValueToNumber(cx, argv[1], &yd) ||
317 0 : !JS_ValueToNumber(cx, argv[2], &width) ||
318 0 : !JS_ValueToNumber(cx, argv[3], &height))
319 0 : return false;
320 :
321 0 : if (!NS_finite(xd) || !NS_finite(yd) ||
322 0 : !NS_finite(width) || !NS_finite(height))
323 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
324 :
325 0 : if (!width || !height)
326 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_INDEX_SIZE_ERR);
327 :
328 0 : int32_t x = JS_DoubleToInt32(xd);
329 0 : int32_t y = JS_DoubleToInt32(yd);
330 0 : int32_t wi = JS_DoubleToInt32(width);
331 0 : int32_t hi = JS_DoubleToInt32(height);
332 :
333 : // Handle negative width and height by flipping the rectangle over in the
334 : // relevant direction.
335 : uint32_t w, h;
336 0 : if (width < 0) {
337 0 : w = -wi;
338 0 : x -= w;
339 : } else {
340 0 : w = wi;
341 : }
342 0 : if (height < 0) {
343 0 : h = -hi;
344 0 : y -= h;
345 : } else {
346 0 : h = hi;
347 : }
348 0 : return CreateImageData(cx, w, h, self, x, y, vp);
349 : }
350 :
351 : static JSBool
352 0 : nsIDOMCanvasRenderingContext2D_PutImageData(JSContext *cx, unsigned argc, jsval *vp)
353 : {
354 0 : XPC_QS_ASSERT_CONTEXT_OK(cx);
355 :
356 0 : JSObject *obj = JS_THIS_OBJECT(cx, vp);
357 0 : if (!obj)
358 0 : return JS_FALSE;
359 :
360 : nsresult rv;
361 :
362 : nsIDOMCanvasRenderingContext2D *self;
363 0 : xpc_qsSelfRef selfref;
364 0 : JS::AutoValueRooter tvr(cx);
365 0 : if (!xpc_qsUnwrapThis(cx, obj, nsnull, &self, &selfref.ptr, tvr.jsval_addr(), nsnull))
366 0 : return JS_FALSE;
367 :
368 0 : if (argc < 3)
369 0 : return xpc_qsThrow(cx, NS_ERROR_XPC_NOT_ENOUGH_ARGS);
370 :
371 0 : jsval *argv = JS_ARGV(cx, vp);
372 :
373 0 : if (JSVAL_IS_PRIMITIVE(argv[0]))
374 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_TYPE_MISMATCH_ERR);
375 :
376 0 : JSObject *dataObject = JSVAL_TO_OBJECT(argv[0]);
377 :
378 : double xd, yd;
379 0 : if (!JS_ValueToNumber(cx, argv[1], &xd) ||
380 0 : !JS_ValueToNumber(cx, argv[2], &yd)) {
381 0 : return false;
382 : }
383 :
384 0 : if (!NS_finite(xd) || !NS_finite(yd)) {
385 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
386 : }
387 :
388 0 : int32_t x = JS_DoubleToInt32(xd);
389 0 : int32_t y = JS_DoubleToInt32(yd);
390 :
391 : // Grab width, height, and the dense array from the dataObject.
392 0 : JS::AutoValueRooter tv(cx);
393 :
394 : uint32_t w, h;
395 0 : if (!GetImageDataDimensions(cx, dataObject, &w, &h))
396 0 : return JS_FALSE;
397 :
398 : // the optional dirty rect
399 0 : bool hasDirtyRect = false;
400 0 : int32_t dirtyX = 0,
401 0 : dirtyY = 0,
402 0 : dirtyWidth = w,
403 0 : dirtyHeight = h;
404 :
405 0 : if (argc >= 7) {
406 : double dx, dy, dw, dh;
407 0 : if (!JS_ValueToNumber(cx, argv[3], &dx) ||
408 0 : !JS_ValueToNumber(cx, argv[4], &dy) ||
409 0 : !JS_ValueToNumber(cx, argv[5], &dw) ||
410 0 : !JS_ValueToNumber(cx, argv[6], &dh)) {
411 0 : return false;
412 : }
413 :
414 0 : if (!NS_finite(dx) || !NS_finite(dy) ||
415 0 : !NS_finite(dw) || !NS_finite(dh)) {
416 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_NOT_SUPPORTED_ERR);
417 : }
418 :
419 0 : dirtyX = JS_DoubleToInt32(dx);
420 0 : dirtyY = JS_DoubleToInt32(dy);
421 0 : dirtyWidth = JS_DoubleToInt32(dw);
422 0 : dirtyHeight = JS_DoubleToInt32(dh);
423 :
424 0 : hasDirtyRect = true;
425 : }
426 :
427 0 : if (!JS_GetProperty(cx, dataObject, "data", tv.jsval_addr()))
428 0 : return JS_FALSE;
429 :
430 0 : if (JSVAL_IS_PRIMITIVE(tv.jsval_value()))
431 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_TYPE_MISMATCH_ERR);
432 :
433 0 : JSObject *darray = JSVAL_TO_OBJECT(tv.jsval_value());
434 :
435 0 : JS::AutoValueRooter tsrc_tvr(cx);
436 :
437 0 : JSObject *tsrc = NULL;
438 0 : if (js::GetObjectClass(darray) == &js::TypedArray::fastClasses[js::TypedArray::TYPE_UINT8] ||
439 0 : js::GetObjectClass(darray) == &js::TypedArray::fastClasses[js::TypedArray::TYPE_UINT8_CLAMPED])
440 : {
441 0 : tsrc = js::TypedArray::getTypedArray(darray);
442 0 : } else if (JS_IsArrayObject(cx, darray) || js_IsTypedArray(darray)) {
443 : // ugh, this isn't a uint8 typed array, someone made their own object; convert it to a typed array
444 0 : JSObject *nobj = js_CreateTypedArrayWithArray(cx, js::TypedArray::TYPE_UINT8, darray);
445 0 : if (!nobj)
446 0 : return JS_FALSE;
447 :
448 0 : *tsrc_tvr.jsval_addr() = OBJECT_TO_JSVAL(nobj);
449 0 : tsrc = js::TypedArray::getTypedArray(nobj);
450 : } else {
451 : // yeah, no.
452 0 : return xpc_qsThrow(cx, NS_ERROR_DOM_TYPE_MISMATCH_ERR);
453 : }
454 :
455 : // make the call
456 0 : rv = self->PutImageData_explicit(x, y, w, h, static_cast<PRUint8*>(JS_GetTypedArrayData(tsrc)), JS_GetTypedArrayByteLength(tsrc), hasDirtyRect, dirtyX, dirtyY, dirtyWidth, dirtyHeight);
457 0 : if (NS_FAILED(rv))
458 0 : return xpc_qsThrowMethodFailed(cx, rv, vp);
459 :
460 0 : *vp = JSVAL_VOID;
461 0 : return JS_TRUE;
462 : }
|