Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(382)

Side by Side Diff: third_party/WebKit/Source/modules/fetch/Response.cpp

Issue 1506023003: Response construction with a ReadableStream (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 "config.h" 5 #include "config.h"
6 #include "modules/fetch/Response.h" 6 #include "modules/fetch/Response.h"
7 7
8 #include "bindings/core/v8/Dictionary.h" 8 #include "bindings/core/v8/Dictionary.h"
9 #include "bindings/core/v8/ExceptionState.h" 9 #include "bindings/core/v8/ExceptionState.h"
10 #include "bindings/core/v8/ReadableStreamOperations.h"
11 #include "bindings/core/v8/ScriptState.h"
12 #include "bindings/core/v8/V8ArrayBuffer.h"
13 #include "bindings/core/v8/V8ArrayBufferView.h"
14 #include "bindings/core/v8/V8Binding.h"
15 #include "bindings/core/v8/V8Blob.h"
16 #include "bindings/core/v8/V8FormData.h"
10 #include "core/dom/DOMArrayBuffer.h" 17 #include "core/dom/DOMArrayBuffer.h"
11 #include "core/dom/DOMArrayBufferView.h" 18 #include "core/dom/DOMArrayBufferView.h"
12 #include "core/fileapi/Blob.h" 19 #include "core/fileapi/Blob.h"
13 #include "core/html/FormData.h" 20 #include "core/html/FormData.h"
14 #include "modules/fetch/BodyStreamBuffer.h" 21 #include "modules/fetch/BodyStreamBuffer.h"
15 #include "modules/fetch/FetchBlobDataConsumerHandle.h" 22 #include "modules/fetch/FetchBlobDataConsumerHandle.h"
16 #include "modules/fetch/FetchFormDataConsumerHandle.h" 23 #include "modules/fetch/FetchFormDataConsumerHandle.h"
24 #include "modules/fetch/ReadableStreamDataConsumerHandle.h"
17 #include "modules/fetch/ResponseInit.h" 25 #include "modules/fetch/ResponseInit.h"
26 #include "platform/RuntimeEnabledFeatures.h"
18 #include "platform/network/EncodedFormData.h" 27 #include "platform/network/EncodedFormData.h"
19 #include "platform/network/HTTPHeaderMap.h" 28 #include "platform/network/HTTPHeaderMap.h"
20 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h" 29 #include "public/platform/modules/serviceworker/WebServiceWorkerResponse.h"
21 #include "wtf/RefPtr.h" 30 #include "wtf/RefPtr.h"
22 31
23 namespace blink { 32 namespace blink {
24 33
25 namespace { 34 namespace {
26 35
27 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec utionContext, const WebServiceWorkerResponse& webResponse) 36 FetchResponseData* createFetchResponseDataFromWebResponse(ExecutionContext* exec utionContext, const WebServiceWorkerResponse& webResponse)
(...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after
88 if (!(c == 0x09 // HTAB 97 if (!(c == 0x09 // HTAB
89 || (0x20 <= c && c <= 0x7E) // SP / VCHAR 98 || (0x20 <= c && c <= 0x7E) // SP / VCHAR
90 || (0x80 <= c && c <= 0xFF))) // obs-text 99 || (0x80 <= c && c <= 0xFF))) // obs-text
91 return false; 100 return false;
92 } 101 }
93 return true; 102 return true;
94 } 103 }
95 104
96 } 105 }
97 106
98 Response* Response::create(ExecutionContext* context, ExceptionState& exceptionS tate) 107 Response* Response::create(ScriptState* scriptState, ExceptionState& exceptionSt ate)
99 { 108 {
100 return create(context, nullptr, String(), ResponseInit(), exceptionState); 109 return create(scriptState->executionContext(), nullptr, String(), ResponseIn it(), exceptionState);
101 } 110 }
102 111
103 Response* Response::create(ExecutionContext* context, const BodyInit& body, cons t Dictionary& init, ExceptionState& exceptionState) 112 Response* Response::create(ScriptState* scriptState, ScriptValue bodyValue, cons t Dictionary& init, ExceptionState& exceptionState)
104 { 113 {
105 ASSERT(!body.isNull()); 114 v8::Local<v8::Value> body = bodyValue.v8Value();
115 v8::Isolate* isolate = scriptState->isolate();
116 ExecutionContext* executionContext = scriptState->executionContext();
117
106 OwnPtr<FetchDataConsumerHandle> bodyHandle; 118 OwnPtr<FetchDataConsumerHandle> bodyHandle;
107 String contentType; 119 String contentType;
108 if (body.isBlob()) { 120 if (V8Blob::hasInstance(body, isolate)) {
109 bodyHandle = FetchBlobDataConsumerHandle::create(context, body.getAsBlob ()->blobDataHandle()); 121 Blob* blob = V8Blob::toImpl(body.As<v8::Object>());
110 contentType = body.getAsBlob()->type(); 122 bodyHandle = FetchBlobDataConsumerHandle::create(executionContext, blob- >blobDataHandle());
111 } else if (body.isUSVString()) { 123 contentType = blob->type();
112 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsUSVString()); 124 } else if (V8ArrayBuffer::hasInstance(body, isolate)) {
113 contentType = "text/plain;charset=UTF-8"; 125 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBuffer::toImpl(b ody.As<v8::Object>()));
114 } else if (body.isArrayBuffer()) { 126 } else if (V8ArrayBufferView::hasInstance(body, isolate)) {
115 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsArrayBuffer() ); 127 bodyHandle = FetchFormDataConsumerHandle::create(V8ArrayBufferView::toIm pl(body.As<v8::Object>()));
116 } else if (body.isArrayBufferView()) { 128 } else if (V8FormData::hasInstance(body, isolate)) {
117 bodyHandle = FetchFormDataConsumerHandle::create(body.getAsArrayBufferVi ew()); 129 RefPtr<EncodedFormData> formData = V8FormData::toImpl(body.As<v8::Object >())->encodeMultiPartFormData();
118 } else if (body.isFormData()) {
119 RefPtr<EncodedFormData> formData = body.getAsFormData()->encodeMultiPart FormData();
120 // Here we handle formData->boundary() as a C-style string. See 130 // Here we handle formData->boundary() as a C-style string. See
121 // FormDataEncoder::generateUniqueBoundaryString. 131 // FormDataEncoder::generateUniqueBoundaryString.
122 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin g::ConstructFromLiteral) + formData->boundary().data(); 132 contentType = AtomicString("multipart/form-data; boundary=", AtomicStrin g::ConstructFromLiteral) + formData->boundary().data();
123 bodyHandle = FetchFormDataConsumerHandle::create(context, formData.relea se()); 133 bodyHandle = FetchFormDataConsumerHandle::create(executionContext, formD ata.release());
134 } else if (RuntimeEnabledFeatures::responseConstructedWithReadableStreamEnab led() && ReadableStreamOperations::isReadableStream(scriptState, body)) {
135 bodyHandle = ReadableStreamDataConsumerHandle::create(scriptState, body) ;
124 } else { 136 } else {
125 ASSERT_NOT_REACHED(); 137 String string = toUSVString(isolate, body, exceptionState);
126 return nullptr; 138 if (exceptionState.hadException())
139 return nullptr;
140 bodyHandle = FetchFormDataConsumerHandle::create(string);
141 contentType = "text/plain;charset=UTF-8";
127 } 142 }
128 return create(context, bodyHandle.release(), contentType, ResponseInit(init, exceptionState), exceptionState); 143 // TODO(yhirano): Add the URLSearchParams case.
144 return create(executionContext, bodyHandle.release(), contentType, ResponseI nit(init, exceptionState), exceptionState);
129 } 145 }
130 146
131 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep tionState& exceptionState) 147 Response* Response::create(ExecutionContext* context, PassOwnPtr<FetchDataConsum erHandle> bodyHandle, const String& contentType, const ResponseInit& init, Excep tionState& exceptionState)
132 { 148 {
133 unsigned short status = init.status; 149 unsigned short status = init.status;
134 150
135 // "1. If |init|'s status member is not in the range 200 to 599, inclusive, throw a 151 // "1. If |init|'s status member is not in the range 200 to 599, inclusive, throw a
136 // RangeError." 152 // RangeError."
137 if (status < 200 || 599 < status) { 153 if (status < 200 || 599 < status) {
138 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe ssages::InclusiveBound)); 154 exceptionState.throwRangeError(ExceptionMessages::indexOutsideRange<unsi gned>("status", status, 200, ExceptionMessages::InclusiveBound, 599, ExceptionMe ssages::InclusiveBound));
(...skipping 181 matching lines...) Expand 10 before | Expand all | Expand 10 after
320 { 336 {
321 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped()) 337 if (!executionContext() || executionContext()->activeDOMObjectsAreStopped())
322 return false; 338 return false;
323 if (!internalBodyBuffer()) 339 if (!internalBodyBuffer())
324 return false; 340 return false;
325 if (internalBodyBuffer()->hasPendingActivity()) 341 if (internalBodyBuffer()->hasPendingActivity())
326 return true; 342 return true;
327 return Body::hasPendingActivity(); 343 return Body::hasPendingActivity();
328 } 344 }
329 345
346 void Response::stop()
347 {
348 if (m_response->internalBuffer())
349 m_response->internalBuffer()->stop();
350 }
351
330 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon se) 352 void Response::populateWebServiceWorkerResponse(WebServiceWorkerResponse& respon se)
331 { 353 {
332 m_response->populateWebServiceWorkerResponse(response); 354 m_response->populateWebServiceWorkerResponse(response);
333 } 355 }
334 356
335 Response::Response(ExecutionContext* context) 357 Response::Response(ExecutionContext* context)
336 : Body(context) 358 : Body(context)
337 , m_response(FetchResponseData::create()) 359 , m_response(FetchResponseData::create())
338 , m_headers(Headers::create(m_response->headerList())) 360 , m_headers(Headers::create(m_response->headerList()))
339 { 361 {
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
372 } 394 }
373 395
374 DEFINE_TRACE(Response) 396 DEFINE_TRACE(Response)
375 { 397 {
376 Body::trace(visitor); 398 Body::trace(visitor);
377 visitor->trace(m_response); 399 visitor->trace(m_response);
378 visitor->trace(m_headers); 400 visitor->trace(m_headers);
379 } 401 }
380 402
381 } // namespace blink 403 } // namespace blink
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/modules/fetch/Response.h ('k') | third_party/WebKit/Source/modules/fetch/Response.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698