Home | History | Annotate | Download | only in custom
      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 "V8HTMLDocument.h"
     47 #include "V8IsolatedContext.h"
     48 #include "V8Node.h"
     49 #include "V8Proxy.h"
     50 #include "V8Touch.h"
     51 #include "V8TouchList.h"
     52 #if ENABLE(WEBGL)
     53 #include "V8WebGLRenderingContext.h"
     54 #endif
     55 #include "V8XPathNSResolver.h"
     56 #include "V8XPathResult.h"
     57 
     58 #if ENABLE(SVG)
     59 #include "V8SVGDocument.h"
     60 #endif
     61 
     62 #include <wtf/RefPtr.h>
     63 
     64 namespace WebCore {
     65 
     66 #if ENABLE(XPATH)
     67 v8::Handle<v8::Value> V8Document::evaluateCallback(const v8::Arguments& args)
     68 {
     69     INC_STATS("DOM.Document.evaluate()");
     70 
     71     RefPtr<Document> document = V8Document::toNative(args.Holder());
     72     ExceptionCode ec = 0;
     73     String expression = toWebCoreString(args[0]);
     74     RefPtr<Node> contextNode;
     75     if (V8Node::HasInstance(args[1]))
     76         contextNode = V8Node::toNative(v8::Handle<v8::Object>::Cast(args[1]));
     77 
     78     RefPtr<XPathNSResolver> resolver = V8DOMWrapper::getXPathNSResolver(args[2]);
     79     if (!resolver && !args[2]->IsNull() && !args[2]->IsUndefined())
     80         return throwError(TYPE_MISMATCH_ERR);
     81 
     82     int type = toInt32(args[3]);
     83     RefPtr<XPathResult> inResult;
     84     if (V8XPathResult::HasInstance(args[4]))
     85         inResult = V8XPathResult::toNative(v8::Handle<v8::Object>::Cast(args[4]));
     86 
     87     v8::TryCatch exceptionCatcher;
     88     RefPtr<XPathResult> result = document->evaluate(expression, contextNode.get(), resolver.get(), type, inResult.get(), ec);
     89     if (exceptionCatcher.HasCaught())
     90         return throwError(exceptionCatcher.Exception());
     91 
     92     if (ec)
     93         return throwError(ec);
     94 
     95     return toV8(result.release());
     96 }
     97 #endif
     98 
     99 v8::Handle<v8::Value> V8Document::getCSSCanvasContextCallback(const v8::Arguments& args)
    100 {
    101     INC_STATS("DOM.Document.getCSSCanvasContext");
    102     v8::Handle<v8::Object> holder = args.Holder();
    103     Document* imp = V8Document::toNative(holder);
    104     String contextId = toWebCoreString(args[0]);
    105     String name = toWebCoreString(args[1]);
    106     int width = toInt32(args[2]);
    107     int height = toInt32(args[3]);
    108     CanvasRenderingContext* result = imp->getCSSCanvasContext(contextId, name, width, height);
    109     if (!result)
    110         return v8::Undefined();
    111     if (result->is2d())
    112         return toV8(static_cast<CanvasRenderingContext2D*>(result));
    113 #if ENABLE(WEBGL)
    114     else if (result->is3d())
    115         return toV8(static_cast<WebGLRenderingContext*>(result));
    116 #endif // ENABLE(WEBGL)
    117     ASSERT_NOT_REACHED();
    118     return v8::Undefined();
    119 }
    120 
    121 v8::Handle<v8::Value> toV8(Document* impl, bool forceNewObject)
    122 {
    123     if (!impl)
    124         return v8::Null();
    125     if (impl->isHTMLDocument())
    126         return toV8(static_cast<HTMLDocument*>(impl), forceNewObject);
    127 #if ENABLE(SVG)
    128     if (impl->isSVGDocument())
    129         return toV8(static_cast<SVGDocument*>(impl), forceNewObject);
    130 #endif
    131     v8::Handle<v8::Object> wrapper = V8Document::wrap(impl, forceNewObject);
    132     if (wrapper.IsEmpty())
    133         return wrapper;
    134     if (!V8IsolatedContext::getEntered()) {
    135         if (V8Proxy* proxy = V8Proxy::retrieve(impl->frame()))
    136             proxy->windowShell()->updateDocumentWrapper(wrapper);
    137     }
    138     return wrapper;
    139 }
    140 
    141 #if ENABLE(TOUCH_EVENTS)
    142 v8::Handle<v8::Value> V8Document::createTouchListCallback(const v8::Arguments& args)
    143 {
    144     RefPtr<TouchList> touchList = TouchList::create();
    145 
    146     for (int i = 0; i < args.Length(); i++) {
    147         if (!args[i]->IsObject())
    148             return v8::Undefined();
    149         touchList->append(V8Touch::toNative(args[i]->ToObject()));
    150     }
    151 
    152     return toV8(touchList.release());
    153 }
    154 #endif
    155 
    156 } // namespace WebCore
    157