2
* Copyright (C) 2008, 2009, 2010 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 "V8XMLHttpRequest.h"
34
#include <wtf/ArrayBuffer.h>
37
#include "InspectorInstrumentation.h"
38
#include "V8ArrayBuffer.h"
39
#include "V8ArrayBufferView.h"
40
#include "V8Binding.h"
42
#include "V8DOMFormData.h"
43
#include "V8Document.h"
44
#include "V8HTMLDocument.h"
45
#include "V8Utilities.h"
46
#include "WorkerContext.h"
47
#include "XMLHttpRequest.h"
51
v8::Handle<v8::Value> V8XMLHttpRequest::constructorCallbackCustom(const v8::Arguments& args)
53
ScriptExecutionContext* context = getScriptExecutionContext();
55
RefPtr<SecurityOrigin> securityOrigin;
56
if (context->isDocument()) {
57
if (DOMWrapperWorld* world = worldForEnteredContextIfIsolated())
58
securityOrigin = world->isolatedWorldSecurityOrigin();
61
RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
63
v8::Handle<v8::Object> wrapper = args.Holder();
64
V8DOMWrapper::createDOMWrapper(xmlHttpRequest.release(), &info, wrapper);
68
v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
70
INC_STATS("DOM.XMLHttpRequest.responsetext._get");
71
XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
73
const String& text = xmlHttpRequest->responseText(ec);
75
return setDOMException(ec, info.GetIsolate());
76
return v8String(text, info.GetIsolate());
79
v8::Handle<v8::Value> V8XMLHttpRequest::responseAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
81
INC_STATS("DOM.XMLHttpRequest.response._get");
82
XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
84
switch (xmlHttpRequest->responseTypeCode()) {
85
case XMLHttpRequest::ResponseTypeDefault:
86
case XMLHttpRequest::ResponseTypeText:
87
return responseTextAccessorGetter(name, info);
89
case XMLHttpRequest::ResponseTypeDocument:
92
Document* document = xmlHttpRequest->responseXML(ec);
94
return setDOMException(ec, info.GetIsolate());
95
return toV8(document, info.Holder(), info.GetIsolate());
98
case XMLHttpRequest::ResponseTypeBlob:
100
ExceptionCode ec = 0;
101
Blob* blob = xmlHttpRequest->responseBlob(ec);
103
return setDOMException(ec, info.GetIsolate());
104
return toV8(blob, info.Holder(), info.GetIsolate());
107
case XMLHttpRequest::ResponseTypeArrayBuffer:
109
ExceptionCode ec = 0;
110
ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(ec);
112
return setDOMException(ec, info.GetIsolate());
113
return toV8(arrayBuffer, info.Holder(), info.GetIsolate());
117
return v8::Undefined();
120
v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
122
INC_STATS("DOM.XMLHttpRequest.open()");
125
// open(method, url, async)
126
// open(method, url, async, user)
127
// open(method, url, async, user, passwd)
129
if (args.Length() < 2)
130
return throwNotEnoughArgumentsError(args.GetIsolate());
132
XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
134
String method = toWebCoreString(args[0]);
135
String urlstring = toWebCoreString(args[1]);
137
ScriptExecutionContext* context = getScriptExecutionContext();
138
KURL url = context->completeURL(urlstring);
140
ExceptionCode ec = 0;
142
if (args.Length() >= 3) {
143
bool async = args[2]->BooleanValue();
145
if (args.Length() >= 4 && !args[3]->IsUndefined()) {
146
String user = toWebCoreStringWithNullCheck(args[3]);
148
if (args.Length() >= 5 && !args[4]->IsUndefined()) {
149
String passwd = toWebCoreStringWithNullCheck(args[4]);
150
xmlHttpRequest->open(method, url, async, user, passwd, ec);
152
xmlHttpRequest->open(method, url, async, user, ec);
154
xmlHttpRequest->open(method, url, async, ec);
156
xmlHttpRequest->open(method, url, ec);
159
return setDOMException(ec, args.GetIsolate());
161
return v8::Undefined();
164
static bool isDocumentType(v8::Handle<v8::Value> value)
166
// FIXME: add other document types.
167
return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
170
v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
172
INC_STATS("DOM.XMLHttpRequest.send()");
173
XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
175
InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->scriptExecutionContext(), xmlHttpRequest->url());
177
ExceptionCode ec = 0;
178
if (args.Length() < 1)
179
xmlHttpRequest->send(ec);
181
v8::Handle<v8::Value> arg = args[0];
182
if (isUndefinedOrNull(arg))
183
xmlHttpRequest->send(ec);
184
else if (isDocumentType(arg)) {
185
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
186
Document* document = V8Document::toNative(object);
188
xmlHttpRequest->send(document, ec);
189
} else if (V8Blob::HasInstance(arg)) {
190
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
191
Blob* blob = V8Blob::toNative(object);
193
xmlHttpRequest->send(blob, ec);
194
} else if (V8DOMFormData::HasInstance(arg)) {
195
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
196
DOMFormData* domFormData = V8DOMFormData::toNative(object);
198
xmlHttpRequest->send(domFormData, ec);
199
#if ENABLE(WEBGL) || ENABLE(BLOB)
200
} else if (V8ArrayBuffer::HasInstance(arg)) {
201
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
202
ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(object);
204
xmlHttpRequest->send(arrayBuffer, ec);
205
} else if (V8ArrayBufferView::HasInstance(arg)) {
206
v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
207
ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(object);
208
ASSERT(arrayBufferView);
209
xmlHttpRequest->send(arrayBufferView, ec);
212
xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
216
return setDOMException(ec, args.GetIsolate());
218
return v8::Undefined();
221
} // namespace WebCore