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.
5 #include "net/tools/quic/synchronous_host_resolver.h"
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"
21 class ResolverThread
: public base::SimpleThread
{
25 ~ResolverThread() override
;
27 // Called on the main thread.
28 int Resolve(const std::string
& host
, AddressList
* addresses
);
30 // SimpleThread methods:
34 void OnResolutionComplete(int rv
);
36 AddressList
* addresses_
;
40 base::WeakPtrFactory
<ResolverThread
> weak_factory_
;
42 DISALLOW_COPY_AND_ASSIGN(ResolverThread
);
45 ResolverThread::ResolverThread()
46 : SimpleThread("resolver_thread"),
48 weak_factory_(this) {}
50 ResolverThread::~ResolverThread() {}
52 void ResolverThread::Run() {
53 base::AtExitManager exit_manager
;
54 base::MessageLoopForIO loop
;
57 net::HostResolver::Options options
;
58 options
.max_concurrent_resolves
= 6;
59 options
.max_retry_attempts
= 3u;
60 scoped_ptr
<net::HostResolverImpl
> resolver_impl(
61 new net::HostResolverImpl(options
, &net_log
));
62 SingleRequestHostResolver
resolver(resolver_impl
.get());
64 HostPortPair
host_port_pair(host_
, 80);
65 rv_
= resolver
.Resolve(
66 HostResolver::RequestInfo(host_port_pair
), DEFAULT_PRIORITY
,
68 base::Bind(&ResolverThread::OnResolutionComplete
,
69 weak_factory_
.GetWeakPtr()),
72 if (rv_
!= ERR_IO_PENDING
)
75 // Run the mesage loop until OnResolutionComplete quits it.
76 base::MessageLoop::current()->Run();
79 int ResolverThread::Resolve(const std::string
& host
, AddressList
* addresses
) {
81 addresses_
= addresses
;
87 void ResolverThread::OnResolutionComplete(int rv
) {
89 base::MessageLoop::current()->PostTask(
91 base::MessageLoop::QuitClosure());
97 int SynchronousHostResolver::Resolve(const std::string
& host
,
98 AddressList
* addresses
) {
99 ResolverThread resolver
;
100 return resolver
.Resolve(host
, addresses
);