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