Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / third_party / WebKit / Source / bindings / core / 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 "bindings/core/v8/V8XMLHttpRequest.h"
33
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"
51 #include <v8.h>
52
53 namespace blink {
54
55 void V8XMLHttpRequest::constructorCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
56 {
57     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
58
59     RefPtr<SecurityOrigin> securityOrigin;
60     if (context->isDocument()) {
61         DOMWrapperWorld& world = DOMWrapperWorld::current(info.GetIsolate());
62         if (world.isIsolatedWorld())
63             securityOrigin = world.isolatedWorldSecurityOrigin();
64     }
65
66     RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
67
68     v8::Handle<v8::Object> wrapper = info.Holder();
69     xmlHttpRequest->associateWithWrapper(xmlHttpRequest->wrapperTypeInfo(), wrapper, info.GetIsolate());
70     info.GetReturnValue().Set(wrapper);
71 }
72
73 void V8XMLHttpRequest::responseTextAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
74 {
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())
79         return;
80     if (text.isEmpty()) {
81         v8SetReturnValueString(info, emptyString(), info.GetIsolate());
82         return;
83     }
84     v8SetReturnValue(info, text.v8Value());
85 }
86
87 void V8XMLHttpRequest::responseAttributeGetterCustom(const v8::PropertyCallbackInfo<v8::Value>& info)
88 {
89     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
90
91     switch (xmlHttpRequest->responseTypeCode()) {
92     case XMLHttpRequest::ResponseTypeDefault:
93     case XMLHttpRequest::ResponseTypeText:
94         responseTextAttributeGetterCustom(info);
95         return;
96
97     case XMLHttpRequest::ResponseTypeJSON:
98         {
99             v8::Isolate* isolate = info.GetIsolate();
100
101             ScriptString jsonSource = xmlHttpRequest->responseJSONSource();
102             if (jsonSource.isEmpty()) {
103                 v8SetReturnValue(info, v8::Null(isolate));
104                 return;
105             }
106
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));
112             else
113                 v8SetReturnValue(info, json);
114             return;
115         }
116
117     case XMLHttpRequest::ResponseTypeDocument:
118         {
119             ExceptionState exceptionState(ExceptionState::GetterContext, "response", "XMLHttpRequest", info.Holder(), info.GetIsolate());
120             Document* document = xmlHttpRequest->responseXML(exceptionState);
121             if (exceptionState.throwIfNeeded())
122                 return;
123             v8SetReturnValueFast(info, document, xmlHttpRequest);
124             return;
125         }
126
127     case XMLHttpRequest::ResponseTypeBlob:
128         {
129             Blob* blob = xmlHttpRequest->responseBlob();
130             v8SetReturnValueFast(info, blob, xmlHttpRequest);
131             return;
132         }
133
134     case XMLHttpRequest::ResponseTypeLegacyStream:
135         {
136             Stream* stream = xmlHttpRequest->responseLegacyStream();
137             v8SetReturnValueFast(info, stream, xmlHttpRequest);
138             return;
139         }
140
141     case XMLHttpRequest::ResponseTypeStream:
142         {
143             ReadableStream* stream = xmlHttpRequest->responseStream();
144             v8SetReturnValueFast(info, stream, xmlHttpRequest);
145             return;
146         }
147
148     case XMLHttpRequest::ResponseTypeArrayBuffer:
149         {
150             DOMArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer();
151             v8SetReturnValueFast(info, arrayBuffer, xmlHttpRequest);
152             return;
153         }
154     }
155 }
156
157 void V8XMLHttpRequest::openMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
158 {
159     // Four cases:
160     // open(method, url)
161     // open(method, url, async)
162     // open(method, url, async, user)
163     // open(method, url, async, user, passwd)
164
165     ExceptionState exceptionState(ExceptionState::ExecutionContext, "open", "XMLHttpRequest", info.Holder(), info.GetIsolate());
166
167     if (info.Length() < 2) {
168         exceptionState.throwTypeError(ExceptionMessages::notEnoughArguments(2, info.Length()));
169         exceptionState.throwIfNeeded();
170         return;
171     }
172
173     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
174
175     TOSTRING_VOID(V8StringResource<>, method, info[0]);
176     TOSTRING_VOID(V8StringResource<>, urlstring, info[1]);
177
178     ExecutionContext* context = currentExecutionContext(info.GetIsolate());
179     KURL url = context->completeURL(urlstring);
180
181     if (info.Length() >= 3) {
182         bool async = info[2]->BooleanValue();
183
184         if (info.Length() >= 4 && !info[3]->IsUndefined()) {
185             TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, user, info[3]);
186
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);
190             } else {
191                 xmlHttpRequest->open(method, url, async, user, exceptionState);
192             }
193         } else {
194             xmlHttpRequest->open(method, url, async, exceptionState);
195         }
196     } else {
197         xmlHttpRequest->open(method, url, exceptionState);
198     }
199
200     exceptionState.throwIfNeeded();
201 }
202
203 static bool isDocumentType(v8::Handle<v8::Value> value, v8::Isolate* isolate)
204 {
205     // FIXME: add other document types.
206     return V8Document::hasInstance(value, isolate) || V8HTMLDocument::hasInstance(value, isolate);
207 }
208
209 void V8XMLHttpRequest::sendMethodCustom(const v8::FunctionCallbackInfo<v8::Value>& info)
210 {
211     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toImpl(info.Holder());
212
213     InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->executionContext(), xmlHttpRequest->url());
214
215     ExceptionState exceptionState(ExceptionState::ExecutionContext, "send", "XMLHttpRequest", info.Holder(), info.GetIsolate());
216     if (info.Length() < 1) {
217         xmlHttpRequest->send(exceptionState);
218     } else {
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);
225             ASSERT(document);
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);
230             ASSERT(blob);
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);
235             ASSERT(domFormData);
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);
240             ASSERT(arrayBuffer);
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);
247         } else {
248             TOSTRING_VOID(V8StringResource<TreatNullAsNullString>, argString, arg);
249             xmlHttpRequest->send(argString, exceptionState);
250         }
251     }
252
253     exceptionState.throwIfNeeded();
254 }
255
256 } // namespace blink