Wed, 31 Dec 2014 06:55:46 +0100
Added tag TORBROWSER_REPLICA for changeset 6474c204b198
michael@0 | 1 | /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* vim: set sw=2 ts=8 et tw=80 : */ |
michael@0 | 3 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "mozilla/net/DNSRequestParent.h" |
michael@0 | 8 | #include "nsIDNSService.h" |
michael@0 | 9 | #include "nsNetCID.h" |
michael@0 | 10 | #include "nsThreadUtils.h" |
michael@0 | 11 | #include "nsIServiceManager.h" |
michael@0 | 12 | #include "nsICancelable.h" |
michael@0 | 13 | #include "nsIDNSRecord.h" |
michael@0 | 14 | #include "nsHostResolver.h" |
michael@0 | 15 | #include "mozilla/unused.h" |
michael@0 | 16 | |
michael@0 | 17 | using namespace mozilla::ipc; |
michael@0 | 18 | |
michael@0 | 19 | namespace mozilla { |
michael@0 | 20 | namespace net { |
michael@0 | 21 | |
michael@0 | 22 | DNSRequestParent::DNSRequestParent() |
michael@0 | 23 | : mIPCClosed(false) |
michael@0 | 24 | { |
michael@0 | 25 | |
michael@0 | 26 | } |
michael@0 | 27 | |
michael@0 | 28 | DNSRequestParent::~DNSRequestParent() |
michael@0 | 29 | { |
michael@0 | 30 | |
michael@0 | 31 | } |
michael@0 | 32 | |
michael@0 | 33 | void |
michael@0 | 34 | DNSRequestParent::DoAsyncResolve(const nsACString &hostname, uint32_t flags) |
michael@0 | 35 | { |
michael@0 | 36 | nsresult rv; |
michael@0 | 37 | mFlags = flags; |
michael@0 | 38 | nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); |
michael@0 | 39 | if (NS_SUCCEEDED(rv)) { |
michael@0 | 40 | nsCOMPtr<nsIThread> mainThread = do_GetMainThread(); |
michael@0 | 41 | nsCOMPtr<nsICancelable> unused; |
michael@0 | 42 | rv = dns->AsyncResolve(hostname, flags, this, mainThread, |
michael@0 | 43 | getter_AddRefs(unused)); |
michael@0 | 44 | } |
michael@0 | 45 | |
michael@0 | 46 | if (NS_FAILED(rv) && !mIPCClosed) { |
michael@0 | 47 | unused << Send__delete__(this, DNSRequestResponse(rv)); |
michael@0 | 48 | } |
michael@0 | 49 | } |
michael@0 | 50 | |
michael@0 | 51 | void |
michael@0 | 52 | DNSRequestParent::ActorDestroy(ActorDestroyReason why) |
michael@0 | 53 | { |
michael@0 | 54 | // We may still have refcount>0 if DNS hasn't called our OnLookupComplete |
michael@0 | 55 | // yet, but child process has crashed. We must not send any more msgs |
michael@0 | 56 | // to child, or IPDL will kill chrome process, too. |
michael@0 | 57 | mIPCClosed = true; |
michael@0 | 58 | } |
michael@0 | 59 | //----------------------------------------------------------------------------- |
michael@0 | 60 | // DNSRequestParent::nsISupports |
michael@0 | 61 | //----------------------------------------------------------------------------- |
michael@0 | 62 | |
michael@0 | 63 | NS_IMPL_ISUPPORTS(DNSRequestParent, |
michael@0 | 64 | nsIDNSListener) |
michael@0 | 65 | |
michael@0 | 66 | //----------------------------------------------------------------------------- |
michael@0 | 67 | // nsIDNSListener functions |
michael@0 | 68 | //----------------------------------------------------------------------------- |
michael@0 | 69 | |
michael@0 | 70 | NS_IMETHODIMP |
michael@0 | 71 | DNSRequestParent::OnLookupComplete(nsICancelable *request, |
michael@0 | 72 | nsIDNSRecord *rec, |
michael@0 | 73 | nsresult status) |
michael@0 | 74 | { |
michael@0 | 75 | if (mIPCClosed) { |
michael@0 | 76 | // nothing to do: child probably crashed |
michael@0 | 77 | return NS_OK; |
michael@0 | 78 | } |
michael@0 | 79 | |
michael@0 | 80 | if (NS_SUCCEEDED(status)) { |
michael@0 | 81 | MOZ_ASSERT(rec); |
michael@0 | 82 | |
michael@0 | 83 | nsAutoCString cname; |
michael@0 | 84 | if (mFlags & nsHostResolver::RES_CANON_NAME) { |
michael@0 | 85 | rec->GetCanonicalName(cname); |
michael@0 | 86 | } |
michael@0 | 87 | |
michael@0 | 88 | // Get IP addresses for hostname (use port 80 as dummy value for NetAddr) |
michael@0 | 89 | NetAddrArray array; |
michael@0 | 90 | NetAddr addr; |
michael@0 | 91 | while (NS_SUCCEEDED(rec->GetNextAddr(80, &addr))) { |
michael@0 | 92 | array.AppendElement(addr); |
michael@0 | 93 | } |
michael@0 | 94 | |
michael@0 | 95 | unused << Send__delete__(this, DNSRequestResponse(DNSRecord(cname, array))); |
michael@0 | 96 | } else { |
michael@0 | 97 | unused << Send__delete__(this, DNSRequestResponse(status)); |
michael@0 | 98 | } |
michael@0 | 99 | |
michael@0 | 100 | return NS_OK; |
michael@0 | 101 | } |
michael@0 | 102 | |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | }} // mozilla::net |