xpcom/tests/RegFactory.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial