Bumping manifests a=b2g-bump
[gecko.git] / dom / canvas / CanvasUtils.cpp
blob41ff889c9d41d1b679dcab57bbf0d6eb61ce4162
1 /* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 #include <stdlib.h>
7 #include <stdarg.h>
9 #include "prprf.h"
11 #include "nsIServiceManager.h"
13 #include "nsIConsoleService.h"
14 #include "nsIDOMCanvasRenderingContext2D.h"
15 #include "nsICanvasRenderingContextInternal.h"
16 #include "nsIHTMLCollection.h"
17 #include "mozilla/dom/HTMLCanvasElement.h"
18 #include "nsIPrincipal.h"
20 #include "nsGfxCIID.h"
22 #include "nsTArray.h"
24 #include "CanvasUtils.h"
25 #include "mozilla/gfx/Matrix.h"
27 using namespace mozilla::gfx;
29 namespace mozilla {
30 namespace CanvasUtils {
32 void
33 DoDrawImageSecurityCheck(dom::HTMLCanvasElement *aCanvasElement,
34 nsIPrincipal *aPrincipal,
35 bool forceWriteOnly,
36 bool CORSUsed)
38 NS_PRECONDITION(aPrincipal, "Must have a principal here");
40 // Callers should ensure that mCanvasElement is non-null before calling this
41 if (!aCanvasElement) {
42 NS_WARNING("DoDrawImageSecurityCheck called without canvas element!");
43 return;
46 if (aCanvasElement->IsWriteOnly())
47 return;
49 // If we explicitly set WriteOnly just do it and get out
50 if (forceWriteOnly) {
51 aCanvasElement->SetWriteOnly();
52 return;
55 // No need to do a security check if the image used CORS for the load
56 if (CORSUsed)
57 return;
59 if (aCanvasElement->NodePrincipal()->Subsumes(aPrincipal)) {
60 // This canvas has access to that image anyway
61 return;
64 aCanvasElement->SetWriteOnly();
67 bool
68 CoerceDouble(JS::Value v, double* d)
70 if (v.isDouble()) {
71 *d = v.toDouble();
72 } else if (v.isInt32()) {
73 *d = double(v.toInt32());
74 } else if (v.isUndefined()) {
75 *d = 0.0;
76 } else {
77 return false;
79 return true;
82 } // namespace CanvasUtils
83 } // namespace mozilla