| 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 24 matching lines...) Expand all Loading... |
| 35 return "other"; | 35 return "other"; |
| 36 case kWebConnectionTypeNone: | 36 case kWebConnectionTypeNone: |
| 37 return "none"; | 37 return "none"; |
| 38 case kWebConnectionTypeUnknown: | 38 case kWebConnectionTypeUnknown: |
| 39 return "unknown"; | 39 return "unknown"; |
| 40 } | 40 } |
| 41 NOTREACHED(); | 41 NOTREACHED(); |
| 42 return "none"; | 42 return "none"; |
| 43 } | 43 } |
| 44 | 44 |
| 45 String EffectiveConnectionTypeToString(WebEffectiveConnectionType type) { |
| 46 switch (type) { |
| 47 case WebEffectiveConnectionType::kTypeUnknown: |
| 48 case WebEffectiveConnectionType::kTypeOffline: |
| 49 case WebEffectiveConnectionType::kType4G: |
| 50 return "4g"; |
| 51 case WebEffectiveConnectionType::kTypeSlow2G: |
| 52 return "slow-2g"; |
| 53 case WebEffectiveConnectionType::kType2G: |
| 54 return "2g"; |
| 55 case WebEffectiveConnectionType::kType3G: |
| 56 return "3g"; |
| 57 } |
| 58 NOTREACHED(); |
| 59 return "4g"; |
| 60 } |
| 61 |
| 45 // 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. |
| 46 unsigned long RoundRtt(const Optional<TimeDelta>& rtt) { | 63 unsigned long RoundRtt(const Optional<TimeDelta>& rtt) { |
| 47 if (!rtt.has_value()) { | 64 if (!rtt.has_value()) { |
| 48 // RTT is unavailable. So, return the fastest value. | 65 // RTT is unavailable. So, return the fastest value. |
| 49 return 0; | 66 return 0; |
| 50 } | 67 } |
| 51 | 68 |
| 52 int rtt_msec = rtt.value().InMilliseconds(); | 69 int rtt_msec = rtt.value().InMilliseconds(); |
| 53 if (rtt.value().InMilliseconds() > std::numeric_limits<int>::max()) | 70 if (rtt.value().InMilliseconds() > std::numeric_limits<int>::max()) |
| 54 rtt_msec = std::numeric_limits<int>::max(); | 71 rtt_msec = std::numeric_limits<int>::max(); |
| (...skipping 20 matching lines...) Expand all Loading... |
| 75 | 92 |
| 76 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { | 93 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { |
| 77 return new NetworkInformation(context); | 94 return new NetworkInformation(context); |
| 78 } | 95 } |
| 79 | 96 |
| 80 NetworkInformation::~NetworkInformation() { | 97 NetworkInformation::~NetworkInformation() { |
| 81 DCHECK(!observing_); | 98 DCHECK(!observing_); |
| 82 } | 99 } |
| 83 | 100 |
| 84 String NetworkInformation::type() const { | 101 String NetworkInformation::type() const { |
| 85 // m_type is only updated when listening for events, so ask | 102 // type_ is only updated when listening for events, so ask |
| 86 // networkStateNotifier if not listening (crbug.com/379841). | 103 // networkStateNotifier if not listening (crbug.com/379841). |
| 87 if (!observing_) | 104 if (!observing_) |
| 88 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); | 105 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); |
| 89 | 106 |
| 90 // If observing, return m_type which changes when the event fires, per spec. | 107 // If observing, return m_type which changes when the event fires, per spec. |
| 91 return ConnectionTypeToString(type_); | 108 return ConnectionTypeToString(type_); |
| 92 } | 109 } |
| 93 | 110 |
| 94 double NetworkInformation::downlinkMax() const { | 111 double NetworkInformation::downlinkMax() const { |
| 95 if (!observing_) | 112 if (!observing_) |
| 96 return GetNetworkStateNotifier().MaxBandwidth(); | 113 return GetNetworkStateNotifier().MaxBandwidth(); |
| 97 | 114 |
| 98 return downlink_max_mbps_; | 115 return downlink_max_mbps_; |
| 99 } | 116 } |
| 100 | 117 |
| 118 String NetworkInformation::effectiveType() const { |
| 119 // effective_type_ is only updated when listening for events, so ask |
| 120 // networkStateNotifier if not listening (crbug.com/379841). |
| 121 if (!observing_) { |
| 122 return EffectiveConnectionTypeToString( |
| 123 GetNetworkStateNotifier().EffectiveType()); |
| 124 } |
| 125 |
| 126 // If observing, return m_type which changes when the event fires, per spec. |
| 127 return EffectiveConnectionTypeToString(effective_type_); |
| 128 } |
| 129 |
| 101 unsigned long NetworkInformation::rtt() const { | 130 unsigned long NetworkInformation::rtt() const { |
| 102 if (!observing_) | 131 if (!observing_) |
| 103 return RoundRtt(GetNetworkStateNotifier().TransportRtt()); | 132 return RoundRtt(GetNetworkStateNotifier().TransportRtt()); |
| 104 | 133 |
| 105 return transport_rtt_msec_; | 134 return transport_rtt_msec_; |
| 106 } | 135 } |
| 107 | 136 |
| 108 double NetworkInformation::downlink() const { | 137 double NetworkInformation::downlink() const { |
| 109 if (!observing_) | 138 if (!observing_) |
| 110 return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps()); | 139 return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps()); |
| 111 | 140 |
| 112 return downlink_mbps_; | 141 return downlink_mbps_; |
| 113 } | 142 } |
| 114 | 143 |
| 115 void NetworkInformation::ConnectionChange( | 144 void NetworkInformation::ConnectionChange( |
| 116 WebConnectionType type, | 145 WebConnectionType type, |
| 117 double downlink_max_mbps, | 146 double downlink_max_mbps, |
| 147 WebEffectiveConnectionType effective_type, |
| 118 const Optional<TimeDelta>& http_rtt, | 148 const Optional<TimeDelta>& http_rtt, |
| 119 const Optional<TimeDelta>& transport_rtt, | 149 const Optional<TimeDelta>& transport_rtt, |
| 120 const Optional<double>& downlink_mbps) { | 150 const Optional<double>& downlink_mbps) { |
| 121 DCHECK(GetExecutionContext()->IsContextThread()); | 151 DCHECK(GetExecutionContext()->IsContextThread()); |
| 122 | 152 |
| 153 effective_type_ = effective_type; |
| 123 transport_rtt_msec_ = RoundRtt(transport_rtt); | 154 transport_rtt_msec_ = RoundRtt(transport_rtt); |
| 124 downlink_mbps_ = RoundMbps(downlink_mbps); | 155 downlink_mbps_ = RoundMbps(downlink_mbps); |
| 125 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the | 156 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the |
| 126 // expected network quality has changed. | 157 // expected network quality has changed. |
| 127 | 158 |
| 128 // This can happen if the observer removes and then adds itself again | 159 // This can happen if the observer removes and then adds itself again |
| 129 // during notification, or if HTTP RTT was the only metric that changed. | 160 // during notification, or if HTTP RTT was the only metric that changed. |
| 130 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) | 161 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) |
| 131 return; | 162 return; |
| 132 | 163 |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 197 this, | 228 this, |
| 198 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); | 229 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); |
| 199 observing_ = false; | 230 observing_ = false; |
| 200 } | 231 } |
| 201 } | 232 } |
| 202 | 233 |
| 203 NetworkInformation::NetworkInformation(ExecutionContext* context) | 234 NetworkInformation::NetworkInformation(ExecutionContext* context) |
| 204 : ContextLifecycleObserver(context), | 235 : ContextLifecycleObserver(context), |
| 205 type_(GetNetworkStateNotifier().ConnectionType()), | 236 type_(GetNetworkStateNotifier().ConnectionType()), |
| 206 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), | 237 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), |
| 238 effective_type_(GetNetworkStateNotifier().EffectiveType()), |
| 207 transport_rtt_msec_(RoundRtt(GetNetworkStateNotifier().TransportRtt())), | 239 transport_rtt_msec_(RoundRtt(GetNetworkStateNotifier().TransportRtt())), |
| 208 downlink_mbps_( | 240 downlink_mbps_( |
| 209 RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps())), | 241 RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps())), |
| 210 observing_(false), | 242 observing_(false), |
| 211 context_stopped_(false) {} | 243 context_stopped_(false) {} |
| 212 | 244 |
| 213 DEFINE_TRACE(NetworkInformation) { | 245 DEFINE_TRACE(NetworkInformation) { |
| 214 EventTargetWithInlineData::Trace(visitor); | 246 EventTargetWithInlineData::Trace(visitor); |
| 215 ContextLifecycleObserver::Trace(visitor); | 247 ContextLifecycleObserver::Trace(visitor); |
| 216 } | 248 } |
| 217 | 249 |
| 218 } // namespace blink | 250 } // namespace blink |
| OLD | NEW |