OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/dns/dns_client.h" | 5 #include "net/dns/dns_client.h" |
6 | 6 |
7 #include <utility> | 7 #include <utility> |
8 | 8 |
9 #include "base/bind.h" | 9 #include "base/bind.h" |
| 10 #include "base/memory/ptr_util.h" |
10 #include "base/rand_util.h" | 11 #include "base/rand_util.h" |
11 #include "net/dns/address_sorter.h" | 12 #include "net/dns/address_sorter.h" |
12 #include "net/dns/dns_config_service.h" | 13 #include "net/dns/dns_config_service.h" |
13 #include "net/dns/dns_session.h" | 14 #include "net/dns/dns_session.h" |
14 #include "net/dns/dns_socket_pool.h" | 15 #include "net/dns/dns_socket_pool.h" |
15 #include "net/dns/dns_transaction.h" | 16 #include "net/dns/dns_transaction.h" |
16 #include "net/log/net_log.h" | 17 #include "net/log/net_log.h" |
17 #include "net/socket/client_socket_factory.h" | 18 #include "net/socket/client_socket_factory.h" |
18 | 19 |
19 namespace net { | 20 namespace net { |
20 | 21 |
21 namespace { | 22 namespace { |
22 | 23 |
23 class DnsClientImpl : public DnsClient { | 24 class DnsClientImpl : public DnsClient { |
24 public: | 25 public: |
25 explicit DnsClientImpl(NetLog* net_log) | 26 DnsClientImpl(NetLog* net_log, |
| 27 ClientSocketFactory* socket_factory, |
| 28 const RandIntCallback& rand_int_callback) |
26 : address_sorter_(AddressSorter::CreateAddressSorter()), | 29 : address_sorter_(AddressSorter::CreateAddressSorter()), |
27 net_log_(net_log) {} | 30 net_log_(net_log), |
| 31 socket_factory_(socket_factory), |
| 32 rand_int_callback_(rand_int_callback) {} |
28 | 33 |
29 void SetConfig(const DnsConfig& config) override { | 34 void SetConfig(const DnsConfig& config) override { |
30 factory_.reset(); | 35 factory_.reset(); |
31 session_ = NULL; | 36 session_ = nullptr; |
32 if (config.IsValid() && !config.unhandled_options) { | 37 if (config.IsValid() && !config.unhandled_options) { |
33 ClientSocketFactory* factory = ClientSocketFactory::GetDefaultFactory(); | |
34 std::unique_ptr<DnsSocketPool> socket_pool( | 38 std::unique_ptr<DnsSocketPool> socket_pool( |
35 config.randomize_ports ? DnsSocketPool::CreateDefault(factory) | 39 config.randomize_ports |
36 : DnsSocketPool::CreateNull(factory)); | 40 ? DnsSocketPool::CreateDefault(socket_factory_, |
| 41 rand_int_callback_) |
| 42 : DnsSocketPool::CreateNull(socket_factory_, rand_int_callback_)); |
37 session_ = new DnsSession(config, std::move(socket_pool), | 43 session_ = new DnsSession(config, std::move(socket_pool), |
38 base::Bind(&base::RandInt), net_log_); | 44 rand_int_callback_, net_log_); |
39 factory_ = DnsTransactionFactory::CreateFactory(session_.get()); | 45 factory_ = DnsTransactionFactory::CreateFactory(session_.get()); |
40 } | 46 } |
41 } | 47 } |
42 | 48 |
43 const DnsConfig* GetConfig() const override { | 49 const DnsConfig* GetConfig() const override { |
44 return session_.get() ? &session_->config() : NULL; | 50 return session_.get() ? &session_->config() : NULL; |
45 } | 51 } |
46 | 52 |
47 DnsTransactionFactory* GetTransactionFactory() override { | 53 DnsTransactionFactory* GetTransactionFactory() override { |
48 return session_.get() ? factory_.get() : NULL; | 54 return session_.get() ? factory_.get() : NULL; |
49 } | 55 } |
50 | 56 |
51 AddressSorter* GetAddressSorter() override { return address_sorter_.get(); } | 57 AddressSorter* GetAddressSorter() override { return address_sorter_.get(); } |
52 | 58 |
53 private: | 59 private: |
54 scoped_refptr<DnsSession> session_; | 60 scoped_refptr<DnsSession> session_; |
55 std::unique_ptr<DnsTransactionFactory> factory_; | 61 std::unique_ptr<DnsTransactionFactory> factory_; |
56 std::unique_ptr<AddressSorter> address_sorter_; | 62 std::unique_ptr<AddressSorter> address_sorter_; |
57 | 63 |
58 NetLog* net_log_; | 64 NetLog* net_log_; |
| 65 |
| 66 ClientSocketFactory* socket_factory_; |
| 67 const RandIntCallback rand_int_callback_; |
| 68 |
| 69 DISALLOW_COPY_AND_ASSIGN(DnsClientImpl); |
59 }; | 70 }; |
60 | 71 |
61 } // namespace | 72 } // namespace |
62 | 73 |
63 // static | 74 // static |
64 std::unique_ptr<DnsClient> DnsClient::CreateClient(NetLog* net_log) { | 75 std::unique_ptr<DnsClient> DnsClient::CreateClient(NetLog* net_log) { |
65 return std::unique_ptr<DnsClient>(new DnsClientImpl(net_log)); | 76 return base::WrapUnique( |
| 77 new DnsClientImpl(net_log, ClientSocketFactory::GetDefaultFactory(), |
| 78 base::Bind(&base::RandInt))); |
| 79 } |
| 80 |
| 81 // static |
| 82 std::unique_ptr<DnsClient> DnsClient::CreateClientForTesting( |
| 83 NetLog* net_log, |
| 84 ClientSocketFactory* socket_factory, |
| 85 const RandIntCallback& rand_int_callback) { |
| 86 return base::WrapUnique( |
| 87 new DnsClientImpl(net_log, socket_factory, rand_int_callback)); |
66 } | 88 } |
67 | 89 |
68 } // namespace net | 90 } // namespace net |
69 | 91 |
OLD | NEW |