OSDN Git Service

Merge WebKit at r66666 : Initial merge by git.
[android-x86/external-webkit.git] / WebCore / bindings / v8 / custom / V8XMLHttpRequestCustom.cpp
1 /*
2  * Copyright (C) 2008, 2009, 2010 Google 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 are
6  * met:
7  *
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
13  * distribution.
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.
17  *
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.
29  */
30
31 #include "config.h"
32 #include "V8XMLHttpRequest.h"
33
34 #include "Frame.h"
35 #include "V8Binding.h"
36 #include "V8Blob.h"
37 #include "V8DOMFormData.h"
38 #include "V8Document.h"
39 #include "V8HTMLDocument.h"
40 #include "V8Proxy.h"
41 #include "V8Utilities.h"
42 #include "WorkerContext.h"
43 #include "WorkerContextExecutionProxy.h"
44 #include "XMLHttpRequest.h"
45
46 namespace WebCore {
47
48 v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
49 {
50     INC_STATS("DOM.XMLHttpRequest.responsetext._get");
51     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
52     ExceptionCode ec = 0;
53     const ScriptString& text = xmlHttpRequest->responseText(ec);
54     if (ec)
55         return throwError(ec);
56     return text.v8StringOrNull();
57 }
58
59 v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
60 {
61     INC_STATS("DOM.XMLHttpRequest.open()");
62     // Four cases:
63     // open(method, url)
64     // open(method, url, async)
65     // open(method, url, async, user)
66     // open(method, url, async, user, passwd)
67
68     if (args.Length() < 2)
69         return throwError("Not enough arguments", V8Proxy::SyntaxError);
70
71     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
72
73     String method = toWebCoreString(args[0]);
74     String urlstring = toWebCoreString(args[1]);
75     ScriptExecutionContext* context = getScriptExecutionContext();
76     if (!context)
77         return v8::Undefined();
78
79     KURL url = context->completeURL(urlstring);
80
81     ExceptionCode ec = 0;
82
83     if (args.Length() >= 3) {
84         bool async = args[2]->BooleanValue();
85
86         if (args.Length() >= 4 && !args[3]->IsUndefined()) {
87             String user = toWebCoreStringWithNullCheck(args[3]);
88             
89             if (args.Length() >= 5 && !args[4]->IsUndefined()) {
90                 String passwd = toWebCoreStringWithNullCheck(args[4]);
91                 xmlHttpRequest->open(method, url, async, user, passwd, ec);
92             } else
93                 xmlHttpRequest->open(method, url, async, user, ec);
94         } else
95             xmlHttpRequest->open(method, url, async, ec);
96     } else
97         xmlHttpRequest->open(method, url, ec);
98
99     if (ec)
100         return throwError(ec);
101
102     return v8::Undefined();
103 }
104
105 static bool isDocumentType(v8::Handle<v8::Value> value)
106 {
107     // FIXME: add other document types.
108     return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
109 }
110
111 v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
112 {
113     INC_STATS("DOM.XMLHttpRequest.send()");
114     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
115
116     ExceptionCode ec = 0;
117     if (args.Length() < 1)
118         xmlHttpRequest->send(ec);
119     else {
120         v8::Handle<v8::Value> arg = args[0];
121         if (isUndefinedOrNull(arg))
122             xmlHttpRequest->send(ec);
123         else if (isDocumentType(arg)) {
124             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
125             Document* document = V8Document::toNative(object);
126             ASSERT(document);
127             xmlHttpRequest->send(document, ec);
128         } else if (V8Blob::HasInstance(arg)) {
129             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
130             Blob* blob = V8Blob::toNative(object);
131             ASSERT(blob);
132             xmlHttpRequest->send(blob, ec);
133         } else if (V8DOMFormData::HasInstance(arg)) {
134             v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
135             DOMFormData* domFormData = V8DOMFormData::toNative(object);
136             ASSERT(domFormData);
137             xmlHttpRequest->send(domFormData, ec);
138         } else
139             xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
140     }
141
142     if (ec)
143         return throwError(ec);
144
145     return v8::Undefined();
146 }
147
148 } // namespace WebCore