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 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 return findOnly ? proxy->FindObjectEventListener(value, false) : proxy->FindOrCreateObjectEventListener(value, false);
59 return PassRefPtr<EventListener>();
62 ACCESSOR_GETTER(XMLHttpRequestOnabort)
64 INC_STATS("DOM.XMLHttpRequest.onabort._get");
65 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
66 if (xmlHttpRequest->onabort()) {
67 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort());
68 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
74 ACCESSOR_SETTER(XMLHttpRequestOnabort)
76 INC_STATS("DOM.XMLHttpRequest.onabort._set");
77 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
78 if (value->IsNull()) {
79 if (xmlHttpRequest->onabort()) {
80 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onabort());
81 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
82 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
85 // Clear the listener.
86 xmlHttpRequest->setOnabort(0);
88 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
90 xmlHttpRequest->setOnabort(listener);
91 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
96 ACCESSOR_GETTER(XMLHttpRequestOnerror)
98 INC_STATS("DOM.XMLHttpRequest.onerror._get");
99 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
100 if (xmlHttpRequest->onerror()) {
101 RefPtr<V8ObjectEventListener> listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror());
102 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
108 ACCESSOR_SETTER(XMLHttpRequestOnerror)
110 INC_STATS("DOM.XMLHttpRequest.onerror._set");
111 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
112 if (value->IsNull()) {
113 if (xmlHttpRequest->onerror()) {
114 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onerror());
115 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
116 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
119 // Clear the listener.
120 xmlHttpRequest->setOnerror(0);
122 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
124 xmlHttpRequest->setOnerror(listener);
125 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
130 ACCESSOR_GETTER(XMLHttpRequestOnload)
132 INC_STATS("DOM.XMLHttpRequest.onload._get");
133 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
134 if (xmlHttpRequest->onload()) {
135 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload());
136 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
142 ACCESSOR_SETTER(XMLHttpRequestOnload)
144 INC_STATS("DOM.XMLHttpRequest.onload._set");
145 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
146 if (value->IsNull()) {
147 if (xmlHttpRequest->onload()) {
148 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onload());
149 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
150 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
153 xmlHttpRequest->setOnload(0);
156 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
158 xmlHttpRequest->setOnload(listener.get());
159 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
164 ACCESSOR_GETTER(XMLHttpRequestOnloadstart)
166 INC_STATS("DOM.XMLHttpRequest.onloadstart._get");
167 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
168 if (xmlHttpRequest->onloadstart()) {
169 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart());
170 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
176 ACCESSOR_SETTER(XMLHttpRequestOnloadstart)
178 INC_STATS("DOM.XMLHttpRequest.onloadstart._set");
179 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
180 if (value->IsNull()) {
181 if (xmlHttpRequest->onloadstart()) {
182 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onloadstart());
183 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
184 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
187 // Clear the listener.
188 xmlHttpRequest->setOnloadstart(0);
190 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
192 xmlHttpRequest->setOnloadstart(listener);
193 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
198 ACCESSOR_GETTER(XMLHttpRequestOnprogress)
200 INC_STATS("DOM.XMLHttpRequest.onprogress._get");
201 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
202 if (xmlHttpRequest->onprogress()) {
203 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress());
204 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
210 ACCESSOR_SETTER(XMLHttpRequestOnprogress)
212 INC_STATS("DOM.XMLHttpRequest.onprogress._set");
213 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
214 if (value->IsNull()) {
215 if (xmlHttpRequest->onprogress()) {
216 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onprogress());
217 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
218 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
221 // Clear the listener.
222 xmlHttpRequest->setOnprogress(0);
224 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
226 xmlHttpRequest->setOnprogress(listener);
227 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
232 ACCESSOR_GETTER(XMLHttpRequestOnreadystatechange)
234 INC_STATS("DOM.XMLHttpRequest.onreadystatechange._get");
235 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
236 if (xmlHttpRequest->onreadystatechange()) {
237 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange());
238 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
244 ACCESSOR_SETTER(XMLHttpRequestOnreadystatechange)
246 INC_STATS("DOM.XMLHttpRequest.onreadystatechange._set");
247 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
248 if (value->IsNull()) {
249 if (xmlHttpRequest->onreadystatechange()) {
250 V8ObjectEventListener* listener = static_cast<V8ObjectEventListener*>(xmlHttpRequest->onreadystatechange());
251 v8::Local<v8::Object> v8Listener = listener->getListenerObject();
252 removeHiddenDependency(info.Holder(), v8Listener, V8Custom::kXMLHttpRequestCacheIndex);
255 // Clear the listener.
256 xmlHttpRequest->setOnreadystatechange(0);
258 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, value, false);
260 xmlHttpRequest->setOnreadystatechange(listener.get());
261 createHiddenDependency(info.Holder(), value, V8Custom::kXMLHttpRequestCacheIndex);
266 ACCESSOR_GETTER(XMLHttpRequestResponseText)
268 // FIXME: This is only needed because webkit set this getter as custom.
269 // So we need a custom method to avoid forking the IDL file.
270 INC_STATS("DOM.XMLHttpRequest.responsetext._get");
271 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
272 return v8StringOrNull(xmlHttpRequest->responseText());
275 CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener)
277 INC_STATS("DOM.XMLHttpRequest.addEventListener()");
278 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
280 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], false);
282 String type = toWebCoreString(args[0]);
283 bool useCapture = args[2]->BooleanValue();
284 xmlHttpRequest->addEventListener(type, listener, useCapture);
286 createHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
288 return v8::Undefined();
291 CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener)
293 INC_STATS("DOM.XMLHttpRequest.removeEventListener()");
294 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
296 RefPtr<EventListener> listener = getEventListener(xmlHttpRequest, args[1], true);
298 String type = toWebCoreString(args[0]);
299 bool useCapture = args[2]->BooleanValue();
300 xmlHttpRequest->removeEventListener(type, listener.get(), useCapture);
302 removeHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
305 return v8::Undefined();
308 CALLBACK_FUNC_DECL(XMLHttpRequestOpen)
310 INC_STATS("DOM.XMLHttpRequest.open()");
313 // open(method, url, async)
314 // open(method, url, async, user)
315 // open(method, url, async, user, passwd)
317 if (args.Length() < 2)
318 return throwError("Not enough arguments", V8Proxy::SYNTAX_ERROR);
320 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
322 String method = toWebCoreString(args[0]);
323 String urlstring = toWebCoreString(args[1]);
324 ScriptExecutionContext* context = 0;
326 WorkerContextExecutionProxy* workerContextProxy = WorkerContextExecutionProxy::retrieve();
327 if (workerContextProxy) {
328 context = workerContextProxy->workerContext();
334 V8Proxy* proxy = V8Proxy::retrieve();
336 return v8::Undefined();
337 context = proxy->frame()->document();
341 KURL url = context->completeURL(urlstring);
343 bool async = (args.Length() < 3) ? true : args[2]->BooleanValue();
345 ExceptionCode ec = 0;
347 if (args.Length() >= 4 && !args[3]->IsUndefined()) {
348 user = valueToStringWithNullCheck(args[3]);
350 if (args.Length() >= 5 && !args[4]->IsUndefined()) {
351 passwd = valueToStringWithNullCheck(args[4]);
352 xmlHttpRequest->open(method, url, async, user, passwd, ec);
354 xmlHttpRequest->open(method, url, async, user, ec);
356 xmlHttpRequest->open(method, url, async, ec);
359 return throwError(ec);
361 return v8::Undefined();
364 static bool IsDocumentType(v8::Handle<v8::Value> value)
366 // FIXME: add other document types.
367 return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
370 CALLBACK_FUNC_DECL(XMLHttpRequestSend)
372 INC_STATS("DOM.XMLHttpRequest.send()");
373 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
375 ExceptionCode ec = 0;
376 if (args.Length() < 1)
377 xmlHttpRequest->send(ec);
379 v8::Handle<v8::Value> arg = args[0];
380 // FIXME: upstream handles "File" objects too.
381 if (IsDocumentType(arg)) {
382 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
383 Document* document = V8Proxy::DOMWrapperToNode<Document>(object);
385 xmlHttpRequest->send(document, ec);
387 xmlHttpRequest->send(valueToStringWithNullCheck(arg), ec);
391 return throwError(ec);
393 return v8::Undefined();
396 CALLBACK_FUNC_DECL(XMLHttpRequestSetRequestHeader) {
397 INC_STATS("DOM.XMLHttpRequest.setRequestHeader()");
398 if (args.Length() < 2)
399 return throwError("Not enough arguments", V8Proxy::SYNTAX_ERROR);
401 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
402 ExceptionCode ec = 0;
403 String header = toWebCoreString(args[0]);
404 String value = toWebCoreString(args[1]);
405 xmlHttpRequest->setRequestHeader(header, value, ec);
407 return throwError(ec);
408 return v8::Undefined();
411 CALLBACK_FUNC_DECL(XMLHttpRequestGetResponseHeader)
413 INC_STATS("DOM.XMLHttpRequest.getResponseHeader()");
414 if (args.Length() < 1)
415 return throwError("Not enough arguments", V8Proxy::SYNTAX_ERROR);
417 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
418 ExceptionCode ec = 0;
419 String header = toWebCoreString(args[0]);
420 String result = xmlHttpRequest->getResponseHeader(header, ec);
422 return throwError(ec);
423 return v8StringOrNull(result);
426 CALLBACK_FUNC_DECL(XMLHttpRequestOverrideMimeType)
428 INC_STATS("DOM.XMLHttpRequest.overrideMimeType()");
429 if (args.Length() < 1)
430 return throwError("Not enough arguments", V8Proxy::SYNTAX_ERROR);
432 XMLHttpRequest* xmlHttpRequest = V8Proxy::ToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
433 String value = toWebCoreString(args[0]);
434 xmlHttpRequest->overrideMimeType(value);
435 return v8::Undefined();
438 CALLBACK_FUNC_DECL(XMLHttpRequestDispatchEvent)
440 INC_STATS("DOM.XMLHttpRequest.dispatchEvent()");
441 return v8::Undefined();
444 } // namespace WebCore