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

Unified Diff: Source/core/xml/XMLHttpRequest.cpp

Issue 79953004: Improve fidelity of XHR progress events. (Closed) Base URL: https://chromium.googlesource.com/chromium/blink.git@master
Patch Set: Created 7 years, 1 month 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 side-by-side diff with in-line comments
Download patch
Index: Source/core/xml/XMLHttpRequest.cpp
diff --git a/Source/core/xml/XMLHttpRequest.cpp b/Source/core/xml/XMLHttpRequest.cpp
index 2af04637525e5de865aba366a12bc99b3cb2dbd4..6fc9cb052fc605fd67303be207776b8e6d169447 100644
--- a/Source/core/xml/XMLHttpRequest.cpp
+++ b/Source/core/xml/XMLHttpRequest.cpp
@@ -441,9 +441,9 @@ void XMLHttpRequest::dispatchReadyStateChangeEvent()
InspectorInstrumentation::didDispatchXHRReadyStateChangeEvent(cookie);
if (m_state == DONE && !m_error) {
InspectorInstrumentationCookie cookie = InspectorInstrumentation::willDispatchXHRLoadEvent(executionContext(), this);
- m_progressEventThrottle.dispatchEvent(XMLHttpRequestProgressEvent::create(EventTypeNames::load));
+ dispatchThrottledProgressEvent(EventTypeNames::load);
InspectorInstrumentation::didDispatchXHRLoadEvent(cookie);
- m_progressEventThrottle.dispatchEvent(XMLHttpRequestProgressEvent::create(EventTypeNames::loadend));
+ dispatchThrottledProgressEvent(EventTypeNames::loadend);
}
}
@@ -856,6 +856,9 @@ void XMLHttpRequest::abort()
bool sendFlag = m_loader;
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 comment that we save these variables here since th
sof 2013/11/22 07:17:05 Done.
+ long long expectedLength = m_response.expectedContentLength();
+ unsigned long long receivedLength = m_receivedLength;
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 i think we can change m_receivedLength to unsigned
sof 2013/11/22 07:17:05 Done; but I'd vote for moving to unsigned long lon
+
if (!internalAbort())
return;
@@ -866,7 +869,7 @@ void XMLHttpRequest::abort()
if (!((m_state <= OPENED && !sendFlag) || m_state == DONE)) {
ASSERT(!m_loader);
- handleRequestError(0, EventTypeNames::abort);
+ handleRequestError(0, EventTypeNames::abort, receivedLength, expectedLength);
}
m_state = UNSENT;
}
@@ -958,31 +961,41 @@ void XMLHttpRequest::handleDidFailGeneric()
m_error = true;
}
-void XMLHttpRequest::dispatchEventAndLoadEnd(const AtomicString& type)
+void XMLHttpRequest::dispatchEventAndLoadEnd(const AtomicString& type, long long receivedLength, unsigned long long expectedLength)
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 long long expectedLength
{
if (!m_uploadComplete) {
m_uploadComplete = true;
if (m_upload && m_uploadEventsAllowed)
- m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::create(type));
+ m_upload->dispatchEventAndLoadEnd(type);
}
- m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::create(type));
+ unsigned long long loaded = receivedLength >= 0 ? static_cast<unsigned long long>(receivedLength) : 0;
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 nice. please add this code also between L979 and 9
sof 2013/11/22 07:17:05 Done.
+ bool lengthComputable = expectedLength > 0 && loaded <= expectedLength;
+ unsigned long long total = lengthComputable ? expectedLength : 0;
+
+ m_progressEventThrottle.dispatchEventAndLoadEnd(type, lengthComputable, loaded, total);
}
-void XMLHttpRequest::dispatchThrottledProgressEvent()
+void XMLHttpRequest::dispatchThrottledProgressEvent(const AtomicString& type)
{
long long expectedLength = m_response.expectedContentLength();
bool lengthComputable = expectedLength > 0 && m_receivedLength <= expectedLength;
unsigned long long total = lengthComputable ? expectedLength : 0;
- m_progressEventThrottle.dispatchProgressEvent(lengthComputable, m_receivedLength, total);
+ if (type == EventTypeNames::progress)
+ m_progressEventThrottle.dispatchProgressEvent(lengthComputable, m_receivedLength, total);
+ else
+ m_progressEventThrottle.dispatchEvent(XMLHttpRequestProgressEvent::create(type, lengthComputable, m_receivedLength, total));
}
void XMLHttpRequest::handleNetworkError()
{
LOG(Network, "XMLHttpRequest %p handleNetworkError()", this);
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 comment the reason why we save them here
+ long long expectedLength = m_response.expectedContentLength();
+ unsigned long long receivedLength = m_receivedLength;
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 long long
+
handleDidFailGeneric();
- handleRequestError(NetworkError, EventTypeNames::error);
+ handleRequestError(NetworkError, EventTypeNames::error, receivedLength, expectedLength);
internalAbort();
}
@@ -990,11 +1003,14 @@ void XMLHttpRequest::handleDidCancel()
{
LOG(Network, "XMLHttpRequest %p handleDidCancel()", this);
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 comment the reason why we save them here
+ long long expectedLength = m_response.expectedContentLength();
+ unsigned long long receivedLength = m_receivedLength;
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 long long
+
handleDidFailGeneric();
- handleRequestError(AbortError, EventTypeNames::abort);
+ handleRequestError(AbortError, EventTypeNames::abort, receivedLength, expectedLength);
}
-void XMLHttpRequest::handleRequestError(ExceptionCode exceptionCode, const AtomicString& type)
+void XMLHttpRequest::handleRequestError(ExceptionCode exceptionCode, const AtomicString& type, long long receivedLength, unsigned long long expectedLength)
tyoshino (SeeGerritForStatus) 2013/11/22 04:48:21 long long expectedLength
{
LOG(Network, "XMLHttpRequest %p handleRequestError()", this);
@@ -1018,8 +1034,8 @@ void XMLHttpRequest::handleRequestError(ExceptionCode exceptionCode, const Atomi
m_upload->handleRequestError(type);
}
- dispatchThrottledProgressEvent();
- m_progressEventThrottle.dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::create(type));
+ dispatchThrottledProgressEvent(EventTypeNames::progress);
+ dispatchEventAndLoadEnd(type, receivedLength, expectedLength);
}
void XMLHttpRequest::dropProtectionSoon()
@@ -1219,8 +1235,6 @@ void XMLHttpRequest::didFinishLoading(unsigned long identifier, double)
if (m_decoder)
m_responseText = m_responseText.concatenateWith(m_decoder->flush());
- clearVariablesForLoading();
-
if (m_responseStream)
m_responseStream->finalize();
@@ -1235,6 +1249,8 @@ void XMLHttpRequest::didFinishLoading(unsigned long identifier, double)
}
changeState(DONE);
+
+ clearVariablesForLoading();
}
void XMLHttpRequest::didSendData(unsigned long long bytesSent, unsigned long long totalBytesToBeSent)
@@ -1250,7 +1266,7 @@ void XMLHttpRequest::didSendData(unsigned long long bytesSent, unsigned long lon
if (bytesSent == totalBytesToBeSent && !m_uploadComplete) {
m_uploadComplete = true;
if (m_uploadEventsAllowed)
- m_upload->dispatchEventAndLoadEnd(XMLHttpRequestProgressEvent::create(EventTypeNames::load));
+ m_upload->dispatchEventAndLoadEnd(EventTypeNames::load, true, bytesSent, totalBytesToBeSent);
}
}
@@ -1321,7 +1337,7 @@ void XMLHttpRequest::didReceiveData(const char* data, int len)
m_receivedLength += len;
if (m_async)
- dispatchThrottledProgressEvent();
+ dispatchThrottledProgressEvent(EventTypeNames::progress);
if (m_state != LOADING) {
changeState(LOADING);
@@ -1342,11 +1358,14 @@ void XMLHttpRequest::handleDidTimeout()
// internalAbort() calls dropProtection(), which may release the last reference.
RefPtr<XMLHttpRequest> protect(this);
+ long long expectedLength = m_response.expectedContentLength();
+ unsigned long long receivedLength = m_receivedLength;
+
if (!internalAbort())
return;
handleDidFailGeneric();
- handleRequestError(TimeoutError, EventTypeNames::timeout);
+ handleRequestError(TimeoutError, EventTypeNames::timeout, receivedLength, expectedLength);
}
void XMLHttpRequest::suspend()

Powered by Google App Engine
This is Rietveld 408576698