1// Copyright (c) 2012 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.
4
5#include "chrome/browser/extensions/api/dns/dns_api.h"
6
7#include "base/bind.h"
8#include "base/values.h"
9#include "chrome/browser/browser_process.h"
10#include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
11#include "chrome/browser/io_thread.h"
12#include "chrome/common/extensions/api/experimental_dns.h"
13#include "content/public/browser/browser_thread.h"
14#include "net/base/host_port_pair.h"
15#include "net/base/net_errors.h"
16#include "net/base/net_log.h"
17
18using content::BrowserThread;
19using extensions::api::experimental_dns::ResolveCallbackResolveInfo;
20
21namespace Resolve = extensions::api::experimental_dns::Resolve;
22
23namespace extensions {
24
25DnsResolveFunction::DnsResolveFunction()
26    : response_(false),
27      io_thread_(g_browser_process->io_thread()),
28      request_handle_(new net::HostResolver::RequestHandle()),
29      addresses_(new net::AddressList) {
30}
31
32DnsResolveFunction::~DnsResolveFunction() {}
33
34bool DnsResolveFunction::RunImpl() {
35  scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_));
36  EXTENSION_FUNCTION_VALIDATE(params.get());
37
38  hostname_ = params->hostname;
39
40  bool result = BrowserThread::PostTask(
41      BrowserThread::IO, FROM_HERE,
42      base::Bind(&DnsResolveFunction::WorkOnIOThread, this));
43  DCHECK(result);
44  return true;
45}
46
47void DnsResolveFunction::WorkOnIOThread() {
48  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));
49
50  net::HostResolver* host_resolver =
51      HostResolverWrapper::GetInstance()->GetHostResolver(
52          io_thread_->globals()->host_resolver.get());
53  DCHECK(host_resolver);
54
55  // Yes, we are passing zero as the port. There are some interesting but not
56  // presently relevant reasons why HostResolver asks for the port of the
57  // hostname you'd like to resolve, even though it doesn't use that value in
58  // determining its answer.
59  net::HostPortPair host_port_pair(hostname_, 0);
60
61  net::HostResolver::RequestInfo request_info(host_port_pair);
62  int resolve_result = host_resolver->Resolve(
63      request_info, addresses_.get(),
64      base::Bind(&DnsResolveFunction::OnLookupFinished, this),
65      request_handle_.get(), net::BoundNetLog());
66
67  // Balanced in OnLookupFinished.
68  AddRef();
69
70  if (resolve_result != net::ERR_IO_PENDING)
71    OnLookupFinished(resolve_result);
72}
73
74void DnsResolveFunction::RespondOnUIThread() {
75  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
76  SendResponse(response_);
77}
78
79void DnsResolveFunction::OnLookupFinished(int resolve_result) {
80  scoped_ptr<ResolveCallbackResolveInfo> resolve_info(
81      new ResolveCallbackResolveInfo());
82  resolve_info->result_code = resolve_result;
83  if (resolve_result == net::OK) {
84    DCHECK(!addresses_->empty());
85    resolve_info->address.reset(
86        new std::string(addresses_->front().ToStringWithoutPort()));
87  }
88  results_ = Resolve::Results::Create(*resolve_info);
89  response_ = true;
90
91  bool post_task_result = BrowserThread::PostTask(
92      BrowserThread::UI, FROM_HERE,
93      base::Bind(&DnsResolveFunction::RespondOnUIThread, this));
94  DCHECK(post_task_result);
95
96  Release();  // Added in WorkOnIOThread().
97}
98
99}  // namespace extensions
100