Skip to content

Commit 98c2feb

Browse files
Eric OrthCommit Bot
Eric Orth
authored and
Commit Bot
committed
Cleanup unnecessary socket abstractions in DnsSession
Bug: 1116579 Change-Id: Ifd4958aa53824914335715df58bd8a8d89913ad9 Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2363401 Commit-Queue: Eric Orth <ericorth@chromium.org> Reviewed-by: Dan McArdle <dmcardle@chromium.org> Cr-Commit-Position: refs/heads/master@{#800253}
1 parent 74a3234 commit 98c2feb

5 files changed

+19
-106
lines changed

net/dns/dns_session.cc

+1-51
Original file line numberDiff line numberDiff line change
@@ -10,34 +10,15 @@
1010
#include <utility>
1111

1212
#include "base/bind.h"
13-
#include "base/macros.h"
1413
#include "base/metrics/histogram_macros.h"
1514
#include "base/rand_util.h"
1615
#include "base/stl_util.h"
17-
#include "net/base/ip_endpoint.h"
18-
#include "net/base/net_errors.h"
1916
#include "net/dns/dns_config.h"
2017
#include "net/dns/dns_socket_pool.h"
21-
#include "net/log/net_log_event_type.h"
22-
#include "net/log/net_log_source.h"
23-
#include "net/log/net_log_with_source.h"
24-
#include "net/socket/datagram_client_socket.h"
25-
#include "net/socket/stream_socket.h"
18+
#include "net/log/net_log.h"
2619

2720
namespace net {
2821

29-
DnsSession::SocketLease::SocketLease(
30-
scoped_refptr<DnsSession> session,
31-
size_t server_index,
32-
std::unique_ptr<DatagramClientSocket> socket)
33-
: session_(session),
34-
server_index_(server_index),
35-
socket_(std::move(socket)) {}
36-
37-
DnsSession::SocketLease::~SocketLease() {
38-
session_->FreeSocket(server_index_, std::move(socket_));
39-
}
40-
4122
DnsSession::DnsSession(const DnsConfig& config,
4223
std::unique_ptr<DnsSocketPool> socket_pool,
4324
const RandIntCallback& rand_int_callback,
@@ -58,39 +39,8 @@ uint16_t DnsSession::NextQueryId() const {
5839
return static_cast<uint16_t>(rand_callback_.Run());
5940
}
6041

61-
// Allocate a socket, already connected to the server address.
62-
std::unique_ptr<DnsSession::SocketLease> DnsSession::AllocateSocket(
63-
size_t server_index,
64-
const NetLogSource& source) {
65-
std::unique_ptr<DatagramClientSocket> socket;
66-
67-
socket = socket_pool_->CreateConnectedUdpSocket(server_index);
68-
if (!socket.get())
69-
return std::unique_ptr<SocketLease>();
70-
71-
socket->NetLog().BeginEventReferencingSource(NetLogEventType::SOCKET_IN_USE,
72-
source);
73-
74-
SocketLease* lease = new SocketLease(this, server_index, std::move(socket));
75-
return std::unique_ptr<SocketLease>(lease);
76-
}
77-
78-
std::unique_ptr<StreamSocket> DnsSession::CreateTCPSocket(
79-
size_t server_index,
80-
const NetLogSource& source) {
81-
return socket_pool_->CreateTcpSocket(server_index, source);
82-
}
83-
8442
void DnsSession::InvalidateWeakPtrsForTesting() {
8543
weak_ptr_factory_.InvalidateWeakPtrs();
8644
}
8745

88-
// Release a socket.
89-
void DnsSession::FreeSocket(size_t server_index,
90-
std::unique_ptr<DatagramClientSocket> socket) {
91-
DCHECK(socket.get());
92-
93-
socket->NetLog().EndEvent(NetLogEventType::SOCKET_IN_USE);
94-
}
95-
9646
} // namespace net

net/dns/dns_session.h

+2-41
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,12 @@
1515
#include "net/base/net_export.h"
1616
#include "net/base/rand_callback.h"
1717
#include "net/dns/dns_config.h"
18-
#include "net/dns/dns_socket_pool.h"
1918
#include "net/dns/dns_udp_tracker.h"
2019

2120
namespace net {
2221

23-
class DatagramClientSocket;
22+
class DnsSocketPool;
2423
class NetLog;
25-
struct NetLogSource;
26-
class StreamSocket;
2724

2825
// Session parameters and state shared between DnsTransactions for a specific
2926
// instance/version of a DnsConfig. Also may be used as a key handle for
@@ -38,51 +35,19 @@ class NET_EXPORT_PRIVATE DnsSession : public base::RefCounted<DnsSession> {
3835
public:
3936
typedef base::RepeatingCallback<int()> RandCallback;
4037

41-
// TODO(crbug.com/1116579): Remove this unnecessary abstraction now that DNS
42-
// sockets are not pooled and do not need a special release signal.
43-
class NET_EXPORT_PRIVATE SocketLease {
44-
public:
45-
SocketLease(scoped_refptr<DnsSession> session,
46-
size_t server_index,
47-
std::unique_ptr<DatagramClientSocket> socket);
48-
~SocketLease();
49-
50-
size_t server_index() const { return server_index_; }
51-
52-
DatagramClientSocket* socket() { return socket_.get(); }
53-
54-
private:
55-
scoped_refptr<DnsSession> session_;
56-
size_t server_index_;
57-
std::unique_ptr<DatagramClientSocket> socket_;
58-
59-
DISALLOW_COPY_AND_ASSIGN(SocketLease);
60-
};
61-
6238
DnsSession(const DnsConfig& config,
6339
std::unique_ptr<DnsSocketPool> socket_pool,
6440
const RandIntCallback& rand_int_callback,
6541
NetLog* net_log);
6642

6743
const DnsConfig& config() const { return config_; }
44+
DnsSocketPool* socket_pool() { return socket_pool_.get(); }
6845
DnsUdpTracker* udp_tracker() { return &udp_tracker_; }
6946
NetLog* net_log() const { return net_log_; }
7047

7148
// Return the next random query ID.
7249
uint16_t NextQueryId() const;
7350

74-
// Allocate a socket, already connected to the server address.
75-
// When the SocketLease is destroyed, the socket will be freed.
76-
// TODO(crbug.com/1116579): Remove this and directly call into DnsSocketPool.
77-
std::unique_ptr<SocketLease> AllocateSocket(size_t server_index,
78-
const NetLogSource& source);
79-
80-
// Creates a StreamSocket from the factory for a transaction over TCP. These
81-
// sockets are not pooled.
82-
// TODO(crbug.com/1116579): Remove this and directly call into DnsSocketPool.
83-
std::unique_ptr<StreamSocket> CreateTCPSocket(size_t server_index,
84-
const NetLogSource& source);
85-
8651
base::WeakPtr<DnsSession> GetWeakPtr() {
8752
return weak_ptr_factory_.GetWeakPtr();
8853
}
@@ -98,10 +63,6 @@ class NET_EXPORT_PRIVATE DnsSession : public base::RefCounted<DnsSession> {
9863

9964
~DnsSession();
10065

101-
// Release a socket.
102-
void FreeSocket(size_t server_index,
103-
std::unique_ptr<DatagramClientSocket> socket);
104-
10566
const DnsConfig config_;
10667
std::unique_ptr<DnsSocketPool> socket_pool_;
10768
DnsUdpTracker udp_tracker_;

net/dns/dns_test_util.cc

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "net/dns/dns_hosts.h"
1919
#include "net/dns/dns_query.h"
2020
#include "net/dns/dns_session.h"
21+
#include "net/dns/dns_socket_pool.h"
2122
#include "net/dns/dns_util.h"
2223
#include "net/dns/resolve_context.h"
2324
#include "testing/gtest/include/gtest/gtest.h"

net/dns/dns_transaction.cc

+14-14
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
#include "net/dns/dns_response.h"
4949
#include "net/dns/dns_server_iterator.h"
5050
#include "net/dns/dns_session.h"
51+
#include "net/dns/dns_socket_pool.h"
5152
#include "net/dns/dns_udp_tracker.h"
5253
#include "net/dns/dns_util.h"
5354
#include "net/dns/public/dns_over_https_server_config.h"
@@ -187,12 +188,12 @@ class DnsAttempt {
187188
class DnsUDPAttempt : public DnsAttempt {
188189
public:
189190
DnsUDPAttempt(size_t server_index,
190-
std::unique_ptr<DnsSession::SocketLease> socket_lease,
191+
std::unique_ptr<DatagramClientSocket> socket,
191192
std::unique_ptr<DnsQuery> query,
192193
DnsUdpTracker* udp_tracker)
193194
: DnsAttempt(server_index),
194195
next_state_(STATE_NONE),
195-
socket_lease_(std::move(socket_lease)),
196+
socket_(std::move(socket)),
196197
query_(std::move(query)),
197198
udp_tracker_(udp_tracker) {}
198199

@@ -205,7 +206,7 @@ class DnsUDPAttempt : public DnsAttempt {
205206
next_state_ = STATE_SEND_QUERY;
206207

207208
IPEndPoint local_address;
208-
if (socket_lease_->socket()->GetLocalAddress(&local_address) == OK)
209+
if (socket_->GetLocalAddress(&local_address) == OK)
209210
udp_tracker_->RecordQuery(local_address.port(), query_->id());
210211

211212
return DoLoop(OK);
@@ -219,7 +220,7 @@ class DnsUDPAttempt : public DnsAttempt {
219220
}
220221

221222
const NetLogWithSource& GetSocketNetLog() const override {
222-
return socket_lease_->socket()->NetLog();
223+
return socket_->NetLog();
223224
}
224225

225226
private:
@@ -231,8 +232,6 @@ class DnsUDPAttempt : public DnsAttempt {
231232
STATE_NONE,
232233
};
233234

234-
DatagramClientSocket* socket() { return socket_lease_->socket(); }
235-
236235
int DoLoop(int result) {
237236
CHECK_NE(STATE_NONE, next_state_);
238237
int rv = result;
@@ -270,7 +269,7 @@ class DnsUDPAttempt : public DnsAttempt {
270269

271270
int DoSendQuery() {
272271
next_state_ = STATE_SEND_QUERY_COMPLETE;
273-
return socket()->Write(
272+
return socket_->Write(
274273
query_->io_buffer(), query_->io_buffer()->size(),
275274
base::BindOnce(&DnsUDPAttempt::OnIOComplete, base::Unretained(this)),
276275
kTrafficAnnotation);
@@ -292,7 +291,7 @@ class DnsUDPAttempt : public DnsAttempt {
292291
int DoReadResponse() {
293292
next_state_ = STATE_READ_RESPONSE_COMPLETE;
294293
response_ = std::make_unique<DnsResponse>();
295-
return socket()->Read(
294+
return socket_->Read(
296295
response_->io_buffer(), response_->io_buffer_size(),
297296
base::BindOnce(&DnsUDPAttempt::OnIOComplete, base::Unretained(this)));
298297
}
@@ -328,7 +327,7 @@ class DnsUDPAttempt : public DnsAttempt {
328327
State next_state_;
329328
base::TimeTicks start_time_;
330329

331-
std::unique_ptr<DnsSession::SocketLease> socket_lease_;
330+
std::unique_ptr<DatagramClientSocket> socket_;
332331
std::unique_ptr<DnsQuery> query_;
333332

334333
// Should be owned by the DnsSession, to which the transaction should own a
@@ -1258,13 +1257,13 @@ class DnsTransactionImpl : public DnsTransaction,
12581257
DCHECK(!secure_);
12591258
size_t attempt_number = attempts_.size();
12601259

1261-
std::unique_ptr<DnsSession::SocketLease> lease =
1262-
session_->AllocateSocket(server_index, net_log_.source());
1260+
std::unique_ptr<DatagramClientSocket> socket =
1261+
session_->socket_pool()->CreateConnectedUdpSocket(server_index);
12631262

1264-
bool got_socket = !!lease.get();
1263+
bool got_socket = !!socket.get();
12651264

12661265
DnsUDPAttempt* attempt =
1267-
new DnsUDPAttempt(server_index, std::move(lease), std::move(query),
1266+
new DnsUDPAttempt(server_index, std::move(socket), std::move(query),
12681267
session_->udp_tracker());
12691268

12701269
attempts_.push_back(base::WrapUnique(attempt));
@@ -1339,7 +1338,8 @@ class DnsTransactionImpl : public DnsTransaction,
13391338
DCHECK(!secure_);
13401339

13411340
std::unique_ptr<StreamSocket> socket(
1342-
session_->CreateTCPSocket(server_index, net_log_.source()));
1341+
session_->socket_pool()->CreateTcpSocket(server_index,
1342+
net_log_.source()));
13431343

13441344
unsigned attempt_number = attempts_.size();
13451345

net/dns/dns_transaction_unittest.cc

+1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include "net/dns/dns_response.h"
3636
#include "net/dns/dns_server_iterator.h"
3737
#include "net/dns/dns_session.h"
38+
#include "net/dns/dns_socket_pool.h"
3839
#include "net/dns/dns_test_util.h"
3940
#include "net/dns/dns_util.h"
4041
#include "net/dns/public/dns_over_https_server_config.h"

0 commit comments

Comments
 (0)