RegFactory.cpp 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 <iostream.h>
  6. #include "plstr.h"
  7. #include "prlink.h"
  8. #include "nsIComponentRegistrar.h"
  9. #include "nsIComponentManager.h"
  10. #include "nsIServiceManager.h"
  11. #include "nsIFile.h"
  12. #include "nsCOMPtr.h"
  13. #include "nsString.h"
  14. static bool gUnreg = false;
  15. void print_err(nsresult err)
  16. {
  17. switch (err) {
  18. case NS_ERROR_FACTORY_NOT_LOADED:
  19. cerr << "Factory not loaded";
  20. break;
  21. case NS_NOINTERFACE:
  22. cerr << "No Interface";
  23. break;
  24. case NS_ERROR_NULL_POINTER:
  25. cerr << "Null pointer";
  26. break;
  27. case NS_ERROR_OUT_OF_MEMORY:
  28. cerr << "Out of memory";
  29. break;
  30. default:
  31. cerr << hex << err << dec;
  32. }
  33. }
  34. nsresult Register(nsIComponentRegistrar* registrar, const char *path)
  35. {
  36. nsCOMPtr<nsIFile> file;
  37. nsresult rv =
  38. NS_NewLocalFile(
  39. NS_ConvertUTF8toUTF16(path),
  40. true,
  41. getter_AddRefs(file));
  42. if (NS_FAILED(rv)) return rv;
  43. rv = registrar->AutoRegister(file);
  44. return rv;
  45. }
  46. nsresult Unregister(const char *path)
  47. {
  48. /* NEEDS IMPLEMENTATION */
  49. #if 0
  50. nsresult res = nsComponentManager::AutoUnregisterComponent(path);
  51. return res;
  52. #else
  53. return NS_ERROR_FAILURE;
  54. #endif
  55. }
  56. int ProcessArgs(nsIComponentRegistrar* registrar, int argc, char *argv[])
  57. {
  58. int i = 1;
  59. nsresult res;
  60. while (i < argc) {
  61. if (argv[i][0] == '-') {
  62. int j;
  63. for (j = 1; argv[i][j] != '\0'; j++) {
  64. switch (argv[i][j]) {
  65. case 'u':
  66. gUnreg = true;
  67. break;
  68. default:
  69. cerr << "Unknown option '" << argv[i][j] << "'\n";
  70. }
  71. }
  72. i++;
  73. } else {
  74. if (gUnreg) {
  75. res = Unregister(argv[i]);
  76. if (NS_SUCCEEDED(res)) {
  77. cout << "Successfully unregistered: " << argv[i] << "\n";
  78. } else {
  79. cerr << "Unregister failed (";
  80. print_err(res);
  81. cerr << "): " << argv[i] << "\n";
  82. }
  83. } else {
  84. res = Register(registrar, argv[i]);
  85. if (NS_SUCCEEDED(res)) {
  86. cout << "Successfully registered: " << argv[i] << "\n";
  87. } else {
  88. cerr << "Register failed (";
  89. print_err(res);
  90. cerr << "): " << argv[i] << "\n";
  91. }
  92. }
  93. i++;
  94. }
  95. }
  96. return 0;
  97. }
  98. int main(int argc, char *argv[])
  99. {
  100. int ret = 0;
  101. nsresult rv;
  102. {
  103. nsCOMPtr<nsIServiceManager> servMan;
  104. rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr);
  105. if (NS_FAILED(rv)) return -1;
  106. nsCOMPtr<nsIComponentRegistrar> registrar = do_QueryInterface(servMan);
  107. NS_ASSERTION(registrar, "Null nsIComponentRegistrar");
  108. /* With no arguments, RegFactory will autoregister */
  109. if (argc <= 1)
  110. {
  111. rv = registrar->AutoRegister(nullptr);
  112. ret = (NS_FAILED(rv)) ? -1 : 0;
  113. }
  114. else
  115. ret = ProcessArgs(registrar, argc, argv);
  116. } // this scopes the nsCOMPtrs
  117. // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM
  118. rv = NS_ShutdownXPCOM(nullptr);
  119. NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed");
  120. return ret;
  121. }