Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
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/DNSRequestChild.h" |
michael@0 | 8 | #include "mozilla/net/NeckoChild.h" |
michael@0 | 9 | #include "nsIDNSRecord.h" |
michael@0 | 10 | #include "nsHostResolver.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsNetAddr.h" |
michael@0 | 13 | #include "nsIThread.h" |
michael@0 | 14 | #include "nsThreadUtils.h" |
michael@0 | 15 | |
michael@0 | 16 | using namespace mozilla::ipc; |
michael@0 | 17 | |
michael@0 | 18 | namespace mozilla { |
michael@0 | 19 | namespace net { |
michael@0 | 20 | |
michael@0 | 21 | //----------------------------------------------------------------------------- |
michael@0 | 22 | // ChildDNSRecord: |
michael@0 | 23 | // A simple class to provide nsIDNSRecord on the child |
michael@0 | 24 | //----------------------------------------------------------------------------- |
michael@0 | 25 | |
michael@0 | 26 | class ChildDNSRecord : public nsIDNSRecord |
michael@0 | 27 | { |
michael@0 | 28 | public: |
michael@0 | 29 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 30 | NS_DECL_NSIDNSRECORD |
michael@0 | 31 | |
michael@0 | 32 | ChildDNSRecord(const DNSRecord& reply, uint16_t flags); |
michael@0 | 33 | virtual ~ChildDNSRecord(); |
michael@0 | 34 | |
michael@0 | 35 | private: |
michael@0 | 36 | nsCString mCanonicalName; |
michael@0 | 37 | nsTArray<NetAddr> mAddresses; |
michael@0 | 38 | uint32_t mCurrent; // addr iterator |
michael@0 | 39 | uint32_t mLength; // number of addrs |
michael@0 | 40 | uint16_t mFlags; |
michael@0 | 41 | }; |
michael@0 | 42 | |
michael@0 | 43 | NS_IMPL_ISUPPORTS(ChildDNSRecord, nsIDNSRecord) |
michael@0 | 44 | |
michael@0 | 45 | ChildDNSRecord::ChildDNSRecord(const DNSRecord& reply, uint16_t flags) |
michael@0 | 46 | : mCurrent(0) |
michael@0 | 47 | , mFlags(flags) |
michael@0 | 48 | { |
michael@0 | 49 | mCanonicalName = reply.canonicalName(); |
michael@0 | 50 | |
michael@0 | 51 | // A shame IPDL gives us no way to grab ownership of array: so copy it. |
michael@0 | 52 | const nsTArray<NetAddr>& addrs = reply.addrs(); |
michael@0 | 53 | uint32_t i = 0; |
michael@0 | 54 | mLength = addrs.Length(); |
michael@0 | 55 | for (; i < mLength; i++) { |
michael@0 | 56 | mAddresses.AppendElement(addrs[i]); |
michael@0 | 57 | } |
michael@0 | 58 | } |
michael@0 | 59 | |
michael@0 | 60 | ChildDNSRecord::~ChildDNSRecord() |
michael@0 | 61 | { |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | //----------------------------------------------------------------------------- |
michael@0 | 65 | // ChildDNSRecord::nsIDNSRecord |
michael@0 | 66 | //----------------------------------------------------------------------------- |
michael@0 | 67 | |
michael@0 | 68 | NS_IMETHODIMP |
michael@0 | 69 | ChildDNSRecord::GetCanonicalName(nsACString &result) |
michael@0 | 70 | { |
michael@0 | 71 | if (!(mFlags & nsHostResolver::RES_CANON_NAME)) { |
michael@0 | 72 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 73 | } |
michael@0 | 74 | |
michael@0 | 75 | result = mCanonicalName; |
michael@0 | 76 | return NS_OK; |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | NS_IMETHODIMP |
michael@0 | 80 | ChildDNSRecord::GetNextAddr(uint16_t port, NetAddr *addr) |
michael@0 | 81 | { |
michael@0 | 82 | if (mCurrent >= mLength) { |
michael@0 | 83 | return NS_ERROR_NOT_AVAILABLE; |
michael@0 | 84 | } |
michael@0 | 85 | |
michael@0 | 86 | memcpy(addr, &mAddresses[mCurrent++], sizeof(NetAddr)); |
michael@0 | 87 | |
michael@0 | 88 | // both Ipv4/6 use same bits for port, so safe to just use ipv4's field |
michael@0 | 89 | addr->inet.port = port; |
michael@0 | 90 | |
michael@0 | 91 | return NS_OK; |
michael@0 | 92 | } |
michael@0 | 93 | |
michael@0 | 94 | // shamelessly copied from nsDNSRecord |
michael@0 | 95 | NS_IMETHODIMP |
michael@0 | 96 | ChildDNSRecord::GetScriptableNextAddr(uint16_t port, nsINetAddr **result) |
michael@0 | 97 | { |
michael@0 | 98 | NetAddr addr; |
michael@0 | 99 | nsresult rv = GetNextAddr(port, &addr); |
michael@0 | 100 | if (NS_FAILED(rv)) return rv; |
michael@0 | 101 | |
michael@0 | 102 | NS_ADDREF(*result = new nsNetAddr(&addr)); |
michael@0 | 103 | |
michael@0 | 104 | return NS_OK; |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | // also copied from nsDNSRecord |
michael@0 | 108 | NS_IMETHODIMP |
michael@0 | 109 | ChildDNSRecord::GetNextAddrAsString(nsACString &result) |
michael@0 | 110 | { |
michael@0 | 111 | NetAddr addr; |
michael@0 | 112 | nsresult rv = GetNextAddr(0, &addr); |
michael@0 | 113 | if (NS_FAILED(rv)) { |
michael@0 | 114 | return rv; |
michael@0 | 115 | } |
michael@0 | 116 | |
michael@0 | 117 | char buf[kIPv6CStrBufSize]; |
michael@0 | 118 | if (NetAddrToString(&addr, buf, sizeof(buf))) { |
michael@0 | 119 | result.Assign(buf); |
michael@0 | 120 | return NS_OK; |
michael@0 | 121 | } |
michael@0 | 122 | NS_ERROR("NetAddrToString failed unexpectedly"); |
michael@0 | 123 | return NS_ERROR_FAILURE; // conversion failed for some reason |
michael@0 | 124 | } |
michael@0 | 125 | |
michael@0 | 126 | NS_IMETHODIMP |
michael@0 | 127 | ChildDNSRecord::HasMore(bool *result) |
michael@0 | 128 | { |
michael@0 | 129 | *result = mCurrent < mLength; |
michael@0 | 130 | return NS_OK; |
michael@0 | 131 | } |
michael@0 | 132 | |
michael@0 | 133 | NS_IMETHODIMP |
michael@0 | 134 | ChildDNSRecord::Rewind() |
michael@0 | 135 | { |
michael@0 | 136 | mCurrent = 0; |
michael@0 | 137 | return NS_OK; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | NS_IMETHODIMP |
michael@0 | 141 | ChildDNSRecord::ReportUnusable(uint16_t aPort) |
michael@0 | 142 | { |
michael@0 | 143 | // "We thank you for your feedback" == >/dev/null |
michael@0 | 144 | // TODO: we could send info back to parent. |
michael@0 | 145 | return NS_OK; |
michael@0 | 146 | } |
michael@0 | 147 | |
michael@0 | 148 | //----------------------------------------------------------------------------- |
michael@0 | 149 | // DNSRequestChild |
michael@0 | 150 | //----------------------------------------------------------------------------- |
michael@0 | 151 | |
michael@0 | 152 | DNSRequestChild::DNSRequestChild(const nsCString& aHost, |
michael@0 | 153 | const uint32_t& aFlags, |
michael@0 | 154 | nsIDNSListener *aListener, |
michael@0 | 155 | nsIEventTarget *target) |
michael@0 | 156 | : mListener(aListener) |
michael@0 | 157 | , mTarget(target) |
michael@0 | 158 | , mResultStatus(NS_OK) |
michael@0 | 159 | , mHost(aHost) |
michael@0 | 160 | , mFlags(aFlags) |
michael@0 | 161 | { |
michael@0 | 162 | } |
michael@0 | 163 | |
michael@0 | 164 | void |
michael@0 | 165 | DNSRequestChild::StartRequest() |
michael@0 | 166 | { |
michael@0 | 167 | // we can only do IPDL on the main thread |
michael@0 | 168 | if (!NS_IsMainThread()) { |
michael@0 | 169 | NS_DispatchToMainThread( |
michael@0 | 170 | NS_NewRunnableMethod(this, &DNSRequestChild::StartRequest)); |
michael@0 | 171 | return; |
michael@0 | 172 | } |
michael@0 | 173 | |
michael@0 | 174 | // Send request to Parent process. |
michael@0 | 175 | gNeckoChild->SendPDNSRequestConstructor(this, mHost, mFlags); |
michael@0 | 176 | |
michael@0 | 177 | // IPDL holds a reference until IPDL channel gets destroyed |
michael@0 | 178 | AddIPDLReference(); |
michael@0 | 179 | } |
michael@0 | 180 | |
michael@0 | 181 | void |
michael@0 | 182 | DNSRequestChild::CallOnLookupComplete() |
michael@0 | 183 | { |
michael@0 | 184 | MOZ_ASSERT(mListener); |
michael@0 | 185 | |
michael@0 | 186 | mListener->OnLookupComplete(this, mResultRecord, mResultStatus); |
michael@0 | 187 | } |
michael@0 | 188 | |
michael@0 | 189 | bool |
michael@0 | 190 | DNSRequestChild::Recv__delete__(const DNSRequestResponse& reply) |
michael@0 | 191 | { |
michael@0 | 192 | MOZ_ASSERT(mListener); |
michael@0 | 193 | |
michael@0 | 194 | switch (reply.type()) { |
michael@0 | 195 | case DNSRequestResponse::TDNSRecord: { |
michael@0 | 196 | mResultRecord = new ChildDNSRecord(reply.get_DNSRecord(), mFlags); |
michael@0 | 197 | break; |
michael@0 | 198 | } |
michael@0 | 199 | case DNSRequestResponse::Tnsresult: { |
michael@0 | 200 | mResultStatus = reply.get_nsresult(); |
michael@0 | 201 | break; |
michael@0 | 202 | } |
michael@0 | 203 | default: |
michael@0 | 204 | NS_NOTREACHED("unknown type"); |
michael@0 | 205 | return false; |
michael@0 | 206 | } |
michael@0 | 207 | |
michael@0 | 208 | MOZ_ASSERT(NS_IsMainThread()); |
michael@0 | 209 | |
michael@0 | 210 | bool targetIsMain = false; |
michael@0 | 211 | if (!mTarget) { |
michael@0 | 212 | targetIsMain = true; |
michael@0 | 213 | } else { |
michael@0 | 214 | mTarget->IsOnCurrentThread(&targetIsMain); |
michael@0 | 215 | } |
michael@0 | 216 | |
michael@0 | 217 | if (targetIsMain) { |
michael@0 | 218 | CallOnLookupComplete(); |
michael@0 | 219 | } else { |
michael@0 | 220 | nsCOMPtr<nsIRunnable> event = |
michael@0 | 221 | NS_NewRunnableMethod(this, &DNSRequestChild::CallOnLookupComplete); |
michael@0 | 222 | mTarget->Dispatch(event, NS_DISPATCH_NORMAL); |
michael@0 | 223 | } |
michael@0 | 224 | |
michael@0 | 225 | return true; |
michael@0 | 226 | } |
michael@0 | 227 | |
michael@0 | 228 | //----------------------------------------------------------------------------- |
michael@0 | 229 | // DNSRequestChild::nsISupports |
michael@0 | 230 | //----------------------------------------------------------------------------- |
michael@0 | 231 | |
michael@0 | 232 | NS_IMPL_ISUPPORTS(DNSRequestChild, |
michael@0 | 233 | nsICancelable) |
michael@0 | 234 | |
michael@0 | 235 | //----------------------------------------------------------------------------- |
michael@0 | 236 | // DNSRequestChild::nsICancelable |
michael@0 | 237 | //----------------------------------------------------------------------------- |
michael@0 | 238 | |
michael@0 | 239 | NS_IMETHODIMP |
michael@0 | 240 | DNSRequestChild::Cancel(nsresult reason) |
michael@0 | 241 | { |
michael@0 | 242 | // for now Cancel is a no-op |
michael@0 | 243 | return NS_OK; |
michael@0 | 244 | } |
michael@0 | 245 | |
michael@0 | 246 | //------------------------------------------------------------------------------ |
michael@0 | 247 | }} // mozilla::net |