michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include michael@0: #include "plstr.h" michael@0: #include "prlink.h" michael@0: #include "nsIComponentRegistrar.h" michael@0: #include "nsIComponentManager.h" michael@0: #include "nsIServiceManager.h" michael@0: #include "nsIFile.h" michael@0: #include "nsCOMPtr.h" michael@0: #include "nsString.h" michael@0: michael@0: static bool gUnreg = false; michael@0: michael@0: void print_err(nsresult err) michael@0: { michael@0: switch (err) { michael@0: case NS_ERROR_FACTORY_NOT_LOADED: michael@0: cerr << "Factory not loaded"; michael@0: break; michael@0: case NS_NOINTERFACE: michael@0: cerr << "No Interface"; michael@0: break; michael@0: case NS_ERROR_NULL_POINTER: michael@0: cerr << "Null pointer"; michael@0: break; michael@0: case NS_ERROR_OUT_OF_MEMORY: michael@0: cerr << "Out of memory"; michael@0: break; michael@0: default: michael@0: cerr << hex << err << dec; michael@0: } michael@0: } michael@0: michael@0: nsresult Register(nsIComponentRegistrar* registrar, const char *path) michael@0: { michael@0: nsCOMPtr file; michael@0: nsresult rv = michael@0: NS_NewLocalFile( michael@0: NS_ConvertUTF8toUTF16(path), michael@0: true, michael@0: getter_AddRefs(file)); michael@0: if (NS_FAILED(rv)) return rv; michael@0: rv = registrar->AutoRegister(file); michael@0: return rv; michael@0: } michael@0: michael@0: nsresult Unregister(const char *path) michael@0: { michael@0: /* NEEDS IMPLEMENTATION */ michael@0: #if 0 michael@0: nsresult res = nsComponentManager::AutoUnregisterComponent(path); michael@0: return res; michael@0: #else michael@0: return NS_ERROR_FAILURE; michael@0: #endif michael@0: } michael@0: michael@0: int ProcessArgs(nsIComponentRegistrar* registrar, int argc, char *argv[]) michael@0: { michael@0: int i = 1; michael@0: nsresult res; michael@0: michael@0: while (i < argc) { michael@0: if (argv[i][0] == '-') { michael@0: int j; michael@0: for (j = 1; argv[i][j] != '\0'; j++) { michael@0: switch (argv[i][j]) { michael@0: case 'u': michael@0: gUnreg = true; michael@0: break; michael@0: default: michael@0: cerr << "Unknown option '" << argv[i][j] << "'\n"; michael@0: } michael@0: } michael@0: i++; michael@0: } else { michael@0: if (gUnreg) { michael@0: res = Unregister(argv[i]); michael@0: if (NS_SUCCEEDED(res)) { michael@0: cout << "Successfully unregistered: " << argv[i] << "\n"; michael@0: } else { michael@0: cerr << "Unregister failed ("; michael@0: print_err(res); michael@0: cerr << "): " << argv[i] << "\n"; michael@0: } michael@0: } else { michael@0: res = Register(registrar, argv[i]); michael@0: if (NS_SUCCEEDED(res)) { michael@0: cout << "Successfully registered: " << argv[i] << "\n"; michael@0: } else { michael@0: cerr << "Register failed ("; michael@0: print_err(res); michael@0: cerr << "): " << argv[i] << "\n"; michael@0: } michael@0: } michael@0: i++; michael@0: } michael@0: } michael@0: return 0; michael@0: } michael@0: michael@0: int main(int argc, char *argv[]) michael@0: { michael@0: int ret = 0; michael@0: nsresult rv; michael@0: { michael@0: nsCOMPtr servMan; michael@0: rv = NS_InitXPCOM2(getter_AddRefs(servMan), nullptr, nullptr); michael@0: if (NS_FAILED(rv)) return -1; michael@0: nsCOMPtr registrar = do_QueryInterface(servMan); michael@0: NS_ASSERTION(registrar, "Null nsIComponentRegistrar"); michael@0: michael@0: /* With no arguments, RegFactory will autoregister */ michael@0: if (argc <= 1) michael@0: { michael@0: rv = registrar->AutoRegister(nullptr); michael@0: ret = (NS_FAILED(rv)) ? -1 : 0; michael@0: } michael@0: else michael@0: ret = ProcessArgs(registrar, argc, argv); michael@0: } // this scopes the nsCOMPtrs michael@0: // no nsCOMPtrs are allowed to be alive when you call NS_ShutdownXPCOM michael@0: rv = NS_ShutdownXPCOM(nullptr); michael@0: NS_ASSERTION(NS_SUCCEEDED(rv), "NS_ShutdownXPCOM failed"); michael@0: return ret; michael@0: }