initial import
[vuplus_webkit] / Source / WebCore / loader / ProgressTracker.cpp
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 "DocumentLoader.h"
30 #include "Frame.h"
31 #include "FrameLoader.h"
32 #include "FrameLoaderStateMachine.h"
33 #include "FrameLoaderClient.h"
34 #include "Logging.h"
35 #include "ResourceResponse.h"
36 #include <wtf/text/CString.h>
37 #include <wtf/CurrentTime.h>
38
39 using std::min;
40
41 namespace WebCore {
42
43 // Always start progress at initialProgressValue. This helps provide feedback as 
44 // soon as a load starts.
45 static const double initialProgressValue = 0.1;
46     
47 // Similarly, always leave space at the end. This helps show the user that we're not done
48 // until we're done.
49 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue
50
51 static const int progressItemDefaultEstimatedLength = 1024 * 16;
52
53 struct ProgressItem {
54     WTF_MAKE_NONCOPYABLE(ProgressItem); WTF_MAKE_FAST_ALLOCATED;
55 public:
56     ProgressItem(long long length) 
57         : bytesReceived(0)
58         , estimatedLength(length) { }
59     
60     long long bytesReceived;
61     long long estimatedLength;
62 };
63
64 unsigned long ProgressTracker::s_uniqueIdentifier = 0;
65
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)
74     , m_progressValue(0)
75     , m_numProgressTrackedFrames(0)
76 {
77 }
78
79 ProgressTracker::~ProgressTracker()
80 {
81     deleteAllValues(m_progressItems);
82 }
83
84 double ProgressTracker::estimatedProgress() const
85 {
86     return m_progressValue;
87 }
88
89 void ProgressTracker::reset()
90 {
91     deleteAllValues(m_progressItems);
92     m_progressItems.clear();    
93
94     m_totalPageAndResourceBytesToLoad = 0;
95     m_totalBytesReceived = 0;
96     m_progressValue = 0;
97     m_lastNotifiedProgressValue = 0;
98     m_lastNotifiedProgressTime = 0;
99     m_finalProgressChangedSent = false;
100     m_numProgressTrackedFrames = 0;
101     m_originatingProgressFrame = 0;
102 }
103
104 void ProgressTracker::progressStarted(Frame* frame)
105 {
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());
107
108     frame->loader()->client()->willChangeEstimatedProgress();
109     
110     if (m_numProgressTrackedFrames == 0 || m_originatingProgressFrame == frame) {
111         reset();
112         m_progressValue = initialProgressValue;
113         m_originatingProgressFrame = frame;
114     
115         m_originatingProgressFrame->loader()->client()->postProgressStartedNotification();
116     }
117     m_numProgressTrackedFrames++;
118
119     frame->loader()->client()->didChangeEstimatedProgress();
120 }
121
122 void ProgressTracker::progressCompleted(Frame* frame)
123 {
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());
125     
126     if (m_numProgressTrackedFrames <= 0)
127         return;
128     
129     frame->loader()->client()->willChangeEstimatedProgress();
130         
131     m_numProgressTrackedFrames--;
132     if (!m_numProgressTrackedFrames || m_originatingProgressFrame == frame)
133         finalProgressComplete();
134     
135     frame->loader()->client()->didChangeEstimatedProgress();
136 }
137
138 void ProgressTracker::finalProgressComplete()
139 {
140     LOG(Progress, "Final progress complete (%p)", this);
141     
142     RefPtr<Frame> frame = m_originatingProgressFrame.release();
143     
144     // Before resetting progress value be sure to send client a least one notification
145     // with final progress value.
146     if (!m_finalProgressChangedSent) {
147         m_progressValue = 1;
148         frame->loader()->client()->postProgressEstimateChangedNotification();
149     }
150
151     reset();
152
153     frame->loader()->client()->setMainFrameDocumentReady(true);
154     frame->loader()->client()->postProgressFinishedNotification();
155 }
156
157 void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
158 {
159     LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d, originating frame %p", this, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
160
161     if (m_numProgressTrackedFrames <= 0)
162         return;
163     
164     long long estimatedLength = response.expectedContentLength();
165     if (estimatedLength < 0)
166         estimatedLength = progressItemDefaultEstimatedLength;
167     
168     m_totalPageAndResourceBytesToLoad += estimatedLength;
169
170     if (ProgressItem* item = m_progressItems.get(identifier)) {
171         item->bytesReceived = 0;
172         item->estimatedLength = estimatedLength;
173     } else
174         m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLength)).leakPtr());
175 }
176
177 void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
178 {
179     ProgressItem* item = m_progressItems.get(identifier);
180     
181     // FIXME: Can this ever happen?
182     if (!item)
183         return;
184
185     RefPtr<Frame> frame = m_originatingProgressFrame;
186     
187     frame->loader()->client()->willChangeEstimatedProgress();
188     
189     unsigned bytesReceived = length;
190     double increment, percentOfRemainingBytes;
191     long long remainingBytes, estimatedBytesForPendingRequests;
192     
193     item->bytesReceived += bytesReceived;
194     if (item->bytesReceived > item->estimatedLength) {
195         m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
196         item->estimatedLength = item->bytesReceived * 2;
197     }
198     
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;
204     else
205         percentOfRemainingBytes = 1.0;
206     
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);
216     
217     m_totalBytesReceived += bytesReceived;
218     
219     double now = currentTime();
220     double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
221     
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;
230             
231             frame->loader()->client()->postProgressEstimateChangedNotification();
232
233             m_lastNotifiedProgressValue = m_progressValue;
234             m_lastNotifiedProgressTime = now;
235         }
236     }
237     
238     frame->loader()->client()->didChangeEstimatedProgress();
239 }
240
241 void ProgressTracker::completeProgress(unsigned long identifier)
242 {
243     ProgressItem* item = m_progressItems.get(identifier);
244     
245     // This can happen if a load fails without receiving any response data.
246     if (!item)
247         return;
248     
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;
253     
254     m_progressItems.remove(identifier);
255     delete item;
256 }
257
258 unsigned long ProgressTracker::createUniqueIdentifier()
259 {
260     return ++s_uniqueIdentifier;
261 }
262
263
264 }