socket_watcher_factory.h 3.7 KB

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