blob: a4bf0415ba04e638cab3be8c66fa4ab45d8fe07a [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
tbansal19720272016-06-07 08:12:1433typedef base::Callback<void(SocketPerformanceWatcherFactory::Protocol protocol,
Devdeep Ray275b75a2017-08-18 23:08:1034 const base::TimeDelta& rtt,
35 const base::Optional<nqe::internal::IPHash>& host)>
tbansal19720272016-06-07 08:12:1436 OnUpdatedRTTAvailableCallback;
Devdeep Ray275b75a2017-08-18 23:08:1037
Tarun Bansal58a0f882017-11-20 18:48:1738typedef base::Callback<bool(base::TimeTicks)> ShouldNotifyRTTCallback;
39
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
70 ~SocketWatcher() override;
71
72 // SocketPerformanceWatcher implementation:
73 bool ShouldNotifyUpdatedRTT() const override;
74 void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override;
75 void OnConnectionChanged() override;
76
77 private:
78 // Transport layer protocol used by the socket that |this| is watching.
79 const SocketPerformanceWatcherFactory::Protocol protocol_;
80
81 scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
82
83 // Called every time a new RTT observation is available.
84 OnUpdatedRTTAvailableCallback updated_rtt_observation_callback_;
85
Tarun Bansal58a0f882017-11-20 18:48:1786 // Called to determine if the RTT notification should be notified using
87 // |updated_rtt_observation_callback_|.
88 ShouldNotifyRTTCallback should_notify_rtt_callback_;
89
tbansal180587c2017-02-16 15:13:2390 // Minimum interval betweeen consecutive incoming notifications.
91 const base::TimeDelta rtt_notifications_minimum_interval_;
92
Tarun Bansal73a04372017-07-27 16:28:4193 // True if the RTT observations from this socket can be notified using
94 // |updated_rtt_observation_callback_|.
95 const bool run_rtt_callback_;
96
tbansal180587c2017-02-16 15:13:2397 // Time when this was last notified of updated RTT.
98 base::TimeTicks last_rtt_notification_;
99
Greg Thompsonaa48ce8d2018-04-03 06:11:43100 const base::TickClock* tick_clock_;
tbansal180587c2017-02-16 15:13:23101
Tarun Bansalc71cf622018-10-16 16:24:44102 SEQUENCE_CHECKER(sequence_checker_);
tbansal19720272016-06-07 08:12:14103
Tarun Bansal6a7df1f2017-10-25 18:15:42104 // True if the first RTT notification from the QUIC connection has been
105 // received.
106 bool first_quic_rtt_notification_received_;
107
Devdeep Ray275b75a2017-08-18 23:08:10108 // A unique identifier for the remote host that this socket connects to.
109 const base::Optional<IPHash> host_;
110
tbansal19720272016-06-07 08:12:14111 DISALLOW_COPY_AND_ASSIGN(SocketWatcher);
112};
113
114} // namespace internal
115
116} // namespace nqe
117
118} // namespace net
119
bnc3698b0a02016-12-09 23:36:50120#endif // NET_NQE_SOCKET_WATCHER_H_