Back to index

lightning-sunbird  0.9+nobinonly
typeinfo.cpp
Go to the documentation of this file.
00001 #include <nsXPCOM.h>
00002 #include <nsDebug.h>
00003 #include <nsIInterfaceInfoManager.h>
00004 #include <nsCOMPtr.h>
00005 #include <xptinfo.h>
00006 #include <stdio.h>
00007 #include <nsString.h>
00008 #include <nsCRT.h>
00009 
00010 nsIInterfaceInfoManager* infomgr;
00011 
00012 extern "C" nsIEnumerator *
00013 typeinfo_EnumerateInterfacesStart()
00014 {
00015     nsIEnumerator *e;
00016     if (NS_FAILED(infomgr->EnumerateInterfaces(&e)) || NS_FAILED(e->First()))
00017         return 0;
00018 
00019     return e;
00020 }
00021 
00022 extern "C" char *
00023 typeinfo_EnumerateInterfacesNext(nsIEnumerator *e)
00024 {
00025     nsCOMPtr<nsISupports> isup;
00026     while (1) {
00027         if (e->IsDone() != NS_ENUMERATOR_FALSE ||
00028             NS_FAILED(e->CurrentItem(getter_AddRefs(isup))) || !isup) {
00029             break;
00030         }
00031             
00032         e->Next();
00033         nsCOMPtr<nsIInterfaceInfo> iface(do_QueryInterface(isup));
00034         if (!iface)
00035             break;
00036         
00037         PRBool scriptable;
00038         if (NS_SUCCEEDED(iface->IsScriptable(&scriptable)) &&
00039             !scriptable) {
00040             continue;
00041         }
00042         
00043         char *name;
00044         iface->GetName(&name);
00045         return name;
00046     }
00047 
00048     return 0;
00049 }
00050 
00051 extern "C" void
00052 typeinfo_EnumerateInterfacesStop(nsIEnumerator *e)
00053 {
00054     NS_IF_RELEASE(e);
00055 }
00056 
00057 extern "C" int
00058 typeinfo_GetParentInfo(const char *ifaceName,
00059                        char **parentName,
00060                        PRUint16 *parentMethodCount)
00061 {
00062     nsIInterfaceInfo *iinfo;
00063     nsresult res;
00064 
00065     res = infomgr->GetInfoForName(ifaceName, &iinfo);
00066     if (NS_FAILED(res))
00067         return res;
00068 
00069     nsCOMPtr<nsIInterfaceInfo> parent;
00070     res = iinfo->GetParent(getter_AddRefs(parent));
00071     if (NS_FAILED(res))
00072         return res;
00073 
00074     if (!parent) {
00075         *parentName = 0;
00076         *parentMethodCount = 0;
00077         return NS_OK;
00078     }
00079 
00080     res = parent->GetName(parentName);
00081     if (NS_FAILED(res))
00082         return res;
00083 
00084     res = parent->GetMethodCount(parentMethodCount);
00085     return res;
00086 }
00087 
00088 extern "C" int
00089 typeinfo_GetAllMethodData(const char *ifaceName,
00090                           const nsXPTMethodInfo ***infos,
00091                           PRUint16 *methodCount)
00092 {
00093     nsIInterfaceInfo *iinfo;
00094     nsresult res;
00095     
00096     res = infomgr->GetInfoForName(ifaceName, &iinfo);
00097     if (NS_FAILED(res))
00098         return res;
00099 
00100     res = iinfo->GetMethodCount(methodCount);
00101     if (NS_FAILED(res))
00102         return res;
00103 
00104     const nsXPTMethodInfo **infoarr = 
00105         new const nsXPTMethodInfo*[*methodCount];
00106 
00107     *infos = &infoarr[0];
00108 
00109     for (int i = 0; i < *methodCount; i++) {
00110         res = iinfo->GetMethodInfo(i, &infoarr[i]);
00111         if (NS_FAILED(res)) {
00112             fprintf(stderr, "Getting method info for %s:%d: %08x\n",
00113                     ifaceName, i, res);
00114         }
00115     }
00116 
00117     return NS_OK;
00118 }
00119 
00120 extern "C" int
00121 typeinfo_GetMethodData(const char *ifaceName, int methIndex,
00122                        const nsXPTMethodInfo **info)
00123 {
00124     nsIInterfaceInfo *iinfo;
00125     nsresult res;
00126     *info = 0;
00127 
00128     res = infomgr->GetInfoForName(ifaceName, &iinfo);
00129     if (NS_FAILED(res))
00130         return res;
00131 
00132     return iinfo->GetMethodInfo(methIndex, info);
00133 }
00134 
00135 extern "C" int
00136 typeinfo_GetMethodData_iid_index(const nsID *iid, int index,
00137                                  const nsXPTMethodInfo **info)
00138 {
00139     nsIInterfaceInfo *iinfo;
00140     nsresult res;
00141     *info = 0;
00142 
00143     res = infomgr->GetInfoForIID(iid, &iinfo);
00144     if (NS_FAILED(res))
00145         return res;
00146 
00147     return iinfo->GetMethodInfo(index, info);
00148 }
00149 
00150 extern "C" int
00151 typeinfo_GetMethodData_byname(const char *ifaceName, const char *methName,
00152                               PRUint16 *index, const nsXPTMethodInfo **info)
00153 {
00154     nsIInterfaceInfo *iinfo;
00155     nsresult res;
00156     *info = 0;
00157 
00158     res = infomgr->GetInfoForName(ifaceName, &iinfo);
00159     if (NS_FAILED(res))
00160         return res;
00161 
00162     return iinfo->GetMethodInfoForName(methName, index, info);
00163 }
00164 
00165 extern "C" int
00166 typeinfo_GetIIDForParam(const char *ifaceName, int methIndex,
00167                         int param, nsID *iid)
00168 {
00169     nsIInterfaceInfo *iinfo;
00170     nsresult res;
00171 
00172     // fprintf(stderr, "getting IID for %s:%d:%d\n", ifaceName, methIndex,
00173     //         param);
00174 
00175     res = infomgr->GetInfoForName(ifaceName, &iinfo);
00176     if (NS_FAILED(res)) {
00177         fprintf(stderr, "%d: FAILED (%08x)\n", __LINE__, res);
00178         return res;
00179     }
00180 
00181     const nsXPTMethodInfo *meth;
00182     res = iinfo->GetMethodInfo(methIndex, &meth);
00183     if (NS_FAILED(res)) {
00184         fprintf(stderr, "%d: FAILED (%08x)\n", __LINE__, res);
00185         return res;
00186     }
00187 
00188     const nsXPTParamInfo& paramInfo = meth->GetParam(param);
00189     res = iinfo->GetIIDForParamNoAlloc(methIndex, &paramInfo, iid);
00190 
00191     char *idstr = iid->ToString();
00192     // fprintf(stderr, "outgoing iid is %s\n", idstr);
00193     nsCRT::free(idstr);
00194     if (NS_FAILED(res)) {
00195         fprintf(stderr, "%d: FAILED (%08x)\n", __LINE__, res);
00196         return res;
00197     }
00198 
00199     return res;
00200 }
00201 
00202 extern "C" int
00203 typeinfo_GetNameForIID(nsID *iid, char **ifaceName)
00204 {
00205   *ifaceName = 0;
00206   return infomgr->GetNameForIID(iid, ifaceName);
00207 }
00208 
00209 extern "C" nsAString *
00210 typeinfo_WrapUnicode(const PRUnichar *chars, PRUint32 length)
00211 {
00212     return new nsString(chars, length);
00213 }
00214 
00215 extern "C" void
00216 typeinfo_FreeWrappedUnicode(nsAString *str)
00217 {
00218     delete str;
00219 }