tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 1 | // Copyright 2016 The Chromium Authors. All rights reserved. |
| 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/callback.h" |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 9 | #include "base/memory/raw_ptr.h" |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 10 | #include "base/memory/ref_counted.h" |
| 11 | #include "base/memory/weak_ptr.h" |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 12 | #include "base/sequence_checker.h" |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 13 | #include "base/time/time.h" |
| 14 | #include "net/base/net_export.h" |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 15 | #include "net/nqe/network_quality_estimator_util.h" |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 16 | #include "net/socket/socket_performance_watcher.h" |
| 17 | #include "net/socket/socket_performance_watcher_factory.h" |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 18 | #include "third_party/abseil-cpp/absl/types/optional.h" |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 19 | |
| 20 | namespace base { |
| 21 | class SingleThreadTaskRunner; |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 22 | class TickClock; |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 23 | class TimeDelta; |
| 24 | } // namespace base |
| 25 | |
| 26 | namespace net { |
| 27 | |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 28 | class AddressList; |
| 29 | |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 30 | namespace { |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 31 | |
Anna Malova | bc6526f | 2020-03-04 19:13:29 | [diff] [blame] | 32 | typedef base::RepeatingCallback<void( |
| 33 | SocketPerformanceWatcherFactory::Protocol protocol, |
| 34 | const base::TimeDelta& rtt, |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 35 | const absl::optional<nqe::internal::IPHash>& host)> |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 36 | OnUpdatedRTTAvailableCallback; |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 37 | |
Anna Malova | bc6526f | 2020-03-04 19:13:29 | [diff] [blame] | 38 | typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback; |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 39 | |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 40 | } // namespace |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 41 | |
| 42 | namespace nqe { |
| 43 | |
| 44 | namespace internal { |
| 45 | |
| 46 | // SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe. |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 47 | class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher { |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 48 | public: |
| 49 | // Creates a SocketWatcher which can be used to watch a socket that uses |
| 50 | // |protocol| as the transport layer protocol. The socket watcher will call |
| 51 | // |updated_rtt_observation_callback| on |task_runner| every time a new RTT |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 52 | // observation is available. |address_list| is the list of addresses that |
| 53 | // the socket may connect to. |min_notification_interval| is the minimum |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 54 | // interval betweeen consecutive notifications to this socket watcher. |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 55 | // |allow_rtt_private_address| is true if |updated_rtt_observation_callback| |
| 56 | // should be called when RTT observation from a socket connected to private |
| 57 | // address is received. |tick_clock| is guaranteed to be non-null. |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 58 | // |should_notify_rtt_callback| callback should be called back on |
| 59 | // |task_runner| by the created socket watchers to check if RTT observation |
| 60 | // should be taken and notified. |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 61 | SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol, |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 62 | const AddressList& address_list, |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 63 | base::TimeDelta min_notification_interval, |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 64 | bool allow_rtt_private_address, |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 65 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 66 | OnUpdatedRTTAvailableCallback updated_rtt_observation_callback, |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 67 | ShouldNotifyRTTCallback should_notify_rtt_callback, |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 68 | const base::TickClock* tick_clock); |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 69 | |
Peter Boström | 293b134 | 2021-09-22 17:31:43 | [diff] [blame] | 70 | SocketWatcher(const SocketWatcher&) = delete; |
| 71 | SocketWatcher& operator=(const SocketWatcher&) = delete; |
| 72 | |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 73 | ~SocketWatcher() override; |
| 74 | |
| 75 | // SocketPerformanceWatcher implementation: |
| 76 | bool ShouldNotifyUpdatedRTT() const override; |
| 77 | void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override; |
| 78 | void OnConnectionChanged() override; |
| 79 | |
| 80 | private: |
| 81 | // Transport layer protocol used by the socket that |this| is watching. |
| 82 | const SocketPerformanceWatcherFactory::Protocol protocol_; |
| 83 | |
| 84 | scoped_refptr<base::SingleThreadTaskRunner> task_runner_; |
| 85 | |
| 86 | // Called every time a new RTT observation is available. |
| 87 | OnUpdatedRTTAvailableCallback updated_rtt_observation_callback_; |
| 88 | |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 89 | // Called to determine if the RTT notification should be notified using |
| 90 | // |updated_rtt_observation_callback_|. |
| 91 | ShouldNotifyRTTCallback should_notify_rtt_callback_; |
| 92 | |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 93 | // Minimum interval betweeen consecutive incoming notifications. |
| 94 | const base::TimeDelta rtt_notifications_minimum_interval_; |
| 95 | |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 96 | // True if the RTT observations from this socket can be notified using |
| 97 | // |updated_rtt_observation_callback_|. |
| 98 | const bool run_rtt_callback_; |
| 99 | |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 100 | // Time when this was last notified of updated RTT. |
| 101 | base::TimeTicks last_rtt_notification_; |
| 102 | |
Keishi Hattori | 0e45c02 | 2021-11-27 09:25:52 | [diff] [blame] | 103 | raw_ptr<const base::TickClock> tick_clock_; |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 104 | |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 105 | SEQUENCE_CHECKER(sequence_checker_); |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 106 | |
Tarun Bansal | 6a7df1f | 2017-10-25 18:15:42 | [diff] [blame] | 107 | // True if the first RTT notification from the QUIC connection has been |
| 108 | // received. |
Tsuyoshi Horo | 2fa6b8de | 2022-06-09 01:40:20 | [diff] [blame^] | 109 | bool first_quic_rtt_notification_received_ = false; |
Tarun Bansal | 6a7df1f | 2017-10-25 18:15:42 | [diff] [blame] | 110 | |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 111 | // A unique identifier for the remote host that this socket connects to. |
Anton Bikineev | 068d291 | 2021-05-15 20:43:52 | [diff] [blame] | 112 | const absl::optional<IPHash> host_; |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | } // namespace internal |
| 116 | |
| 117 | } // namespace nqe |
| 118 | |
| 119 | } // namespace net |
| 120 | |
bnc | 3698b0a0 | 2016-12-09 23:36:50 | [diff] [blame] | 121 | #endif // NET_NQE_SOCKET_WATCHER_H_ |