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