1/*
2 * Copyright (C) 2007, 2013 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 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 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#include "Document.h"
30#include "ExceptionCode.h"
31#include "Frame.h"
32#include "JSDOMWindowCustom.h"
33#include "JSMainThreadExecState.h"
34#include "Page.h"
35#include "PageConsole.h"
36#include "SecurityOrigin.h"
37#include <runtime/JSLock.h>
38#include <wtf/Ref.h>
39
40namespace WebCore {
41
42using namespace JSC;
43
44PassRefPtr<JSCustomXPathNSResolver> JSCustomXPathNSResolver::create(ExecState* exec, JSValue value)
45{
46    if (value.isUndefinedOrNull())
47        return 0;
48
49    JSObject* resolverObject = value.getObject();
50    if (!resolverObject) {
51        setDOMException(exec, TYPE_MISMATCH_ERR);
52        return 0;
53    }
54
55    return adoptRef(new JSCustomXPathNSResolver(exec, resolverObject, asJSDOMWindow(exec->vmEntryGlobalObject())));
56}
57
58JSCustomXPathNSResolver::JSCustomXPathNSResolver(ExecState* exec, JSObject* customResolver, JSDOMWindow* globalObject)
59    : m_customResolver(exec->vm(), customResolver)
60    , m_globalObject(exec->vm(), globalObject)
61{
62}
63
64JSCustomXPathNSResolver::~JSCustomXPathNSResolver()
65{
66}
67
68String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
69{
70    ASSERT(m_customResolver);
71
72    JSLockHolder lock(JSDOMWindowBase::commonVM());
73
74    ExecState* exec = m_globalObject->globalExec();
75
76    JSValue function = m_customResolver->get(exec, Identifier(exec, "lookupNamespaceURI"));
77    CallData callData;
78    CallType callType = getCallData(function, callData);
79    if (callType == CallTypeNone) {
80        callType = m_customResolver->methodTable()->getCallData(m_customResolver.get(), callData);
81        if (callType == CallTypeNone) {
82            // FIXME: <http://webkit.org/b/114312> JSCustomXPathNSResolver::lookupNamespaceURI Console Message should include Line, Column, and SourceURL
83            if (PageConsole* console = m_globalObject->impl().pageConsole())
84                console->addMessage(MessageSource::JS, MessageLevel::Error, ASCIILiteral("XPathNSResolver does not have a lookupNamespaceURI method."));
85            return String();
86        }
87        function = m_customResolver.get();
88    }
89
90    Ref<JSCustomXPathNSResolver> selfProtector(*this);
91
92    MarkedArgumentBuffer args;
93    args.append(jsStringWithCache(exec, prefix));
94
95    JSValue exception;
96    JSValue retval = JSMainThreadExecState::call(exec, function, callType, callData, m_customResolver.get(), args, &exception);
97
98    String result;
99    if (exception)
100        reportException(exec, exception);
101    else {
102        if (!retval.isUndefinedOrNull())
103            result = retval.toString(exec)->value(exec);
104    }
105
106    return result;
107}
108
109} // namespace WebCore
110