Index: net/base/network_quality_estimator.cc |
diff --git a/net/base/network_quality_estimator.cc b/net/base/network_quality_estimator.cc |
index 8ba2d03e06b1a42f80af86a907a6ad99696ee07d..9a6d8d60f0660841119ea7af663753b3f42fddc7 100644 |
--- a/net/base/network_quality_estimator.cc |
+++ b/net/base/network_quality_estimator.cc |
@@ -15,11 +15,13 @@ |
#include "base/metrics/histogram.h" |
#include "base/metrics/histogram_base.h" |
#include "base/strings/string_number_conversions.h" |
+#include "base/thread_task_runner_handle.h" |
#include "base/trace_event/trace_event.h" |
#include "build/build_config.h" |
#include "net/base/load_flags.h" |
#include "net/base/load_timing_info.h" |
#include "net/base/network_interfaces.h" |
+#include "net/base/socket_performance_watcher.h" |
#include "net/base/url_util.h" |
#include "net/url_request/url_request.h" |
#include "url/gurl.h" |
@@ -118,6 +120,81 @@ base::HistogramBase* GetHistogram( |
namespace net { |
+// SocketWatcher implements SocketPerformanceWatcher, and notifies |
+// NetworkQualityEstimator of various socket performance events. SocketWatcher |
+// is not thread-safe. |
+class NetworkQualityEstimator::SocketWatcher : public SocketPerformanceWatcher { |
+ public: |
+ SocketWatcher( |
+ SocketPerformanceWatcherFactory::Protocol protocol, |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
+ const base::WeakPtr<NetworkQualityEstimator>& network_quality_estimator) |
+ : protocol_(protocol), |
+ task_runner_(std::move(task_runner)), |
+ network_quality_estimator_(network_quality_estimator) {} |
+ |
+ ~SocketWatcher() override {} |
+ |
+ // SocketPerformanceWatcher implementation: |
+ bool ShouldNotifyUpdatedRTT() const override { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ return true; |
+ } |
+ |
+ void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ task_runner_->PostTask( |
+ FROM_HERE, base::Bind(&NetworkQualityEstimator::OnUpdatedRTTAvailable, |
+ network_quality_estimator_, protocol_, rtt)); |
+ } |
+ |
+ void OnConnectionChanged() override { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ } |
+ |
+ private: |
+ // Transport layer protocol used by the socket that |this| is watching. |
+ const SocketPerformanceWatcherFactory::Protocol protocol_; |
+ |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
+ |
+ base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; |
+ |
+ base::ThreadChecker thread_checker_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SocketWatcher); |
+}; |
+ |
+// SocketWatcherFactory implements SocketPerformanceWatcherFactory, and is |
+// owned by NetworkQualityEstimator. SocketWatcherFactory is thread safe. |
+class NetworkQualityEstimator::SocketWatcherFactory |
+ : public SocketPerformanceWatcherFactory { |
+ public: |
+ SocketWatcherFactory( |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
+ const base::WeakPtr<NetworkQualityEstimator>& network_quality_estimator) |
+ : task_runner_(std::move(task_runner)), |
+ network_quality_estimator_(network_quality_estimator) {} |
+ |
+ ~SocketWatcherFactory() override {} |
+ |
+ // SocketPerformanceWatcherFactory implementation: |
+ scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( |
+ const Protocol protocol) override { |
+ return scoped_ptr<SocketPerformanceWatcher>( |
+ new SocketWatcher(protocol, task_runner_, network_quality_estimator_)); |
+ } |
+ |
+ private: |
+ scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
+ |
+ base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; |
+ |
+ DISALLOW_COPY_AND_ASSIGN(SocketWatcherFactory); |
+}; |
+ |
const int32_t NetworkQualityEstimator::kInvalidThroughput = 0; |
NetworkQualityEstimator::NetworkQualityEstimator( |
@@ -142,7 +219,8 @@ NetworkQualityEstimator::NetworkQualityEstimator( |
downstream_throughput_kbps_observations_( |
GetWeightMultiplierPerSecond(variation_params)), |
rtt_observations_(GetWeightMultiplierPerSecond(variation_params)), |
- external_estimate_provider_(std::move(external_estimates_provider)) { |
+ external_estimate_provider_(std::move(external_estimates_provider)), |
+ weak_ptr_factory_(this) { |
static_assert(kMinRequestDurationMicroseconds > 0, |
"Minimum request duration must be > 0"); |
static_assert(kDefaultHalfLifeSeconds > 0, |
@@ -167,6 +245,9 @@ NetworkQualityEstimator::NetworkQualityEstimator( |
} |
current_network_id_ = GetCurrentNetworkID(); |
AddDefaultEstimates(); |
+ |
+ watcher_factory_.reset(new SocketWatcherFactory( |
+ base::ThreadTaskRunnerHandle::Get(), weak_ptr_factory_.GetWeakPtr())); |
} |
// static |
@@ -381,6 +462,13 @@ void NetworkQualityEstimator::RemoveThroughputObserver( |
throughput_observer_list_.RemoveObserver(throughput_observer); |
} |
+SocketPerformanceWatcherFactory* |
+NetworkQualityEstimator::GetSocketPerformanceWatcherFactory() { |
+ DCHECK(thread_checker_.CalledOnValidThread()); |
+ |
+ return watcher_factory_.get(); |
+} |
+ |
void NetworkQualityEstimator::RecordRTTUMA(int32_t estimated_value_msec, |
int32_t actual_value_msec) const { |
DCHECK(thread_checker_.CalledOnValidThread()); |
@@ -909,25 +997,16 @@ void NetworkQualityEstimator::CacheNetworkQualityEstimate() { |
static_cast<size_t>(kMaximumNetworkQualityCacheSize)); |
} |
-scoped_ptr<SocketPerformanceWatcher> |
-NetworkQualityEstimator::CreateSocketPerformanceWatcher( |
- const Protocol protocol) { |
- DCHECK(thread_checker_.CalledOnValidThread()); |
- |
- return scoped_ptr<SocketPerformanceWatcher>( |
- new SocketPerformanceWatcher(protocol, this)); |
-} |
- |
void NetworkQualityEstimator::OnUpdatedRTTAvailable( |
- const Protocol protocol, |
+ SocketPerformanceWatcherFactory::Protocol protocol, |
const base::TimeDelta& rtt) { |
DCHECK(thread_checker_.CalledOnValidThread()); |
switch (protocol) { |
- case PROTOCOL_TCP: |
+ case SocketPerformanceWatcherFactory::PROTOCOL_TCP: |
NotifyObserversOfRTT(RttObservation(rtt, base::TimeTicks::Now(), TCP)); |
return; |
- case PROTOCOL_QUIC: |
+ case SocketPerformanceWatcherFactory::PROTOCOL_QUIC: |
NotifyObserversOfRTT(RttObservation(rtt, base::TimeTicks::Now(), QUIC)); |
return; |
default: |