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

Side by Side Diff: Source/core/xml/XMLHttpRequest.cpp

Issue 455303002: Add 'stream' to XMLHttpRequest response type. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@stream-promise-property-reset
Patch Set: Created 6 years, 4 months 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 /* 1 /*
2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved. 2 * Copyright (C) 2004, 2006, 2008 Apple Inc. All rights reserved.
3 * Copyright (C) 2005-2007 Alexey Proskuryakov <ap@webkit.org> 3 * Copyright (C) 2005-2007 Alexey Proskuryakov <ap@webkit.org>
4 * Copyright (C) 2007, 2008 Julien Chaffraix <jchaffraix@webkit.org> 4 * Copyright (C) 2007, 2008 Julien Chaffraix <jchaffraix@webkit.org>
5 * Copyright (C) 2008, 2011 Google Inc. All rights reserved. 5 * Copyright (C) 2008, 2011 Google Inc. All rights reserved.
6 * Copyright (C) 2012 Intel Corporation 6 * Copyright (C) 2012 Intel Corporation
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public 9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
11 * version 2 of the License, or (at your option) any later version. 11 * version 2 of the License, or (at your option) any later version.
12 * 12 *
13 * This library is distributed in the hope that it will be useful, 13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details. 16 * Lesser General Public License for more details.
17 * 17 *
18 * You should have received a copy of the GNU Lesser General Public 18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software 19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 U SA 20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 U SA
21 */ 21 */
22 22
23 #include "config.h" 23 #include "config.h"
24 #include "core/xml/XMLHttpRequest.h" 24 #include "core/xml/XMLHttpRequest.h"
25 25
26 #include "bindings/core/v8/ExceptionState.h" 26 #include "bindings/core/v8/ExceptionState.h"
27 #include "core/FetchInitiatorTypeNames.h" 27 #include "core/FetchInitiatorTypeNames.h"
28 #include "core/dom/ContextFeatures.h" 28 #include "core/dom/ContextFeatures.h"
29 #include "core/dom/DOMException.h"
29 #include "core/dom/DOMImplementation.h" 30 #include "core/dom/DOMImplementation.h"
30 #include "core/dom/ExceptionCode.h" 31 #include "core/dom/ExceptionCode.h"
31 #include "core/dom/XMLDocument.h" 32 #include "core/dom/XMLDocument.h"
32 #include "core/editing/markup.h" 33 #include "core/editing/markup.h"
33 #include "core/events/Event.h" 34 #include "core/events/Event.h"
34 #include "core/fetch/FetchUtils.h" 35 #include "core/fetch/FetchUtils.h"
35 #include "core/fileapi/Blob.h" 36 #include "core/fileapi/Blob.h"
36 #include "core/fileapi/File.h" 37 #include "core/fileapi/File.h"
37 #include "core/frame/Settings.h" 38 #include "core/frame/Settings.h"
38 #include "core/frame/UseCounter.h" 39 #include "core/frame/UseCounter.h"
39 #include "core/frame/csp/ContentSecurityPolicy.h" 40 #include "core/frame/csp/ContentSecurityPolicy.h"
40 #include "core/html/DOMFormData.h" 41 #include "core/html/DOMFormData.h"
41 #include "core/html/HTMLDocument.h" 42 #include "core/html/HTMLDocument.h"
42 #include "core/html/parser/TextResourceDecoder.h" 43 #include "core/html/parser/TextResourceDecoder.h"
43 #include "core/inspector/InspectorInstrumentation.h" 44 #include "core/inspector/InspectorInstrumentation.h"
44 #include "core/inspector/InspectorTraceEvents.h" 45 #include "core/inspector/InspectorTraceEvents.h"
45 #include "core/loader/ThreadableLoader.h" 46 #include "core/loader/ThreadableLoader.h"
47 #include "core/streams/ReadableStream.h"
48 #include "core/streams/ReadableStreamImpl.h"
46 #include "core/streams/Stream.h" 49 #include "core/streams/Stream.h"
50 #include "core/streams/UnderlyingSource.h"
47 #include "core/xml/XMLHttpRequestProgressEvent.h" 51 #include "core/xml/XMLHttpRequestProgressEvent.h"
48 #include "core/xml/XMLHttpRequestUpload.h" 52 #include "core/xml/XMLHttpRequestUpload.h"
49 #include "platform/Logging.h" 53 #include "platform/Logging.h"
50 #include "platform/RuntimeEnabledFeatures.h" 54 #include "platform/RuntimeEnabledFeatures.h"
51 #include "platform/SharedBuffer.h" 55 #include "platform/SharedBuffer.h"
52 #include "platform/blob/BlobData.h" 56 #include "platform/blob/BlobData.h"
53 #include "platform/network/HTTPParsers.h" 57 #include "platform/network/HTTPParsers.h"
54 #include "platform/network/ParsedContentType.h" 58 #include "platform/network/ParsedContentType.h"
55 #include "platform/network/ResourceError.h" 59 #include "platform/network/ResourceError.h"
56 #include "platform/network/ResourceRequest.h" 60 #include "platform/network/ResourceRequest.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
103 107
104 static void logConsoleError(ExecutionContext* context, const String& message) 108 static void logConsoleError(ExecutionContext* context, const String& message)
105 { 109 {
106 if (!context) 110 if (!context)
107 return; 111 return;
108 // FIXME: It's not good to report the bad usage without indicating what sour ce line it came from. 112 // FIXME: It's not good to report the bad usage without indicating what sour ce line it came from.
109 // We should pass additional parameters so we can tell the console where the mistake occurred. 113 // We should pass additional parameters so we can tell the console where the mistake occurred.
110 context->addConsoleMessage(JSMessageSource, ErrorMessageLevel, message); 114 context->addConsoleMessage(JSMessageSource, ErrorMessageLevel, message);
111 } 115 }
112 116
117 namespace {
118
119 class ReadableStreamSource : public GarbageCollectedFinalized<ReadableStreamSour ce>, public UnderlyingSource {
120 USING_GARBAGE_COLLECTED_MIXIN(ReadableStreamSource);
121 public:
122 ReadableStreamSource(XMLHttpRequest* owner) : m_owner(owner) { }
123 virtual ~ReadableStreamSource() { }
124 virtual void pullSource() OVERRIDE { }
125 virtual ScriptPromise cancelSource(ScriptState* scriptState, ScriptValue rea son) OVERRIDE
126 {
127 m_owner->abort();
128 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te()));
129 }
130 virtual void trace(Visitor* visitor) OVERRIDE
131 {
132 UnderlyingSource::trace(visitor);
133 visitor->trace(m_owner);
134 }
135
136 private:
137 RawPtrWillBeMember<XMLHttpRequest> m_owner;
138 };
139
140 } // namespace
141
113 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin) 142 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin)
114 { 143 {
115 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin)); 144 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin));
116 xmlHttpRequest->suspendIfNeeded(); 145 xmlHttpRequest->suspendIfNeeded();
117 146
118 return xmlHttpRequest.release(); 147 return xmlHttpRequest.release();
119 } 148 }
120 149
121 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin) 150 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin)
122 : ActiveDOMObject(context) 151 : ActiveDOMObject(context)
(...skipping 161 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 } 313 }
285 m_binaryResponseBuilder.clear(); 314 m_binaryResponseBuilder.clear();
286 } else { 315 } else {
287 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 ); 316 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 );
288 } 317 }
289 } 318 }
290 319
291 return m_responseArrayBuffer.get(); 320 return m_responseArrayBuffer.get();
292 } 321 }
293 322
294 Stream* XMLHttpRequest::responseStream() 323 Stream* XMLHttpRequest::responseLegacyStream()
295 { 324 {
296 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream); 325 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream);
297 326
298 if (m_error || (m_state != LOADING && m_state != DONE)) 327 if (m_error || (m_state != LOADING && m_state != DONE))
299 return 0; 328 return 0;
300 329
301 return m_responseStream.get(); 330 return m_responseLegacyStream.get();
331 }
332
333 ReadableStream* XMLHttpRequest::responseStream()
334 {
335 ASSERT(m_responseTypeCode == ResponseTypeStream);
336 if (m_error || (m_state != LOADING && m_state != DONE))
337 return nullptr;
tyoshino (SeeGerritForStatus) 2014/08/14 00:38:56 i heard that we're still using 0 for a raw pointer
yhirano 2014/08/14 06:48:53 Done.
338
339 return m_responseStream;
302 } 340 }
303 341
304 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State) 342 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State)
305 { 343 {
306 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156 344 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156
307 // XHR2 spec, 4.7.3. "This implies that the timeout attribute can be set whi le fetching is in progress. If that occurs it will still be measured relative to the start of fetching." 345 // XHR2 spec, 4.7.3. "This implies that the timeout attribute can be set whi le fetching is in progress. If that occurs it will still be measured relative to the start of fetching."
308 if (executionContext()->isDocument() && !m_async) { 346 if (executionContext()->isDocument() && !m_async) {
309 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document."); 347 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document.");
310 return; 348 return;
311 } 349 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
345 m_responseTypeCode = ResponseTypeDocument; 383 m_responseTypeCode = ResponseTypeDocument;
346 } else if (responseType == "blob") { 384 } else if (responseType == "blob") {
347 m_responseTypeCode = ResponseTypeBlob; 385 m_responseTypeCode = ResponseTypeBlob;
348 } else if (responseType == "arraybuffer") { 386 } else if (responseType == "arraybuffer") {
349 m_responseTypeCode = ResponseTypeArrayBuffer; 387 m_responseTypeCode = ResponseTypeArrayBuffer;
350 } else if (responseType == "legacystream") { 388 } else if (responseType == "legacystream") {
351 if (RuntimeEnabledFeatures::streamEnabled()) 389 if (RuntimeEnabledFeatures::streamEnabled())
352 m_responseTypeCode = ResponseTypeLegacyStream; 390 m_responseTypeCode = ResponseTypeLegacyStream;
353 else 391 else
354 return; 392 return;
393 } else if (responseType == "stream") {
394 if (RuntimeEnabledFeatures::streamEnabled())
395 m_responseTypeCode = ResponseTypeStream;
396 else
397 return;
355 } else { 398 } else {
356 ASSERT_NOT_REACHED(); 399 ASSERT_NOT_REACHED();
357 } 400 }
358 } 401 }
359 402
360 String XMLHttpRequest::responseType() 403 String XMLHttpRequest::responseType()
361 { 404 {
362 switch (m_responseTypeCode) { 405 switch (m_responseTypeCode) {
363 case ResponseTypeDefault: 406 case ResponseTypeDefault:
364 return ""; 407 return "";
365 case ResponseTypeText: 408 case ResponseTypeText:
366 return "text"; 409 return "text";
367 case ResponseTypeJSON: 410 case ResponseTypeJSON:
368 return "json"; 411 return "json";
369 case ResponseTypeDocument: 412 case ResponseTypeDocument:
370 return "document"; 413 return "document";
371 case ResponseTypeBlob: 414 case ResponseTypeBlob:
372 return "blob"; 415 return "blob";
373 case ResponseTypeArrayBuffer: 416 case ResponseTypeArrayBuffer:
374 return "arraybuffer"; 417 return "arraybuffer";
375 case ResponseTypeLegacyStream: 418 case ResponseTypeLegacyStream:
376 return "legacystream"; 419 return "legacystream";
420 case ResponseTypeStream:
421 return "stream";
377 } 422 }
378 return ""; 423 return "";
379 } 424 }
380 425
381 String XMLHttpRequest::responseURL() 426 String XMLHttpRequest::responseURL()
382 { 427 {
383 return m_response.url().string(); 428 return m_response.url().string();
384 } 429 }
385 430
386 XMLHttpRequestUpload* XMLHttpRequest::upload() 431 XMLHttpRequestUpload* XMLHttpRequest::upload()
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
882 } 927 }
883 928
884 bool XMLHttpRequest::internalAbort() 929 bool XMLHttpRequest::internalAbort()
885 { 930 {
886 m_error = true; 931 m_error = true;
887 932
888 clearVariablesForLoading(); 933 clearVariablesForLoading();
889 934
890 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this); 935 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this);
891 936
892 if (m_responseStream && m_state != DONE) 937 if (m_responseLegacyStream && m_state != DONE)
893 m_responseStream->abort(); 938 m_responseLegacyStream->abort();
939
940 if (m_responseStream) {
941 // When the stream is already closed (including canceled from the
942 // user), |error| does nothing.
943 // FIXME: Create a more specific error.
tyoshino (SeeGerritForStatus) 2014/08/14 00:38:56 yes. internalAbort is used for non-abort() cases,
yhirano 2014/08/14 06:48:53 Done.
944 m_responseStream->error(DOMException::create(AbortError, "XMLHttpRequest ::abort"));
945 }
894 946
895 if (!m_loader) 947 if (!m_loader)
896 return true; 948 return true;
897 949
898 // Cancelling the ThreadableLoader m_loader may result in calling 950 // Cancelling the ThreadableLoader m_loader may result in calling
899 // window.onload synchronously. If such an onload handler contains open() 951 // window.onload synchronously. If such an onload handler contains open()
900 // call on the same XMLHttpRequest object, reentry happens. 952 // call on the same XMLHttpRequest object, reentry happens.
901 // 953 //
902 // If, window.onload contains open() and send(), m_loader will be set to 954 // If, window.onload contains open() and send(), m_loader will be set to
903 // non 0 value. So, we cannot continue the outer open(). In such case, 955 // non 0 value. So, we cannot continue the outer open(). In such case,
(...skipping 21 matching lines...) Expand all
925 m_response = ResourceResponse(); 977 m_response = ResourceResponse();
926 978
927 m_responseText.clear(); 979 m_responseText.clear();
928 980
929 m_parsedResponse = false; 981 m_parsedResponse = false;
930 m_responseDocument = nullptr; 982 m_responseDocument = nullptr;
931 983
932 m_responseBlob = nullptr; 984 m_responseBlob = nullptr;
933 m_downloadedBlobLength = 0; 985 m_downloadedBlobLength = 0;
934 986
987 m_responseLegacyStream = nullptr;
935 m_responseStream = nullptr; 988 m_responseStream = nullptr;
936 989
937 // These variables may referred by the response accessors. So, we can clear 990 // These variables may referred by the response accessors. So, we can clear
938 // this only when we clear the response holder variables above. 991 // this only when we clear the response holder variables above.
939 m_binaryResponseBuilder.clear(); 992 m_binaryResponseBuilder.clear();
940 m_responseArrayBuffer.clear(); 993 m_responseArrayBuffer.clear();
941 } 994 }
942 995
943 void XMLHttpRequest::clearRequest() 996 void XMLHttpRequest::clearRequest()
944 { 997 {
(...skipping 260 matching lines...) Expand 10 before | Expand all | Expand 10 after
1205 1258
1206 if (m_error) 1259 if (m_error)
1207 return; 1260 return;
1208 1261
1209 if (m_state < HEADERS_RECEIVED) 1262 if (m_state < HEADERS_RECEIVED)
1210 changeState(HEADERS_RECEIVED); 1263 changeState(HEADERS_RECEIVED);
1211 1264
1212 if (m_decoder) 1265 if (m_decoder)
1213 m_responseText = m_responseText.concatenateWith(m_decoder->flush()); 1266 m_responseText = m_responseText.concatenateWith(m_decoder->flush());
1214 1267
1268 if (m_responseLegacyStream)
1269 m_responseLegacyStream->finalize();
1270
1215 if (m_responseStream) 1271 if (m_responseStream)
1216 m_responseStream->finalize(); 1272 m_responseStream->close();
1217 1273
1218 clearVariablesForLoading(); 1274 clearVariablesForLoading();
1219 1275
1220 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er); 1276 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er);
1221 1277
1222 if (m_loader) 1278 if (m_loader)
1223 m_loader = nullptr; 1279 m_loader = nullptr;
1224 1280
1225 changeState(DONE); 1281 changeState(DONE);
1226 } 1282 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1302 if (!m_decoder) 1358 if (!m_decoder)
1303 m_decoder = createDecoder(); 1359 m_decoder = createDecoder();
1304 1360
1305 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len)); 1361 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len));
1306 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) { 1362 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) {
1307 // Buffer binary data. 1363 // Buffer binary data.
1308 if (!m_binaryResponseBuilder) 1364 if (!m_binaryResponseBuilder)
1309 m_binaryResponseBuilder = SharedBuffer::create(); 1365 m_binaryResponseBuilder = SharedBuffer::create();
1310 m_binaryResponseBuilder->append(data, len); 1366 m_binaryResponseBuilder->append(data, len);
1311 } else if (m_responseTypeCode == ResponseTypeLegacyStream) { 1367 } else if (m_responseTypeCode == ResponseTypeLegacyStream) {
1312 if (!m_responseStream) 1368 if (!m_responseLegacyStream)
1313 m_responseStream = Stream::create(executionContext(), responseMIMETy pe()); 1369 m_responseLegacyStream = Stream::create(executionContext(), response MIMEType());
1314 m_responseStream->addData(data, len); 1370 m_responseLegacyStream->addData(data, len);
1371 } else if (m_responseTypeCode == ResponseTypeStream) {
1372 if (!m_responseStream) {
1373 m_responseStream = new ReadableStreamImpl<ReadableStreamChunkTypeTra its<ArrayBuffer> >(executionContext(), new ReadableStreamSource(this));
1374 m_responseStream->didSourceStart();
1375 }
1376 m_responseStream->enqueue(ArrayBuffer::create(data, len));
1315 } 1377 }
1316 1378
1317 if (m_error) 1379 if (m_error)
1318 return; 1380 return;
1319 1381
1320 trackProgress(len); 1382 trackProgress(len);
1321 } 1383 }
1322 1384
1323 void XMLHttpRequest::didDownloadData(int dataLength) 1385 void XMLHttpRequest::didDownloadData(int dataLength)
1324 { 1386 {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1393 } 1455 }
1394 1456
1395 ExecutionContext* XMLHttpRequest::executionContext() const 1457 ExecutionContext* XMLHttpRequest::executionContext() const
1396 { 1458 {
1397 return ActiveDOMObject::executionContext(); 1459 return ActiveDOMObject::executionContext();
1398 } 1460 }
1399 1461
1400 void XMLHttpRequest::trace(Visitor* visitor) 1462 void XMLHttpRequest::trace(Visitor* visitor)
1401 { 1463 {
1402 visitor->trace(m_responseBlob); 1464 visitor->trace(m_responseBlob);
1465 visitor->trace(m_responseLegacyStream);
1403 visitor->trace(m_responseStream); 1466 visitor->trace(m_responseStream);
1467 visitor->trace(m_streamSource);
1404 visitor->trace(m_responseDocument); 1468 visitor->trace(m_responseDocument);
1405 visitor->trace(m_progressEventThrottle); 1469 visitor->trace(m_progressEventThrottle);
1406 visitor->trace(m_upload); 1470 visitor->trace(m_upload);
1407 XMLHttpRequestEventTarget::trace(visitor); 1471 XMLHttpRequestEventTarget::trace(visitor);
1408 } 1472 }
1409 1473
1410 } // namespace blink 1474 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698