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

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
« no previous file with comments | « Source/core/xml/XMLHttpRequest.h ('k') | Source/core/xml/XMLHttpRequest.idl » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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/ConsoleMessage.h" 44 #include "core/inspector/ConsoleMessage.h"
44 #include "core/inspector/InspectorInstrumentation.h" 45 #include "core/inspector/InspectorInstrumentation.h"
45 #include "core/inspector/InspectorTraceEvents.h" 46 #include "core/inspector/InspectorTraceEvents.h"
46 #include "core/loader/ThreadableLoader.h" 47 #include "core/loader/ThreadableLoader.h"
48 #include "core/streams/ReadableStream.h"
49 #include "core/streams/ReadableStreamImpl.h"
47 #include "core/streams/Stream.h" 50 #include "core/streams/Stream.h"
51 #include "core/streams/UnderlyingSource.h"
48 #include "core/xml/XMLHttpRequestProgressEvent.h" 52 #include "core/xml/XMLHttpRequestProgressEvent.h"
49 #include "core/xml/XMLHttpRequestUpload.h" 53 #include "core/xml/XMLHttpRequestUpload.h"
50 #include "platform/Logging.h" 54 #include "platform/Logging.h"
51 #include "platform/RuntimeEnabledFeatures.h" 55 #include "platform/RuntimeEnabledFeatures.h"
52 #include "platform/SharedBuffer.h" 56 #include "platform/SharedBuffer.h"
53 #include "platform/blob/BlobData.h" 57 #include "platform/blob/BlobData.h"
54 #include "platform/network/HTTPParsers.h" 58 #include "platform/network/HTTPParsers.h"
55 #include "platform/network/ParsedContentType.h" 59 #include "platform/network/ParsedContentType.h"
56 #include "platform/network/ResourceError.h" 60 #include "platform/network/ResourceError.h"
57 #include "platform/network/ResourceRequest.h" 61 #include "platform/network/ResourceRequest.h"
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
104 108
105 static void logConsoleError(ExecutionContext* context, const String& message) 109 static void logConsoleError(ExecutionContext* context, const String& message)
106 { 110 {
107 if (!context) 111 if (!context)
108 return; 112 return;
109 // FIXME: It's not good to report the bad usage without indicating what sour ce line it came from. 113 // FIXME: It's not good to report the bad usage without indicating what sour ce line it came from.
110 // We should pass additional parameters so we can tell the console where the mistake occurred. 114 // We should pass additional parameters so we can tell the console where the mistake occurred.
111 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, ErrorMess ageLevel, message)); 115 context->addConsoleMessage(ConsoleMessage::create(JSMessageSource, ErrorMess ageLevel, message));
112 } 116 }
113 117
118 namespace {
119
120 class ReadableStreamSource : public GarbageCollectedFinalized<ReadableStreamSour ce>, public UnderlyingSource {
121 USING_GARBAGE_COLLECTED_MIXIN(ReadableStreamSource);
122 public:
123 ReadableStreamSource(XMLHttpRequest* owner) : m_owner(owner) { }
124 virtual ~ReadableStreamSource() { }
125 virtual void pullSource() OVERRIDE { }
126 virtual ScriptPromise cancelSource(ScriptState* scriptState, ScriptValue rea son) OVERRIDE
127 {
128 m_owner->abort();
129 return ScriptPromise::cast(scriptState, v8::Undefined(scriptState->isola te()));
130 }
131 virtual void trace(Visitor* visitor) OVERRIDE
132 {
133 visitor->trace(m_owner);
134 UnderlyingSource::trace(visitor);
135 }
136
137 private:
138 // This is RawPtr in non-oilpan build to avoid the reference cycle. To
139 // avoid use-after free, the associated ReadableStream must be closed
140 // or errored when m_owner is gone.
141 RawPtrWillBeMember<XMLHttpRequest> m_owner;
142 };
143
144 } // namespace
145
114 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin) 146 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin)
115 { 147 {
116 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin)); 148 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin));
117 xmlHttpRequest->suspendIfNeeded(); 149 xmlHttpRequest->suspendIfNeeded();
118 150
119 return xmlHttpRequest.release(); 151 return xmlHttpRequest.release();
120 } 152 }
121 153
122 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin) 154 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin)
123 : ActiveDOMObject(context) 155 : ActiveDOMObject(context)
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 320 }
289 m_binaryResponseBuilder.clear(); 321 m_binaryResponseBuilder.clear();
290 } else { 322 } else {
291 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 ); 323 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 );
292 } 324 }
293 } 325 }
294 326
295 return m_responseArrayBuffer.get(); 327 return m_responseArrayBuffer.get();
296 } 328 }
297 329
298 Stream* XMLHttpRequest::responseStream() 330 Stream* XMLHttpRequest::responseLegacyStream()
299 { 331 {
300 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream); 332 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream);
301 333
302 if (m_error || (m_state != LOADING && m_state != DONE)) 334 if (m_error || (m_state != LOADING && m_state != DONE))
303 return 0; 335 return 0;
304 336
305 return m_responseStream.get(); 337 return m_responseLegacyStream.get();
338 }
339
340 ReadableStream* XMLHttpRequest::responseStream()
341 {
342 ASSERT(m_responseTypeCode == ResponseTypeStream);
343 if (m_error || (m_state != LOADING && m_state != DONE))
344 return 0;
345
346 return m_responseStream;
306 } 347 }
307 348
308 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State) 349 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State)
309 { 350 {
310 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156 351 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156
311 // 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." 352 // 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."
312 if (executionContext()->isDocument() && !m_async) { 353 if (executionContext()->isDocument() && !m_async) {
313 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document."); 354 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document.");
314 return; 355 return;
315 } 356 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 m_responseTypeCode = ResponseTypeDocument; 390 m_responseTypeCode = ResponseTypeDocument;
350 } else if (responseType == "blob") { 391 } else if (responseType == "blob") {
351 m_responseTypeCode = ResponseTypeBlob; 392 m_responseTypeCode = ResponseTypeBlob;
352 } else if (responseType == "arraybuffer") { 393 } else if (responseType == "arraybuffer") {
353 m_responseTypeCode = ResponseTypeArrayBuffer; 394 m_responseTypeCode = ResponseTypeArrayBuffer;
354 } else if (responseType == "legacystream") { 395 } else if (responseType == "legacystream") {
355 if (RuntimeEnabledFeatures::streamEnabled()) 396 if (RuntimeEnabledFeatures::streamEnabled())
356 m_responseTypeCode = ResponseTypeLegacyStream; 397 m_responseTypeCode = ResponseTypeLegacyStream;
357 else 398 else
358 return; 399 return;
400 } else if (responseType == "stream") {
401 if (RuntimeEnabledFeatures::streamEnabled())
402 m_responseTypeCode = ResponseTypeStream;
403 else
404 return;
359 } else { 405 } else {
360 ASSERT_NOT_REACHED(); 406 ASSERT_NOT_REACHED();
361 } 407 }
362 } 408 }
363 409
364 String XMLHttpRequest::responseType() 410 String XMLHttpRequest::responseType()
365 { 411 {
366 switch (m_responseTypeCode) { 412 switch (m_responseTypeCode) {
367 case ResponseTypeDefault: 413 case ResponseTypeDefault:
368 return ""; 414 return "";
369 case ResponseTypeText: 415 case ResponseTypeText:
370 return "text"; 416 return "text";
371 case ResponseTypeJSON: 417 case ResponseTypeJSON:
372 return "json"; 418 return "json";
373 case ResponseTypeDocument: 419 case ResponseTypeDocument:
374 return "document"; 420 return "document";
375 case ResponseTypeBlob: 421 case ResponseTypeBlob:
376 return "blob"; 422 return "blob";
377 case ResponseTypeArrayBuffer: 423 case ResponseTypeArrayBuffer:
378 return "arraybuffer"; 424 return "arraybuffer";
379 case ResponseTypeLegacyStream: 425 case ResponseTypeLegacyStream:
380 return "legacystream"; 426 return "legacystream";
427 case ResponseTypeStream:
428 return "stream";
381 } 429 }
382 return ""; 430 return "";
383 } 431 }
384 432
385 String XMLHttpRequest::responseURL() 433 String XMLHttpRequest::responseURL()
386 { 434 {
387 return m_response.url().string(); 435 return m_response.url().string();
388 } 436 }
389 437
390 XMLHttpRequestUpload* XMLHttpRequest::upload() 438 XMLHttpRequestUpload* XMLHttpRequest::upload()
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 } 934 }
887 935
888 bool XMLHttpRequest::internalAbort() 936 bool XMLHttpRequest::internalAbort()
889 { 937 {
890 m_error = true; 938 m_error = true;
891 939
892 clearVariablesForLoading(); 940 clearVariablesForLoading();
893 941
894 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this); 942 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this);
895 943
896 if (m_responseStream && m_state != DONE) 944 if (m_responseLegacyStream && m_state != DONE)
897 m_responseStream->abort(); 945 m_responseLegacyStream->abort();
946
947 if (m_responseStream) {
948 // When the stream is already closed (including canceled from the
949 // user), |error| does nothing.
950 // FIXME: Create a more specific error.
951 m_responseStream->error(DOMException::create(!m_async && m_exceptionCode ? m_exceptionCode : AbortError, "XMLHttpRequest::abort"));
952 }
898 953
899 if (!m_loader) 954 if (!m_loader)
900 return true; 955 return true;
901 956
902 // Cancelling the ThreadableLoader m_loader may result in calling 957 // Cancelling the ThreadableLoader m_loader may result in calling
903 // window.onload synchronously. If such an onload handler contains open() 958 // window.onload synchronously. If such an onload handler contains open()
904 // call on the same XMLHttpRequest object, reentry happens. 959 // call on the same XMLHttpRequest object, reentry happens.
905 // 960 //
906 // If, window.onload contains open() and send(), m_loader will be set to 961 // If, window.onload contains open() and send(), m_loader will be set to
907 // non 0 value. So, we cannot continue the outer open(). In such case, 962 // non 0 value. So, we cannot continue the outer open(). In such case,
(...skipping 21 matching lines...) Expand all
929 m_response = ResourceResponse(); 984 m_response = ResourceResponse();
930 985
931 m_responseText.clear(); 986 m_responseText.clear();
932 987
933 m_parsedResponse = false; 988 m_parsedResponse = false;
934 m_responseDocument = nullptr; 989 m_responseDocument = nullptr;
935 990
936 m_responseBlob = nullptr; 991 m_responseBlob = nullptr;
937 m_downloadedBlobLength = 0; 992 m_downloadedBlobLength = 0;
938 993
994 m_responseLegacyStream = nullptr;
939 m_responseStream = nullptr; 995 m_responseStream = nullptr;
940 996
941 // These variables may referred by the response accessors. So, we can clear 997 // These variables may referred by the response accessors. So, we can clear
942 // this only when we clear the response holder variables above. 998 // this only when we clear the response holder variables above.
943 m_binaryResponseBuilder.clear(); 999 m_binaryResponseBuilder.clear();
944 m_responseArrayBuffer.clear(); 1000 m_responseArrayBuffer.clear();
945 } 1001 }
946 1002
947 void XMLHttpRequest::clearRequest() 1003 void XMLHttpRequest::clearRequest()
948 { 1004 {
(...skipping 273 matching lines...) Expand 10 before | Expand all | Expand 10 after
1222 1278
1223 if (m_error) 1279 if (m_error)
1224 return; 1280 return;
1225 1281
1226 if (m_state < HEADERS_RECEIVED) 1282 if (m_state < HEADERS_RECEIVED)
1227 changeState(HEADERS_RECEIVED); 1283 changeState(HEADERS_RECEIVED);
1228 1284
1229 if (m_decoder) 1285 if (m_decoder)
1230 m_responseText = m_responseText.concatenateWith(m_decoder->flush()); 1286 m_responseText = m_responseText.concatenateWith(m_decoder->flush());
1231 1287
1288 if (m_responseLegacyStream)
1289 m_responseLegacyStream->finalize();
1290
1232 if (m_responseStream) 1291 if (m_responseStream)
1233 m_responseStream->finalize(); 1292 m_responseStream->close();
1234 1293
1235 clearVariablesForLoading(); 1294 clearVariablesForLoading();
1236 1295
1237 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er); 1296 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er);
1238 1297
1239 if (m_loader) 1298 if (m_loader)
1240 m_loader = nullptr; 1299 m_loader = nullptr;
1241 1300
1242 changeState(DONE); 1301 changeState(DONE);
1243 } 1302 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1319 if (!m_decoder) 1378 if (!m_decoder)
1320 m_decoder = createDecoder(); 1379 m_decoder = createDecoder();
1321 1380
1322 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len)); 1381 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len));
1323 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) { 1382 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) {
1324 // Buffer binary data. 1383 // Buffer binary data.
1325 if (!m_binaryResponseBuilder) 1384 if (!m_binaryResponseBuilder)
1326 m_binaryResponseBuilder = SharedBuffer::create(); 1385 m_binaryResponseBuilder = SharedBuffer::create();
1327 m_binaryResponseBuilder->append(data, len); 1386 m_binaryResponseBuilder->append(data, len);
1328 } else if (m_responseTypeCode == ResponseTypeLegacyStream) { 1387 } else if (m_responseTypeCode == ResponseTypeLegacyStream) {
1329 if (!m_responseStream) 1388 if (!m_responseLegacyStream)
1330 m_responseStream = Stream::create(executionContext(), finalResponseM IMEType()); 1389 m_responseLegacyStream = Stream::create(executionContext(), response Type());
1331 m_responseStream->addData(data, len); 1390 m_responseLegacyStream->addData(data, len);
1391 } else if (m_responseTypeCode == ResponseTypeStream) {
1392 if (!m_responseStream) {
1393 m_responseStream = new ReadableStreamImpl<ReadableStreamChunkTypeTra its<ArrayBuffer> >(executionContext(), new ReadableStreamSource(this));
1394 m_responseStream->didSourceStart();
1395 }
1396 m_responseStream->enqueue(ArrayBuffer::create(data, len));
1332 } 1397 }
1333 1398
1334 if (m_error) 1399 if (m_error)
1335 return; 1400 return;
1336 1401
1337 trackProgress(len); 1402 trackProgress(len);
1338 } 1403 }
1339 1404
1340 void XMLHttpRequest::didDownloadData(int dataLength) 1405 void XMLHttpRequest::didDownloadData(int dataLength)
1341 { 1406 {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1410 } 1475 }
1411 1476
1412 ExecutionContext* XMLHttpRequest::executionContext() const 1477 ExecutionContext* XMLHttpRequest::executionContext() const
1413 { 1478 {
1414 return ActiveDOMObject::executionContext(); 1479 return ActiveDOMObject::executionContext();
1415 } 1480 }
1416 1481
1417 void XMLHttpRequest::trace(Visitor* visitor) 1482 void XMLHttpRequest::trace(Visitor* visitor)
1418 { 1483 {
1419 visitor->trace(m_responseBlob); 1484 visitor->trace(m_responseBlob);
1485 visitor->trace(m_responseLegacyStream);
1420 visitor->trace(m_responseStream); 1486 visitor->trace(m_responseStream);
1487 visitor->trace(m_streamSource);
1421 visitor->trace(m_responseDocument); 1488 visitor->trace(m_responseDocument);
1422 visitor->trace(m_progressEventThrottle); 1489 visitor->trace(m_progressEventThrottle);
1423 visitor->trace(m_upload); 1490 visitor->trace(m_upload);
1424 XMLHttpRequestEventTarget::trace(visitor); 1491 XMLHttpRequestEventTarget::trace(visitor);
1425 } 1492 }
1426 1493
1427 } // namespace blink 1494 } // namespace blink
OLDNEW
« no previous file with comments | « Source/core/xml/XMLHttpRequest.h ('k') | Source/core/xml/XMLHttpRequest.idl » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698