File: Body.cpp

package info (click to toggle)
chromium-browser 41.0.2272.118-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 2,189,132 kB
  • sloc: cpp: 9,691,462; ansic: 3,341,451; python: 712,689; asm: 518,779; xml: 208,926; java: 169,820; sh: 119,353; perl: 68,907; makefile: 28,311; yacc: 13,305; objc: 11,385; tcl: 3,186; cs: 2,225; sql: 2,217; lex: 2,215; lisp: 1,349; pascal: 1,256; awk: 407; ruby: 155; sed: 53; php: 14; exp: 11
file content (441 lines) | stat: -rw-r--r-- 13,945 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2014 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "config.h"
#include "modules/fetch/Body.h"

#include "bindings/core/v8/ExceptionState.h"
#include "bindings/core/v8/ScriptPromiseResolver.h"
#include "bindings/core/v8/ScriptState.h"
#include "bindings/core/v8/V8ArrayBuffer.h"
#include "bindings/core/v8/V8ThrowException.h"
#include "core/dom/DOMArrayBuffer.h"
#include "core/fileapi/Blob.h"
#include "core/fileapi/FileReaderLoader.h"
#include "core/fileapi/FileReaderLoaderClient.h"
#include "core/streams/UnderlyingSource.h"
#include "modules/fetch/BodyStreamBuffer.h"

namespace blink {

class Body::BlobHandleReceiver final : public BodyStreamBuffer::BlobHandleCreatorClient {
public:
    explicit BlobHandleReceiver(Body* body)
        : m_body(body)
    {
    }
    void didCreateBlobHandle(PassRefPtr<BlobDataHandle> handle) override
    {
        ASSERT(m_body);
        m_body->readAsyncFromBlob(handle);
        m_body = nullptr;
    }
    void didFail(PassRefPtrWillBeRawPtr<DOMException> exception) override
    {
        ASSERT(m_body);
        m_body->didBlobHandleReceiveError(exception);
        m_body = nullptr;
    }
    void trace(Visitor* visitor) override
    {
        BodyStreamBuffer::BlobHandleCreatorClient::trace(visitor);
        visitor->trace(m_body);
    }
private:
    Member<Body> m_body;
};

class Body::ReadableStreamSource : public GarbageCollectedFinalized<ReadableStreamSource>, public UnderlyingSource {
    USING_GARBAGE_COLLECTED_MIXIN(ReadableStreamSource);
public:
    ReadableStreamSource(Body* body) : m_body(body) { }
    ~ReadableStreamSource() override { }
    void pullSource() override { m_body->pullSource(); }

    ScriptPromise cancelSource(ScriptState* scriptState, ScriptValue reason) override
    {
        return ScriptPromise();
    }

