PageRenderTime 40ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/gecko_api/include/nsIGenericFactory.h

http://firefox-mac-pdf.googlecode.com/
C Header | 454 lines | 188 code | 45 blank | 221 comment | 13 complexity | 2a110437b76428cfbdf29ea9fa0e8676 MD5 | raw file
  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 <benjamin@smedbergs.us>
  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_DECLARE_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_DEFINE_STATIC_IID_ACCESSOR(nsIGenericFactory, NS_IGENERICFACTORY_IID)
  67. #ifndef XPCOM_GLUE_AVOID_NSPR
  68. NS_COM_GLUE nsresult
  69. NS_NewGenericFactory(nsIGenericFactory **result,
  70. const nsModuleComponentInfo *info);
  71. #endif
  72. /** Component Callbacks **/
  73. /**
  74. * NSConstructorProcPtr
  75. *
  76. * This function will be used by the generic factory to create an
  77. * instance of the given CID.
  78. *
  79. * @param aOuter : Pointer to a component that wishes to be aggregated
  80. * in the resulting instance. This will be nsnull if no
  81. * aggregation is requested.
  82. * @param iid : The IID of the interface being requested in
  83. * the component which is being currently created.
  84. * @param result : [out] Pointer to the newly created instance, if successful.
  85. *
  86. * @return NS_OK Component successfully created and the interface
  87. * being requested was successfully returned in result.
  88. * NS_NOINTERFACE Interface not accessible.
  89. * NS_ERROR_NO_AGGREGATION if an 'outer' object is supplied, but the
  90. * component is not aggregatable.
  91. * NS_ERROR* Method failure.
  92. **/
  93. typedef NS_CALLBACK(NSConstructorProcPtr)(nsISupports *aOuter,
  94. REFNSIID aIID,
  95. void **aResult);
  96. /**
  97. * NSRegisterSelfProcPtr
  98. *
  99. * One time registration call back. Allows you to perform registration
  100. * specific activity like adding yourself to a category.
  101. *
  102. * @param aCompMgr : The global component manager
  103. * @param aFile : Component File. This file must have an associated
  104. * loader and export the required symbols which this
  105. * loader defines.
  106. * @param aLoaderStr : Opaque loader specific string. This value is
  107. * passed into the nsIModule's registerSelf
  108. * callback and must be fowarded unmodified when
  109. * registering factories via their location.
  110. * @param aType : Component Type of CID aClass. This value is
  111. * passed into the nsIModule's registerSelf
  112. * callback and must be fowarded unmodified when
  113. * registering factories via their location.
  114. * @param aInfo : Pointer to array of nsModuleComponentInfo
  115. *
  116. * @param aInfo
  117. * @return NS_OK Registration was successful.
  118. * NS_ERROR* Method failure.
  119. **/
  120. typedef NS_CALLBACK(NSRegisterSelfProcPtr)(nsIComponentManager *aCompMgr,
  121. nsIFile *aPath,
  122. const char *aLoaderStr,
  123. const char *aType,
  124. const nsModuleComponentInfo *aInfo);
  125. /**
  126. * NSUnregisterSelfProcPtr
  127. *
  128. * One time unregistration call back. Allows you to perform unregistration
  129. * specific activity like removing yourself from a category.
  130. *
  131. * @param aCompMgr : The global component manager
  132. * @param aFile : Component File. This file must have an associated
  133. * loader and export the required symbols which this
  134. * loader defines.
  135. * @param aLoaderStr : Opaque loader specific string. This value is
  136. * passed into the nsIModule's registerSelf
  137. * callback and must be fowarded unmodified when
  138. * registering factories via their location
  139. * @param aInfo : Pointer to array of nsModuleComponentInfo
  140. *
  141. * @param aInfo
  142. * @return NS_OK Registration was successful.
  143. * NS_ERROR* Method failure.
  144. **/
  145. typedef NS_CALLBACK(NSUnregisterSelfProcPtr)(nsIComponentManager *aCompMgr,
  146. nsIFile *aPath,
  147. const char *aLoaderStr,
  148. const nsModuleComponentInfo *aInfo);
  149. /**
  150. * NSFactoryDestructorProcPtr
  151. *
  152. * This function will be called when the factory is being destroyed.
  153. *
  154. **/
  155. typedef NS_CALLBACK(NSFactoryDestructorProcPtr)(void);
  156. /**
  157. * NSGetInterfacesProcPtr
  158. *
  159. * This function is used to implement class info.
  160. *
  161. * Get an ordered list of the interface ids that instances of the class
  162. * promise to implement. Note that nsISupports is an implicit member
  163. * of any such list and need not be included.
  164. *
  165. * Should set *count = 0 and *array = null and return NS_OK if getting the
  166. * list is not supported.
  167. *
  168. * @see nsIClassInfo.idl
  169. **/
  170. typedef NS_CALLBACK(NSGetInterfacesProcPtr)(PRUint32 *countp,
  171. nsIID* **array);
  172. /**
  173. * NSGetLanguageHelperProcPtr
  174. *
  175. * This function is used to implement class info.
  176. *
  177. * Get a language mapping specific helper object that may assist in using
  178. * objects of this class in a specific lanaguage. For instance, if asked
  179. * for the helper for nsIProgrammingLanguage::JAVASCRIPT this might return
  180. * an object that can be QI'd into the nsIXPCScriptable interface to assist
  181. * XPConnect in supplying JavaScript specific behavior to callers of the
  182. * instance object.
  183. *
  184. * @see: nsIClassInfo.idl, nsIProgrammingLanguage.idl
  185. *
  186. * Should return null if no helper available for given language.
  187. **/
  188. typedef NS_CALLBACK(NSGetLanguageHelperProcPtr)(PRUint32 language,
  189. nsISupports **helper);
  190. /**
  191. * nsModuleComponentInfo
  192. *
  193. * Use this type to define a list of module component info to pass to
  194. * NS_NewGenericModule.
  195. *
  196. * @param mDescription : Class Name of given object
  197. * @param mCID : CID of given object
  198. * @param mContractID : Contract ID of given object
  199. * @param mConstructor : Constructor of given object
  200. * @param mRegisterSelfProc : (optional) Registration Callback
  201. * @param mUnregisterSelfProc : (optional) Unregistration Callback
  202. * @param mFactoryDestructor : (optional) Destruction Callback
  203. * @param mGetInterfacesProc : (optional) Interfaces Callback
  204. * @param mGetLanguageHelperProc : (optional) Language Helper Callback
  205. * @param mClassInfoGlobal : (optional) Global Class Info of given object
  206. * @param mFlags : (optional) Class Info Flags @see nsIClassInfo
  207. *
  208. * E.g.:
  209. * static nsModuleComponentInfo components[] = { ... };
  210. *
  211. * See xpcom/sample/nsSampleModule.cpp for more info.
  212. */
  213. struct nsModuleComponentInfo {
  214. const char* mDescription;
  215. nsCID mCID;
  216. const char* mContractID;
  217. NSConstructorProcPtr mConstructor;
  218. NSRegisterSelfProcPtr mRegisterSelfProc;
  219. NSUnregisterSelfProcPtr mUnregisterSelfProc;
  220. NSFactoryDestructorProcPtr mFactoryDestructor;
  221. NSGetInterfacesProcPtr mGetInterfacesProc;
  222. NSGetLanguageHelperProcPtr mGetLanguageHelperProc;
  223. nsIClassInfo ** mClassInfoGlobal;
  224. PRUint32 mFlags;
  225. };
  226. /** Module Callbacks **/
  227. /**
  228. * nsModuleConstructorProc
  229. *
  230. * This function is called when the module is first being constructed.
  231. * @param self module which is being constructed.
  232. *
  233. * @return NS_OK Construction successful.
  234. * NS_ERROR* Method failure which will result in module not being
  235. * loaded.
  236. **/
  237. typedef nsresult (PR_CALLBACK *nsModuleConstructorProc) (nsIModule *self);
  238. /**
  239. * nsModuleDestructorProc
  240. *
  241. * This function is called when the module is being destroyed.
  242. * @param self module which is being destroyed.
  243. *
  244. **/
  245. typedef void (PR_CALLBACK *nsModuleDestructorProc) (nsIModule *self);
  246. /**
  247. * nsModuleInfo
  248. *
  249. * Use this structure to define meta-information about the module
  250. * itself, including the name, its components, and an optional
  251. * module-level initialization or shutdown routine.
  252. *
  253. * @param mVersion : Module Info Version
  254. * @param mModuleName : Module Name
  255. * @param mComponents : Array of Components
  256. * @param mCount : Count of mComponents
  257. * @param mCtor : Module user defined constructor
  258. * @param mDtor : Module user defined destructor
  259. *
  260. **/
  261. struct nsModuleInfo {
  262. PRUint32 mVersion;
  263. const char* mModuleName;
  264. const nsModuleComponentInfo *mComponents;
  265. PRUint32 mCount;
  266. nsModuleConstructorProc mCtor;
  267. nsModuleDestructorProc mDtor;
  268. };
  269. /**
  270. * Rev this if you change the nsModuleInfo, and are worried about
  271. * binary compatibility. (Ostensibly fix NS_NewGenericModule2() to deal
  272. * with older rev's at the same time.)
  273. */
  274. #define NS_MODULEINFO_VERSION 0x00015000UL // 1.5
  275. #ifndef XPCOM_GLUE_AVOID_NSPR
  276. /**
  277. * Create a new generic module. Use the NS_IMPL_NSGETMODULE macro, or
  278. * one of its relatives, rather than using this directly.
  279. */
  280. NS_COM_GLUE nsresult
  281. NS_NewGenericModule2(nsModuleInfo const *info, nsIModule* *result);
  282. /**
  283. * Obsolete. Use NS_NewGenericModule2() instead.
  284. */
  285. NS_COM_GLUE nsresult
  286. NS_NewGenericModule(const char* moduleName,
  287. PRUint32 componentCount,
  288. nsModuleComponentInfo* components,
  289. nsModuleDestructorProc dtor,
  290. nsIModule* *result);
  291. #endif // XPCOM_GLUE_AVOID_NSPR
  292. #if defined(XPCOM_TRANSLATE_NSGM_ENTRY_POINT)
  293. # define NSGETMODULE_ENTRY_POINT(_name) NS_VISIBILITY_HIDDEN nsresult _name##_NSGetModule
  294. #else
  295. # define NSGETMODULE_ENTRY_POINT(_name) extern "C" NS_EXPORT nsresult NSGetModule
  296. #endif
  297. /**
  298. * Ease of use Macros which define NSGetModule for your component.
  299. * See xpcom/sample/nsSampleModule.cpp for more info.
  300. *
  301. **/
  302. #define NS_IMPL_NSGETMODULE(_name, _components) \
  303. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, nsnull)
  304. #define NS_IMPL_NSGETMODULE_WITH_CTOR(_name, _components, _ctor) \
  305. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, nsnull)
  306. #define NS_IMPL_NSGETMODULE_WITH_DTOR(_name, _components, _dtor) \
  307. NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, nsnull, _dtor)
  308. #define NS_IMPL_NSGETMODULE_WITH_CTOR_DTOR(_name, _components, _ctor, _dtor) \
  309. static nsModuleInfo const kModuleInfo = { \
  310. NS_MODULEINFO_VERSION, \
  311. (#_name), \
  312. (_components), \
  313. (sizeof(_components) / sizeof(_components[0])), \
  314. (_ctor), \
  315. (_dtor) \
  316. }; \
  317. NSGETMODULE_ENTRY_POINT(_name) \
  318. (nsIComponentManager *servMgr, \
  319. nsIFile* location, \
  320. nsIModule** result) \
  321. { \
  322. return NS_NewGenericModule2(&kModuleInfo, result); \
  323. }
  324. ////////////////////////////////////////////////////////////////////////////////
  325. #define NS_GENERIC_FACTORY_CONSTRUCTOR(_InstanceClass) \
  326. static NS_IMETHODIMP \
  327. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  328. void **aResult) \
  329. { \
  330. nsresult rv; \
  331. \
  332. _InstanceClass * inst; \
  333. \
  334. *aResult = NULL; \
  335. if (NULL != aOuter) { \
  336. rv = NS_ERROR_NO_AGGREGATION; \
  337. return rv; \
  338. } \
  339. \
  340. NS_NEWXPCOM(inst, _InstanceClass); \
  341. if (NULL == inst) { \
  342. rv = NS_ERROR_OUT_OF_MEMORY; \
  343. return rv; \
  344. } \
  345. NS_ADDREF(inst); \
  346. rv = inst->QueryInterface(aIID, aResult); \
  347. NS_RELEASE(inst); \
  348. \
  349. return rv; \
  350. } \
  351. #define NS_GENERIC_FACTORY_CONSTRUCTOR_INIT(_InstanceClass, _InitMethod) \
  352. static NS_IMETHODIMP \
  353. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  354. void **aResult) \
  355. { \
  356. nsresult rv; \
  357. \
  358. _InstanceClass * inst; \
  359. \
  360. *aResult = NULL; \
  361. if (NULL != aOuter) { \
  362. rv = NS_ERROR_NO_AGGREGATION; \
  363. return rv; \
  364. } \
  365. \
  366. NS_NEWXPCOM(inst, _InstanceClass); \
  367. if (NULL == inst) { \
  368. rv = NS_ERROR_OUT_OF_MEMORY; \
  369. return rv; \
  370. } \
  371. NS_ADDREF(inst); \
  372. rv = inst->_InitMethod(); \
  373. if(NS_SUCCEEDED(rv)) { \
  374. rv = inst->QueryInterface(aIID, aResult); \
  375. } \
  376. NS_RELEASE(inst); \
  377. \
  378. return rv; \
  379. } \
  380. // 'Constructor' that uses an existing getter function that gets a singleton.
  381. // NOTE: assumes that getter does an AddRef - so additional AddRef is not done.
  382. #define NS_GENERIC_FACTORY_SINGLETON_CONSTRUCTOR(_InstanceClass, _GetterProc) \
  383. static NS_IMETHODIMP \
  384. _InstanceClass##Constructor(nsISupports *aOuter, REFNSIID aIID, \
  385. void **aResult) \
  386. { \
  387. nsresult rv; \
  388. \
  389. _InstanceClass * inst; \
  390. \
  391. *aResult = NULL; \
  392. if (NULL != aOuter) { \
  393. rv = NS_ERROR_NO_AGGREGATION; \
  394. return rv; \
  395. } \
  396. \
  397. inst = _GetterProc(); \
  398. if (NULL == inst) { \
  399. rv = NS_ERROR_OUT_OF_MEMORY; \
  400. return rv; \
  401. } \
  402. /* NS_ADDREF(inst); */ \
  403. rv = inst->QueryInterface(aIID, aResult); \
  404. NS_RELEASE(inst); \
  405. \
  406. return rv; \
  407. } \
  408. #endif /* nsIGenericFactory_h___ */