OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 5 #ifndef NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 6 #define NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
7 | 7 |
8 #include <stddef.h> | 8 #include <stddef.h> |
9 #include <stdint.h> | 9 #include <stdint.h> |
10 | 10 |
11 #include <deque> | 11 #include <deque> |
12 #include <map> | 12 #include <map> |
13 #include <string> | 13 #include <string> |
14 #include <tuple> | 14 #include <tuple> |
15 | 15 |
16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/scoped_ptr.h" | 19 #include "base/memory/scoped_ptr.h" |
20 #include "base/memory/weak_ptr.h" | |
20 #include "base/observer_list.h" | 21 #include "base/observer_list.h" |
21 #include "base/threading/thread_checker.h" | 22 #include "base/threading/thread_checker.h" |
22 #include "base/time/time.h" | 23 #include "base/time/time.h" |
23 #include "net/base/external_estimate_provider.h" | 24 #include "net/base/external_estimate_provider.h" |
24 #include "net/base/net_export.h" | 25 #include "net/base/net_export.h" |
25 #include "net/base/network_change_notifier.h" | 26 #include "net/base/network_change_notifier.h" |
26 #include "net/base/socket_performance_watcher.h" | 27 #include "net/base/socket_performance_watcher.h" |
27 #include "net/base/socket_performance_watcher_factory.h" | 28 #include "net/base/socket_performance_watcher_factory.h" |
28 | 29 |
30 namespace base { | |
31 class SingleThreadTaskRunner; | |
32 } // namespace base | |
33 | |
29 namespace net { | 34 namespace net { |
30 | 35 |
31 class URLRequest; | 36 class URLRequest; |
32 | 37 |
33 // NetworkQualityEstimator provides network quality estimates (quality of the | 38 // NetworkQualityEstimator provides network quality estimates (quality of the |
34 // full paths to all origins that have been connected to). | 39 // full paths to all origins that have been connected to). |
35 // The estimates are based on the observed organic traffic. | 40 // The estimates are based on the observed organic traffic. |
36 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 41 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
37 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 42 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
38 // A single instance of NQE can be attached to multiple URLRequestContexts, | 43 // A single instance of NQE can be attached to multiple URLRequestContexts, |
39 // thereby increasing the single NQE instance's accuracy by providing more | 44 // thereby increasing the single NQE instance's accuracy by providing more |
40 // observed traffic characteristics. | 45 // observed traffic characteristics. |
41 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 46 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
42 : public NetworkChangeNotifier::ConnectionTypeObserver, | 47 : public NetworkChangeNotifier::ConnectionTypeObserver, |
43 public ExternalEstimateProvider::UpdatedEstimateDelegate, | 48 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
44 public SocketPerformanceWatcherFactory { | |
45 public: | 49 public: |
46 // On Android, a Java counterpart will be generated for this enum. | 50 // On Android, a Java counterpart will be generated for this enum. |
47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | 51 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
48 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | 52 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
49 // GENERATED_JAVA_PREFIX_TO_STRIP: | 53 // GENERATED_JAVA_PREFIX_TO_STRIP: |
50 enum ObservationSource { | 54 enum ObservationSource { |
51 // The observation was taken at the request layer, e.g., a round trip time | 55 // The observation was taken at the request layer, e.g., a round trip time |
52 // is recorded as the time between the request being sent and the first byte | 56 // is recorded as the time between the request being sent and the first byte |
53 // being received. | 57 // being received. |
54 URL_REQUEST, | 58 URL_REQUEST, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
161 base::TimeDelta* rtt) const; | 165 base::TimeDelta* rtt) const; |
162 | 166 |
163 // Returns true if median downstream throughput is available and sets |kbps| | 167 // Returns true if median downstream throughput is available and sets |kbps| |
164 // to the median of downstream throughput (in kilobits per second) | 168 // to the median of downstream throughput (in kilobits per second) |
165 // observations since |begin_timestamp|. Virtualized for testing. |kbps| | 169 // observations since |begin_timestamp|. Virtualized for testing. |kbps| |
166 // should not be null. | 170 // should not be null. |
167 virtual bool GetRecentMedianDownlinkThroughputKbps( | 171 virtual bool GetRecentMedianDownlinkThroughputKbps( |
168 const base::TimeTicks& begin_timestamp, | 172 const base::TimeTicks& begin_timestamp, |
169 int32_t* kbps) const; | 173 int32_t* kbps) const; |
170 | 174 |
171 // SocketPerformanceWatcherFactory implementation: | |
172 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( | |
173 const Protocol protocol) override; | |
174 void OnUpdatedRTTAvailable(const Protocol protocol, | |
175 const base::TimeDelta& rtt) override; | |
176 | |
177 // Adds |rtt_observer| to the list of round trip time observers. Must be | 175 // Adds |rtt_observer| to the list of round trip time observers. Must be |
178 // called on the IO thread. | 176 // called on the IO thread. |
179 void AddRTTObserver(RTTObserver* rtt_observer); | 177 void AddRTTObserver(RTTObserver* rtt_observer); |
180 | 178 |
181 // Removes |rtt_observer| from the list of round trip time observers if it | 179 // Removes |rtt_observer| from the list of round trip time observers if it |
182 // is on the list of observers. Must be called on the IO thread. | 180 // is on the list of observers. Must be called on the IO thread. |
183 void RemoveRTTObserver(RTTObserver* rtt_observer); | 181 void RemoveRTTObserver(RTTObserver* rtt_observer); |
184 | 182 |
185 // Adds |throughput_observer| to the list of throughput observers. Must be | 183 // Adds |throughput_observer| to the list of throughput observers. Must be |
186 // called on the IO thread. | 184 // called on the IO thread. |
187 void AddThroughputObserver(ThroughputObserver* throughput_observer); | 185 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
188 | 186 |
189 // Removes |throughput_observer| from the list of throughput observers if it | 187 // Removes |throughput_observer| from the list of throughput observers if it |
190 // is on the list of observers. Must be called on the IO thread. | 188 // is on the list of observers. Must be called on the IO thread. |
191 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | 189 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
192 | 190 |
191 SocketPerformanceWatcherFactory* GetSocketPerformanceWatcherFactory(); | |
192 | |
193 protected: | 193 protected: |
194 // NetworkID is used to uniquely identify a network. | 194 // NetworkID is used to uniquely identify a network. |
195 // For the purpose of network quality estimation and caching, a network is | 195 // For the purpose of network quality estimation and caching, a network is |
196 // uniquely identified by a combination of |type| and | 196 // uniquely identified by a combination of |type| and |
197 // |id|. This approach is unable to distinguish networks with | 197 // |id|. This approach is unable to distinguish networks with |
198 // same name (e.g., different Wi-Fi networks with same SSID). | 198 // same name (e.g., different Wi-Fi networks with same SSID). |
199 // This is a protected member to expose it to tests. | 199 // This is a protected member to expose it to tests. |
200 struct NET_EXPORT_PRIVATE NetworkID { | 200 struct NET_EXPORT_PRIVATE NetworkID { |
201 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 201 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
202 : type(type), id(id) {} | 202 : type(type), id(id) {} |
(...skipping 233 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
436 // The factor by which the weight of an observation reduces every second. | 436 // The factor by which the weight of an observation reduces every second. |
437 // For example, if an observation is 6 seconds old, its weight would be: | 437 // For example, if an observation is 6 seconds old, its weight would be: |
438 // weight_multiplier_per_second_ ^ 6 | 438 // weight_multiplier_per_second_ ^ 6 |
439 // Calculated from |kHalfLifeSeconds| by solving the following equation: | 439 // Calculated from |kHalfLifeSeconds| by solving the following equation: |
440 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 | 440 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 |
441 const double weight_multiplier_per_second_; | 441 const double weight_multiplier_per_second_; |
442 | 442 |
443 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | 443 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
444 }; | 444 }; |
445 | 445 |
446 // SocketWatcher implements SocketPerformanceWatcher, and notifies | |
447 // NetworkQualityEstimator of various socket performance events on the | |
448 // provided |task_runner|. SocketWatcher is not thread-safe. | |
449 class SocketWatcher : public SocketPerformanceWatcher { | |
Ryan Sleevi
2016/04/02 00:48:20
Exposing this much implementation logic in your he
tbansal1
2016/04/04 16:55:44
Done. I could not move it to the anonymous namespa
| |
450 public: | |
451 SocketWatcher( | |
452 SocketPerformanceWatcherFactory::Protocol protocol, | |
453 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
Ryan Sleevi
2016/04/02 00:48:20
You should not be passing scoped_refptr<>'s as con
tbansal1
2016/04/04 16:55:44
Done.
| |
454 const base::WeakPtr<NetworkQualityEstimator>& | |
455 network_quality_estimator); | |
456 ~SocketWatcher(); | |
457 | |
458 // SocketPerformanceWatcher implementation: | |
459 void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override; | |
460 bool ShouldNotifyUpdatedRTT() const override; | |
461 void Reset() override; | |
462 | |
463 private: | |
464 // Transport layer protocol used by the socket that |this| is watching. | |
465 const SocketPerformanceWatcherFactory::Protocol protocol_; | |
466 | |
467 // TaskRunner on which |network_quality_estimator_| is notified. | |
468 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
469 | |
470 base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; | |
471 | |
472 base::ThreadChecker thread_checker_; | |
473 | |
474 DISALLOW_COPY_AND_ASSIGN(SocketWatcher); | |
475 }; | |
476 | |
477 // SocketWatcherFactory implements SocketPerformanceWatcherFactory, and is | |
478 // owned by |NetworkQualityEstimator|. |SocketWatcherFactory| is thread safe. | |
479 class NET_EXPORT_PRIVATE SocketWatcherFactory | |
480 : public SocketPerformanceWatcherFactory { | |
481 public: | |
482 SocketWatcherFactory( | |
483 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, | |
Ryan Sleevi
2016/04/02 00:48:20
pass & move.
tbansal1
2016/04/04 16:55:44
Done.
| |
484 const base::WeakPtr<NetworkQualityEstimator>& | |
485 network_quality_estimator); | |
486 ~SocketWatcherFactory() override; | |
487 | |
488 // SocketPerformanceWatcherFactory implementation: | |
489 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( | |
490 const Protocol protocol) override; | |
491 | |
492 private: | |
493 // TaskRunner on which the |network_quality_estimator_| is notified. | |
494 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; | |
495 | |
496 base::WeakPtr<NetworkQualityEstimator> network_quality_estimator_; | |
497 | |
498 DISALLOW_COPY_AND_ASSIGN(SocketWatcherFactory); | |
499 }; | |
500 | |
446 // Value of round trip time observations is in base::TimeDelta. | 501 // Value of round trip time observations is in base::TimeDelta. |
447 typedef net::NetworkQualityEstimator::Observation<base::TimeDelta> | 502 typedef net::NetworkQualityEstimator::Observation<base::TimeDelta> |
448 RttObservation; | 503 RttObservation; |
449 typedef net::NetworkQualityEstimator::ObservationBuffer<base::TimeDelta> | 504 typedef net::NetworkQualityEstimator::ObservationBuffer<base::TimeDelta> |
450 RttObservationBuffer; | 505 RttObservationBuffer; |
451 | 506 |
452 // Value of throughput observations is in kilobits per second. | 507 // Value of throughput observations is in kilobits per second. |
453 typedef net::NetworkQualityEstimator::Observation<int32_t> | 508 typedef net::NetworkQualityEstimator::Observation<int32_t> |
454 ThroughputObservation; | 509 ThroughputObservation; |
455 typedef net::NetworkQualityEstimator::ObservationBuffer<int32_t> | 510 typedef net::NetworkQualityEstimator::ObservationBuffer<int32_t> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
491 | 546 |
492 // Time duration (in milliseconds) after which the estimate provided by | 547 // Time duration (in milliseconds) after which the estimate provided by |
493 // external estimate provider is considered stale. | 548 // external estimate provider is considered stale. |
494 static const int kExternalEstimateProviderFreshnessDurationMsec = | 549 static const int kExternalEstimateProviderFreshnessDurationMsec = |
495 5 * 60 * 1000; | 550 5 * 60 * 1000; |
496 | 551 |
497 // Returns the RTT value to be used when the valid RTT is unavailable. Readers | 552 // Returns the RTT value to be used when the valid RTT is unavailable. Readers |
498 // should discard RTT if it is set to the value returned by |InvalidRTT()|. | 553 // should discard RTT if it is set to the value returned by |InvalidRTT()|. |
499 static const base::TimeDelta InvalidRTT(); | 554 static const base::TimeDelta InvalidRTT(); |
500 | 555 |
556 // Notifies |this| of a new transport layer RTT. | |
557 void OnUpdatedRTTAvailable(SocketPerformanceWatcherFactory::Protocol protocol, | |
558 const base::TimeDelta& rtt); | |
559 | |
501 // Queries the external estimate provider for the latest network quality | 560 // Queries the external estimate provider for the latest network quality |
502 // estimates, and adds those estimates to the current observation buffer. | 561 // estimates, and adds those estimates to the current observation buffer. |
503 void QueryExternalEstimateProvider(); | 562 void QueryExternalEstimateProvider(); |
504 | 563 |
505 // Obtains operating parameters from the field trial parameters. | 564 // Obtains operating parameters from the field trial parameters. |
506 void ObtainOperatingParams( | 565 void ObtainOperatingParams( |
507 const std::map<std::string, std::string>& variation_params); | 566 const std::map<std::string, std::string>& variation_params); |
508 | 567 |
509 // Adds the default median RTT and downstream throughput estimate for the | 568 // Adds the default median RTT and downstream throughput estimate for the |
510 // current connection type to the observation buffer. | 569 // current connection type to the observation buffer. |
(...skipping 17 matching lines...) Expand all Loading... | |
528 // Virtualized for testing. | 587 // Virtualized for testing. |
529 virtual NetworkID GetCurrentNetworkID() const; | 588 virtual NetworkID GetCurrentNetworkID() const; |
530 | 589 |
531 // Writes the estimated quality of the current network to the cache. | 590 // Writes the estimated quality of the current network to the cache. |
532 void CacheNetworkQualityEstimate(); | 591 void CacheNetworkQualityEstimate(); |
533 | 592 |
534 void NotifyObserversOfRTT(const RttObservation& observation); | 593 void NotifyObserversOfRTT(const RttObservation& observation); |
535 | 594 |
536 void NotifyObserversOfThroughput(const ThroughputObservation& observation); | 595 void NotifyObserversOfThroughput(const ThroughputObservation& observation); |
537 | 596 |
597 base::WeakPtr<NetworkQualityEstimator> GetWeakPtr(); | |
Ryan Sleevi
2016/04/02 00:48:20
Don't expose this as a method; you have a weak_ptr
tbansal1
2016/04/04 16:55:44
Done. I also realized that WatcherFactory was maki
Ryan Sleevi
2016/04/04 17:30:09
I apologize that this wasn't clear, although I tri
| |
598 | |
538 // Records the UMA related to RTT. | 599 // Records the UMA related to RTT. |
539 void RecordRTTUMA(int32_t estimated_value_msec, | 600 void RecordRTTUMA(int32_t estimated_value_msec, |
540 int32_t actual_value_msec) const; | 601 int32_t actual_value_msec) const; |
541 | 602 |
542 // Returns true only if |request| can be used for network quality estimation. | 603 // Returns true only if |request| can be used for network quality estimation. |
543 // Only the requests that go over network are considered to provide useful | 604 // Only the requests that go over network are considered to provide useful |
544 // observations. | 605 // observations. |
545 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 606 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
546 | 607 |
547 // Values of external estimate provider status. This enum must remain | 608 // Values of external estimate provider status. This enum must remain |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
603 NetworkQuality estimated_median_network_quality_; | 664 NetworkQuality estimated_median_network_quality_; |
604 | 665 |
605 // ExternalEstimateProvider that provides network quality using operating | 666 // ExternalEstimateProvider that provides network quality using operating |
606 // system APIs. May be NULL. | 667 // system APIs. May be NULL. |
607 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 668 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
608 | 669 |
609 // Observer lists for round trip times and throughput measurements. | 670 // Observer lists for round trip times and throughput measurements. |
610 base::ObserverList<RTTObserver> rtt_observer_list_; | 671 base::ObserverList<RTTObserver> rtt_observer_list_; |
611 base::ObserverList<ThroughputObserver> throughput_observer_list_; | 672 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
612 | 673 |
674 scoped_ptr<SocketPerformanceWatcherFactory> watcher_factory_; | |
675 | |
613 base::ThreadChecker thread_checker_; | 676 base::ThreadChecker thread_checker_; |
614 | 677 |
678 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; | |
679 | |
615 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 680 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
616 }; | 681 }; |
617 | 682 |
618 } // namespace net | 683 } // namespace net |
619 | 684 |
620 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 685 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |