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

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: ps 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.
44 int roundRtt(int rtt_msec) {
jkarlin 2017/05/16 14:32:59 RoundRtt
tbansal1 2017/05/16 18:23:28 Done.
45 return std::round(static_cast<double>(rtt_msec) / 25) * 25;
46 }
47
48 // Rounds |downlink_mbps| to the nearest 25 kbps. The returned value is Mbps.
49 double roundMbps(double downlink_mbps) {
jkarlin 2017/05/16 14:32:59 RoundMbps
tbansal1 2017/05/16 18:23:28 Done.
50 double downlink_kbps = downlink_mbps * 1000;
51 double downlink_kbps_rounded = std::round(downlink_kbps / 25) * 25;
52 return downlink_kbps_rounded / 1000;
53 }
54
43 } // namespace 55 } // namespace
44 56
45 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) { 57 NetworkInformation* NetworkInformation::Create(ExecutionContext* context) {
46 return new NetworkInformation(context); 58 return new NetworkInformation(context);
47 } 59 }
48 60
49 NetworkInformation::~NetworkInformation() { 61 NetworkInformation::~NetworkInformation() {
50 DCHECK(!observing_); 62 DCHECK(!observing_);
51 } 63 }
52 64
53 String NetworkInformation::type() const { 65 String NetworkInformation::type() const {
54 // m_type is only updated when listening for events, so ask 66 // m_type is only updated when listening for events, so ask
55 // networkStateNotifier if not listening (crbug.com/379841). 67 // networkStateNotifier if not listening (crbug.com/379841).
56 if (!observing_) 68 if (!observing_)
57 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType()); 69 return ConnectionTypeToString(GetNetworkStateNotifier().ConnectionType());
58 70
59 // If observing, return m_type which changes when the event fires, per spec. 71 // If observing, return m_type which changes when the event fires, per spec.
60 return ConnectionTypeToString(type_); 72 return ConnectionTypeToString(type_);
61 } 73 }
62 74
63 double NetworkInformation::downlinkMax() const { 75 double NetworkInformation::downlinkMax() const {
64 if (!observing_) 76 if (!observing_)
65 return GetNetworkStateNotifier().MaxBandwidth(); 77 return GetNetworkStateNotifier().MaxBandwidth();
66 78
67 return downlink_max_mbps_; 79 return downlink_max_mbps_;
68 } 80 }
69 81
82 double NetworkInformation::rtt() const {
83 if (!observing_)
84 return roundRtt(GetNetworkStateNotifier().TransportRttMsec());
85
86 return transport_rtt_msec_;
87 }
88
89 double NetworkInformation::downlink() const {
90 if (!observing_)
91 return roundMbps(GetNetworkStateNotifier().DownlinkThroughputMbps());
92
93 return downlink_throughput_mbps_;
94 }
95
70 void NetworkInformation::ConnectionChange(WebConnectionType type, 96 void NetworkInformation::ConnectionChange(WebConnectionType type,
71 double downlink_max_mbps) { 97 double downlink_max_mbps,
98 int http_rtt_msec,
99 int transport_rtt_msec,
100 double downlink_throughput_mbps) {
72 DCHECK(GetExecutionContext()->IsContextThread()); 101 DCHECK(GetExecutionContext()->IsContextThread());
73 102
103 transport_rtt_msec_ = roundRtt(transport_rtt_msec);
104 downlink_throughput_mbps_ = roundMbps(downlink_throughput_mbps);
105 // TODO(tbansal): https://crbug.com/719108. Dispatch |change| event if the
106 // expected network quality has changed.
107
74 // This can happen if the observer removes and then adds itself again 108 // This can happen if the observer removes and then adds itself again
75 // during notification. 109 // during notification, or if HTTP RTT was the only metric that changed.
76 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps) 110 if (type_ == type && downlink_max_mbps_ == downlink_max_mbps)
77 return; 111 return;
78 112
79 type_ = type; 113 type_ = type;
80 downlink_max_mbps_ = downlink_max_mbps; 114 downlink_max_mbps_ = downlink_max_mbps;
115
81 DispatchEvent(Event::Create(EventTypeNames::typechange)); 116 DispatchEvent(Event::Create(EventTypeNames::typechange));
82 117
83 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled()) 118 if (RuntimeEnabledFeatures::netInfoDownlinkMaxEnabled())
84 DispatchEvent(Event::Create(EventTypeNames::change)); 119 DispatchEvent(Event::Create(EventTypeNames::change));
85 } 120 }
86 121
87 const AtomicString& NetworkInformation::InterfaceName() const { 122 const AtomicString& NetworkInformation::InterfaceName() const {
88 return EventTargetNames::NetworkInformation; 123 return EventTargetNames::NetworkInformation;
89 } 124 }
90 125
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
152 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()), 187 downlink_max_mbps_(GetNetworkStateNotifier().MaxBandwidth()),
153 observing_(false), 188 observing_(false),
154 context_stopped_(false) {} 189 context_stopped_(false) {}
155 190
156 DEFINE_TRACE(NetworkInformation) { 191 DEFINE_TRACE(NetworkInformation) {
157 EventTargetWithInlineData::Trace(visitor); 192 EventTargetWithInlineData::Trace(visitor);
158 ContextLifecycleObserver::Trace(visitor); 193 ContextLifecycleObserver::Trace(visitor);
159 } 194 }
160 195
161 } // namespace blink 196 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698