1 | /* |
2 | * Copyright (C) 2007, 2018 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 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 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 "InspectorInstrumentation.h" |
35 | #include "Logging.h" |
36 | #include "ProgressTrackerClient.h" |
37 | #include "ResourceResponse.h" |
38 | #include <wtf/text/CString.h> |
39 | |
40 | #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(isAlwaysOnLoggingAllowed(), Network, "%p - ProgressTracker::" fmt, this, ##__VA_ARGS__) |
41 | |
42 | namespace WebCore { |
43 | |
44 | // Always start progress at initialProgressValue. This helps provide feedback as |
45 | // soon as a load starts. |
46 | static const double initialProgressValue = 0.1; |
47 | |
48 | // Similarly, always leave space at the end. This helps show the user that we're not done |
49 | // until we're done. |
50 | static const double finalProgressValue = 0.9; // 1.0 - initialProgressValue |
51 | |
52 | static const int progressItemDefaultEstimatedLength = 1024 * 16; |
53 | |
54 | // Check if the load is progressing this often. |
55 | static const Seconds progressHeartbeatInterval { 100_ms }; |
56 | |
57 | // How many heartbeats must pass without progress before deciding the load is currently stalled. |
58 | static const unsigned loadStalledHeartbeatCount = 4; |
59 | |
60 | // How many bytes are required between heartbeats to consider it progress. |
61 | static const unsigned minumumBytesPerHeartbeatForProgress = 1024; |
62 | |
63 | static const Seconds progressNotificationTimeInterval { 200_ms }; |
64 | |
65 | struct ProgressItem { |
66 | WTF_MAKE_NONCOPYABLE(ProgressItem); WTF_MAKE_FAST_ALLOCATED; |
67 | public: |
68 | ProgressItem(long long length) |
69 | : bytesReceived(0) |
70 | , estimatedLength(length) |
71 | { |
72 | } |
73 | |
74 | long long bytesReceived; |
75 | long long estimatedLength; |
76 | }; |
77 | |
78 | unsigned long ProgressTracker::s_uniqueIdentifier = 0; |
79 | |
80 | ProgressTracker::ProgressTracker(ProgressTrackerClient& client) |
81 | : m_client(client) |
82 | , m_progressHeartbeatTimer(*this, &ProgressTracker::progressHeartbeatTimerFired) |
83 | { |
84 | } |
85 | |
86 | ProgressTracker::~ProgressTracker() |
87 | { |
88 | m_client.progressTrackerDestroyed(); |
89 | } |
90 | |
91 | double ProgressTracker::estimatedProgress() const |
92 | { |
93 | return m_progressValue; |
94 | } |
95 | |
96 | void ProgressTracker::reset() |
97 | { |
98 | m_progressItems.clear(); |
99 | |
100 | m_totalPageAndResourceBytesToLoad = 0; |
101 | m_totalBytesReceived = 0; |
102 | m_totalBytesReceivedBeforePreviousHeartbeat = 0; |
103 | |
104 | m_lastNotifiedProgressValue = 0; |
105 | m_progressValue = 0; |
106 | |
107 | m_lastNotifiedProgressTime = MonotonicTime(); |
108 | m_finalProgressChangedSent = false; |
109 | m_numProgressTrackedFrames = 0; |
110 | m_originatingProgressFrame = nullptr; |
111 | |
112 | m_heartbeatsWithNoProgress = 0; |
113 | m_progressHeartbeatTimer.stop(); |
114 | } |
115 | |
116 | void ProgressTracker::progressStarted(Frame& frame) |
117 | { |
118 | 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()); |
119 | |
120 | m_client.willChangeEstimatedProgress(); |
121 | |
122 | if (!m_numProgressTrackedFrames || m_originatingProgressFrame == &frame) { |
123 | reset(); |
124 | m_progressValue = initialProgressValue; |
125 | m_originatingProgressFrame = &frame; |
126 | |
127 | m_progressHeartbeatTimer.startRepeating(progressHeartbeatInterval); |
128 | m_originatingProgressFrame->loader().loadProgressingStatusChanged(); |
129 | |
130 | bool isMainFrame = !m_originatingProgressFrame->tree().parent(); |
131 | auto elapsedTimeSinceMainLoadComplete = MonotonicTime::now() - m_mainLoadCompletionTime; |
132 | |
133 | static const auto subframePartOfMainLoadThreshold = 1_s; |
134 | m_isMainLoad = isMainFrame || elapsedTimeSinceMainLoadComplete < subframePartOfMainLoadThreshold; |
135 | |
136 | m_client.progressStarted(*m_originatingProgressFrame); |
137 | } |
138 | m_numProgressTrackedFrames++; |
139 | |
140 | RELEASE_LOG_IF_ALLOWED("progressStarted: frame %p, value %f, tracked frames %d, originating frame %p, isMainLoad %d" , &frame, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get(), m_isMainLoad); |
141 | |
142 | m_client.didChangeEstimatedProgress(); |
143 | InspectorInstrumentation::frameStartedLoading(frame); |
144 | } |
145 | |
146 | void ProgressTracker::progressCompleted(Frame& frame) |
147 | { |
148 | 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()); |
149 | RELEASE_LOG_IF_ALLOWED("progressCompleted: frame %p, value %f, tracked frames %d, originating frame %p, isMainLoad %d" , &frame, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get(), m_isMainLoad); |
150 | |
151 | if (m_numProgressTrackedFrames <= 0) |
152 | return; |
153 | |
154 | m_client.willChangeEstimatedProgress(); |
155 | |
156 | m_numProgressTrackedFrames--; |
157 | if (!m_numProgressTrackedFrames || m_originatingProgressFrame == &frame) |
158 | finalProgressComplete(); |
159 | |
160 | m_client.didChangeEstimatedProgress(); |
161 | } |
162 | |
163 | void ProgressTracker::finalProgressComplete() |
164 | { |
165 | LOG(Progress, "Final progress complete (%p)" , this); |
166 | RELEASE_LOG_IF_ALLOWED("finalProgressComplete: value %f, tracked frames %d, originating frame %p, isMainLoad %d, isMainLoadProgressing %d" , m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get(), m_isMainLoad, isMainLoadProgressing()); |
167 | |
168 | auto frame = WTFMove(m_originatingProgressFrame); |
169 | |
170 | // Before resetting progress value be sure to send client a least one notification |
171 | // with final progress value. |
172 | if (!m_finalProgressChangedSent) { |
173 | m_progressValue = 1; |
174 | m_client.progressEstimateChanged(*frame); |
175 | } |
176 | |
177 | reset(); |
178 | |
179 | if (m_isMainLoad) |
180 | m_mainLoadCompletionTime = MonotonicTime::now(); |
181 | |
182 | frame->loader().client().setMainFrameDocumentReady(true); |
183 | m_client.progressFinished(*frame); |
184 | frame->loader().loadProgressingStatusChanged(); |
185 | |
186 | InspectorInstrumentation::frameStoppedLoading(*frame); |
187 | } |
188 | |
189 | void ProgressTracker::incrementProgress(unsigned long identifier, const ResourceResponse& response) |
190 | { |
191 | LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d, originating frame %p" , this, m_progressValue, m_numProgressTrackedFrames, m_originatingProgressFrame.get()); |
192 | |
193 | if (m_numProgressTrackedFrames <= 0) |
194 | return; |
195 | |
196 | long long estimatedLength = response.expectedContentLength(); |
197 | if (estimatedLength < 0) |
198 | estimatedLength = progressItemDefaultEstimatedLength; |
199 | |
200 | m_totalPageAndResourceBytesToLoad += estimatedLength; |
201 | |
202 | auto& item = m_progressItems.add(identifier, nullptr).iterator->value; |
203 | if (!item) { |
204 | item = std::make_unique<ProgressItem>(estimatedLength); |
205 | return; |
206 | } |
207 | |
208 | item->bytesReceived = 0; |
209 | item->estimatedLength = estimatedLength; |
210 | } |
211 | |
212 | void ProgressTracker::incrementProgress(unsigned long identifier, unsigned bytesReceived) |
213 | { |
214 | ProgressItem* item = m_progressItems.get(identifier); |
215 | |
216 | // FIXME: Can this ever happen? |
217 | if (!item) |
218 | return; |
219 | |
220 | RefPtr<Frame> frame = m_originatingProgressFrame; |
221 | |
222 | m_client.willChangeEstimatedProgress(); |
223 | |
224 | double increment, percentOfRemainingBytes; |
225 | long long remainingBytes, estimatedBytesForPendingRequests; |
226 | |
227 | item->bytesReceived += bytesReceived; |
228 | if (item->bytesReceived > item->estimatedLength) { |
229 | m_totalPageAndResourceBytesToLoad += ((item->bytesReceived * 2) - item->estimatedLength); |
230 | item->estimatedLength = item->bytesReceived * 2; |
231 | } |
232 | |
233 | int numPendingOrLoadingRequests = frame->loader().numPendingOrLoadingRequests(true); |
234 | estimatedBytesForPendingRequests = static_cast<long long>(progressItemDefaultEstimatedLength) * numPendingOrLoadingRequests; |
235 | remainingBytes = ((m_totalPageAndResourceBytesToLoad + estimatedBytesForPendingRequests) - m_totalBytesReceived); |
236 | if (remainingBytes > 0) // Prevent divide by 0. |
237 | percentOfRemainingBytes = (double)bytesReceived / (double)remainingBytes; |
238 | else |
239 | percentOfRemainingBytes = 1.0; |
240 | |
241 | // For documents that use WebCore's layout system, treat first layout as the half-way point. |
242 | // FIXME: The hasHTMLView function is a sort of roundabout way of asking "do you use WebCore's layout system". |
243 | bool useClampedMaxProgress = frame->loader().client().hasHTMLView() |
244 | && !frame->loader().stateMachine().firstLayoutDone(); |
245 | double maxProgressValue = useClampedMaxProgress ? 0.5 : finalProgressValue; |
246 | increment = (maxProgressValue - m_progressValue) * percentOfRemainingBytes; |
247 | m_progressValue += increment; |
248 | m_progressValue = std::min(m_progressValue, maxProgressValue); |
249 | ASSERT(m_progressValue >= initialProgressValue); |
250 | |
251 | m_totalBytesReceived += bytesReceived; |
252 | |
253 | auto now = MonotonicTime::now(); |
254 | auto notifiedProgressTimeDelta = now - m_lastNotifiedProgressTime; |
255 | |
256 | LOG(Progress, "Progress incremented (%p) - value %f, tracked frames %d" , this, m_progressValue, m_numProgressTrackedFrames); |
257 | if ((notifiedProgressTimeDelta >= progressNotificationTimeInterval || m_progressValue == 1) && m_numProgressTrackedFrames > 0) { |
258 | if (!m_finalProgressChangedSent) { |
259 | if (m_progressValue == 1) |
260 | m_finalProgressChangedSent = true; |
261 | |
262 | m_client.progressEstimateChanged(*frame); |
263 | |
264 | m_lastNotifiedProgressValue = m_progressValue; |
265 | m_lastNotifiedProgressTime = now; |
266 | } |
267 | } |
268 | |
269 | m_client.didChangeEstimatedProgress(); |
270 | } |
271 | |
272 | void ProgressTracker::completeProgress(unsigned long identifier) |
273 | { |
274 | auto it = m_progressItems.find(identifier); |
275 | |
276 | // This can happen if a load fails without receiving any response data. |
277 | if (it == m_progressItems.end()) |
278 | return; |
279 | |
280 | ProgressItem& item = *it->value; |
281 | |
282 | // Adjust the total expected bytes to account for any overage/underage. |
283 | long long delta = item.bytesReceived - item.estimatedLength; |
284 | m_totalPageAndResourceBytesToLoad += delta; |
285 | |
286 | m_progressItems.remove(it); |
287 | } |
288 | |
289 | unsigned long ProgressTracker::createUniqueIdentifier() |
290 | { |
291 | return ++s_uniqueIdentifier; |
292 | } |
293 | |
294 | bool ProgressTracker::isMainLoadProgressing() const |
295 | { |
296 | if (!m_originatingProgressFrame) |
297 | return false; |
298 | |
299 | if (!m_isMainLoad) |
300 | return false; |
301 | |
302 | return m_progressValue && m_progressValue < finalProgressValue && m_heartbeatsWithNoProgress < loadStalledHeartbeatCount; |
303 | } |
304 | |
305 | void ProgressTracker::progressHeartbeatTimerFired() |
306 | { |
307 | if (m_totalBytesReceived < m_totalBytesReceivedBeforePreviousHeartbeat + minumumBytesPerHeartbeatForProgress) |
308 | ++m_heartbeatsWithNoProgress; |
309 | else |
310 | m_heartbeatsWithNoProgress = 0; |
311 | |
312 | m_totalBytesReceivedBeforePreviousHeartbeat = m_totalBytesReceived; |
313 | |
314 | if (m_originatingProgressFrame) |
315 | m_originatingProgressFrame->loader().loadProgressingStatusChanged(); |
316 | |
317 | if (m_progressValue >= finalProgressValue) |
318 | m_progressHeartbeatTimer.stop(); |
319 | } |
320 | |
321 | bool ProgressTracker::isAlwaysOnLoggingAllowed() const |
322 | { |
323 | if (!m_originatingProgressFrame) |
324 | return false; |
325 | |
326 | return m_originatingProgressFrame->isAlwaysOnLoggingAllowed(); |
327 | } |
328 | |
329 | } |
330 | |