2 * Copyright (C) 2007 Apple Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "ProgressTracker.h"
29 #include "DocumentLoader.h"
31 #include "FrameLoader.h"
32 #include "FrameLoaderStateMachine.h"
33 #include "FrameLoaderClient.h"
35 #include "ResourceResponse.h"
36 #include <wtf/text/CString.h>
37 #include <wtf/CurrentTime.h>
43 // Always start progress at initialProgressValue. This helps provide feedback as
44 // soon as a load starts.
45 static const double initialProgressValue = 0.1;
47 // Similarly, always leave space at the end. This helps show the user that we're not done
49 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue
51 static const int progressItemDefaultEstimatedLength = 1024 * 16;
53 struct ProgressItem : Noncopyable {
54 ProgressItem(long long length)
56 , estimatedLength(length) { }
58 long long bytesReceived;
59 long long estimatedLength;
62 unsigned long ProgressTracker::s_uniqueIdentifier = 0;
64 ProgressTracker::ProgressTracker()
65 : m_totalPageAndResourceBytesToLoad(0)
66 , m_totalBytesReceived(0)
67 , m_lastNotifiedProgressValue(0)
68 , m_lastNotifiedProgressTime(0)
69 , m_progressNotificationInterval(0.02)
70 , m_progressNotificationTimeInterval(0.1)
71 , m_finalProgressChangedSent(false)
73 , m_numProgressTrackedFrames(0)
77 ProgressTracker::~ProgressTracker()
79 deleteAllValues(m_progressItems);
82 double ProgressTracker::estimatedProgress() const
84 return m_progressValue;
87 void ProgressTracker::reset()
89 deleteAllValues(m_progressItems);
90 m_progressItems.clear();
92 m_totalPageAndResourceBytesToLoad = 0;
93 m_totalBytesReceived = 0;
95 m_lastNotifiedProgressValue = 0;
96 m_lastNotifiedProgressTime = 0;
97 m_finalProgressChangedSent = false;
98 m_numProgressTrackedFrames = 0;
99 m_originatingProgressFrame = 0;
102 void ProgressTracker::progressStarted(Frame* frame)
104 LOG(Progress, "Progress started (%p) - frame %p(\"%s\"), value %f, tracked frames %d, originating frame %p", this, frame, frame->tree()->uniqueName().string().utf8().data(), m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
106 frame->loader()->client()->willChangeEstimatedProgress();
108 if (m_numProgressTrackedFrames == 0 || m_originatingProgressFrame == frame) {
110 m_progressValue = initialProgressValue;
111 m_originatingProgressFrame = frame;
113 m_originatingProgressFrame->loader()->client()->postProgressStartedNotification();
115 m_numProgressTrackedFrames++;
117 frame->loader()->client()->didChangeEstimatedProgress();
120 void ProgressTracker::progressCompleted(Frame* frame)
122 LOG(Progress, "Progress completed (%p) - frame %p(\"%s\"), value %f, tracked frames %d, originating frame %p", this, frame, frame->tree()->uniqueName().string().utf8().data(), m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
124 if (m_numProgressTrackedFrames <= 0)
127 frame->loader()->client()->willChangeEstimatedProgress();
129 m_numProgressTrackedFrames--;
130 if (m_numProgressTrackedFrames == 0 ||
131 (frame == m_originatingProgressFrame && m_numProgressTrackedFrames != 0))
132 finalProgressComplete();
134 frame->loader()->client()->didChangeEstimatedProgress();
137 void ProgressTracker::finalProgressComplete()
139 LOG(Progress, "Final progress complete (%p)", this);
141 RefPtr<Frame> frame = m_originatingProgressFrame.release();
143 // Before resetting progress value be sure to send client a least one notification
144 // with final progress value.
145 if (!m_finalProgressChangedSent) {
147 frame->loader()->client()->postProgressEstimateChangedNotification();
152 frame->loader()->client()->setMainFrameDocumentReady(true);
153 frame->loader()->client()->postProgressFinishedNotification();
156 void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
158 LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d, originating frame %p", this, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
160 if (m_numProgressTrackedFrames <= 0)
163 long long estimatedLength = response.expectedContentLength();
164 if (estimatedLength < 0)
165 estimatedLength = progressItemDefaultEstimatedLength;
167 m_totalPageAndResourceBytesToLoad += estimatedLength;
169 if (ProgressItem* item = m_progressItems.get(identifier)) {
170 item->bytesReceived = 0;
171 item->estimatedLength = estimatedLength;
173 m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLength)).leakPtr());
176 void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
178 ProgressItem* item = m_progressItems.get(identifier);
180 // FIXME: Can this ever happen?
184 RefPtr<Frame> frame = m_originatingProgressFrame;
186 frame->loader()->client()->willChangeEstimatedProgress();
188 unsigned bytesReceived = length;
189 double increment, percentOfRemainingBytes;
190 long long remainingBytes, estimatedBytesForPendingRequests;
192 item->bytesReceived += bytesReceived;
193 if (item->bytesReceived > item->estimatedLength) {
194 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
195 item->estimatedLength = item->bytesReceived * 2;
198 int numPendingOrLoadingRequests = frame->loader()->numPendingOrLoadingRequests(true);
199 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numPendingOrLoadingRequests;
200 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPendingRequests) - m_totalBytesReceived);
201 if (remainingBytes > 0) // Prevent divide by 0.
202 percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes;
204 percentOfRemainingBytes = 1.0;
206 // For documents that use WebCore's layout system, treat first layout as the half-way point.
207 // FIXME: The hasHTMLView function is a sort of roundabout way of asking "do you use WebCore's layout system".
208 bool useClampedMaxProgress = frame->loader()->client()->hasHTMLView()
209 && !frame->loader()->stateMachine()->firstLayoutDone();
210 double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue;
211 increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes;
212 m_progressValue += increment;
213 m_progressValue = min(m_progressValue, maxProgressValue);
214 ASSERT(m_progressValue >= initialProgressValue);
216 m_totalBytesReceived += bytesReceived;
218 double now = currentTime();
219 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
221 LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d", this, m_progressValue, m_numProgressTrackedFrames);
222 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressValue;
223 if ((notificationProgressDelta >= m_progressNotificationInterval ||
224 notifiedProgressTimeDelta >= m_progressNotificationTimeInterval) &&
225 m_numProgressTrackedFrames > 0) {
226 if (!m_finalProgressChangedSent) {
227 if (m_progressValue == 1)
228 m_finalProgressChangedSent = true;
230 frame->loader()->client()->postProgressEstimateChangedNotification();
232 m_lastNotifiedProgressValue = m_progressValue;
233 m_lastNotifiedProgressTime = now;
237 frame->loader()->client()->didChangeEstimatedProgress();
240 void ProgressTracker::completeProgress(unsigned long identifier)
242 ProgressItem* item = m_progressItems.get(identifier);
244 // FIXME: Can this happen?
248 // Adjust the total expected bytes to account for any overage/underage.
249 long long delta = item->bytesReceived - item->estimatedLength;
250 m_totalPageAndResourceBytesToLoad += delta;
251 item->estimatedLength = item->bytesReceived;
253 m_progressItems.remove(identifier);
257 unsigned long ProgressTracker::createUniqueIdentifier()
259 return ++s_uniqueIdentifier;