2 * Copyright (C) 2008, 2009 Google 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 are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 #include "XMLHttpRequest.h"
35 #include "V8Binding.h"
36 #include "V8Document.h"
37 #include "V8CustomBinding.h"
38 #include "V8HTMLDocument.h"
39 #include "V8ObjectEventListener.h"
41 #include "V8Utilities.h"
42 #include "WorkerContext.h"
43 #include "WorkerContextExecutionProxy.h"
47 static PassRefPtr<EventListener> getEventListener(XMLHttpRequest* xmlHttpRequest, v8::Local<v8::Value> value, bool findOnly)
50 WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
51 if (workerContextProxy)
52 return workerContextProxy->findOrCreateObjectEventListener(value, false, findOnly);
55 V8Proxy* proxy = V8Proxy::retrieve(xmlHttpRequest->scriptExecutionContext());
57 V8EventListenerList* list = proxy->objectListeners();
58 return findOnly ? list->findWrapper(value, false) : list->findOrCreateWrapper<V8ObjectEventListener>(proxy->frame(), value, false);
61 return PassRefPtr<EventListener>();
64 ACCESSOR_GETTER(XMLHttpRequestOnabort)
66 INC_STATS("DOM.XMLHttpRequest.onabort._get");
67 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
68 if (xmlHttpRequest->onabort()) {
69 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort());
70 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
76 ACCESSOR_SETTER(XMLHttpRequestOnabort)
78 INC_STATS("DOM.XMLHttpRequest.onabort._set");
79 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
80 if (value->IsNull()) {
81 if (xmlHttpRequest->onabort()) {
82 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort());
83 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
84 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
87 // Clear the listener.
88 xmlHttpRequest->setOnabort(0);
90 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
92 xmlHttpRequest->setOnabort(listener);
93 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
98 ACCESSOR_GETTER(XMLHttpRequestOnerror)
100 INC_STATS("DOM.XMLHttpRequest.onerror._get");
101 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
102 if (xmlHttpRequest->onerror()) {
103 RefPtr<V8ObjectEventListener> listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror());
104 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
110 ACCESSOR_SETTER(XMLHttpRequestOnerror)
112 INC_STATS("DOM.XMLHttpRequest.onerror._set");
113 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
114 if (value->IsNull()) {
115 if (xmlHttpRequest->onerror()) {
116 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror());
117 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
118 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
121 // Clear the listener.
122 xmlHttpRequest->setOnerror(0);
124 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
126 xmlHttpRequest->setOnerror(listener);
127 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
132 ACCESSOR_GETTER(XMLHttpRequestOnload)
134 INC_STATS("DOM.XMLHttpRequest.onload._get");
135 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
136 if (xmlHttpRequest->onload()) {
137 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload());
138 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
144 ACCESSOR_SETTER(XMLHttpRequestOnload)
146 INC_STATS("DOM.XMLHttpRequest.onload._set");
147 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
148 if (value->IsNull()) {
149 if (xmlHttpRequest->onload()) {
150 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload());
151 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
152 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
155 xmlHttpRequest->setOnload(0);
158 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
160 xmlHttpRequest->setOnload(listener.get());
161 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
166 ACCESSOR_GETTER(XMLHttpRequestOnloadstart)
168 INC_STATS("DOM.XMLHttpRequest.onloadstart._get");
169 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
170 if (xmlHttpRequest->onloadstart()) {
171 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart());
172 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
178 ACCESSOR_SETTER(XMLHttpRequestOnloadstart)
180 INC_STATS("DOM.XMLHttpRequest.onloadstart._set");
181 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
182 if (value->IsNull()) {
183 if (xmlHttpRequest->onloadstart()) {
184 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart());
185 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
186 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
189 // Clear the listener.
190 xmlHttpRequest->setOnloadstart(0);
192 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
194 xmlHttpRequest->setOnloadstart(listener);
195 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
200 ACCESSOR_GETTER(XMLHttpRequestOnprogress)
202 INC_STATS("DOM.XMLHttpRequest.onprogress._get");
203 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
204 if (xmlHttpRequest->onprogress()) {
205 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress());
206 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
212 ACCESSOR_SETTER(XMLHttpRequestOnprogress)
214 INC_STATS("DOM.XMLHttpRequest.onprogress._set");
215 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
216 if (value->IsNull()) {
217 if (xmlHttpRequest->onprogress()) {
218 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress());
219 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
220 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
223 // Clear the listener.
224 xmlHttpRequest->setOnprogress(0);
226 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
228 xmlHttpRequest->setOnprogress(listener);
229 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
234 ACCESSOR_GETTER(XMLHttpRequestOnreadystatechange)
236 INC_STATS("DOM.XMLHttpRequest.onreadystatechange._get");
237 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
238 if (xmlHttpRequest->onreadystatechange()) {
239 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange());
240 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
246 ACCESSOR_SETTER(XMLHttpRequestOnreadystatechange)
248 INC_STATS("DOM.XMLHttpRequest.onreadystatechange._set");
249 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
250 if (value->IsNull()) {
251 if (xmlHttpRequest->onreadystatechange()) {
252 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange());
253 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
254 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
257 // Clear the listener.
258 xmlHttpRequest->setOnreadystatechange(0);
260 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
262 xmlHttpRequest->setOnreadystatechange(listener.get());
263 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
268 ACCESSOR_GETTER(XMLHttpRequestResponseText)
270 // FIXME: This is only needed because webkit set this getter as custom.
271 // So we need a custom method to avoid forking the IDL file.
272 INC_STATS("DOM.XMLHttpRequest.responsetext._get");
273 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
274 return v8StringOrNull(xmlHttpRequest->responseText());
277 CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener)
279 INC_STATS("DOM.XMLHttpRequest.addEventListener()");
280 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
282 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], false);
284 String type = toWebCoreString(args[0]);
285 bool useCapture = args[2]->BooleanValue();
286 xmlHttpRequest->addEventListener(type, listener, useCapture);
288 createHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
290 return v8::Undefined();
293 CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener)
295 INC_STATS("DOM.XMLHttpRequest.removeEventListener()");
296 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
298 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], true);
300 String type = toWebCoreString(args[0]);
301 bool useCapture = args[2]->BooleanValue();
302 xmlHttpRequest->removeEventListener(type, listener.get(), useCapture);
304 removeHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
307 return v8::Undefined();
310 CALLBACK_FUNC_DECL(XMLHttpRequestOpen)
312 INC_STATS("DOM.XMLHttpRequest.open()");
315 // open(method, url, async)
316 // open(method, url, async, user)
317 // open(method, url, async, user, passwd)
319 if (args.Length() < 2)
320 return throwError("Not enough arguments", V8Proxy::SyntaxError);
322 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
324 String method = toWebCoreString(args[0]);
325 String urlstring = toWebCoreString(args[1]);
326 ScriptExecutionContext* context = 0;
328 WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
329 if (workerContextProxy) {
330 context = workerContextProxy->workerContext();
336 V8Proxy* proxy = V8Proxy::retrieve();
338 return v8::Undefined();
339 context = proxy->frame()->document();
343 KURL url = context->completeURL(urlstring);
345 bool async = (args.Length() < 3) ? true : args[2]->BooleanValue();
347 ExceptionCode ec = 0;
349 if (args.Length() >= 4 && !args[3]->IsUndefined()) {
350 user = toWebCoreStringWithNullCheck(args[3]);
352 if (args.Length() >= 5 && !args[4]->IsUndefined()) {
353 passwd = toWebCoreStringWithNullCheck(args[4]);
354 xmlHttpRequest->open(method, url, async, user, passwd, ec);
356 xmlHttpRequest->open(method, url, async, user, ec);
358 xmlHttpRequest->open(method, url, async, ec);
361 return throwError(ec);
363 return v8::Undefined();
366 static bool IsDocumentType(v8::Handle<v8::Value> value)
368 // FIXME: add other document types.
369 return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
372 CALLBACK_FUNC_DECL(XMLHttpRequestSend)
374 INC_STATS("DOM.XMLHttpRequest.send()");
375 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
377 ExceptionCode ec = 0;
378 if (args.Length() < 1)
379 xmlHttpRequest->send(ec);
381 v8::Handle<v8::Value> arg = args[0];
382 // FIXME: upstream handles "File" objects too.
383 if (IsDocumentType(arg)) {
384 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
385 Document* document = V8DOMWrapper::convertDOMWrapperToNode<Document>(object);
387 xmlHttpRequest->send(document, ec);
389 xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
393 return throwError(ec);
395 return v8::Undefined();
398 CALLBACK_FUNC_DECL(XMLHttpRequestSetRequestHeader) {
399 INC_STATS("DOM.XMLHttpRequest.setRequestHeader()");
400 if (args.Length() < 2)
401 return throwError("Not enough arguments", V8Proxy::SyntaxError);
403 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
404 ExceptionCode ec = 0;
405 String header = toWebCoreString(args[0]);
406 String value = toWebCoreString(args[1]);
407 xmlHttpRequest->setRequestHeader(header, value, ec);
409 return throwError(ec);
410 return v8::Undefined();
413 CALLBACK_FUNC_DECL(XMLHttpRequestGetResponseHeader)
415 INC_STATS("DOM.XMLHttpRequest.getResponseHeader()");
416 if (args.Length() < 1)
417 return throwError("Not enough arguments", V8Proxy::SyntaxError);
419 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
420 ExceptionCode ec = 0;
421 String header = toWebCoreString(args[0]);
422 String result = xmlHttpRequest->getResponseHeader(header, ec);
424 return throwError(ec);
425 return v8StringOrNull(result);
428 CALLBACK_FUNC_DECL(XMLHttpRequestOverrideMimeType)
430 INC_STATS("DOM.XMLHttpRequest.overrideMimeType()");
431 if (args.Length() < 1)
432 return throwError("Not enough arguments", V8Proxy::SyntaxError);
434 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
435 String value = toWebCoreString(args[0]);
436 xmlHttpRequest->overrideMimeType(value);
437 return v8::Undefined();
440 CALLBACK_FUNC_DECL(XMLHttpRequestDispatchEvent)
442 INC_STATS("DOM.XMLHttpRequest.dispatchEvent()");
443 return v8::Undefined();
446 } // namespace WebCore