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

Side by Side Diff: third_party/WebKit/Source/platform/network/NetworkStateNotifier.cpp

Issue 2863973003: Expose RTT and downlink bandwidth using experimental Javascript API (Closed)
Patch Set: kinuko 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 /* 1 /*
2 * Copyright (C) 2008 Apple Inc. All Rights Reserved. 2 * Copyright (C) 2008 Apple Inc. All Rights Reserved.
3 * 3 *
4 * Redistribution and use in source and binary forms, with or without 4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions 5 * modification, are permitted provided that the following conditions
6 * are met: 6 * are met:
7 * 1. Redistributions of source code must retain the above copyright 7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer. 8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright 9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the 10 * notice, this list of conditions and the following disclaimer in the
(...skipping 40 matching lines...) Expand 10 before | Expand all | Expand 10 after
51 : notifier_(notifier) { 51 : notifier_(notifier) {
52 DCHECK(IsMainThread()); 52 DCHECK(IsMainThread());
53 before_ = notifier_.has_override_ ? notifier_.override_ : notifier_.state_; 53 before_ = notifier_.has_override_ ? notifier_.override_ : notifier_.state_;
54 } 54 }
55 55
56 NetworkStateNotifier::ScopedNotifier::~ScopedNotifier() { 56 NetworkStateNotifier::ScopedNotifier::~ScopedNotifier() {
57 DCHECK(IsMainThread()); 57 DCHECK(IsMainThread());
58 const NetworkState& after = 58 const NetworkState& after =
59 notifier_.has_override_ ? notifier_.override_ : notifier_.state_; 59 notifier_.has_override_ ? notifier_.override_ : notifier_.state_;
60 if ((after.type != before_.type || 60 if ((after.type != before_.type ||
61 after.max_bandwidth_mbps != before_.max_bandwidth_mbps) && 61 after.max_bandwidth_mbps != before_.max_bandwidth_mbps ||
62 after.http_rtt != before_.http_rtt ||
63 after.transport_rtt != before_.transport_rtt ||
64 after.downlink_throughput_mbps != before_.downlink_throughput_mbps) &&
62 before_.connection_initialized) { 65 before_.connection_initialized) {
63 notifier_.NotifyObservers(notifier_.connection_observers_, 66 notifier_.NotifyObservers(notifier_.connection_observers_,
64 ObserverType::CONNECTION_TYPE, after); 67 ObserverType::CONNECTION_TYPE, after);
65 } 68 }
66 if (after.on_line != before_.on_line && before_.on_line_initialized) { 69 if (after.on_line != before_.on_line && before_.on_line_initialized) {
67 notifier_.NotifyObservers(notifier_.on_line_state_observers_, 70 notifier_.NotifyObservers(notifier_.on_line_state_observers_,
68 ObserverType::ONLINE_STATE, after); 71 ObserverType::ONLINE_STATE, after);
69 } 72 }
70 } 73 }
71 74
(...skipping 12 matching lines...) Expand all
84 DCHECK(IsMainThread()); 87 DCHECK(IsMainThread());
85 ScopedNotifier notifier(*this); 88 ScopedNotifier notifier(*this);
86 { 89 {
87 MutexLocker locker(mutex_); 90 MutexLocker locker(mutex_);
88 state_.connection_initialized = true; 91 state_.connection_initialized = true;
89 state_.type = type; 92 state_.type = type;
90 state_.max_bandwidth_mbps = max_bandwidth_mbps; 93 state_.max_bandwidth_mbps = max_bandwidth_mbps;
91 } 94 }
92 } 95 }
93 96
97 void NetworkStateNotifier::SetNetworkQuality(TimeDelta http_rtt,
98 TimeDelta transport_rtt,
99 int downlink_throughput_kbps) {
100 DCHECK(IsMainThread());
101 ScopedNotifier notifier(*this);
102 {
103 MutexLocker locker(mutex_);
104
105 state_.http_rtt =
106 http_rtt.InMilliseconds() < 0 ? Optional<TimeDelta>() : http_rtt;
dcheng 2017/05/17 12:27:46 Nit: does nullopt instead of Optional<TimeDelta>()
tbansal1 2017/05/18 00:10:30 Done.
107
108 state_.transport_rtt = transport_rtt.InMilliseconds() < 0
109 ? Optional<TimeDelta>()
110 : transport_rtt;
111
112 state_.downlink_throughput_mbps =
113 downlink_throughput_kbps < 0
114 ? Optional<double>()
115 : static_cast<double>(downlink_throughput_kbps) / 1000;
116 }
117 }
118
94 void NetworkStateNotifier::AddConnectionObserver( 119 void NetworkStateNotifier::AddConnectionObserver(
95 NetworkStateObserver* observer, 120 NetworkStateObserver* observer,
96 PassRefPtr<WebTaskRunner> task_runner) { 121 PassRefPtr<WebTaskRunner> task_runner) {
97 AddObserver(connection_observers_, observer, std::move(task_runner)); 122 AddObserver(connection_observers_, observer, std::move(task_runner));
98 } 123 }
99 124
100 void NetworkStateNotifier::AddOnLineObserver( 125 void NetworkStateNotifier::AddOnLineObserver(
101 NetworkStateObserver* observer, 126 NetworkStateObserver* observer,
102 PassRefPtr<WebTaskRunner> task_runner) { 127 PassRefPtr<WebTaskRunner> task_runner) {
103 AddObserver(on_line_state_observers_, observer, std::move(task_runner)); 128 AddObserver(on_line_state_observers_, observer, std::move(task_runner));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 198
174 for (size_t i = 0; i < observer_list->observers.size(); ++i) { 199 for (size_t i = 0; i < observer_list->observers.size(); ++i) {
175 // Observers removed during iteration are zeroed out, skip them. 200 // Observers removed during iteration are zeroed out, skip them.
176 if (!observer_list->observers[i]) 201 if (!observer_list->observers[i])
177 continue; 202 continue;
178 switch (type) { 203 switch (type) {
179 case ObserverType::ONLINE_STATE: 204 case ObserverType::ONLINE_STATE:
180 observer_list->observers[i]->OnLineStateChange(state.on_line); 205 observer_list->observers[i]->OnLineStateChange(state.on_line);
181 continue; 206 continue;
182 case ObserverType::CONNECTION_TYPE: 207 case ObserverType::CONNECTION_TYPE:
183 observer_list->observers[i]->ConnectionChange(state.type, 208 observer_list->observers[i]->ConnectionChange(
184 state.max_bandwidth_mbps); 209 state.type, state.max_bandwidth_mbps, state.http_rtt,
210 state.transport_rtt, state.downlink_throughput_mbps);
185 continue; 211 continue;
186 } 212 }
187 NOTREACHED(); 213 NOTREACHED();
188 } 214 }
189 215
190 observer_list->iterating = false; 216 observer_list->iterating = false;
191 217
192 if (!observer_list->zeroed_observers.IsEmpty()) 218 if (!observer_list->zeroed_observers.IsEmpty())
193 CollectZeroedObservers(*map, observer_list, std::move(task_runner)); 219 CollectZeroedObservers(*map, observer_list, std::move(task_runner));
194 } 220 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 279
254 list->zeroed_observers.clear(); 280 list->zeroed_observers.clear();
255 281
256 if (list->observers.IsEmpty()) { 282 if (list->observers.IsEmpty()) {
257 MutexLocker locker(mutex_); 283 MutexLocker locker(mutex_);
258 map.erase(task_runner); // deletes list 284 map.erase(task_runner); // deletes list
259 } 285 }
260 } 286 }
261 287
262 } // namespace blink 288 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698