1 /* 2 * Copyright (C) 2007-2009 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 8 * * Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer. 10 * * Redistributions in binary form must reproduce the above 11 * copyright notice, this list of conditions and the following disclaimer 12 * in the documentation and/or other materials provided with the 13 * distribution. 14 * * Neither the name of Google Inc. nor the names of its 15 * contributors may be used to endorse or promote products derived from 16 * this software without specific prior written permission. 17 * 18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29 */ 30 31 #include "config.h" 32 #include "V8Document.h" 33 34 #include "CanvasRenderingContext.h" 35 #include "Document.h" 36 #include "ExceptionCode.h" 37 #include "Node.h" 38 #include "TouchList.h" 39 #include "XPathNSResolver.h" 40 #include "XPathResult.h" 41 42 #include "V8Binding.h" 43 #include "V8CanvasRenderingContext2D.h" 44 #include "V8CustomXPathNSResolver.h" 45 #include "V8DOMImplementation.h" 46 #include "V8DOMWrapper.h" 47 #include "V8HTMLDocument.h" 48 #include "V8IsolatedContext.h" 49 #include "V8Node.h" 50 #include "V8Proxy.h" 51 #include "V8Touch.h" 52 #include "V8TouchList.h" 53 #if ENABLE(WEBGL) 54 #include "V8WebGLRenderingContext.h" 55 #endif 56 #include "V8XPathNSResolver.h" 57 #include "V8XPathResult.h" 58 59 #if ENABLE(SVG) 60 #include "V8SVGDocument.h" 61 #endif 62 63 #include <wtf/RefPtr.h> 64 65 namespace WebCore { 66 67 #if ENABLE(XPATH) 68 v8::Handle<v8::Value> V8Document::evaluateCallback(const v8::Arguments& args) 69 { 70 INC_STATS("DOM.Document.evaluate()"); 71 72 RefPtr<Document> document = V8Document::toNative(args.Holder()); 73 ExceptionCode ec = 0; 74 String expression = toWebCoreString(args[0]); 75 RefPtr<Node> contextNode; 76 if (V8Node::HasInstance(args[1])) 77 contextNode = V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])); 78 79 RefPtr<XPathNSResolver> resolver = V8DOMWrapper::getXPathNSResolver(args[2]); 80 if (!resolver && !args[2]->IsNull() && !args[2]->IsUndefined()) 81 return throwError(TYPE_MISMATCH_ERR); 82 83 int type = toInt32(args[3]); 84 RefPtr<XPathResult> inResult; 85 if (V8XPathResult::HasInstance(args[4])) 86 inResult = V8XPathResult::toNative(v8::Handle<v8::Object>::Cast(args[4])); 87 88 v8::TryCatch exceptionCatcher; 89 RefPtr<XPathResult> result = document->evaluate(expression, contextNode.get(), resolver.get(), type, inResult.get(), ec); 90 if (exceptionCatcher.HasCaught()) 91 return throwError(exceptionCatcher.Exception()); 92 93 if (ec) 94 return throwError(ec); 95 96 return toV8(result.release()); 97 } 98 #endif 99 100 v8::Handle<v8::Value> V8Document::getCSSCanvasContextCallback(const v8::Arguments& args) 101 { 102 INC_STATS("DOM.Document.getCSSCanvasContext"); 103 v8::Handle<v8::Object> holder = args.Holder(); 104 Document* imp = V8Document::toNative(holder); 105 String contextId = toWebCoreString(args[0]); 106 String name = toWebCoreString(args[1]); 107 int width = toInt32(args[2]); 108 int height = toInt32(args[3]); 109 CanvasRenderingContext* result = imp->getCSSCanvasContext(contextId, name, width, height); 110 if (!result) 111 return v8::Undefined(); 112 if (result->is2d()) 113 return toV8(static_cast<CanvasRenderingContext2D*>(result)); 114 #if ENABLE(WEBGL) 115 else if (result->is3d()) 116 return toV8(static_cast<WebGLRenderingContext*>(result)); 117 #endif // ENABLE(WEBGL) 118 ASSERT_NOT_REACHED(); 119 return v8::Undefined(); 120 } 121 122 v8::Handle<v8::Value> toV8(Document* impl, bool forceNewObject) 123 { 124 if (!impl) 125 return v8::Null(); 126 if (impl->isHTMLDocument()) 127 return toV8(static_cast<HTMLDocument*>(impl), forceNewObject); 128 #if ENABLE(SVG) 129 if (impl->isSVGDocument()) 130 return toV8(static_cast<SVGDocument*>(impl), forceNewObject); 131 #endif 132 v8::Handle<v8::Object> wrapper = V8Document::wrap(impl, forceNewObject); 133 if (wrapper.IsEmpty()) 134 return wrapper; 135 if (!V8IsolatedContext::getEntered()) { 136 if (V8Proxy* proxy = V8Proxy::retrieve(impl->frame())) 137 proxy->windowShell()->updateDocumentWrapper(wrapper); 138 } 139 return wrapper; 140 } 141 142 #if ENABLE(TOUCH_EVENTS) 143 v8::Handle<v8::Value> V8Document::createTouchListCallback(const v8::Arguments& args) 144 { 145 RefPtr<TouchList> touchList = TouchList::create(); 146 147 for (int i = 0; i < args.Length(); i++) { 148 Touch* touch = V8DOMWrapper::isWrapperOfType(args[i], &V8Touch::info) ? V8Touch::toNative(args[i]->ToObject()) : 0; 149 touchList->append(touch); 150 } 151 152 return toV8(touchList.release()); 153 } 154 #endif 155 156 } // namespace WebCore 157