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 "bindings/core/v8/V8XMLHttpRequest.h"
34 #include "bindings/core/v8/ExceptionMessages.h"
35 #include "bindings/core/v8/ExceptionState.h"
36 #include "bindings/core/v8/V8ArrayBuffer.h"
37 #include "bindings/core/v8/V8ArrayBufferView.h"
38 #include "bindings/core/v8/V8Binding.h"
39 #include "bindings/core/v8/V8Blob.h"
40 #include "bindings/core/v8/V8Document.h"
41 #include "bindings/core/v8/V8FormData.h"
42 #include "bindings/core/v8/V8HTMLDocument.h"
43 #include "bindings/core/v8/V8ReadableStream.h"
44 #include "bindings/core/v8/V8Stream.h"
45 #include "core/dom/Document.h"
46 #include "core/inspector/InspectorInstrumentation.h"
47 #include "core/streams/ReadableStream.h"
48 #include "core/streams/Stream.h"
49 #include "core/workers/WorkerGlobalScope.h"
50 #include "core/xmlhttprequest/XMLHttpRequest.h"
55 void V8XMLHttpRequest::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
57 ExecutionContext* context = currentExecutionContext(info.GetIsolate());
59 RefPtr<SecurityOrigin> securityOrigin;
60 if (context->isDocument()) {
61 DOMWrapperWorld& world = DOMWrapperWorld::current(info.GetIsolate());
62 if (world.isIsolatedWorld())
63 securityOrigin = world.isolatedWorldSecurityOrigin();
66 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
68 v8::Handle<v8::Object> wrapper = info.Holder();
69 xmlHttpRequest->associateWithWrapper(xmlHttpRequest->wrapperTypeInfo(), wrapper, info.GetIsolate());
70 info.GetReturnValue().Set(wrapper);
73 void V8XMLHttpRequest::responseTextAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
75 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
76 ExceptionState exceptionState(ExceptionState::GetterContext, "responseText", "XMLHttpRequest", info.Holder(), info.GetIsolate());
77 ScriptString text = xmlHttpRequest->responseText(exceptionState);
78 if (exceptionState.throwIfNeeded())
81 v8SetReturnValueString(info, emptyString(), info.GetIsolate());
84 v8SetReturnValue(info, text.v8Value());
87 void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
89 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
91 switch (xmlHttpRequest->responseTypeCode()) {
92 case XMLHttpRequest::ResponseTypeDefault:
93 case XMLHttpRequest::ResponseTypeText:
94 responseTextAttributeGetterCustom(info);
97 case XMLHttpRequest::ResponseTypeJSON:
99 v8::Isolate* isolate = info.GetIsolate();
101 ScriptString jsonSource = xmlHttpRequest->responseJSONSource();
102 if (jsonSource.isEmpty()) {
103 v8SetReturnValue(info, v8::Null(isolate));
107 // Catch syntax error.
108 v8::TryCatch exceptionCatcher;
109 v8::Handle<v8::Value> json = v8::JSON::Parse(jsonSource.v8Value());
110 if (exceptionCatcher.HasCaught() || json.IsEmpty())
111 v8SetReturnValue(info, v8::Null(isolate));
113 v8SetReturnValue(info, json);
117 case XMLHttpRequest::ResponseTypeDocument:
119 ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate());
120 Document* document = xmlHttpRequest->responseXML(exceptionState);
121 if (exceptionState.throwIfNeeded())
123 v8SetReturnValueFast(info, document, xmlHttpRequest);
127 case XMLHttpRequest::ResponseTypeBlob:
129 Blob* blob = xmlHttpRequest->responseBlob();
130 v8SetReturnValueFast(info, blob, xmlHttpRequest);
134 case XMLHttpRequest::ResponseTypeLegacyStream:
136 Stream* stream = xmlHttpRequest->responseLegacyStream();
137 v8SetReturnValueFast(info, stream, xmlHttpRequest);
141 case XMLHttpRequest::ResponseTypeStream:
143 ReadableStream* stream = xmlHttpRequest->responseStream();
144 v8SetReturnValueFast(info, stream, xmlHttpRequest);
148 case XMLHttpRequest::ResponseTypeArrayBuffer:
150 DOMArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
151 v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
157 void V8XMLHttpRequest::openMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
161 // open(method, url, async)
162 // open(method, url, async, user)
163 // open(method, url, async, user, passwd)
165 ExceptionState exceptionState(ExceptionState::ExecutionContext, "open", "XMLHttpRequest", info.Holder(), info.GetIsolate());
167 if (info.Length() < 2) {
168 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
169 exceptionState.throwIfNeeded();
173 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
175 TOSTRING_VOID(V8StringResource<>, method, info[0]);
176 TOSTRING_VOID(V8StringResource<>, urlstring, info[1]);
178 ExecutionContext* context = currentExecutionContext(info.GetIsolate());
179 KURL url = context->completeURL(urlstring);
181 if (info.Length() >= 3) {
182 bool async = info[2]->BooleanValue();
184 if (info.Length() >= 4 && !info[3]->IsUndefined()) {
185 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, user, info[3]);
187 if (info.Length() >= 5 && !info[4]->IsUndefined()) {
188 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, password, info[4]);
189 xmlHttpRequest->open(method, url, async, user, password, exceptionState);
191 xmlHttpRequest->open(method, url, async, user, exceptionState);
194 xmlHttpRequest->open(method, url, async, exceptionState);
197 xmlHttpRequest->open(method, url, exceptionState);
200 exceptionState.throwIfNeeded();
203 static bool isDocumentType(v8::Handle<v8::Value> value, v8::Isolate* isolate)
205 // FIXME: add other document types.
206 return V8Document::hasInstance(value, isolate) || V8HTMLDocument::hasInstance(value, isolate);
209 void V8XMLHttpRequest::sendMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
211 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
213 InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->executionContext(), xmlHttpRequest->url());
215 ExceptionState exceptionState(ExceptionState::ExecutionContext, "send", "XMLHttpRequest", info.Holder(), info.GetIsolate());
216 if (info.Length() < 1) {
217 xmlHttpRequest->send(exceptionState);
219 v8::Handle<v8::Value> arg = info[0];
220 if (isUndefinedOrNull(arg)) {
221 xmlHttpRequest->send(exceptionState);
222 } else if (isDocumentType(arg, info.GetIsolate())) {
223 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
224 Document* document = V8Document::toImpl(object);
226 xmlHttpRequest->send(document, exceptionState);
227 } else if (V8Blob::hasInstance(arg, info.GetIsolate())) {
228 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
229 Blob* blob = V8Blob::toImpl(object);
231 xmlHttpRequest->send(blob, exceptionState);
232 } else if (V8FormData::hasInstance(arg, info.GetIsolate())) {
233 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
234 DOMFormData* domFormData = V8FormData::toImpl(object);
236 xmlHttpRequest->send(domFormData, exceptionState);
237 } else if (V8ArrayBuffer::hasInstance(arg, info.GetIsolate())) {
238 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
239 DOMArrayBuffer* arrayBuffer = V8ArrayBuffer::toImpl(object);
241 xmlHttpRequest->send(arrayBuffer->buffer(), exceptionState);
242 } else if (V8ArrayBufferView::hasInstance(arg, info.GetIsolate())) {
243 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
244 DOMArrayBufferView* arrayBufferView = V8ArrayBufferView::toImpl(object);
245 ASSERT(arrayBufferView);
246 xmlHttpRequest->send(arrayBufferView->view(), exceptionState);
248 TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, argString, arg);
249 xmlHttpRequest->send(argString, exceptionState);
253 exceptionState.throwIfNeeded();