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 "InspectorController.h"
36 #include "V8Binding.h"
38 #include "V8DOMFormData.h"
39 #include "V8Document.h"
40 #include "V8HTMLDocument.h"
42 #include "V8Utilities.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
45 #include "XMLHttpRequest.h"
49 v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
51 INC_STATS("DOM.XMLHttpRequest.responsetext._get");
52 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
54 const ScriptString& text = xmlHttpRequest->responseText(ec);
56 return throwError(ec);
57 return text.v8StringOrNull();
60 v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
62 INC_STATS("DOM.XMLHttpRequest.open()");
65 // open(method, url, async)
66 // open(method, url, async, user)
67 // open(method, url, async, user, passwd)
69 if (args.Length() < 2)
70 return throwError("Not enough arguments", V8Proxy::SyntaxError);
72 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
74 String method = toWebCoreString(args[0]);
75 String urlstring = toWebCoreString(args[1]);
76 ScriptExecutionContext* context = getScriptExecutionContext();
78 return v8::Undefined();
80 KURL url = context->completeURL(urlstring);
84 if (args.Length() >= 3) {
85 bool async = args[2]->BooleanValue();
87 if (args.Length() >= 4 && !args[3]->IsUndefined()) {
88 String user = toWebCoreStringWithNullCheck(args[3]);
90 if (args.Length() >= 5 && !args[4]->IsUndefined()) {
91 String passwd = toWebCoreStringWithNullCheck(args[4]);
92 xmlHttpRequest->open(method, url, async, user, passwd, ec);
94 xmlHttpRequest->open(method, url, async, user, ec);
96 xmlHttpRequest->open(method, url, async, ec);
98 xmlHttpRequest->open(method, url, ec);
101 return throwError(ec);
103 return v8::Undefined();
106 static bool isDocumentType(v8::Handle<v8::Value> value)
108 // FIXME: add other document types.
109 return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
112 v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
114 INC_STATS("DOM.XMLHttpRequest.send()");
115 XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
117 InspectorController::instrumentWillSendXMLHttpRequest(xmlHttpRequest->scriptExecutionContext(), xmlHttpRequest->url());
119 ExceptionCode ec = 0;
120 if (args.Length() < 1)
121 xmlHttpRequest->send(ec);
123 v8::Handle<v8::Value> arg = args[0];
124 if (isUndefinedOrNull(arg))
125 xmlHttpRequest->send(ec);
126 else if (isDocumentType(arg)) {
127 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
128 Document* document = V8Document::toNative(object);
130 xmlHttpRequest->send(document, ec);
131 } else if (V8Blob::HasInstance(arg)) {
132 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
133 Blob* blob = V8Blob::toNative(object);
135 xmlHttpRequest->send(blob, ec);
136 } else if (V8DOMFormData::HasInstance(arg)) {
137 v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
138 DOMFormData* domFormData = V8DOMFormData::toNative(object);
140 xmlHttpRequest->send(domFormData, ec);
142 xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
146 return throwError(ec);
148 return v8::Undefined();
151 } // namespace WebCore