|
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 "JSBlob.h" |
|
41 #include "JSDOMFormData.h" |
|
42 #include "JSDOMWindowCustom.h" |
|
43 #include "JSDocument.h" |
|
44 #include "JSEvent.h" |
|
45 #include "JSEventListener.h" |
|
46 #include "XMLHttpRequest.h" |
|
47 #include <runtime/Error.h> |
|
48 #include <interpreter/Interpreter.h> |
|
49 |
|
50 using namespace JSC; |
|
51 |
|
52 namespace WebCore { |
|
53 |
|
54 void JSXMLHttpRequest::markChildren(MarkStack& markStack) |
|
55 { |
|
56 Base::markChildren(markStack); |
|
57 |
|
58 if (XMLHttpRequestUpload* upload = m_impl->optionalUpload()) |
|
59 markDOMObjectWrapper(markStack, *Heap::heap(this)->globalData(), upload); |
|
60 |
|
61 m_impl->markJSEventListeners(markStack); |
|
62 } |
|
63 |
|
64 // Custom functions |
|
65 JSValue JSXMLHttpRequest::open(ExecState* exec) |
|
66 { |
|
67 if (exec->argumentCount() < 2) |
|
68 return throwError(exec, createSyntaxError(exec, "Not enough arguments")); |
|
69 |
|
70 const KURL& url = impl()->scriptExecutionContext()->completeURL(ustringToString(exec->argument(1).toString(exec))); |
|
71 String method = ustringToString(exec->argument(0).toString(exec)); |
|
72 |
|
73 ExceptionCode ec = 0; |
|
74 if (exec->argumentCount() >= 3) { |
|
75 bool async = exec->argument(2).toBoolean(exec); |
|
76 |
|
77 if (exec->argumentCount() >= 4 && !exec->argument(3).isUndefined()) { |
|
78 String user = valueToStringWithNullCheck(exec, exec->argument(3)); |
|
79 |
|
80 if (exec->argumentCount() >= 5 && !exec->argument(4).isUndefined()) { |
|
81 String password = valueToStringWithNullCheck(exec, exec->argument(4)); |
|
82 impl()->open(method, url, async, user, password, ec); |
|
83 } else |
|
84 impl()->open(method, url, async, user, ec); |
|
85 } else |
|
86 impl()->open(method, url, async, ec); |
|
87 } else |
|
88 impl()->open(method, url, ec); |
|
89 |
|
90 setDOMException(exec, ec); |
|
91 return jsUndefined(); |
|
92 } |
|
93 |
|
94 JSValue JSXMLHttpRequest::send(ExecState* exec) |
|
95 { |
|
96 ExceptionCode ec = 0; |
|
97 if (!exec->argumentCount()) |
|
98 impl()->send(ec); |
|
99 else { |
|
100 JSValue val = exec->argument(0); |
|
101 if (val.isUndefinedOrNull()) |
|
102 impl()->send(ec); |
|
103 else if (val.inherits(&JSDocument::s_info)) |
|
104 impl()->send(toDocument(val), ec); |
|
105 else if (val.inherits(&JSBlob::s_info)) |
|
106 impl()->send(toBlob(val), ec); |
|
107 else if (val.inherits(&JSDOMFormData::s_info)) |
|
108 impl()->send(toDOMFormData(val), ec); |
|
109 else |
|
110 impl()->send(ustringToString(val.toString(exec)), ec); |
|
111 } |
|
112 |
|
113 int signedLineNumber; |
|
114 intptr_t sourceID; |
|
115 UString sourceURL; |
|
116 JSValue function; |
|
117 exec->interpreter()->retrieveLastCaller(exec, signedLineNumber, sourceID, sourceURL, function); |
|
118 impl()->setLastSendLineNumber(signedLineNumber >= 0 ? signedLineNumber : 0); |
|
119 impl()->setLastSendURL(ustringToString(sourceURL)); |
|
120 |
|
121 setDOMException(exec, ec); |
|
122 return jsUndefined(); |
|
123 } |
|
124 |
|
125 JSValue JSXMLHttpRequest::responseText(ExecState* exec) const |
|
126 { |
|
127 return jsOwnedStringOrNull(exec, impl()->responseText()); |
|
128 } |
|
129 |
|
130 EncodedJSValue JSC_HOST_CALL JSXMLHttpRequestConstructor::constructJSXMLHttpRequest(ExecState* exec) |
|
131 { |
|
132 JSXMLHttpRequestConstructor* jsConstructor = static_cast<JSXMLHttpRequestConstructor*>(exec->callee()); |
|
133 ScriptExecutionContext* context = jsConstructor->scriptExecutionContext(); |
|
134 if (!context) |
|
135 return throwVMError(exec, createReferenceError(exec, "XMLHttpRequest constructor associated document is unavailable")); |
|
136 |
|
137 RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context); |
|
138 return JSValue::encode(CREATE_DOM_OBJECT_WRAPPER(exec, jsConstructor->globalObject(), XMLHttpRequest, xmlHttpRequest.get())); |
|
139 } |
|
140 |
|
141 } // namespace WebCore |