TestIncrementalDownload.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* vim:set ts=2 sw=2 sts=2 cin et: */
  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. #include <inttypes.h>
  7. #include <stdlib.h>
  8. #include "TestCommon.h"
  9. #include "nsNetUtil.h"
  10. #include "nsComponentManagerUtils.h"
  11. #include "nsIIncrementalDownload.h"
  12. #include "nsIRequestObserver.h"
  13. #include "nsIProgressEventSink.h"
  14. #include "nsThreadUtils.h"
  15. #include "nsAutoPtr.h"
  16. #include "prprf.h"
  17. #include "prenv.h"
  18. #include "mozilla/Attributes.h"
  19. //-----------------------------------------------------------------------------
  20. class FetchObserver final : public nsIRequestObserver
  21. , public nsIProgressEventSink
  22. {
  23. ~FetchObserver() = default;
  24. public:
  25. NS_DECL_ISUPPORTS
  26. NS_DECL_NSIREQUESTOBSERVER
  27. NS_DECL_NSIPROGRESSEVENTSINK
  28. };
  29. NS_IMPL_ISUPPORTS(FetchObserver, nsIRequestObserver,
  30. nsIProgressEventSink)
  31. NS_IMETHODIMP
  32. FetchObserver::OnStartRequest(nsIRequest *request, nsISupports *context)
  33. {
  34. printf("FetchObserver::OnStartRequest\n");
  35. return NS_OK;
  36. }
  37. NS_IMETHODIMP
  38. FetchObserver::OnProgress(nsIRequest *request, nsISupports *context,
  39. int64_t progress, int64_t progressMax)
  40. {
  41. printf("FetchObserver::OnProgress [%" PRId64 "/%" PRId64 "]\n",
  42. progress, progressMax);
  43. return NS_OK;
  44. }
  45. NS_IMETHODIMP
  46. FetchObserver::OnStatus(nsIRequest *request, nsISupports *context,
  47. nsresult status, const char16_t *statusText)
  48. {
  49. return NS_OK;
  50. }
  51. NS_IMETHODIMP
  52. FetchObserver::OnStopRequest(nsIRequest *request, nsISupports *context,
  53. nsresult status)
  54. {
  55. printf("FetchObserver::OnStopRequest [status=%x]\n",
  56. static_cast<uint32_t>(status));
  57. QuitPumpingEvents();
  58. return NS_OK;
  59. }
  60. //-----------------------------------------------------------------------------
  61. static nsresult
  62. DoIncrementalFetch(const char *uriSpec, const char *resultPath, int32_t chunkSize,
  63. int32_t interval)
  64. {
  65. nsCOMPtr<nsIFile> resultFile;
  66. nsresult rv = NS_NewNativeLocalFile(nsDependentCString(resultPath),
  67. false, getter_AddRefs(resultFile));
  68. if (NS_FAILED(rv))
  69. return rv;
  70. nsCOMPtr<nsIURI> uri;
  71. rv = NS_NewURI(getter_AddRefs(uri), uriSpec);
  72. if (NS_FAILED(rv))
  73. return rv;
  74. nsCOMPtr<nsIRequestObserver> observer = new FetchObserver();
  75. if (!observer)
  76. return NS_ERROR_OUT_OF_MEMORY;
  77. nsCOMPtr<nsIIncrementalDownload> download =
  78. do_CreateInstance(NS_INCREMENTALDOWNLOAD_CONTRACTID, &rv);
  79. if (NS_FAILED(rv))
  80. return rv;
  81. rv = download->Init(uri, resultFile, chunkSize, interval);
  82. if (NS_FAILED(rv))
  83. return rv;
  84. rv = download->Start(observer, nullptr);
  85. if (NS_FAILED(rv))
  86. return rv;
  87. PumpEvents();
  88. return NS_OK;
  89. }
  90. int
  91. main(int argc, char **argv)
  92. {
  93. if (PR_GetEnv("MOZ_BREAK_ON_MAIN"))
  94. NS_BREAK();
  95. if (argc < 5) {
  96. fprintf(stderr, "USAGE: TestIncrementalDownload <url> <file> <chunksize> <interval-in-seconds>\n");
  97. return -1;
  98. }
  99. nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
  100. if (NS_FAILED(rv))
  101. return -1;
  102. int32_t chunkSize = atoi(argv[3]);
  103. int32_t interval = atoi(argv[4]);
  104. rv = DoIncrementalFetch(argv[1], argv[2], chunkSize, interval);
  105. if (NS_FAILED(rv))
  106. fprintf(stderr, "ERROR: DoIncrementalFetch failed [%x]\n",
  107. static_cast<uint32_t>(rv));
  108. NS_ShutdownXPCOM(nullptr);
  109. return 0;
  110. }