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 "V8CanvasRenderingContext2D.h" 35 #include "V8DOMImplementation.h" 36 #include "V8HTMLDocument.h" 37 #include "V8Node.h" 38 #include "V8SVGDocument.h" 39 #include "V8Touch.h" 40 #include "V8TouchList.h" 41 #include "V8WebGLRenderingContext.h" 42 #include "V8XPathNSResolver.h" 43 #include "V8XPathResult.h" 44 #include "bindings/v8/ExceptionState.h" 45 #include "bindings/v8/ScriptController.h" 46 #include "bindings/v8/V8Binding.h" 47 #include "bindings/v8/V8DOMWrapper.h" 48 #include "bindings/v8/V8WindowShell.h" 49 #include "bindings/v8/custom/V8CustomXPathNSResolver.h" 50 #include "core/dom/Document.h" 51 #include "core/dom/ExceptionCode.h" 52 #include "core/dom/Node.h" 53 #include "core/dom/TouchList.h" 54 #include "core/html/canvas/CanvasRenderingContext.h" 55 #include "core/page/Frame.h" 56 #include "core/xml/DocumentXPathEvaluator.h" 57 #include "core/xml/XPathNSResolver.h" 58 #include "core/xml/XPathResult.h" 59 #include "wtf/RefPtr.h" 60 61 namespace WebCore { 62 63 void V8Document::evaluateMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) 64 { 65 RefPtr<Document> document = V8Document::toNative(args.Holder()); 66 ExceptionState es(args.GetIsolate()); 67 String expression = toWebCoreString(args[0]); 68 RefPtr<Node> contextNode; 69 if (V8Node::HasInstance(args[1], args.GetIsolate(), worldType(args.GetIsolate()))) 70 contextNode = V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1])); 71 72 RefPtr<XPathNSResolver> resolver = toXPathNSResolver(args[2], args.GetIsolate()); 73 if (!resolver && !args[2]->IsNull() && !args[2]->IsUndefined()) { 74 setDOMException(TypeMismatchError, args.GetIsolate()); 75 return; 76 } 77 78 int type = toInt32(args[3]); 79 RefPtr<XPathResult> inResult; 80 if (V8XPathResult::HasInstance(args[4], args.GetIsolate(), worldType(args.GetIsolate()))) 81 inResult = V8XPathResult::toNative(v8::Handle<v8::Object>::Cast(args[4])); 82 83 V8TRYCATCH_VOID(RefPtr<XPathResult>, result, DocumentXPathEvaluator::evaluate(document.get(), expression, contextNode.get(), resolver.get(), type, inResult.get(), es)); 84 if (es.throwIfNeeded()) 85 return; 86 87 v8SetReturnValue(args, toV8Fast(result.release(), args, document.get())); 88 } 89 90 v8::Handle<v8::Object> wrap(Document* impl, v8::Handle<v8::Object> creationContext, v8::Isolate* isolate) 91 { 92 ASSERT(impl); 93 if (impl->isHTMLDocument()) 94 return wrap(toHTMLDocument(impl), creationContext, isolate); 95 if (impl->isSVGDocument()) 96 return wrap(toSVGDocument(impl), creationContext, isolate); 97 v8::Handle<v8::Object> wrapper = V8Document::createWrapper(impl, creationContext, isolate); 98 if (wrapper.IsEmpty()) 99 return wrapper; 100 if (!isolatedWorldForEnteredContext()) { 101 if (Frame* frame = impl->frame()) 102 frame->script()->windowShell(mainThreadNormalWorld())->updateDocumentWrapper(wrapper); 103 } 104 return wrapper; 105 } 106 107 void V8Document::createTouchListMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& args) 108 { 109 RefPtr<TouchList> touchList = TouchList::create(); 110 111 for (int i = 0; i < args.Length(); i++) { 112 Touch* touch = V8DOMWrapper::isWrapperOfType(args[i], &V8Touch::info) ? V8Touch::toNative(args[i]->ToObject()) : 0; 113 touchList->append(touch); 114 } 115 116 v8SetReturnValue(args, toV8(touchList.release(), args.Holder(), args.GetIsolate())); 117 } 118 119 } // namespace WebCore 120