Update {virtual,override,final} to follow C++11 style.
[chromium-blink-merge.git] / extensions / browser / api / dns / dns_api.cc
blob17bfdd7ad6b514e9355a87ebd2c0883aed5c5531
1 // Copyright 2014 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 "extensions/browser/api/dns/dns_api.h"
7 #include "base/bind.h"
8 #include "base/profiler/scoped_tracker.h"
9 #include "base/values.h"
10 #include "content/public/browser/browser_context.h"
11 #include "content/public/browser/browser_thread.h"
12 #include "content/public/browser/resource_context.h"
13 #include "extensions/browser/api/dns/host_resolver_wrapper.h"
14 #include "extensions/common/api/dns.h"
15 #include "net/base/host_port_pair.h"
16 #include "net/base/net_errors.h"
17 #include "net/base/net_log.h"
19 using content::BrowserThread;
20 using extensions::core_api::dns::ResolveCallbackResolveInfo;
22 namespace Resolve = extensions::core_api::dns::Resolve;
24 namespace extensions {
26 DnsResolveFunction::DnsResolveFunction()
27 : resource_context_(NULL),
28 response_(false),
29 request_handle_(new net::HostResolver::RequestHandle()),
30 addresses_(new net::AddressList) {}
32 DnsResolveFunction::~DnsResolveFunction() {}
34 bool DnsResolveFunction::RunAsync() {
35 scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_));
36 EXTENSION_FUNCTION_VALIDATE(params.get());
38 hostname_ = params->hostname;
39 resource_context_ = browser_context()->GetResourceContext();
41 bool result = BrowserThread::PostTask(
42 BrowserThread::IO,
43 FROM_HERE,
44 base::Bind(&DnsResolveFunction::WorkOnIOThread, this));
45 DCHECK(result);
46 return true;
49 void DnsResolveFunction::WorkOnIOThread() {
50 DCHECK_CURRENTLY_ON(BrowserThread::IO);
52 net::HostResolver* host_resolver =
53 HostResolverWrapper::GetInstance()->GetHostResolver(resource_context_);
54 DCHECK(host_resolver);
56 // Yes, we are passing zero as the port. There are some interesting but not
57 // presently relevant reasons why HostResolver asks for the port of the
58 // hostname you'd like to resolve, even though it doesn't use that value in
59 // determining its answer.
60 net::HostPortPair host_port_pair(hostname_, 0);
62 net::HostResolver::RequestInfo request_info(host_port_pair);
63 int resolve_result = host_resolver->Resolve(
64 request_info,
65 net::DEFAULT_PRIORITY,
66 addresses_.get(),
67 base::Bind(&DnsResolveFunction::OnLookupFinished, this),
68 request_handle_.get(),
69 net::BoundNetLog());
71 // Balanced in OnLookupFinished.
72 AddRef();
74 if (resolve_result != net::ERR_IO_PENDING)
75 OnLookupFinished(resolve_result);
78 void DnsResolveFunction::RespondOnUIThread() {
79 DCHECK_CURRENTLY_ON(BrowserThread::UI);
80 SendResponse(response_);
83 void DnsResolveFunction::OnLookupFinished(int resolve_result) {
84 // TODO(vadimt): Remove ScopedTracker below once crbug.com/436634 is fixed.
85 tracked_objects::ScopedTracker tracking_profile(
86 FROM_HERE_WITH_EXPLICIT_FUNCTION(
87 "436634 DnsResolveFunction::OnLookupFinished"));
89 scoped_ptr<ResolveCallbackResolveInfo> resolve_info(
90 new ResolveCallbackResolveInfo());
91 resolve_info->result_code = resolve_result;
92 if (resolve_result == net::OK) {
93 DCHECK(!addresses_->empty());
94 resolve_info->address.reset(
95 new std::string(addresses_->front().ToStringWithoutPort()));
97 results_ = Resolve::Results::Create(*resolve_info);
98 response_ = true;
100 bool post_task_result = BrowserThread::PostTask(
101 BrowserThread::UI,
102 FROM_HERE,
103 base::Bind(&DnsResolveFunction::RespondOnUIThread, this));
104 DCHECK(post_task_result);
106 Release(); // Added in WorkOnIOThread().
109 } // namespace extensions