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

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: 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 /* 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::SetWebNetworkQuality(int http_rtt_msec,
98 int transport_rtt_msec,
99 int downlink_throughput_kbps) {
100 DCHECK(IsMainThread());
101 ScopedNotifier notifier(*this);
102 {
103 MutexLocker locker(mutex_);
104
105 state_.http_rtt = http_rtt_msec < 0
106 ? Optional<TimeDelta>()
107 : TimeDelta::FromMilliseconds(http_rtt_msec);
108
109 state_.transport_rtt =
110 transport_rtt_msec < 0
111 ? Optional<TimeDelta>()
112 : TimeDelta::FromMilliseconds(transport_rtt_msec);
113
114 state_.downlink_throughput_mbps =
115 downlink_throughput_kbps < 0
116 ? Optional<double>()
117 : static_cast<double>(downlink_throughput_kbps) / 1000;
118 }
119 }
120
94 void NetworkStateNotifier::AddConnectionObserver( 121 void NetworkStateNotifier::AddConnectionObserver(
95 NetworkStateObserver* observer, 122 NetworkStateObserver* observer,
96 PassRefPtr<WebTaskRunner> task_runner) { 123 PassRefPtr<WebTaskRunner> task_runner) {
97 AddObserver(connection_observers_, observer, std::move(task_runner)); 124 AddObserver(connection_observers_, observer, std::move(task_runner));
98 } 125 }
99 126
100 void NetworkStateNotifier::AddOnLineObserver( 127 void NetworkStateNotifier::AddOnLineObserver(
101 NetworkStateObserver* observer, 128 NetworkStateObserver* observer,
102 PassRefPtr<WebTaskRunner> task_runner) { 129 PassRefPtr<WebTaskRunner> task_runner) {
103 AddObserver(on_line_state_observers_, observer, std::move(task_runner)); 130 AddObserver(on_line_state_observers_, observer, std::move(task_runner));
(...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after
173 200
174 for (size_t i = 0; i < observer_list->observers.size(); ++i) { 201 for (size_t i = 0; i < observer_list->observers.size(); ++i) {
175 // Observers removed during iteration are zeroed out, skip them. 202 // Observers removed during iteration are zeroed out, skip them.
176 if (!observer_list->observers[i]) 203 if (!observer_list->observers[i])
177 continue; 204 continue;
178 switch (type) { 205 switch (type) {
179 case ObserverType::ONLINE_STATE: 206 case ObserverType::ONLINE_STATE:
180 observer_list->observers[i]->OnLineStateChange(state.on_line); 207 observer_list->observers[i]->OnLineStateChange(state.on_line);
181 continue; 208 continue;
182 case ObserverType::CONNECTION_TYPE: 209 case ObserverType::CONNECTION_TYPE:
183 observer_list->observers[i]->ConnectionChange(state.type, 210 observer_list->observers[i]->ConnectionChange(
184 state.max_bandwidth_mbps); 211 state.type, state.max_bandwidth_mbps, state.http_rtt,
212 state.transport_rtt, state.downlink_throughput_mbps);
185 continue; 213 continue;
186 } 214 }
187 NOTREACHED(); 215 NOTREACHED();
188 } 216 }
189 217
190 observer_list->iterating = false; 218 observer_list->iterating = false;
191 219
192 if (!observer_list->zeroed_observers.IsEmpty()) 220 if (!observer_list->zeroed_observers.IsEmpty())
193 CollectZeroedObservers(*map, observer_list, std::move(task_runner)); 221 CollectZeroedObservers(*map, observer_list, std::move(task_runner));
194 } 222 }
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after
253 281
254 list->zeroed_observers.clear(); 282 list->zeroed_observers.clear();
255 283
256 if (list->observers.IsEmpty()) { 284 if (list->observers.IsEmpty()) {
257 MutexLocker locker(mutex_); 285 MutexLocker locker(mutex_);
258 map.erase(task_runner); // deletes list 286 map.erase(task_runner); // deletes list
259 } 287 }
260 } 288 }
261 289
262 } // namespace blink 290 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698