OLD | NEW |
| (Empty) |
1 // Copyright (c) 2015 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 | |
5 #include "net/tools/quic/synchronous_host_resolver.h" | |
6 | |
7 #include "base/at_exit.h" | |
8 #include "base/memory/weak_ptr.h" | |
9 #include "base/threading/simple_thread.h" | |
10 #include "net/base/host_port_pair.h" | |
11 #include "net/base/net_errors.h" | |
12 #include "net/dns/host_resolver_impl.h" | |
13 #include "net/dns/single_request_host_resolver.h" | |
14 | |
15 namespace net { | |
16 | |
17 namespace tools { | |
18 | |
19 namespace { | |
20 | |
21 class ResolverThread : public base::SimpleThread { | |
22 public: | |
23 ResolverThread(); | |
24 | |
25 ~ResolverThread() override; | |
26 | |
27 // Called on the main thread. | |
28 int Resolve(const std::string& host, AddressList* addresses); | |
29 | |
30 // SimpleThread methods: | |
31 void Run() override; | |
32 | |
33 private: | |
34 void OnResolutionComplete(int rv); | |
35 | |
36 AddressList* addresses_; | |
37 std::string host_; | |
38 int rv_; | |
39 | |
40 base::WaitableEvent resolved_; // Notified when the resolution is complete. | |
41 | |
42 base::WeakPtrFactory<ResolverThread> weak_factory_; | |
43 | |
44 DISALLOW_COPY_AND_ASSIGN(ResolverThread); | |
45 }; | |
46 | |
47 ResolverThread::ResolverThread() | |
48 : SimpleThread("resolver_thread"), | |
49 rv_(ERR_UNEXPECTED), | |
50 resolved_(true, false), | |
51 weak_factory_(this) {} | |
52 | |
53 ResolverThread::~ResolverThread() {} | |
54 | |
55 void ResolverThread::Run() { | |
56 base::AtExitManager exit_manager; | |
57 base::MessageLoopForIO loop; | |
58 | |
59 net::HostResolver::Options options; | |
60 options.max_concurrent_resolves = 6; | |
61 options.max_retry_attempts = 3u; | |
62 net::NetLog net_log; | |
63 scoped_ptr<net::HostResolverImpl> resolver_impl( | |
64 new net::HostResolverImpl(options, &net_log)); | |
65 SingleRequestHostResolver resolver(resolver_impl.get()); | |
66 | |
67 HostPortPair host_port_pair(host_, 80); | |
68 | |
69 rv_ = resolver.Resolve( | |
70 HostResolver::RequestInfo(host_port_pair), DEFAULT_PRIORITY, | |
71 addresses_, | |
72 base::Bind(&ResolverThread::OnResolutionComplete, | |
73 weak_factory_.GetWeakPtr()), | |
74 BoundNetLog()); | |
75 | |
76 if (rv_ != ERR_IO_PENDING) { | |
77 resolved_.Signal(); | |
78 } | |
79 | |
80 while (!resolved_.IsSignaled()) { | |
81 base::MessageLoop::current()->RunUntilIdle(); | |
82 } | |
83 } | |
84 | |
85 int ResolverThread::Resolve(const std::string& host, AddressList* addresses) { | |
86 host_ = host; | |
87 addresses_ = addresses; | |
88 this->Start(); | |
89 resolved_.Wait(); | |
90 this->Join(); | |
91 return rv_; | |
92 } | |
93 | |
94 void ResolverThread::OnResolutionComplete(int rv) { | |
95 rv_ = rv; | |
96 resolved_.Signal(); | |
97 } | |
98 | |
99 } // namespace | |
100 | |
101 // static | |
102 int SynchronousHostResolver::Resolve(const std::string& host, | |
103 AddressList* addresses) { | |
104 ResolverThread resolver; | |
105 return resolver.Resolve(host, addresses); | |
106 } | |
107 | |
108 } // namespace tools | |
109 } // namespace net | |
OLD | NEW |