| 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_NQE_NETWORK_QUALITY_ESTIMATOR_H_ | 5 #ifndef NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 6 #define NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ | 6 #define NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <map> | 10 #include <map> |
| 11 #include <memory> | 11 #include <memory> |
| 12 #include <string> | 12 #include <string> |
| 13 | 13 |
| 14 #include "base/compiler_specific.h" | 14 #include "base/compiler_specific.h" |
| 15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
| 16 #include "base/macros.h" | 16 #include "base/macros.h" |
| 17 #include "base/memory/ref_counted.h" | 17 #include "base/memory/ref_counted.h" |
| 18 #include "base/memory/weak_ptr.h" | 18 #include "base/memory/weak_ptr.h" |
| 19 #include "base/observer_list.h" | 19 #include "base/observer_list.h" |
| 20 #include "base/optional.h" | 20 #include "base/optional.h" |
| 21 #include "base/threading/thread_checker.h" | 21 #include "base/threading/thread_checker.h" |
| 22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
| 23 #include "net/base/net_export.h" | 23 #include "net/base/net_export.h" |
| 24 #include "net/base/network_change_notifier.h" | 24 #include "net/base/network_change_notifier.h" |
| 25 #include "net/nqe/cached_network_quality.h" | 25 #include "net/nqe/cached_network_quality.h" |
| 26 #include "net/nqe/effective_connection_type.h" | 26 #include "net/nqe/effective_connection_type.h" |
| 27 #include "net/nqe/effective_connection_type_observer.h" |
| 27 #include "net/nqe/event_creator.h" | 28 #include "net/nqe/event_creator.h" |
| 28 #include "net/nqe/external_estimate_provider.h" | 29 #include "net/nqe/external_estimate_provider.h" |
| 29 #include "net/nqe/network_id.h" | 30 #include "net/nqe/network_id.h" |
| 30 #include "net/nqe/network_quality.h" | 31 #include "net/nqe/network_quality.h" |
| 31 #include "net/nqe/network_quality_estimator_params.h" | 32 #include "net/nqe/network_quality_estimator_params.h" |
| 32 #include "net/nqe/network_quality_observation.h" | 33 #include "net/nqe/network_quality_observation.h" |
| 33 #include "net/nqe/network_quality_observation_source.h" | 34 #include "net/nqe/network_quality_observation_source.h" |
| 35 #include "net/nqe/network_quality_provider.h" |
| 34 #include "net/nqe/network_quality_store.h" | 36 #include "net/nqe/network_quality_store.h" |
| 35 #include "net/nqe/observation_buffer.h" | 37 #include "net/nqe/observation_buffer.h" |
| 38 #include "net/nqe/rtt_throughput_estimates_observer.h" |
| 36 #include "net/socket/socket_performance_watcher_factory.h" | 39 #include "net/socket/socket_performance_watcher_factory.h" |
| 37 | 40 |
| 38 namespace base { | 41 namespace base { |
| 39 class TickClock; | 42 class TickClock; |
| 40 } // namespace base | 43 } // namespace base |
| 41 | 44 |
| 42 namespace net { | 45 namespace net { |
| 43 | 46 |
| 44 class NetLog; | 47 class NetLog; |
| 45 | 48 |
| 46 namespace nqe { | 49 namespace nqe { |
| 47 namespace internal { | 50 namespace internal { |
| 48 class ThroughputAnalyzer; | 51 class ThroughputAnalyzer; |
| 49 } | 52 } |
| 50 } | 53 } |
| 51 | 54 |
| 52 class URLRequest; | 55 class URLRequest; |
| 53 | 56 |
| 54 // NetworkQualityEstimator provides network quality estimates (quality of the | 57 // NetworkQualityEstimator provides network quality estimates (quality of the |
| 55 // full paths to all origins that have been connected to). | 58 // full paths to all origins that have been connected to). |
| 56 // The estimates are based on the observed organic traffic. | 59 // The estimates are based on the observed organic traffic. |
| 57 // A NetworkQualityEstimator instance is attached to URLRequestContexts and | 60 // A NetworkQualityEstimator instance is attached to URLRequestContexts and |
| 58 // observes the traffic of URLRequests spawned from the URLRequestContexts. | 61 // observes the traffic of URLRequests spawned from the URLRequestContexts. |
| 59 // A single instance of NQE can be attached to multiple URLRequestContexts, | 62 // A single instance of NQE can be attached to multiple URLRequestContexts, |
| 60 // thereby increasing the single NQE instance's accuracy by providing more | 63 // thereby increasing the single NQE instance's accuracy by providing more |
| 61 // observed traffic characteristics. | 64 // observed traffic characteristics. |
| 62 class NET_EXPORT NetworkQualityEstimator | 65 class NET_EXPORT NetworkQualityEstimator |
| 63 : public NetworkChangeNotifier::ConnectionTypeObserver, | 66 : public NetworkChangeNotifier::ConnectionTypeObserver, |
| 64 public ExternalEstimateProvider::UpdatedEstimateDelegate { | 67 public ExternalEstimateProvider::UpdatedEstimateDelegate, |
| 68 public NetworkQualityProvider { |
| 65 public: | 69 public: |
| 66 // Observes changes in effective connection type. | |
| 67 class NET_EXPORT EffectiveConnectionTypeObserver { | |
| 68 public: | |
| 69 // Notifies the observer of a change in the effective connection type. | |
| 70 // NetworkQualityEstimator computes the effective connection type once in | |
| 71 // every interval of duration | |
| 72 // |effective_connection_type_recomputation_interval_|. Additionally, when | |
| 73 // there is a change in the connection type of the device, then the | |
| 74 // effective connection type is immediately recomputed. The observer must | |
| 75 // register and unregister itself on the IO thread. All the observers would | |
| 76 // be notified on the IO thread. | |
| 77 // | |
| 78 // If the computed effective connection type is different from the | |
| 79 // previously notified effective connection type, then all the registered | |
| 80 // observers are notified of the new effective connection type. | |
| 81 virtual void OnEffectiveConnectionTypeChanged( | |
| 82 EffectiveConnectionType type) = 0; | |
| 83 | |
| 84 protected: | |
| 85 EffectiveConnectionTypeObserver() {} | |
| 86 virtual ~EffectiveConnectionTypeObserver() {} | |
| 87 | |
| 88 private: | |
| 89 DISALLOW_COPY_AND_ASSIGN(EffectiveConnectionTypeObserver); | |
| 90 }; | |
| 91 | |
| 92 // Observes changes in the network quality. | |
| 93 class NET_EXPORT RTTAndThroughputEstimatesObserver { | |
| 94 public: | |
| 95 // Notifies the observer when estimated HTTP RTT, estimated transport RTT or | |
| 96 // estimated downstream throughput is computed. NetworkQualityEstimator | |
| 97 // computes the RTT and throughput estimates at regular intervals. | |
| 98 // Additionally, when there is a change in the connection type of the | |
| 99 // device, then the estimates are immediately computed. The observer must | |
| 100 // register and unregister itself on the IO thread. All the observers would | |
| 101 // be notified on the IO thread. | |
| 102 // | |
| 103 // |http_rtt|, |transport_rtt| and |downstream_throughput_kbps| are the | |
| 104 // computed estimates of the HTTP RTT, transport RTT and downstream | |
| 105 // throughput (in kilobits per second), respectively. If an estimate of the | |
| 106 // HTTP or transport RTT is unavailable, it will be set to | |
| 107 // nqe::internal::InvalidRTT(). If the throughput estimate is unavailable, | |
| 108 // it will be set to nqe::internal::kInvalidThroughput. | |
| 109 virtual void OnRTTOrThroughputEstimatesComputed( | |
| 110 base::TimeDelta http_rtt, | |
| 111 base::TimeDelta transport_rtt, | |
| 112 int32_t downstream_throughput_kbps) = 0; | |
| 113 | |
| 114 virtual ~RTTAndThroughputEstimatesObserver() {} | |
| 115 | |
| 116 protected: | |
| 117 RTTAndThroughputEstimatesObserver() {} | |
| 118 | |
| 119 private: | |
| 120 DISALLOW_COPY_AND_ASSIGN(RTTAndThroughputEstimatesObserver); | |
| 121 }; | |
| 122 | |
| 123 // Observes measurements of round trip time. | 70 // Observes measurements of round trip time. |
| 124 class NET_EXPORT_PRIVATE RTTObserver { | 71 class NET_EXPORT_PRIVATE RTTObserver { |
| 125 public: | 72 public: |
| 126 // Will be called when a new RTT observation is available. The round trip | 73 // Will be called when a new RTT observation is available. The round trip |
| 127 // time is specified in milliseconds. The time when the observation was | 74 // time is specified in milliseconds. The time when the observation was |
| 128 // taken and the source of the observation are provided. | 75 // taken and the source of the observation are provided. |
| 129 virtual void OnRTTObservation(int32_t rtt_ms, | 76 virtual void OnRTTObservation(int32_t rtt_ms, |
| 130 const base::TimeTicks& timestamp, | 77 const base::TimeTicks& timestamp, |
| 131 NetworkQualityObservationSource source) = 0; | 78 NetworkQualityObservationSource source) = 0; |
| 132 | 79 |
| (...skipping 16 matching lines...) Expand all Loading... |
| 149 NetworkQualityObservationSource source) = 0; | 96 NetworkQualityObservationSource source) = 0; |
| 150 | 97 |
| 151 protected: | 98 protected: |
| 152 ThroughputObserver() {} | 99 ThroughputObserver() {} |
| 153 virtual ~ThroughputObserver() {} | 100 virtual ~ThroughputObserver() {} |
| 154 | 101 |
| 155 private: | 102 private: |
| 156 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); | 103 DISALLOW_COPY_AND_ASSIGN(ThroughputObserver); |
| 157 }; | 104 }; |
| 158 | 105 |
| 159 // Provides simple interface to obtain the effective connection type. | |
| 160 class NET_EXPORT NetworkQualityProvider { | |
| 161 public: | |
| 162 virtual ~NetworkQualityProvider() {} | |
| 163 | |
| 164 // Returns the current effective connection type. | |
| 165 virtual EffectiveConnectionType GetEffectiveConnectionType() const = 0; | |
| 166 | |
| 167 // Adds |observer| to a list of effective connection type observers. | |
| 168 virtual void AddEffectiveConnectionTypeObserver( | |
| 169 EffectiveConnectionTypeObserver* observer) = 0; | |
| 170 | |
| 171 // Removes |observer| from a list of effective connection type observers. | |
| 172 virtual void RemoveEffectiveConnectionTypeObserver( | |
| 173 EffectiveConnectionTypeObserver* observer) = 0; | |
| 174 | |
| 175 // Returns the current HTTP RTT estimate. If the estimate is unavailable, | |
| 176 // the returned optional value is null. | |
| 177 virtual base::Optional<base::TimeDelta> GetHttpRTT() const; | |
| 178 | |
| 179 // Returns the current transport RTT estimate. If the estimate is | |
| 180 // unavailable, the returned optional value is null. | |
| 181 virtual base::Optional<base::TimeDelta> GetTransportRTT() const; | |
| 182 | |
| 183 // Returns the current downstream throughput estimate (in kilobits per | |
| 184 // second). If the estimate is unavailable, the returned optional value is | |
| 185 // null. | |
| 186 virtual base::Optional<int32_t> GetDownstreamThroughputKbps() const; | |
| 187 | |
| 188 // Adds |observer| to the list of RTT and throughput estimate observers. | |
| 189 // |observer| would be notified of the current RTT and throughput estimates | |
| 190 // in the next message pump. | |
| 191 virtual void AddRTTAndThroughputEstimatesObserver( | |
| 192 RTTAndThroughputEstimatesObserver* observer) = 0; | |
| 193 | |
| 194 // Removes |observer| from the list of RTT and throughput estimate | |
| 195 // observers. | |
| 196 virtual void RemoveRTTAndThroughputEstimatesObserver( | |
| 197 RTTAndThroughputEstimatesObserver* observer) = 0; | |
| 198 | |
| 199 protected: | |
| 200 NetworkQualityProvider() {} | |
| 201 | |
| 202 private: | |
| 203 DISALLOW_COPY_AND_ASSIGN(NetworkQualityProvider); | |
| 204 }; | |
| 205 | |
| 206 // Creates a new NetworkQualityEstimator. | 106 // Creates a new NetworkQualityEstimator. |
| 207 // |variation_params| is the map containing all field trial parameters | 107 // |variation_params| is the map containing all field trial parameters |
| 208 // related to NetworkQualityEstimator field trial. | 108 // related to NetworkQualityEstimator field trial. |
| 209 // |external_estimates_provider| may be NULL. The caller must guarantee that | 109 // |external_estimates_provider| may be NULL. The caller must guarantee that |
| 210 // |net_log| outlives |this|. | 110 // |net_log| outlives |this|. |
| 211 NetworkQualityEstimator( | 111 NetworkQualityEstimator( |
| 212 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 112 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 213 const std::map<std::string, std::string>& variation_params, | 113 const std::map<std::string, std::string>& variation_params, |
| 214 NetLog* net_log); | 114 NetLog* net_log); |
| 215 | 115 |
| (...skipping 12 matching lines...) Expand all Loading... |
| 228 // outlives |this|. | 128 // outlives |this|. |
| 229 NetworkQualityEstimator( | 129 NetworkQualityEstimator( |
| 230 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 130 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 231 const std::map<std::string, std::string>& variation_params, | 131 const std::map<std::string, std::string>& variation_params, |
| 232 bool use_local_host_requests_for_tests, | 132 bool use_local_host_requests_for_tests, |
| 233 bool use_smaller_responses_for_tests, | 133 bool use_smaller_responses_for_tests, |
| 234 NetLog* net_log); | 134 NetLog* net_log); |
| 235 | 135 |
| 236 ~NetworkQualityEstimator() override; | 136 ~NetworkQualityEstimator() override; |
| 237 | 137 |
| 238 // Returns the last computed effective type of the current connection. The | |
| 239 // effective connection type is computed by the network quality estimator at | |
| 240 // regular intervals and at certain events (e.g., connection change). | |
| 241 // Virtualized for testing. | |
| 242 virtual EffectiveConnectionType GetEffectiveConnectionType() const; | |
| 243 | |
| 244 // Returns the effective type of the current connection based on only the | 138 // Returns the effective type of the current connection based on only the |
| 245 // samples observed after |start_time|. This should only be used for | 139 // samples observed after |start_time|. This should only be used for |
| 246 // recording the metrics. Virtualized for testing. | 140 // recording the metrics. Virtualized for testing. |
| 247 virtual EffectiveConnectionType GetRecentEffectiveConnectionType( | 141 virtual EffectiveConnectionType GetRecentEffectiveConnectionType( |
| 248 const base::TimeTicks& start_time) const; | 142 const base::TimeTicks& start_time) const; |
| 249 | 143 |
| 250 // Adds |observer| to the list of effective connection type observers. Must be | 144 // NetworkQualityProvider implementation: |
| 251 // called on the IO thread. |observer| would be notified of the current | 145 // Must be called on the IO thread. |
| 252 // effective connection type in the next message pump. | 146 EffectiveConnectionType GetEffectiveConnectionType() const override; |
| 253 void AddEffectiveConnectionTypeObserver( | 147 void AddEffectiveConnectionTypeObserver( |
| 254 EffectiveConnectionTypeObserver* observer); | 148 EffectiveConnectionTypeObserver* observer) override; |
| 255 | |
| 256 // Removes |observer| from the list of effective connection type observers. | |
| 257 // Must be called on the IO thread. | |
| 258 void RemoveEffectiveConnectionTypeObserver( | 149 void RemoveEffectiveConnectionTypeObserver( |
| 259 EffectiveConnectionTypeObserver* observer); | 150 EffectiveConnectionTypeObserver* observer) override; |
| 260 | 151 base::Optional<base::TimeDelta> GetHttpRTT() const override; |
| 261 // Adds |observer| to the list of RTT and throughput estimate observers. Must | 152 base::Optional<base::TimeDelta> GetTransportRTT() const override; |
| 262 // be called on the IO thread. |observer| would be notified of the current RTT | 153 base::Optional<int32_t> GetDownstreamThroughputKbps() const override; |
| 263 // and throughput estimates in the next message pump. | |
| 264 void AddRTTAndThroughputEstimatesObserver( | 154 void AddRTTAndThroughputEstimatesObserver( |
| 265 RTTAndThroughputEstimatesObserver* observer); | 155 RTTAndThroughputEstimatesObserver* observer) override; |
| 266 | |
| 267 // Removes |observer| from the list of RTT and throughput estimate observers. | |
| 268 // Must be called on the IO thread. | |
| 269 void RemoveRTTAndThroughputEstimatesObserver( | 156 void RemoveRTTAndThroughputEstimatesObserver( |
| 270 RTTAndThroughputEstimatesObserver* observer); | 157 RTTAndThroughputEstimatesObserver* observer) override; |
| 271 | 158 |
| 272 // Notifies NetworkQualityEstimator that the response header of |request| has | 159 // Notifies NetworkQualityEstimator that the response header of |request| has |
| 273 // been received. | 160 // been received. |
| 274 void NotifyHeadersReceived(const URLRequest& request); | 161 void NotifyHeadersReceived(const URLRequest& request); |
| 275 | 162 |
| 276 // Notifies NetworkQualityEstimator that the headers of |request| are about to | 163 // Notifies NetworkQualityEstimator that the headers of |request| are about to |
| 277 // be sent. | 164 // be sent. |
| 278 void NotifyStartTransaction(const URLRequest& request); | 165 void NotifyStartTransaction(const URLRequest& request); |
| 279 | 166 |
| 280 // Notifies NetworkQualityEstimator that the response body of |request| has | 167 // Notifies NetworkQualityEstimator that the response body of |request| has |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 334 void RemoveNetworkQualitiesCacheObserver( | 221 void RemoveNetworkQualitiesCacheObserver( |
| 335 nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver* | 222 nqe::internal::NetworkQualityStore::NetworkQualitiesCacheObserver* |
| 336 observer); | 223 observer); |
| 337 | 224 |
| 338 // Called when the persistent prefs have been read. |read_prefs| contains the | 225 // Called when the persistent prefs have been read. |read_prefs| contains the |
| 339 // parsed prefs as a map between NetworkIDs and CachedNetworkQualities. | 226 // parsed prefs as a map between NetworkIDs and CachedNetworkQualities. |
| 340 void OnPrefsRead( | 227 void OnPrefsRead( |
| 341 const std::map<nqe::internal::NetworkID, | 228 const std::map<nqe::internal::NetworkID, |
| 342 nqe::internal::CachedNetworkQuality> read_prefs); | 229 nqe::internal::CachedNetworkQuality> read_prefs); |
| 343 | 230 |
| 344 // Returns the current transport RTT estimate. If the estimate is unavailable, | |
| 345 // the returned optional has no value. | |
| 346 base::Optional<base::TimeDelta> GetTransportRTT() const; | |
| 347 | |
| 348 protected: | 231 protected: |
| 349 // A protected constructor for testing that allows setting the value of | 232 // A protected constructor for testing that allows setting the value of |
| 350 // |add_default_platform_observations_|. | 233 // |add_default_platform_observations_|. |
| 351 NetworkQualityEstimator( | 234 NetworkQualityEstimator( |
| 352 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, | 235 std::unique_ptr<ExternalEstimateProvider> external_estimates_provider, |
| 353 const std::map<std::string, std::string>& variation_params, | 236 const std::map<std::string, std::string>& variation_params, |
| 354 bool use_local_host_requests_for_tests, | 237 bool use_local_host_requests_for_tests, |
| 355 bool use_smaller_responses_for_tests, | 238 bool use_smaller_responses_for_tests, |
| 356 bool add_default_platform_observations, | 239 bool add_default_platform_observations, |
| 357 const NetLogWithSource& net_log); | 240 const NetLogWithSource& net_log); |
| (...skipping 435 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 793 disallowed_observation_sources_for_transport_; | 676 disallowed_observation_sources_for_transport_; |
| 794 | 677 |
| 795 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; | 678 base::WeakPtrFactory<NetworkQualityEstimator> weak_ptr_factory_; |
| 796 | 679 |
| 797 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); | 680 DISALLOW_COPY_AND_ASSIGN(NetworkQualityEstimator); |
| 798 }; | 681 }; |
| 799 | 682 |
| 800 } // namespace net | 683 } // namespace net |
| 801 | 684 |
| 802 #endif // NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ | 685 #endif // NET_NQE_NETWORK_QUALITY_ESTIMATOR_H_ |
| OLD | NEW |