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