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 | |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 8 | #include <memory> |
| 9 | |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 10 | #include "base/callback.h" |
| 11 | #include "base/memory/ref_counted.h" |
| 12 | #include "base/memory/weak_ptr.h" |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 13 | #include "base/optional.h" |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 14 | #include "base/sequence_checker.h" |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 15 | #include "base/time/time.h" |
| 16 | #include "net/base/net_export.h" |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 17 | #include "net/nqe/network_quality_estimator_util.h" |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 18 | #include "net/socket/socket_performance_watcher.h" |
| 19 | #include "net/socket/socket_performance_watcher_factory.h" |
| 20 | |
| 21 | namespace base { |
| 22 | class SingleThreadTaskRunner; |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 23 | class TickClock; |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 24 | class TimeDelta; |
| 25 | } // namespace base |
| 26 | |
| 27 | namespace net { |
| 28 | |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 29 | class AddressList; |
| 30 | |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 31 | namespace { |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 32 | |
Anna Malova | bc6526f | 2020-03-04 19:13:29 | [diff] [blame^] | 33 | typedef base::RepeatingCallback<void( |
| 34 | SocketPerformanceWatcherFactory::Protocol protocol, |
| 35 | const base::TimeDelta& rtt, |
| 36 | const base::Optional<nqe::internal::IPHash>& host)> |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 37 | OnUpdatedRTTAvailableCallback; |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 38 | |
Anna Malova | bc6526f | 2020-03-04 19:13:29 | [diff] [blame^] | 39 | typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback; |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 40 | |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 41 | } // namespace |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 42 | |
| 43 | namespace nqe { |
| 44 | |
| 45 | namespace internal { |
| 46 | |
| 47 | // SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe. |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 48 | class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher { |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 49 | public: |
| 50 | // Creates a SocketWatcher which can be used to watch a socket that uses |
| 51 | // |protocol| as the transport layer protocol. The socket watcher will call |
| 52 | // |updated_rtt_observation_callback| on |task_runner| every time a new RTT |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 53 | // observation is available. |address_list| is the list of addresses that |
| 54 | // the socket may connect to. |min_notification_interval| is the minimum |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 55 | // interval betweeen consecutive notifications to this socket watcher. |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 56 | // |allow_rtt_private_address| is true if |updated_rtt_observation_callback| |
| 57 | // should be called when RTT observation from a socket connected to private |
| 58 | // address is received. |tick_clock| is guaranteed to be non-null. |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 59 | // |should_notify_rtt_callback| callback should be called back on |
| 60 | // |task_runner| by the created socket watchers to check if RTT observation |
| 61 | // should be taken and notified. |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 62 | SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol, |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 63 | const AddressList& address_list, |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 64 | base::TimeDelta min_notification_interval, |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 65 | bool allow_rtt_private_address, |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 66 | scoped_refptr<base::SingleThreadTaskRunner> task_runner, |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 67 | OnUpdatedRTTAvailableCallback updated_rtt_observation_callback, |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 68 | ShouldNotifyRTTCallback should_notify_rtt_callback, |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 69 | const base::TickClock* tick_clock); |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 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 | |
Tarun Bansal | 58a0f88 | 2017-11-20 18:48:17 | [diff] [blame] | 87 | // Called to determine if the RTT notification should be notified using |
| 88 | // |updated_rtt_observation_callback_|. |
| 89 | ShouldNotifyRTTCallback should_notify_rtt_callback_; |
| 90 | |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 91 | // Minimum interval betweeen consecutive incoming notifications. |
| 92 | const base::TimeDelta rtt_notifications_minimum_interval_; |
| 93 | |
Tarun Bansal | 73a0437 | 2017-07-27 16:28:41 | [diff] [blame] | 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 | |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 98 | // Time when this was last notified of updated RTT. |
| 99 | base::TimeTicks last_rtt_notification_; |
| 100 | |
Greg Thompson | aa48ce8d | 2018-04-03 06:11:43 | [diff] [blame] | 101 | const base::TickClock* tick_clock_; |
tbansal | 180587c | 2017-02-16 15:13:23 | [diff] [blame] | 102 | |
Tarun Bansal | c71cf62 | 2018-10-16 16:24:44 | [diff] [blame] | 103 | SEQUENCE_CHECKER(sequence_checker_); |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 104 | |
Tarun Bansal | 6a7df1f | 2017-10-25 18:15:42 | [diff] [blame] | 105 | // True if the first RTT notification from the QUIC connection has been |
| 106 | // received. |
| 107 | bool first_quic_rtt_notification_received_; |
| 108 | |
Devdeep Ray | 275b75a | 2017-08-18 23:08:10 | [diff] [blame] | 109 | // A unique identifier for the remote host that this socket connects to. |
| 110 | const base::Optional<IPHash> host_; |
| 111 | |
tbansal | 1972027 | 2016-06-07 08:12:14 | [diff] [blame] | 112 | DISALLOW_COPY_AND_ASSIGN(SocketWatcher); |
| 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_ |