Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(251)

Side by Side Diff: third_party/WebKit/Source/core/loader/ProgressTracker.cpp

Issue 1860743002: Add a flag to change when android's progress bar completes (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // until we're done.
51 static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue
52
53 static const int progressItemDefaultEstimatedLength = 1024 * 1024; 50 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 ProgressTracker* ProgressTracker::create(LocalFrame* frame) 63 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)
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_finalProgressChangedSent = false;
108 m_finishedParsing = false;
116 } 109 }
117 110
118 void ProgressTracker::progressStarted() 111 void ProgressTracker::progressStarted()
119 { 112 {
120 if (!m_frame->isLoading()) { 113 if (!m_frame->isLoading())
121 reset();
122 m_progressValue = initialProgressValue;
123 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen t); 114 m_frame->loader().client()->didStartLoading(NavigationToDifferentDocumen t);
124 } 115 reset();
116 m_progressValue = initialProgressValue;
125 m_frame->setIsLoading(true); 117 m_frame->setIsLoading(true);
126 InspectorInstrumentation::frameStartedLoading(m_frame); 118 InspectorInstrumentation::frameStartedLoading(m_frame);
127 } 119 }
128 120
129 void ProgressTracker::progressCompleted() 121 void ProgressTracker::progressCompleted()
130 { 122 {
131 ASSERT(m_frame->isLoading()); 123 ASSERT(m_frame->isLoading());
132 m_frame->setIsLoading(false); 124 m_frame->setIsLoading(false);
133 sendFinalProgress(); 125 sendFinalProgress();
134 reset(); 126 reset();
135 m_frame->loader().client()->didStopLoading(); 127 m_frame->loader().client()->didStopLoading();
136 InspectorInstrumentation::frameStoppedLoading(m_frame); 128 InspectorInstrumentation::frameStoppedLoading(m_frame);
137 } 129 }
138 130
139 void ProgressTracker::finishedParsing() 131 void ProgressTracker::finishedParsing()
140 { 132 {
141 if (m_frame->settings()->mainResourceOnlyProgress()) 133 m_finishedParsing = true;
142 sendFinalProgress(); 134 maybeSendProgress();
143 } 135 }
144 136
145 void ProgressTracker::sendFinalProgress() 137 void ProgressTracker::sendFinalProgress()
146 { 138 {
147 if (!m_finalProgressChangedSent) { 139 if (!m_finalProgressChangedSent) {
140 m_finalProgressChangedSent = true;
148 m_progressValue = 1; 141 m_progressValue = 1;
149 m_frame->loader().client()->progressEstimateChanged(m_progressValue); 142 m_frame->loader().client()->progressEstimateChanged(m_progressValue);
150 } 143 }
151 } 144 }
152 145
153 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource Response& response) 146 static bool isParserBlockingOrImage(Resource::Type type)
147 {
148 return type == Resource::MainResource || type == Resource::Image || type == Resource::CSSStyleSheet || type == Resource::Script || type == Resource::XSLStyl eSheet;
149 }
150
151 void ProgressTracker::willStartLoading(const Resource* resource)
154 { 152 {
155 if (!m_frame->isLoading()) 153 if (!m_frame->isLoading())
156 return; 154 return;
155 if (m_frame->settings()->progressBarCompletion() != ProgressBarCompletionLoa dEvent) {
156 if (m_finishedParsing || !isParserBlockingOrImage(resource->getType()) | | resource->resourceRequest().priority() < ResourceLoadPriorityMedium)
157 return;
158 }
159 ASSERT(!m_progressItems.get(resource->identifier()));
160 m_progressItems.set(resource->identifier(), adoptPtr(new ProgressItem(progre ssItemDefaultEstimatedLength)));
161 }
157 162
158 if (m_frame->loader().provisionalDocumentLoader() && m_frame->loader().provi sionalDocumentLoader()->mainResourceIdentifier() == identifier) 163 void ProgressTracker::incrementProgress(unsigned long identifier, const Resource Response& response)
159 m_mainResourceIdentifier = identifier; 164 {
165 ProgressItem* item = m_progressItems.get(identifier);
166 if (!item)
167 return;
160 168
161 long long estimatedLength = response.expectedContentLength(); 169 long long estimatedLength = response.expectedContentLength();
162 if (estimatedLength < 0) 170 if (estimatedLength < 0)
163 estimatedLength = progressItemDefaultEstimatedLength; 171 estimatedLength = progressItemDefaultEstimatedLength;
164 172 item->bytesReceived = 0;
165 m_totalPageAndResourceBytesToLoad += estimatedLength; 173 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 } 174 }
207 175
208 void ProgressTracker::incrementProgress(unsigned long identifier, int length) 176 void ProgressTracker::incrementProgress(unsigned long identifier, int length)
209 { 177 {
210 if (m_frame->settings()->mainResourceOnlyProgress()) {
211 incrementProgressForMainResourceOnly(identifier, length);
212 return;
213 }
214
215 ProgressItem* item = m_progressItems.get(identifier); 178 ProgressItem* item = m_progressItems.get(identifier);
216 179
217 // FIXME: Can this ever happen? 180 // FIXME: Can this ever happen?
218 if (!item) 181 if (!item)
219 return; 182 return;
183 item->bytesReceived += length;
184 if (item->bytesReceived > item->estimatedLength)
185 item->estimatedLength = item->bytesReceived * 2;
186 maybeSendProgress();
187 }
220 188
221 unsigned bytesReceived = length; 189 void ProgressTracker::maybeSendProgress()
222 double increment, percentOfRemainingBytes; 190 {
223 long long remainingBytes, estimatedBytesForPendingRequests; 191 m_progressValue = initialProgressValue + 0.1; // +0.1 for committing
192 if (m_finishedParsing)
193 m_progressValue += 0.2;
224 194
225 item->bytesReceived += bytesReceived; 195 long long bytesReceived = 0;
226 if (item->bytesReceived > item->estimatedLength) { 196 long long estimatedBytesForPendingRequests = 0;
227 m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item-> estimatedLength); 197 for (const auto& progressItem : m_progressItems) {
228 item->estimatedLength = item->bytesReceived * 2; 198 bytesReceived += progressItem.value->bytesReceived;
199 estimatedBytesForPendingRequests += progressItem.value->estimatedLength;
200 }
201 ASSERT(estimatedBytesForPendingRequests >= 0);
202 ASSERT(estimatedBytesForPendingRequests >= bytesReceived);
203
204 if (m_finishedParsing) {
205 if (m_frame->settings()->progressBarCompletion() == ProgressBarCompletio nDOMContentLoaded) {
206 sendFinalProgress();
207 return;
208 }
209 if (m_frame->settings()->progressBarCompletion() == ProgressBarCompletio nDOMContentLoadedAndImage && estimatedBytesForPendingRequests == bytesReceived) {
dcheng 2016/05/10 06:28:50 Is exact equality here reliable?
Nate Chapin 2016/05/11 00:06:59 I believe so. If at any point incrementProgress()
210 sendFinalProgress();
211 return;
212 }
229 } 213 }
230 214
231 int numPendingOrLoadingRequests = m_frame->document()->fetcher()->requestCou nt(); 215 double percentOfBytesReceived = !estimatedBytesForPendingRequests ? 1.0 :
232 estimatedBytesForPendingRequests = progressItemDefaultEstimatedLength * numP endingOrLoadingRequests; 216 (double)bytesReceived / (double)estimatedBytesForPendingRequests;
233 remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPend ingRequests) - m_totalBytesReceived); 217 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 218
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); 219 ASSERT(m_progressValue >= initialProgressValue);
246 220 // Always leave space at the end. This helps show the user that we're not
247 m_totalBytesReceived += bytesReceived; 221 // done until we're done.
222 ASSERT(m_progressValue <= 0.9);
223 if (m_progressValue < m_lastNotifiedProgressValue)
224 return;
248 225
249 double now = currentTime(); 226 double now = currentTime();
250 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; 227 double notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime;
251 228
252 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV alue; 229 double notificationProgressDelta = m_progressValue - m_lastNotifiedProgressV alue;
253 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP rogressTimeDelta >= m_progressNotificationTimeInterval) { 230 if (notificationProgressDelta >= m_progressNotificationInterval || notifiedP rogressTimeDelta >= m_progressNotificationTimeInterval) {
254 if (!m_finalProgressChangedSent) { 231 ASSERT(!m_finalProgressChangedSent);
255 if (m_progressValue == 1) 232 m_frame->loader().client()->progressEstimateChanged(m_progressValue);
256 m_finalProgressChangedSent = true; 233 m_lastNotifiedProgressValue = m_progressValue;
257 234 m_lastNotifiedProgressTime = now;
258 m_frame->loader().client()->progressEstimateChanged(m_progressValue) ;
259
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
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698