dns_session.h 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. // Copyright (c) 2012 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_DNS_DNS_SESSION_H_
  5. #define NET_DNS_DNS_SESSION_H_
  6. #include <stdint.h>
  7. #include "base/memory/raw_ptr.h"
  8. #include "base/memory/ref_counted.h"
  9. #include "base/memory/weak_ptr.h"
  10. #include "net/base/net_export.h"
  11. #include "net/base/rand_callback.h"
  12. #include "net/dns/dns_config.h"
  13. #include "net/dns/dns_udp_tracker.h"
  14. namespace net {
  15. class NetLog;
  16. // Session parameters and state shared between DnsTransactions for a specific
  17. // instance/version of a DnsConfig. Also may be used as a key handle for
  18. // per-session state stored elsewhere, e.g. in ResolveContext. A new DnsSession
  19. // should be created for each new DnsConfig for DnsTransactions to be based on
  20. // that new configuration.
  21. //
  22. // Ref-counted so that DnsTransactions can keep working in absence of
  23. // DnsClient or continue operating on the same session after DnsClient has moved
  24. // to a new DnsConfig/DnsSession.
  25. class NET_EXPORT_PRIVATE DnsSession : public base::RefCounted<DnsSession> {
  26. public:
  27. typedef base::RepeatingCallback<int()> RandCallback;
  28. DnsSession(const DnsConfig& config,
  29. const RandIntCallback& rand_int_callback,
  30. NetLog* net_log);
  31. DnsSession(const DnsSession&) = delete;
  32. DnsSession& operator=(const DnsSession&) = delete;
  33. const DnsConfig& config() const { return config_; }
  34. DnsUdpTracker* udp_tracker() { return &udp_tracker_; }
  35. NetLog* net_log() const { return net_log_; }
  36. // Return the next random query ID.
  37. uint16_t NextQueryId() const;
  38. base::WeakPtr<DnsSession> GetWeakPtr() {
  39. return weak_ptr_factory_.GetWeakPtr();
  40. }
  41. base::WeakPtr<const DnsSession> GetWeakPtr() const {
  42. return weak_ptr_factory_.GetWeakPtr();
  43. }
  44. void InvalidateWeakPtrsForTesting();
  45. private:
  46. friend class base::RefCounted<DnsSession>;
  47. ~DnsSession();
  48. const DnsConfig config_;
  49. DnsUdpTracker udp_tracker_;
  50. RandCallback rand_callback_;
  51. raw_ptr<NetLog> net_log_;
  52. mutable base::WeakPtrFactory<DnsSession> weak_ptr_factory_{this};
  53. };
  54. } // namespace net
  55. #endif // NET_DNS_DNS_SESSION_H_