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> |
| 8 |
7 #include "core/dom/ExecutionContext.h" | 9 #include "core/dom/ExecutionContext.h" |
8 #include "core/dom/TaskRunnerHelper.h" | 10 #include "core/dom/TaskRunnerHelper.h" |
9 #include "core/events/Event.h" | 11 #include "core/events/Event.h" |
10 #include "modules/EventTargetModules.h" | 12 #include "modules/EventTargetModules.h" |
11 #include "platform/RuntimeEnabledFeatures.h" | 13 #include "platform/RuntimeEnabledFeatures.h" |
12 #include "platform/wtf/text/WTFString.h" | 14 #include "platform/wtf/text/WTFString.h" |
13 | 15 |
14 namespace blink { | 16 namespace blink { |
15 | 17 |
16 namespace { | 18 namespace { |
(...skipping 16 matching lines...) Expand all Loading... |
33 return "other"; | 35 return "other"; |
34 case kWebConnectionTypeNone: | 36 case kWebConnectionTypeNone: |
35 return "none"; | 37 return "none"; |
36 case kWebConnectionTypeUnknown: | 38 case kWebConnectionTypeUnknown: |
37 return "unknown"; | 39 return "unknown"; |
38 } | 40 } |
39 NOTREACHED(); | 41 NOTREACHED(); |
40 return "none"; | 42 return "none"; |
41 } | 43 } |
42 | 44 |
| 45 // Rounds |rtt_msec| to the nearest 25 milliseconds as per the NetInfo spec. |
| 46 unsigned long RoundRtt(const Optional<TimeDelta>& rtt) { |
| 47 if (!rtt.has_value()) { |
| 48 // RTT is unavailable. So, return the fastest value. |
| 49 return 0; |
| 50 } |
| 51 |
| 52 int rtt_msec = rtt.value().InMilliseconds(); |
| 53 if (rtt.value().InMilliseconds() > std::numeric_limits<int>::max()) |
| 54 rtt_msec = std::numeric_limits<int>::max(); |
| 55 |
| 56 DCHECK_LE(0, rtt_msec); |
| 57 return std::round(static_cast<double>(rtt_msec) / 25) * 25; |
| 58 } |
| 59 |
| 60 // Rounds |downlink_mbps| to the nearest 25 kbps as per the NetInfo spec. The |
| 61 // returned value is in Mbps. |
| 62 double RoundMbps(const Optional<double>& downlink_mbps) { |
| 63 if (!downlink_mbps.has_value()) { |
| 64 // Throughput is unavailable. So, return the fastest value. |
| 65 return std::numeric_limits<double>::infinity(); |
| 66 } |
| 67 |
| 68 DCHECK_LE(0, downlink_mbps.value()); |
| 69 double downlink_kbps = downlink_mbps.value() * 1000; |
| 70 double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25; |
| 71 return downlink_kbps_rounded / 1000; |
| 72 } |
| 73 |
43 } // namespace | 74 } // namespace |
44 | 75 |
45 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { | 76 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { |
46 return new NetworkInformation(context); | 77 return new NetworkInformation(context); |
47 } | 78 } |
48 | 79 |
49 NetworkInformation::~NetworkInformation() { | 80 NetworkInformation::~NetworkInformation() { |
50 DCHECK(!observing_); | 81 DCHECK(!observing_); |
51 } | 82 } |
52 | 83 |
53 String NetworkInformation::type() const { | 84 String NetworkInformation::type() const { |
54 // m_type is only updated when listening for events, so ask | 85 // m_type is only updated when listening for events, so ask |
55 // networkStateNotifier if not listening (crbug.com/379841). | 86 // networkStateNotifier if not listening (crbug.com/379841). |
56 if (!observing_) | 87 if (!observing_) |
57 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); | 88 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); |
58 | 89 |
59 // If observing, return m_type which changes when the event fires, per spec. | 90 // If observing, return m_type which changes when the event fires, per spec. |
60 return ConnectionTypeToString(type_); | 91 return ConnectionTypeToString(type_); |
61 } | 92 } |
62 | 93 |
63 double NetworkInformation::downlinkMax() const { | 94 double NetworkInformation::downlinkMax() const { |
64 if (!observing_) | 95 if (!observing_) |
65 return GetNetworkStateNotifier().MaxBandwidth(); | 96 return GetNetworkStateNotifier().MaxBandwidth(); |
66 | 97 |
67 return downlink_max_mbps_; | 98 return downlink_max_mbps_; |
68 } | 99 } |
69 | 100 |
70 void NetworkInformation::ConnectionChange(WebConnectionType type, | 101 unsigned long NetworkInformation::rtt() const { |
71 double downlink_max_mbps) { | 102 if (!observing_) |
| 103 return RoundRtt(GetNetworkStateNotifier().TransportRtt()); |
| 104 |
| 105 return transport_rtt_msec_; |
| 106 } |
| 107 |
| 108 double NetworkInformation::downlink() const { |
| 109 if (!observing_) |
| 110 return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps()); |
| 111 |
| 112 return downlink_mbps_; |
| 113 } |
| 114 |
| 115 void NetworkInformation::ConnectionChange( |
| 116 WebConnectionType type, |
| 117 double downlink_max_mbps, |
| 118 const Optional<TimeDelta>& http_rtt, |
| 119 const Optional<TimeDelta>& transport_rtt, |
| 120 const Optional<double>& downlink_mbps) { |
72 DCHECK(GetExecutionContext()->IsContextThread()); | 121 DCHECK(GetExecutionContext()->IsContextThread()); |
73 | 122 |
| 123 transport_rtt_msec_ = RoundRtt(transport_rtt); |
| 124 downlink_mbps_ = RoundMbps(downlink_mbps); |
| 125 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the |
| 126 // expected network quality has changed. |
| 127 |
74 // This can happen if the observer removes and then adds itself again | 128 // This can happen if the observer removes and then adds itself again |
75 // during notification. | 129 // during notification, or if HTTP RTT was the only metric that changed. |
76 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) | 130 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) |
77 return; | 131 return; |
78 | 132 |
79 type_ = type; | 133 type_ = type; |
80 downlink_max_mbps_ = downlink_max_mbps; | 134 downlink_max_mbps_ = downlink_max_mbps; |
81 DispatchEvent(Event::Create(EventTypeNames::typechange)); | 135 DispatchEvent(Event::Create(EventTypeNames::typechange)); |
82 | 136 |
83 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled()) | 137 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled()) |
84 DispatchEvent(Event::Create(EventTypeNames::change)); | 138 DispatchEvent(Event::Create(EventTypeNames::change)); |
85 } | 139 } |
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
143 this, | 197 this, |
144 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); | 198 TaskRunnerHelper::Get(TaskType::kNetworking, GetExecutionContext())); |
145 observing_ = false; | 199 observing_ = false; |
146 } | 200 } |
147 } | 201 } |
148 | 202 |
149 NetworkInformation::NetworkInformation(ExecutionContext* context) | 203 NetworkInformation::NetworkInformation(ExecutionContext* context) |
150 : ContextLifecycleObserver(context), | 204 : ContextLifecycleObserver(context), |
151 type_(GetNetworkStateNotifier().ConnectionType()), | 205 type_(GetNetworkStateNotifier().ConnectionType()), |
152 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), | 206 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), |
| 207 transport_rtt_msec_(RoundRtt(GetNetworkStateNotifier().TransportRtt())), |
| 208 downlink_mbps_( |
| 209 RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps())), |
153 observing_(false), | 210 observing_(false), |
154 context_stopped_(false) {} | 211 context_stopped_(false) {} |
155 | 212 |
156 DEFINE_TRACE(NetworkInformation) { | 213 DEFINE_TRACE(NetworkInformation) { |
157 EventTargetWithInlineData::Trace(visitor); | 214 EventTargetWithInlineData::Trace(visitor); |
158 ContextLifecycleObserver::Trace(visitor); | 215 ContextLifecycleObserver::Trace(visitor); |
159 } | 216 } |
160 | 217 |
161 } // namespace blink | 218 } // namespace blink |
OLD | NEW |