/*
- * Copyright (C) 2008, 2009 Google Inc. All rights reserved.
+ * Copyright (C) 2008, 2009, 2010 Google Inc. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are
*/
#include "config.h"
-#include "XMLHttpRequest.h"
+#include "V8XMLHttpRequest.h"
+#include "ArrayBuffer.h"
#include "Frame.h"
+#include "InspectorInstrumentation.h"
+#include "V8ArrayBuffer.h"
#include "V8Binding.h"
-#include "V8CustomBinding.h"
+#include "V8Blob.h"
+#include "V8DOMFormData.h"
#include "V8Document.h"
-#include "V8File.h"
#include "V8HTMLDocument.h"
#include "V8Proxy.h"
#include "V8Utilities.h"
#include "WorkerContext.h"
#include "WorkerContextExecutionProxy.h"
+#include "XMLHttpRequest.h"
namespace WebCore {
-ACCESSOR_GETTER(XMLHttpRequestResponseText)
+v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
INC_STATS("DOM.XMLHttpRequest.responsetext._get");
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
- return xmlHttpRequest->responseText().v8StringOrNull();
-}
-
-CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener)
-{
- INC_STATS("DOM.XMLHttpRequest.addEventListener()");
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
-
- RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOrCreate);
- if (listener) {
- String type = toWebCoreString(args[0]);
- bool useCapture = args[2]->BooleanValue();
- xmlHttpRequest->addEventListener(type, listener, useCapture);
-
- createHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
- }
- return v8::Undefined();
+ XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
+ ExceptionCode ec = 0;
+ const String& text = xmlHttpRequest->responseText(ec);
+ if (ec)
+ return throwError(ec);
+ return v8String(text);
}
-CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener)
+v8::Handle<v8::Value> V8XMLHttpRequest::responseAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
{
- INC_STATS("DOM.XMLHttpRequest.removeEventListener()");
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
-
- RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOnly);
- if (listener) {
- String type = toWebCoreString(args[0]);
- bool useCapture = args[2]->BooleanValue();
- xmlHttpRequest->removeEventListener(type, listener.get(), useCapture);
-
- removeHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
+ INC_STATS("DOM.XMLHttpRequest.response._get");
+ XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
+
+ switch (xmlHttpRequest->responseTypeCode()) {
+ case XMLHttpRequest::ResponseTypeDefault:
+ case XMLHttpRequest::ResponseTypeText:
+ return responseTextAccessorGetter(name, info);
+
+ case XMLHttpRequest::ResponseTypeDocument:
+ {
+ ExceptionCode ec = 0;
+ Document* document = xmlHttpRequest->responseXML(ec);
+ if (ec) {
+ V8Proxy::setDOMException(ec);
+ return v8::Undefined();
+ }
+ return toV8(document);
+ }
+
+ case XMLHttpRequest::ResponseTypeBlob:
+#if ENABLE(XHR_RESPONSE_BLOB)
+ {
+ ExceptionCode ec = 0;
+ Blob* blob = xmlHttpRequest->responseBlob(ec);
+ if (ec) {
+ V8Proxy::setDOMException(ec);
+ return v8::Undefined();
+ }
+ return toV8(blob);
+ }
+#else
+ return v8::Undefined();
+#endif
+
+#if ENABLE(3D_CANVAS) || ENABLE(BLOB)
+ case XMLHttpRequest::ResponseTypeArrayBuffer:
+ {
+ ExceptionCode ec = 0;
+ ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(ec);
+ if (ec) {
+ V8Proxy::setDOMException(ec);
+ return v8::Undefined();
+ }
+ return toV8(arrayBuffer);
+ }
+#endif
}
return v8::Undefined();
}
-CALLBACK_FUNC_DECL(XMLHttpRequestOpen)
+v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
{
INC_STATS("DOM.XMLHttpRequest.open()");
// Four cases:
if (args.Length() < 2)
return throwError("Not enough arguments", V8Proxy::SyntaxError);
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
+ XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
String method = toWebCoreString(args[0]);
String urlstring = toWebCoreString(args[1]);
KURL url = context->completeURL(urlstring);
- bool async = (args.Length() < 3) ? true : args[2]->BooleanValue();
-
ExceptionCode ec = 0;
- String user, passwd;
- if (args.Length() >= 4 && !args[3]->IsUndefined()) {
- user = toWebCoreStringWithNullCheck(args[3]);
- if (args.Length() >= 5 && !args[4]->IsUndefined()) {
- passwd = toWebCoreStringWithNullCheck(args[4]);
- xmlHttpRequest->open(method, url, async, user, passwd, ec);
+ if (args.Length() >= 3) {
+ bool async = args[2]->BooleanValue();
+
+ if (args.Length() >= 4 && !args[3]->IsUndefined()) {
+ String user = toWebCoreStringWithNullCheck(args[3]);
+
+ if (args.Length() >= 5 && !args[4]->IsUndefined()) {
+ String passwd = toWebCoreStringWithNullCheck(args[4]);
+ xmlHttpRequest->open(method, url, async, user, passwd, ec);
+ } else
+ xmlHttpRequest->open(method, url, async, user, ec);
} else
- xmlHttpRequest->open(method, url, async, user, ec);
+ xmlHttpRequest->open(method, url, async, ec);
} else
- xmlHttpRequest->open(method, url, async, ec);
+ xmlHttpRequest->open(method, url, ec);
if (ec)
return throwError(ec);
return v8::Undefined();
}
-static bool IsDocumentType(v8::Handle<v8::Value> value)
+static bool isDocumentType(v8::Handle<v8::Value> value)
{
// FIXME: add other document types.
return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
}
-CALLBACK_FUNC_DECL(XMLHttpRequestSend)
+v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
{
INC_STATS("DOM.XMLHttpRequest.send()");
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
+ XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
+
+ InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->scriptExecutionContext(), xmlHttpRequest->url());
ExceptionCode ec = 0;
if (args.Length() < 1)
xmlHttpRequest->send(ec);
else {
v8::Handle<v8::Value> arg = args[0];
- if (IsDocumentType(arg)) {
+ if (isUndefinedOrNull(arg))
+ xmlHttpRequest->send(ec);
+ else if (isDocumentType(arg)) {
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
- Document* document = V8DOMWrapper::convertDOMWrapperToNode<Document>(object);
+ Document* document = V8Document::toNative(object);
ASSERT(document);
xmlHttpRequest->send(document, ec);
- } else if (V8File::HasInstance(arg)) {
+ } else if (V8Blob::HasInstance(arg)) {
+ v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
+ Blob* blob = V8Blob::toNative(object);
+ ASSERT(blob);
+ xmlHttpRequest->send(blob, ec);
+ } else if (V8DOMFormData::HasInstance(arg)) {
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
- File* file = V8DOMWrapper::convertDOMWrapperToNative<File>(object);
- ASSERT(file);
- xmlHttpRequest->send(file, ec);
+ DOMFormData* domFormData = V8DOMFormData::toNative(object);
+ ASSERT(domFormData);
+ xmlHttpRequest->send(domFormData, ec);
+#if ENABLE(3D_CANVAS) || ENABLE(BLOB)
+ } else if (V8ArrayBuffer::HasInstance(arg)) {
+ v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
+ ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(object);
+ ASSERT(arrayBuffer);
+ xmlHttpRequest->send(arrayBuffer, ec);
+#endif
} else
xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
}
return v8::Undefined();
}
-CALLBACK_FUNC_DECL(XMLHttpRequestSetRequestHeader) {
- INC_STATS("DOM.XMLHttpRequest.setRequestHeader()");
- if (args.Length() < 2)
- return throwError("Not enough arguments", V8Proxy::SyntaxError);
-
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
- ExceptionCode ec = 0;
- String header = toWebCoreString(args[0]);
- String value = toWebCoreString(args[1]);
- xmlHttpRequest->setRequestHeader(header, value, ec);
- if (ec)
- return throwError(ec);
- return v8::Undefined();
-}
-
-CALLBACK_FUNC_DECL(XMLHttpRequestGetResponseHeader)
-{
- INC_STATS("DOM.XMLHttpRequest.getResponseHeader()");
- if (args.Length() < 1)
- return throwError("Not enough arguments", V8Proxy::SyntaxError);
-
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
- ExceptionCode ec = 0;
- String header = toWebCoreString(args[0]);
- String result = xmlHttpRequest->getResponseHeader(header, ec);
- if (ec)
- return throwError(ec);
- return v8StringOrNull(result);
-}
-
-CALLBACK_FUNC_DECL(XMLHttpRequestOverrideMimeType)
-{
- INC_STATS("DOM.XMLHttpRequest.overrideMimeType()");
- if (args.Length() < 1)
- return throwError("Not enough arguments", V8Proxy::SyntaxError);
-
- XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
- String value = toWebCoreString(args[0]);
- xmlHttpRequest->overrideMimeType(value);
- return v8::Undefined();
-}
-
-CALLBACK_FUNC_DECL(XMLHttpRequestDispatchEvent)
-{
- INC_STATS("DOM.XMLHttpRequest.dispatchEvent()");
- return v8::Undefined();
-}
-
} // namespace WebCore