[Android WebView] Fix webview perf bot switchover to use org.chromium.webview_shell...
[chromium-blink-merge.git] / net / tools / quic / synchronous_host_resolver.cc
blob44d2cad57d9248bcdcd2b31a322646a1d2a69073
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"
18 namespace net {
20 namespace tools {
22 namespace {
24 class ResolverThread : public base::SimpleThread {
25 public:
26 ResolverThread();
28 ~ResolverThread() override;
30 // Called on the main thread.
31 int Resolve(const std::string& host, AddressList* addresses);
33 // SimpleThread methods:
34 void Run() override;
36 private:
37 void OnResolutionComplete(int rv);
39 AddressList* addresses_;
40 std::string host_;
41 int rv_;
43 base::WeakPtrFactory<ResolverThread> weak_factory_;
45 DISALLOW_COPY_AND_ASSIGN(ResolverThread);
48 ResolverThread::ResolverThread()
49 : SimpleThread("resolver_thread"),
50 rv_(ERR_UNEXPECTED),
51 weak_factory_(this) {}
53 ResolverThread::~ResolverThread() {}
55 void ResolverThread::Run() {
56 base::AtExitManager exit_manager;
57 base::MessageLoopForIO loop;
59 net::NetLog net_log;
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,
70 addresses_,
71 base::Bind(&ResolverThread::OnResolutionComplete,
72 weak_factory_.GetWeakPtr()),
73 BoundNetLog());
75 if (rv_ != ERR_IO_PENDING)
76 return;
78 // Run the mesage loop until OnResolutionComplete quits it.
79 base::MessageLoop::current()->Run();
82 int ResolverThread::Resolve(const std::string& host, AddressList* addresses) {
83 host_ = host;
84 addresses_ = addresses;
85 this->Start();
86 this->Join();
87 return rv_;
90 void ResolverThread::OnResolutionComplete(int rv) {
91 rv_ = rv;
92 base::ThreadTaskRunnerHandle::Get()->PostTask(
93 FROM_HERE, base::MessageLoop::QuitClosure());
96 } // namespace
98 // static
99 int SynchronousHostResolver::Resolve(const std::string& host,
100 AddressList* addresses) {
101 ResolverThread resolver;
102 return resolver.Resolve(host, addresses);
105 } // namespace tools
106 } // namespace net