    void trace(Visitor* visitor) override
    {
        visitor->trace(m_body);
        UnderlyingSource::trace(visitor);
    }

private:
    Member<Body> m_body;
};

void Body::pullSource()
{
    if (!m_streamAccessed) {
        // We do not download data unless the user explicitly uses the
        // ReadableStream object in order to avoid performance regression,
        // because currently Chrome cannot handle Streams efficiently
        // especially with ServiceWorker or Blob.
        return;
    }
    if (m_bodyUsed) {
        m_stream->error(DOMException::create(InvalidStateError, "The stream is locked."));
        return;
    }
    ASSERT(!m_loader);
    if (buffer()) {
        // If the body has a body buffer, we read all data from the buffer and
        // create a blob and then put the data from the blob to |m_stream|.
        // FIXME: Put the data directry from the buffer.
        buffer()->readAllAndCreateBlobHandle(contentTypeForBuffer(), new BlobHandleReceiver(this));
        return;
    }
    RefPtr<BlobDataHandle> blobHandle = blobDataHandle();
    if (!blobHandle.get()) {
        blobHandle = BlobDataHandle::create(BlobData::create(), 0);
    }
    readAsyncFromBlob(blobHandle);
}

ScriptPromise Body::readAsync(ScriptState* scriptState, ResponseType type)
{
    if (m_bodyUsed)
        return ScriptPromise::reject(scriptState, V8ThrowException::createTypeError(scriptState->isolate(), "Already read"));

    // When the main thread sends a V8::TerminateExecution() signal to a worker
    // thread, any V8 API on the worker thread starts returning an empty
    // handle. This can happen in Body::readAsync. To avoid the situation, we
    // first check the ExecutionContext and return immediately if it's already
    // gone (which means that the V8::TerminateExecution() signal has been sent
    // to this worker thread).
    ExecutionContext* executionContext = scriptState->executionContext();
    if (!executionContext)
        return ScriptPromise();

    m_bodyUsed = true;
    m_responseType = type;

    ASSERT(!m_resolver);
    m_resolver = ScriptPromiseResolver::create(scriptState);
    ScriptPromise promise = m_resolver->promise();

    if (m_streamAccessed) {
        // 'body' attribute was accessed and the stream source started pulling.
        switch (m_stream->state()) {
        case ReadableStream::Readable:
            readAllFromStream(scriptState);
            return promise;
        case ReadableStream::Waiting:
            // m_loader is working and m_resolver will be resolved when it
            // ends.
            return promise;
        case ReadableStream::Closed:
        case ReadableStream::Errored:
            m_resolver->resolve(m_stream->closed(scriptState).v8Value());
            return promise;
            break;
        }
        ASSERT_NOT_REACHED();
        return promise;
    }

    if (buffer()) {
        buffer()->readAllAndCreateBlobHandle(contentTypeForBuffer(), new BlobHandleReceiver(this));
        return promise;
    }
    readAsyncFromBlob(blobDataHandle());
    return promise;
}

void Body::readAsyncFromBlob(PassRefPtr<BlobDataHandle> handle)
{
    if (m_streamAccessed) {
        FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsArrayBuffer;
        m_loader = adoptPtr(new FileReaderLoader(readType, this));
        m_loader->start(executionContext(), handle);
        return;
    }
    FileReaderLoader::ReadType readType = FileReaderLoader::ReadAsText;
    RefPtr<BlobDataHandle> blobHandle = handle;
    if (!blobHandle.get()) {
        blobHandle = BlobDataHandle::create(BlobData::create(), 0);
    }
    switch (m_responseType) {
    case ResponseAsArrayBuffer:
        readType = FileReaderLoader::ReadAsArrayBuffer;
        break;
    case ResponseAsBlob:
        if (blobHandle->size() != kuint64max) {
            // If the size of |blobHandle| is set correctly, creates Blob from
            // it.
            m_resolver->resolve(Blob::create(blobHandle));
            m_resolver.clear();
            return;
        }
        // If the size is not set, read as ArrayBuffer and create a new blob to
        // get the size.
        // FIXME: This workaround is not good for performance.
        // When we will stop using Blob as a base system of Body to support
        // stream, this problem should be solved.
        readType = FileReaderLoader::ReadAsArrayBuffer;
        break;
    case ResponseAsFormData:
        // FIXME: Implement this.
        ASSERT_NOT_REACHED();
        break;
    case ResponseAsJSON:
    case ResponseAsText:
        break;
    default:
        ASSERT_NOT_REACHED();
    }

    m_loader = adoptPtr(new FileReaderLoader(readType, this));
    m_loader->start(m_resolver->scriptState()->executionContext(), blobHandle);

    return;
}

void Body::readAllFromStream(ScriptState* scriptState)
{
    // With the current loading mechanism, the data is loaded atomically.
    ASSERT(m_stream->isDraining());
    TrackExceptionState es;
    // FIXME: Implement and use another |read| method that doesn't
    // need an exception state and V8ArrayBuffer.
    ScriptValue value = m_stream->read(scriptState, es);
    ASSERT(!es.hadException());
    ASSERT(m_stream->state() == ReadableStream::Closed);
    ASSERT(!value.isEmpty() && V8ArrayBuffer::hasInstance(value.v8Value(), scriptState->isolate()));
    DOMArrayBuffer* buffer = V8ArrayBuffer::toImpl(value.v8Value().As<v8::Object>());
    didFinishLoadingViaStream(buffer);
    m_resolver.clear();
    m_stream->close();
}

ScriptPromise Body::arrayBuffer(ScriptState* scriptState)
{
    return readAsync(scriptState, ResponseAsArrayBuffer);
}

ScriptPromise Body::blob(ScriptState* scriptState)
{
    return readAsync(scriptState, ResponseAsBlob);
}

ScriptPromise Body::formData(ScriptState* scriptState)
{
    return readAsync(scriptState, ResponseAsFormData);
}

ScriptPromise Body::json(ScriptState* scriptState)
{
    return readAsync(scriptState, ResponseAsJSON);
}

ScriptPromise Body::text(ScriptState* scriptState)
{
    return readAsync(scriptState, ResponseAsText);
}

ReadableStream* Body::body()
{
    if (!m_streamAccessed) {
        m_streamAccessed = true;
        if (m_stream->isPulling()) {
            // The stream has been pulling, but the source ignored the
            // instruction because it didn't know the user wanted to use the
            // ReadableStream interface. Now it knows the user does, so have
            // the source start pulling.
            m_streamSource->pullSource();
        }
    }
    return m_stream;
}

bool Body::bodyUsed() const
{
    return m_bodyUsed;
}

void Body::setBodyUsed()
{
    m_bodyUsed = true;
}

bool Body::streamAccessed() const
{
    return m_streamAccessed;
}

void Body::stop()
{
    // Canceling the load will call didFail which will remove the resolver.
    if (m_loader)
        m_loader->cancel();
}

bool Body::hasPendingActivity() const
{
    if (m_resolver)
        return true;
    if (m_streamAccessed && (m_stream->state() == ReadableStream::Readable || m_stream->state() == ReadableStream::Waiting))
        return true;
    return false;
}

void Body::trace(Visitor* visitor)
{
    visitor->trace(m_resolver);
    visitor->trace(m_stream);
    visitor->trace(m_streamSource);
    ActiveDOMObject::trace(visitor);
}

Body::Body(ExecutionContext* context)
    : ActiveDOMObject(context)
    , m_bodyUsed(false)
    , m_streamAccessed(false)
    , m_responseType(ResponseType::ResponseUnknown)
    , m_streamSource(new ReadableStreamSource(this))
    , m_stream(new ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArrayBuffer>>(context, m_streamSource))
{
    m_stream->didSourceStart();
}

Body::Body(const Body& copy_from)
    : ActiveDOMObject(copy_from.lifecycleContext())
    , m_bodyUsed(copy_from.bodyUsed())
    , m_responseType(ResponseType::ResponseUnknown)
    , m_streamSource(new ReadableStreamSource(this))
    , m_stream(new ReadableStreamImpl<ReadableStreamChunkTypeTraits<DOMArrayBuffer>>(copy_from.executionContext(), m_streamSource))
{
    m_stream->didSourceStart();
}

void Body::resolveJSON(const String& string)
{
    ASSERT(m_responseType == ResponseAsJSON);
    ScriptState::Scope scope(m_resolver->scriptState());
    v8::Isolate* isolate = m_resolver->scriptState()->isolate();
    v8::Local<v8::String> inputString = v8String(isolate, string);
    v8::TryCatch trycatch;
    v8::Local<v8::Value> parsed = v8::JSON::Parse(inputString);
    if (parsed.IsEmpty()) {
        if (trycatch.HasCaught())
            m_resolver->reject(trycatch.Exception());
        else
            m_resolver->reject(v8::Exception::Error(v8::String::NewFromUtf8(isolate, "JSON parse error")));
        return;
    }
    m_resolver->resolve(parsed);
}

// FileReaderLoaderClient functions.
void Body::didStartLoading() { }
void Body::didReceiveData() { }
void Body::didFinishLoading()
{
    if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
        return;

    if (m_streamAccessed) {
        didFinishLoadingViaStream(m_loader->arrayBufferResult().get());
        m_resolver.clear();
        m_stream->close();
        return;
    }

    switch (m_responseType) {
    case ResponseAsArrayBuffer:
        m_resolver->resolve(m_loader->arrayBufferResult());
        break;
    case ResponseAsBlob: {
        ASSERT(blobDataHandle()->size() == kuint64max);
        OwnPtr<BlobData> blobData = BlobData::create();
        RefPtr<DOMArrayBuffer> buffer = m_loader->arrayBufferResult();
        blobData->appendBytes(buffer->data(), buffer->byteLength());
        const size_t length = blobData->length();
        m_resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release(), length)));
        break;
    }
    case ResponseAsFormData:
        ASSERT_NOT_REACHED();
        break;
    case ResponseAsJSON:
        resolveJSON(m_loader->stringResult());
        break;
    case ResponseAsText:
        m_resolver->resolve(m_loader->stringResult());
        break;
    default:
        ASSERT_NOT_REACHED();
    }
    m_resolver.clear();
    m_stream->close();
}

