1 | /* |
2 | * Copyright (C) 2019 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. AND ITS CONTRIBUTORS ``AS IS'' |
14 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
15 | * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
16 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS |
17 | * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
18 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
19 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
20 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
21 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
22 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
23 | * THE POSSIBILITY OF SUCH DAMAGE. |
24 | */ |
25 | |
26 | #include "config.h" |
27 | #include "DownloadMonitor.h" |
28 | |
29 | #include "Download.h" |
30 | #include "Logging.h" |
31 | |
32 | #undef RELEASE_LOG_IF_ALLOWED |
33 | #define RELEASE_LOG_IF_ALLOWED(fmt, ...) RELEASE_LOG_IF(m_download.isAlwaysOnLoggingAllowed(), Network, "%p - DownloadMonitor::" fmt, this, ##__VA_ARGS__) |
34 | |
35 | namespace WebKit { |
36 | |
37 | constexpr uint64_t operator"" _kbps(unsigned long long kilobytesPerSecond) |
38 | { |
39 | return kilobytesPerSecond * 1024; |
40 | } |
41 | |
42 | struct ThroughputInterval { |
43 | Seconds time; |
44 | uint64_t bytesPerSecond; |
45 | }; |
46 | |
47 | static const ThroughputInterval throughputIntervals[] = { |
48 | { 1_min, 1_kbps }, |
49 | { 5_min, 2_kbps }, |
50 | { 10_min, 4_kbps }, |
51 | { 15_min, 8_kbps }, |
52 | { 20_min, 16_kbps }, |
53 | { 25_min, 32_kbps }, |
54 | { 30_min, 64_kbps }, |
55 | { 45_min, 96_kbps }, |
56 | { 60_min, 128_kbps } |
57 | }; |
58 | |
59 | static Seconds timeUntilNextInterval(size_t currentInterval) |
60 | { |
61 | RELEASE_ASSERT(currentInterval + 1 < WTF_ARRAY_LENGTH(throughputIntervals)); |
62 | return throughputIntervals[currentInterval + 1].time - throughputIntervals[currentInterval].time; |
63 | } |
64 | |
65 | DownloadMonitor::DownloadMonitor(Download& download) |
66 | : m_download(download) |
67 | { |
68 | } |
69 | |
70 | double DownloadMonitor::measuredThroughputRate() const |
71 | { |
72 | uint64_t bytes { 0 }; |
73 | for (const auto& timestamp : m_timestamps) |
74 | bytes += timestamp.bytesReceived; |
75 | if (!bytes) |
76 | return 0; |
77 | ASSERT(!m_timestamps.isEmpty()); |
78 | Seconds timeDifference = m_timestamps.last().time.secondsSinceEpoch() - m_timestamps.first().time.secondsSinceEpoch(); |
79 | double seconds = timeDifference.seconds(); |
80 | if (!seconds) |
81 | return std::numeric_limits<double>::max(); |
82 | return bytes / seconds; |
83 | } |
84 | |
85 | void DownloadMonitor::downloadReceivedBytes(uint64_t bytesReceived) |
86 | { |
87 | if (m_timestamps.size() > timestampCapacity - 1) { |
88 | ASSERT(m_timestamps.size() == timestampCapacity); |
89 | m_timestamps.removeFirst(); |
90 | } |
91 | m_timestamps.append({ MonotonicTime::now(), bytesReceived }); |
92 | } |
93 | |
94 | void DownloadMonitor::applicationWillEnterForeground() |
95 | { |
96 | RELEASE_LOG_IF_ALLOWED("applicationWillEnterForeground (id = %" PRIu64 ")" , m_download.downloadID().downloadID()); |
97 | m_timer.stop(); |
98 | m_interval = 0; |
99 | } |
100 | |
101 | void DownloadMonitor::applicationDidEnterBackground() |
102 | { |
103 | RELEASE_LOG_IF_ALLOWED("applicationDidEnterBackground (id = %" PRIu64 ")" , m_download.downloadID().downloadID()); |
104 | ASSERT(!m_timer.isActive()); |
105 | ASSERT(!m_interval); |
106 | m_timer.startOneShot(throughputIntervals[0].time / speedMultiplier()); |
107 | } |
108 | |
109 | uint32_t DownloadMonitor::speedMultiplier() const |
110 | { |
111 | return m_download.manager().client().downloadMonitorSpeedMultiplier(); |
112 | } |
113 | |
114 | void DownloadMonitor::timerFired() |
115 | { |
116 | RELEASE_ASSERT(m_interval < WTF_ARRAY_LENGTH(throughputIntervals)); |
117 | if (measuredThroughputRate() < throughputIntervals[m_interval].bytesPerSecond) { |
118 | RELEASE_LOG_IF_ALLOWED("timerFired: cancelling download (id = %" PRIu64 ")" , m_download.downloadID().downloadID()); |
119 | m_download.cancel(); |
120 | } else if (m_interval + 1 < WTF_ARRAY_LENGTH(throughputIntervals)) { |
121 | RELEASE_LOG_IF_ALLOWED("timerFired: sufficient throughput rate (id = %" PRIu64 ")" , m_download.downloadID().downloadID()); |
122 | m_timer.startOneShot(timeUntilNextInterval(m_interval++) / speedMultiplier()); |
123 | } else |
124 | RELEASE_LOG_IF_ALLOWED("timerFired: Download reached threshold to not be terminated (id = %" PRIu64 ")" , m_download.downloadID().downloadID()); |
125 | } |
126 | |
127 | } // namespace WebKit |
128 | |