| 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/PtrUtil.h" | 40 #include "wtf/PtrUtil.h" |
| 40 #include "wtf/text/CString.h" | 41 #include "wtf/text/CString.h" |
| 41 | 42 |
| 42 using namespace std; | 43 using namespace std; |
| 43 | 44 |
| 44 namespace blink { | 45 namespace blink { |
| 45 | 46 |
| 46 // Always start progress at initialProgressValue. This helps provide feedback as | 47 // Always start progress at initialProgressValue. This helps provide feedback as |
| 47 // soon as a load starts. | 48 // soon as a load starts. |
| 48 static const double initialProgressValue = 0.1; | 49 static const double initialProgressValue = 0.1; |
| 49 | 50 |
| 50 // Similarly, always leave space at the end. This helps show the user that we're
not done | 51 static const int progressItemDefaultEstimatedLength = 1024 * 1024; |
| 51 // until we're done. | |
| 52 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue | |
| 53 | 52 |
| 54 static const int progressItemDefaultEstimatedLength = 1024 * 1024; | 53 static const double progressNotificationInterval = 0.02; |
| 54 static const double progressNotificationTimeInterval = 0.1; |
| 55 | 55 |
| 56 struct ProgressItem { | 56 struct ProgressItem { |
| 57 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); | 57 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); |
| 58 public: | 58 public: |
| 59 ProgressItem(long long length) | 59 ProgressItem(long long length) |
| 60 : bytesReceived(0) | 60 : bytesReceived(0) |
| 61 , estimatedLength(length) { } | 61 , estimatedLength(length) { } |
| 62 | 62 |
| 63 long long bytesReceived; | 63 long long bytesReceived; |
| 64 long long estimatedLength; | 64 long long estimatedLength; |
| 65 }; | 65 }; |
| 66 | 66 |
| 67 ProgressTracker* ProgressTracker::create(LocalFrame* frame) | 67 ProgressTracker* ProgressTracker::create(LocalFrame* frame) |
| 68 { | 68 { |
| 69 return new ProgressTracker(frame); | 69 return new ProgressTracker(frame); |
| 70 } | 70 } |
| 71 | 71 |
| 72 ProgressTracker::ProgressTracker(LocalFrame* frame) | 72 ProgressTracker::ProgressTracker(LocalFrame* frame) |
| 73 : m_frame(frame) | 73 : m_frame(frame) |
| 74 , m_mainResourceIdentifier(0) | |
| 75 , m_totalPageAndResourceBytesToLoad(0) | |
| 76 , m_totalBytesReceived(0) | |
| 77 , m_lastNotifiedProgressValue(0) | 74 , m_lastNotifiedProgressValue(0) |
| 78 , m_lastNotifiedProgressTime(0) | 75 , m_lastNotifiedProgressTime(0) |
| 79 , m_progressNotificationInterval(0.02) | 76 , m_finishedParsing(false) |
| 80 , m_progressNotificationTimeInterval(0.1) | |
| 81 , m_finalProgressChangedSent(false) | |
| 82 , m_progressValue(0) | 77 , m_progressValue(0) |
| 83 { | 78 { |
| 84 } | 79 } |
| 85 | 80 |
| 86 ProgressTracker::~ProgressTracker() | 81 ProgressTracker::~ProgressTracker() |
| 87 { | 82 { |
| 88 } | 83 } |
| 89 | 84 |
| 90 DEFINE_TRACE(ProgressTracker) | 85 DEFINE_TRACE(ProgressTracker) |
| 91 { | 86 { |
| 92 visitor->trace(m_frame); | 87 visitor->trace(m_frame); |
| 93 } | 88 } |
| 94 | 89 |
| 95 void ProgressTracker::dispose() | 90 void ProgressTracker::dispose() |
| 96 { | 91 { |
| 97 if (m_frame->isLoading()) | 92 if (m_frame->isLoading()) |
| 98 progressCompleted(); | 93 progressCompleted(); |
| 99 ASSERT(!m_frame->isLoading()); | 94 ASSERT(!m_frame->isLoading()); |
| 100 } | 95 } |
| 101 | 96 |
| 102 double ProgressTracker::estimatedProgress() const | 97 double ProgressTracker::estimatedProgress() const |
| 103 { | 98 { |
| 104 return m_progressValue; | 99 return m_progressValue; |
| 105 } | 100 } |
| 106 | 101 |
| 107 void ProgressTracker::reset() | 102 void ProgressTracker::reset() |
| 108 { | 103 { |
| 109 m_progressItems.clear(); | 104 m_progressItems.clear(); |
| 110 | |
| 111 m_totalPageAndResourceBytesToLoad = 0; | |
| 112 m_totalBytesReceived = 0; | |
| 113 m_progressValue = 0; | 105 m_progressValue = 0; |
| 114 m_lastNotifiedProgressValue = 0; | 106 m_lastNotifiedProgressValue = 0; |
| 115 m_lastNotifiedProgressTime = 0; | 107 m_lastNotifiedProgressTime = 0; |
| 116 m_finalProgressChangedSent = false; | 108 m_finishedParsing = false; |
| 117 } | 109 } |
| 118 | 110 |
| 119 void ProgressTracker::progressStarted() | 111 void ProgressTracker::progressStarted() |
| 120 { | 112 { |
| 121 if (!m_frame->isLoading()) { | 113 if (!m_frame->isLoading()) |
| 122 reset(); | |
| 123 m_progressValue = initialProgressValue; | |
| 124 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen
t); | 114 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen
t); |
| 125 } | 115 reset(); |
| 116 m_progressValue = initialProgressValue; |
| 126 m_frame->setIsLoading(true); | 117 m_frame->setIsLoading(true); |
| 127 InspectorInstrumentation::frameStartedLoading(m_frame); | 118 InspectorInstrumentation::frameStartedLoading(m_frame); |
| 128 } | 119 } |
| 129 | 120 |
| 130 void ProgressTracker::progressCompleted() | 121 void ProgressTracker::progressCompleted() |
| 131 { | 122 { |
| 132 ASSERT(m_frame->isLoading()); | 123 ASSERT(m_frame->isLoading()); |
| 133 m_frame->setIsLoading(false); | 124 m_frame->setIsLoading(false); |
| 134 sendFinalProgress(); | 125 sendFinalProgress(); |
| 135 reset(); | 126 reset(); |
| 136 m_frame->loader().client()->didStopLoading(); | 127 m_frame->loader().client()->didStopLoading(); |
| 137 InspectorInstrumentation::frameStoppedLoading(m_frame); | 128 InspectorInstrumentation::frameStoppedLoading(m_frame); |
| 138 } | 129 } |
| 139 | 130 |
| 140 void ProgressTracker::finishedParsing() | 131 void ProgressTracker::finishedParsing() |
| 141 { | 132 { |
| 142 if (m_frame->settings()->mainResourceOnlyProgress()) | 133 m_finishedParsing = true; |
| 143 sendFinalProgress(); | 134 maybeSendProgress(); |
| 144 } | 135 } |
| 145 | 136 |
| 146 void ProgressTracker::sendFinalProgress() | 137 void ProgressTracker::sendFinalProgress() |
| 147 { | 138 { |
| 148 if (!m_finalProgressChangedSent) { | 139 if (m_progressValue == 1) |
| 149 m_progressValue = 1; | 140 return; |
| 150 m_frame->loader().client()->progressEstimateChanged(m_progressValue); | 141 m_progressValue = 1; |
| 151 } | 142 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
| 143 } |
| 144 |
| 145 void ProgressTracker::willStartLoading(unsigned long identifier) |
| 146 { |
| 147 if (!m_frame->isLoading()) |
| 148 return; |
| 149 // All of the progress bar completion policies besides LoadEvent instead blo
ck on parsing |
| 150 // completion, which corresponds to finishing parsing. For those policies, d
on't consider |
| 151 // resource load that start after DOMContentLoaded finishes. |
| 152 if (m_frame->settings()->progressBarCompletion() != ProgressBarCompletion::L
oadEvent && m_finishedParsing) |
| 153 return; |
| 154 DCHECK(!m_progressItems.get(identifier)); |
| 155 m_progressItems.set(identifier, wrapUnique(new ProgressItem(progressItemDefa
ultEstimatedLength))); |
| 152 } | 156 } |
| 153 | 157 |
| 154 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource
Response& response) | 158 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource
Response& response) |
| 155 { | 159 { |
| 156 if (!m_frame->isLoading()) | 160 ProgressItem* item = m_progressItems.get(identifier); |
| 161 if (!item) |
| 157 return; | 162 return; |
| 158 | 163 |
| 159 if (m_frame->loader().provisionalDocumentLoader() && m_frame->loader().provi
sionalDocumentLoader()->mainResourceIdentifier() == identifier) | |
| 160 m_mainResourceIdentifier = identifier; | |
| 161 | |
| 162 long long estimatedLength = response.expectedContentLength(); | 164 long long estimatedLength = response.expectedContentLength(); |
| 163 if (estimatedLength < 0) | 165 if (estimatedLength < 0) |
| 164 estimatedLength = progressItemDefaultEstimatedLength; | 166 estimatedLength = progressItemDefaultEstimatedLength; |
| 165 | 167 item->bytesReceived = 0; |
| 166 m_totalPageAndResourceBytesToLoad += estimatedLength; | 168 item->estimatedLength = estimatedLength; |
| 167 | |
| 168 if (ProgressItem* item = m_progressItems.get(identifier)) { | |
| 169 item->bytesReceived = 0; | |
| 170 item->estimatedLength = estimatedLength; | |
| 171 } else { | |
| 172 m_progressItems.set(identifier, wrapUnique(new ProgressItem(estimatedLen
gth))); | |
| 173 } | |
| 174 } | 169 } |
| 175 | 170 |
| 176 void ProgressTracker::incrementProgressForMainResourceOnly(unsigned long identif
ier, int length) | 171 void ProgressTracker::incrementProgress(unsigned long identifier, int length) |
| 177 { | 172 { |
| 178 if (identifier != m_mainResourceIdentifier) | |
| 179 return; | |
| 180 | |
| 181 ProgressItem* item = m_progressItems.get(identifier); | 173 ProgressItem* item = m_progressItems.get(identifier); |
| 182 if (!item) | 174 if (!item) |
| 183 return; | 175 return; |
| 184 | 176 |
| 185 item->bytesReceived += length; | 177 item->bytesReceived += length; |
| 186 if (item->bytesReceived > item->estimatedLength) | 178 if (item->bytesReceived > item->estimatedLength) |
| 187 item->estimatedLength *= 2; | 179 item->estimatedLength = item->bytesReceived * 2; |
| 188 double newProgress = initialProgressValue + 0.1; // +0.1 for committing | 180 maybeSendProgress(); |
| 189 if (m_frame->view()->didFirstLayout()) | |
| 190 newProgress += 0.2; | |
| 191 // 0.4 possible so far, allow 0.5 from bytes loaded, for a max of 0.9. | |
| 192 newProgress += ((double) item->bytesReceived / (double) item->estimatedLengt
h) / 2; | |
| 193 | |
| 194 if (newProgress < m_progressValue) | |
| 195 return; | |
| 196 | |
| 197 m_progressValue = newProgress; | |
| 198 double now = currentTime(); | |
| 199 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; | |
| 200 | |
| 201 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; | |
| 202 if (notificationProgressDelta < m_progressNotificationInterval && notifiedPr
ogressTimeDelta < m_progressNotificationTimeInterval) | |
| 203 return; | |
| 204 m_frame->loader().client()->progressEstimateChanged(m_progressValue); | |
| 205 m_lastNotifiedProgressValue = m_progressValue; | |
| 206 m_lastNotifiedProgressTime = now; | |
| 207 } | 181 } |
| 208 | 182 |
| 209 void ProgressTracker::incrementProgress(unsigned long identifier, int length) | 183 void ProgressTracker::maybeSendProgress() |
| 210 { | 184 { |
| 211 if (m_frame->settings()->mainResourceOnlyProgress()) { | 185 m_progressValue = initialProgressValue + 0.1; // +0.1 for committing |
| 212 incrementProgressForMainResourceOnly(identifier, length); | 186 if (m_finishedParsing) |
| 213 return; | 187 m_progressValue += 0.2; |
| 188 |
| 189 long long bytesReceived = 0; |
| 190 long long estimatedBytesForPendingRequests = 0; |
| 191 for (const auto& progressItem : m_progressItems) { |
| 192 bytesReceived += progressItem.value->bytesReceived; |
| 193 estimatedBytesForPendingRequests += progressItem.value->estimatedLength; |
| 194 } |
| 195 DCHECK_GE(estimatedBytesForPendingRequests, 0); |
| 196 DCHECK_GE(estimatedBytesForPendingRequests, bytesReceived); |
| 197 |
| 198 if (m_finishedParsing) { |
| 199 if (m_frame->settings()->progressBarCompletion() == ProgressBarCompletio
n::DOMContentLoaded) { |
| 200 sendFinalProgress(); |
| 201 return; |
| 202 } |
| 203 if (m_frame->settings()->progressBarCompletion() != ProgressBarCompletio
n::LoadEvent && estimatedBytesForPendingRequests == bytesReceived) { |
| 204 sendFinalProgress(); |
| 205 return; |
| 206 } |
| 214 } | 207 } |
| 215 | 208 |
| 216 ProgressItem* item = m_progressItems.get(identifier); | 209 double percentOfBytesReceived = !estimatedBytesForPendingRequests ? 1.0 : (d
ouble)bytesReceived / (double)estimatedBytesForPendingRequests; |
| 210 m_progressValue += percentOfBytesReceived / 2; |
| 217 | 211 |
| 218 // FIXME: Can this ever happen? | 212 DCHECK_GE(m_progressValue, initialProgressValue); |
| 219 if (!item) | 213 // Always leave space at the end. This helps show the user that we're not |
| 214 // done until we're done. |
| 215 DCHECK(m_progressValue <= 0.9); |
| 216 if (m_progressValue < m_lastNotifiedProgressValue) |
| 220 return; | 217 return; |
| 221 | 218 |
| 222 unsigned bytesReceived = length; | |
| 223 double increment, percentOfRemainingBytes; | |
| 224 long long remainingBytes, estimatedBytesForPendingRequests; | |
| 225 | |
| 226 item->bytesReceived += bytesReceived; | |
| 227 if (item->bytesReceived > item->estimatedLength) { | |
| 228 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->
estimatedLength); | |
| 229 item->estimatedLength = item->bytesReceived * 2; | |
| 230 } | |
| 231 | |
| 232 int numPendingOrLoadingRequests = m_frame->document()->fetcher()->requestCou
nt(); | |
| 233 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numP
endingOrLoadingRequests; | |
| 234 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPend
ingRequests) - m_totalBytesReceived); | |
| 235 if (remainingBytes > 0) // Prevent divide by 0. | |
| 236 percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes
; | |
| 237 else | |
| 238 percentOfRemainingBytes = 1.0; | |
| 239 | |
| 240 // For documents that use WebCore's layout system, treat first layout as the
half-way point. | |
| 241 bool useClampedMaxProgress = !m_frame->view()->didFirstLayout(); | |
| 242 double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue; | |
| 243 increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes; | |
| 244 m_progressValue += increment; | |
| 245 m_progressValue = min(m_progressValue, maxProgressValue); | |
| 246 ASSERT(m_progressValue >= initialProgressValue); | |
| 247 | |
| 248 m_totalBytesReceived += bytesReceived; | |
| 249 | |
| 250 double now = currentTime(); | 219 double now = currentTime(); |
| 251 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; | 220 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; |
| 252 | 221 |
| 253 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; | 222 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV
alue; |
| 254 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP
rogressTimeDelta >= m_progressNotificationTimeInterval) { | 223 if (notificationProgressDelta >= progressNotificationInterval || notifiedPro
gressTimeDelta >= progressNotificationTimeInterval) { |
| 255 if (!m_finalProgressChangedSent) { | 224 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
| 256 if (m_progressValue == 1) | 225 m_lastNotifiedProgressValue = m_progressValue; |
| 257 m_finalProgressChangedSent = true; | 226 m_lastNotifiedProgressTime = now; |
| 258 | |
| 259 m_frame->loader().client()->progressEstimateChanged(m_progressValue)
; | |
| 260 | |
| 261 m_lastNotifiedProgressValue = m_progressValue; | |
| 262 m_lastNotifiedProgressTime = now; | |
| 263 } | |
| 264 } | 227 } |
| 265 } | 228 } |
| 266 | 229 |
| 267 void ProgressTracker::completeProgress(unsigned long identifier) | 230 void ProgressTracker::completeProgress(unsigned long identifier) |
| 268 { | 231 { |
| 269 ProgressItem* item = m_progressItems.get(identifier); | 232 ProgressItem* item = m_progressItems.get(identifier); |
| 270 | |
| 271 // This can happen if a load fails without receiving any response data. | |
| 272 if (!item) | 233 if (!item) |
| 273 return; | 234 return; |
| 274 | 235 |
| 275 // Adjust the total expected bytes to account for any overage/underage. | 236 item->estimatedLength = item->bytesReceived; |
| 276 long long delta = item->bytesReceived - item->estimatedLength; | 237 maybeSendProgress(); |
| 277 m_totalPageAndResourceBytesToLoad += delta; | |
| 278 | |
| 279 m_progressItems.remove(identifier); | |
| 280 } | 238 } |
| 281 | 239 |
| 282 } // namespace blink | 240 } // namespace blink |
| OLD | NEW |