2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14 * its contributors may be used to endorse or promote products derived
15 * from this software without specific prior written permission.
17 * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20 * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 #include "JSXMLHttpRequest.h"
32 #include "DOMWindow.h"
37 #include "FrameLoader.h"
38 #include "HTMLDocument.h"
39 #include "JSDOMWindowCustom.h"
40 #include "JSDocument.h"
42 #include "JSEventListener.h"
44 #include "XMLHttpRequest.h"
45 #include <runtime/Error.h>
46 #include <interpreter/Interpreter.h>
52 void JSXMLHttpRequest::mark()
56 if (XMLHttpRequestUpload* upload = m_impl->optionalUpload()) {
57 DOMObject* wrapper = getCachedDOMObjectWrapper(*Heap::heap(this)->globalData(), upload);
58 if (wrapper && !wrapper->marked())
62 markIfNotNull(m_impl->onreadystatechange());
63 markIfNotNull(m_impl->onabort());
64 markIfNotNull(m_impl->onerror());
65 markIfNotNull(m_impl->onload());
66 markIfNotNull(m_impl->onloadstart());
67 markIfNotNull(m_impl->onprogress());
69 typedef XMLHttpRequest::EventListenersMap EventListenersMap;
70 typedef XMLHttpRequest::ListenerVector ListenerVector;
71 EventListenersMap& eventListeners = m_impl->eventListeners();
72 for (EventListenersMap::iterator mapIter = eventListeners.begin(); mapIter != eventListeners.end(); ++mapIter) {
73 for (ListenerVector::iterator vecIter = mapIter->second.begin(); vecIter != mapIter->second.end(); ++vecIter)
79 JSValuePtr JSXMLHttpRequest::open(ExecState* exec, const ArgList& args)
82 return throwError(exec, SyntaxError, "Not enough arguments");
84 const KURL& url = impl()->scriptExecutionContext()->completeURL(args.at(exec, 1).toString(exec));
85 String method = args.at(exec, 0).toString(exec);
88 async = args.at(exec, 2).toBoolean(exec);
91 if (args.size() >= 4 && !args.at(exec, 3).isUndefined()) {
92 String user = valueToStringWithNullCheck(exec, args.at(exec, 3));
94 if (args.size() >= 5 && !args.at(exec, 4).isUndefined()) {
95 String password = valueToStringWithNullCheck(exec, args.at(exec, 4));
96 impl()->open(method, url, async, user, password, ec);
98 impl()->open(method, url, async, user, ec);
100 impl()->open(method, url, async, ec);
102 setDOMException(exec, ec);
103 return jsUndefined();
106 JSValuePtr JSXMLHttpRequest::setRequestHeader(ExecState* exec, const ArgList& args)
109 return throwError(exec, SyntaxError, "Not enough arguments");
111 ExceptionCode ec = 0;
112 impl()->setRequestHeader(args.at(exec, 0).toString(exec), args.at(exec, 1).toString(exec), ec);
113 setDOMException(exec, ec);
114 return jsUndefined();
117 JSValuePtr JSXMLHttpRequest::send(ExecState* exec, const ArgList& args)
119 ExceptionCode ec = 0;
123 JSValuePtr val = args.at(exec, 0);
124 if (val.isUndefinedOrNull())
126 else if (val.isObject(&JSDocument::s_info))
127 impl()->send(toDocument(val), ec);
128 else if (val.isObject(&JSFile::s_info))
129 impl()->send(toFile(val), ec);
131 impl()->send(val.toString(exec), ec);
134 int signedLineNumber;
138 exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function);
139 impl()->setLastSendLineNumber(signedLineNumber >= 0 ? signedLineNumber : 0);
140 impl()->setLastSendURL(sourceURL);
142 setDOMException(exec, ec);
143 return jsUndefined();
146 JSValuePtr JSXMLHttpRequest::getResponseHeader(ExecState* exec, const ArgList& args)
149 return throwError(exec, SyntaxError, "Not enough arguments");
151 ExceptionCode ec = 0;
152 JSValuePtr header = jsStringOrNull(exec, impl()->getResponseHeader(args.at(exec, 0).toString(exec), ec));
153 setDOMException(exec, ec);
157 JSValuePtr JSXMLHttpRequest::overrideMimeType(ExecState* exec, const ArgList& args)
160 return throwError(exec, SyntaxError, "Not enough arguments");
162 impl()->overrideMimeType(args.at(exec, 0).toString(exec));
163 return jsUndefined();
166 JSValuePtr JSXMLHttpRequest::addEventListener(ExecState* exec, const ArgList& args)
168 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
170 return jsUndefined();
171 RefPtr<JSEventListener> listener = globalObject->findOrCreateJSEventListener(exec, args.at(exec, 1));
173 return jsUndefined();
174 impl()->addEventListener(args.at(exec, 0).toString(exec), listener.release(), args.at(exec, 2).toBoolean(exec));
175 return jsUndefined();
178 JSValuePtr JSXMLHttpRequest::removeEventListener(ExecState* exec, const ArgList& args)
180 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
182 return jsUndefined();
183 JSEventListener* listener = globalObject->findJSEventListener(exec, args.at(exec, 1));
185 return jsUndefined();
186 impl()->removeEventListener(args.at(exec, 0).toString(exec), listener, args.at(exec, 2).toBoolean(exec));
187 return jsUndefined();
190 JSValuePtr JSXMLHttpRequest::responseText(ExecState* exec) const
192 return jsOwnedStringOrNull(exec, impl()->responseText());
195 } // namespace WebCore