This source file includes following definitions.
- m_progressValue
- create
- estimatedProgress
- reset
- progressStarted
- progressCompleted
- incrementProgress
- incrementProgress
- completeProgress
#include "config.h"
#include "core/loader/ProgressTracker.h"
#include "core/fetch/ResourceFetcher.h"
#include "core/frame/FrameView.h"
#include "core/frame/LocalFrame.h"
#include "core/inspector/InspectorInstrumentation.h"
#include "core/loader/FrameLoader.h"
#include "core/loader/FrameLoaderClient.h"
#include "platform/Logging.h"
#include "platform/network/ResourceResponse.h"
#include "wtf/CurrentTime.h"
#include "wtf/text/CString.h"
using std::min;
namespace WebCore {
static const double initialProgressValue = 0.1;
static const double finalProgressValue = 0.9;
static const int progressItemDefaultEstimatedLength = 1024 * 16;
struct ProgressItem {
WTF_MAKE_NONCOPYABLE(ProgressItem); WTF_MAKE_FAST_ALLOCATED;
public:
ProgressItem(long long length)
: bytesReceived(0)
, estimatedLength(length) { }
long long bytesReceived;
long long estimatedLength;
};
ProgressTracker::ProgressTracker(LocalFrame* frame)
: m_frame(frame)
, m_inProgress(false)
, m_totalPageAndResourceBytesToLoad(0)
, m_totalBytesReceived(0)
, m_lastNotifiedProgressValue(0)
, m_lastNotifiedProgressTime(0)
, m_progressNotificationInterval(0.02)
, m_progressNotificationTimeInterval(0.1)
, m_finalProgressChangedSent(false)
, m_progressValue(0)
{
}
ProgressTracker::~ProgressTracker()
{
if (m_inProgress)
progressCompleted();
}
PassOwnPtr<ProgressTracker> ProgressTracker::create(LocalFrame* frame)
{
return adoptPtr(new ProgressTracker(frame));
}
double ProgressTracker::estimatedProgress() const
{
return m_progressValue;
}
void ProgressTracker::reset()
{
m_progressItems.clear();
m_totalPageAndResourceBytesToLoad = 0;
m_totalBytesReceived = 0;
m_progressValue = 0;
m_lastNotifiedProgressValue = 0;
m_lastNotifiedProgressTime = 0;
m_finalProgressChangedSent = false;
}
void ProgressTracker::progressStarted()
{
if (!m_inProgress) {
reset();
m_progressValue = initialProgressValue;
m_frame->loader().client()->didStartLoading(NavigationToDifferentDocument);
}
m_inProgress = true;
InspectorInstrumentation::frameStartedLoading(m_frame);
}
void ProgressTracker::progressCompleted()
{
ASSERT(m_inProgress);
m_inProgress = false;
if (!m_finalProgressChangedSent) {
m_progressValue = 1;
m_frame->loader().client()->progressEstimateChanged(m_progressValue);
}
reset();
m_frame->loader().client()->didStopLoading();
InspectorInstrumentation::frameStoppedLoading(m_frame);
}
void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
{
if (!m_inProgress)
return;
long long estimatedLength = response.expectedContentLength();
if (estimatedLength < 0)
estimatedLength = progressItemDefaultEstimatedLength;
m_totalPageAndResourceBytesToLoad += estimatedLength;
if (ProgressItem* item = m_progressItems.get(identifier)) {
item->bytesReceived = 0;
item->estimatedLength = estimatedLength;
} else
m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLength)));
}
void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
{
ProgressItem* item = m_progressItems.get(identifier);
if (!item)
return;
unsigned bytesReceived = length;
double increment, percentOfRemainingBytes;
long long remainingBytes, estimatedBytesForPendingRequests;
item->bytesReceived += bytesReceived;
if (item->bytesReceived > item->estimatedLength) {
m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
item->estimatedLength = item->bytesReceived * 2;
}
int numPendingOrLoadingRequests = m_frame->document()->fetcher()->requestCount();
estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numPendingOrLoadingRequests;
remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPendingRequests) - m_totalBytesReceived);
if (remainingBytes > 0)
percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes;
else
percentOfRemainingBytes = 1.0;
bool useClampedMaxProgress = !m_frame->view()->didFirstLayout();
double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue;
increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes;
m_progressValue += increment;
m_progressValue = min(m_progressValue, maxProgressValue);
ASSERT(m_progressValue >= initialProgressValue);
m_totalBytesReceived += bytesReceived;
double now = currentTime();
double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressValue;
if (notificationProgressDelta >= m_progressNotificationInterval || notifiedProgressTimeDelta >= m_progressNotificationTimeInterval) {
if (!m_finalProgressChangedSent) {
if (m_progressValue == 1)
m_finalProgressChangedSent = true;
m_frame->loader().client()->progressEstimateChanged(m_progressValue);
m_lastNotifiedProgressValue = m_progressValue;
m_lastNotifiedProgressTime = now;
}
}
}
void ProgressTracker::completeProgress(unsigned long identifier)
{
ProgressItem* item = m_progressItems.get(identifier);
if (!item)
return;
long long delta = item->bytesReceived - item->estimatedLength;
m_totalPageAndResourceBytesToLoad += delta;
m_progressItems.remove(identifier);
}
}