2 * Copyright (C) 2008, 2009, 2010 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 "V8XMLHttpRequest.h"
35 #include "V8Binding.h"
37 #include "V8DOMFormData.h"
38 #include "V8Document.h"
39 #include "V8HTMLDocument.h"
41 #include "V8Utilities.h"
42 #include "WorkerContext.h"
43 #include "WorkerContextExecutionProxy.h"
44 #include "XMLHttpRequest.h"
48 v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
50 INC_STATS("DOM.XMLHttpRequest.responsetext._get");
51 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
52 return xmlHttpRequest->responseText().v8StringOrNull();
55 v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
57 INC_STATS("DOM.XMLHttpRequest.open()");
60 // open(method, url, async)
61 // open(method, url, async, user)
62 // open(method, url, async, user, passwd)
64 if (args.Length() < 2)
65 return throwError("Not enough arguments", V8Proxy::SyntaxError);
67 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
69 String method = toWebCoreString(args[0]);
70 String urlstring = toWebCoreString(args[1]);
71 ScriptExecutionContext* context = getScriptExecutionContext();
73 return v8::Undefined();
75 KURL url = context->completeURL(urlstring);
79 if (args.Length() >= 3) {
80 bool async = args[2]->BooleanValue();
82 if (args.Length() >= 4 && !args[3]->IsUndefined()) {
83 String user = toWebCoreStringWithNullCheck(args[3]);
85 if (args.Length() >= 5 && !args[4]->IsUndefined()) {
86 String passwd = toWebCoreStringWithNullCheck(args[4]);
87 xmlHttpRequest->open(method, url, async, user, passwd, ec);
89 xmlHttpRequest->open(method, url, async, user, ec);
91 xmlHttpRequest->open(method, url, async, ec);
93 xmlHttpRequest->open(method, url, ec);
96 return throwError(ec);
98 return v8::Undefined();
101 static bool isDocumentType(v8::Handle<v8::Value> value)
103 // FIXME: add other document types.
104 return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
107 v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
109 INC_STATS("DOM.XMLHttpRequest.send()");
110 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
112 ExceptionCode ec = 0;
113 if (args.Length() < 1)
114 xmlHttpRequest->send(ec);
116 v8::Handle<v8::Value> arg = args[0];
117 if (isUndefinedOrNull(arg))
118 xmlHttpRequest->send(ec);
119 else if (isDocumentType(arg)) {
120 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
121 Document* document = V8Document::toNative(object);
123 xmlHttpRequest->send(document, ec);
124 } else if (V8Blob::HasInstance(arg)) {
125 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
126 Blob* blob = V8Blob::toNative(object);
128 xmlHttpRequest->send(blob, ec);
129 } else if (V8DOMFormData::HasInstance(arg)) {
130 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
131 DOMFormData* domFormData = V8DOMFormData::toNative(object);
133 xmlHttpRequest->send(domFormData, ec);
135 xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
139 return throwError(ec);
141 return v8::Undefined();
144 } // namespace WebCore