1 /*
2 * Copyright (C) 2007 Alexey Proskuryakov (ap@nypop.com)
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 */
25
26 #include "config.h"
27 #include "JSCustomXPathNSResolver.h"
28
29 #if ENABLE(XPATH)
30
31 #include "CString.h"
32 #include "Console.h"
33 #include "DOMWindow.h"
34 #include "Document.h"
35 #include "ExceptionCode.h"
36 #include "Frame.h"
37 #include "JSDOMWindowCustom.h"
38 #include "JSDOMBinding.h"
39 #include "ScriptController.h"
40 #include <runtime/JSLock.h>
41
42 namespace WebCore {
43
44 using namespace JSC;
45
create(JSC::ExecState * exec,JSC::JSValuePtr value)46 PassRefPtr<JSCustomXPathNSResolver> JSCustomXPathNSResolver::create(JSC::ExecState* exec, JSC::JSValuePtr value)
47 {
48 if (value.isUndefinedOrNull())
49 return 0;
50
51 JSObject* resolverObject = value.getObject();
52 if (!resolverObject) {
53 setDOMException(exec, TYPE_MISMATCH_ERR);
54 return 0;
55 }
56
57 return adoptRef(new JSCustomXPathNSResolver(resolverObject, asJSDOMWindow(exec->dynamicGlobalObject())->impl()->frame()));
58 }
59
JSCustomXPathNSResolver(JSObject * customResolver,Frame * frame)60 JSCustomXPathNSResolver::JSCustomXPathNSResolver(JSObject* customResolver, Frame* frame)
61 : m_customResolver(customResolver)
62 , m_frame(frame)
63 {
64 }
65
~JSCustomXPathNSResolver()66 JSCustomXPathNSResolver::~JSCustomXPathNSResolver()
67 {
68 }
69
lookupNamespaceURI(const String & prefix)70 String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
71 {
72 ASSERT(m_customResolver);
73
74 if (!m_frame)
75 return String();
76 if (!m_frame->script()->isEnabled())
77 return String();
78
79 JSLock lock(false);
80
81 JSGlobalObject* globalObject = m_frame->script()->globalObject();
82 ExecState* exec = globalObject->globalExec();
83
84 JSValuePtr function = m_customResolver->get(exec, Identifier(exec, "lookupNamespaceURI"));
85 CallData callData;
86 CallType callType = function.getCallData(callData);
87 if (callType == CallTypeNone) {
88 callType = m_customResolver->getCallData(callData);
89 if (callType == CallTypeNone) {
90 // FIXME: Pass actual line number and source URL.
91 m_frame->domWindow()->console()->addMessage(JSMessageSource, ErrorMessageLevel, "XPathNSResolver does not have a lookupNamespaceURI method.", 0, String());
92 return String();
93 }
94 function = m_customResolver;
95 }
96
97 RefPtr<JSCustomXPathNSResolver> selfProtector(this);
98
99 ArgList args;
100 args.append(jsString(exec, prefix));
101
102 globalObject->startTimeoutCheck();
103 JSValuePtr retval = call(exec, function, callType, callData, m_customResolver, args);
104 globalObject->stopTimeoutCheck();
105
106 String result;
107 if (exec->hadException())
108 reportCurrentException(exec);
109 else {
110 if (!retval.isUndefinedOrNull())
111 result = retval.toString(exec);
112 }
113
114 Document::updateDocumentsRendering();
115
116 return result;
117 }
118
119 } // namespace WebCore
120
121 #endif // ENABLE(XPATH)
122