netwerk/dns/DNSRequestParent.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/dns/DNSRequestParent.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,105 @@
     1.4 +/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
     1.5 +/* vim: set sw=2 ts=8 et tw=80 : */
     1.6 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#include "mozilla/net/DNSRequestParent.h"
    1.11 +#include "nsIDNSService.h"
    1.12 +#include "nsNetCID.h"
    1.13 +#include "nsThreadUtils.h"
    1.14 +#include "nsIServiceManager.h"
    1.15 +#include "nsICancelable.h"
    1.16 +#include "nsIDNSRecord.h"
    1.17 +#include "nsHostResolver.h"
    1.18 +#include "mozilla/unused.h"
    1.19 +
    1.20 +using namespace mozilla::ipc;
    1.21 +
    1.22 +namespace mozilla {
    1.23 +namespace net {
    1.24 +
    1.25 +DNSRequestParent::DNSRequestParent()
    1.26 +  : mIPCClosed(false)
    1.27 +{
    1.28 +
    1.29 +}
    1.30 +
    1.31 +DNSRequestParent::~DNSRequestParent()
    1.32 +{
    1.33 +
    1.34 +}
    1.35 +
    1.36 +void
    1.37 +DNSRequestParent::DoAsyncResolve(const nsACString &hostname, uint32_t flags)
    1.38 +{
    1.39 +  nsresult rv;
    1.40 +  mFlags = flags;
    1.41 +  nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
    1.42 +  if (NS_SUCCEEDED(rv)) {
    1.43 +    nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
    1.44 +    nsCOMPtr<nsICancelable> unused;
    1.45 +    rv = dns->AsyncResolve(hostname, flags, this, mainThread,
    1.46 +                           getter_AddRefs(unused));
    1.47 +  }
    1.48 +
    1.49 +  if (NS_FAILED(rv) && !mIPCClosed) {
    1.50 +    unused << Send__delete__(this, DNSRequestResponse(rv));
    1.51 +  }
    1.52 +}
    1.53 +
    1.54 +void
    1.55 +DNSRequestParent::ActorDestroy(ActorDestroyReason why)
    1.56 +{
    1.57 +  // We may still have refcount>0 if DNS hasn't called our OnLookupComplete
    1.58 +  // yet, but child process has crashed.  We must not send any more msgs
    1.59 +  // to child, or IPDL will kill chrome process, too.
    1.60 +  mIPCClosed = true;
    1.61 +}
    1.62 +//-----------------------------------------------------------------------------
    1.63 +// DNSRequestParent::nsISupports
    1.64 +//-----------------------------------------------------------------------------
    1.65 +
    1.66 +NS_IMPL_ISUPPORTS(DNSRequestParent,
    1.67 +                  nsIDNSListener)
    1.68 +
    1.69 +//-----------------------------------------------------------------------------
    1.70 +// nsIDNSListener functions
    1.71 +//-----------------------------------------------------------------------------
    1.72 +
    1.73 +NS_IMETHODIMP
    1.74 +DNSRequestParent::OnLookupComplete(nsICancelable *request,
    1.75 +                                   nsIDNSRecord  *rec,
    1.76 +                                   nsresult       status)
    1.77 +{
    1.78 +  if (mIPCClosed) {
    1.79 +    // nothing to do: child probably crashed
    1.80 +    return NS_OK;
    1.81 +  }
    1.82 +
    1.83 +  if (NS_SUCCEEDED(status)) {
    1.84 +    MOZ_ASSERT(rec);
    1.85 +
    1.86 +    nsAutoCString cname;
    1.87 +    if (mFlags & nsHostResolver::RES_CANON_NAME) {
    1.88 +      rec->GetCanonicalName(cname);
    1.89 +    }
    1.90 +
    1.91 +    // Get IP addresses for hostname (use port 80 as dummy value for NetAddr)
    1.92 +    NetAddrArray array;
    1.93 +    NetAddr addr;
    1.94 +    while (NS_SUCCEEDED(rec->GetNextAddr(80, &addr))) {
    1.95 +      array.AppendElement(addr);
    1.96 +    }
    1.97 +
    1.98 +    unused << Send__delete__(this, DNSRequestResponse(DNSRecord(cname, array)));
    1.99 +  } else {
   1.100 +    unused << Send__delete__(this, DNSRequestResponse(status));
   1.101 +  }
   1.102 +
   1.103 +  return NS_OK;
   1.104 +}
   1.105 +
   1.106 +
   1.107 +
   1.108 +}} // mozilla::net

mercurial