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 "CommonVM.h"
30#include "DOMWindow.h"
31#include "Document.h"
32#include "Frame.h"
33#include "JSDOMExceptionHandling.h"
34#include "JSDOMWindowCustom.h"
35#include "JSExecState.h"
36#include "Page.h"
37#include "PageConsoleClient.h"
38#include <JavaScriptCore/JSLock.h>
39#include <wtf/Ref.h>
40
41namespace WebCore {
42
43using namespace JSC;
44
45ExceptionOr<Ref<JSCustomXPathNSResolver>> JSCustomXPathNSResolver::create(ExecState& state, JSValue value)
46{
47 if (value.isUndefinedOrNull())
48 return Exception { TypeError };
49
50 auto* resolverObject = value.getObject();
51 if (!resolverObject)
52 return Exception { TypeMismatchError };
53
54 VM& vm = state.vm();
55 return adoptRef(*new JSCustomXPathNSResolver(vm, resolverObject, asJSDOMWindow(vm.vmEntryGlobalObject(&state))));
56}
57
58JSCustomXPathNSResolver::JSCustomXPathNSResolver(VM& vm, JSObject* customResolver, JSDOMWindow* globalObject)
59 : m_customResolver(vm, customResolver)
60 , m_globalObject(vm, globalObject)
61{
62}
63
64JSCustomXPathNSResolver::~JSCustomXPathNSResolver() = default;
65
66String JSCustomXPathNSResolver::lookupNamespaceURI(const String& prefix)
67{
68 ASSERT(m_customResolver);
69
70 JSLockHolder lock(commonVM());
71
72 ExecState* exec = m_globalObject->globalExec();
73 VM& vm = exec->vm();
74
75 JSValue function = m_customResolver->get(exec, Identifier::fromString(exec, "lookupNamespaceURI"));
76 CallData callData;
77 CallType callType = getCallData(vm, function, callData);
78 if (callType == CallType::None) {
79 callType = m_customResolver->methodTable(vm)->getCallData(m_customResolver.get(), callData);
80 if (callType == CallType::None) {
81 if (PageConsoleClient* console = m_globalObject->wrapped().console())
82 console->addMessage(MessageSource::JS, MessageLevel::Error, "XPathNSResolver does not have a lookupNamespaceURI method."_s);
83 return String();
84 }
85 function = m_customResolver.get();
86 }
87
88 Ref<JSCustomXPathNSResolver> protectedThis(*this);
89
90 MarkedArgumentBuffer args;
91 args.append(jsStringWithCache(exec, prefix));
92 ASSERT(!args.hasOverflowed());
93
94 NakedPtr<JSC::Exception> exception;
95 JSValue retval = JSExecState::call(exec, function, callType, callData, m_customResolver.get(), args, exception);
96
97 String result;
98 if (exception)
99 reportException(exec, exception);
100 else {
101 if (!retval.isUndefinedOrNull())
102 result = retval.toWTFString(exec);
103 }
104
105 return result;
106}
107
108} // namespace WebCore
109