nsIGenericFactory.h 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
  2. /* ***** BEGIN LICENSE BLOCK *****
  3. * Version: MPL 1.1/GPL 2.0/LGPL 2.1
  4. *
  5. * The contents of this file are subject to the Mozilla Public License Version
  6. * 1.1 (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. * http://www.mozilla.org/MPL/
  9. *
  10. * Software distributed under the License is distributed on an "AS IS" basis,
  11. * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  12. * for the specific language governing rights and limitations under the
  13. * License.
  14. *
  15. * The Original Code is mozilla.org code.
  16. *
  17. * The Initial Developer of the Original Code is
  18. * Netscape Communications Corporation.
  19. * Portions created by the Initial Developer are Copyright (C) 1998
  20. * the Initial Developer. All Rights Reserved.
  21. *
  22. * Contributor(s):
  23. * Benjamin Smedberg <[email protected]>
  24. *
  25. * Alternatively, the contents of this file may be used under the terms of
  26. * either of the GNU General Public License Version 2 or later (the "GPL"),
  27. * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
  28. * in which case the provisions of the GPL or the LGPL are applicable instead
  29. * of those above. If you wish to allow use of your version of this file only
  30. * under the terms of either the GPL or the LGPL, and not to allow others to
  31. * use your version of this file under the terms of the MPL, indicate your
  32. * decision by deleting the provisions above and replace them with the notice
  33. * and other provisions required by the GPL or the LGPL. If you do not delete
  34. * the provisions above, a recipient may use your version of this file under
  35. * the terms of any one of the MPL, the GPL or the LGPL.
  36. *
  37. * ***** END LICENSE BLOCK ***** */
  38. #ifndef nsIGenericFactory_h___
  39. #define nsIGenericFactory_h___
  40. #include "nsIFactory.h"
  41. #include "nsIModule.h"
  42. #include "nsIClassInfo.h"
  43. class nsIFile;
  44. class nsIComponentManager;
  45. // {3bc97f01-ccdf-11d2-bab8-b548654461fc}
  46. #define NS_GENERICFACTORY_CID \
  47. { 0x3bc97f01, 0xccdf, 0x11d2, \
  48. { 0xba, 0xb8, 0xb5, 0x48, 0x65, 0x44, 0x61, 0xfc } }
  49. // {3bc97f00-ccdf-11d2-bab8-b548654461fc}
  50. #define NS_IGENERICFACTORY_IID \
  51. { 0x3bc97f00, 0xccdf, 0x11d2, \
  52. { 0xba, 0xb8, 0xb5, 0x48, 0x65, 0x44, 0x61, 0xfc } }
  53. #define NS_GENERICFACTORY_CONTRACTID "@mozilla.org/generic-factory;1"
  54. #define NS_GENERICFACTORY_CLASSNAME "Generic Factory"
  55. struct nsModuleComponentInfo; // forward declaration
  56. /**
  57. * Provides a Generic nsIFactory implementation that can be used by
  58. * DLLs with very simple factory needs.
  59. */
  60. class nsIGenericFactory : public nsIFactory {
  61. public:
  62. NS_DEFINE_STATIC_IID_ACCESSOR(NS_IGENERICFACTORY_IID)
  63. NS_IMETHOD SetComponentInfo(const nsModuleComponentInfo *info) = 0;
  64. NS_IMETHOD GetComponentInfo(const nsModuleComponentInfo **infop) = 0;
  65. };
  66. NS_COM_GLUE nsresult
  67. NS_NewGenericFactory(nsIGenericFactory **result,
  68. const nsModuleComponentInfo *info);
  69. /** Component Callbacks **/
  70. /**
  71. * NSConstructorProcPtr
  72. *
  73. * This function will be used by the generic factory to create an
  74. * instance of the given CID.
  75. *
  76. * @param aOuter : Pointer to a component that wishes to be aggregated
  77. * in the resulting instance. This will be nsnull if no
  78. * aggregation is requested.
  79. * @param iid : The IID of the interface being requested in
  80. * the component which is being currently created.
  81. * @param result : [out] Pointer to the newly created instance, if successful.
  82. *
  83. * @return NS_OK Component successfully created and the interface
  84. * being requested was successfully returned in result.
  85. * NS_NOINTERFACE Interface not accessible.
  86. * NS_ERROR_NO_AGGREGATION if an 'outer' object is supplied, but the
  87. * component is not aggregatable.
  88. * NS_ERROR* Method failure.
  89. **/
  90. typedef NS_CALLBACK(NSConstructorProcPtr)(nsISupports *aOuter,
  91. REFNSIID aIID,
  92. void **aResult);
  93. /**
  94. * NSRegisterSelfProcPtr
  95. *
  96. * One time registration call back. Allows you to perform registration
  97. * specific activity like adding yourself to a category.
  98. *
  99. * @param aCompMgr : The global component manager
  100. * @param aFile : Component File. This file must have an associated
  101. * loader and export the required symbols which this
  102. * loader defines.
  103. * @param aLoaderStr : Opaque loader specific string. This value is
  104. * passed into the nsIModule's registerSelf
  105. * callback and must be fowarded unmodified when
  106. * registering factories via their location.
  107. * @param aType : Component Type of CID aClass. This value is
  108. * passed into the nsIModule's registerSelf
  109. * callback and must be fowarded unmodified when
  110. * registering factories via their location.
  111. * @param aInfo : Pointer to array of nsModuleComponentInfo
  112. *
  113. * @param aInfo
  114. * @return NS_OK Registration was successful.
  115. * NS_ERROR* Method failure.
  116. **/
  117. typedef NS_CALLBACK(NSRegisterSelfProcPtr)(nsIComponentManager *aCompMgr,
  118. nsIFile *aPath,
  119. const char *aLoaderStr,
  120. const char *aType,
  121. const nsModuleComponentInfo *aInfo);
  122. /**
  123. * NSUnregisterSelfProcPtr
  124. *
  125. * One time unregistration call back. Allows you to perform unregistration
  126. * specific activity like removing yourself from a category.
  127. *
  128. * @param aCompMgr : The global component manager
  129. * @param aFile : Component File. This file must have an associated
  130. * loader and export the required symbols which this
  131. * loader defines.
  132. * @param aLoaderStr : Opaque loader specific string. This value is
  133. * passed into the nsIModule's registerSelf
  134. * callback and must be fowarded unmodified when
  135. * registering factories via their location
  136. * @param aInfo : Pointer to array of nsModuleComponentInfo
  137. *
  138. * @param aInfo
  139. * @return NS_OK Registration was successful.
  140. * NS_ERROR* Method failure.
  141. **/
  142. typedef NS_CALLBACK(NSUnregisterSelfProcPtr)(nsIComponentManager *aCompMgr,
  143. nsIFile *aPath,
  144. const char *aLoaderStr,
  145. const nsModuleComponentInfo *aInfo);
  146. /**
  147. * NSFactoryDestructorProcPtr
  148. *
  149. * This function will be called when the factory is being destroyed.
  150. *
  151. **/
  152. typedef NS_CALLBACK(NSFactoryDestructorProcPtr)(void);
  153. /**
  154. * NSGetInterfacesProcPtr
  155. *
  156. * This function is used to implement class info.
  157. *
  158. * Get an ordered list of the interface ids that instances of the class
  159. * promise to implement. Note that nsISupports is an implicit member
  160. * of any such list and need not be included.
  161. *
  162. * Should set *count = 0 and *array = null and return NS_OK if getting the
  163. * list is not supported.
  164. *
  165. * @see nsIClassInfo.idl
  166. **/
  167. typedef NS_CALLBACK(NSGetInterfacesProcPtr)(PRUint32 *countp,
  168. nsIID* **array);
  169. /**
  170. * NSGetLanguageHelperProcPtr
  171. *
  172. * This function is used to implement class info.
  173. *
  174. * Get a language mapping specific helper object that may assist in using
  175. * objects of this class in a specific lanaguage. For instance, if asked
  176. * for the helper for nsIProgrammingLanguage::JAVASCRIPT this might return
  177. * an object that can be QI'd into the nsIXPCScriptable interface to assist
  178. * XPConnect in supplying JavaScript specific behavior to callers of the
  179. * instance object.
  180. *
  181. * @see: nsIClassInfo.idl, nsIProgrammingLanguage.idl
  182. *
  183. * Should return null if no helper available for given language.
  184. **/
  185. typedef NS_CALLBACK(NSGetLanguageHelperProcPtr)(PRUint32 language,
  186. nsISupports **helper);
  187. /**
  188. * nsModuleComponentInfo
  189. *
  190. * Use this type to define a list of module component info to pass to
  191. * NS_NewGenericModule.
  192. *
  193. * @param mDescription : Class Name of given object
  194. * @param mCID : CID of given object
  195. * @param mContractID : Contract ID of given object
  196. * @param mConstructor : Constructor of given object
  197. * @param mRegisterSelfProc : (optional) Registration Callback
  198. * @param mUnregisterSelfProc : (optional) Unregistration Callback
  199. * @param mFactoryDestructor : (optional) Destruction Callback
  200. * @param mGetInterfacesProc : (optional) Interfaces Callback
  201. * @param mGetLanguageHelperProc : (optional) Language Helper Callback
  202. * @param mClassInfoGlobal : (optional) Global Class Info of given object
  203. * @param mFlags : (optional) Class Info Flags @see nsIClassInfo
  204. *
  205. * E.g.:
  206. * static nsModuleComponentInfo components[] = { ... };
  207. *
  208. * See xpcom/sample/nsSampleModule.cpp for more info.
  209. */
  210. struct nsModuleComponentInfo {
  211. const char* mDescription;
  212. nsCID mCID;
  213. const char* mContractID;
  214. NSConstructorProcPtr mConstructor;
  215. NSRegisterSelfProcPtr mRegisterSelfProc;
  216. NSUnregisterSelfProcPtr mUnregisterSelfProc;
  217. NSFactoryDestructorProcPtr mFactoryDestructor;
  218. NSGetInterfacesProcPtr mGetInterfacesProc;
  219. NSGetLanguageHelperProcPtr mGetLanguageHelperProc;
  220. nsIClassInfo ** mClassInfoGlobal;
  221. PRUint32 mFlags;
  222. };
  223. /** Module Callbacks **/
  224. /**
  225. * nsModuleConstructorProc
  226. *
  227. * This function is called when the module is first being constructed.
  228. * @param self module which is being constructed.
  229. *
  230. * @return NS_OK Construction successful.
  231. * NS_ERROR* Method failure which will result in module not being
  232. * loaded.
  233. **/
  234. typedef nsresult (PR_CALLBACK *nsModuleConstructorProc) (nsIModule *self);
  235. /**
  236. * nsModuleDestructorProc
  237. *
  238. * This function is called when the module is being destroyed.
  239. * @param self module which is being destroyed.
  240. *
  241. **/
  242. typedef void (PR_CALLBACK *nsModuleDestructorProc) (nsIModule *self);
  243. /**
  244. * nsModuleInfo
  245. *
  246. * Use this structure to define meta-information about the module
  247. * itself, including the name, its components, and an optional
  248. * module-level initialization or shutdown routine.
  249. *
  250. * @param mVersion : Module Info Version
  251. * @param mModuleName : Module Name
  252. * @param mComponents : Array of Components
  253. * @param mCount : Count of mComponents
  254. * @param mCtor : Module user defined constructor
  255. * @param mDtor : Module user defined destructor
  256. *
  257. **/
  258. struct nsModuleInfo {
  259. PRUint32 mVersion;
  260. const char* mModuleName;
  261. const nsModuleComponentInfo *mComponents;
  262. PRUint32 mCount;
  263. nsModuleConstructorProc mCtor;
  264. nsModuleDestructorProc mDtor;
  265. };
  266. /**
  267. * Rev this if you change the nsModuleInfo, and are worried about
  268. * binary compatibility. (Ostensibly fix NS_NewGenericModule2() to deal
  269. * with older rev's at the same time.)
  270. */
  271. #define NS_MODULEINFO_VERSION 0x00015000UL // 1.5
  272. /**
  273. * Create a new generic module. Use the NS_IMPL_NSGETMODULE macro, or
  274. * one of its relatives, rather than using this directly.
  275. */
  276. NS_COM_GLUE nsresult
  277. NS_NewGenericModule2(nsModuleInfo const *info, nsIModule* *result);
  278. /**
  279. * Obsolete. Use NS_NewGenericModule2() instead.
  280. */
  281. NS_COM_GLUE nsresult
  282. NS_NewGenericModule(const char* moduleName,
  283. PRUint32 componentCount,
  284. nsModuleComponentInfo* components,
  285. nsModuleDestructorProc dtor,
  286. nsIModule* *result);
  287. #if defined(XPCOM_TRANSLATE_NSGM_ENTRY_POINT)
  288. # define NSGETMODULE_ENTRY_POINT(_name) NS_VISIBILITY_HIDDEN nsresult _name##_NSGetModule
  289. #else
  290. # define NSGETMODULE_ENTRY_POINT(_name) extern "C" NS_EXPORT nsresult NSGetModule
  291. #endif
  292. /**
  293. * Ease of use Macros which define NSGetModule for your component.
  294. * See xpcom/sample/nsSampleModule.cpp for more info.
  295. *
  296. **/
  297. #define NS_IMPL_NSGETMODULE(_name, _components) \
  298. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, nsnull)
  299. #define NS_IMPL_NSGETMODULE_WITH_CTOR(_name, _components, _ctor) \
  300. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, nsnull)
  301. #define NS_IMPL_NSGETMODULE_WITH_DTOR(_name, _components, _dtor) \
  302. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, _dtor)
  303. #define NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, _dtor) \
  304. static nsModuleInfo const kModuleInfo = { \
  305. NS_MODULEINFO_VERSION, \
  306. (#_name), \
  307. (_components), \
  308. (sizeof(_components) / sizeof(_components[0])), \
  309. (_ctor), \
  310. (_dtor) \
  311. }; \
  312. NSGETMODULE_ENTRY_POINT(_name) \
  313. (nsIComponentManager *servMgr, \
  314. nsIFile* location, \
  315. nsIModule** result) \
  316. { \
  317. return NS_NewGenericModule2(&kModuleInfo, result); \
  318. }
  319. ////////////////////////////////////////////////////////////////////////////////
  320. #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \
  321. static NS_IMETHODIMP \
  322. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  323. void **aResult) \
  324. { \
  325. nsresult rv; \
  326. \
  327. _InstanceClass * inst; \
  328. \
  329. *aResult = NULL; \
  330. if (NULL != aOuter) { \
  331. rv = NS_ERROR_NO_AGGREGATION; \
  332. return rv; \
  333. } \
  334. \
  335. NS_NEWXPCOM(inst, _InstanceClass); \
  336. if (NULL == inst) { \
  337. rv = NS_ERROR_OUT_OF_MEMORY; \
  338. return rv; \
  339. } \
  340. NS_ADDREF(inst); \
  341. rv = inst->QueryInterface(aIID, aResult); \
  342. NS_RELEASE(inst); \
  343. \
  344. return rv; \
  345. } \
  346. #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
  347. static NS_IMETHODIMP \
  348. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  349. void **aResult) \
  350. { \
  351. nsresult rv; \
  352. \
  353. _InstanceClass * inst; \
  354. \
  355. *aResult = NULL; \
  356. if (NULL != aOuter) { \
  357. rv = NS_ERROR_NO_AGGREGATION; \
  358. return rv; \
  359. } \
  360. \
  361. NS_NEWXPCOM(inst, _InstanceClass); \
  362. if (NULL == inst) { \
  363. rv = NS_ERROR_OUT_OF_MEMORY; \
  364. return rv; \
  365. } \
  366. NS_ADDREF(inst); \
  367. rv = inst->_InitMethod(); \
  368. if(NS_SUCCEEDED(rv)) { \
  369. rv = inst->QueryInterface(aIID, aResult); \
  370. } \
  371. NS_RELEASE(inst); \
  372. \
  373. return rv; \
  374. } \
  375. // 'Constructor' that uses an existing getter function that gets a singleton.
  376. // NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
  377. #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
  378. static NS_IMETHODIMP \
  379. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  380. void **aResult) \
  381. { \
  382. nsresult rv; \
  383. \
  384. _InstanceClass * inst; \
  385. \
  386. *aResult = NULL; \
  387. if (NULL != aOuter) { \
  388. rv = NS_ERROR_NO_AGGREGATION; \
  389. return rv; \
  390. } \
  391. \
  392. inst = _GetterProc(); \
  393. if (NULL == inst) { \
  394. rv = NS_ERROR_OUT_OF_MEMORY; \
  395. return rv; \
  396. } \
  397. /* NS_ADDREF(inst); */ \
  398. rv = inst->QueryInterface(aIID, aResult); \
  399. NS_RELEASE(inst); \
  400. \
  401. return rv; \
  402. } \
  403. #endif /* nsIGenericFactory_h___ */