blob: dc6b3226e9251b7619570a5a66e56e4db5e02ba4 [file] [log] [blame]
tbansalea2fb8c2015-05-22 22:23:001// Copyright 2015 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
6#define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_
7
8#include <stdint.h>
9
tbansal6f840fc52015-06-13 03:30:3410#include <deque>
tbansalb177b5392015-06-25 11:13:0211#include <map>
tbansal75540a52015-07-15 22:18:2112#include <string>
tbansal6f840fc52015-06-13 03:30:3413
tbansalea2fb8c2015-05-22 22:23:0014#include "base/gtest_prod_util.h"
15#include "base/macros.h"
bengrf1b6738e2015-10-07 01:21:3016#include "base/memory/ref_counted.h"
tbansal75540a52015-07-15 22:18:2117#include "base/memory/scoped_ptr.h"
bengrf1b6738e2015-10-07 01:21:3018#include "base/observer_list.h"
tbansalea2fb8c2015-05-22 22:23:0019#include "base/threading/thread_checker.h"
20#include "base/time/time.h"
tbansal1c92d5b2015-08-14 20:14:4321#include "net/base/external_estimate_provider.h"
tbansal6f840fc52015-06-13 03:30:3422#include "net/base/net_export.h"
tbansalea2fb8c2015-05-22 22:23:0023#include "net/base/network_change_notifier.h"
tbansalba8f4112015-09-03 21:57:1924#include "net/base/socket_performance_watcher.h"
25#include "net/base/socket_performance_watcher_factory.h"
tbansalea2fb8c2015-05-22 22:23:0026
27namespace net {
28
tbansal79ed5cd2015-08-10 18:53:5629class URLRequest;
30
tbansalea2fb8c2015-05-22 22:23:0031// NetworkQualityEstimator provides network quality estimates (quality of the
32// full paths to all origins that have been connected to).
33// The estimates are based on the observed organic traffic.
34// A NetworkQualityEstimator instance is attached to URLRequestContexts and
35// observes the traffic of URLRequests spawned from the URLRequestContexts.
36// A single instance of NQE can be attached to multiple URLRequestContexts,
37// thereby increasing the single NQE instance's accuracy by providing more
38// observed traffic characteristics.
39class NET_EXPORT_PRIVATE NetworkQualityEstimator
tbansal1c92d5b2015-08-14 20:14:4340 : public NetworkChangeNotifier::ConnectionTypeObserver,
tbansalba8f4112015-09-03 21:57:1941 public ExternalEstimateProvider::UpdatedEstimateDelegate,
42 public SocketPerformanceWatcherFactory {
tbansalea2fb8c2015-05-22 22:23:0043 public:
bengrf1b6738e2015-10-07 01:21:3044 // On Android, a Java counterpart will be generated for this enum.
45 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net
46 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource
47 // GENERATED_JAVA_PREFIX_TO_STRIP:
48 enum ObservationSource {
49 // The observation was taken at the request layer, e.g., a round trip time
50 // is recorded as the time between the request being sent and the first byte
51 // being received.
52 URL_REQUEST,
53 // The observation is taken from TCP statistics maintained by the kernel.
54 TCP,
55 // The observation is taken at the QUIC layer.
56 QUIC,
57 // The observation is a previously cached estimate of the metric.
58 CACHED_ESTIMATE,
59 // The observation is derived from network connection information provided
60 // by the platform. For example, typical RTT and throughput values are used
61 // for a given type of network connection.
62 DEFAULT_FROM_PLATFORM,
63 // The observation came from a Chromium-external source.
64 EXTERNAL_ESTIMATE
65 };
66
67 // Observes measurements of round trip time.
68 class NET_EXPORT_PRIVATE RTTObserver {
69 public:
70 // Will be called when a new RTT observation is available. The round trip
71 // time is specified in milliseconds. The time when the observation was
72 // taken and the source of the observation are provided.
73 virtual void OnRTTObservation(int32_t rtt_ms,
74 const base::TimeTicks& timestamp,
75 ObservationSource source) = 0;
76
77 protected:
78 RTTObserver() {}
79 virtual ~RTTObserver() {}
80
81 private:
82 DISALLOW_COPY_AND_ASSIGN(RTTObserver);
83 };
84
85 // Observes measurements of throughput.
86 class NET_EXPORT_PRIVATE ThroughputObserver {
87 public:
88 // Will be called when a new throughput observation is available.
89 // Throughput is specified in kilobits per second.
90 virtual void OnThroughputObservation(int32_t throughput_kbps,
91 const base::TimeTicks& timestamp,
92 ObservationSource source) = 0;
93
94 protected:
95 ThroughputObserver() {}
96 virtual ~ThroughputObserver() {}
97
98 private:
99 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver);
100 };
101
tbansalea2fb8c2015-05-22 22:23:00102 // Creates a new NetworkQualityEstimator.
tbansalb177b5392015-06-25 11:13:02103 // |variation_params| is the map containing all field trial parameters
104 // related to NetworkQualityEstimator field trial.
tbansal1c92d5b2015-08-14 20:14:43105 // |external_estimates_provider| may be NULL.
106 NetworkQualityEstimator(
107 scoped_ptr<ExternalEstimateProvider> external_estimates_provider,
tbansalb177b5392015-06-25 11:13:02108 const std::map<std::string, std::string>& variation_params);
tbansalea2fb8c2015-05-22 22:23:00109
bengrf1b6738e2015-10-07 01:21:30110 // Construct a NetworkQualityEstimator instance allowing for test
111 // configuration. Registers for network type change notifications so estimates
112 // can be kept network specific.
113 // |external_estimates_provider| may be NULL.
114 // |variation_params| is the map containing all field trial parameters for the
115 // network quality estimator field trial.
116 // |allow_local_host_requests_for_tests| should only be true when testing
117 // against local HTTP server and allows the requests to local host to be
118 // used for network quality estimation.
119 // |allow_smaller_responses_for_tests| should only be true when testing.
120 // Allows the responses smaller than |kMinTransferSizeInBytes| or shorter than
121 // |kMinRequestDurationMicroseconds| to be used for network quality
122 // estimation.
123 NetworkQualityEstimator(
124 scoped_ptr<ExternalEstimateProvider> external_estimates_provider,
125 const std::map<std::string, std::string>& variation_params,
126 bool allow_local_host_requests_for_tests,
127 bool allow_smaller_responses_for_tests);
128
tbansalea2fb8c2015-05-22 22:23:00129 ~NetworkQualityEstimator() override;
130
rockotf5eb5752015-08-11 19:45:31131 // Returns true if RTT is available and sets |rtt| to estimated RTT.
tbansalb658f3882015-08-19 21:27:49132 // Virtualized for testing. |rtt| should not be null.
tbansal9533b6f2015-08-11 22:06:08133 virtual bool GetRTTEstimate(base::TimeDelta* rtt) const;
tbansal91fb5882015-08-05 04:24:56134
135 // Returns true if downlink throughput is available and sets |kbps| to
136 // estimated downlink throughput (in Kilobits per second).
tbansalb658f3882015-08-19 21:27:49137 // Virtualized for testing. |kbps| should not be null.
tbansal9533b6f2015-08-11 22:06:08138 virtual bool GetDownlinkThroughputKbpsEstimate(int32_t* kbps) const;
tbansal91fb5882015-08-05 04:24:56139
tbansal79ed5cd2015-08-10 18:53:56140 // Notifies NetworkQualityEstimator that the response header of |request| has
141 // been received.
142 void NotifyHeadersReceived(const URLRequest& request);
143
144 // Notifies NetworkQualityEstimator that the response body of |request| has
145 // been received.
146 void NotifyRequestCompleted(const URLRequest& request);
tbansalea2fb8c2015-05-22 22:23:00147
tbansal9533b6f2015-08-11 22:06:08148 // Returns true if median RTT is available and sets |rtt| to the median of
149 // RTT observations since |begin_timestamp|.
tbansalb658f3882015-08-19 21:27:49150 // Virtualized for testing. |rtt| should not be null.
151 virtual bool GetRecentMedianRTT(const base::TimeTicks& begin_timestamp,
152 base::TimeDelta* rtt) const;
153
154 // Returns true if median downstream throughput is available and sets |kbps|
155 // to the median of downstream Kbps observations since |begin_timestamp|.
156 // Virtualized for testing. |kbps| should not be null.
157 virtual bool GetRecentMedianDownlinkThroughputKbps(
158 const base::TimeTicks& begin_timestamp,
159 int32_t* kbps) const;
tbansalf7c6d522015-08-05 21:59:36160
tbansalba8f4112015-09-03 21:57:19161 // SocketPerformanceWatcherFactory implementation:
tbansalc8a94ea2015-11-02 23:58:51162 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher(
163 const Protocol protocol) override;
164 void OnUpdatedRTTAvailable(const Protocol protocol,
165 const base::TimeDelta& rtt) override;
tbansalba8f4112015-09-03 21:57:19166
bengrf1b6738e2015-10-07 01:21:30167 // Adds |rtt_observer| to the list of round trip time observers. Must be
168 // called on the IO thread.
169 void AddRTTObserver(RTTObserver* rtt_observer);
170
171 // Removes |rtt_observer| from the list of round trip time observers if it
172 // is on the list of observers. Must be called on the IO thread.
173 void RemoveRTTObserver(RTTObserver* rtt_observer);
174
175 // Adds |throughput_observer| to the list of throughput observers. Must be
176 // called on the IO thread.
177 void AddThroughputObserver(ThroughputObserver* throughput_observer);
178
179 // Removes |throughput_observer| from the list of throughput observers if it
180 // is on the list of observers. Must be called on the IO thread.
181 void RemoveThroughputObserver(ThroughputObserver* throughput_observer);
182
tbansal75540a52015-07-15 22:18:21183 protected:
184 // NetworkID is used to uniquely identify a network.
185 // For the purpose of network quality estimation and caching, a network is
186 // uniquely identified by a combination of |type| and
187 // |id|. This approach is unable to distinguish networks with
188 // same name (e.g., different Wi-Fi networks with same SSID).
189 // This is a protected member to expose it to tests.
190 struct NET_EXPORT_PRIVATE NetworkID {
191 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id)
192 : type(type), id(id) {}
193 NetworkID(const NetworkID& other) : type(other.type), id(other.id) {}
194 ~NetworkID() {}
195
196 NetworkID& operator=(const NetworkID& other) {
197 type = other.type;
198 id = other.id;
199 return *this;
200 }
201
202 // Overloaded because NetworkID is used as key in a map.
203 bool operator<(const NetworkID& other) const {
204 return type < other.type || (type == other.type && id < other.id);
205 }
206
207 // Connection type of the network.
208 NetworkChangeNotifier::ConnectionType type;
209
210 // Name of this network. This is set to:
211 // - Wi-Fi SSID if the device is connected to a Wi-Fi access point and the
212 // SSID name is available, or
213 // - MCC/MNC code of the cellular carrier if the device is connected to a
214 // cellular network, or
215 // - "Ethernet" in case the device is connected to ethernet.
216 // - An empty string in all other cases or if the network name is not
217 // exposed by platform APIs.
218 std::string id;
219 };
220
tbansal75540a52015-07-15 22:18:21221 // Returns true if the cached network quality estimate was successfully read.
222 bool ReadCachedNetworkQualityEstimate();
223
tbansalba8f4112015-09-03 21:57:19224 // NetworkChangeNotifier::ConnectionTypeObserver implementation:
tbansal75540a52015-07-15 22:18:21225 void OnConnectionTypeChanged(
226 NetworkChangeNotifier::ConnectionType type) override;
227
tbansal2c6b0d32015-09-17 03:49:02228 // ExternalEstimateProvider::UpdatedEstimateObserver implementation.
229 void OnUpdatedEstimateAvailable() override;
230
tbansalea2fb8c2015-05-22 22:23:00231 private:
tbansal6f840fc52015-06-13 03:30:34232 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansal2e590c62015-07-09 00:22:02233 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestKbpsRTTUpdates);
tbansal6f840fc52015-06-13 03:30:34234 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestAddObservation);
tbansalb177b5392015-06-25 11:13:02235 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33236 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansalea2fb8c2015-05-22 22:23:00237 FRIEND_TEST_ALL_PREFIXES(URLRequestTestHTTP, NetworkQualityEstimator);
tbansal11af83132015-06-24 20:34:22238 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
239 PercentileSameTimestamps);
240 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
241 PercentileDifferentTimestamps);
242 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, ComputedPercentiles);
tbansal75540a52015-07-15 22:18:21243 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestCaching);
244 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
245 TestLRUCacheMaximumSize);
tbansalf7c6d522015-08-05 21:59:36246 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestGetMedianRTTSince);
tbansal2c6b0d32015-09-17 03:49:02247 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
248 TestExternalEstimateProvider);
249 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
250 TestExternalEstimateProviderMergeEstimates);
bengrf1b6738e2015-10-07 01:21:30251 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, TestObservers);
tbansal75540a52015-07-15 22:18:21252
tbansal9533b6f2015-08-11 22:06:08253 // NetworkQuality is used to cache the quality of a network connection.
254 class NET_EXPORT_PRIVATE NetworkQuality {
255 public:
256 NetworkQuality();
257 // |rtt| is the estimate of the round trip time.
258 // |downstream_throughput_kbps| is the estimate of the downstream
259 // throughput.
260 NetworkQuality(const base::TimeDelta& rtt,
261 int32_t downstream_throughput_kbps);
262 NetworkQuality(const NetworkQuality& other);
263 ~NetworkQuality();
264
265 NetworkQuality& operator=(const NetworkQuality& other);
266
267 // Returns the estimate of the round trip time.
268 const base::TimeDelta& rtt() const { return rtt_; }
269
270 // Returns the estimate of the downstream throughput in Kbps (Kilo bits per
271 // second).
272 int32_t downstream_throughput_kbps() const {
273 return downstream_throughput_kbps_;
274 }
275
276 private:
277 // Estimated round trip time.
278 base::TimeDelta rtt_;
279
280 // Estimated downstream throughput in Kbps.
281 int32_t downstream_throughput_kbps_;
282 };
283
tbansal75540a52015-07-15 22:18:21284 // CachedNetworkQuality stores the quality of a previously seen network.
285 class NET_EXPORT_PRIVATE CachedNetworkQuality {
286 public:
287 explicit CachedNetworkQuality(const NetworkQuality& network_quality);
288 CachedNetworkQuality(const CachedNetworkQuality& other);
289 ~CachedNetworkQuality();
290
291 // Returns the network quality associated with this cached entry.
shiva.jmad81bb8932015-08-31 05:25:43292 const NetworkQuality& network_quality() const { return network_quality_; }
tbansal75540a52015-07-15 22:18:21293
294 // Returns true if this cache entry was updated before
295 // |cached_network_quality|.
296 bool OlderThan(const CachedNetworkQuality& cached_network_quality) const;
297
298 // Time when this cache entry was last updated.
299 const base::TimeTicks last_update_time_;
300
301 // Quality of this cached network.
302 const NetworkQuality network_quality_;
303
304 private:
305 DISALLOW_ASSIGN(CachedNetworkQuality);
306 };
tbansalea2fb8c2015-05-22 22:23:00307
tbansal6f840fc52015-06-13 03:30:34308 // Records the round trip time or throughput observation, along with the time
bengrf1b6738e2015-10-07 01:21:30309 // the observation was made. The units of value are type specific. For round
310 // trip time observations, the value is in milliseconds. For throughput,
311 // the value is in kilobits per second. Observations can be made at several
312 // places in the network stack, thus the observation source is provided as
313 // well.
tbansal5659c35e2015-06-25 16:49:36314 struct NET_EXPORT_PRIVATE Observation {
bengrf1b6738e2015-10-07 01:21:30315 Observation(int32_t value,
316 base::TimeTicks timestamp,
317 ObservationSource source);
tbansal6f840fc52015-06-13 03:30:34318 ~Observation();
319
320 // Value of the observation.
321 const int32_t value;
322
323 // Time when the observation was taken.
324 const base::TimeTicks timestamp;
bengrf1b6738e2015-10-07 01:21:30325
326 // The source of the observation.
327 const ObservationSource source;
tbansal6f840fc52015-06-13 03:30:34328 };
329
tbansal11af83132015-06-24 20:34:22330 // Holds an observation and its weight.
tbansal75540a52015-07-15 22:18:21331 struct NET_EXPORT_PRIVATE WeightedObservation {
tbansal11af83132015-06-24 20:34:22332 WeightedObservation(int32_t value, double weight)
333 : value(value), weight(weight) {}
334 WeightedObservation(const WeightedObservation& other)
335 : WeightedObservation(other.value, other.weight) {}
336
337 WeightedObservation& operator=(const WeightedObservation& other) {
338 value = other.value;
339 weight = other.weight;
340 return *this;
341 }
342
343 // Required for sorting the samples in the ascending order of values.
344 bool operator<(const WeightedObservation& other) const {
345 return (value < other.value);
346 }
347
348 // Value of the sample.
349 int32_t value;
350
351 // Weight of the sample. This is computed based on how much time has passed
352 // since the sample was taken.
353 double weight;
354 };
355
tbansal6f840fc52015-06-13 03:30:34356 // Stores observations sorted by time.
tbansal5659c35e2015-06-25 16:49:36357 class NET_EXPORT_PRIVATE ObservationBuffer {
tbansal6f840fc52015-06-13 03:30:34358 public:
tbansal4418ad122015-06-26 19:06:33359 explicit ObservationBuffer(double weight_multiplier_per_second);
tbansal6f840fc52015-06-13 03:30:34360 ~ObservationBuffer();
361
362 // Adds |observation| to the buffer. The oldest observation in the buffer
363 // will be evicted to make room if the buffer is already full.
364 void AddObservation(const Observation& observation);
365
366 // Returns the number of observations in this buffer.
367 size_t Size() const;
368
369 // Clears the observations stored in this buffer.
370 void Clear();
371
tbansalf7c6d522015-08-05 21:59:36372 // Returns true iff the |percentile| value of the observations in this
373 // buffer is available. Sets |result| to the computed |percentile|
374 // value among all observations since |begin_timestamp|. If the value is
375 // unavailable, false is returned and |result| is not modified. Percentile
376 // value is unavailable if all the values in observation buffer are older
377 // than |begin_timestamp|.
378 // |result| must not be null.
379 bool GetPercentile(const base::TimeTicks& begin_timestamp,
380 int32_t* result,
381 int percentile) const;
tbansal11af83132015-06-24 20:34:22382
tbansal6f840fc52015-06-13 03:30:34383 private:
384 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, StoreObservations);
tbansalb177b5392015-06-25 11:13:02385 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest,
386 ObtainOperatingParams);
tbansal4418ad122015-06-26 19:06:33387 FRIEND_TEST_ALL_PREFIXES(NetworkQualityEstimatorTest, HalfLifeParam);
tbansal6f840fc52015-06-13 03:30:34388
tbansal11af83132015-06-24 20:34:22389 // Computes the weighted observations and stores them in
390 // |weighted_observations| sorted by ascending |WeightedObservation.value|.
tbansalf7c6d522015-08-05 21:59:36391 // Only the observations with timestamp later than |begin_timestamp| are
392 // considered. Also, sets |total_weight| to the total weight of all
393 // observations. Should be called only when there is at least one
394 // observation in the buffer.
tbansal11af83132015-06-24 20:34:22395 void ComputeWeightedObservations(
tbansalf7c6d522015-08-05 21:59:36396 const base::TimeTicks& begin_timestamp,
tbansal11af83132015-06-24 20:34:22397 std::vector<WeightedObservation>& weighted_observations,
398 double* total_weight) const;
399
tbansal6f840fc52015-06-13 03:30:34400 // Holds observations sorted by time, with the oldest observation at the
401 // front of the queue.
402 std::deque<Observation> observations_;
403
tbansal11af83132015-06-24 20:34:22404 // The factor by which the weight of an observation reduces every second.
405 // For example, if an observation is 6 seconds old, its weight would be:
406 // weight_multiplier_per_second_ ^ 6
407 // Calculated from |kHalfLifeSeconds| by solving the following equation:
408 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5
409 const double weight_multiplier_per_second_;
410
tbansal6f840fc52015-06-13 03:30:34411 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer);
412 };
413
tbansal75540a52015-07-15 22:18:21414 // This does not use a unordered_map or hash_map for code simplicity (key just
415 // implements operator<, rather than hash and equality) and because the map is
416 // tiny.
417 typedef std::map<NetworkID, CachedNetworkQuality> CachedNetworkQualities;
418
tbansal9533b6f2015-08-11 22:06:08419 // Throughput is set to |kInvalidThroughput| if a valid value is
420 // unavailable. Readers should discard throughput value if it is set to
421 // |kInvalidThroughput|.
422 static const int32_t kInvalidThroughput;
423
tbansalea2fb8c2015-05-22 22:23:00424 // Tiny transfer sizes may give inaccurate throughput results.
425 // Minimum size of the transfer over which the throughput is computed.
426 static const int kMinTransferSizeInBytes = 10000;
427
428 // Minimum duration (in microseconds) of the transfer over which the
429 // throughput is computed.
430 static const int kMinRequestDurationMicroseconds = 1000;
431
tbansalb177b5392015-06-25 11:13:02432 // Minimum valid value of the variation parameter that holds RTT (in
433 // milliseconds) values.
434 static const int kMinimumRTTVariationParameterMsec = 1;
435
436 // Minimum valid value of the variation parameter that holds throughput (in
437 // kbps) values.
438 static const int kMinimumThroughputVariationParameterKbps = 1;
439
tbansal75540a52015-07-15 22:18:21440 // Maximum size of the cache that holds network quality estimates.
441 // Smaller size may reduce the cache hit rate due to frequent evictions.
442 // Larger size may affect performance.
443 static const size_t kMaximumNetworkQualityCacheSize = 10;
444
445 // Maximum number of observations that can be held in the ObservationBuffer.
446 static const size_t kMaximumObservationsBufferSize = 300;
tbansalb177b5392015-06-25 11:13:02447
tbansal2c6b0d32015-09-17 03:49:02448 // Time duration (in milliseconds) after which the estimate provided by
449 // external estimate provider is considered stale.
450 static const int kExternalEstimateProviderFreshnessDurationMsec =
451 5 * 60 * 1000;
452
tbansal9533b6f2015-08-11 22:06:08453 // Returns the RTT value to be used when the valid RTT is unavailable. Readers
454 // should discard RTT if it is set to the value returned by |InvalidRTT()|.
455 static const base::TimeDelta InvalidRTT();
456
tbansal2c6b0d32015-09-17 03:49:02457 // Queries the external estimate provider for the latest network quality
458 // estimates, and adds those estimates to the current observation buffer.
459 void QueryExternalEstimateProvider();
tbansal1c92d5b2015-08-14 20:14:43460
tbansalb177b5392015-06-25 11:13:02461 // Obtains operating parameters from the field trial parameters.
462 void ObtainOperatingParams(
463 const std::map<std::string, std::string>& variation_params);
464
465 // Adds the default median RTT and downstream throughput estimate for the
466 // current connection type to the observation buffer.
467 void AddDefaultEstimates();
tbansalea2fb8c2015-05-22 22:23:00468
tbansal11af83132015-06-24 20:34:22469 // Returns an estimate of network quality at the specified |percentile|.
tbansalf7c6d522015-08-05 21:59:36470 // Only the observations later than |begin_timestamp| are taken into account.
tbansal11af83132015-06-24 20:34:22471 // |percentile| must be between 0 and 100 (both inclusive) with higher
472 // percentiles indicating less performant networks. For example, if
473 // |percentile| is 90, then the network is expected to be faster than the
474 // returned estimate with 0.9 probability. Similarly, network is expected to
475 // be slower than the returned estimate with 0.1 probability.
tbansalf7c6d522015-08-05 21:59:36476 base::TimeDelta GetRTTEstimateInternal(const base::TimeTicks& begin_timestamp,
477 int percentile) const;
478 int32_t GetDownlinkThroughputKbpsEstimateInternal(
479 const base::TimeTicks& begin_timestamp,
480 int percentile) const;
tbansal11af83132015-06-24 20:34:22481
tbansal75540a52015-07-15 22:18:21482 // Returns the current network ID checking by calling the platform APIs.
483 // Virtualized for testing.
484 virtual NetworkID GetCurrentNetworkID() const;
485
486 // Writes the estimated quality of the current network to the cache.
487 void CacheNetworkQualityEstimate();
488
bengrf1b6738e2015-10-07 01:21:30489 void NotifyObserversOfRTT(const Observation& observation);
490
491 void NotifyObserversOfThroughput(const Observation& observation);
492
tbansal2e590c62015-07-09 00:22:02493 // Records the UMA related to RTT.
494 void RecordRTTUMA(int32_t estimated_value_msec,
495 int32_t actual_value_msec) const;
496
tbansal79ed5cd2015-08-10 18:53:56497 // Returns true only if |request| can be used for network quality estimation.
498 // Only the requests that go over network are considered to provide useful
499 // observations.
500 bool RequestProvidesUsefulObservations(const URLRequest& request) const;
501
tbansal2c6b0d32015-09-17 03:49:02502 // Values of external estimate provider status. This enum must remain
503 // synchronized with the enum of the same name in
504 // metrics/histograms/histograms.xml.
505 enum NQEExternalEstimateProviderStatus {
506 EXTERNAL_ESTIMATE_PROVIDER_STATUS_NOT_AVAILABLE,
507 EXTERNAL_ESTIMATE_PROVIDER_STATUS_AVAILABLE,
508 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERIED,
509 EXTERNAL_ESTIMATE_PROVIDER_STATUS_QUERY_SUCCESSFUL,
510 EXTERNAL_ESTIMATE_PROVIDER_STATUS_CALLBACK,
511 EXTERNAL_ESTIMATE_PROVIDER_STATUS_BOUNDARY
512 };
513
514 // Records the metrics related to external estimate provider.
515 void RecordExternalEstimateProviderMetrics(
516 NQEExternalEstimateProviderStatus status) const;
517
tbansalea2fb8c2015-05-22 22:23:00518 // Determines if the requests to local host can be used in estimating the
519 // network quality. Set to true only for tests.
520 const bool allow_localhost_requests_;
521
tbansalafc2c1cb2015-06-15 23:58:59522 // Determines if the responses smaller than |kMinTransferSizeInBytes|
523 // or shorter than |kMinTransferSizeInBytes| can be used in estimating the
524 // network quality. Set to true only for tests.
525 const bool allow_small_responses_;
526
tbansalea2fb8c2015-05-22 22:23:00527 // Time when last connection change was observed.
528 base::TimeTicks last_connection_change_;
529
tbansal75540a52015-07-15 22:18:21530 // ID of the current network.
531 NetworkID current_network_id_;
tbansalea2fb8c2015-05-22 22:23:00532
tbansal75540a52015-07-15 22:18:21533 // Peak network quality (fastest round-trip-time (RTT) and highest
534 // downstream throughput) measured since last connectivity change. RTT is
535 // measured from time the request is sent until the first byte received.
536 // The accuracy is decreased by ignoring these factors:
tbansalea2fb8c2015-05-22 22:23:00537 // 1) Multiple URLRequests can occur concurrently.
tbansal75540a52015-07-15 22:18:21538 // 2) Includes server processing time.
539 NetworkQuality peak_network_quality_;
540
541 // Cache that stores quality of previously seen networks.
542 CachedNetworkQualities cached_network_qualities_;
tbansal6f840fc52015-06-13 03:30:34543
tbansal11af83132015-06-24 20:34:22544 // Buffer that holds Kbps observations sorted by timestamp.
tbansal79ed5cd2015-08-10 18:53:56545 ObservationBuffer downstream_throughput_kbps_observations_;
tbansal6f840fc52015-06-13 03:30:34546
tbansal11af83132015-06-24 20:34:22547 // Buffer that holds RTT (in milliseconds) observations sorted by timestamp.
tbansal6f840fc52015-06-13 03:30:34548 ObservationBuffer rtt_msec_observations_;
tbansalea2fb8c2015-05-22 22:23:00549
tbansalb177b5392015-06-25 11:13:02550 // Default network quality observations obtained from the network quality
551 // estimator field trial parameters. The observations are indexed by
552 // ConnectionType.
553 NetworkQuality
554 default_observations_[NetworkChangeNotifier::CONNECTION_LAST + 1];
555
tbansal2e590c62015-07-09 00:22:02556 // Estimated network quality. Updated on mainframe requests.
557 NetworkQuality estimated_median_network_quality_;
558
tbansal1c92d5b2015-08-14 20:14:43559 // ExternalEstimateProvider that provides network quality using operating
560 // system APIs. May be NULL.
tbansal2c6b0d32015-09-17 03:49:02561 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_;
tbansal1c92d5b2015-08-14 20:14:43562
bengrf1b6738e2015-10-07 01:21:30563 // Observer lists for round trip times and throughput measurements.
564 base::ObserverList<RTTObserver> rtt_observer_list_;
565 base::ObserverList<ThroughputObserver> throughput_observer_list_;
566
tbansalea2fb8c2015-05-22 22:23:00567 base::ThreadChecker thread_checker_;
568
569 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator);
570};
571
572} // namespace net
573
574#endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_