nsIGenericFactory.h
上传用户:goldcmy89
上传日期:2017-12-03
资源大小:2246k
文件大小:21k
源码类别:

PlugIns编程

开发平台:

Visual C++

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