1 /*
2 * Copyright (C) 2008, 2009 Apple Inc. All rights reserved.
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 *
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.
16 *
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.
27 */
28
29 #include "config.h"
30 #include "JSXMLHttpRequest.h"
31
32 #include "DOMWindow.h"
33 #include "Document.h"
34 #include "Event.h"
35 #include "File.h"
36 #include "Frame.h"
37 #include "FrameLoader.h"
38 #include "HTMLDocument.h"
39 #include "JSDOMWindowCustom.h"
40 #include "JSDocument.h"
41 #include "JSEvent.h"
42 #include "JSEventListener.h"
43 #include "JSFile.h"
44 #include "XMLHttpRequest.h"
45 #include <runtime/Error.h>
46 #include <interpreter/Interpreter.h>
47
48 using namespace JSC;
49
50 namespace WebCore {
51
markChildren(MarkStack & markStack)52 void JSXMLHttpRequest::markChildren(MarkStack& markStack)
53 {
54 Base::markChildren(markStack);
55
56 if (XMLHttpRequestUpload* upload = m_impl->optionalUpload()) {
57 DOMObject* wrapper = getCachedDOMObjectWrapper(*Heap::heap(this)->globalData(), upload);
58 if (wrapper)
59 markStack.append(wrapper);
60 }
61
62 markIfNotNull(markStack, m_impl->onreadystatechange());
63 markIfNotNull(markStack, m_impl->onabort());
64 markIfNotNull(markStack, m_impl->onerror());
65 markIfNotNull(markStack, m_impl->onload());
66 markIfNotNull(markStack, m_impl->onloadstart());
67 markIfNotNull(markStack, m_impl->onprogress());
68
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)
74 (*vecIter)->markJSFunction(markStack);
75 }
76 }
77
78 // Custom functions
open(ExecState * exec,const ArgList & args)79 JSValue JSXMLHttpRequest::open(ExecState* exec, const ArgList& args)
80 {
81 if (args.size() < 2)
82 return throwError(exec, SyntaxError, "Not enough arguments");
83
84 const KURL& url = impl()->scriptExecutionContext()->completeURL(args.at(1).toString(exec));
85 String method = args.at(0).toString(exec);
86 bool async = true;
87 if (args.size() >= 3)
88 async = args.at(2).toBoolean(exec);
89
90 ExceptionCode ec = 0;
91 if (args.size() >= 4 && !args.at(3).isUndefined()) {
92 String user = valueToStringWithNullCheck(exec, args.at(3));
93
94 if (args.size() >= 5 && !args.at(4).isUndefined()) {
95 String password = valueToStringWithNullCheck(exec, args.at(4));
96 impl()->open(method, url, async, user, password, ec);
97 } else
98 impl()->open(method, url, async, user, ec);
99 } else
100 impl()->open(method, url, async, ec);
101
102 setDOMException(exec, ec);
103 return jsUndefined();
104 }
105
setRequestHeader(ExecState * exec,const ArgList & args)106 JSValue JSXMLHttpRequest::setRequestHeader(ExecState* exec, const ArgList& args)
107 {
108 if (args.size() < 2)
109 return throwError(exec, SyntaxError, "Not enough arguments");
110
111 ExceptionCode ec = 0;
112 impl()->setRequestHeader(args.at(0).toString(exec), args.at(1).toString(exec), ec);
113 setDOMException(exec, ec);
114 return jsUndefined();
115 }
116
send(ExecState * exec,const ArgList & args)117 JSValue JSXMLHttpRequest::send(ExecState* exec, const ArgList& args)
118 {
119 ExceptionCode ec = 0;
120 if (args.isEmpty())
121 impl()->send(ec);
122 else {
123 JSValue val = args.at(0);
124 if (val.isUndefinedOrNull())
125 impl()->send(ec);
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);
130 else
131 impl()->send(val.toString(exec), ec);
132 }
133
134 int signedLineNumber;
135 intptr_t sourceID;
136 UString sourceURL;
137 JSValue function;
138 exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function);
139 impl()->setLastSendLineNumber(signedLineNumber >= 0 ? signedLineNumber : 0);
140 impl()->setLastSendURL(sourceURL);
141
142 setDOMException(exec, ec);
143 return jsUndefined();
144 }
145
getResponseHeader(ExecState * exec,const ArgList & args)146 JSValue JSXMLHttpRequest::getResponseHeader(ExecState* exec, const ArgList& args)
147 {
148 if (args.size() < 1)
149 return throwError(exec, SyntaxError, "Not enough arguments");
150
151 ExceptionCode ec = 0;
152 JSValue header = jsStringOrNull(exec, impl()->getResponseHeader(args.at(0).toString(exec), ec));
153 setDOMException(exec, ec);
154 return header;
155 }
156
overrideMimeType(ExecState * exec,const ArgList & args)157 JSValue JSXMLHttpRequest::overrideMimeType(ExecState* exec, const ArgList& args)
158 {
159 if (args.size() < 1)
160 return throwError(exec, SyntaxError, "Not enough arguments");
161
162 impl()->overrideMimeType(args.at(0).toString(exec));
163 return jsUndefined();
164 }
165
addEventListener(ExecState * exec,const ArgList & args)166 JSValue JSXMLHttpRequest::addEventListener(ExecState* exec, const ArgList& args)
167 {
168 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
169 if (!globalObject)
170 return jsUndefined();
171 RefPtr<JSEventListener> listener = globalObject->findOrCreateJSEventListener(args.at(1));
172 if (!listener)
173 return jsUndefined();
174 impl()->addEventListener(args.at(0).toString(exec), listener.release(), args.at(2).toBoolean(exec));
175 return jsUndefined();
176 }
177
removeEventListener(ExecState * exec,const ArgList & args)178 JSValue JSXMLHttpRequest::removeEventListener(ExecState* exec, const ArgList& args)
179 {
180 JSDOMGlobalObject* globalObject = toJSDOMGlobalObject(impl()->scriptExecutionContext());
181 if (!globalObject)
182 return jsUndefined();
183 JSEventListener* listener = globalObject->findJSEventListener(args.at(1));
184 if (!listener)
185 return jsUndefined();
186 impl()->removeEventListener(args.at(0).toString(exec), listener, args.at(2).toBoolean(exec));
187 return jsUndefined();
188 }
189
responseText(ExecState * exec) const190 JSValue JSXMLHttpRequest::responseText(ExecState* exec) const
191 {
192 return jsOwnedStringOrNull(exec, impl()->responseText());
193 }
194
195 } // namespace WebCore
196