tizen beta release
[profile/ivi/webkit-efl.git] / Source / WebCore / bindings / js / JSXMLHttpRequestCustom.cpp
1 /*
2  * Copyright (C) 2008, 2009 Apple 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
6  * are met:
7  *
8  * 1.  Redistributions of source code must retain the above copyright
9  *     notice, this list of conditions and the following disclaimer.
10  * 2.  Redistributions in binary form must reproduce the above copyright
11  *     notice, this list of conditions and the following disclaimer in the
12  *     documentation and/or other materials provided with the distribution.
13  * 3.  Neither the name of Apple Computer, Inc. ("Apple") nor the names of
14  *     its contributors may be used to endorse or promote products derived
15  *     from this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY APPLE AND ITS CONTRIBUTORS "AS IS" AND ANY
18  * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
20  * DISCLAIMED. IN NO EVENT SHALL APPLE OR ITS CONTRIBUTORS BE LIABLE FOR ANY
21  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
23  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
24  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28
29 #include "config.h"
30 #include "JSXMLHttpRequest.h"
31
32 #include "Blob.h"
33 #include "DOMFormData.h"
34 #include "DOMWindow.h"
35 #include "Document.h"
36 #include "Event.h"
37 #include "Frame.h"
38 #include "FrameLoader.h"
39 #include "HTMLDocument.h"
40 #include "InspectorInstrumentation.h"
41 #include "JSArrayBuffer.h"
42 #include "JSBlob.h"
43 #include "JSDOMFormData.h"
44 #include "JSDOMWindowCustom.h"
45 #include "JSDocument.h"
46 #include "JSEvent.h"
47 #include "JSEventListener.h"
48 #include "XMLHttpRequest.h"
49 #include <runtime/Error.h>
50 #include <interpreter/Interpreter.h>
51 #include <wtf/ArrayBuffer.h>
52
53 using namespace JSC;
54
55 namespace WebCore {
56
57 void JSXMLHttpRequest::visitChildren(JSCell* cell, SlotVisitor& visitor)
58 {
59     JSXMLHttpRequest* thisObject = jsCast<JSXMLHttpRequest*>(cell);
60     ASSERT_GC_OBJECT_INHERITS(thisObject, &s_info);
61     COMPILE_ASSERT(StructureFlags & OverridesVisitChildren, OverridesVisitChildrenWithoutSettingFlag);
62     ASSERT(thisObject->structure()->typeInfo().overridesVisitChildren());
63     Base::visitChildren(thisObject, visitor);
64
65     if (XMLHttpRequestUpload* upload = thisObject->m_impl->optionalUpload())
66         visitor.addOpaqueRoot(upload);
67
68     if (Document* responseDocument = thisObject->m_impl->optionalResponseXML())
69         visitor.addOpaqueRoot(responseDocument);
70
71     if (ArrayBuffer* responseArrayBuffer = thisObject->m_impl->optionalResponseArrayBuffer())
72         visitor.addOpaqueRoot(responseArrayBuffer);
73
74 #if ENABLE(XHR_RESPONSE_BLOB)
75     if (Blob* responseBlob = thisObject->m_impl->optionalResponseBlob())
76         visitor.addOpaqueRoot(responseBlob);
77 #endif
78
79     thisObject->m_impl->visitJSEventListeners(visitor);
80 }
81
82 // Custom functions
83 JSValue JSXMLHttpRequest::open(ExecState* exec)
84 {
85     if (exec->argumentCount() < 2)
86         return throwError(exec, createSyntaxError(exec, "Not enough arguments"));
87
88     const KURL& url = impl()->scriptExecutionContext()->completeURL(ustringToString(exec->argument(1).toString(exec)));
89     String method = ustringToString(exec->argument(0).toString(exec));
90
91     ExceptionCode ec = 0;
92     if (exec->argumentCount() >= 3) {
93         bool async = exec->argument(2).toBoolean(exec);
94
95         if (exec->argumentCount() >= 4 && !exec->argument(3).isUndefined()) {
96             String user = valueToStringWithNullCheck(exec, exec->argument(3));
97
98             if (exec->argumentCount() >= 5 && !exec->argument(4).isUndefined()) {
99                 String password = valueToStringWithNullCheck(exec, exec->argument(4));
100                 impl()->open(method, url, async, user, password, ec);
101             } else
102                 impl()->open(method, url, async, user, ec);
103         } else
104             impl()->open(method, url, async, ec);
105     } else
106         impl()->open(method, url, ec);
107
108     setDOMException(exec, ec);
109     return jsUndefined();
110 }
111
112 JSValue JSXMLHttpRequest::send(ExecState* exec)
113 {
114     InspectorInstrumentation::willSendXMLHttpRequest(impl()->scriptExecutionContext(), impl()->url());
115
116     ExceptionCode ec = 0;
117     if (!exec->argumentCount())
118         impl()->send(ec);
119     else {
120         JSValue val = exec->argument(0);
121         if (val.isUndefinedOrNull())
122             impl()->send(ec);
123         else if (val.inherits(&JSDocument::s_info))
124             impl()->send(toDocument(val), ec);
125         else if (val.inherits(&JSBlob::s_info))
126             impl()->send(toBlob(val), ec);
127         else if (val.inherits(&JSDOMFormData::s_info))
128             impl()->send(toDOMFormData(val), ec);
129         else if (val.inherits(&JSArrayBuffer::s_info))
130             impl()->send(toArrayBuffer(val), ec);
131         else
132             impl()->send(ustringToString(val.toString(exec)), ec);
133     }
134
135     int signedLineNumber;
136     intptr_t sourceID;
137     UString sourceURL;
138     JSValue function;
139     exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function);
140     impl()->setLastSendLineNumber(signedLineNumber >= 0 ? signedLineNumber : 0);
141     impl()->setLastSendURL(ustringToString(sourceURL));
142
143     setDOMException(exec, ec);
144     return jsUndefined();
145 }
146
147 JSValue JSXMLHttpRequest::responseText(ExecState* exec) const
148 {
149     ExceptionCode ec = 0;
150     String text = impl()->responseText(ec);
151     if (ec) {
152         setDOMException(exec, ec);
153         return jsUndefined();
154     }
155     return jsOwnedStringOrNull(exec, text);
156 }
157
158 JSValue JSXMLHttpRequest::response(ExecState* exec) const
159 {
160     switch (impl()->responseTypeCode()) {
161     case XMLHttpRequest::ResponseTypeDefault:
162     case XMLHttpRequest::ResponseTypeText:
163         return responseText(exec);
164
165     case XMLHttpRequest::ResponseTypeDocument:
166         {
167             ExceptionCode ec = 0;
168             Document* document = impl()->responseXML(ec);
169             if (ec) {
170                 setDOMException(exec, ec);
171                 return jsUndefined();
172             }
173             return toJS(exec, globalObject(), document);
174         }
175
176     case XMLHttpRequest::ResponseTypeBlob:
177 #if ENABLE(XHR_RESPONSE_BLOB)
178         {
179             ExceptionCode ec = 0;
180             Blob* blob = impl()->responseBlob(ec);
181             if (ec) {
182                 setDOMException(exec, ec);
183                 return jsUndefined();
184             }
185             return toJS(exec, globalObject(), blob);
186         }
187 #else
188         return jsUndefined();
189 #endif
190
191     case XMLHttpRequest::ResponseTypeArrayBuffer:
192         {
193             ExceptionCode ec = 0;
194             ArrayBuffer* arrayBuffer = impl()->responseArrayBuffer(ec);
195             if (ec) {
196                 setDOMException(exec, ec);
197                 return jsUndefined();
198             }
199             return toJS(exec, globalObject(), arrayBuffer);
200         }
201     }
202
203     return jsUndefined();
204 }
205
206 } // namespace WebCore