~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/bindings/v8/custom/V8XMLHttpRequestCustom.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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 <wtf/ArrayBuffer.h>
 
35
#include "Document.h"
 
36
#include "Frame.h"
 
37
#include "InspectorInstrumentation.h"
 
38
#include "V8ArrayBuffer.h"
 
39
#include "V8ArrayBufferView.h"
 
40
#include "V8Binding.h"
 
41
#include "V8Blob.h"
 
42
#include "V8DOMFormData.h"
 
43
#include "V8Document.h"
 
44
#include "V8HTMLDocument.h"
 
45
#include "V8Utilities.h"
 
46
#include "WorkerContext.h"
 
47
#include "XMLHttpRequest.h"
 
48
 
 
49
namespace WebCore {
 
50
 
 
51
v8::Handle<v8::Value> V8XMLHttpRequest::constructorCallbackCustom(const v8::Arguments& args)
 
52
{
 
53
    ScriptExecutionContext* context = getScriptExecutionContext();
 
54
 
 
55
    RefPtr<SecurityOrigin> securityOrigin;
 
56
    if (context->isDocument()) {
 
57
        if (DOMWrapperWorld* world = worldForEnteredContextIfIsolated())
 
58
            securityOrigin = world->isolatedWorldSecurityOrigin();
 
59
    }
 
60
 
 
61
    RefPtr<XMLHttpRequest> xmlHttpRequest = XMLHttpRequest::create(context, securityOrigin);
 
62
 
 
63
    v8::Handle<v8::Object> wrapper = args.Holder();
 
64
    V8DOMWrapper::createDOMWrapper(xmlHttpRequest.release(), &info, wrapper);
 
65
    return wrapper;
 
66
}
 
67
 
 
68
v8::Handle<v8::Value> V8XMLHttpRequest::responseTextAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
 
69
{
 
70
    INC_STATS("DOM.XMLHttpRequest.responsetext._get");
 
71
    XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
 
72
    ExceptionCode ec = 0;
 
73
    const String& text = xmlHttpRequest->responseText(ec);
 
74
    if (ec)
 
75
        return setDOMException(ec, info.GetIsolate());
 
76
    return v8String(text, info.GetIsolate());
 
77
}
 
78
 
 
79
v8::Handle<v8::Value> V8XMLHttpRequest::responseAccessorGetter(v8::Local<v8::String> name, const v8::AccessorInfo& info)
 
80
{
 
81
    INC_STATS("DOM.XMLHttpRequest.response._get");
 
82
    XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(info.Holder());
 
83
 
 
84
    switch (xmlHttpRequest->responseTypeCode()) {
 
85
    case XMLHttpRequest::ResponseTypeDefault:
 
86
    case XMLHttpRequest::ResponseTypeText:
 
87
        return responseTextAccessorGetter(name, info);
 
88
 
 
89
    case XMLHttpRequest::ResponseTypeDocument:
 
90
        {
 
91
            ExceptionCode ec = 0;
 
92
            Document* document = xmlHttpRequest->responseXML(ec);
 
93
            if (ec)
 
94
                return setDOMException(ec, info.GetIsolate());
 
95
            return toV8(document, info.Holder(), info.GetIsolate());
 
96
        }
 
97
 
 
98
    case XMLHttpRequest::ResponseTypeBlob:
 
99
        {
 
100
            ExceptionCode ec = 0;
 
101
            Blob* blob = xmlHttpRequest->responseBlob(ec);
 
102
            if (ec)
 
103
                return setDOMException(ec, info.GetIsolate());
 
104
            return toV8(blob, info.Holder(), info.GetIsolate());
 
105
        }
 
106
 
 
107
    case XMLHttpRequest::ResponseTypeArrayBuffer:
 
108
        {
 
109
            ExceptionCode ec = 0;
 
110
            ArrayBuffer* arrayBuffer = xmlHttpRequest->responseArrayBuffer(ec);
 
111
            if (ec)
 
112
                return setDOMException(ec, info.GetIsolate());
 
113
            return toV8(arrayBuffer, info.Holder(), info.GetIsolate());
 
114
        }
 
115
    }
 
116
 
 
117
    return v8::Undefined();
 
118
}
 
119
 
 
120
v8::Handle<v8::Value> V8XMLHttpRequest::openCallback(const v8::Arguments& args)
 
121
{
 
122
    INC_STATS("DOM.XMLHttpRequest.open()");
 
123
    // Four cases:
 
124
    // open(method, url)
 
125
    // open(method, url, async)
 
126
    // open(method, url, async, user)
 
127
    // open(method, url, async, user, passwd)
 
128
 
 
129
    if (args.Length() < 2)
 
130
        return throwNotEnoughArgumentsError(args.GetIsolate());
 
131
 
 
132
    XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
 
133
 
 
134
    String method = toWebCoreString(args[0]);
 
135
    String urlstring = toWebCoreString(args[1]);
 
136
 
 
137
    ScriptExecutionContext* context = getScriptExecutionContext();
 
138
    KURL url = context->completeURL(urlstring);
 
139
 
 
140
    ExceptionCode ec = 0;
 
141
 
 
142
    if (args.Length() >= 3) {
 
143
        bool async = args[2]->BooleanValue();
 
144
 
 
145
        if (args.Length() >= 4 && !args[3]->IsUndefined()) {
 
146
            String user = toWebCoreStringWithNullCheck(args[3]);
 
147
            
 
148
            if (args.Length() >= 5 && !args[4]->IsUndefined()) {
 
149
                String passwd = toWebCoreStringWithNullCheck(args[4]);
 
150
                xmlHttpRequest->open(method, url, async, user, passwd, ec);
 
151
            } else
 
152
                xmlHttpRequest->open(method, url, async, user, ec);
 
153
        } else
 
154
            xmlHttpRequest->open(method, url, async, ec);
 
155
    } else
 
156
        xmlHttpRequest->open(method, url, ec);
 
157
 
 
158
    if (ec)
 
159
        return setDOMException(ec, args.GetIsolate());
 
160
 
 
161
    return v8::Undefined();
 
162
}
 
163
 
 
164
static bool isDocumentType(v8::Handle<v8::Value> value)
 
165
{
 
166
    // FIXME: add other document types.
 
167
    return V8Document::HasInstance(value) || V8HTMLDocument::HasInstance(value);
 
168
}
 
169
 
 
170
v8::Handle<v8::Value> V8XMLHttpRequest::sendCallback(const v8::Arguments& args)
 
171
{
 
172
    INC_STATS("DOM.XMLHttpRequest.send()");
 
173
    XMLHttpRequest* xmlHttpRequest = V8XMLHttpRequest::toNative(args.Holder());
 
174
 
 
175
    InspectorInstrumentation::willSendXMLHttpRequest(xmlHttpRequest->scriptExecutionContext(), xmlHttpRequest->url());
 
176
 
 
177
    ExceptionCode ec = 0;
 
178
    if (args.Length() < 1)
 
179
        xmlHttpRequest->send(ec);
 
180
    else {
 
181
        v8::Handle<v8::Value> arg = args[0];
 
182
        if (isUndefinedOrNull(arg))
 
183
            xmlHttpRequest->send(ec);
 
184
        else if (isDocumentType(arg)) {
 
185
            v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
 
186
            Document* document = V8Document::toNative(object);
 
187
            ASSERT(document);
 
188
            xmlHttpRequest->send(document, ec);
 
189
        } else if (V8Blob::HasInstance(arg)) {
 
190
            v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
 
191
            Blob* blob = V8Blob::toNative(object);
 
192
            ASSERT(blob);
 
193
            xmlHttpRequest->send(blob, ec);
 
194
        } else if (V8DOMFormData::HasInstance(arg)) {
 
195
            v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
 
196
            DOMFormData* domFormData = V8DOMFormData::toNative(object);
 
197
            ASSERT(domFormData);
 
198
            xmlHttpRequest->send(domFormData, ec);
 
199
#if ENABLE(WEBGL) || ENABLE(BLOB)
 
200
        } else if (V8ArrayBuffer::HasInstance(arg)) {
 
201
            v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
 
202
            ArrayBuffer* arrayBuffer = V8ArrayBuffer::toNative(object);
 
203
            ASSERT(arrayBuffer);
 
204
            xmlHttpRequest->send(arrayBuffer, ec);
 
205
        } else if (V8ArrayBufferView::HasInstance(arg)) {
 
206
            v8::Handle<v8::Object> object = v8::Handle<v8::Object>::Cast(arg);
 
207
            ArrayBufferView* arrayBufferView = V8ArrayBufferView::toNative(object);
 
208
            ASSERT(arrayBufferView);
 
209
            xmlHttpRequest->send(arrayBufferView, ec);
 
210
#endif
 
211
        } else
 
212
            xmlHttpRequest->send(toWebCoreStringWithNullCheck(arg), ec);
 
213
    }
 
214
 
 
215
    if (ec)
 
216
        return setDOMException(ec, args.GetIsolate());
 
217
 
 
218
    return v8::Undefined();
 
219
}
 
220
 
 
221
} // namespace WebCore