TestMakeAbs.cpp 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. /* -*- Mode: C++; tab-width: 2; 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 "nspr.h"
  6. #include "nscore.h"
  7. #include "nsCOMPtr.h"
  8. #include "nsIIOService.h"
  9. #include "nsIServiceManager.h"
  10. #include "nsIComponentRegistrar.h"
  11. #include "nsIURI.h"
  12. static NS_DEFINE_CID(kIOServiceCID, NS_IOSERVICE_CID);
  13. nsresult ServiceMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) {
  14. nsresult rv;
  15. nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
  16. if (NS_FAILED(rv)) return rv;
  17. return serv->MakeAbsolute(relativeInfo, baseURI, _retval);
  18. }
  19. nsresult URLMakeAbsolute(nsIURI *baseURI, char *relativeInfo, char **_retval) {
  20. return baseURI->MakeAbsolute(relativeInfo, _retval);
  21. }
  22. int
  23. main(int argc, char* argv[])
  24. {
  25. nsresult rv = NS_OK;
  26. if (argc < 4) {
  27. printf("usage: %s int (loop count) baseURL relativeSpec\n", argv[0]);
  28. return -1;
  29. }
  30. uint32_t cycles = atoi(argv[1]);
  31. char *base = argv[2];
  32. char *rel = argv[3];
  33. {
  34. nsCOMPtr<nsIServiceManager> servMan;
  35. NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  36. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  37. NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  38. if (registrar)
  39. registrar->AutoRegister(nullptr);
  40. nsCOMPtr<nsIIOService> serv(do_GetService(kIOServiceCID, &rv));
  41. if (NS_FAILED(rv)) return rv;
  42. nsCOMPtr<nsIURI> uri;
  43. rv = serv->NewURI(base, nullptr, getter_AddRefs(uri));
  44. if (NS_FAILED(rv)) return rv;
  45. char *absURLString;
  46. uint32_t i = 0;
  47. while (i++ < cycles) {
  48. rv = ServiceMakeAbsolute(uri, rel, &absURLString);
  49. if (NS_FAILED(rv)) return rv;
  50. free(absURLString);
  51. rv = URLMakeAbsolute(uri, rel, &absURLString);
  52. free(absURLString);
  53. }
  54. } // this scopes the nsCOMPtrs
  55. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  56. NS_ShutdownXPCOM(nullptr);
  57. return rv;
  58. }