1 // Copyright 2016 The Chromium Authors 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_NQE_SOCKET_WATCHER_H_ 6 #define NET_NQE_SOCKET_WATCHER_H_ 7 8 #include "base/functional/callback.h" 9 #include "base/memory/raw_ptr.h" 10 #include "base/memory/scoped_refptr.h" 11 #include "base/memory/weak_ptr.h" 12 #include "base/sequence_checker.h" 13 #include "base/time/time.h" 14 #include "net/base/net_export.h" 15 #include "net/nqe/network_quality_estimator_util.h" 16 #include "net/socket/socket_performance_watcher.h" 17 #include "net/socket/socket_performance_watcher_factory.h" 18 #include "third_party/abseil-cpp/absl/types/optional.h" 19 20 namespace base { 21 class SingleThreadTaskRunner; 22 class TickClock; 23 class TimeDelta; 24 } // namespace base 25 26 namespace net { 27 28 class IPAddress; 29 30 namespace { 31 32 typedef base::RepeatingCallback<void( 33 SocketPerformanceWatcherFactory::Protocol protocol, 34 const base::TimeDelta& rtt, 35 const absl::optional<nqe::internal::IPHash>& host)> 36 OnUpdatedRTTAvailableCallback; 37 38 typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback; 39 40 } // namespace 41 42 namespace nqe::internal { 43 44 // SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe. 45 class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher { 46 public: 47 // Creates a SocketWatcher which can be used to watch a socket that uses 48 // |protocol| as the transport layer protocol. The socket watcher will call 49 // |updated_rtt_observation_callback| on |task_runner| every time a new RTT 50 // observation is available. |address| is the IPAddress that the socket may 51 // connect to. |min_notification_interval| is the minimum interval between 52 // consecutive notifications to this socket watcher. 53 // |allow_rtt_private_address| is true if |updated_rtt_observation_callback| 54 // should be called when RTT observation from a socket connected to private 55 // address is received. |tick_clock| is guaranteed to be non-null. 56 // |should_notify_rtt_callback| callback should be called back on 57 // |task_runner| by the created socket watchers to check if RTT observation 58 // should be taken and notified. 59 SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol, 60 const IPAddress& address, 61 base::TimeDelta min_notification_interval, 62 bool allow_rtt_private_address, 63 scoped_refptr<base::SingleThreadTaskRunner> task_runner, 64 OnUpdatedRTTAvailableCallback updated_rtt_observation_callback, 65 ShouldNotifyRTTCallback should_notify_rtt_callback, 66 const base::TickClock* tick_clock); 67 68 SocketWatcher(const SocketWatcher&) = delete; 69 SocketWatcher& operator=(const SocketWatcher&) = delete; 70 71 ~SocketWatcher() override; 72 73 // SocketPerformanceWatcher implementation: 74 bool ShouldNotifyUpdatedRTT() const override; 75 void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override; 76 void OnConnectionChanged() override; 77 78 private: 79 // Transport layer protocol used by the socket that |this| is watching. 80 const SocketPerformanceWatcherFactory::Protocol protocol_; 81 82 scoped_refptr<base::SingleThreadTaskRunner> task_runner_; 83 84 // Called every time a new RTT observation is available. 85 OnUpdatedRTTAvailableCallback updated_rtt_observation_callback_; 86 87 // Called to determine if the RTT notification should be notified using 88 // |updated_rtt_observation_callback_|. 89 ShouldNotifyRTTCallback should_notify_rtt_callback_; 90 91 // Minimum interval betweeen consecutive incoming notifications. 92 const base::TimeDelta rtt_notifications_minimum_interval_; 93 94 // True if the RTT observations from this socket can be notified using 95 // |updated_rtt_observation_callback_|. 96 const bool run_rtt_callback_; 97 98 // Time when this was last notified of updated RTT. 99 base::TimeTicks last_rtt_notification_; 100 101 raw_ptr<const base::TickClock> tick_clock_; 102 103 SEQUENCE_CHECKER(sequence_checker_); 104 105 // True if the first RTT notification from the QUIC connection has been 106 // received. 107 bool first_quic_rtt_notification_received_ = false; 108 109 // A unique identifier for the remote host that this socket connects to. 110 const absl::optional<IPHash> host_; 111 }; 112 113 } // namespace nqe::internal 114 115 } // namespace net 116 117 #endif // NET_NQE_SOCKET_WATCHER_H_ 118