OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2007 Apple Inc. All rights reserved. | 2 * Copyright (C) 2007 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
11 * documentation and/or other materials provided with the distribution. | 11 * documentation and/or other materials provided with the distribution. |
12 * | 12 * |
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY | 13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY |
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR | 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR | 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR |
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | 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. | 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
24 */ | 24 */ |
25 | 25 |
26 #include "core/loader/ProgressTracker.h" | 26 #include "core/loader/ProgressTracker.h" |
27 | 27 |
| 28 #include "core/fetch/Resource.h" |
28 #include "core/fetch/ResourceFetcher.h" | 29 #include "core/fetch/ResourceFetcher.h" |
29 #include "core/frame/FrameView.h" | 30 #include "core/frame/FrameView.h" |
30 #include "core/frame/LocalFrame.h" | 31 #include "core/frame/LocalFrame.h" |
31 #include "core/frame/Settings.h" | 32 #include "core/frame/Settings.h" |
32 #include "core/inspector/InspectorInstrumentation.h" | 33 #include "core/inspector/InspectorInstrumentation.h" |
33 #include "core/loader/DocumentLoader.h" | 34 #include "core/loader/DocumentLoader.h" |
34 #include "core/loader/FrameLoader.h" | 35 #include "core/loader/FrameLoader.h" |
35 #include "core/loader/FrameLoaderClient.h" | 36 #include "core/loader/FrameLoaderClient.h" |
36 #include "platform/Logging.h" | 37 #include "platform/Logging.h" |
37 #include "platform/network/ResourceResponse.h" | 38 #include "platform/network/ResourceResponse.h" |
38 #include "wtf/CurrentTime.h" | 39 #include "wtf/CurrentTime.h" |
39 #include "wtf/text/CString.h" | 40 #include "wtf/text/CString.h" |
40 | 41 |
41 using namespace std; | 42 using namespace std; |
42 | 43 |
43 namespace blink { | 44 namespace blink { |
44 | 45 |
45 // Always start progress at initialProgressValue. This helps provide feedback as | 46 // Always start progress at initialProgressValue. This helps provide feedback as |
46 // soon as a load starts. | 47 // soon as a load starts. |
47 static const double initialProgressValue = 0.1; | 48 static const double initialProgressValue = 0.1; |
48 | 49 |
49 // Similarly, always leave space at the end. This helps show the user that we're
not done | 50 static const int progressItemDefaultEstimatedLength = 1024 * 1024; |
50 // until we're done. | |
51 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue | |
52 | 51 |
53 static const int progressItemDefaultEstimatedLength = 1024 * 1024; | 52 static const double progressNotificationInterval = 0.02; |
| 53 static const double progressNotificationTimeInterval = 0.1; |
54 | 54 |
55 struct ProgressItem { | 55 struct ProgressItem { |
56 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); | 56 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); |
57 public: | 57 public: |
58 ProgressItem(long long length) | 58 ProgressItem(long long length) |
59 : bytesReceived(0) | 59 : bytesReceived(0) |
60 , estimatedLength(length) { } | 60 , estimatedLength(length) { } |
61 | 61 |
62 long long bytesReceived; | 62 long long bytesReceived; |
63 long long estimatedLength; | 63 long long estimatedLength; |
64 }; | 64 }; |
65 | 65 |
66 ProgressTracker* ProgressTracker::create(LocalFrame* frame) | 66 ProgressTracker* ProgressTracker::create(LocalFrame* frame) |
67 { | 67 { |
68 return new ProgressTracker(frame); | 68 return new ProgressTracker(frame); |
69 } | 69 } |
70 | 70 |
71 ProgressTracker::ProgressTracker(LocalFrame* frame) | 71 ProgressTracker::ProgressTracker(LocalFrame* frame) |
72 : m_frame(frame) | 72 : m_frame(frame) |
73 , m_mainResourceIdentifier(0) | |
74 , m_totalPageAndResourceBytesToLoad(0) | |
75 , m_totalBytesReceived(0) | |
76 , m_lastNotifiedProgressValue(0) | 73 , m_lastNotifiedProgressValue(0) |
77 , m_lastNotifiedProgressTime(0) | 74 , m_lastNotifiedProgressTime(0) |
78 , m_progressNotificationInterval(0.02) | 75 , m_finishedParsing(false) |
79 , m_progressNotificationTimeInterval(0.1) | |
80 , m_finalProgressChangedSent(false) | |
81 , m_progressValue(0) | 76 , m_progressValue(0) |
82 { | 77 { |
83 } | 78 } |
84 | 79 |
85 ProgressTracker::~ProgressTracker() | 80 ProgressTracker::~ProgressTracker() |
86 { | 81 { |
87 } | 82 } |
88 | 83 |
89 DEFINE_TRACE(ProgressTracker) | 84 DEFINE_TRACE(ProgressTracker) |
90 { | 85 { |
91 visitor->trace(m_frame); | 86 visitor->trace(m_frame); |
92 } | 87 } |
93 | 88 |
94 void ProgressTracker::dispose() | 89 void ProgressTracker::dispose() |
95 { | 90 { |
96 if (m_frame->isLoading()) | 91 if (m_frame->isLoading()) |
97 progressCompleted(); | 92 progressCompleted(); |
98 ASSERT(!m_frame->isLoading()); | 93 ASSERT(!m_frame->isLoading()); |
99 } | 94 } |
100 | 95 |
101 double ProgressTracker::estimatedProgress() const | 96 double ProgressTracker::estimatedProgress() const |
102 { | 97 { |
103 return m_progressValue; | 98 return m_progressValue; |
104 } | 99 } |
105 | 100 |
106 void ProgressTracker::reset() | 101 void ProgressTracker::reset() |
107 { | 102 { |
108 m_progressItems.clear(); | 103 m_progressItems.clear(); |
109 | |
110 m_totalPageAndResourceBytesToLoad = 0; | |
111 m_totalBytesReceived = 0; | |
112 m_progressValue = 0; | 104 m_progressValue = 0; |
113 m_lastNotifiedProgressValue = 0; | 105 m_lastNotifiedProgressValue = 0; |
114 m_lastNotifiedProgressTime = 0; | 106 m_lastNotifiedProgressTime = 0; |
115 m_finalProgressChangedSent = false; | 107 m_finishedParsing = false; |
116 } | 108 } |
117 | 109 |
118 void ProgressTracker::progressStarted() | 110 void ProgressTracker::progressStarted() |
119 { | 111 { |
120 if (!m_frame->isLoading()) { | 112 if (!m_frame->isLoading()) |
121 reset(); | |
122 m_progressValue = initialProgressValue; | |
123 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen
t); | 113 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen
t); |
124 } | 114 reset(); |
| 115 m_progressValue = initialProgressValue; |
125 m_frame->setIsLoading(true); | 116 m_frame->setIsLoading(true); |
126 InspectorInstrumentation::frameStartedLoading(m_frame); | 117 InspectorInstrumentation::frameStartedLoading(m_frame); |
127 } | 118 } |
128 | 119 |
129 void ProgressTracker::progressCompleted() | 120 void ProgressTracker::progressCompleted() |
130 { | 121 { |
131 ASSERT(m_frame->isLoading()); | 122 ASSERT(m_frame->isLoading()); |
132 m_frame->setIsLoading(false); | 123 m_frame->setIsLoading(false); |
133 sendFinalProgress(); | 124 sendFinalProgress(); |
134 reset(); | 125 reset(); |
135 m_frame->loader().client()->didStopLoading(); | 126 m_frame->loader().client()->didStopLoading(); |
136 InspectorInstrumentation::frameStoppedLoading(m_frame); | 127 InspectorInstrumentation::frameStoppedLoading(m_frame); |
137 } | 128 } |
138 | 129 |
139 void ProgressTracker::finishedParsing() | 130 void ProgressTracker::finishedParsing() |
140 { | 131 { |
141 if (m_frame->settings()->mainResourceOnlyProgress()) | 132 m_finishedParsing = true; |
142 sendFinalProgress(); | 133 maybeSendProgress(); |
143 } | 134 } |
144 | 135 |
145 void ProgressTracker::sendFinalProgress() | 136 void ProgressTracker::sendFinalProgress() |
146 { | 137 { |
147 if (!m_finalProgressChangedSent) { | 138 if (m_progressValue == 1) |
148 m_progressValue = 1; | 139 return; |
149 m_frame->loader().client()->progressEstimateChanged(m_progressValue); | 140 m_progressValue = 1; |
150 } | 141 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
| 142 } |
| 143 |
| 144 void ProgressTracker::willStartLoading(unsigned long identifier) |
| 145 { |
| 146 if (!m_frame->isLoading()) |
| 147 return; |
| 148 // All of the progress bar completion policies besides LoadEvent instead blo
ck on parsing |
| 149 // completion, which corresponds to finishing parsing. For those policies, d
on't consider |
| 150 // resource load that start after DOMContentLoaded finishes. |
| 151 if (m_frame->settings()->progressBarCompletion() != ProgressBarCompletion::L
oadEvent && m_finishedParsing) |
| 152 return; |
| 153 DCHECK(!m_progressItems.get(identifier)); |
| 154 m_progressItems.set(identifier, adoptPtr(new ProgressItem(progressItemDefaul
tEstimatedLength))); |
151 } | 155 } |
152 | 156 |
153 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource
Response& response) | 157 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource
Response& response) |
154 { | 158 { |
155 if (!m_frame->isLoading()) | 159 ProgressItem* item = m_progressItems.get(identifier); |
| 160 if (!item) |
156 return; | 161 return; |
157 | 162 |
158 if (m_frame->loader().provisionalDocumentLoader() && m_frame->loader().provi
sionalDocumentLoader()->mainResourceIdentifier() == identifier) | |
159 m_mainResourceIdentifier = identifier; | |
160 | |
161 long long estimatedLength = response.expectedContentLength(); | 163 long long estimatedLength = response.expectedContentLength(); |
162 if (estimatedLength < 0) | 164 if (estimatedLength < 0) |
163 estimatedLength = progressItemDefaultEstimatedLength; | 165 estimatedLength = progressItemDefaultEstimatedLength; |
164 | 166 item->bytesReceived = 0; |
165 m_totalPageAndResourceBytesToLoad += estimatedLength; | 167 item->estimatedLength = estimatedLength; |
166 | |
167 if (ProgressItem* item = m_progressItems.get(identifier)) { | |
168 item->bytesReceived = 0; | |
169 item->estimatedLength = estimatedLength; | |
170 } else { | |
171 m_progressItems.set(identifier, adoptPtr(new ProgressItem(estimatedLengt
h))); | |
172 } | |
173 } | 168 } |
174 | 169 |
175 void ProgressTracker::incrementProgressForMainResourceOnly(unsigned long identif
ier, int length) | 170 void ProgressTracker::incrementProgress(unsigned long identifier, int length) |
176 { | 171 { |
177 if (identifier != m_mainResourceIdentifier) | |
178 return; | |
179 | |
180 ProgressItem* item = m_progressItems.get(identifier); | 172 ProgressItem* item = m_progressItems.get(identifier); |
181 if (!item) | 173 if (!item) |
182 return; | 174 return; |
183 | 175 |
184 item->bytesReceived += length; | 176 item->bytesReceived += length; |
185 if (item->bytesReceived > item->estimatedLength) | 177 if (item->bytesReceived > item->estimatedLength) |
186 item->estimatedLength *= 2; | 178 item->estimatedLength = item->bytesReceived * 2; |
187 double newProgress = initialProgressValue + 0.1; // +0.1 for committing | 179 maybeSendProgress(); |
188 if (m_frame->view()->didFirstLayout()) | |
189 newProgress += 0.2; | |
190 // 0.4 possible so far, allow 0.5 from bytes loaded, for a max of 0.9. | |
191 newProgress += ((double) item->bytesReceived / (double) item->estimatedLengt
h) / 2; | |
192 | |
193 if (newProgress < m_progressValue) | |
194 return; | |
195 | |
196 m_progressValue = newProgress; | |
197 double now = currentTime(); | |
198 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; | |
199 | |
200 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; | |
201 if (notificationProgressDelta < m_progressNotificationInterval && notifiedPr
ogressTimeDelta < m_progressNotificationTimeInterval) | |
202 return; | |
203 m_frame->loader().client()->progressEstimateChanged(m_progressValue); | |
204 m_lastNotifiedProgressValue = m_progressValue; | |
205 m_lastNotifiedProgressTime = now; | |
206 } | 180 } |
207 | 181 |
208 void ProgressTracker::incrementProgress(unsigned long identifier, int length) | 182 void ProgressTracker::maybeSendProgress() |
209 { | 183 { |
210 if (m_frame->settings()->mainResourceOnlyProgress()) { | 184 m_progressValue = initialProgressValue + 0.1; // +0.1 for committing |
211 incrementProgressForMainResourceOnly(identifier, length); | 185 if (m_finishedParsing) |
212 return; | 186 m_progressValue += 0.2; |
| 187 |
| 188 long long bytesReceived = 0; |
| 189 long long estimatedBytesForPendingRequests = 0; |
| 190 for (const auto& progressItem : m_progressItems) { |
| 191 bytesReceived += progressItem.value->bytesReceived; |
| 192 estimatedBytesForPendingRequests += progressItem.value->estimatedLength; |
| 193 } |
| 194 DCHECK_GE(estimatedBytesForPendingRequests, 0); |
| 195 DCHECK_GE(estimatedBytesForPendingRequests, bytesReceived); |
| 196 |
| 197 if (m_finishedParsing) { |
| 198 if (m_frame->settings()->progressBarCompletion() == ProgressBarCompletio
n::DOMContentLoaded) { |
| 199 sendFinalProgress(); |
| 200 return; |
| 201 } |
| 202 if (m_frame->settings()->progressBarCompletion() != ProgressBarCompletio
n::LoadEvent && estimatedBytesForPendingRequests == bytesReceived) { |
| 203 sendFinalProgress(); |
| 204 return; |
| 205 } |
213 } | 206 } |
214 | 207 |
215 ProgressItem* item = m_progressItems.get(identifier); | 208 double percentOfBytesReceived = !estimatedBytesForPendingRequests ? 1.0 : (d
ouble)bytesReceived / (double)estimatedBytesForPendingRequests; |
| 209 m_progressValue += percentOfBytesReceived / 2; |
216 | 210 |
217 // FIXME: Can this ever happen? | 211 DCHECK_GE(m_progressValue, initialProgressValue); |
218 if (!item) | 212 // Always leave space at the end. This helps show the user that we're not |
| 213 // done until we're done. |
| 214 DCHECK(m_progressValue <= 0.9); |
| 215 if (m_progressValue < m_lastNotifiedProgressValue) |
219 return; | 216 return; |
220 | 217 |
221 unsigned bytesReceived = length; | |
222 double increment, percentOfRemainingBytes; | |
223 long long remainingBytes, estimatedBytesForPendingRequests; | |
224 | |
225 item->bytesReceived += bytesReceived; | |
226 if (item->bytesReceived > item->estimatedLength) { | |
227 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->
estimatedLength); | |
228 item->estimatedLength = item->bytesReceived * 2; | |
229 } | |
230 | |
231 int numPendingOrLoadingRequests = m_frame->document()->fetcher()->requestCou
nt(); | |
232 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numP
endingOrLoadingRequests; | |
233 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPend
ingRequests) - m_totalBytesReceived); | |
234 if (remainingBytes > 0) // Prevent divide by 0. | |
235 percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes
; | |
236 else | |
237 percentOfRemainingBytes = 1.0; | |
238 | |
239 // For documents that use WebCore's layout system, treat first layout as the
half-way point. | |
240 bool useClampedMaxProgress = !m_frame->view()->didFirstLayout(); | |
241 double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue; | |
242 increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes; | |
243 m_progressValue += increment; | |
244 m_progressValue = min(m_progressValue, maxProgressValue); | |
245 ASSERT(m_progressValue >= initialProgressValue); | |
246 | |
247 m_totalBytesReceived += bytesReceived; | |
248 | |
249 double now = currentTime(); | 218 double now = currentTime(); |
250 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; | 219 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; |
251 | 220 |
252 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; | 221 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; |
253 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP
rogressTimeDelta >= m_progressNotificationTimeInterval) { | 222 if (notificationProgressDelta >= progressNotificationInterval || notifiedPro
gressTimeDelta >= progressNotificationTimeInterval) { |
254 if (!m_finalProgressChangedSent) { | 223 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
255 if (m_progressValue == 1) | 224 m_lastNotifiedProgressValue = m_progressValue; |
256 m_finalProgressChangedSent = true; | 225 m_lastNotifiedProgressTime = now; |
257 | |
258 m_frame->loader().client()->progressEstimateChanged(m_progressValue)
; | |
259 | |
260 m_lastNotifiedProgressValue = m_progressValue; | |
261 m_lastNotifiedProgressTime = now; | |
262 } | |
263 } | 226 } |
264 } | 227 } |
265 | 228 |
266 void ProgressTracker::completeProgress(unsigned long identifier) | 229 void ProgressTracker::completeProgress(unsigned long identifier) |
267 { | 230 { |
268 ProgressItem* item = m_progressItems.get(identifier); | 231 ProgressItem* item = m_progressItems.get(identifier); |
269 | |
270 // This can happen if a load fails without receiving any response data. | |
271 if (!item) | 232 if (!item) |
272 return; | 233 return; |
273 | 234 |
274 // Adjust the total expected bytes to account for any overage/underage. | 235 item->estimatedLength = item->bytesReceived; |
275 long long delta = item->bytesReceived - item->estimatedLength; | 236 maybeSendProgress(); |
276 m_totalPageAndResourceBytesToLoad += delta; | |
277 | |
278 m_progressItems.remove(identifier); | |
279 } | 237 } |
280 | 238 |
281 } // namespace blink | 239 } // namespace blink |
OLD | NEW |