Chromium Code Reviews| 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 = 128 * 1024; |
| 50 // until we're done. | |
| 51 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue | |
| 52 | |
| 53 static const int progressItemDefaultEstimatedLength = 1024 * 1024; | |
| 54 | 51 |
| 55 struct ProgressItem { | 52 struct ProgressItem { |
| 56 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); | 53 WTF_MAKE_NONCOPYABLE(ProgressItem); USING_FAST_MALLOC(ProgressItem); |
| 57 public: | 54 public: |
| 58 ProgressItem(long long length) | 55 ProgressItem(long long length) |
| 59 : bytesReceived(0) | 56 : bytesReceived(0) |
| 60 , estimatedLength(length) { } | 57 , estimatedLength(length) { } |
| 61 | 58 |
| 62 long long bytesReceived; | 59 long long bytesReceived; |
| 63 long long estimatedLength; | 60 long long estimatedLength; |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 RawPtr<ProgressTracker> ProgressTracker::create(LocalFrame* frame) | 63 RawPtr<ProgressTracker> ProgressTracker::create(LocalFrame* frame) |
| 67 { | 64 { |
| 68 return new ProgressTracker(frame); | 65 return new ProgressTracker(frame); |
| 69 } | 66 } |
| 70 | 67 |
| 71 ProgressTracker::ProgressTracker(LocalFrame* frame) | 68 ProgressTracker::ProgressTracker(LocalFrame* frame) |
| 72 : m_frame(frame) | 69 : m_frame(frame) |
| 73 , m_mainResourceIdentifier(0) | |
| 74 , m_totalPageAndResourceBytesToLoad(0) | |
| 75 , m_totalBytesReceived(0) | |
| 76 , m_lastNotifiedProgressValue(0) | 70 , m_lastNotifiedProgressValue(0) |
| 77 , m_lastNotifiedProgressTime(0) | 71 , m_lastNotifiedProgressTime(0) |
| 78 , m_progressNotificationInterval(0.02) | 72 , m_progressNotificationInterval(0.02) |
| 79 , m_progressNotificationTimeInterval(0.1) | 73 , m_progressNotificationTimeInterval(0.1) |
| 74 , m_finishedParsing(false) | |
| 80 , m_finalProgressChangedSent(false) | 75 , m_finalProgressChangedSent(false) |
| 81 , m_progressValue(0) | 76 , m_progressValue(0) |
| 77 , m_bytesLoadedSoFar(0) | |
| 82 { | 78 { |
| 83 } | 79 } |
| 84 | 80 |
| 85 ProgressTracker::~ProgressTracker() | 81 ProgressTracker::~ProgressTracker() |
| 86 { | 82 { |
| 87 } | 83 } |
| 88 | 84 |
| 89 DEFINE_TRACE(ProgressTracker) | 85 DEFINE_TRACE(ProgressTracker) |
| 90 { | 86 { |
| 91 visitor->trace(m_frame); | 87 visitor->trace(m_frame); |
| 92 } | 88 } |
| 93 | 89 |
| 94 void ProgressTracker::dispose() | 90 void ProgressTracker::dispose() |
| 95 { | 91 { |
| 96 if (m_frame->isLoading()) | 92 if (m_frame->isLoading()) |
| 97 progressCompleted(); | 93 progressCompleted(); |
| 98 ASSERT(!m_frame->isLoading()); | 94 ASSERT(!m_frame->isLoading()); |
| 99 } | 95 } |
| 100 | 96 |
| 101 double ProgressTracker::estimatedProgress() const | 97 double ProgressTracker::estimatedProgress() const |
| 102 { | 98 { |
| 103 return m_progressValue; | 99 return m_progressValue; |
| 104 } | 100 } |
| 105 | 101 |
| 106 void ProgressTracker::reset() | 102 void ProgressTracker::reset() |
| 107 { | 103 { |
| 108 m_progressItems.clear(); | 104 m_progressItems.clear(); |
| 109 | |
| 110 m_totalPageAndResourceBytesToLoad = 0; | |
| 111 m_totalBytesReceived = 0; | |
| 112 m_progressValue = 0; | 105 m_progressValue = 0; |
| 113 m_lastNotifiedProgressValue = 0; | 106 m_lastNotifiedProgressValue = 0; |
| 114 m_lastNotifiedProgressTime = 0; | 107 m_lastNotifiedProgressTime = 0; |
| 115 m_finalProgressChangedSent = false; | 108 m_finalProgressChangedSent = false; |
| 109 m_finishedParsing = false; | |
| 110 m_bytesLoadedSoFar = 0; | |
| 111 m_progressUpdate.clear(); | |
| 112 m_bytesSoFarUpdate.clear(); | |
| 116 } | 113 } |
| 117 | 114 |
| 118 void ProgressTracker::progressStarted() | 115 void ProgressTracker::progressStarted() |
| 119 { | 116 { |
| 120 if (!m_frame->isLoading()) { | 117 if (!m_frame->isLoading()) { |
| 121 reset(); | 118 reset(); |
| 122 m_progressValue = initialProgressValue; | 119 m_progressValue = initialProgressValue; |
| 123 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen t); | 120 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen t); |
| 124 } | 121 } |
| 125 m_frame->setIsLoading(true); | 122 m_frame->setIsLoading(true); |
| 126 InspectorInstrumentation::frameStartedLoading(m_frame); | 123 InspectorInstrumentation::frameStartedLoading(m_frame); |
| 127 } | 124 } |
| 128 | 125 |
| 129 void ProgressTracker::progressCompleted() | 126 void ProgressTracker::progressCompleted() |
| 130 { | 127 { |
| 131 ASSERT(m_frame->isLoading()); | 128 ASSERT(m_frame->isLoading()); |
| 132 m_frame->setIsLoading(false); | 129 m_frame->setIsLoading(false); |
| 133 sendFinalProgress(); | 130 sendFinalProgress(); |
| 134 reset(); | 131 reset(); |
| 135 m_frame->loader().client()->didStopLoading(); | 132 m_frame->loader().client()->didStopLoading(); |
| 136 InspectorInstrumentation::frameStoppedLoading(m_frame); | 133 InspectorInstrumentation::frameStoppedLoading(m_frame); |
| 137 } | 134 } |
| 138 | 135 |
| 139 void ProgressTracker::finishedParsing() | 136 void ProgressTracker::finishedParsing() |
| 140 { | 137 { |
| 141 if (m_frame->settings()->mainResourceOnlyProgress()) | 138 m_finishedParsing = true; |
| 142 sendFinalProgress(); | 139 maybeSendProgress(); |
| 143 } | 140 } |
| 144 | 141 |
| 145 void ProgressTracker::sendFinalProgress() | 142 void ProgressTracker::sendFinalProgress() |
| 146 { | 143 { |
| 147 if (!m_finalProgressChangedSent) { | 144 if (!m_finalProgressChangedSent) { |
| 145 m_finalProgressChangedSent = true; | |
| 148 m_progressValue = 1; | 146 m_progressValue = 1; |
| 149 m_frame->loader().client()->progressEstimateChanged(m_progressValue); | 147 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
| 150 } | 148 } |
| 151 } | 149 } |
| 152 | 150 |
| 153 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource Response& response) | 151 static bool isParserBlockingOrImage(Resource::Type type) |
| 152 { | |
| 153 return type == Resource::MainResource || type == Resource::Image || type == Resource::CSSStyleSheet || type == Resource::Script || type == Resource::XSLStyl eSheet; | |
|
paulirish
2016/04/07 21:42:30
Mentioned this offline already,
but I think you'l
| |
| 154 } | |
| 155 | |
| 156 void ProgressTracker::willSendRequest(const Resource* resource) | |
| 154 { | 157 { |
| 155 if (!m_frame->isLoading()) | 158 if (!m_frame->isLoading()) |
| 156 return; | 159 return; |
| 160 if (m_finishedParsing || !isParserBlockingOrImage(resource->getType())) | |
| 161 return; | |
| 162 ASSERT(!m_progressItems.get(resource->identifier())); | |
| 163 m_progressItems.set(resource->identifier(), adoptPtr(new ProgressItem(progre ssItemDefaultEstimatedLength))); | |
| 164 } | |
| 157 | 165 |
| 158 if (m_frame->loader().provisionalDocumentLoader() && m_frame->loader().provi sionalDocumentLoader()->mainResourceIdentifier() == identifier) | 166 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource Response& response) |
| 159 m_mainResourceIdentifier = identifier; | 167 { |
| 168 ProgressItem* item = m_progressItems.get(identifier); | |
| 169 if (!item) | |
| 170 return; | |
| 160 | 171 |
| 161 long long estimatedLength = response.expectedContentLength(); | 172 long long estimatedLength = response.expectedContentLength(); |
| 162 if (estimatedLength < 0) | 173 if (estimatedLength < 0) |
| 163 estimatedLength = progressItemDefaultEstimatedLength; | 174 estimatedLength = progressItemDefaultEstimatedLength; |
| 164 | 175 item->bytesReceived = 0; |
| 165 m_totalPageAndResourceBytesToLoad += estimatedLength; | 176 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 } | |
| 174 | |
| 175 void ProgressTracker::incrementProgressForMainResourceOnly(unsigned long identif ier, int length) | |
| 176 { | |
| 177 if (identifier != m_mainResourceIdentifier) | |
| 178 return; | |
| 179 | |
| 180 ProgressItem* item = m_progressItems.get(identifier); | |
| 181 if (!item) | |
| 182 return; | |
| 183 | |
| 184 item->bytesReceived += length; | |
| 185 if (item->bytesReceived > item->estimatedLength) | |
| 186 item->estimatedLength *= 2; | |
| 187 double newProgress = initialProgressValue + 0.1; // +0.1 for committing | |
| 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 } | 177 } |
| 207 | 178 |
| 208 void ProgressTracker::incrementProgress(unsigned long identifier, int length) | 179 void ProgressTracker::incrementProgress(unsigned long identifier, int length) |
| 209 { | 180 { |
| 210 if (m_frame->settings()->mainResourceOnlyProgress()) { | |
| 211 incrementProgressForMainResourceOnly(identifier, length); | |
| 212 return; | |
| 213 } | |
| 214 | |
| 215 ProgressItem* item = m_progressItems.get(identifier); | 181 ProgressItem* item = m_progressItems.get(identifier); |
| 216 | 182 |
| 217 // FIXME: Can this ever happen? | 183 // FIXME: Can this ever happen? |
| 218 if (!item) | 184 if (!item) |
| 219 return; | 185 return; |
| 186 item->bytesReceived += length; | |
| 187 m_bytesLoadedSoFar += length; | |
| 188 if (item->bytesReceived > item->estimatedLength) | |
| 189 item->estimatedLength = item->bytesReceived * 2; | |
| 190 maybeSendProgress(); | |
| 191 } | |
| 220 | 192 |
| 221 unsigned bytesReceived = length; | 193 void ProgressTracker::maybeSendProgress() |
| 222 double increment, percentOfRemainingBytes; | 194 { |
| 223 long long remainingBytes, estimatedBytesForPendingRequests; | 195 m_progressValue = initialProgressValue + 0.1; // +0.1 for committing |
| 196 if (m_finishedParsing) | |
| 197 m_progressValue += 0.2; | |
| 224 | 198 |
| 225 item->bytesReceived += bytesReceived; | 199 long long bytesReceived = 0; |
| 226 if (item->bytesReceived > item->estimatedLength) { | 200 long long estimatedBytesForPendingRequests = 0; |
| 227 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item-> estimatedLength); | 201 for (const auto& progressItem : m_progressItems) { |
| 228 item->estimatedLength = item->bytesReceived * 2; | 202 bytesReceived += progressItem.value->bytesReceived; |
| 203 estimatedBytesForPendingRequests += progressItem.value->estimatedLength; | |
| 204 } | |
| 205 ASSERT(estimatedBytesForPendingRequests >= 0); | |
| 206 ASSERT(estimatedBytesForPendingRequests >= bytesReceived); | |
| 207 | |
| 208 if (m_finishedParsing && estimatedBytesForPendingRequests == bytesReceived) { | |
| 209 sendFinalProgress(); | |
| 210 return; | |
| 229 } | 211 } |
| 230 | 212 |
| 231 int numPendingOrLoadingRequests = m_frame->document()->fetcher()->requestCou nt(); | 213 double percentOfBytesReceived = !estimatedBytesForPendingRequests ? 1.0 : |
| 232 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numP endingOrLoadingRequests; | 214 (double)bytesReceived / (double)estimatedBytesForPendingRequests; |
| 233 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPend ingRequests) - m_totalBytesReceived); | 215 m_progressValue += percentOfBytesReceived / 2; |
| 234 if (remainingBytes > 0) // Prevent divide by 0. | |
| 235 percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes ; | |
| 236 else | |
| 237 percentOfRemainingBytes = 1.0; | |
| 238 | 216 |
| 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); | 217 ASSERT(m_progressValue >= initialProgressValue); |
| 246 | 218 // Always leave space at the end. This helps show the user that we're not |
| 247 m_totalBytesReceived += bytesReceived; | 219 // done until we're done. |
| 220 ASSERT(m_progressValue <= 0.9); | |
| 221 if (m_progressValue < m_lastNotifiedProgressValue) | |
| 222 return; | |
| 248 | 223 |
| 249 double now = currentTime(); | 224 double now = currentTime(); |
| 250 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; | 225 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; |
| 251 | 226 |
| 252 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV alue; | 227 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV alue; |
| 253 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP rogressTimeDelta >= m_progressNotificationTimeInterval) { | 228 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP rogressTimeDelta >= m_progressNotificationTimeInterval) { |
| 254 if (!m_finalProgressChangedSent) { | 229 ASSERT(!m_finalProgressChangedSent); |
| 255 if (m_progressValue == 1) | 230 m_frame->loader().client()->progressEstimateChanged(m_progressValue); |
| 256 m_finalProgressChangedSent = true; | 231 m_lastNotifiedProgressValue = m_progressValue; |
| 257 | 232 m_lastNotifiedProgressTime = now; |
| 258 m_frame->loader().client()->progressEstimateChanged(m_progressValue) ; | 233 m_progressUpdate.append(m_progressValue); |
| 259 | 234 m_bytesSoFarUpdate.append(m_bytesLoadedSoFar); |
| 260 m_lastNotifiedProgressValue = m_progressValue; | |
| 261 m_lastNotifiedProgressTime = now; | |
| 262 } | |
| 263 } | 235 } |
| 264 } | 236 } |
| 265 | 237 |
| 266 void ProgressTracker::completeProgress(unsigned long identifier) | 238 void ProgressTracker::completeProgress(unsigned long identifier) |
| 267 { | 239 { |
| 268 ProgressItem* item = m_progressItems.get(identifier); | 240 ProgressItem* item = m_progressItems.get(identifier); |
| 269 | 241 |
| 270 // This can happen if a load fails without receiving any response data. | 242 // This can happen if a load fails without receiving any response data. |
| 271 if (!item) | 243 if (!item) |
| 272 return; | 244 return; |
| 273 | 245 |
| 274 // Adjust the total expected bytes to account for any overage/underage. | 246 item->estimatedLength = item->bytesReceived; |
| 275 long long delta = item->bytesReceived - item->estimatedLength; | 247 maybeSendProgress(); |
| 276 m_totalPageAndResourceBytesToLoad += delta; | |
| 277 | |
| 278 m_progressItems.remove(identifier); | |
| 279 } | 248 } |
| 280 | 249 |
| 281 } // namespace blink | 250 } // namespace blink |
| OLD | NEW |