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