Back to index

lightning-sunbird  0.9+nobinonly
nsXTFWeakTearoff.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is the Mozilla XTF project.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Alex Fritze.
00019  * Portions created by the Initial Developer are Copyright (C) 2004
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Alex Fritze <alex@croczilla.com> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsCOMPtr.h"
00040 #include "xptcall.h"
00041 #include "nsIInterfaceInfo.h"
00042 #include "nsIInterfaceInfoManager.h"
00043 #ifdef DEBUG
00044 #include <stdio.h>
00045 #endif
00046 
00048 // nsXTFWeakTearoff class
00049 
00050 class nsXTFWeakTearoff : public nsXPTCStubBase
00051 {
00052 protected:
00053   friend nsresult
00054   NS_NewXTFWeakTearoff(const nsIID& iid,
00055                        nsISupports* obj,
00056                        nsISupports** result);
00057 
00058   nsXTFWeakTearoff(const nsIID& iid,
00059                    nsISupports* obj);
00060   ~nsXTFWeakTearoff();
00061   
00062 public:
00063   // nsISupports interface
00064   NS_DECL_ISUPPORTS
00065   
00066   // nsXPTCStubBase
00067   NS_IMETHOD GetInterfaceInfo(nsIInterfaceInfo** info);
00068 
00069   NS_IMETHOD CallMethod(PRUint16 methodIndex,
00070                         const nsXPTMethodInfo* info,
00071                         nsXPTCMiniVariant* params);
00072 
00073 private:
00074   nsISupports *mObj;
00075   nsIID mIID;
00076 };
00077 
00078 //----------------------------------------------------------------------
00079 // implementation:
00080 
00081 nsXTFWeakTearoff::nsXTFWeakTearoff(const nsIID& iid,
00082                                    nsISupports* obj)
00083     : mObj(obj), mIID(iid)
00084 {
00085 #ifdef DEBUG
00086 //  printf("nsXTFWeakTearoff CTOR\n");
00087 #endif
00088 }
00089 
00090 nsXTFWeakTearoff::~nsXTFWeakTearoff()
00091 {
00092 #ifdef DEBUG
00093 //  printf("nsXTFWeakTearoff DTOR\n");
00094 #endif
00095 }
00096 
00097 nsresult
00098 NS_NewXTFWeakTearoff(const nsIID& iid,
00099                      nsISupports* obj,
00100                      nsISupports** aResult){
00101   NS_PRECONDITION(aResult != nsnull, "null ptr");
00102   if (!aResult)
00103     return NS_ERROR_NULL_POINTER;
00104 
00105   nsXTFWeakTearoff* result = new nsXTFWeakTearoff(iid,obj);
00106   if (! result)
00107     return NS_ERROR_OUT_OF_MEMORY;
00108 
00109   NS_ADDREF(result);
00110   *aResult = result;
00111   return NS_OK;
00112 }
00113 
00114 //----------------------------------------------------------------------
00115 // nsISupports implementation
00116 
00117 NS_IMPL_ADDREF(nsXTFWeakTearoff)
00118 NS_IMPL_RELEASE(nsXTFWeakTearoff)
00119 
00120 NS_IMETHODIMP
00121 nsXTFWeakTearoff::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00122 {
00123   if(aIID.Equals(mIID) || aIID.Equals(NS_GET_IID(nsISupports))) {
00124     *aInstancePtr = NS_STATIC_CAST(nsXPTCStubBase*, this);
00125     NS_ADDREF_THIS();
00126     return NS_OK;
00127   }
00128   // we can't map QI onto the obj, because the xpcom wrapper otherwise
00129   // QI-accumulates all interfaces defined on mObj
00130 //  else return mObj->QueryInterface(aIID, aInstancePtr); 
00131   else return NS_ERROR_NO_INTERFACE;
00132 }
00133 
00134 //----------------------------------------------------------------------
00135 // nsXPTCStubBase implementation
00136 
00137 NS_IMETHODIMP
00138 nsXTFWeakTearoff::GetInterfaceInfo(nsIInterfaceInfo** info)
00139 {
00140   nsCOMPtr<nsIInterfaceInfoManager> iim = XPTI_GetInterfaceInfoManager();
00141   NS_ASSERTION(iim, "could not get interface info manager");
00142   return iim->GetInfoForIID( &mIID, info);
00143 }
00144 
00145 NS_IMETHODIMP
00146 nsXTFWeakTearoff::CallMethod(PRUint16 methodIndex,
00147                              const nsXPTMethodInfo* info,
00148                              nsXPTCMiniVariant* params)
00149 {
00150   if (methodIndex < 3) {
00151     NS_ERROR("huh? indirect nsISupports method call unexpected on nsXTFWeakTearoff.");
00152     return NS_ERROR_FAILURE;
00153   }
00154 
00155   // prepare args:
00156   int paramCount = info->GetParamCount();
00157   nsXPTCVariant* fullPars = paramCount ? new nsXPTCVariant[paramCount] : nsnull;
00158 
00159   for (int i=0; i<paramCount; ++i) {
00160     const nsXPTParamInfo& paramInfo = info->GetParam(i);
00161     uint8 flags = paramInfo.IsOut() ? nsXPTCVariant::PTR_IS_DATA : 0;
00162     fullPars[i].Init(params[i], paramInfo.GetType(), flags);
00163   }
00164   
00165   // make the call:
00166   nsresult rv = XPTC_InvokeByIndex(mObj,
00167                                    methodIndex,
00168                                    paramCount,
00169                                    fullPars);
00170   if (fullPars)
00171     delete []fullPars;
00172   return rv;
00173 }