OSDN Git Service

am 2346109e: (-s ours) am 93bec1ec: DO NOT MERGE : Fix Webkit comments for document...
[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 "InspectorController.h"
36 #include "V8Binding.h"
37 #include "V8Blob.h"
38 #include "V8DOMFormData.h"
39 #include "V8Document.h"
40 #include "V8HTMLDocument.h"
41 #include "V8Proxy.h"
42 #include "V8Utilities.h"
43 #include "WorkerContext.h"
44 #include "WorkerContextExecutionProxy.h"
45 #include "XMLHttpRequest.h"
46
47 namespace WebCore {
48
49 v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
50 {
51     INC_STATS("DOM.XMLHttpRequest.responsetext._get");
52     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
53     ExceptionCode ec = 0;
54     const ScriptString& text = xmlHttpRequest->responseText(ec);
55     if (ec)
56         return throwError(ec);
57     return text.v8StringOrNull();
58 }
59
60 v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
61 {
62     INC_STATS("DOM.XMLHttpRequest.open()");
63     // Four cases:
64     // open(method, url)
65     // open(method, url, async)
66     // open(method, url, async, user)
67     // open(method, url, async, user, passwd)
68
69     if (args.Length() < 2)
70         return throwError("Not enough arguments", V8Proxy::SyntaxError);
71
72     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
73
74     String method = toWebCoreString(args[0]);
75     String urlstring = toWebCoreString(args[1]);
76     ScriptExecutionContext* context = getScriptExecutionContext();
77     if (!context)
78         return v8::Undefined();
79
80     KURL url = context->completeURL(urlstring);
81
82     ExceptionCode ec = 0;
83
84     if (args.Length() >= 3) {
85         bool async = args[2]->BooleanValue();
86
87         if (args.Length() >= 4 && !args[3]->IsUndefined()) {
88             String user = toWebCoreStringWithNullCheck(args[3]);
89             
90             if (args.Length() >= 5 && !args[4]->IsUndefined()) {
91                 String passwd = toWebCoreStringWithNullCheck(args[4]);
92                 xmlHttpRequest->open(method, url, async, user, passwd, ec);
93             } else
94                 xmlHttpRequest->open(method, url, async, user, ec);
95         } else
96             xmlHttpRequest->open(method, url, async, ec);
97     } else
98         xmlHttpRequest->open(method, url, ec);
99
100     if (ec)
101         return throwError(ec);
102
103     return v8::Undefined();
104 }
105
106 static bool isDocumentType(v8::Handle<v8::Value> value)
107 {
108     // FIXME: add other document types.
109     return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
110 }
111
112 v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
113 {
114     INC_STATS("DOM.XMLHttpRequest.send()");
115     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
116
117     InspectorController::instrumentWillSendXMLHttpRequest(xmlHttpRequest->scriptExecutionContext(), xmlHttpRequest->url());
118
119     ExceptionCode ec = 0;
120     if (args.Length() < 1)
121         xmlHttpRequest->send(ec);
122     else {
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);
129             ASSERT(document);
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);
134             ASSERT(blob);
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);
139             ASSERT(domFormData);
140             xmlHttpRequest->send(domFormData, ec);
141         } else
142             xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
143     }
144
145     if (ec)
146         return throwError(ec);
147
148     return v8::Undefined();
149 }
150
151 } // namespace WebCore