Bumping manifests a=b2g-bump
[gecko.git] / netwerk / dns / DNSRequestParent.cpp
blob656fabc4af4bdea8d51604a1dc27afaa54434ed6
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set sw=2 ts=8 et tw=80 : */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/net/DNSRequestParent.h"
8 #include "nsIDNSService.h"
9 #include "nsNetCID.h"
10 #include "nsThreadUtils.h"
11 #include "nsIServiceManager.h"
12 #include "nsICancelable.h"
13 #include "nsIDNSRecord.h"
14 #include "nsHostResolver.h"
15 #include "mozilla/unused.h"
17 using namespace mozilla::ipc;
19 namespace mozilla {
20 namespace net {
22 DNSRequestParent::DNSRequestParent()
23 : mIPCClosed(false)
28 DNSRequestParent::~DNSRequestParent()
33 void
34 DNSRequestParent::DoAsyncResolve(const nsACString &hostname, uint32_t flags,
35 const nsACString &networkInterface)
37 nsresult rv;
38 mFlags = flags;
39 nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
40 if (NS_SUCCEEDED(rv)) {
41 nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
42 nsCOMPtr<nsICancelable> unused;
43 rv = dns->AsyncResolveExtended(hostname, flags, networkInterface, this,
44 mainThread, getter_AddRefs(unused));
47 if (NS_FAILED(rv) && !mIPCClosed) {
48 mIPCClosed = true;
49 unused << SendLookupCompleted(DNSRequestResponse(rv));
53 bool
54 DNSRequestParent::RecvCancelDNSRequest(const nsCString& hostName,
55 const uint32_t& flags,
56 const nsCString& networkInterface,
57 const nsresult& reason)
59 nsresult rv;
60 nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv);
61 if (NS_SUCCEEDED(rv)) {
62 rv = dns->CancelAsyncResolveExtended(hostName, flags, networkInterface,
63 this, reason);
65 return true;
68 bool
69 DNSRequestParent::Recv__delete__()
71 mIPCClosed = true;
72 return true;
75 void
76 DNSRequestParent::ActorDestroy(ActorDestroyReason why)
78 // We may still have refcount>0 if DNS hasn't called our OnLookupComplete
79 // yet, but child process has crashed. We must not send any more msgs
80 // to child, or IPDL will kill chrome process, too.
81 mIPCClosed = true;
83 //-----------------------------------------------------------------------------
84 // DNSRequestParent::nsISupports
85 //-----------------------------------------------------------------------------
87 NS_IMPL_ISUPPORTS(DNSRequestParent,
88 nsIDNSListener)
90 //-----------------------------------------------------------------------------
91 // nsIDNSListener functions
92 //-----------------------------------------------------------------------------
94 NS_IMETHODIMP
95 DNSRequestParent::OnLookupComplete(nsICancelable *request,
96 nsIDNSRecord *rec,
97 nsresult status)
99 if (mIPCClosed) {
100 // nothing to do: child probably crashed
101 return NS_OK;
104 if (NS_SUCCEEDED(status)) {
105 MOZ_ASSERT(rec);
107 nsAutoCString cname;
108 if (mFlags & nsHostResolver::RES_CANON_NAME) {
109 rec->GetCanonicalName(cname);
112 // Get IP addresses for hostname (use port 80 as dummy value for NetAddr)
113 NetAddrArray array;
114 NetAddr addr;
115 while (NS_SUCCEEDED(rec->GetNextAddr(80, &addr))) {
116 array.AppendElement(addr);
119 unused << SendLookupCompleted(DNSRequestResponse(DNSRecord(cname, array)));
120 } else {
121 unused << SendLookupCompleted(DNSRequestResponse(status));
124 mIPCClosed = true;
125 return NS_OK;
130 }} // mozilla::net