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"
35 #include "V8Document.h"
36 #include "V8FormData.h"
37 #include "V8HTMLDocument.h"
39 #include "bindings/v8/ExceptionMessages.h"
40 #include "bindings/v8/ExceptionState.h"
41 #include "bindings/v8/V8Binding.h"
42 #include "bindings/v8/custom/V8ArrayBufferCustom.h"
43 #include "bindings/v8/custom/V8ArrayBufferViewCustom.h"
44 #include "core/dom/Document.h"
45 #include "core/fileapi/Stream.h"
46 #include "core/inspector/InspectorInstrumentation.h"
47 #include "core/workers/WorkerGlobalScope.h"
48 #include "core/xml/XMLHttpRequest.h"
49 #include "wtf/ArrayBuffer.h"
54 void V8XMLHttpRequest::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
56 ExecutionContext* context = currentExecutionContext(info.GetIsolate());
58 RefPtr<SecurityOrigin> securityOrigin;
59 if (context->isDocument()) {
60 DOMWrapperWorld* world = DOMWrapperWorld::current(info.GetIsolate());
61 if (world->isIsolatedWorld())
62 securityOrigin = world->isolatedWorldSecurityOrigin();
65 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
67 v8::Handle<v8::Object> wrapper = info.Holder();
68 V8DOMWrapper::associateObjectWithWrapper<V8XMLHttpRequest>(xmlHttpRequest.release(), &wrapperTypeInfo, wrapper, info.GetIsolate(), WrapperConfiguration::Dependent);
69 info.GetReturnValue().Set(wrapper);
72 void V8XMLHttpRequest::responseTextAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
74 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
75 ExceptionState exceptionState(ExceptionState::GetterContext, "responseText", "XMLHttpRequest", info.Holder(), info.GetIsolate());
76 ScriptValue text = xmlHttpRequest->responseText(exceptionState);
77 if (exceptionState.throwIfNeeded())
79 if (text.hasNoValue()) {
80 v8SetReturnValueString(info, emptyString(), info.GetIsolate());
83 v8SetReturnValue(info, text.v8Value());
86 void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
88 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
90 switch (xmlHttpRequest->responseTypeCode()) {
91 case XMLHttpRequest::ResponseTypeDefault:
92 case XMLHttpRequest::ResponseTypeText:
93 responseTextAttributeGetterCustom(info);
96 case XMLHttpRequest::ResponseTypeJSON:
98 v8::Isolate* isolate = info.GetIsolate();
100 ScriptString jsonSource = xmlHttpRequest->responseJSONSource();
101 if (jsonSource.hasNoValue() || !jsonSource.v8Value()->IsString()) {
102 v8SetReturnValue(info, v8::Null(isolate));
106 // Catch syntax error.
107 v8::TryCatch exceptionCatcher;
109 v8::Handle<v8::Value> json = v8::JSON::Parse(jsonSource.v8Value().As<v8::String>());
111 if (exceptionCatcher.HasCaught() || json.IsEmpty())
112 v8SetReturnValue(info, v8::Null(isolate));
114 v8SetReturnValue(info, json);
119 case XMLHttpRequest::ResponseTypeDocument:
121 ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate());
122 Document* document = xmlHttpRequest->responseXML(exceptionState);
123 if (exceptionState.throwIfNeeded())
125 v8SetReturnValueFast(info, document, xmlHttpRequest);
129 case XMLHttpRequest::ResponseTypeBlob:
131 Blob* blob = xmlHttpRequest->responseBlob();
132 v8SetReturnValueFast(info, blob, xmlHttpRequest);
136 case XMLHttpRequest::ResponseTypeStream:
138 Stream* stream = xmlHttpRequest->responseStream();
139 v8SetReturnValueFast(info, stream, xmlHttpRequest);
143 case XMLHttpRequest::ResponseTypeArrayBuffer:
145 ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
147 arrayBuffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
149 v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
155 void V8XMLHttpRequest::openMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
159 // open(method, url, async)
160 // open(method, url, async, user)
161 // open(method, url, async, user, passwd)
163 ExceptionState exceptionState(ExceptionState::ExecutionContext, "open", "XMLHttpRequest", info.Holder(), info.GetIsolate());
165 if (info.Length() < 2) {
166 exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
167 exceptionState.throwIfNeeded();
171 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
173 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, method, info[0]);
174 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, urlstring, info[1]);
176 ExecutionContext* context = currentExecutionContext(info.GetIsolate());
177 KURL url = context->completeURL(urlstring);
179 if (info.Length() >= 3) {
180 bool async = info[2]->BooleanValue();
182 if (info.Length() >= 4 && !info[3]->IsUndefined()) {
183 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, user, info[3]);
185 if (info.Length() >= 5 && !info[4]->IsUndefined()) {
186 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, password, info[4]);
187 xmlHttpRequest->open(method, url, async, user, password, exceptionState);
189 xmlHttpRequest->open(method, url, async, user, exceptionState);
192 xmlHttpRequest->open(method, url, async, exceptionState);
195 xmlHttpRequest->open(method, url, exceptionState);
198 exceptionState.throwIfNeeded();
201 static bool isDocumentType(v8::Handle<v8::Value> value, v8::Isolate* isolate)
203 // FIXME: add other document types.
204 return V8Document::hasInstance(value, isolate) || V8HTMLDocument::hasInstance(value, isolate);
207 void V8XMLHttpRequest::sendMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
209 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
211 InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->executionContext(), xmlHttpRequest->url());
213 ExceptionState exceptionState(ExceptionState::ExecutionContext, "send", "XMLHttpRequest", info.Holder(), info.GetIsolate());
214 if (info.Length() < 1)
215 xmlHttpRequest->send(exceptionState);
217 v8::Handle<v8::Value> arg = info[0];
218 if (isUndefinedOrNull(arg)) {
219 xmlHttpRequest->send(exceptionState);
220 } else if (isDocumentType(arg, info.GetIsolate())) {
221 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
222 Document* document = V8Document::toNative(object);
224 xmlHttpRequest->send(document, exceptionState);
225 } else if (V8Blob::hasInstance(arg, info.GetIsolate())) {
226 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
227 Blob* blob = V8Blob::toNative(object);
229 xmlHttpRequest->send(blob, exceptionState);
230 } else if (V8FormData::hasInstance(arg, info.GetIsolate())) {
231 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
232 DOMFormData* domFormData = V8FormData::toNative(object);
234 xmlHttpRequest->send(domFormData, exceptionState);
235 } else if (V8ArrayBuffer::hasInstance(arg, info.GetIsolate())) {
236 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
237 ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(object);
239 xmlHttpRequest->send(arrayBuffer, exceptionState);
240 } else if (V8ArrayBufferView::hasInstance(arg, info.GetIsolate())) {
241 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
242 ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(object);
243 ASSERT(arrayBufferView);
244 xmlHttpRequest->send(arrayBufferView, exceptionState);
246 V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, argString, arg);
247 xmlHttpRequest->send(argString, exceptionState);
251 exceptionState.throwIfNeeded();
254 } // namespace WebCore