blob: 7fe97c9d4adc97435baecda3795225b421e2c02f [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
tbansal180587c2017-02-16 15:13:238#include <memory>
9
tbansal19720272016-06-07 08:12:1410#include "base/callback.h"
11#include "base/memory/ref_counted.h"
12#include "base/memory/weak_ptr.h"
Devdeep Ray275b75a2017-08-18 23:08:1013#include "base/optional.h"
Tarun Bansalc71cf622018-10-16 16:24:4414#include "base/sequence_checker.h"
tbansal180587c2017-02-16 15:13:2315#include "base/time/time.h"
16#include "net/base/net_export.h"
Devdeep Ray275b75a2017-08-18 23:08:1017#include "net/nqe/network_quality_estimator_util.h"
tbansal19720272016-06-07 08:12:1418#include "net/socket/socket_performance_watcher.h"
19#include "net/socket/socket_performance_watcher_factory.h"
20
21namespace base {
22class SingleThreadTaskRunner;
tbansal180587c2017-02-16 15:13:2323class TickClock;
tbansal19720272016-06-07 08:12:1424class TimeDelta;
25} // namespace base
26
27namespace net {
28
Tarun Bansal73a04372017-07-27 16:28:4129class AddressList;
30
tbansal19720272016-06-07 08:12:1431namespace {
Devdeep Ray275b75a2017-08-18 23:08:1032
Anna Malovabc6526f2020-03-04 19:13:2933typedef base::RepeatingCallback<void(
34 SocketPerformanceWatcherFactory::Protocol protocol,
35 const base::TimeDelta& rtt,
36 const base::Optional<nqe::internal::IPHash>& host)>
tbansal19720272016-06-07 08:12:1437 OnUpdatedRTTAvailableCallback;
Devdeep Ray275b75a2017-08-18 23:08:1038
Anna Malovabc6526f2020-03-04 19:13:2939typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback;
Tarun Bansal58a0f882017-11-20 18:48:1740
Devdeep Ray275b75a2017-08-18 23:08:1041} // namespace
tbansal19720272016-06-07 08:12:1442
43namespace nqe {
44
45namespace internal {
46
47// SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe.
tbansal180587c2017-02-16 15:13:2348class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher {
tbansal19720272016-06-07 08:12:1449 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 Bansal73a04372017-07-27 16:28:4153 // observation is available. |address_list| is the list of addresses that
54 // the socket may connect to. |min_notification_interval| is the minimum
tbansal180587c2017-02-16 15:13:2355 // interval betweeen consecutive notifications to this socket watcher.
Tarun Bansal73a04372017-07-27 16:28:4156 // |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 Bansal58a0f882017-11-20 18:48:1759 // |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.
tbansal19720272016-06-07 08:12:1462 SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol,
Tarun Bansal73a04372017-07-27 16:28:4163 const AddressList& address_list,
tbansal180587c2017-02-16 15:13:2364 base::TimeDelta min_notification_interval,
Tarun Bansal73a04372017-07-27 16:28:4165 bool allow_rtt_private_address,
tbansal19720272016-06-07 08:12:1466 scoped_refptr<base::SingleThreadTaskRunner> task_runner,
tbansal180587c2017-02-16 15:13:2367 OnUpdatedRTTAvailableCallback updated_rtt_observation_callback,
Tarun Bansal58a0f882017-11-20 18:48:1768 ShouldNotifyRTTCallback should_notify_rtt_callback,
Greg Thompsonaa48ce8d2018-04-03 06:11:4369 const base::TickClock* tick_clock);
tbansal19720272016-06-07 08:12:1470
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 Bansal58a0f882017-11-20 18:48:1787 // Called to determine if the RTT notification should be notified using
88 // |updated_rtt_observation_callback_|.
89 ShouldNotifyRTTCallback should_notify_rtt_callback_;
90
tbansal180587c2017-02-16 15:13:2391 // Minimum interval betweeen consecutive incoming notifications.
92 const base::TimeDelta rtt_notifications_minimum_interval_;
93
Tarun Bansal73a04372017-07-27 16:28:4194 // True if the RTT observations from this socket can be notified using
95 // |updated_rtt_observation_callback_|.
96 const bool run_rtt_callback_;
97
tbansal180587c2017-02-16 15:13:2398 // Time when this was last notified of updated RTT.
99 base::TimeTicks last_rtt_notification_;
100
Greg Thompsonaa48ce8d2018-04-03 06:11:43101 const base::TickClock* tick_clock_;
tbansal180587c2017-02-16 15:13:23102
Tarun Bansalc71cf622018-10-16 16:24:44103 SEQUENCE_CHECKER(sequence_checker_);
tbansal19720272016-06-07 08:12:14104
Tarun Bansal6a7df1f2017-10-25 18:15:42105 // True if the first RTT notification from the QUIC connection has been
106 // received.
107 bool first_quic_rtt_notification_received_;
108
Devdeep Ray275b75a2017-08-18 23:08:10109 // A unique identifier for the remote host that this socket connects to.
110 const base::Optional<IPHash> host_;
111
tbansal19720272016-06-07 08:12:14112 DISALLOW_COPY_AND_ASSIGN(SocketWatcher);
113};
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_