[V8] Fix Canvas layout test failures in Chromium build.
[WebKit-https.git] / WebCore / bindings / v8 / custom / V8DocumentCustom.cpp
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 "Document.h"
33
34 #include "CanvasRenderingContext.h"
35 #include "ExceptionCode.h"
36 #include "Node.h"
37 #include "XPathNSResolver.h"
38 #include "XPathResult.h"
39
40 #include "V8Binding.h"
41 #include "V8CustomBinding.h"
42 #include "V8CustomXPathNSResolver.h"
43 #include "V8Node.h"
44 #include "V8Proxy.h"
45 #include "V8XPathNSResolver.h"
46 #include "V8XPathResult.h"
47
48 #include <wtf/RefPtr.h>
49
50 namespace WebCore {
51
52 CALLBACK_FUNC_DECL(DocumentEvaluate)
53 {
54     INC_STATS("DOM.Document.evaluate()");
55
56     RefPtr<Document> document = V8DOMWrapper::convertDOMWrapperToNode<Document>(args.Holder());
57     ExceptionCode ec = 0;
58     String expression = toWebCoreString(args[0]);
59     RefPtr<Node> contextNode;
60     if (V8Node::HasInstance(args[1]))
61         contextNode = V8DOMWrapper::convertDOMWrapperToNode<Node>(v8::Handle<v8::Object>::Cast(args[1]));
62
63     RefPtr<XPathNSResolver> resolver;
64     if (V8XPathNSResolver::HasInstance(args[2]))
65         resolver = V8DOMWrapper::convertToNativeObject<XPathNSResolver>(V8ClassIndex::XPATHNSRESOLVER, v8::Handle<v8::Object>::Cast(args[2]));
66     else if (args[2]->IsObject())
67         resolver = V8CustomXPathNSResolver::create(args[2]->ToObject());
68     else if (!args[2]->IsNull() && !args[2]->IsUndefined())
69         return throwError(TYPE_MISMATCH_ERR);
70
71     int type = toInt32(args[3]);
72     RefPtr<XPathResult> inResult;
73     if (V8XPathResult::HasInstance(args[4]))
74         inResult = V8DOMWrapper::convertToNativeObject<XPathResult>(V8ClassIndex::XPATHRESULT, v8::Handle<v8::Object>::Cast(args[4]));
75
76     v8::TryCatch exceptionCatcher;
77     RefPtr<XPathResult> result = document->evaluate(expression, contextNode.get(), resolver.get(), type, inResult.get(), ec);
78     if (exceptionCatcher.HasCaught())
79         return throwError(exceptionCatcher.Exception());
80
81     if (ec)
82         return throwError(ec);
83
84     return V8DOMWrapper::convertToV8Object(V8ClassIndex::XPATHRESULT, result.release());
85 }
86
87 CALLBACK_FUNC_DECL(DocumentGetCSSCanvasContext)
88 {
89     INC_STATS("DOM.Document.getCSSCanvasContext");
90     v8::Handle<v8::Object> holder = args.Holder();
91     Document* imp = V8DOMWrapper::convertDOMWrapperToNode<Document>(holder);
92     String contextId = toWebCoreString(args[0]);
93     String name = toWebCoreString(args[1]);
94     int width = toInt32(args[2]);
95     int height = toInt32(args[3]);
96     CanvasRenderingContext* result = imp->getCSSCanvasContext(contextId, name, width, height);
97     if (result->is2d())
98         return V8DOMWrapper::convertToV8Object(V8ClassIndex::CANVASRENDERINGCONTEXT2D, result);
99     else {
100         // FIXME: Add the conversion to CanvasRenderingContext3D when it is hooked up.
101         ASSERT_NOT_REACHED();
102         return v8::Handle<v8::Value>();
103     }
104 }
105
106 } // namespace WebCore