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::AtExitManager exit_manager
;
57 base::MessageLoopForIO loop
;
60 net::HostResolver::Options options
;
61 options
.max_concurrent_resolves
= 6;
62 options
.max_retry_attempts
= 3u;
63 scoped_ptr
<net::HostResolverImpl
> resolver_impl(
64 new net::HostResolverImpl(options
, &net_log
));
65 SingleRequestHostResolver
resolver(resolver_impl
.get());
67 HostPortPair
host_port_pair(host_
, 80);
68 rv_
= resolver
.Resolve(
69 HostResolver::RequestInfo(host_port_pair
), DEFAULT_PRIORITY
,
71 base::Bind(&ResolverThread::OnResolutionComplete
,
72 weak_factory_
.GetWeakPtr()),
75 if (rv_
!= ERR_IO_PENDING
)
78 // Run the mesage loop until OnResolutionComplete quits it.
79 base::MessageLoop::current()->Run();
82 int ResolverThread::Resolve(const std::string
& host
, AddressList
* addresses
) {
84 addresses_
= addresses
;
90 void ResolverThread::OnResolutionComplete(int rv
) {
92 base::ThreadTaskRunnerHandle::Get()->PostTask(
93 FROM_HERE
, base::MessageLoop::QuitClosure());
99 int SynchronousHostResolver::Resolve(const std::string
& host
,
100 AddressList
* addresses
) {
101 ResolverThread resolver
;
102 return resolver
.Resolve(host
, addresses
);