michael@0: /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* vim: set sw=2 ts=8 et tw=80 : */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/net/DNSRequestParent.h" michael@0: #include "nsIDNSService.h" michael@0: #include "nsNetCID.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsICancelable.h" michael@0: #include "nsIDNSRecord.h" michael@0: #include "nsHostResolver.h" michael@0: #include "mozilla/unused.h" michael@0: michael@0: using namespace mozilla::ipc; michael@0: michael@0: namespace mozilla { michael@0: namespace net { michael@0: michael@0: DNSRequestParent::DNSRequestParent() michael@0: : mIPCClosed(false) michael@0: { michael@0: michael@0: } michael@0: michael@0: DNSRequestParent::~DNSRequestParent() michael@0: { michael@0: michael@0: } michael@0: michael@0: void michael@0: DNSRequestParent::DoAsyncResolve(const nsACString &hostname, uint32_t flags) michael@0: { michael@0: nsresult rv; michael@0: mFlags = flags; michael@0: nsCOMPtr dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: nsCOMPtr mainThread = do_GetMainThread(); michael@0: nsCOMPtr unused; michael@0: rv = dns->AsyncResolve(hostname, flags, this, mainThread, michael@0: getter_AddRefs(unused)); michael@0: } michael@0: michael@0: if (NS_FAILED(rv) && !mIPCClosed) { michael@0: unused << Send__delete__(this, DNSRequestResponse(rv)); michael@0: } michael@0: } michael@0: michael@0: void michael@0: DNSRequestParent::ActorDestroy(ActorDestroyReason why) michael@0: { michael@0: // We may still have refcount>0 if DNS hasn't called our OnLookupComplete michael@0: // yet, but child process has crashed. We must not send any more msgs michael@0: // to child, or IPDL will kill chrome process, too. michael@0: mIPCClosed = true; michael@0: } michael@0: //----------------------------------------------------------------------------- michael@0: // DNSRequestParent::nsISupports michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMPL_ISUPPORTS(DNSRequestParent, michael@0: nsIDNSListener) michael@0: michael@0: //----------------------------------------------------------------------------- michael@0: // nsIDNSListener functions michael@0: //----------------------------------------------------------------------------- michael@0: michael@0: NS_IMETHODIMP michael@0: DNSRequestParent::OnLookupComplete(nsICancelable *request, michael@0: nsIDNSRecord *rec, michael@0: nsresult status) michael@0: { michael@0: if (mIPCClosed) { michael@0: // nothing to do: child probably crashed michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (NS_SUCCEEDED(status)) { michael@0: MOZ_ASSERT(rec); michael@0: michael@0: nsAutoCString cname; michael@0: if (mFlags & nsHostResolver::RES_CANON_NAME) { michael@0: rec->GetCanonicalName(cname); michael@0: } michael@0: michael@0: // Get IP addresses for hostname (use port 80 as dummy value for NetAddr) michael@0: NetAddrArray array; michael@0: NetAddr addr; michael@0: while (NS_SUCCEEDED(rec->GetNextAddr(80, &addr))) { michael@0: array.AppendElement(addr); michael@0: } michael@0: michael@0: unused << Send__delete__(this, DNSRequestResponse(DNSRecord(cname, array))); michael@0: } else { michael@0: unused << Send__delete__(this, DNSRequestResponse(status)); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: michael@0: michael@0: }} // mozilla::net