DNSRequestParent.cpp 3.6 KB

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