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/Body.h" | 5 #include "modules/fetch/Body.h" |
6 | 6 |
7 #include <memory> | 7 #include <memory> |
8 #include "bindings/core/v8/ScriptPromiseResolver.h" | 8 #include "bindings/core/v8/ScriptPromiseResolver.h" |
9 #include "bindings/core/v8/ScriptState.h" | 9 #include "bindings/core/v8/ScriptState.h" |
10 #include "bindings/core/v8/V8ArrayBuffer.h" | 10 #include "bindings/core/v8/V8ArrayBuffer.h" |
11 #include "bindings/core/v8/V8ThrowException.h" | 11 #include "bindings/core/v8/V8ThrowException.h" |
12 #include "core/dom/DOMArrayBuffer.h" | 12 #include "core/dom/DOMArrayBuffer.h" |
13 #include "core/dom/DOMTypedArray.h" | 13 #include "core/dom/DOMTypedArray.h" |
14 #include "core/dom/ExecutionContext.h" | |
15 #include "core/fileapi/Blob.h" | 14 #include "core/fileapi/Blob.h" |
16 #include "modules/fetch/BodyStreamBuffer.h" | 15 #include "modules/fetch/BodyStreamBuffer.h" |
17 #include "modules/fetch/FetchDataLoader.h" | 16 #include "modules/fetch/FetchDataLoader.h" |
18 #include "platform/wtf/PassRefPtr.h" | 17 #include "platform/wtf/PassRefPtr.h" |
19 #include "platform/wtf/RefPtr.h" | 18 #include "platform/wtf/RefPtr.h" |
20 #include "public/platform/WebDataConsumerHandle.h" | 19 #include "public/platform/WebDataConsumerHandle.h" |
21 | 20 |
22 namespace blink { | 21 namespace blink { |
23 | 22 |
24 namespace { | 23 namespace { |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
113 ScriptPromise promise = RejectInvalidConsumption(script_state); | 112 ScriptPromise promise = RejectInvalidConsumption(script_state); |
114 if (!promise.IsEmpty()) | 113 if (!promise.IsEmpty()) |
115 return promise; | 114 return promise; |
116 | 115 |
117 // When the main thread sends a V8::TerminateExecution() signal to a worker | 116 // When the main thread sends a V8::TerminateExecution() signal to a worker |
118 // thread, any V8 API on the worker thread starts returning an empty | 117 // thread, any V8 API on the worker thread starts returning an empty |
119 // handle. This can happen in this function. To avoid the situation, we | 118 // handle. This can happen in this function. To avoid the situation, we |
120 // first check the ExecutionContext and return immediately if it's already | 119 // first check the ExecutionContext and return immediately if it's already |
121 // gone (which means that the V8::TerminateExecution() signal has been sent | 120 // gone (which means that the V8::TerminateExecution() signal has been sent |
122 // to this worker thread). | 121 // to this worker thread). |
123 if (!ExecutionContext::From(script_state)) | 122 if (!script_state->GetExecutionContext()) |
124 return ScriptPromise(); | 123 return ScriptPromise(); |
125 | 124 |
126 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 125 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
127 promise = resolver->Promise(); | 126 promise = resolver->Promise(); |
128 if (BodyBuffer()) { | 127 if (BodyBuffer()) { |
129 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsArrayBuffer(), | 128 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsArrayBuffer(), |
130 new BodyArrayBufferConsumer(resolver)); | 129 new BodyArrayBufferConsumer(resolver)); |
131 } else { | 130 } else { |
132 resolver->Resolve(DOMArrayBuffer::Create(0u, 1)); | 131 resolver->Resolve(DOMArrayBuffer::Create(0u, 1)); |
133 } | 132 } |
134 return promise; | 133 return promise; |
135 } | 134 } |
136 | 135 |
137 ScriptPromise Body::blob(ScriptState* script_state) { | 136 ScriptPromise Body::blob(ScriptState* script_state) { |
138 ScriptPromise promise = RejectInvalidConsumption(script_state); | 137 ScriptPromise promise = RejectInvalidConsumption(script_state); |
139 if (!promise.IsEmpty()) | 138 if (!promise.IsEmpty()) |
140 return promise; | 139 return promise; |
141 | 140 |
142 // See above comment. | 141 // See above comment. |
143 if (!ExecutionContext::From(script_state)) | 142 if (!script_state->GetExecutionContext()) |
144 return ScriptPromise(); | 143 return ScriptPromise(); |
145 | 144 |
146 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 145 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
147 promise = resolver->Promise(); | 146 promise = resolver->Promise(); |
148 if (BodyBuffer()) { | 147 if (BodyBuffer()) { |
149 BodyBuffer()->StartLoading( | 148 BodyBuffer()->StartLoading( |
150 FetchDataLoader::CreateLoaderAsBlobHandle(MimeType()), | 149 FetchDataLoader::CreateLoaderAsBlobHandle(MimeType()), |
151 new BodyBlobConsumer(resolver)); | 150 new BodyBlobConsumer(resolver)); |
152 } else { | 151 } else { |
153 std::unique_ptr<BlobData> blob_data = BlobData::Create(); | 152 std::unique_ptr<BlobData> blob_data = BlobData::Create(); |
154 blob_data->SetContentType(MimeType()); | 153 blob_data->SetContentType(MimeType()); |
155 resolver->Resolve( | 154 resolver->Resolve( |
156 Blob::Create(BlobDataHandle::Create(std::move(blob_data), 0))); | 155 Blob::Create(BlobDataHandle::Create(std::move(blob_data), 0))); |
157 } | 156 } |
158 return promise; | 157 return promise; |
159 } | 158 } |
160 | 159 |
161 ScriptPromise Body::json(ScriptState* script_state) { | 160 ScriptPromise Body::json(ScriptState* script_state) { |
162 ScriptPromise promise = RejectInvalidConsumption(script_state); | 161 ScriptPromise promise = RejectInvalidConsumption(script_state); |
163 if (!promise.IsEmpty()) | 162 if (!promise.IsEmpty()) |
164 return promise; | 163 return promise; |
165 | 164 |
166 // See above comment. | 165 // See above comment. |
167 if (!ExecutionContext::From(script_state)) | 166 if (!script_state->GetExecutionContext()) |
168 return ScriptPromise(); | 167 return ScriptPromise(); |
169 | 168 |
170 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 169 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
171 promise = resolver->Promise(); | 170 promise = resolver->Promise(); |
172 if (BodyBuffer()) { | 171 if (BodyBuffer()) { |
173 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(), | 172 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(), |
174 new BodyJsonConsumer(resolver)); | 173 new BodyJsonConsumer(resolver)); |
175 } else { | 174 } else { |
176 resolver->Reject(V8ThrowException::CreateSyntaxError( | 175 resolver->Reject(V8ThrowException::CreateSyntaxError( |
177 script_state->GetIsolate(), "Unexpected end of input")); | 176 script_state->GetIsolate(), "Unexpected end of input")); |
178 } | 177 } |
179 return promise; | 178 return promise; |
180 } | 179 } |
181 | 180 |
182 ScriptPromise Body::text(ScriptState* script_state) { | 181 ScriptPromise Body::text(ScriptState* script_state) { |
183 ScriptPromise promise = RejectInvalidConsumption(script_state); | 182 ScriptPromise promise = RejectInvalidConsumption(script_state); |
184 if (!promise.IsEmpty()) | 183 if (!promise.IsEmpty()) |
185 return promise; | 184 return promise; |
186 | 185 |
187 // See above comment. | 186 // See above comment. |
188 if (!ExecutionContext::From(script_state)) | 187 if (!script_state->GetExecutionContext()) |
189 return ScriptPromise(); | 188 return ScriptPromise(); |
190 | 189 |
191 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); | 190 ScriptPromiseResolver* resolver = ScriptPromiseResolver::Create(script_state); |
192 promise = resolver->Promise(); | 191 promise = resolver->Promise(); |
193 if (BodyBuffer()) { | 192 if (BodyBuffer()) { |
194 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(), | 193 BodyBuffer()->StartLoading(FetchDataLoader::CreateLoaderAsString(), |
195 new BodyTextConsumer(resolver)); | 194 new BodyTextConsumer(resolver)); |
196 } else { | 195 } else { |
197 resolver->Resolve(String()); | 196 resolver->Resolve(String()); |
198 } | 197 } |
(...skipping 28 matching lines...) Expand all Loading... |
227 | 226 |
228 ScriptPromise Body::RejectInvalidConsumption(ScriptState* script_state) { | 227 ScriptPromise Body::RejectInvalidConsumption(ScriptState* script_state) { |
229 if (IsBodyLocked() || bodyUsed()) | 228 if (IsBodyLocked() || bodyUsed()) |
230 return ScriptPromise::Reject( | 229 return ScriptPromise::Reject( |
231 script_state, V8ThrowException::CreateTypeError( | 230 script_state, V8ThrowException::CreateTypeError( |
232 script_state->GetIsolate(), "Already read")); | 231 script_state->GetIsolate(), "Already read")); |
233 return ScriptPromise(); | 232 return ScriptPromise(); |
234 } | 233 } |
235 | 234 |
236 } // namespace blink | 235 } // namespace blink |
OLD | NEW |