2 * Copyright (C) 2008, 2009 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 "XMLHttpRequest.h"
35 #include "V8Binding.h"
36 #include "V8CustomBinding.h"
37 #include "V8Document.h"
39 #include "V8HTMLDocument.h"
41 #include "V8Utilities.h"
42 #include "WorkerContext.h"
43 #include "WorkerContextExecutionProxy.h"
47 ACCESSOR_GETTER(XMLHttpRequestResponseText)
49 INC_STATS("DOM.XMLHttpRequest.responsetext._get");
50 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, info.Holder());
51 return xmlHttpRequest->responseText().v8StringOrNull();
54 CALLBACK_FUNC_DECL(XMLHttpRequestAddEventListener)
56 INC_STATS("DOM.XMLHttpRequest.addEventListener()");
57 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
59 RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOrCreate);
61 String type = toWebCoreString(args[0]);
62 bool useCapture = args[2]->BooleanValue();
63 xmlHttpRequest->addEventListener(type, listener, useCapture);
65 createHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
67 return v8::Undefined();
70 CALLBACK_FUNC_DECL(XMLHttpRequestRemoveEventListener)
72 INC_STATS("DOM.XMLHttpRequest.removeEventListener()");
73 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
75 RefPtr<EventListener> listener = V8DOMWrapper::getEventListener(xmlHttpRequest, args[1], false, ListenerFindOnly);
77 String type = toWebCoreString(args[0]);
78 bool useCapture = args[2]->BooleanValue();
79 xmlHttpRequest->removeEventListener(type, listener.get(), useCapture);
81 removeHiddenDependency(args.Holder(), args[1], V8Custom::kXMLHttpRequestCacheIndex);
84 return v8::Undefined();
87 CALLBACK_FUNC_DECL(XMLHttpRequestOpen)
89 INC_STATS("DOM.XMLHttpRequest.open()");
92 // open(method, url, async)
93 // open(method, url, async, user)
94 // open(method, url, async, user, passwd)
96 if (args.Length() < 2)
97 return throwError("Not enough arguments", V8Proxy::SyntaxError);
99 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
101 String method = toWebCoreString(args[0]);
102 String urlstring = toWebCoreString(args[1]);
103 ScriptExecutionContext* context = getScriptExecutionContext();
105 return v8::Undefined();
107 KURL url = context->completeURL(urlstring);
109 bool async = (args.Length() < 3) ? true : args[2]->BooleanValue();
111 ExceptionCode ec = 0;
113 if (args.Length() >= 4 && !args[3]->IsUndefined()) {
114 user = toWebCoreStringWithNullCheck(args[3]);
116 if (args.Length() >= 5 && !args[4]->IsUndefined()) {
117 passwd = toWebCoreStringWithNullCheck(args[4]);
118 xmlHttpRequest->open(method, url, async, user, passwd, ec);
120 xmlHttpRequest->open(method, url, async, user, ec);
122 xmlHttpRequest->open(method, url, async, ec);
125 return throwError(ec);
127 return v8::Undefined();
130 static bool IsDocumentType(v8::Handle<v8::Value> value)
132 // FIXME: add other document types.
133 return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
136 CALLBACK_FUNC_DECL(XMLHttpRequestSend)
138 INC_STATS("DOM.XMLHttpRequest.send()");
139 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
141 ExceptionCode ec = 0;
142 if (args.Length() < 1)
143 xmlHttpRequest->send(ec);
145 v8::Handle<v8::Value> arg = args[0];
146 if (IsDocumentType(arg)) {
147 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
148 Document* document = V8DOMWrapper::convertDOMWrapperToNode<Document>(object);
150 xmlHttpRequest->send(document, ec);
151 } else if (V8File::HasInstance(arg)) {
152 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
153 File* file = V8DOMWrapper::convertDOMWrapperToNative<File>(object);
155 xmlHttpRequest->send(file, ec);
157 xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
161 return throwError(ec);
163 return v8::Undefined();
166 CALLBACK_FUNC_DECL(XMLHttpRequestSetRequestHeader) {
167 INC_STATS("DOM.XMLHttpRequest.setRequestHeader()");
168 if (args.Length() < 2)
169 return throwError("Not enough arguments", V8Proxy::SyntaxError);
171 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
172 ExceptionCode ec = 0;
173 String header = toWebCoreString(args[0]);
174 String value = toWebCoreString(args[1]);
175 xmlHttpRequest->setRequestHeader(header, value, ec);
177 return throwError(ec);
178 return v8::Undefined();
181 CALLBACK_FUNC_DECL(XMLHttpRequestGetResponseHeader)
183 INC_STATS("DOM.XMLHttpRequest.getResponseHeader()");
184 if (args.Length() < 1)
185 return throwError("Not enough arguments", V8Proxy::SyntaxError);
187 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
188 ExceptionCode ec = 0;
189 String header = toWebCoreString(args[0]);
190 String result = xmlHttpRequest->getResponseHeader(header, ec);
192 return throwError(ec);
193 return v8StringOrNull(result);
196 CALLBACK_FUNC_DECL(XMLHttpRequestOverrideMimeType)
198 INC_STATS("DOM.XMLHttpRequest.overrideMimeType()");
199 if (args.Length() < 1)
200 return throwError("Not enough arguments", V8Proxy::SyntaxError);
202 XMLHttpRequest* xmlHttpRequest = V8DOMWrapper::convertToNativeObject<XMLHttpRequest>(V8ClassIndex::XMLHTTPREQUEST, args.Holder());
203 String value = toWebCoreString(args[0]);
204 xmlHttpRequest->overrideMimeType(value);
205 return v8::Undefined();
208 CALLBACK_FUNC_DECL(XMLHttpRequestDispatchEvent)
210 INC_STATS("DOM.XMLHttpRequest.dispatchEvent()");
211 return v8::Undefined();
214 } // namespace WebCore