Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(61)

Side by Side Diff: third_party/WebKit/Source/modules/netinfo/NetworkInformation.cpp

Issue 2863973003: Expose RTT and downlink bandwidth using experimental Javascript API (Closed)
Patch Set: jkarlin comments Created 3 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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
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 int RoundRtt(int rtt_msec) {
45 if (rtt_msec < 0)
46 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.
47 return std::round(static_cast<double>(rtt_msec) / 25) * 25;
48 }
49
50 // Rounds |downlink_mbps| to the nearest 25 kbps as per the NetInfo spec. The
51 // returned value is in Mbps.
52 double RoundMbps(double downlink_mbps) {
53 if (downlink_mbps < 0)
54 return downlink_mbps;
55 double downlink_kbps = downlink_mbps * 1000;
56 double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25;
57 return downlink_kbps_rounded / 1000;
58 }
59
43 } // namespace 60 } // namespace
44 61
45 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { 62 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) {
46 return new NetworkInformation(context); 63 return new NetworkInformation(context);
47 } 64 }
48 65
49 NetworkInformation::~NetworkInformation() { 66 NetworkInformation::~NetworkInformation() {
50 DCHECK(!observing_); 67 DCHECK(!observing_);
51 } 68 }
52 69
53 String NetworkInformation::type() const { 70 String NetworkInformation::type() const {
54 // m_type is only updated when listening for events, so ask 71 // m_type is only updated when listening for events, so ask
55 // networkStateNotifier if not listening (crbug.com/379841). 72 // networkStateNotifier if not listening (crbug.com/379841).
56 if (!observing_) 73 if (!observing_)
57 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); 74 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType());
58 75
59 // If observing, return m_type which changes when the event fires, per spec. 76 // If observing, return m_type which changes when the event fires, per spec.
60 return ConnectionTypeToString(type_); 77 return ConnectionTypeToString(type_);
61 } 78 }
62 79
63 double NetworkInformation::downlinkMax() const { 80 double NetworkInformation::downlinkMax() const {
64 if (!observing_) 81 if (!observing_)
65 return GetNetworkStateNotifier().MaxBandwidth(); 82 return GetNetworkStateNotifier().MaxBandwidth();
66 83
67 return downlink_max_mbps_; 84 return downlink_max_mbps_;
68 } 85 }
69 86
87 long NetworkInformation::rtt() const {
jkarlin 2017/05/16 18:37:19 unsigned long long?
tbansal1 2017/05/16 21:34:30 Done.
88 if (!observing_)
89 return RoundRtt(GetNetworkStateNotifier().TransportRttMsec());
90
91 return transport_rtt_msec_;
92 }
93
94 double NetworkInformation::downlink() const {
95 if (!observing_)
96 return RoundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps());
97
98 return downlink_mbps_;
99 }
100
70 void NetworkInformation::ConnectionChange(WebConnectionType type, 101 void NetworkInformation::ConnectionChange(WebConnectionType type,
71 double downlink_max_mbps) { 102 double downlink_max_mbps,
103 int http_rtt_msec,
104 int transport_rtt_msec,
105 double downlink_mbps) {
72 DCHECK(GetExecutionContext()->IsContextThread()); 106 DCHECK(GetExecutionContext()->IsContextThread());
73 107
108 transport_rtt_msec_ = RoundRtt(transport_rtt_msec);
109 downlink_mbps_ = RoundMbps(downlink_mbps);
110 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the
111 // expected network quality has changed.
112
74 // This can happen if the observer removes and then adds itself again 113 // This can happen if the observer removes and then adds itself again
75 // during notification. 114 // during notification, or if HTTP RTT was the only metric that changed.
76 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) 115 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps)
77 return; 116 return;
78 117
79 type_ = type; 118 type_ = type;
80 downlink_max_mbps_ = downlink_max_mbps; 119 downlink_max_mbps_ = downlink_max_mbps;
120
81 DispatchEvent(Event::Create(EventTypeNames::typechange)); 121 DispatchEvent(Event::Create(EventTypeNames::typechange));
82 122
83 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled()) 123 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled())
84 DispatchEvent(Event::Create(EventTypeNames::change)); 124 DispatchEvent(Event::Create(EventTypeNames::change));
85 } 125 }
86 126
87 const AtomicString& NetworkInformation::InterfaceName() const { 127 const AtomicString& NetworkInformation::InterfaceName() const {
88 return EventTargetNames::NetworkInformation; 128 return EventTargetNames::NetworkInformation;
89 } 129 }
90 130
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), 192 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()),
153 observing_(false), 193 observing_(false),
154 context_stopped_(false) {} 194 context_stopped_(false) {}
155 195
156 DEFINE_TRACE(NetworkInformation) { 196 DEFINE_TRACE(NetworkInformation) {
157 EventTargetWithInlineData::Trace(visitor); 197 EventTargetWithInlineData::Trace(visitor);
158 ContextLifecycleObserver::Trace(visitor); 198 ContextLifecycleObserver::Trace(visitor);
159 } 199 }
160 200
161 } // namespace blink 201 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698