nsDNSPrefetch.cpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  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 "nsDNSPrefetch.h"
  6. #include "nsCOMPtr.h"
  7. #include "nsString.h"
  8. #include "nsThreadUtils.h"
  9. #include "nsIDNSListener.h"
  10. #include "nsIDNSService.h"
  11. #include "nsICancelable.h"
  12. #include "nsIURI.h"
  13. static nsIDNSService *sDNSService = nullptr;
  14. nsresult
  15. nsDNSPrefetch::Initialize(nsIDNSService *aDNSService)
  16. {
  17. NS_IF_RELEASE(sDNSService);
  18. sDNSService = aDNSService;
  19. NS_IF_ADDREF(sDNSService);
  20. return NS_OK;
  21. }
  22. nsresult
  23. nsDNSPrefetch::Shutdown()
  24. {
  25. NS_IF_RELEASE(sDNSService);
  26. return NS_OK;
  27. }
  28. nsDNSPrefetch::nsDNSPrefetch(nsIURI *aURI,
  29. nsIDNSListener *aListener,
  30. bool storeTiming)
  31. : mStoreTiming(storeTiming)
  32. , mListener(do_GetWeakReference(aListener))
  33. {
  34. aURI->GetAsciiHost(mHostname);
  35. }
  36. nsresult
  37. nsDNSPrefetch::Prefetch(uint16_t flags)
  38. {
  39. if (mHostname.IsEmpty())
  40. return NS_ERROR_NOT_AVAILABLE;
  41. if (!sDNSService)
  42. return NS_ERROR_NOT_AVAILABLE;
  43. nsCOMPtr<nsICancelable> tmpOutstanding;
  44. if (mStoreTiming)
  45. mStartTimestamp = mozilla::TimeStamp::Now();
  46. // If AsyncResolve fails, for example because prefetching is disabled,
  47. // then our timing will be useless. However, in such a case,
  48. // mEndTimestamp will be a null timestamp and callers should check
  49. // TimingsValid() before using the timing.
  50. nsCOMPtr<nsIThread> mainThread = do_GetMainThread();
  51. return sDNSService->AsyncResolve(mHostname,
  52. flags | nsIDNSService::RESOLVE_SPECULATE,
  53. this, mainThread,
  54. getter_AddRefs(tmpOutstanding));
  55. }
  56. nsresult
  57. nsDNSPrefetch::PrefetchLow(bool refreshDNS)
  58. {
  59. return Prefetch(nsIDNSService::RESOLVE_PRIORITY_LOW |
  60. (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
  61. }
  62. nsresult
  63. nsDNSPrefetch::PrefetchMedium(bool refreshDNS)
  64. {
  65. return Prefetch(nsIDNSService::RESOLVE_PRIORITY_MEDIUM |
  66. (refreshDNS ? nsIDNSService::RESOLVE_BYPASS_CACHE : 0));
  67. }
  68. nsresult
  69. nsDNSPrefetch::PrefetchHigh(bool refreshDNS)
  70. {
  71. return Prefetch(refreshDNS ?
  72. nsIDNSService::RESOLVE_BYPASS_CACHE : 0);
  73. }
  74. NS_IMPL_ISUPPORTS(nsDNSPrefetch, nsIDNSListener)
  75. NS_IMETHODIMP
  76. nsDNSPrefetch::OnLookupComplete(nsICancelable *request,
  77. nsIDNSRecord *rec,
  78. nsresult status)
  79. {
  80. MOZ_ASSERT(NS_IsMainThread(), "Expecting DNS callback on main thread.");
  81. if (mStoreTiming) {
  82. mEndTimestamp = mozilla::TimeStamp::Now();
  83. }
  84. nsCOMPtr<nsIDNSListener> listener = do_QueryReferent(mListener);
  85. if (listener) {
  86. listener->OnLookupComplete(request, rec, status);
  87. }
  88. return NS_OK;
  89. }