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/callback.h" |
16 #include "base/gtest_prod_util.h" | 17 #include "base/gtest_prod_util.h" |
17 #include "base/macros.h" | 18 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 19 #include "base/memory/ref_counted.h" |
19 #include "base/memory/scoped_ptr.h" | 20 #include "base/memory/scoped_ptr.h" |
| 21 #include "base/memory/weak_ptr.h" |
20 #include "base/observer_list.h" | 22 #include "base/observer_list.h" |
21 #include "base/threading/thread_checker.h" | 23 #include "base/threading/thread_checker.h" |
22 #include "base/time/time.h" | 24 #include "base/time/time.h" |
23 #include "net/base/external_estimate_provider.h" | 25 #include "net/base/external_estimate_provider.h" |
24 #include "net/base/net_export.h" | 26 #include "net/base/net_export.h" |
25 #include "net/base/network_change_notifier.h" | 27 #include "net/base/network_change_notifier.h" |
26 #include "net/base/socket_performance_watcher.h" | 28 #include "net/base/socket_performance_watcher.h" |
27 #include "net/base/socket_performance_watcher_factory.h" | 29 #include "net/base/socket_performance_watcher_factory.h" |
28 | 30 |
| 31 namespace base { |
| 32 class SingleThreadTaskRunner; |
| 33 } // namespace base |
| 34 |
29 namespace net { | 35 namespace net { |
30 | 36 |
31 class URLRequest; | 37 class URLRequest; |
32 | 38 |
33 // NetworkQualityEstimator provides network quality estimates (quality of the | 39 // NetworkQualityEstimator provides network quality estimates (quality of the |
34 // full paths to all origins that have been connected to). | 40 // full paths to all origins that have been connected to). |
35 // The estimates are based on the observed organic traffic. | 41 // The estimates are based on the observed organic traffic. |
36 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 42 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
37 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 43 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
38 // A single instance of NQE can be attached to multiple URLRequestContexts, | 44 // A single instance of NQE can be attached to multiple URLRequestContexts, |
39 // thereby increasing the single NQE instance's accuracy by providing more | 45 // thereby increasing the single NQE instance's accuracy by providing more |
40 // observed traffic characteristics. | 46 // observed traffic characteristics. |
41 class NET_EXPORT_PRIVATE NetworkQualityEstimator | 47 class NET_EXPORT_PRIVATE NetworkQualityEstimator |
42 : public NetworkChangeNotifier::ConnectionTypeObserver, | 48 : public NetworkChangeNotifier::ConnectionTypeObserver, |
43 public ExternalEstimateProvider::UpdatedEstimateDelegate, | 49 public ExternalEstimateProvider::UpdatedEstimateDelegate { |
44 public SocketPerformanceWatcherFactory { | |
45 public: | 50 public: |
46 // On Android, a Java counterpart will be generated for this enum. | 51 // On Android, a Java counterpart will be generated for this enum. |
47 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net | 52 // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.net |
48 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource | 53 // GENERATED_JAVA_CLASS_NAME_OVERRIDE: NetworkQualityObservationSource |
49 // GENERATED_JAVA_PREFIX_TO_STRIP: | 54 // GENERATED_JAVA_PREFIX_TO_STRIP: |
50 enum ObservationSource { | 55 enum ObservationSource { |
51 // The observation was taken at the request layer, e.g., a round trip time | 56 // 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 | 57 // is recorded as the time between the request being sent and the first byte |
53 // being received. | 58 // being received. |
54 URL_REQUEST, | 59 URL_REQUEST, |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
161 base::TimeDelta* rtt) const; | 166 base::TimeDelta* rtt) const; |
162 | 167 |
163 // Returns true if median downstream throughput is available and sets |kbps| | 168 // Returns true if median downstream throughput is available and sets |kbps| |
164 // to the median of downstream throughput (in kilobits per second) | 169 // to the median of downstream throughput (in kilobits per second) |
165 // observations since |begin_timestamp|. Virtualized for testing. |kbps| | 170 // observations since |begin_timestamp|. Virtualized for testing. |kbps| |
166 // should not be null. | 171 // should not be null. |
167 virtual bool GetRecentMedianDownlinkThroughputKbps( | 172 virtual bool GetRecentMedianDownlinkThroughputKbps( |
168 const base::TimeTicks& begin_timestamp, | 173 const base::TimeTicks& begin_timestamp, |
169 int32_t* kbps) const; | 174 int32_t* kbps) const; |
170 | 175 |
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 | 176 // Adds |rtt_observer| to the list of round trip time observers. Must be |
178 // called on the IO thread. | 177 // called on the IO thread. |
179 void AddRTTObserver(RTTObserver* rtt_observer); | 178 void AddRTTObserver(RTTObserver* rtt_observer); |
180 | 179 |
181 // Removes |rtt_observer| from the list of round trip time observers if it | 180 // 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. | 181 // is on the list of observers. Must be called on the IO thread. |
183 void RemoveRTTObserver(RTTObserver* rtt_observer); | 182 void RemoveRTTObserver(RTTObserver* rtt_observer); |
184 | 183 |
185 // Adds |throughput_observer| to the list of throughput observers. Must be | 184 // Adds |throughput_observer| to the list of throughput observers. Must be |
186 // called on the IO thread. | 185 // called on the IO thread. |
187 void AddThroughputObserver(ThroughputObserver* throughput_observer); | 186 void AddThroughputObserver(ThroughputObserver* throughput_observer); |
188 | 187 |
189 // Removes |throughput_observer| from the list of throughput observers if it | 188 // 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. | 189 // is on the list of observers. Must be called on the IO thread. |
191 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); | 190 void RemoveThroughputObserver(ThroughputObserver* throughput_observer); |
192 | 191 |
| 192 // Virtualized for testing. |
| 193 virtual SocketPerformanceWatcherFactory* GetSocketPerformanceWatcherFactory(); |
| 194 |
193 protected: | 195 protected: |
194 // NetworkID is used to uniquely identify a network. | 196 // NetworkID is used to uniquely identify a network. |
195 // For the purpose of network quality estimation and caching, a network is | 197 // For the purpose of network quality estimation and caching, a network is |
196 // uniquely identified by a combination of |type| and | 198 // uniquely identified by a combination of |type| and |
197 // |id|. This approach is unable to distinguish networks with | 199 // |id|. This approach is unable to distinguish networks with |
198 // same name (e.g., different Wi-Fi networks with same SSID). | 200 // same name (e.g., different Wi-Fi networks with same SSID). |
199 // This is a protected member to expose it to tests. | 201 // This is a protected member to expose it to tests. |
200 struct NET_EXPORT_PRIVATE NetworkID { | 202 struct NET_EXPORT_PRIVATE NetworkID { |
201 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) | 203 NetworkID(NetworkChangeNotifier::ConnectionType type, const std::string& id) |
202 : type(type), id(id) {} | 204 : 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. | 438 // 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: | 439 // For example, if an observation is 6 seconds old, its weight would be: |
438 // weight_multiplier_per_second_ ^ 6 | 440 // weight_multiplier_per_second_ ^ 6 |
439 // Calculated from |kHalfLifeSeconds| by solving the following equation: | 441 // Calculated from |kHalfLifeSeconds| by solving the following equation: |
440 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 | 442 // weight_multiplier_per_second_ ^ kHalfLifeSeconds = 0.5 |
441 const double weight_multiplier_per_second_; | 443 const double weight_multiplier_per_second_; |
442 | 444 |
443 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); | 445 DISALLOW_COPY_AND_ASSIGN(ObservationBuffer); |
444 }; | 446 }; |
445 | 447 |
| 448 typedef base::Callback<void( |
| 449 SocketPerformanceWatcherFactory::Protocol protocol, |
| 450 const base::TimeDelta& rtt)> |
| 451 RTTUpdateCallback; |
| 452 |
| 453 // SocketWatcherFactory implements SocketPerformanceWatcherFactory, and is |
| 454 // owned by |NetworkQualityEstimator|. |SocketWatcherFactory| is thread safe. |
| 455 class NET_EXPORT_PRIVATE SocketWatcherFactory |
| 456 : public SocketPerformanceWatcherFactory { |
| 457 public: |
| 458 SocketWatcherFactory( |
| 459 const scoped_refptr<base::SingleThreadTaskRunner>& task_runner, |
| 460 RTTUpdateCallback rtt_update_callback); |
| 461 ~SocketWatcherFactory() override; |
| 462 |
| 463 // SocketPerformanceWatcherFactory implementation: |
| 464 scoped_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher( |
| 465 const Protocol protocol) override; |
| 466 void OnUpdatedRTTAvailable(const Protocol protocol, |
| 467 const base::TimeDelta& rtt) override; |
| 468 void OnWatcherReset() override; |
| 469 |
| 470 private: |
| 471 // TaskRunner on which the |rtt_update_callback_| is invoked. |
| 472 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 473 |
| 474 RTTUpdateCallback rtt_update_callback_; |
| 475 |
| 476 base::WeakPtrFactory<SocketWatcherFactory> weak_ptr_factory_; |
| 477 |
| 478 DISALLOW_COPY_AND_ASSIGN(SocketWatcherFactory); |
| 479 }; |
| 480 |
446 // Value of round trip time observations is in base::TimeDelta. | 481 // Value of round trip time observations is in base::TimeDelta. |
447 typedef net::NetworkQualityEstimator::Observation<base::TimeDelta> | 482 typedef net::NetworkQualityEstimator::Observation<base::TimeDelta> |
448 RttObservation; | 483 RttObservation; |
449 typedef net::NetworkQualityEstimator::ObservationBuffer<base::TimeDelta> | 484 typedef net::NetworkQualityEstimator::ObservationBuffer<base::TimeDelta> |
450 RttObservationBuffer; | 485 RttObservationBuffer; |
451 | 486 |
452 // Value of throughput observations is in kilobits per second. | 487 // Value of throughput observations is in kilobits per second. |
453 typedef net::NetworkQualityEstimator::Observation<int32_t> | 488 typedef net::NetworkQualityEstimator::Observation<int32_t> |
454 ThroughputObservation; | 489 ThroughputObservation; |
455 typedef net::NetworkQualityEstimator::ObservationBuffer<int32_t> | 490 typedef net::NetworkQualityEstimator::ObservationBuffer<int32_t> |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
491 | 526 |
492 // Time duration (in milliseconds) after which the estimate provided by | 527 // Time duration (in milliseconds) after which the estimate provided by |
493 // external estimate provider is considered stale. | 528 // external estimate provider is considered stale. |
494 static const int kExternalEstimateProviderFreshnessDurationMsec = | 529 static const int kExternalEstimateProviderFreshnessDurationMsec = |
495 5 * 60 * 1000; | 530 5 * 60 * 1000; |
496 | 531 |
497 // Returns the RTT value to be used when the valid RTT is unavailable. Readers | 532 // 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()|. | 533 // should discard RTT if it is set to the value returned by |InvalidRTT()|. |
499 static const base::TimeDelta InvalidRTT(); | 534 static const base::TimeDelta InvalidRTT(); |
500 | 535 |
| 536 // Notifies |this| of a new transport layer RTT. |
| 537 void OnUpdatedRTTAvailable(SocketPerformanceWatcherFactory::Protocol protocol, |
| 538 const base::TimeDelta& rtt); |
| 539 |
501 // Queries the external estimate provider for the latest network quality | 540 // Queries the external estimate provider for the latest network quality |
502 // estimates, and adds those estimates to the current observation buffer. | 541 // estimates, and adds those estimates to the current observation buffer. |
503 void QueryExternalEstimateProvider(); | 542 void QueryExternalEstimateProvider(); |
504 | 543 |
505 // Obtains operating parameters from the field trial parameters. | 544 // Obtains operating parameters from the field trial parameters. |
506 void ObtainOperatingParams( | 545 void ObtainOperatingParams( |
507 const std::map<std::string, std::string>& variation_params); | 546 const std::map<std::string, std::string>& variation_params); |
508 | 547 |
509 // Adds the default median RTT and downstream throughput estimate for the | 548 // Adds the default median RTT and downstream throughput estimate for the |
510 // current connection type to the observation buffer. | 549 // current connection type to the observation buffer. |
(...skipping 17 matching lines...) Expand all Loading... |
528 // Virtualized for testing. | 567 // Virtualized for testing. |
529 virtual NetworkID GetCurrentNetworkID() const; | 568 virtual NetworkID GetCurrentNetworkID() const; |
530 | 569 |
531 // Writes the estimated quality of the current network to the cache. | 570 // Writes the estimated quality of the current network to the cache. |
532 void CacheNetworkQualityEstimate(); | 571 void CacheNetworkQualityEstimate(); |
533 | 572 |
534 void NotifyObserversOfRTT(const RttObservation& observation); | 573 void NotifyObserversOfRTT(const RttObservation& observation); |
535 | 574 |
536 void NotifyObserversOfThroughput(const ThroughputObservation& observation); | 575 void NotifyObserversOfThroughput(const ThroughputObservation& observation); |
537 | 576 |
| 577 base::WeakPtr<NetworkQualityEstimator> GetWeakPtr(); |
| 578 |
538 // Records the UMA related to RTT. | 579 // Records the UMA related to RTT. |
539 void RecordRTTUMA(int32_t estimated_value_msec, | 580 void RecordRTTUMA(int32_t estimated_value_msec, |
540 int32_t actual_value_msec) const; | 581 int32_t actual_value_msec) const; |
541 | 582 |
542 // Returns true only if |request| can be used for network quality estimation. | 583 // 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 | 584 // Only the requests that go over network are considered to provide useful |
544 // observations. | 585 // observations. |
545 bool RequestProvidesUsefulObservations(const URLRequest& request) const; | 586 bool RequestProvidesUsefulObservations(const URLRequest& request) const; |
546 | 587 |
547 // Values of external estimate provider status. This enum must remain | 588 // 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_; | 644 NetworkQuality estimated_median_network_quality_; |
604 | 645 |
605 // ExternalEstimateProvider that provides network quality using operating | 646 // ExternalEstimateProvider that provides network quality using operating |
606 // system APIs. May be NULL. | 647 // system APIs. May be NULL. |
607 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; | 648 const scoped_ptr<ExternalEstimateProvider> external_estimate_provider_; |
608 | 649 |
609 // Observer lists for round trip times and throughput measurements. | 650 // Observer lists for round trip times and throughput measurements. |
610 base::ObserverList<RTTObserver> rtt_observer_list_; | 651 base::ObserverList<RTTObserver> rtt_observer_list_; |
611 base::ObserverList<ThroughputObserver> throughput_observer_list_; | 652 base::ObserverList<ThroughputObserver> throughput_observer_list_; |
612 | 653 |
| 654 scoped_ptr<SocketPerformanceWatcherFactory> watcher_factory_; |
| 655 |
613 base::ThreadChecker thread_checker_; | 656 base::ThreadChecker thread_checker_; |
614 | 657 |
| 658 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; |
| 659 |
615 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 660 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
616 }; | 661 }; |
617 | 662 |
618 } // namespace net | 663 } // namespace net |
619 | 664 |
620 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ | 665 #endif // NET_BASE_NETWORK_QUALITY_ESTIMATOR_H_ |
OLD | NEW |