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