• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2016 The Chromium Authors
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_FACTORY_H_
6 #define NET_NQE_SOCKET_WATCHER_FACTORY_H_
7 
8 #include <memory>
9 
10 #include "base/functional/callback.h"
11 #include "base/memory/raw_ptr.h"
12 #include "base/memory/scoped_refptr.h"
13 #include "base/memory/weak_ptr.h"
14 #include "base/sequence_checker.h"
15 #include "base/task/single_thread_task_runner.h"
16 #include "base/time/time.h"
17 #include "net/nqe/network_quality_estimator_util.h"
18 #include "net/socket/socket_performance_watcher.h"
19 #include "net/socket/socket_performance_watcher_factory.h"
20 #include "third_party/abseil-cpp/absl/types/optional.h"
21 
22 namespace base {
23 class TickClock;
24 class TimeDelta;
25 }  // namespace base
26 
27 namespace net {
28 
29 namespace {
30 
31 typedef base::RepeatingCallback<void(
32     SocketPerformanceWatcherFactory::Protocol protocol,
33     const base::TimeDelta& rtt,
34     const absl::optional<nqe::internal::IPHash>& host)>
35     OnUpdatedRTTAvailableCallback;
36 
37 typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback;
38 
39 }  // namespace
40 
41 namespace nqe::internal {
42 
43 // SocketWatcherFactory implements SocketPerformanceWatcherFactory.
44 // SocketWatcherFactory is thread safe.
45 class SocketWatcherFactory : public SocketPerformanceWatcherFactory {
46  public:
47   // Creates a SocketWatcherFactory.  All socket watchers created by
48   // SocketWatcherFactory call |updated_rtt_observation_callback| on
49   // |task_runner| every time a new RTT observation is available.
50   // |min_notification_interval| is the minimum interval betweeen consecutive
51   // notifications to the socket watchers created by this factory. |tick_clock|
52   // is guaranteed to be non-null. |should_notify_rtt_callback| is the callback
53   // that should be called back on |task_runner| to check if RTT observation
54   // should be taken and notified.
55   SocketWatcherFactory(
56       scoped_refptr<base::SingleThreadTaskRunner> task_runner,
57       base::TimeDelta min_notification_interval,
58       OnUpdatedRTTAvailableCallback updated_rtt_observation_callback,
59       ShouldNotifyRTTCallback should_notify_rtt_callback,
60       const base::TickClock* tick_clock);
61 
62   SocketWatcherFactory(const SocketWatcherFactory&) = delete;
63   SocketWatcherFactory& operator=(const SocketWatcherFactory&) = delete;
64 
65   ~SocketWatcherFactory() override;
66 
67   // SocketPerformanceWatcherFactory implementation:
68   std::unique_ptr<SocketPerformanceWatcher> CreateSocketPerformanceWatcher(
69       const Protocol protocol,
70       const IPAddress& address) override;
71 
SetUseLocalHostRequestsForTesting(bool use_localhost_requests)72   void SetUseLocalHostRequestsForTesting(bool use_localhost_requests) {
73     allow_rtt_private_address_ = use_localhost_requests;
74   }
75 
76   // Overrides the tick clock used by |this| for testing.
77   void SetTickClockForTesting(const base::TickClock* tick_clock);
78 
79  private:
80   scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
81 
82   // Minimum interval betweeen consecutive notifications to the socket watchers
83   // created by this factory.
84   const base::TimeDelta min_notification_interval_;
85 
86   // True if socket watchers constructed by this factory can use the RTT from
87   // the sockets that are connected to the private addresses.
88   bool allow_rtt_private_address_ = false;
89 
90   // Called every time a new RTT observation is available.
91   OnUpdatedRTTAvailableCallback updated_rtt_observation_callback_;
92 
93   // Callback that should be called by socket watchers to determine if the RTT
94   // notification should be notified using |updated_rtt_observation_callback_|.
95   ShouldNotifyRTTCallback should_notify_rtt_callback_;
96 
97   raw_ptr<const base::TickClock> tick_clock_;
98 };
99 
100 }  // namespace nqe::internal
101 
102 }  // namespace net
103 
104 #endif  // NET_NQE_SOCKET_WATCHER_FACTORY_H_
105