blob: 27ee5c8f1ee9ab25b2d76c67583b2a95bef7176a (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef NET_DNS_DNS_SESSION_H_
#define NET_DNS_DNS_SESSION_H_
#include <stdint.h>
#include <memory>
#include "base/macros.h"
#include "base/memory/ref_counted.h"
#include "base/memory/weak_ptr.h"
#include "net/base/net_export.h"
#include "net/base/rand_callback.h"
#include "net/dns/dns_config.h"
#include "net/dns/dns_socket_pool.h"
namespace net {
class DatagramClientSocket;
class NetLog;
struct NetLogSource;
class StreamSocket;
// Session parameters and state shared between DnsTransactions for a specific
// instance/version of a DnsConfig. Also may be used as a key handle for
// per-session state stored elsewhere, e.g. in ResolveContext. A new DnsSession
// should be created for each new DnsConfig for DnsTransactions to be based on
// that new configuration.
//
// Ref-counted so that DnsTransactions can keep working in absence of
// DnsClient or continue operating on the same session after DnsClient has moved
// to a new DnsConfig/DnsSession.
class NET_EXPORT_PRIVATE DnsSession : public base::RefCounted<DnsSession> {
public:
typedef base::RepeatingCallback<int()> RandCallback;
class NET_EXPORT_PRIVATE SocketLease {
public:
SocketLease(scoped_refptr<DnsSession> session,
size_t server_index,
std::unique_ptr<DatagramClientSocket> socket);
~SocketLease();
size_t server_index() const { return server_index_; }
DatagramClientSocket* socket() { return socket_.get(); }
private:
scoped_refptr<DnsSession> session_;
size_t server_index_;
std::unique_ptr<DatagramClientSocket> socket_;
DISALLOW_COPY_AND_ASSIGN(SocketLease);
};
DnsSession(const DnsConfig& config,
std::unique_ptr<DnsSocketPool> socket_pool,
const RandIntCallback& rand_int_callback,
NetLog* net_log);
const DnsConfig& config() const { return config_; }
NetLog* net_log() const { return net_log_; }
// Return the next random query ID.
uint16_t NextQueryId() const;
// Allocate a socket, already connected to the server address.
// When the SocketLease is destroyed, the socket will be freed.
std::unique_ptr<SocketLease> AllocateSocket(size_t server_index,
const NetLogSource& source);
// Creates a StreamSocket from the factory for a transaction over TCP. These
// sockets are not pooled.
std::unique_ptr<StreamSocket> CreateTCPSocket(size_t server_index,
const NetLogSource& source);
base::WeakPtr<DnsSession> GetWeakPtr() {
return weak_ptr_factory_.GetWeakPtr();
}
base::WeakPtr<const DnsSession> GetWeakPtr() const {
return weak_ptr_factory_.GetWeakPtr();
}
void InvalidateWeakPtrsForTesting();
private:
friend class base::RefCounted<DnsSession>;
~DnsSession();
// Release a socket.
void FreeSocket(size_t server_index,
std::unique_ptr<DatagramClientSocket> socket);
const DnsConfig config_;
std::unique_ptr<DnsSocketPool> socket_pool_;
RandCallback rand_callback_;
NetLog* net_log_;
mutable base::WeakPtrFactory<DnsSession> weak_ptr_factory_{this};
DISALLOW_COPY_AND_ASSIGN(DnsSession);
};
} // namespace net
#endif // NET_DNS_DNS_SESSION_H_
|