blob: 5d3ebda3444607913321302d485caef7af496a3b [file] [log] [blame]
tbansal19720272016-06-07 08:12:141// 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 Hattori0e45c022021-11-27 09:25:529#include "base/memory/raw_ptr.h"
tbansal19720272016-06-07 08:12:1410#include "base/memory/ref_counted.h"
11#include "base/memory/weak_ptr.h"
Tarun Bansalc71cf622018-10-16 16:24:4412#include "base/sequence_checker.h"
tbansal180587c2017-02-16 15:13:2313#include "base/time/time.h"
14#include "net/base/net_export.h"
Devdeep Ray275b75a2017-08-18 23:08:1015#include "net/nqe/network_quality_estimator_util.h"
tbansal19720272016-06-07 08:12:1416#include "net/socket/socket_performance_watcher.h"
17#include "net/socket/socket_performance_watcher_factory.h"
Anton Bikineev068d2912021-05-15 20:43:5218#include "third_party/abseil-cpp/absl/types/optional.h"
tbansal19720272016-06-07 08:12:1419
20namespace base {
21class SingleThreadTaskRunner;
tbansal180587c2017-02-16 15:13:2322class TickClock;
tbansal19720272016-06-07 08:12:1423class TimeDelta;
24} // namespace base
25
26namespace net {
27
Tarun Bansal73a04372017-07-27 16:28:4128class AddressList;
29
tbansal19720272016-06-07 08:12:1430namespace {
Devdeep Ray275b75a2017-08-18 23:08:1031
Anna Malovabc6526f2020-03-04 19:13:2932typedef base::RepeatingCallback<void(
33 SocketPerformanceWatcherFactory::Protocol protocol,
34 const base::TimeDelta& rtt,
Anton Bikineev068d2912021-05-15 20:43:5235 const absl::optional<nqe::internal::IPHash>& host)>
tbansal19720272016-06-07 08:12:1436 OnUpdatedRTTAvailableCallback;
Devdeep Ray275b75a2017-08-18 23:08:1037
Anna Malovabc6526f2020-03-04 19:13:2938typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback;
Tarun Bansal58a0f882017-11-20 18:48:1739
Devdeep Ray275b75a2017-08-18 23:08:1040} // namespace
tbansal19720272016-06-07 08:12:1441
42namespace nqe {
43
44namespace internal {
45
46// SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe.
tbansal180587c2017-02-16 15:13:2347class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher {
tbansal19720272016-06-07 08:12:1448 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 Bansal73a04372017-07-27 16:28:4152 // observation is available. |address_list| is the list of addresses that
53 // the socket may connect to. |min_notification_interval| is the minimum
tbansal180587c2017-02-16 15:13:2354 // interval betweeen consecutive notifications to this socket watcher.
Tarun Bansal73a04372017-07-27 16:28:4155 // |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 Bansal58a0f882017-11-20 18:48:1758 // |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.
tbansal19720272016-06-07 08:12:1461 SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol,
Tarun Bansal73a04372017-07-27 16:28:4162 const AddressList& address_list,
tbansal180587c2017-02-16 15:13:2363 base::TimeDelta min_notification_interval,
Tarun Bansal73a04372017-07-27 16:28:4164 bool allow_rtt_private_address,
tbansal19720272016-06-07 08:12:1465 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
tbansal180587c2017-02-16 15:13:2366 OnUpdatedRTTAvailableCallback updated_rtt_observation_callback,
Tarun Bansal58a0f882017-11-20 18:48:1767 ShouldNotifyRTTCallback should_notify_rtt_callback,
Greg Thompsonaa48ce8d2018-04-03 06:11:4368 const base::TickClock* tick_clock);
tbansal19720272016-06-07 08:12:1469
Peter Boström293b1342021-09-22 17:31:4370 SocketWatcher(const SocketWatcher&) = delete;
71 SocketWatcher& operator=(const SocketWatcher&) = delete;
72
tbansal19720272016-06-07 08:12:1473 ~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 Bansal58a0f882017-11-20 18:48:1789 // Called to determine if the RTT notification should be notified using
90 // |updated_rtt_observation_callback_|.
91 ShouldNotifyRTTCallback should_notify_rtt_callback_;
92
tbansal180587c2017-02-16 15:13:2393 // Minimum interval betweeen consecutive incoming notifications.
94 const base::TimeDelta rtt_notifications_minimum_interval_;
95
Tarun Bansal73a04372017-07-27 16:28:4196 // True if the RTT observations from this socket can be notified using
97 // |updated_rtt_observation_callback_|.
98 const bool run_rtt_callback_;
99
tbansal180587c2017-02-16 15:13:23100 // Time when this was last notified of updated RTT.
101 base::TimeTicks last_rtt_notification_;
102
Keishi Hattori0e45c022021-11-27 09:25:52103 raw_ptr<const base::TickClock> tick_clock_;
tbansal180587c2017-02-16 15:13:23104
Tarun Bansalc71cf622018-10-16 16:24:44105 SEQUENCE_CHECKER(sequence_checker_);
tbansal19720272016-06-07 08:12:14106
Tarun Bansal6a7df1f2017-10-25 18:15:42107 // True if the first RTT notification from the QUIC connection has been
108 // received.
Tsuyoshi Horo2fa6b8de2022-06-09 01:40:20109 bool first_quic_rtt_notification_received_ = false;
Tarun Bansal6a7df1f2017-10-25 18:15:42110
Devdeep Ray275b75a2017-08-18 23:08:10111 // A unique identifier for the remote host that this socket connects to.
Anton Bikineev068d2912021-05-15 20:43:52112 const absl::optional<IPHash> host_;
tbansal19720272016-06-07 08:12:14113};
114
115} // namespace internal
116
117} // namespace nqe
118
119} // namespace net
120
bnc3698b0a02016-12-09 23:36:50121#endif // NET_NQE_SOCKET_WATCHER_H_