OSDN Git Service

am a9b9e574: am da4ffba3: Cherry-pick security fix in WebKit change 64077
[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     return xmlHttpRequest->responseText().v8StringOrNull();
53 }
54
55 v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
56 {
57     INC_STATS("DOM.XMLHttpRequest.open()");
58     // Four cases:
59     // open(method, url)
60     // open(method, url, async)
61     // open(method, url, async, user)
62     // open(method, url, async, user, passwd)
63
64     if (args.Length() < 2)
65         return throwError("Not enough arguments", V8Proxy::SyntaxError);
66
67     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
68
69     String method = toWebCoreString(args[0]);
70     String urlstring = toWebCoreString(args[1]);
71     ScriptExecutionContext* context = getScriptExecutionContext();
72     if (!context)
73         return v8::Undefined();
74
75     KURL url = context->completeURL(urlstring);
76
77     ExceptionCode ec = 0;
78
79     if (args.Length() >= 3) {
80         bool async = args[2]->BooleanValue();
81
82         if (args.Length() >= 4 && !args[3]->IsUndefined()) {
83             String user = toWebCoreStringWithNullCheck(args[3]);
84             
85             if (args.Length() >= 5 && !args[4]->IsUndefined()) {
86                 String passwd = toWebCoreStringWithNullCheck(args[4]);
87                 xmlHttpRequest->open(method, url, async, user, passwd, ec);
88             } else
89                 xmlHttpRequest->open(method, url, async, user, ec);
90         } else
91             xmlHttpRequest->open(method, url, async, ec);
92     } else
93         xmlHttpRequest->open(method, url, ec);
94
95     if (ec)
96         return throwError(ec);
97
98     return v8::Undefined();
99 }
100
101 static bool isDocumentType(v8::Handle<v8::Value> value)
102 {
103     // FIXME: add other document types.
104     return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
105 }
106
107 v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
108 {
109     INC_STATS("DOM.XMLHttpRequest.send()");
110     XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
111
112     ExceptionCode ec = 0;
113     if (args.Length() < 1)
114         xmlHttpRequest->send(ec);
115     else {
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);
122             ASSERT(document);
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);
127             ASSERT(blob);
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);
132             ASSERT(domFormData);
133             xmlHttpRequest->send(domFormData, ec);
134         } else
135             xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
136     }
137
138     if (ec)
139         return throwError(ec);
140
141     return v8::Undefined();
142 }
143
144 } // namespace WebCore