OSDN Git Service

Merge WebKit at r71558: Initial merge by git.
[android-x86/external-webkit.git] / 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 : Noncopyable {
54     ProgressItem(long long length) 
55         : bytesReceived(0)
56         , estimatedLength(length) { }
57     
58     long long bytesReceived;
59     long long estimatedLength;
60 };
61
62 unsigned long ProgressTracker::s_uniqueIdentifier = 0;
63
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)
72     , m_progressValue(0)
73     , m_numProgressTrackedFrames(0)
74 {
75 }
76
77 ProgressTracker::~ProgressTracker()
78 {
79     deleteAllValues(m_progressItems);
80 }
81
82 double ProgressTracker::estimatedProgress() const
83 {
84     return m_progressValue;
85 }
86
87 void ProgressTracker::reset()
88 {
89     deleteAllValues(m_progressItems);
90     m_progressItems.clear();    
91
92     m_totalPageAndResourceBytesToLoad = 0;
93     m_totalBytesReceived = 0;
94     m_progressValue = 0;
95     m_lastNotifiedProgressValue = 0;
96     m_lastNotifiedProgressTime = 0;
97     m_finalProgressChangedSent = false;
98     m_numProgressTrackedFrames = 0;
99     m_originatingProgressFrame = 0;
100 }
101
102 void ProgressTracker::progressStarted(Frame* frame)
103 {
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());
105
106     frame->loader()->client()->willChangeEstimatedProgress();
107     
108     if (m_numProgressTrackedFrames == 0 || m_originatingProgressFrame == frame) {
109         reset();
110         m_progressValue = initialProgressValue;
111         m_originatingProgressFrame = frame;
112     
113         m_originatingProgressFrame->loader()->client()->postProgressStartedNotification();
114     }
115     m_numProgressTrackedFrames++;
116
117     frame->loader()->client()->didChangeEstimatedProgress();
118 }
119
120 void ProgressTracker::progressCompleted(Frame* frame)
121 {
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());
123     
124     if (m_numProgressTrackedFrames <= 0)
125         return;
126     
127     frame->loader()->client()->willChangeEstimatedProgress();
128         
129     m_numProgressTrackedFrames--;
130     if (m_numProgressTrackedFrames == 0 ||
131         (frame == m_originatingProgressFrame && m_numProgressTrackedFrames != 0))
132         finalProgressComplete();
133     
134     frame->loader()->client()->didChangeEstimatedProgress();
135 }
136
137 void ProgressTracker::finalProgressComplete()
138 {
139     LOG(Progress, "Final progress complete (%p)", this);
140     
141     RefPtr<Frame> frame = m_originatingProgressFrame.release();
142     
143     // Before resetting progress value be sure to send client a least one notification
144     // with final progress value.
145     if (!m_finalProgressChangedSent) {
146         m_progressValue = 1;
147         frame->loader()->client()->postProgressEstimateChangedNotification();
148     }
149
150     reset();
151
152     frame->loader()->client()->setMainFrameDocumentReady(true);
153     frame->loader()->client()->postProgressFinishedNotification();
154 }
155
156 void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response)
157 {
158     LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d, originating frame %p", this, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get());
159
160     if (m_numProgressTrackedFrames <= 0)
161         return;
162     
163     long long estimatedLength = response.expectedContentLength();
164     if (estimatedLength < 0)
165         estimatedLength = progressItemDefaultEstimatedLength;
166     
167     m_totalPageAndResourceBytesToLoad += estimatedLength;
168
169     if (ProgressItem* item = m_progressItems.get(identifier)) {
170         item->bytesReceived = 0;
171         item->estimatedLength = estimatedLength;
172     } else
173         m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLength)).leakPtr());
174 }
175
176 void ProgressTracker::incrementProgress(unsigned long identifier, const char*, int length)
177 {
178     ProgressItem* item = m_progressItems.get(identifier);
179     
180     // FIXME: Can this ever happen?
181     if (!item)
182         return;
183
184     RefPtr<Frame> frame = m_originatingProgressFrame;
185     
186     frame->loader()->client()->willChangeEstimatedProgress();
187     
188     unsigned bytesReceived = length;
189     double increment, percentOfRemainingBytes;
190     long long remainingBytes, estimatedBytesForPendingRequests;
191     
192     item->bytesReceived += bytesReceived;
193     if (item->bytesReceived > item->estimatedLength) {
194         m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength);
195         item->estimatedLength = item->bytesReceived * 2;
196     }
197     
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;
203     else
204         percentOfRemainingBytes = 1.0;
205     
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);
215     
216     m_totalBytesReceived += bytesReceived;
217     
218     double now = currentTime();
219     double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
220     
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;
229             
230             frame->loader()->client()->postProgressEstimateChangedNotification();
231
232             m_lastNotifiedProgressValue = m_progressValue;
233             m_lastNotifiedProgressTime = now;
234         }
235     }
236     
237     frame->loader()->client()->didChangeEstimatedProgress();
238 }
239
240 void ProgressTracker::completeProgress(unsigned long identifier)
241 {
242     ProgressItem* item = m_progressItems.get(identifier);
243     
244     // FIXME: Can this happen?
245     if (!item)
246         return;
247     
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;
252     
253     m_progressItems.remove(identifier);
254     delete item;
255 }
256
257 unsigned long ProgressTracker::createUniqueIdentifier()
258 {
259     return ++s_uniqueIdentifier;
260 }
261
262
263 }