void Body::didFinishLoadingViaStream(DOMArrayBuffer* buffer)
{
    if (!m_bodyUsed) {
        // |m_stream| is pulling.
        ASSERT(m_streamAccessed);
        m_stream->enqueue(buffer);
        return;
    }

    switch (m_responseType) {
    case ResponseAsArrayBuffer:
        m_resolver->resolve(buffer);
        break;
    case ResponseAsBlob: {
        OwnPtr<BlobData> blobData = BlobData::create();
        blobData->appendBytes(buffer->data(), buffer->byteLength());
        m_resolver->resolve(Blob::create(BlobDataHandle::create(blobData.release(), blobData->length())));
        break;
    }
    case ResponseAsFormData:
        ASSERT_NOT_REACHED();
        break;
    case ResponseAsJSON: {
        String s = String::fromUTF8(static_cast<const char*>(buffer->data()), buffer->byteLength());
        if (s.isNull())
            m_resolver->reject(DOMException::create(NetworkError, "Invalid utf-8 string"));
        else
            resolveJSON(s);
        break;
    }
    case ResponseAsText: {
        String s = String::fromUTF8(static_cast<const char*>(buffer->data()), buffer->byteLength());
        if (s.isNull())
            m_resolver->reject(DOMException::create(NetworkError, "Invalid utf-8 string"));
        else
            m_resolver->resolve(s);
        break;
    }
    default:
        ASSERT_NOT_REACHED();
    }
}

void Body::didFail(FileError::ErrorCode code)
{
    if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
        return;

    if (m_resolver) {
        // FIXME: We should reject the promise.
        m_resolver->resolve("");
        m_resolver.clear();
    }
    m_stream->error(DOMException::create(NetworkError, "network error"));
}

void Body::didBlobHandleReceiveError(PassRefPtrWillBeRawPtr<DOMException> exception)
{
    if (!m_resolver)
        return;
    m_resolver->reject(exception);
    m_resolver.clear();
}

} // namespace blink