Chromium Code Reviews| Index: third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp |
| diff --git a/third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp b/third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp |
| index 02f037404613dbd4635906ccd968738681270bff..8537e8c939184da84f1336a4472005e09400d97a 100644 |
| --- a/third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp |
| +++ b/third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp |
| @@ -40,6 +40,23 @@ String ConnectionTypeToString(WebConnectionType type) { |
| return "none"; |
| } |
| +// Rounds |rtt_msec| to the nearest 25 milliseconds as per the NetInfo spec. |
| +int RoundRtt(int rtt_msec) { |
| + if (rtt_msec < 0) |
| + return rtt_msec; |
|
jkarlin
2017/05/16 18:37:19
This is returning a negative value but rtt() is su
tbansal1
2017/05/16 21:34:30
Done.
|
| + return std::round(static_cast<double>(rtt_msec) / 25) * 25; |
| +} |
| + |
| +// Rounds |downlink_mbps| to the nearest 25 kbps as per the NetInfo spec. The |
| +// returned value is in Mbps. |
| +double RoundMbps(double downlink_mbps) { |
| + if (downlink_mbps < 0) |
| + return downlink_mbps; |
| + double downlink_kbps = downlink_mbps * 1000; |
| + double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25; |
| + return downlink_kbps_rounded / 1000; |
| +} |
| + |
| } // namespace |
| NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { |
| @@ -67,17 +84,40 @@ double NetworkInformation::downlinkMax() const { |
| return downlink_max_mbps_; |
| } |
| +long NetworkInformation::rtt() const { |
|
jkarlin
2017/05/16 18:37:19
unsigned long long?
tbansal1
2017/05/16 21:34:30
Done.
|
| + if (!observing_) |
| + return RoundRtt(GetNetworkStateNotifier().TransportRttMsec()); |
| + |
| + return transport_rtt_msec_; |
| +} |
| + |
| +double NetworkInformation::downlink() const { |
| + if (!observing_) |
| + return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps()); |
| + |
| + return downlink_mbps_; |
| +} |
| + |
| void NetworkInformation::ConnectionChange(WebConnectionType type, |
| - double downlink_max_mbps) { |
| + double downlink_max_mbps, |
| + int http_rtt_msec, |
| + int transport_rtt_msec, |
| + double downlink_mbps) { |
| DCHECK(GetExecutionContext()->IsContextThread()); |
| + transport_rtt_msec_ = RoundRtt(transport_rtt_msec); |
| + downlink_mbps_ = RoundMbps(downlink_mbps); |
| + // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the |
| + // expected network quality has changed. |
| + |
| // This can happen if the observer removes and then adds itself again |
| - // during notification. |
| + // during notification, or if HTTP RTT was the only metric that changed. |
| if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) |
| return; |
| type_ = type; |
| downlink_max_mbps_ = downlink_max_mbps; |
| + |
| DispatchEvent(Event::Create(EventTypeNames::typechange)); |
| if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled()) |