socket_watcher.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. #ifndef NET_NQE_SOCKET_WATCHER_H_
  5. #define NET_NQE_SOCKET_WATCHER_H_
  6. #include "base/callback.h"
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "base/sequence_checker.h"
  11. #include "base/time/time.h"
  12. #include "net/base/net_export.h"
  13. #include "net/nqe/network_quality_estimator_util.h"
  14. #include "net/socket/socket_performance_watcher.h"
  15. #include "net/socket/socket_performance_watcher_factory.h"
  16. #include "third_party/abseil-cpp/absl/types/optional.h"
  17. namespace base {
  18. class SingleThreadTaskRunner;
  19. class TickClock;
  20. class TimeDelta;
  21. } // namespace base
  22. namespace net {
  23. class AddressList;
  24. namespace {
  25. typedef base::RepeatingCallback<void(
  26. SocketPerformanceWatcherFactory::Protocol protocol,
  27. const base::TimeDelta& rtt,
  28. const absl::optional<nqe::internal::IPHash>& host)>
  29. OnUpdatedRTTAvailableCallback;
  30. typedef base::RepeatingCallback<bool(base::TimeTicks)> ShouldNotifyRTTCallback;
  31. } // namespace
  32. namespace nqe::internal {
  33. // SocketWatcher implements SocketPerformanceWatcher, and is not thread-safe.
  34. class NET_EXPORT_PRIVATE SocketWatcher : public SocketPerformanceWatcher {
  35. public:
  36. // Creates a SocketWatcher which can be used to watch a socket that uses
  37. // |protocol| as the transport layer protocol. The socket watcher will call
  38. // |updated_rtt_observation_callback| on |task_runner| every time a new RTT
  39. // observation is available. |address_list| is the list of addresses that
  40. // the socket may connect to. |min_notification_interval| is the minimum
  41. // interval betweeen consecutive notifications to this socket watcher.
  42. // |allow_rtt_private_address| is true if |updated_rtt_observation_callback|
  43. // should be called when RTT observation from a socket connected to private
  44. // address is received. |tick_clock| is guaranteed to be non-null.
  45. // |should_notify_rtt_callback| callback should be called back on
  46. // |task_runner| by the created socket watchers to check if RTT observation
  47. // should be taken and notified.
  48. SocketWatcher(SocketPerformanceWatcherFactory::Protocol protocol,
  49. const AddressList& address_list,
  50. base::TimeDelta min_notification_interval,
  51. bool allow_rtt_private_address,
  52. scoped_refptr<base::SingleThreadTaskRunner> task_runner,
  53. OnUpdatedRTTAvailableCallback updated_rtt_observation_callback,
  54. ShouldNotifyRTTCallback should_notify_rtt_callback,
  55. const base::TickClock* tick_clock);
  56. SocketWatcher(const SocketWatcher&) = delete;
  57. SocketWatcher& operator=(const SocketWatcher&) = delete;
  58. ~SocketWatcher() override;
  59. // SocketPerformanceWatcher implementation:
  60. bool ShouldNotifyUpdatedRTT() const override;
  61. void OnUpdatedRTTAvailable(const base::TimeDelta& rtt) override;
  62. void OnConnectionChanged() override;
  63. private:
  64. // Transport layer protocol used by the socket that |this| is watching.
  65. const SocketPerformanceWatcherFactory::Protocol protocol_;
  66. scoped_refptr<base::SingleThreadTaskRunner> task_runner_;
  67. // Called every time a new RTT observation is available.
  68. OnUpdatedRTTAvailableCallback updated_rtt_observation_callback_;
  69. // Called to determine if the RTT notification should be notified using
  70. // |updated_rtt_observation_callback_|.
  71. ShouldNotifyRTTCallback should_notify_rtt_callback_;
  72. // Minimum interval betweeen consecutive incoming notifications.
  73. const base::TimeDelta rtt_notifications_minimum_interval_;
  74. // True if the RTT observations from this socket can be notified using
  75. // |updated_rtt_observation_callback_|.
  76. const bool run_rtt_callback_;
  77. // Time when this was last notified of updated RTT.
  78. base::TimeTicks last_rtt_notification_;
  79. raw_ptr<const base::TickClock> tick_clock_;
  80. SEQUENCE_CHECKER(sequence_checker_);
  81. // True if the first RTT notification from the QUIC connection has been
  82. // received.
  83. bool first_quic_rtt_notification_received_ = false;
  84. // A unique identifier for the remote host that this socket connects to.
  85. const absl::optional<IPHash> host_;
  86. };
  87. } // namespace nqe::internal
  88. } // namespace net
  89. #endif // NET_NQE_SOCKET_WATCHER_H_