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/location.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/single_thread_task_runner.h"
11 #include "base/thread_task_runner_handle.h"
12 #include "base/threading/simple_thread.h"
13 #include "net/base/host_port_pair.h"
14 #include "net/base/net_errors.h"
15 #include "net/dns/host_resolver_impl.h"
16 #include "net/dns/single_request_host_resolver.h"
24 class ResolverThread
: public base::SimpleThread
{
28 ~ResolverThread() override
;
30 // Called on the main thread.
31 int Resolve(const std::string
& host
, AddressList
* addresses
);
33 // SimpleThread methods:
37 void OnResolutionComplete(int rv
);
39 AddressList
* addresses_
;
43 base::WeakPtrFactory
<ResolverThread
> weak_factory_
;
45 DISALLOW_COPY_AND_ASSIGN(ResolverThread
);
48 ResolverThread::ResolverThread()
49 : SimpleThread("resolver_thread"),
51 weak_factory_(this) {}
53 ResolverThread::~ResolverThread() {}
55 void ResolverThread::Run() {
56 base::MessageLoopForIO loop
;
59 net::HostResolver::Options options
;
60 options
.max_concurrent_resolves
= 6;
61 options
.max_retry_attempts
= 3u;
62 scoped_ptr
<net::HostResolverImpl
> resolver_impl(
63 new net::HostResolverImpl(options
, &net_log
));
64 SingleRequestHostResolver
resolver(resolver_impl
.get());
66 HostPortPair
host_port_pair(host_
, 80);
67 rv_
= resolver
.Resolve(
68 HostResolver::RequestInfo(host_port_pair
), DEFAULT_PRIORITY
,
70 base::Bind(&ResolverThread::OnResolutionComplete
,
71 weak_factory_
.GetWeakPtr()),
74 if (rv_
!= ERR_IO_PENDING
)
77 // Run the mesage loop until OnResolutionComplete quits it.
78 base::MessageLoop::current()->Run();
81 int ResolverThread::Resolve(const std::string
& host
, AddressList
* addresses
) {
83 addresses_
= addresses
;
89 void ResolverThread::OnResolutionComplete(int rv
) {
91 base::ThreadTaskRunnerHandle::Get()->PostTask(
92 FROM_HERE
, base::MessageLoop::QuitClosure());
98 int SynchronousHostResolver::Resolve(const std::string
& host
,
99 AddressList
* addresses
) {
100 ResolverThread resolver
;
101 return resolver
.Resolve(host
, addresses
);