• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2007 Apple Inc.  All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
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.
12  *
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.
24  */
25 
26 #include "config.h"
27 #include "ProgressTracker.h"
28 
29 #include "Frame.h"
30 #include "FrameLoader.h"
31 #include "FrameLoaderClient.h"
32 #include "ResourceResponse.h"
33 #include <wtf/CurrentTime.h>
34 
35 using std::min;
36 
37 namespace WebCore {
38 
39 // Always start progress at initialProgressValue. This helps provide feedback as
40 // soon as a load starts.
41 static const double initialProgressValue = 0.1;
42 
43 // Similarly, always leave space at the end. This helps show the user that we're not done
44 // until we're done.
45 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue
46 
47 static const int progressItemDefaultEstimatedLength = 1024 * 16;
48 
49 struct ProgressItem {
ProgressItemWebCore::ProgressItem50     ProgressItem(long long length)
51         : bytesReceived(0)
52         , estimatedLength(length) { }
53 
54     long long bytesReceived;
55     long long estimatedLength;
56 };
57 
ProgressTracker()58 ProgressTracker::ProgressTracker()
59     : m_uniqueIdentifier(0)
60     , m_totalPageAndResourceBytesToLoad(0)
61     , m_totalBytesReceived(0)
62     , m_lastNotifiedProgressValue(0)
63     , m_lastNotifiedProgressTime(0)
64     , m_progressNotificationInterval(0.02)
65     , m_progressNotificationTimeInterval(0.1)
66     , m_finalProgressChangedSent(false)
67     , m_progressValue(0)
68     , m_numProgressTrackedFrames(0)
69 {
70 }
71 
~ProgressTracker()72 ProgressTracker::~ProgressTracker()
73 {
74     deleteAllValues(m_progressItems);
75 }
76 
estimatedProgress() const77 double ProgressTracker::estimatedProgress() const
78 {
79     return m_progressValue;
80 }
81 
reset()82 void ProgressTracker::reset()
83 {
84     deleteAllValues(m_progressItems);
85     m_progressItems.clear();
86 
87     m_totalPageAndResourceBytesToLoad = 0;
88     m_totalBytesReceived = 0;
89     m_progressValue = 0;
90     m_lastNotifiedProgressValue = 0;
91     m_lastNotifiedProgressTime = 0;
92     m_finalProgressChangedSent = false;
93     m_numProgressTrackedFrames = 0;
94     m_originatingProgressFrame = 0;
95 }
96 
progressStarted(Frame * frame)97 void ProgressTracker::progressStarted(Frame* frame)
98 {
99     // LOG (Progress, "frame %p(%@), _private->numProgressTrackedFrames %d, _private->originatingProgressFrame %p", frame, [frame name], _private->numProgressTrackedFrames, _private->originatingProgressFrame);
100 
101     frame->loader()->client()->willChangeEstimatedProgress();
102 
103     if (m_numProgressTrackedFrames == 0 || m_originatingProgressFrame == frame) {
104         reset();
105         m_progressValue = initialProgressValue;
106         m_originatingProgressFrame = frame;
107 
108         m_originatingProgressFrame->loader()->client()->postProgressStartedNotification();
109     }
110     m_numProgressTrackedFrames++;
111 
112     frame->loader()->client()->didChangeEstimatedProgress();
113 }
114 
progressCompleted(Frame * frame)115 void ProgressTracker::progressCompleted(Frame* frame)
116 {
117     // LOG (Progress, "frame %p(%@), _private->numProgressTrackedFrames %d, _private->originatingProgressFrame %p", frame, [frame name], _private->numProgressTrackedFrames, _private->originatingProgressFrame);
118 
119     if (m_numProgressTrackedFrames <= 0)
120         return;
121 
122     frame->loader()->client()->willChangeEstimatedProgress();
123 
124     m_numProgressTrackedFrames--;
125     if (m_numProgressTrackedFrames == 0 ||
126         (frame == m_originatingProgressFrame && m_numProgressTrackedFrames != 0))
127         finalProgressComplete();
128 
129     frame->loader()->client()->didChangeEstimatedProgress();
130 }
131 
finalProgressComplete()132 void ProgressTracker::finalProgressComplete()
133 {
134     // LOG (Progress, "");
135 
136     RefPtr<Frame> frame = m_originatingProgressFrame.release();
137 
138     // Before resetting progress value be sure to send client a least one notification
139     // with final progress value.
140     if (!m_finalProgressChangedSent) {
141         m_progressValue = 1;
142         frame->loader()->client()->postProgressEstimateChangedNotification();
143     }
144 
145     reset();
146 
147     frame->loader()->client()->setMainFrameDocumentReady(true);
148     frame->loader()->client()->postProgressFinishedNotification();
149 }
150 
incrementProgress(unsigned long identifier,const ResourceResponse & response)151 void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
152 {
153     // LOG (Progress, "_private->numProgressTrackedFrames %d, _private->originatingProgressFrame %p", _private->numProgressTrackedFrames, _private->originatingProgressFrame);
154 
155     if (m_numProgressTrackedFrames <= 0)
156         return;
157 
158     long long estimatedLength = response.expectedContentLength();
159     if (estimatedLength < 0)
160         estimatedLength = progressItemDefaultEstimatedLength;
161 
162     m_totalPageAndResourceBytesToLoad += estimatedLength;
163 
164     if (ProgressItem* item = m_progressItems.get(identifier)) {
165         item->bytesReceived = 0;
166         item->estimatedLength = estimatedLength;
167     } else
168         m_progressItems.set(identifier, new ProgressItem(estimatedLength));
169 }
170 
incrementProgress(unsigned long identifier,const char *,int length)171 void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
172 {
173     ProgressItem* item = m_progressItems.get(identifier);
174 
175     // FIXME: Can this ever happen?
176     if (!item)
177         return;
178 
179     m_originatingProgressFrame->loader()->client()->willChangeEstimatedProgress();
180 
181     unsigned bytesReceived = length;
182     double increment, percentOfRemainingBytes;
183     long long remainingBytes, estimatedBytesForPendingRequests;
184 
185     item->bytesReceived += bytesReceived;
186     if (item->bytesReceived > item->estimatedLength) {
187         m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
188         item->estimatedLength = item->bytesReceived * 2;
189     }
190 
191     int numPendingOrLoadingRequests = m_originatingProgressFrame->loader()->numPendingOrLoadingRequests(true);
192     estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numPendingOrLoadingRequests;
193     remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPendingRequests) - m_totalBytesReceived);
194     if (remainingBytes > 0)  // Prevent divide by 0.
195         percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes;
196     else
197         percentOfRemainingBytes = 1.0;
198 
199     // Treat the first layout as the half-way point.
200     double maxProgressValue = m_originatingProgressFrame->loader()->firstLayoutDone() ? finalProgressValue : .5;
201     increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes;
202     m_progressValue += increment;
203     m_progressValue = min(m_progressValue, maxProgressValue);
204     ASSERT(m_progressValue >= initialProgressValue);
205 
206     m_totalBytesReceived += bytesReceived;
207 
208     double now = currentTime();
209     double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
210 
211     // LOG (Progress, "_private->progressValue %g, _private->numProgressTrackedFrames %d", _private->progressValue, _private->numProgressTrackedFrames);
212     double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressValue;
213     if ((notificationProgressDelta >= m_progressNotificationInterval ||
214          notifiedProgressTimeDelta >= m_progressNotificationTimeInterval) &&
215         m_numProgressTrackedFrames > 0) {
216         if (!m_finalProgressChangedSent) {
217             if (m_progressValue == 1)
218                 m_finalProgressChangedSent = true;
219 
220             m_originatingProgressFrame->loader()->client()->postProgressEstimateChangedNotification();
221 
222             m_lastNotifiedProgressValue = m_progressValue;
223             m_lastNotifiedProgressTime = now;
224         }
225     }
226 
227     m_originatingProgressFrame->loader()->client()->didChangeEstimatedProgress();
228 }
229 
completeProgress(unsigned long identifier)230 void ProgressTracker::completeProgress(unsigned long identifier)
231 {
232     ProgressItem* item = m_progressItems.get(identifier);
233 
234     // FIXME: Can this happen?
235     if (!item)
236         return;
237 
238     // Adjust the total expected bytes to account for any overage/underage.
239     long long delta = item->bytesReceived - item->estimatedLength;
240     m_totalPageAndResourceBytesToLoad += delta;
241     item->estimatedLength = item->bytesReceived;
242 
243     m_progressItems.remove(identifier);
244     delete item;
245 }
246 
createUniqueIdentifier()247 unsigned long ProgressTracker::createUniqueIdentifier()
248 {
249     return ++m_uniqueIdentifier;
250 }
251 
252 
253 }
254