| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "modules/netinfo/NetworkInformation.h" | 5 #include "modules/netinfo/NetworkInformation.h" |
| 6 | 6 |
| 7 #include <limits> | 7 #include <limits> |
| 8 | 8 |
| 9 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
| 10 #include "core/dom/TaskRunnerHelper.h" | 10 #include "core/dom/TaskRunnerHelper.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 53 case WebEffectiveConnectionType::kType2G: | 53 case WebEffectiveConnectionType::kType2G: |
| 54 return "2g"; | 54 return "2g"; |
| 55 case WebEffectiveConnectionType::kType3G: | 55 case WebEffectiveConnectionType::kType3G: |
| 56 return "3g"; | 56 return "3g"; |
| 57 } | 57 } |
| 58 NOTREACHED(); | 58 NOTREACHED(); |
| 59 return "4g"; | 59 return "4g"; |
| 60 } | 60 } |
| 61 | 61 |
| 62 // Rounds |rtt_msec| to the nearest 25 milliseconds as per the NetInfo spec. | 62 // Rounds |rtt_msec| to the nearest 25 milliseconds as per the NetInfo spec. |
| 63 unsigned long RoundRtt(const Optional<TimeDelta>& rtt) { | 63 // If |rtt| has no value, |type| is used to determine a default value. |
| 64 unsigned long RoundTransportRtt(WebConnectionType type, |
| 65 const Optional<TimeDelta>& rtt) { |
| 66 int rtt_msec = 0; |
| 64 if (!rtt.has_value()) { | 67 if (!rtt.has_value()) { |
| 65 // RTT is unavailable. So, return the fastest value. | 68 DCHECK(kWebConnectionTypeUnknown == type || |
| 66 return 0; | 69 kWebConnectionTypeNone == type || kWebConnectionTypeOther == type); |
| 70 rtt_msec = 55; |
| 71 } else { |
| 72 rtt_msec = rtt.value().InMilliseconds(); |
| 73 if (rtt.value().InMilliseconds() > std::numeric_limits<int>::max()) |
| 74 rtt_msec = std::numeric_limits<int>::max(); |
| 67 } | 75 } |
| 68 | 76 |
| 69 int rtt_msec = rtt.value().InMilliseconds(); | |
| 70 if (rtt.value().InMilliseconds() > std::numeric_limits<int>::max()) | |
| 71 rtt_msec = std::numeric_limits<int>::max(); | |
| 72 | |
| 73 DCHECK_LE(0, rtt_msec); | 77 DCHECK_LE(0, rtt_msec); |
| 74 return std::round(static_cast<double>(rtt_msec) / 25) * 25; | 78 return std::round(static_cast<double>(rtt_msec) / 25) * 25; |
| 75 } | 79 } |
| 76 | 80 |
| 77 // Rounds |downlink_mbps| to the nearest 25 kbps as per the NetInfo spec. The | 81 // Rounds |downlink_mbps| to the nearest 25 kbps as per the NetInfo spec. The |
| 78 // returned value is in Mbps. | 82 // returned value is in Mbps. If |downlink_mbps| has no value, |type| is used to |
| 79 double RoundMbps(const Optional<double>& downlink_mbps) { | 83 // determine a default value. |
| 84 double RoundMbps(WebConnectionType type, |
| 85 const Optional<double>& downlink_mbps) { |
| 86 double downlink_kbps = std::numeric_limits<double>::infinity(); |
| 80 if (!downlink_mbps.has_value()) { | 87 if (!downlink_mbps.has_value()) { |
| 81 // Throughput is unavailable. So, return the fastest value. | 88 DCHECK(kWebConnectionTypeUnknown == type || |
| 82 return std::numeric_limits<double>::infinity(); | 89 kWebConnectionTypeNone == type || kWebConnectionTypeOther == type); |
| 90 downlink_kbps = 1961; |
| 91 } else { |
| 92 downlink_kbps = downlink_mbps.value() * 1000; |
| 83 } | 93 } |
| 84 | 94 |
| 85 DCHECK_LE(0, downlink_mbps.value()); | 95 DCHECK_LE(0, downlink_kbps); |
| 86 double downlink_kbps = downlink_mbps.value() * 1000; | |
| 87 double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25; | 96 double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25; |
| 88 return downlink_kbps_rounded / 1000; | 97 return downlink_kbps_rounded / 1000; |
| 89 } | 98 } |
| 90 | 99 |
| 91 } // namespace | 100 } // namespace |
| 92 | 101 |
| 93 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { | 102 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { |
| 94 return new NetworkInformation(context); | 103 return new NetworkInformation(context); |
| 95 } | 104 } |
| 96 | 105 |
| (...skipping 24 matching lines...) Expand all Loading... |
| 121 if (!observing_) { | 130 if (!observing_) { |
| 122 return EffectiveConnectionTypeToString( | 131 return EffectiveConnectionTypeToString( |
| 123 GetNetworkStateNotifier().EffectiveType()); | 132 GetNetworkStateNotifier().EffectiveType()); |
| 124 } | 133 } |
| 125 | 134 |
| 126 // If observing, return m_type which changes when the event fires, per spec. | 135 // If observing, return m_type which changes when the event fires, per spec. |
| 127 return EffectiveConnectionTypeToString(effective_type_); | 136 return EffectiveConnectionTypeToString(effective_type_); |
| 128 } | 137 } |
| 129 | 138 |
| 130 unsigned long NetworkInformation::rtt() const { | 139 unsigned long NetworkInformation::rtt() const { |
| 131 if (!observing_) | 140 if (!observing_) { |
| 132 return RoundRtt(GetNetworkStateNotifier().TransportRtt()); | 141 return RoundTransportRtt(GetNetworkStateNotifier().ConnectionType(), |
| 133 | 142 GetNetworkStateNotifier().TransportRtt()); |
| 143 } |
| 134 return transport_rtt_msec_; | 144 return transport_rtt_msec_; |
| 135 } | 145 } |
| 136 | 146 |
| 137 double NetworkInformation::downlink() const { | 147 double NetworkInformation::downlink() const { |
| 138 if (!observing_) | 148 if (!observing_) { |
| 139 return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps()); | 149 return RoundMbps(GetNetworkStateNotifier().ConnectionType(), |
| 140 | 150 GetNetworkStateNotifier().DownlinkThroughputMbps()); |
| 151 } |
| 141 return downlink_mbps_; | 152 return downlink_mbps_; |
| 142 } | 153 } |
| 143 | 154 |
| 144 void NetworkInformation::ConnectionChange( | 155 void NetworkInformation::ConnectionChange( |
| 145 WebConnectionType type, | 156 WebConnectionType type, |
| 146 double downlink_max_mbps, | 157 double downlink_max_mbps, |
| 147 WebEffectiveConnectionType effective_type, | 158 WebEffectiveConnectionType effective_type, |
| 148 const Optional<TimeDelta>& http_rtt, | 159 const Optional<TimeDelta>& http_rtt, |
| 149 const Optional<TimeDelta>& transport_rtt, | 160 const Optional<TimeDelta>& transport_rtt, |
| 150 const Optional<double>& downlink_mbps) { | 161 const Optional<double>& downlink_mbps) { |
| 151 DCHECK(GetExecutionContext()->IsContextThread()); | 162 DCHECK(GetExecutionContext()->IsContextThread()); |
| 152 | 163 |
| 153 effective_type_ = effective_type; | 164 effective_type_ = effective_type; |
| 154 transport_rtt_msec_ = RoundRtt(transport_rtt); | 165 transport_rtt_msec_ = RoundTransportRtt(type, transport_rtt); |
| 155 downlink_mbps_ = RoundMbps(downlink_mbps); | 166 downlink_mbps_ = RoundMbps(type, downlink_mbps); |
| 156 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the | 167 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the |
| 157 // expected network quality has changed. | 168 // expected network quality has changed. |
| 158 | 169 |
| 159 // This can happen if the observer removes and then adds itself again | 170 // This can happen if the observer removes and then adds itself again |
| 160 // during notification, or if HTTP RTT was the only metric that changed. | 171 // during notification, or if HTTP RTT was the only metric that changed. |
| 161 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) | 172 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) |
| 162 return; | 173 return; |
| 163 | 174 |
| 164 type_ = type; | 175 type_ = type; |
| 165 downlink_max_mbps_ = downlink_max_mbps; | 176 downlink_max_mbps_ = downlink_max_mbps; |
| (...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 229 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); | 240 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); |
| 230 observing_ = false; | 241 observing_ = false; |
| 231 } | 242 } |
| 232 } | 243 } |
| 233 | 244 |
| 234 NetworkInformation::NetworkInformation(ExecutionContext* context) | 245 NetworkInformation::NetworkInformation(ExecutionContext* context) |
| 235 : ContextLifecycleObserver(context), | 246 : ContextLifecycleObserver(context), |
| 236 type_(GetNetworkStateNotifier().ConnectionType()), | 247 type_(GetNetworkStateNotifier().ConnectionType()), |
| 237 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), | 248 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), |
| 238 effective_type_(GetNetworkStateNotifier().EffectiveType()), | 249 effective_type_(GetNetworkStateNotifier().EffectiveType()), |
| 239 transport_rtt_msec_(RoundRtt(GetNetworkStateNotifier().TransportRtt())), | 250 transport_rtt_msec_( |
| 251 RoundTransportRtt(type_, GetNetworkStateNotifier().TransportRtt())), |
| 240 downlink_mbps_( | 252 downlink_mbps_( |
| 241 RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps())), | 253 RoundMbps(type_, GetNetworkStateNotifier().DownlinkThroughputMbps())), |
| 242 observing_(false), | 254 observing_(false), |
| 243 context_stopped_(false) {} | 255 context_stopped_(false) {} |
| 244 | 256 |
| 245 DEFINE_TRACE(NetworkInformation) { | 257 DEFINE_TRACE(NetworkInformation) { |
| 246 EventTargetWithInlineData::Trace(visitor); | 258 EventTargetWithInlineData::Trace(visitor); |
| 247 ContextLifecycleObserver::Trace(visitor); | 259 ContextLifecycleObserver::Trace(visitor); |
| 248 } | 260 } |
| 249 | 261 |
| 250 } // namespace blink | 262 } // namespace blink |
| OLD | NEW |