summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/dns/dns_api.cc
blob: 063873adfe6b2e2ad5b1fab09bc16b44f77ccbc9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/browser/extensions/api/dns/dns_api.h"

#include "base/bind.h"
#include "base/values.h"
#include "chrome/browser/browser_process.h"
#include "chrome/browser/extensions/api/dns/host_resolver_wrapper.h"
#include "chrome/browser/io_thread.h"
#include "chrome/common/extensions/api/experimental_dns.h"
#include "content/public/browser/browser_thread.h"
#include "net/base/host_port_pair.h"
#include "net/base/net_errors.h"
#include "net/base/net_log.h"

using content::BrowserThread;
using extensions::api::experimental_dns::ResolveCallbackResolveInfo;

namespace Resolve = extensions::api::experimental_dns::Resolve;

namespace extensions {

DnsResolveFunction::DnsResolveFunction()
    : response_(false),
      io_thread_(g_browser_process->io_thread()),
      request_handle_(new net::HostResolver::RequestHandle()),
      addresses_(new net::AddressList) {
}

DnsResolveFunction::~DnsResolveFunction() {}

bool DnsResolveFunction::RunImpl() {
  scoped_ptr<Resolve::Params> params(Resolve::Params::Create(*args_));
  EXTENSION_FUNCTION_VALIDATE(params.get());

  hostname_ = params->hostname;

  bool result = BrowserThread::PostTask(
      BrowserThread::IO, FROM_HERE,
      base::Bind(&DnsResolveFunction::WorkOnIOThread, this));
  DCHECK(result);
  return true;
}

void DnsResolveFunction::WorkOnIOThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO));

  net::HostResolver* host_resolver =
      HostResolverWrapper::GetInstance()->GetHostResolver(
          io_thread_->globals()->host_resolver.get());
  DCHECK(host_resolver);

  // Yes, we are passing zero as the port. There are some interesting but not
  // presently relevant reasons why HostResolver asks for the port of the
  // hostname you'd like to resolve, even though it doesn't use that value in
  // determining its answer.
  net::HostPortPair host_port_pair(hostname_, 0);

  net::HostResolver::RequestInfo request_info(host_port_pair);
  int resolve_result = host_resolver->Resolve(
      request_info, addresses_.get(),
      base::Bind(&DnsResolveFunction::OnLookupFinished, this),
      request_handle_.get(), net::BoundNetLog());

  // Balanced in OnLookupFinished.
  AddRef();

  if (resolve_result != net::ERR_IO_PENDING)
    OnLookupFinished(resolve_result);
}

void DnsResolveFunction::RespondOnUIThread() {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
  SendResponse(response_);
}

void DnsResolveFunction::OnLookupFinished(int resolve_result) {
  scoped_ptr<ResolveCallbackResolveInfo> resolve_info(
      new ResolveCallbackResolveInfo());
  resolve_info->result_code = resolve_result;
  if (resolve_result == net::OK) {
    DCHECK(!addresses_->empty());
    resolve_info->address.reset(
        new std::string(addresses_->front().ToStringWithoutPort()));
  }
  result_.reset(Resolve::Result::Create(*resolve_info));
  response_ = true;

  bool post_task_result = BrowserThread::PostTask(
      BrowserThread::UI, FROM_HERE,
      base::Bind(&DnsResolveFunction::RespondOnUIThread, this));
  DCHECK(post_task_result);

  Release();  // Added in WorkOnIOThread().
}

}  // namespace extensions