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"
33 #include "DOMFormData.h"
34 #include "DOMWindow.h"
38 #include "FrameLoader.h"
39 #include "HTMLDocument.h"
40 #include "InspectorInstrumentation.h"
41 #include "JSArrayBuffer.h"
43 #include "JSDOMFormData.h"
44 #include "JSDOMWindowCustom.h"
45 #include "JSDocument.h"
47 #include "JSEventListener.h"
48 #include "XMLHttpRequest.h"
49 #include <runtime/Error.h>
50 #include <interpreter/Interpreter.h>
51 #include <wtf/ArrayBuffer.h>
57 void JSXMLHttpRequest::visitChildren(JSCell* cell, SlotVisitor& visitor)
59 JSXMLHttpRequest* thisObject = jsCast<JSXMLHttpRequest*>(cell);
60 ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
61 COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
62 ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
63 Base::visitChildren(thisObject, visitor);
65 if (XMLHttpRequestUpload* upload = thisObject->m_impl->optionalUpload())
66 visitor.addOpaqueRoot(upload);
68 if (Document* responseDocument = thisObject->m_impl->optionalResponseXML())
69 visitor.addOpaqueRoot(responseDocument);
71 if (ArrayBuffer* responseArrayBuffer = thisObject->m_impl->optionalResponseArrayBuffer())
72 visitor.addOpaqueRoot(responseArrayBuffer);
74 #if ENABLE(XHR_RESPONSE_BLOB)
75 if (Blob* responseBlob = thisObject->m_impl->optionalResponseBlob())
76 visitor.addOpaqueRoot(responseBlob);
79 thisObject->m_impl->visitJSEventListeners(visitor);
83 JSValue JSXMLHttpRequest::open(ExecState* exec)
85 if (exec->argumentCount() < 2)
86 return throwError(exec, createSyntaxError(exec, "Not enough arguments"));
88 const KURL& url = impl()->scriptExecutionContext()->completeURL(ustringToString(exec->argument(1).toString(exec)));
89 String method = ustringToString(exec->argument(0).toString(exec));
92 if (exec->argumentCount() >= 3) {
93 bool async = exec->argument(2).toBoolean(exec);
95 if (exec->argumentCount() >= 4 && !exec->argument(3).isUndefined()) {
96 String user = valueToStringWithNullCheck(exec, exec->argument(3));
98 if (exec->argumentCount() >= 5 && !exec->argument(4).isUndefined()) {
99 String password = valueToStringWithNullCheck(exec, exec->argument(4));
100 impl()->open(method, url, async, user, password, ec);
102 impl()->open(method, url, async, user, ec);
104 impl()->open(method, url, async, ec);
106 impl()->open(method, url, ec);
108 setDOMException(exec, ec);
109 return jsUndefined();
112 JSValue JSXMLHttpRequest::send(ExecState* exec)
114 InspectorInstrumentation::willSendXMLHttpRequest(impl()->scriptExecutionContext(), impl()->url());
116 ExceptionCode ec = 0;
117 if (!exec->argumentCount())
120 JSValue val = exec->argument(0);
121 if (val.isUndefinedOrNull())
123 else if (val.inherits(&JSDocument::s_info))
124 impl()->send(toDocument(val), ec);
125 else if (val.inherits(&JSBlob::s_info))
126 impl()->send(toBlob(val), ec);
127 else if (val.inherits(&JSDOMFormData::s_info))
128 impl()->send(toDOMFormData(val), ec);
129 else if (val.inherits(&JSArrayBuffer::s_info))
130 impl()->send(toArrayBuffer(val), ec);
132 impl()->send(ustringToString(val.toString(exec)), ec);
135 int signedLineNumber;
139 exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function);
140 impl()->setLastSendLineNumber(signedLineNumber >= 0 ? signedLineNumber : 0);
141 impl()->setLastSendURL(ustringToString(sourceURL));
143 setDOMException(exec, ec);
144 return jsUndefined();
147 JSValue JSXMLHttpRequest::responseText(ExecState* exec) const
149 ExceptionCode ec = 0;
150 String text = impl()->responseText(ec);
152 setDOMException(exec, ec);
153 return jsUndefined();
155 return jsOwnedStringOrNull(exec, text);
158 JSValue JSXMLHttpRequest::response(ExecState* exec) const
160 switch (impl()->responseTypeCode()) {
161 case XMLHttpRequest::ResponseTypeDefault:
162 case XMLHttpRequest::ResponseTypeText:
163 return responseText(exec);
165 case XMLHttpRequest::ResponseTypeDocument:
167 ExceptionCode ec = 0;
168 Document* document = impl()->responseXML(ec);
170 setDOMException(exec, ec);
171 return jsUndefined();
173 return toJS(exec, globalObject(), document);
176 case XMLHttpRequest::ResponseTypeBlob:
177 #if ENABLE(XHR_RESPONSE_BLOB)
179 ExceptionCode ec = 0;
180 Blob* blob = impl()->responseBlob(ec);
182 setDOMException(exec, ec);
183 return jsUndefined();
185 return toJS(exec, globalObject(), blob);
188 return jsUndefined();
191 case XMLHttpRequest::ResponseTypeArrayBuffer:
193 ExceptionCode ec = 0;
194 ArrayBuffer* arrayBuffer = impl()->responseArrayBuffer(ec);
196 setDOMException(exec, ec);
197 return jsUndefined();
199 return toJS(exec, globalObject(), arrayBuffer);
203 return jsUndefined();
206 } // namespace WebCore