TestStreamLoader.cpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. #include <stdio.h>
  2. #include "TestCommon.h"
  3. #include "nsNetUtil.h"
  4. #include "nsServiceManagerUtils.h"
  5. #include "nsThreadUtils.h"
  6. #include "NetwerkTestLogging.h"
  7. #include "mozilla/Attributes.h"
  8. #include "nsIScriptSecurityManager.h"
  9. //
  10. // set NSPR_LOG_MODULES=Test:5
  11. //
  12. static PRLogModuleInfo *gTestLog = nullptr;
  13. #define LOG(args) MOZ_LOG(gTestLog, mozilla::LogLevel::Debug, args)
  14. class MyStreamLoaderObserver final : public nsIStreamLoaderObserver
  15. {
  16. ~MyStreamLoaderObserver() = default;
  17. public:
  18. NS_DECL_ISUPPORTS
  19. NS_DECL_NSISTREAMLOADEROBSERVER
  20. };
  21. NS_IMPL_ISUPPORTS(MyStreamLoaderObserver, nsIStreamLoaderObserver)
  22. NS_IMETHODIMP
  23. MyStreamLoaderObserver::OnStreamComplete(nsIStreamLoader *loader,
  24. nsISupports *ctxt,
  25. nsresult status,
  26. uint32_t resultLen,
  27. const uint8_t *result)
  28. {
  29. LOG(("OnStreamComplete [status=%x resultLen=%u]\n", status, resultLen));
  30. nsCOMPtr<nsIRequest> request;
  31. loader->GetRequest(getter_AddRefs(request));
  32. LOG((" request=%p\n", request.get()));
  33. QuitPumpingEvents();
  34. return NS_OK;
  35. }
  36. int main(int argc, char **argv)
  37. {
  38. if (test_common_init(&argc, &argv) != 0)
  39. return -1;
  40. if (argc < 2) {
  41. printf("usage: %s <url>\n", argv[0]);
  42. return -1;
  43. }
  44. gTestLog = PR_NewLogModule("Test");
  45. nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
  46. if (NS_FAILED(rv))
  47. return -1;
  48. {
  49. nsCOMPtr<nsIURI> uri;
  50. rv = NS_NewURI(getter_AddRefs(uri), nsDependentCString(argv[1]));
  51. if (NS_FAILED(rv))
  52. return -1;
  53. nsCOMPtr<nsIScriptSecurityManager> secman =
  54. do_GetService(NS_SCRIPTSECURITYMANAGER_CONTRACTID, &rv);
  55. NS_ENSURE_SUCCESS(rv, -1);
  56. nsCOMPtr<nsIPrincipal> systemPrincipal;
  57. rv = secman->GetSystemPrincipal(getter_AddRefs(systemPrincipal));
  58. NS_ENSURE_SUCCESS(rv, -1);
  59. nsCOMPtr<nsIChannel> chan;
  60. rv = NS_NewChannel(getter_AddRefs(chan),
  61. uri,
  62. systemPrincipal,
  63. nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_INHERITS,
  64. nsIContentPolicy::TYPE_OTHER);
  65. if (NS_FAILED(rv))
  66. return -1;
  67. nsCOMPtr<nsIStreamLoaderObserver> observer = new MyStreamLoaderObserver();
  68. if (!observer)
  69. return -1;
  70. nsCOMPtr<nsIStreamLoader> loader;
  71. rv = NS_NewStreamLoader(getter_AddRefs(loader), observer);
  72. if (NS_FAILED(rv))
  73. return -1;
  74. rv = chan->AsyncOpen2(loader);
  75. if (NS_FAILED(rv))
  76. return -1;
  77. PumpEvents();
  78. } // this scopes the nsCOMPtrs
  79. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  80. NS_ShutdownXPCOM(nullptr);
  81. return 0;
  82. }