Upstream version 6.35.121.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / v8 / custom / V8XMLHttpRequestCustom.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2010 Google Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "V8XMLHttpRequest.h"
33
34 #include "V8Blob.h"
35 #include "V8Document.h"
36 #include "V8FormData.h"
37 #include "V8HTMLDocument.h"
38 #include "V8Stream.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"
50 #include <v8.h>
51
52 namespace WebCore {
53
54 void V8XMLHttpRequest::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
55 {
56     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
57
58     RefPtr<SecurityOrigin> securityOrigin;
59     if (context->isDocument()) {
60         DOMWrapperWorld* world = DOMWrapperWorld::current(info.GetIsolate());
61         if (world->isIsolatedWorld())
62             securityOrigin = world->isolatedWorldSecurityOrigin();
63     }
64
65     RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
66
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);
70 }
71
72 void V8XMLHttpRequest::responseTextAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
73 {
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())
78         return;
79     if (text.hasNoValue()) {
80         v8SetReturnValueString(info, emptyString(), info.GetIsolate());
81         return;
82     }
83     v8SetReturnValue(info, text.v8Value());
84 }
85
86 void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
87 {
88     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
89
90     switch (xmlHttpRequest->responseTypeCode()) {
91     case XMLHttpRequest::ResponseTypeDefault:
92     case XMLHttpRequest::ResponseTypeText:
93         responseTextAttributeGetterCustom(info);
94         return;
95
96     case XMLHttpRequest::ResponseTypeJSON:
97         {
98             v8::Isolate* isolate = info.GetIsolate();
99
100             ScriptString jsonSource = xmlHttpRequest->responseJSONSource();
101             if (jsonSource.hasNoValue() || !jsonSource.v8Value()->IsString()) {
102                 v8SetReturnValue(info, v8::Null(isolate));
103                 return;
104             }
105
106             // Catch syntax error.
107             v8::TryCatch exceptionCatcher;
108
109             v8::Handle<v8::Value> json = v8::JSON::Parse(jsonSource.v8Value().As<v8::String>());
110
111             if (exceptionCatcher.HasCaught() || json.IsEmpty())
112                 v8SetReturnValue(info, v8::Null(isolate));
113             else
114                 v8SetReturnValue(info, json);
115
116             return;
117         }
118
119     case XMLHttpRequest::ResponseTypeDocument:
120         {
121             ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate());
122             Document* document = xmlHttpRequest->responseXML(exceptionState);
123             if (exceptionState.throwIfNeeded())
124                 return;
125             v8SetReturnValueFast(info, document, xmlHttpRequest);
126             return;
127         }
128
129     case XMLHttpRequest::ResponseTypeBlob:
130         {
131             Blob* blob = xmlHttpRequest->responseBlob();
132             v8SetReturnValueFast(info, blob, xmlHttpRequest);
133             return;
134         }
135
136     case XMLHttpRequest::ResponseTypeStream:
137         {
138             Stream* stream = xmlHttpRequest->responseStream();
139             v8SetReturnValueFast(info, stream, xmlHttpRequest);
140             return;
141         }
142
143     case XMLHttpRequest::ResponseTypeArrayBuffer:
144         {
145             ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
146             if (arrayBuffer) {
147                 arrayBuffer->setDeallocationObserver(V8ArrayBufferDeallocationObserver::instanceTemplate());
148             }
149             v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
150             return;
151         }
152     }
153 }
154
155 void V8XMLHttpRequest::openMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
156 {
157     // Four cases:
158     // open(method, url)
159     // open(method, url, async)
160     // open(method, url, async, user)
161     // open(method, url, async, user, passwd)
162
163     ExceptionState exceptionState(ExceptionState::ExecutionContext, "open", "XMLHttpRequest", info.Holder(), info.GetIsolate());
164
165     if (info.Length() < 2) {
166         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
167         exceptionState.throwIfNeeded();
168         return;
169     }
170
171     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
172
173     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, method, info[0]);
174     V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<>, urlstring, info[1]);
175
176     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
177     KURL url = context->completeURL(urlstring);
178
179     if (info.Length() >= 3) {
180         bool async = info[2]->BooleanValue();
181
182         if (info.Length() >= 4 && !info[3]->IsUndefined()) {
183             V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, user, info[3]);
184
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);
188             } else {
189                 xmlHttpRequest->open(method, url, async, user, exceptionState);
190             }
191         } else {
192             xmlHttpRequest->open(method, url, async, exceptionState);
193         }
194     } else {
195         xmlHttpRequest->open(method, url, exceptionState);
196     }
197
198     exceptionState.throwIfNeeded();
199 }
200
201 static bool isDocumentType(v8::Handle<v8::Value> value, v8::Isolate* isolate)
202 {
203     // FIXME: add other document types.
204     return V8Document::hasInstance(value, isolate) || V8HTMLDocument::hasInstance(value, isolate);
205 }
206
207 void V8XMLHttpRequest::sendMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
208 {
209     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
210
211     InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->executionContext(), xmlHttpRequest->url());
212
213     ExceptionState exceptionState(ExceptionState::ExecutionContext, "send", "XMLHttpRequest", info.Holder(), info.GetIsolate());
214     if (info.Length() < 1)
215         xmlHttpRequest->send(exceptionState);
216     else {
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);
223             ASSERT(document);
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);
228             ASSERT(blob);
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);
233             ASSERT(domFormData);
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);
238             ASSERT(arrayBuffer);
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);
245         } else {
246             V8TRYCATCH_FOR_V8STRINGRESOURCE_VOID(V8StringResource<WithNullCheck>, argString, arg);
247             xmlHttpRequest->send(argString, exceptionState);
248         }
249     }
250
251     exceptionState.throwIfNeeded();
252 }
253
254 } // namespace WebCore