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;
54 WTF_MAKE_NONCOPYABLE(ProgressItem); WTF_MAKE_FAST_ALLOCATED;
56 ProgressItem(long long length)
58 , estimatedLength(length) { }
60 long long bytesReceived;
61 long long estimatedLength;
64 unsigned long ProgressTracker::s_uniqueIdentifier = 0;
66 ProgressTracker::ProgressTracker()
67 : m_totalPageAndResourceBytesToLoad(0)
68 , m_totalBytesReceived(0)
69 , m_lastNotifiedProgressValue(0)
70 , m_lastNotifiedProgressTime(0)
71 , m_progressNotificationInterval(0.02)
72 , m_progressNotificationTimeInterval(0.1)
73 , m_finalProgressChangedSent(false)
75 , m_numProgressTrackedFrames(0)
79 ProgressTracker::~ProgressTracker()
81 deleteAllValues(m_progressItems);
84 double ProgressTracker::estimatedProgress() const
86 return m_progressValue;
89 void ProgressTracker::reset()
91 deleteAllValues(m_progressItems);
92 m_progressItems.clear();
94 m_totalPageAndResourceBytesToLoad = 0;
95 m_totalBytesReceived = 0;
97 m_lastNotifiedProgressValue = 0;
98 m_lastNotifiedProgressTime = 0;
99 m_finalProgressChangedSent = false;
100 m_numProgressTrackedFrames = 0;
101 m_originatingProgressFrame = 0;
104 void ProgressTracker::progressStarted(Frame* frame)
106 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());
108 frame->loader()->client()->willChangeEstimatedProgress();
110 if (m_numProgressTrackedFrames == 0 || m_originatingProgressFrame == frame) {
112 m_progressValue = initialProgressValue;
113 m_originatingProgressFrame = frame;
115 m_originatingProgressFrame->loader()->client()->postProgressStartedNotification();
117 m_numProgressTrackedFrames++;
119 frame->loader()->client()->didChangeEstimatedProgress();
122 void ProgressTracker::progressCompleted(Frame* frame)
124 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());
126 if (m_numProgressTrackedFrames <= 0)
129 frame->loader()->client()->willChangeEstimatedProgress();
131 m_numProgressTrackedFrames--;
132 if (!m_numProgressTrackedFrames || m_originatingProgressFrame == frame)
133 finalProgressComplete();
135 frame->loader()->client()->didChangeEstimatedProgress();
138 void ProgressTracker::finalProgressComplete()
140 LOG(Progress, "Final progress complete (%p)", this);
142 RefPtr<Frame> frame = m_originatingProgressFrame.release();
144 // Before resetting progress value be sure to send client a least one notification
145 // with final progress value.
146 if (!m_finalProgressChangedSent) {
148 frame->loader()->client()->postProgressEstimateChangedNotification();
153 frame->loader()->client()->setMainFrameDocumentReady(true);
154 frame->loader()->client()->postProgressFinishedNotification();
157 void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
159 LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d, originating frame %p", this, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
161 if (m_numProgressTrackedFrames <= 0)
164 long long estimatedLength = response.expectedContentLength();
165 if (estimatedLength < 0)
166 estimatedLength = progressItemDefaultEstimatedLength;
168 m_totalPageAndResourceBytesToLoad += estimatedLength;
170 if (ProgressItem* item = m_progressItems.get(identifier)) {
171 item->bytesReceived = 0;
172 item->estimatedLength = estimatedLength;
174 m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLength)).leakPtr());
177 void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
179 ProgressItem* item = m_progressItems.get(identifier);
181 // FIXME: Can this ever happen?
185 RefPtr<Frame> frame = m_originatingProgressFrame;
187 frame->loader()->client()->willChangeEstimatedProgress();
189 unsigned bytesReceived = length;
190 double increment, percentOfRemainingBytes;
191 long long remainingBytes, estimatedBytesForPendingRequests;
193 item->bytesReceived += bytesReceived;
194 if (item->bytesReceived > item->estimatedLength) {
195 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
196 item->estimatedLength = item->bytesReceived * 2;
199 int numPendingOrLoadingRequests = frame->loader()->numPendingOrLoadingRequests(true);
200 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numPendingOrLoadingRequests;
201 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPendingRequests) - m_totalBytesReceived);
202 if (remainingBytes > 0) // Prevent divide by 0.
203 percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes;
205 percentOfRemainingBytes = 1.0;
207 // For documents that use WebCore's layout system, treat first layout as the half-way point.
208 // FIXME: The hasHTMLView function is a sort of roundabout way of asking "do you use WebCore's layout system".
209 bool useClampedMaxProgress = frame->loader()->client()->hasHTMLView()
210 && !frame->loader()->stateMachine()->firstLayoutDone();
211 double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue;
212 increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes;
213 m_progressValue += increment;
214 m_progressValue = min(m_progressValue, maxProgressValue);
215 ASSERT(m_progressValue >= initialProgressValue);
217 m_totalBytesReceived += bytesReceived;
219 double now = currentTime();
220 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
222 LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d", this, m_progressValue, m_numProgressTrackedFrames);
223 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressValue;
224 if ((notificationProgressDelta >= m_progressNotificationInterval ||
225 notifiedProgressTimeDelta >= m_progressNotificationTimeInterval) &&
226 m_numProgressTrackedFrames > 0) {
227 if (!m_finalProgressChangedSent) {
228 if (m_progressValue == 1)
229 m_finalProgressChangedSent = true;
231 frame->loader()->client()->postProgressEstimateChangedNotification();
233 m_lastNotifiedProgressValue = m_progressValue;
234 m_lastNotifiedProgressTime = now;
238 frame->loader()->client()->didChangeEstimatedProgress();
241 void ProgressTracker::completeProgress(unsigned long identifier)
243 ProgressItem* item = m_progressItems.get(identifier);
245 // This can happen if a load fails without receiving any response data.
249 // Adjust the total expected bytes to account for any overage/underage.
250 long long delta = item->bytesReceived - item->estimatedLength;
251 m_totalPageAndResourceBytesToLoad += delta;
252 item->estimatedLength = item->bytesReceived;
254 m_progressItems.remove(identifier);
258 unsigned long ProgressTracker::createUniqueIdentifier()
260 return ++s_uniqueIdentifier;