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

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/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 UnderlyingSource::trace(visitor);
134 visitor->trace(m_owner);
135 }
136
137 private:
138 RawPtrWillBeMember<XMLHttpRequest> m_owner;
tyoshino (SeeGerritForStatus) 2014/08/15 09:32:06 please write the reason why we an use RawPtr here
yhirano 2014/08/15 09:51:35 Done.
139 };
140
141 } // namespace
142
114 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin) 143 PassRefPtrWillBeRawPtr<XMLHttpRequest> XMLHttpRequest::create(ExecutionContext* context, PassRefPtr<SecurityOrigin> securityOrigin)
115 { 144 {
116 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin)); 145 RefPtrWillBeRawPtr<XMLHttpRequest> xmlHttpRequest = adoptRefWillBeRefCounted GarbageCollected(new XMLHttpRequest(context, securityOrigin));
117 xmlHttpRequest->suspendIfNeeded(); 146 xmlHttpRequest->suspendIfNeeded();
118 147
119 return xmlHttpRequest.release(); 148 return xmlHttpRequest.release();
120 } 149 }
121 150
122 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin) 151 XMLHttpRequest::XMLHttpRequest(ExecutionContext* context, PassRefPtr<SecurityOri gin> securityOrigin)
123 : ActiveDOMObject(context) 152 : ActiveDOMObject(context)
(...skipping 164 matching lines...) Expand 10 before | Expand all | Expand 10 after
288 } 317 }
289 m_binaryResponseBuilder.clear(); 318 m_binaryResponseBuilder.clear();
290 } else { 319 } else {
291 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 ); 320 m_responseArrayBuffer = ArrayBuffer::create(static_cast<void*>(0), 0 );
292 } 321 }
293 } 322 }
294 323
295 return m_responseArrayBuffer.get(); 324 return m_responseArrayBuffer.get();
296 } 325 }
297 326
298 Stream* XMLHttpRequest::responseStream() 327 Stream* XMLHttpRequest::responseLegacyStream()
299 { 328 {
300 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream); 329 ASSERT(m_responseTypeCode == ResponseTypeLegacyStream);
301 330
302 if (m_error || (m_state != LOADING && m_state != DONE)) 331 if (m_error || (m_state != LOADING && m_state != DONE))
303 return 0; 332 return 0;
304 333
305 return m_responseStream.get(); 334 return m_responseLegacyStream.get();
335 }
336
337 ReadableStream* XMLHttpRequest::responseStream()
338 {
339 ASSERT(m_responseTypeCode == ResponseTypeStream);
340 if (m_error || (m_state != LOADING && m_state != DONE))
341 return 0;
342
343 return m_responseStream;
306 } 344 }
307 345
308 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State) 346 void XMLHttpRequest::setTimeout(unsigned long timeout, ExceptionState& exception State)
309 { 347 {
310 // FIXME: Need to trigger or update the timeout Timer here, if needed. http: //webkit.org/b/98156 348 // 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." 349 // 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) { 350 if (executionContext()->isDocument() && !m_async) {
313 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document."); 351 exceptionState.throwDOMException(InvalidAccessError, "Timeouts cannot be set for synchronous requests made from a document.");
314 return; 352 return;
315 } 353 }
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
349 m_responseTypeCode = ResponseTypeDocument; 387 m_responseTypeCode = ResponseTypeDocument;
350 } else if (responseType == "blob") { 388 } else if (responseType == "blob") {
351 m_responseTypeCode = ResponseTypeBlob; 389 m_responseTypeCode = ResponseTypeBlob;
352 } else if (responseType == "arraybuffer") { 390 } else if (responseType == "arraybuffer") {
353 m_responseTypeCode = ResponseTypeArrayBuffer; 391 m_responseTypeCode = ResponseTypeArrayBuffer;
354 } else if (responseType == "legacystream") { 392 } else if (responseType == "legacystream") {
355 if (RuntimeEnabledFeatures::streamEnabled()) 393 if (RuntimeEnabledFeatures::streamEnabled())
356 m_responseTypeCode = ResponseTypeLegacyStream; 394 m_responseTypeCode = ResponseTypeLegacyStream;
357 else 395 else
358 return; 396 return;
397 } else if (responseType == "stream") {
398 if (RuntimeEnabledFeatures::streamEnabled())
399 m_responseTypeCode = ResponseTypeStream;
400 else
401 return;
359 } else { 402 } else {
360 ASSERT_NOT_REACHED(); 403 ASSERT_NOT_REACHED();
361 } 404 }
362 } 405 }
363 406
364 String XMLHttpRequest::responseType() 407 String XMLHttpRequest::responseType()
365 { 408 {
366 switch (m_responseTypeCode) { 409 switch (m_responseTypeCode) {
367 case ResponseTypeDefault: 410 case ResponseTypeDefault:
368 return ""; 411 return "";
369 case ResponseTypeText: 412 case ResponseTypeText:
370 return "text"; 413 return "text";
371 case ResponseTypeJSON: 414 case ResponseTypeJSON:
372 return "json"; 415 return "json";
373 case ResponseTypeDocument: 416 case ResponseTypeDocument:
374 return "document"; 417 return "document";
375 case ResponseTypeBlob: 418 case ResponseTypeBlob:
376 return "blob"; 419 return "blob";
377 case ResponseTypeArrayBuffer: 420 case ResponseTypeArrayBuffer:
378 return "arraybuffer"; 421 return "arraybuffer";
379 case ResponseTypeLegacyStream: 422 case ResponseTypeLegacyStream:
380 return "legacystream"; 423 return "legacystream";
424 case ResponseTypeStream:
425 return "stream";
381 } 426 }
382 return ""; 427 return "";
383 } 428 }
384 429
385 String XMLHttpRequest::responseURL() 430 String XMLHttpRequest::responseURL()
386 { 431 {
387 return m_response.url().string(); 432 return m_response.url().string();
388 } 433 }
389 434
390 XMLHttpRequestUpload* XMLHttpRequest::upload() 435 XMLHttpRequestUpload* XMLHttpRequest::upload()
(...skipping 495 matching lines...) Expand 10 before | Expand all | Expand 10 after
886 } 931 }
887 932
888 bool XMLHttpRequest::internalAbort() 933 bool XMLHttpRequest::internalAbort()
889 { 934 {
890 m_error = true; 935 m_error = true;
891 936
892 clearVariablesForLoading(); 937 clearVariablesForLoading();
893 938
894 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this); 939 InspectorInstrumentation::didFailXHRLoading(executionContext(), this, this);
895 940
896 if (m_responseStream && m_state != DONE) 941 if (m_responseLegacyStream && m_state != DONE)
897 m_responseStream->abort(); 942 m_responseLegacyStream->abort();
943
944 if (m_responseStream) {
945 // When the stream is already closed (including canceled from the
946 // user), |error| does nothing.
947 // FIXME: Create a more specific error.
948 m_responseStream->error(DOMException::create(!m_async && m_exceptionCode ? m_exceptionCode : AbortError, "XMLHttpRequest::abort"));
949 }
898 950
899 if (!m_loader) 951 if (!m_loader)
900 return true; 952 return true;
901 953
902 // Cancelling the ThreadableLoader m_loader may result in calling 954 // Cancelling the ThreadableLoader m_loader may result in calling
903 // window.onload synchronously. If such an onload handler contains open() 955 // window.onload synchronously. If such an onload handler contains open()
904 // call on the same XMLHttpRequest object, reentry happens. 956 // call on the same XMLHttpRequest object, reentry happens.
905 // 957 //
906 // If, window.onload contains open() and send(), m_loader will be set to 958 // 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, 959 // non 0 value. So, we cannot continue the outer open(). In such case,
(...skipping 21 matching lines...) Expand all
929 m_response = ResourceResponse(); 981 m_response = ResourceResponse();
930 982
931 m_responseText.clear(); 983 m_responseText.clear();
932 984
933 m_parsedResponse = false; 985 m_parsedResponse = false;
934 m_responseDocument = nullptr; 986 m_responseDocument = nullptr;
935 987
936 m_responseBlob = nullptr; 988 m_responseBlob = nullptr;
937 m_downloadedBlobLength = 0; 989 m_downloadedBlobLength = 0;
938 990
991 m_responseLegacyStream = nullptr;
939 m_responseStream = nullptr; 992 m_responseStream = nullptr;
940 993
941 // These variables may referred by the response accessors. So, we can clear 994 // These variables may referred by the response accessors. So, we can clear
942 // this only when we clear the response holder variables above. 995 // this only when we clear the response holder variables above.
943 m_binaryResponseBuilder.clear(); 996 m_binaryResponseBuilder.clear();
944 m_responseArrayBuffer.clear(); 997 m_responseArrayBuffer.clear();
945 } 998 }
946 999
947 void XMLHttpRequest::clearRequest() 1000 void XMLHttpRequest::clearRequest()
948 { 1001 {
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after
1220 1273
1221 if (m_error) 1274 if (m_error)
1222 return; 1275 return;
1223 1276
1224 if (m_state < HEADERS_RECEIVED) 1277 if (m_state < HEADERS_RECEIVED)
1225 changeState(HEADERS_RECEIVED); 1278 changeState(HEADERS_RECEIVED);
1226 1279
1227 if (m_decoder) 1280 if (m_decoder)
1228 m_responseText = m_responseText.concatenateWith(m_decoder->flush()); 1281 m_responseText = m_responseText.concatenateWith(m_decoder->flush());
1229 1282
1283 if (m_responseLegacyStream)
1284 m_responseLegacyStream->finalize();
1285
1230 if (m_responseStream) 1286 if (m_responseStream)
1231 m_responseStream->finalize(); 1287 m_responseStream->close();
1232 1288
1233 clearVariablesForLoading(); 1289 clearVariablesForLoading();
1234 1290
1235 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er); 1291 InspectorInstrumentation::didFinishXHRLoading(executionContext(), this, this , identifier, m_responseText, m_method, m_url, m_lastSendURL, m_lastSendLineNumb er);
1236 1292
1237 if (m_loader) 1293 if (m_loader)
1238 m_loader = nullptr; 1294 m_loader = nullptr;
1239 1295
1240 changeState(DONE); 1296 changeState(DONE);
1241 } 1297 }
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
1317 if (!m_decoder) 1373 if (!m_decoder)
1318 m_decoder = createDecoder(); 1374 m_decoder = createDecoder();
1319 1375
1320 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len)); 1376 m_responseText = m_responseText.concatenateWith(m_decoder->decode(data, len));
1321 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) { 1377 } else if (m_responseTypeCode == ResponseTypeArrayBuffer) {
1322 // Buffer binary data. 1378 // Buffer binary data.
1323 if (!m_binaryResponseBuilder) 1379 if (!m_binaryResponseBuilder)
1324 m_binaryResponseBuilder = SharedBuffer::create(); 1380 m_binaryResponseBuilder = SharedBuffer::create();
1325 m_binaryResponseBuilder->append(data, len); 1381 m_binaryResponseBuilder->append(data, len);
1326 } else if (m_responseTypeCode == ResponseTypeLegacyStream) { 1382 } else if (m_responseTypeCode == ResponseTypeLegacyStream) {
1327 if (!m_responseStream) 1383 if (!m_responseLegacyStream)
1328 m_responseStream = Stream::create(executionContext(), finalResponseM IMEType()); 1384 m_responseLegacyStream = Stream::create(executionContext(), response Type());
1329 m_responseStream->addData(data, len); 1385 m_responseLegacyStream->addData(data, len);
1386 } else if (m_responseTypeCode == ResponseTypeStream) {
1387 if (!m_responseStream) {
1388 m_responseStream = new ReadableStreamImpl<ReadableStreamChunkTypeTra its<ArrayBuffer> >(executionContext(), new ReadableStreamSource(this));
1389 m_responseStream->didSourceStart();
1390 }
1391 m_responseStream->enqueue(ArrayBuffer::create(data, len));
1330 } 1392 }
1331 1393
1332 if (m_error) 1394 if (m_error)
1333 return; 1395 return;
1334 1396
1335 trackProgress(len); 1397 trackProgress(len);
1336 } 1398 }
1337 1399
1338 void XMLHttpRequest::didDownloadData(int dataLength) 1400 void XMLHttpRequest::didDownloadData(int dataLength)
1339 { 1401 {
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after
1408 } 1470 }
1409 1471
1410 ExecutionContext* XMLHttpRequest::executionContext() const 1472 ExecutionContext* XMLHttpRequest::executionContext() const
1411 { 1473 {
1412 return ActiveDOMObject::executionContext(); 1474 return ActiveDOMObject::executionContext();
1413 } 1475 }
1414 1476
1415 void XMLHttpRequest::trace(Visitor* visitor) 1477 void XMLHttpRequest::trace(Visitor* visitor)
1416 { 1478 {
1417 visitor->trace(m_responseBlob); 1479 visitor->trace(m_responseBlob);
1480 visitor->trace(m_responseLegacyStream);
1418 visitor->trace(m_responseStream); 1481 visitor->trace(m_responseStream);
1482 visitor->trace(m_streamSource);
1419 visitor->trace(m_responseDocument); 1483 visitor->trace(m_responseDocument);
1420 visitor->trace(m_progressEventThrottle); 1484 visitor->trace(m_progressEventThrottle);
1421 visitor->trace(m_upload); 1485 visitor->trace(m_upload);
1422 XMLHttpRequestEventTarget::trace(visitor); 1486 XMLHttpRequestEventTarget::trace(visitor);
1423 } 1487 }
1424 1488
1425 } // namespace blink 1489 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698