Pin Chrome's shortcut to the Win10 Start menu on install and OS upgrade.
[chromium-blink-merge.git] / net / tools / quic / synchronous_host_resolver.cc
blob4c0cb3f216bdbf6b24c3abe349fd431fc33892a4
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::MessageLoopForIO loop;
58 net::NetLog net_log;
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,
69 addresses_,
70 base::Bind(&ResolverThread::OnResolutionComplete,
71 weak_factory_.GetWeakPtr()),
72 BoundNetLog());
74 if (rv_ != ERR_IO_PENDING)
75 return;
77 // Run the mesage loop until OnResolutionComplete quits it.
78 base::MessageLoop::current()->Run();
81 int ResolverThread::Resolve(const std::string& host, AddressList* addresses) {
82 host_ = host;
83 addresses_ = addresses;
84 this->Start();
85 this->Join();
86 return rv_;
89 void ResolverThread::OnResolutionComplete(int rv) {
90 rv_ = rv;
91 base::ThreadTaskRunnerHandle::Get()->PostTask(
92 FROM_HERE, base::MessageLoop::QuitClosure());
95 } // namespace
97 // static
98 int SynchronousHostResolver::Resolve(const std::string& host,
99 AddressList* addresses) {
100 ResolverThread resolver;
101 return resolver.Resolve(host, addresses);
104 } // namespace tools
105 } // namespace net