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