00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00040 #include "TclXPCOMPrivate.h"
00041 #include "nsXPCOM.h"
00042 #include "nsILocalFile.h"
00043 #include "nsIDirectoryService.h"
00044 #include "nsIThread.h"
00045 #include "nsString.h"
00046
00047
00048 nsIServiceManager *servmanager;
00049 nsIInterfaceInfoManager *manager;
00050 nsIComponentRegistrar *registrar;
00051 nsIMemory *memmgr;
00052
00053 nsIID nsisupports_iid = NS_ISUPPORTS_IID;
00054 nsIID nsiclassinfo_iid = NS_ICLASSINFO_IID;
00055 nsIID nsiservicemanager_iid = NS_ISERVICEMANAGER_IID;
00056
00057 nsIInterfaceInfo *nsisupports_info;
00058 nsIInterfaceInfo *nsiservicemanager_info;
00059 nsIInterfaceInfo *nsiclassinfo_info;
00060
00061 Tcl_Encoding ascii_enc;
00062
00063 static int TclXPCOMInitialized = 0;
00064
00065
00073 int
00074 Tclxpcom_SafeInit(
00075 Tcl_Interp *interp
00076 )
00077 {
00078 NS_PRECONDITION(interp, "null pointer");
00079
00080 return Tclxpcom_Init(interp);
00081 }
00082
00083
00094 int
00095 Tclxpcom_Init(
00096 Tcl_Interp *interp
00097 )
00098 {
00099 int rv;
00100 nsresult res;
00101 Tcl_Obj *mozhome;
00102 nsILocalFile *mozBinDirectory;
00103 nsIDirectoryServiceProvider *appFileLocProvider;
00104 nsIThread *mainthread;
00105
00106 NS_PRECONDITION(interp, "null pointer");
00107
00108 if (Tcl_InitStubs(interp, "8.3", 0) == NULL)
00109 {
00110 return TCL_ERROR;
00111 }
00112
00113 rv = Tcl_PkgProvide(interp, "tclxpcom", "0.1");
00114 if (rv != TCL_OK)
00115 {
00116 return rv;
00117 }
00118
00119 if (!TclXPCOMInitialized)
00120 {
00121 ascii_enc = Tcl_GetEncoding(interp, "ascii");
00122 if (!ascii_enc)
00123 {
00124 return TCL_ERROR;
00125 }
00126
00127 res = nsIThread::GetMainThread(&mainthread);
00128 if (NS_FAILED(res))
00129 {
00130 mozhome = Tcl_GetVar2Ex(interp, TCLXPCOM_MOZHOMEVARNAME, NULL, TCL_GLOBAL_ONLY);
00131 if (mozhome)
00132 {
00133 res = NS_NewLocalFile(nsString(Tcl_GetUnicode(mozhome), Tcl_GetCharLength(mozhome)), false, &mozBinDirectory);
00134 if (NS_FAILED(res))
00135 {
00136 Tcl_ResetResult(interp);
00137 Tcl_AppendResult(interp, "invalid path \"", Tcl_GetString(mozhome), "\" for mozilla home", NULL);
00138 return TCL_ERROR;
00139 }
00140 } else
00141 {
00142 mozBinDirectory = nsnull;
00143 }
00144
00145 appFileLocProvider = nsnull;
00146
00147 NS_InitXPCOM2(nsnull, mozBinDirectory, appFileLocProvider);
00148
00149 if (mozhome)
00150 {
00151 NS_RELEASE(mozBinDirectory);
00152 }
00153 } else
00154 {
00155 NS_RELEASE(mainthread);
00156 }
00157
00158 res = NS_GetServiceManager(&servmanager);
00159 if (NS_FAILED(res))
00160 {
00161 Tcl_ResetResult(interp);
00162 Tcl_AppendResult(interp, "unable to get service manager", NULL);
00163 return TCL_ERROR;
00164 }
00165
00166 res = NS_GetComponentRegistrar(®istrar);
00167 if (NS_FAILED(res))
00168 {
00169 Tcl_ResetResult(interp);
00170 Tcl_AppendResult(interp, "unable to get component registrar", NULL);
00171 return TCL_ERROR;
00172 }
00173
00174 res = NS_GetMemoryManager(&memmgr);
00175 if (NS_FAILED(res))
00176 {
00177 Tcl_ResetResult(interp);
00178 Tcl_AppendResult(interp, "unable to get memory manager", NULL);
00179 return TCL_ERROR;
00180 }
00181
00182 manager = XPTI_GetInterfaceInfoManager();
00183 if (!manager)
00184 {
00185 Tcl_ResetResult(interp);
00186 Tcl_AppendResult(interp, "unable to get interfaceinfo manager", NULL);
00187 return TCL_ERROR;
00188 }
00189
00190 res = manager->GetInfoForIID(&nsisupports_iid, &nsisupports_info);
00191 if (NS_FAILED(res))
00192 {
00193 Tcl_ResetResult(interp);
00194 Tcl_AppendResult(interp, "unable to get nsISupports interfaceinfo", NULL);
00195 return TCL_ERROR;
00196 }
00197
00198 res = manager->GetInfoForIID(&nsiservicemanager_iid, &nsiservicemanager_info);
00199 if (NS_FAILED(res))
00200 {
00201 Tcl_ResetResult(interp);
00202 Tcl_AppendResult(interp, "unable to get nsIServiceManager interfaceinfo", NULL);
00203 return TCL_ERROR;
00204 }
00205
00206 res = manager->GetInfoForIID(&nsiclassinfo_iid, &nsiclassinfo_info);
00207 if (NS_FAILED(res))
00208 {
00209 Tcl_ResetResult(interp);
00210 Tcl_AppendResult(interp, "unable to get nsIClassInfo interfaceinfo", NULL);
00211 return TCL_ERROR;
00212 }
00213
00214 TclXPCOM_RegisterObjTypes();
00215 TclXPCOM_InitInterfaceInfo();
00216 TclXPCOM_InitClassInfo();
00217 TclXPCOM_InitInterfaceRef();
00218
00219 TclXPCOMInitialized = 1;
00220 }
00221
00222 TclXPCOM_RegisterCommands(interp);
00223
00224 return TCL_OK;
00225 }