1 /* 2 * Copyright (C) 2007-2009 Google 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 are 7 * met: 8 * 9 * * Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * * Redistributions in binary form must reproduce the above 12 * copyright notice, this list of conditions and the following disclaimer 13 * in the documentation and/or other materials provided with the 14 * distribution. 15 * * Neither the name of Google Inc. nor the names of its 16 * contributors may be used to endorse or promote products derived from 17 * this software without specific prior written permission. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 20 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 21 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 22 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 23 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 25 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 29 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 #include "config.h" 33 #include "V8HTMLCanvasElement.h" 34 35 #include "V8CanvasRenderingContext2D.h" 36 #include "V8Node.h" 37 #include "V8WebGLRenderingContext.h" 38 #include "bindings/v8/ExceptionState.h" 39 #include "bindings/v8/V8Binding.h" 40 #include "core/html/HTMLCanvasElement.h" 41 #include "core/html/canvas/Canvas2DContextAttributes.h" 42 #include "core/html/canvas/CanvasRenderingContext.h" 43 #include "core/html/canvas/WebGLContextAttributes.h" 44 #include "core/inspector/InspectorCanvasInstrumentation.h" 45 #include "wtf/MathExtras.h" 46 #include "wtf/text/WTFString.h" 47 48 namespace WebCore { 49 50 void V8HTMLCanvasElement::getContextMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) 51 { 52 v8::Handle<v8::Object> holder = args.Holder(); 53 HTMLCanvasElement* imp = V8HTMLCanvasElement::toNative(holder); 54 String contextId = toWebCoreString(args[0]); 55 RefPtr<CanvasContextAttributes> attrs; 56 if (contextId == "webgl" || contextId == "experimental-webgl" || contextId == "webkit-3d") { 57 RefPtr<WebGLContextAttributes> webGLAttrs = WebGLContextAttributes::create(); 58 if (args.Length() > 1 && args[1]->IsObject()) { 59 v8::Handle<v8::Object> jsAttrs = args[1]->ToObject(); 60 v8::Handle<v8::String> alpha = v8::String::NewSymbol("alpha"); 61 if (jsAttrs->Has(alpha)) 62 webGLAttrs->setAlpha(jsAttrs->Get(alpha)->BooleanValue()); 63 v8::Handle<v8::String> depth = v8::String::NewSymbol("depth"); 64 if (jsAttrs->Has(depth)) 65 webGLAttrs->setDepth(jsAttrs->Get(depth)->BooleanValue()); 66 v8::Handle<v8::String> stencil = v8::String::NewSymbol("stencil"); 67 if (jsAttrs->Has(stencil)) 68 webGLAttrs->setStencil(jsAttrs->Get(stencil)->BooleanValue()); 69 v8::Handle<v8::String> antialias = v8::String::NewSymbol("antialias"); 70 if (jsAttrs->Has(antialias)) 71 webGLAttrs->setAntialias(jsAttrs->Get(antialias)->BooleanValue()); 72 v8::Handle<v8::String> premultipliedAlpha = v8::String::NewSymbol("premultipliedAlpha"); 73 if (jsAttrs->Has(premultipliedAlpha)) 74 webGLAttrs->setPremultipliedAlpha(jsAttrs->Get(premultipliedAlpha)->BooleanValue()); 75 v8::Handle<v8::String> preserveDrawingBuffer = v8::String::NewSymbol("preserveDrawingBuffer"); 76 if (jsAttrs->Has(preserveDrawingBuffer)) 77 webGLAttrs->setPreserveDrawingBuffer(jsAttrs->Get(preserveDrawingBuffer)->BooleanValue()); 78 } 79 attrs = webGLAttrs; 80 } else { 81 RefPtr<Canvas2DContextAttributes> canvas2DAttrs = Canvas2DContextAttributes::create(); 82 if (args.Length() > 1 && args[1]->IsObject()) { 83 v8::Handle<v8::Object> jsAttrs = args[1]->ToObject(); 84 v8::Handle<v8::String> alpha = v8::String::NewSymbol("alpha"); 85 if (jsAttrs->Has(alpha)) 86 canvas2DAttrs->setAlpha(jsAttrs->Get(alpha)->BooleanValue()); 87 } 88 attrs = canvas2DAttrs; 89 } 90 CanvasRenderingContext* result = imp->getContext(contextId, attrs.get()); 91 if (!result) { 92 v8SetReturnValueNull(args); 93 return; 94 } 95 if (result->is2d()) { 96 v8::Handle<v8::Value> v8Result = toV8Fast(static_cast<CanvasRenderingContext2D*>(result), args, imp); 97 if (InspectorInstrumentation::canvasAgentEnabled(imp->document())) { 98 ScriptState* scriptState = ScriptState::forContext(v8::Context::GetCurrent()); 99 ScriptObject context(scriptState, v8::Handle<v8::Object>::Cast(v8Result)); 100 ScriptObject wrapped = InspectorInstrumentation::wrapCanvas2DRenderingContextForInstrumentation(imp->document(), context); 101 if (!wrapped.hasNoValue()) { 102 v8SetReturnValue(args, wrapped.v8Value()); 103 return; 104 } 105 } 106 v8SetReturnValue(args, v8Result); 107 return; 108 } 109 if (result->is3d()) { 110 v8::Handle<v8::Value> v8Result = toV8Fast(static_cast<WebGLRenderingContext*>(result), args, imp); 111 if (InspectorInstrumentation::canvasAgentEnabled(imp->document())) { 112 ScriptState* scriptState = ScriptState::forContext(v8::Context::GetCurrent()); 113 ScriptObject glContext(scriptState, v8::Handle<v8::Object>::Cast(v8Result)); 114 ScriptObject wrapped = InspectorInstrumentation::wrapWebGLRenderingContextForInstrumentation(imp->document(), glContext); 115 if (!wrapped.hasNoValue()) { 116 v8SetReturnValue(args, wrapped.v8Value()); 117 return; 118 } 119 } 120 v8SetReturnValue(args, v8Result); 121 return; 122 } 123 ASSERT_NOT_REACHED(); 124 v8SetReturnValueNull(args); 125 } 126 127 void V8HTMLCanvasElement::toDataURLMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) 128 { 129 v8::Handle<v8::Object> holder = args.Holder(); 130 HTMLCanvasElement* canvas = V8HTMLCanvasElement::toNative(holder); 131 ExceptionState es(args.GetIsolate()); 132 133 String type = toWebCoreString(args[0]); 134 double quality; 135 double* qualityPtr = 0; 136 if (args.Length() > 1 && args[1]->IsNumber()) { 137 quality = args[1]->NumberValue(); 138 qualityPtr = &quality; 139 } 140 141 String result = canvas->toDataURL(type, qualityPtr, es); 142 es.throwIfNeeded(); 143 v8SetReturnValueStringOrUndefined(args, result, args.GetIsolate()); 144 } 145 146 } // namespace WebCore 147