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 "Document.h"
32#include "ExceptionCode.h"
33#include "Frame.h"
34#include "JSDOMWindowCustom.h"
35#include "ScriptController.h"
36#include <runtime/JSLock.h>
37
38namespace WebCore {
39
40using namespace JSC;
41
42PassRefPtr<JSCustomXPathNSResolver> JSCustomXPathNSResolver::create(JSC::ExecState* exec, JSC::JSValue value)
43{
44    if (value.isUndefinedOrNull())
45        return 0;
46
47    JSObject* resolverObject = value.getObject();
48    if (!resolverObject) {
49        setDOMException(exec, TYPE_MISMATCH_ERR);
50        return 0;
51    }
52
53    return adoptRef(new JSCustomXPathNSResolver(resolverObject, asJSDOMWindow(exec->dynamicGlobalObject())));
54}
55
56JSCustomXPathNSResolver::JSCustomXPathNSResolver(JSObject* customResolver, JSDOMWindow* globalObject)
57    : m_customResolver(customResolver)
58    , m_globalObject(globalObject)
59{
60}
61
62JSCustomXPathNSResolver::~JSCustomXPathNSResolver()
63{
64}
65
66String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
67{
68    ASSERT(m_customResolver);
69
70    JSLock lock(SilenceAssertionsOnly);
71
72    ExecState* exec = m_globalObject->globalExec();
73
74    JSValue function = m_customResolver->get(exec, Identifier(exec, "lookupNamespaceURI"));
75    CallData callData;
76    CallType callType = getCallData(function, callData);
77    if (callType == CallTypeNone) {
78        callType = m_customResolver->getCallData(callData);
79        if (callType == CallTypeNone) {
80            // FIXME: Pass actual line number and source URL.
81            m_globalObject->impl()->console()->addMessage(JSMessageSource, LogMessageType, ErrorMessageLevel, "XPathNSResolver does not have a lookupNamespaceURI method.", 0, String());
82            return String();
83        }
84        function = m_customResolver;
85    }
86
87    RefPtr<JSCustomXPathNSResolver> selfProtector(this);
88
89    MarkedArgumentBuffer args;
90    args.append(jsString(exec, prefix));
91
92    m_globalObject->globalData().timeoutChecker.start();
93    JSValue retval = JSC::call(exec, function, callType, callData, m_customResolver, args);
94    m_globalObject->globalData().timeoutChecker.stop();
95
96    String result;
97    if (exec->hadException())
98        reportCurrentException(exec);
99    else {
100        if (!retval.isUndefinedOrNull())
101            result = ustringToString(retval.toString(exec));
102    }
103
104    Document::updateStyleForAllDocuments();
105
106    return result;
107}
108
109} // namespace WebCore
110
111#endif // ENABLE(XPATH)
112