/src/mozilla-central/netwerk/dns/DNSRequestParent.cpp
Line | Count | Source (jump to first uncovered line) |
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/. */ |
6 | | |
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" |
16 | | |
17 | | using namespace mozilla::ipc; |
18 | | |
19 | | namespace mozilla { |
20 | | namespace net { |
21 | | |
22 | | DNSRequestParent::DNSRequestParent() |
23 | | : mFlags(0) |
24 | | , mIPCClosed(false) |
25 | 0 | { |
26 | 0 |
|
27 | 0 | } |
28 | | |
29 | | void |
30 | | DNSRequestParent::DoAsyncResolve(const nsACString &hostname, |
31 | | const OriginAttributes &originAttributes, |
32 | | uint32_t flags) |
33 | 0 | { |
34 | 0 | nsresult rv; |
35 | 0 | mFlags = flags; |
36 | 0 | nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); |
37 | 0 | if (NS_SUCCEEDED(rv)) { |
38 | 0 | nsCOMPtr<nsIEventTarget> main = GetMainThreadEventTarget(); |
39 | 0 | nsCOMPtr<nsICancelable> unused; |
40 | 0 | rv = dns->AsyncResolveNative(hostname, flags, this, |
41 | 0 | main, originAttributes, |
42 | 0 | getter_AddRefs(unused)); |
43 | 0 | } |
44 | 0 |
|
45 | 0 | if (NS_FAILED(rv) && !mIPCClosed) { |
46 | 0 | mIPCClosed = true; |
47 | 0 | Unused << SendLookupCompleted(DNSRequestResponse(rv)); |
48 | 0 | } |
49 | 0 | } |
50 | | |
51 | | mozilla::ipc::IPCResult |
52 | | DNSRequestParent::RecvCancelDNSRequest(const nsCString& hostName, |
53 | | const uint16_t& type, |
54 | | const OriginAttributes& originAttributes, |
55 | | const uint32_t& flags, |
56 | | const nsresult& reason) |
57 | 0 | { |
58 | 0 | nsresult rv; |
59 | 0 | nsCOMPtr<nsIDNSService> dns = do_GetService(NS_DNSSERVICE_CONTRACTID, &rv); |
60 | 0 | if (NS_SUCCEEDED(rv)) { |
61 | 0 | if (type == nsIDNSService::RESOLVE_TYPE_DEFAULT) { |
62 | 0 | rv = dns->CancelAsyncResolveNative(hostName, flags, |
63 | 0 | this, reason, |
64 | 0 | originAttributes); |
65 | 0 | } else { |
66 | 0 | rv = dns->CancelAsyncResolveByTypeNative(hostName, type, flags, |
67 | 0 | this, reason, |
68 | 0 | originAttributes); |
69 | 0 | } |
70 | 0 | } |
71 | 0 | return IPC_OK(); |
72 | 0 | } |
73 | | |
74 | | mozilla::ipc::IPCResult |
75 | | DNSRequestParent::Recv__delete__() |
76 | 0 | { |
77 | 0 | mIPCClosed = true; |
78 | 0 | return IPC_OK(); |
79 | 0 | } |
80 | | |
81 | | void |
82 | | DNSRequestParent::ActorDestroy(ActorDestroyReason why) |
83 | 0 | { |
84 | 0 | // We may still have refcount>0 if DNS hasn't called our OnLookupComplete |
85 | 0 | // yet, but child process has crashed. We must not send any more msgs |
86 | 0 | // to child, or IPDL will kill chrome process, too. |
87 | 0 | mIPCClosed = true; |
88 | 0 | } |
89 | | //----------------------------------------------------------------------------- |
90 | | // DNSRequestParent::nsISupports |
91 | | //----------------------------------------------------------------------------- |
92 | | |
93 | | NS_IMPL_ISUPPORTS(DNSRequestParent, |
94 | | nsIDNSListener) |
95 | | |
96 | | //----------------------------------------------------------------------------- |
97 | | // nsIDNSListener functions |
98 | | //----------------------------------------------------------------------------- |
99 | | |
100 | | NS_IMETHODIMP |
101 | | DNSRequestParent::OnLookupComplete(nsICancelable *request, |
102 | | nsIDNSRecord *rec, |
103 | | nsresult status) |
104 | 0 | { |
105 | 0 | if (mIPCClosed) { |
106 | 0 | // nothing to do: child probably crashed |
107 | 0 | return NS_OK; |
108 | 0 | } |
109 | 0 | |
110 | 0 | if (NS_SUCCEEDED(status)) { |
111 | 0 | MOZ_ASSERT(rec); |
112 | 0 |
|
113 | 0 | nsAutoCString cname; |
114 | 0 | if (mFlags & nsHostResolver::RES_CANON_NAME) { |
115 | 0 | rec->GetCanonicalName(cname); |
116 | 0 | } |
117 | 0 |
|
118 | 0 | // Get IP addresses for hostname (use port 80 as dummy value for NetAddr) |
119 | 0 | NetAddrArray array; |
120 | 0 | NetAddr addr; |
121 | 0 | while (NS_SUCCEEDED(rec->GetNextAddr(80, &addr))) { |
122 | 0 | array.AppendElement(addr); |
123 | 0 | } |
124 | 0 |
|
125 | 0 | Unused << SendLookupCompleted(DNSRequestResponse(DNSRecord(cname, array))); |
126 | 0 | } else { |
127 | 0 | Unused << SendLookupCompleted(DNSRequestResponse(status)); |
128 | 0 | } |
129 | 0 |
|
130 | 0 | mIPCClosed = true; |
131 | 0 | return NS_OK; |
132 | 0 | } |
133 | | |
134 | | NS_IMETHODIMP |
135 | | DNSRequestParent::OnLookupByTypeComplete(nsICancelable *aRequest, |
136 | | nsIDNSByTypeRecord *aRes, |
137 | | nsresult aStatus) |
138 | 0 | { |
139 | 0 | if (mIPCClosed) { |
140 | 0 | // nothing to do: child probably crashed |
141 | 0 | return NS_OK; |
142 | 0 | } |
143 | 0 | |
144 | 0 | if (NS_SUCCEEDED(aStatus)) { |
145 | 0 | nsTArray<nsCString> rec; |
146 | 0 | aRes->GetRecords(rec); |
147 | 0 | Unused << SendLookupCompleted(DNSRequestResponse(rec)); |
148 | 0 | } else { |
149 | 0 | Unused << SendLookupCompleted(DNSRequestResponse(aStatus)); |
150 | 0 | } |
151 | 0 | mIPCClosed = true; |
152 | 0 | return NS_OK; |
153 | 0 | } |
154 | | |
155 | | } // namespace net |
156 | | } // namespace mozilla |