OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "modules/fetch/BodyStreamBuffer.h" | 5 #include "modules/fetch/BodyStreamBuffer.h" |
6 | 6 |
7 #include "bindings/core/v8/ScriptState.h" | 7 #include "bindings/core/v8/ScriptState.h" |
8 #include "bindings/core/v8/V8HiddenValue.h" | 8 #include "bindings/core/v8/V8HiddenValue.h" |
| 9 #include "bindings/core/v8/WorkerOrWorkletScriptController.h" |
9 #include "core/dom/DOMArrayBuffer.h" | 10 #include "core/dom/DOMArrayBuffer.h" |
10 #include "core/dom/DOMTypedArray.h" | 11 #include "core/dom/DOMTypedArray.h" |
11 #include "core/dom/ExceptionCode.h" | 12 #include "core/dom/ExceptionCode.h" |
12 #include "core/streams/ReadableStreamController.h" | 13 #include "core/streams/ReadableStreamController.h" |
13 #include "core/streams/ReadableStreamOperations.h" | 14 #include "core/streams/ReadableStreamOperations.h" |
| 15 #include "core/workers/WorkerGlobalScope.h" |
14 #include "modules/fetch/Body.h" | 16 #include "modules/fetch/Body.h" |
15 #include "modules/fetch/BytesConsumerForDataConsumerHandle.h" | 17 #include "modules/fetch/BytesConsumerForDataConsumerHandle.h" |
16 #include "modules/fetch/DataConsumerHandleUtil.h" | 18 #include "modules/fetch/DataConsumerHandleUtil.h" |
17 #include "modules/fetch/DataConsumerTee.h" | 19 #include "modules/fetch/DataConsumerTee.h" |
18 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" | 20 #include "modules/fetch/ReadableStreamDataConsumerHandle.h" |
19 #include "platform/blob/BlobData.h" | 21 #include "platform/blob/BlobData.h" |
20 #include "platform/network/EncodedFormData.h" | 22 #include "platform/network/EncodedFormData.h" |
21 #include <memory> | 23 #include <memory> |
22 | 24 |
23 namespace blink { | 25 namespace blink { |
24 | 26 |
| 27 namespace { |
| 28 |
| 29 bool isTerminating(ScriptState* scriptState) |
| 30 { |
| 31 ExecutionContext* executionContext = scriptState->getExecutionContext(); |
| 32 if (!executionContext) |
| 33 return true; |
| 34 if (!executionContext->isWorkerGlobalScope()) |
| 35 return false; |
| 36 return toWorkerGlobalScope(executionContext)->scriptController()->isExecutio
nTerminating(); |
| 37 } |
| 38 |
| 39 } // namespace |
| 40 |
25 class BodyStreamBuffer::LoaderClient final : public GarbageCollectedFinalized<Lo
aderClient>, public ActiveDOMObject, public FetchDataLoader::Client { | 41 class BodyStreamBuffer::LoaderClient final : public GarbageCollectedFinalized<Lo
aderClient>, public ActiveDOMObject, public FetchDataLoader::Client { |
26 WTF_MAKE_NONCOPYABLE(LoaderClient); | 42 WTF_MAKE_NONCOPYABLE(LoaderClient); |
27 USING_GARBAGE_COLLECTED_MIXIN(LoaderClient); | 43 USING_GARBAGE_COLLECTED_MIXIN(LoaderClient); |
28 public: | 44 public: |
29 LoaderClient(ExecutionContext* executionContext, BodyStreamBuffer* buffer, F
etchDataLoader::Client* client) | 45 LoaderClient(ExecutionContext* executionContext, BodyStreamBuffer* buffer, F
etchDataLoader::Client* client) |
30 : ActiveDOMObject(executionContext) | 46 : ActiveDOMObject(executionContext) |
31 , m_buffer(buffer) | 47 , m_buffer(buffer) |
32 , m_client(client) | 48 , m_client(client) |
33 { | 49 { |
34 suspendIfNeeded(); | 50 suspendIfNeeded(); |
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
82 Member<FetchDataLoader::Client> m_client; | 98 Member<FetchDataLoader::Client> m_client; |
83 }; | 99 }; |
84 | 100 |
85 BodyStreamBuffer::BodyStreamBuffer(ScriptState* scriptState, std::unique_ptr<Fet
chDataConsumerHandle> handle) | 101 BodyStreamBuffer::BodyStreamBuffer(ScriptState* scriptState, std::unique_ptr<Fet
chDataConsumerHandle> handle) |
86 : UnderlyingSourceBase(scriptState) | 102 : UnderlyingSourceBase(scriptState) |
87 , m_scriptState(scriptState) | 103 , m_scriptState(scriptState) |
88 , m_handle(std::move(handle)) | 104 , m_handle(std::move(handle)) |
89 , m_reader(m_handle->obtainFetchDataReader(this)) | 105 , m_reader(m_handle->obtainFetchDataReader(this)) |
90 , m_madeFromReadableStream(false) | 106 , m_madeFromReadableStream(false) |
91 { | 107 { |
| 108 if (isTerminating(scriptState)) { |
| 109 m_reader = nullptr; |
| 110 m_handle = nullptr; |
| 111 return; |
| 112 } |
92 v8::Local<v8::Value> bodyValue = toV8(this, scriptState); | 113 v8::Local<v8::Value> bodyValue = toV8(this, scriptState); |
| 114 if (bodyValue.IsEmpty()) { |
| 115 DCHECK(isTerminating(scriptState)); |
| 116 m_reader = nullptr; |
| 117 m_handle = nullptr; |
| 118 return; |
| 119 } |
93 DCHECK(bodyValue->IsObject()); | 120 DCHECK(bodyValue->IsObject()); |
94 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); | 121 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); |
95 | 122 |
96 ScriptValue readableStream = ReadableStreamOperations::createReadableStream( | 123 ScriptValue readableStream = ReadableStreamOperations::createReadableStream( |
97 scriptState, this, ReadableStreamOperations::createCountQueuingStrategy(
scriptState, 0)); | 124 scriptState, this, ReadableStreamOperations::createCountQueuingStrategy(
scriptState, 0)); |
| 125 if (isTerminating(scriptState)) { |
| 126 m_reader = nullptr; |
| 127 m_handle = nullptr; |
| 128 return; |
| 129 } |
98 V8HiddenValue::setHiddenValue(scriptState, body, V8HiddenValue::internalBody
Stream(scriptState->isolate()), readableStream.v8Value()); | 130 V8HiddenValue::setHiddenValue(scriptState, body, V8HiddenValue::internalBody
Stream(scriptState->isolate()), readableStream.v8Value()); |
99 } | 131 } |
100 | 132 |
101 BodyStreamBuffer::BodyStreamBuffer(ScriptState* scriptState, ScriptValue stream) | 133 BodyStreamBuffer::BodyStreamBuffer(ScriptState* scriptState, ScriptValue stream) |
102 : UnderlyingSourceBase(scriptState) | 134 : UnderlyingSourceBase(scriptState) |
103 , m_scriptState(scriptState) | 135 , m_scriptState(scriptState) |
104 , m_madeFromReadableStream(true) | 136 , m_madeFromReadableStream(true) |
105 { | 137 { |
106 DCHECK(ReadableStreamOperations::isReadableStream(scriptState, stream)); | 138 DCHECK(ReadableStreamOperations::isReadableStream(scriptState, stream)); |
| 139 if (isTerminating(scriptState)) |
| 140 return; |
107 v8::Local<v8::Value> bodyValue = toV8(this, scriptState); | 141 v8::Local<v8::Value> bodyValue = toV8(this, scriptState); |
| 142 if (bodyValue.IsEmpty()) { |
| 143 DCHECK(isTerminating(scriptState)); |
| 144 return; |
| 145 } |
108 DCHECK(bodyValue->IsObject()); | 146 DCHECK(bodyValue->IsObject()); |
109 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); | 147 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); |
110 | 148 |
111 V8HiddenValue::setHiddenValue(scriptState, body, V8HiddenValue::internalBody
Stream(scriptState->isolate()), stream.v8Value()); | 149 V8HiddenValue::setHiddenValue(scriptState, body, V8HiddenValue::internalBody
Stream(scriptState->isolate()), stream.v8Value()); |
112 } | 150 } |
113 | 151 |
114 ScriptValue BodyStreamBuffer::stream() | 152 ScriptValue BodyStreamBuffer::stream() |
115 { | 153 { |
116 ScriptState::Scope scope(m_scriptState.get()); | 154 ScriptState::Scope scope(m_scriptState.get()); |
| 155 if (isTerminating(m_scriptState.get())) |
| 156 return ScriptValue(); |
117 v8::Local<v8::Value> bodyValue = toV8(this, m_scriptState.get()); | 157 v8::Local<v8::Value> bodyValue = toV8(this, m_scriptState.get()); |
| 158 if (bodyValue.IsEmpty()) { |
| 159 DCHECK(isTerminating(m_scriptState.get())); |
| 160 return ScriptValue(); |
| 161 } |
118 DCHECK(bodyValue->IsObject()); | 162 DCHECK(bodyValue->IsObject()); |
119 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); | 163 v8::Local<v8::Object> body = bodyValue.As<v8::Object>(); |
120 return ScriptValue(m_scriptState.get(), V8HiddenValue::getHiddenValue(m_scri
ptState.get(), body, V8HiddenValue::internalBodyStream(m_scriptState->isolate())
)); | 164 return ScriptValue(m_scriptState.get(), V8HiddenValue::getHiddenValue(m_scri
ptState.get(), body, V8HiddenValue::internalBodyStream(m_scriptState->isolate())
)); |
121 } | 165 } |
122 | 166 |
123 PassRefPtr<BlobDataHandle> BodyStreamBuffer::drainAsBlobDataHandle(FetchDataCons
umerHandle::Reader::BlobSizePolicy policy) | 167 PassRefPtr<BlobDataHandle> BodyStreamBuffer::drainAsBlobDataHandle(FetchDataCons
umerHandle::Reader::BlobSizePolicy policy) |
124 { | 168 { |
125 ASSERT(!isStreamLocked()); | 169 ASSERT(!isStreamLocked()); |
126 ASSERT(!isStreamDisturbed()); | 170 ASSERT(!isStreamDisturbed()); |
127 if (isStreamClosed() || isStreamErrored()) | 171 if (isStreamClosed() || isStreamErrored()) |
(...skipping 248 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
376 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); | 420 return createFetchDataConsumerHandleFromWebHandle(createDoneDataConsumer
Handle()); |
377 } | 421 } |
378 if (isErrored) | 422 if (isErrored) |
379 return createFetchDataConsumerHandleFromWebHandle(createUnexpectedErrorD
ataConsumerHandle()); | 423 return createFetchDataConsumerHandleFromWebHandle(createUnexpectedErrorD
ataConsumerHandle()); |
380 | 424 |
381 DCHECK(handle); | 425 DCHECK(handle); |
382 return handle; | 426 return handle; |
383 } | 427 } |
384 | 428 |
385 } // namespace blink | 429 } // namespace blink |
OLD | NEW |