1 /*
2  * Copyright (C) 2007 Apple Inc. All rights reserved.
3  * Copyright (C) 2010 Torch Mobile (Beijing) Co. Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  *
14  * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
15  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
17  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL APPLE COMPUTER, INC. OR
18  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
19  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
21  * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
22  * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include "config.h"
28 #include "JSHTMLCanvasElement.h"
29 
30 #include "CanvasContextAttributes.h"
31 #include "HTMLCanvasElement.h"
32 #include "JSCanvasRenderingContext2D.h"
33 #if ENABLE(WEBGL)
34 #include "JSWebGLRenderingContext.h"
35 #include "WebGLContextAttributes.h"
36 #endif
37 #include <wtf/GetPtr.h>
38 
39 using namespace JSC;
40 
41 namespace WebCore {
42 
getContext(ExecState * exec)43 JSValue JSHTMLCanvasElement::getContext(ExecState* exec)
44 {
45     HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(impl());
46     const UString& contextId = exec->argument(0).toString(exec);
47     RefPtr<CanvasContextAttributes> attrs;
48 #if ENABLE(WEBGL)
49     if (contextId == "experimental-webgl" || contextId == "webkit-3d") {
50         attrs = WebGLContextAttributes::create();
51         WebGLContextAttributes* webGLAttrs = static_cast<WebGLContextAttributes*>(attrs.get());
52         if (exec->argumentCount() > 1 && exec->argument(1).isObject()) {
53             JSObject* jsAttrs = exec->argument(1).getObject();
54             Identifier alpha(exec, "alpha");
55             if (jsAttrs->hasProperty(exec, alpha))
56                 webGLAttrs->setAlpha(jsAttrs->get(exec, alpha).toBoolean(exec));
57             Identifier depth(exec, "depth");
58             if (jsAttrs->hasProperty(exec, depth))
59                 webGLAttrs->setDepth(jsAttrs->get(exec, depth).toBoolean(exec));
60             Identifier stencil(exec, "stencil");
61             if (jsAttrs->hasProperty(exec, stencil))
62                 webGLAttrs->setStencil(jsAttrs->get(exec, stencil).toBoolean(exec));
63             Identifier antialias(exec, "antialias");
64             if (jsAttrs->hasProperty(exec, antialias))
65                 webGLAttrs->setAntialias(jsAttrs->get(exec, antialias).toBoolean(exec));
66             Identifier premultipliedAlpha(exec, "premultipliedAlpha");
67             if (jsAttrs->hasProperty(exec, premultipliedAlpha))
68                 webGLAttrs->setPremultipliedAlpha(jsAttrs->get(exec, premultipliedAlpha).toBoolean(exec));
69             Identifier preserveDrawingBuffer(exec, "preserveDrawingBuffer");
70             if (jsAttrs->hasProperty(exec, preserveDrawingBuffer))
71                 webGLAttrs->setPreserveDrawingBuffer(jsAttrs->get(exec, preserveDrawingBuffer).toBoolean(exec));
72         }
73     }
74 #endif
75     CanvasRenderingContext* context = canvas->getContext(ustringToString(contextId), attrs.get());
76     if (!context)
77         return jsNull();
78     return toJS(exec, globalObject(), WTF::getPtr(context));
79 }
80 
toDataURL(ExecState * exec)81 JSValue JSHTMLCanvasElement::toDataURL(ExecState* exec)
82 {
83     const String& type = valueToStringWithUndefinedOrNullCheck(exec, exec->argument(0));
84     HTMLCanvasElement* canvas = static_cast<HTMLCanvasElement*>(impl());
85     ExceptionCode ec = 0;
86 
87     JSC::JSValue result;
88     double quality;
89     double* qualityPtr = 0;
90     if (exec->argumentCount() > 1) {
91         JSValue v = exec->argument(1);
92         if (v.isNumber()) {
93             quality = v.toNumber(exec);
94             qualityPtr = &quality;
95         }
96     }
97 
98     result = jsString(exec, canvas->toDataURL(type, qualityPtr, ec));
99     setDOMException(exec, ec);
100     return result;
101 }
102 
103 } // namespace WebCore
104