Home | History | Annotate | Download | only in dns
      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/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 
     18 using content::BrowserThread;
     19 using extensions::api::dns::ResolveCallbackResolveInfo;
     20 
     21 namespace Resolve = extensions::api::dns::Resolve;
     22 
     23 namespace extensions {
     24 
     25 DnsResolveFunction::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 
     32 DnsResolveFunction::~DnsResolveFunction() {}
     33 
     34 bool 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 
     47 void 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,
     64       net::DEFAULT_PRIORITY,
     65       addresses_.get(),
     66       base::Bind(&DnsResolveFunction::OnLookupFinished, this),
     67       request_handle_.get(),
     68       net::BoundNetLog());
     69 
     70   // Balanced in OnLookupFinished.
     71   AddRef();
     72 
     73   if (resolve_result != net::ERR_IO_PENDING)
     74     OnLookupFinished(resolve_result);
     75 }
     76 
     77 void DnsResolveFunction::RespondOnUIThread() {
     78   DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI));
     79   SendResponse(response_);
     80 }
     81 
     82 void DnsResolveFunction::OnLookupFinished(int resolve_result) {
     83   scoped_ptr<ResolveCallbackResolveInfo> resolve_info(
     84       new ResolveCallbackResolveInfo());
     85   resolve_info->result_code = resolve_result;
     86   if (resolve_result == net::OK) {
     87     DCHECK(!addresses_->empty());
     88     resolve_info->address.reset(
     89         new std::string(addresses_->front().ToStringWithoutPort()));
     90   }
     91   results_ = Resolve::Results::Create(*resolve_info);
     92   response_ = true;
     93 
     94   bool post_task_result = BrowserThread::PostTask(
     95       BrowserThread::UI, FROM_HERE,
     96       base::Bind(&DnsResolveFunction::RespondOnUIThread, this));
     97   DCHECK(post_task_result);
     98 
     99   Release();  // Added in WorkOnIOThread().
    100 }
    101 
    102 }  // namespace extensions
    103