Back to index

lightning-sunbird  0.9+nobinonly
wsppropertybagwrapper.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 mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 2001
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Vidur Apparao (vidur@netscape.com)  (Original author)
00024  *   John Bandhauer (jband@netscape.com)
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "wspprivate.h"
00041 #include "nsIXPConnect.h"
00042 #include "jsapi.h"
00043 
00044 
00045 WSPPropertyBagWrapper::WSPPropertyBagWrapper()
00046   : mIID(nsnull)
00047 {
00048 }
00049 
00050 WSPPropertyBagWrapper::~WSPPropertyBagWrapper()
00051 {
00052 }
00053 
00054 nsresult
00055 WSPPropertyBagWrapper::Init(nsIPropertyBag* aPropertyBag,
00056                             nsIInterfaceInfo* aInterfaceInfo)
00057 {
00058   mPropertyBag = aPropertyBag;
00059   mInterfaceInfo = aInterfaceInfo;
00060   mInterfaceInfo->GetIIDShared(&mIID);
00061   return NS_OK;
00062 }
00063 
00064 NS_METHOD
00065 WSPPropertyBagWrapper::Create(nsISupports* outer, const nsIID& aIID,
00066                               void* *aInstancePtr)
00067 {
00068   NS_ENSURE_ARG_POINTER(aInstancePtr);
00069   NS_ENSURE_NO_AGGREGATION(outer);
00070 
00071   WSPPropertyBagWrapper* wrapper = new WSPPropertyBagWrapper();
00072   if (!wrapper) {
00073     return NS_ERROR_OUT_OF_MEMORY;
00074   }
00075 
00076   NS_ADDREF(wrapper);
00077   nsresult rv = wrapper->QueryInterface(aIID, aInstancePtr);
00078   NS_RELEASE(wrapper);
00079   return rv;
00080 }
00081 
00082 NS_IMPL_ADDREF(WSPPropertyBagWrapper)
00083 NS_IMPL_RELEASE(WSPPropertyBagWrapper)
00084 
00085 NS_IMETHODIMP
00086 WSPPropertyBagWrapper::QueryInterface(REFNSIID aIID, void** aInstancePtr)
00087 {
00088   if((mIID && aIID.Equals(*mIID)) || aIID.Equals(NS_GET_IID(nsISupports))) {
00089     *aInstancePtr = NS_STATIC_CAST(nsXPTCStubBase*, this);
00090   }
00091   else if (aIID.Equals(NS_GET_IID(nsIWebServicePropertyBagWrapper))) {
00092     *aInstancePtr = NS_STATIC_CAST(nsIWebServicePropertyBagWrapper*, this);
00093   }
00094   else if (aIID.Equals(NS_GET_IID(nsIClassInfo))) {
00095     *aInstancePtr = NS_STATIC_CAST(nsIClassInfo*, this);
00096   } else {
00097     return NS_ERROR_NO_INTERFACE;
00098   }
00099 
00100   NS_ADDREF_THIS();
00101   return NS_OK;
00102 }
00103 
00104 NS_IMETHODIMP
00105 WSPPropertyBagWrapper::CallMethod(PRUint16 methodIndex,
00106                                   const nsXPTMethodInfo* info,
00107                                   nsXPTCMiniVariant* params)
00108 {
00109   if (methodIndex < 3) {
00110     NS_ERROR("WSPPropertyBagWrapper: bad method index");
00111     return NS_ERROR_FAILURE;
00112   }
00113 
00114   nsresult rv = NS_OK;
00115   nsAutoString propName;
00116 
00117   rv = WSPFactory::C2XML(nsDependentCString(info->GetName()), propName);
00118   if (NS_FAILED(rv)) {
00119     return rv;
00120   }
00121 
00122   nsCOMPtr<nsIVariant> val;
00123   rv = mPropertyBag->GetProperty(propName, getter_AddRefs(val));
00124   if (NS_FAILED(rv)) {
00125     return rv;
00126   }
00127 
00128   nsCOMPtr<nsIInterfaceInfo> iinfo;
00129   if (info->IsGetter()) {
00130     const nsXPTParamInfo& paramInfo = info->GetParam(0);
00131     const nsXPTType& type = paramInfo.GetType();
00132     uint8 type_tag = type.TagPart();
00133 
00134     if (type_tag == nsXPTType::T_INTERFACE) {
00135       rv = mInterfaceInfo->GetInfoForParam(methodIndex, &paramInfo,
00136                                            getter_AddRefs(iinfo));
00137       if (NS_FAILED(rv)) {
00138         return rv;
00139       }
00140     }
00141 
00142     rv = WSPProxy::VariantToValue(type_tag, params[0].val.p, iinfo, val);
00143   }
00144   else if (info->GetParamCount() == 2) {
00145     // If it's not an explicit getter, it has to be an array getter
00146     // method.
00147 
00148     // The first parameter should be the array length out param
00149     const nsXPTParamInfo& paramInfo1 = info->GetParam(0);
00150     const nsXPTType& type1 = paramInfo1.GetType();
00151     if (!paramInfo1.IsOut() || (type1.TagPart() != nsXPTType::T_U32)) {
00152       NS_ERROR("Unexpected parameter type for getter");
00153       return NS_ERROR_FAILURE;
00154     }
00155 
00156     // The second parameter should be the array out pointer itself.
00157     const nsXPTParamInfo& paramInfo2 = info->GetParam(1);
00158     const nsXPTType& type2 = paramInfo2.GetType();
00159     if (!paramInfo2.IsOut() || !type2.IsArray()) {
00160       NS_ERROR("Unexpected parameter type for getter");
00161       return NS_ERROR_FAILURE;
00162     }
00163 
00164     nsXPTType arrayType;
00165     rv = mInterfaceInfo->GetTypeForParam(methodIndex, &paramInfo2,
00166                                          1, &arrayType);
00167     if (NS_FAILED(rv)) {
00168       return rv;
00169     }
00170 
00171     if (arrayType.IsInterfacePointer()) {
00172       rv = mInterfaceInfo->GetInfoForParam(methodIndex, &paramInfo2,
00173                                            getter_AddRefs(iinfo));
00174       if (NS_FAILED(rv)) {
00175         return rv;
00176       }
00177     }
00178 
00179     rv = WSPProxy::VariantToArrayValue(arrayType.TagPart(), params, params + 1,
00180                                        iinfo, val);
00181   }
00182   else {
00183     NS_ERROR("Unexpected method signature for property bag wrapper");
00184     return NS_ERROR_FAILURE;
00185   }
00186 
00187   return rv;
00188 }
00189 
00190 NS_IMETHODIMP
00191 WSPPropertyBagWrapper::GetInterfaceInfo(nsIInterfaceInfo** info)
00192 {
00193   NS_ENSURE_ARG_POINTER(info);
00194 
00195   *info = mInterfaceInfo;
00196   NS_ADDREF(*info);
00197 
00198   return NS_OK;
00199 }
00200 
00202 //
00203 // Implementation of nsIClassInfo
00204 //
00206 
00207 /* void getInterfaces (out PRUint32 count, [array, size_is (count),
00208    retval] out nsIIDPtr array); */
00209 NS_IMETHODIMP
00210 WSPPropertyBagWrapper::GetInterfaces(PRUint32 *count, nsIID * **array)
00211 {
00212   if (!mIID) {
00213     return NS_ERROR_NOT_INITIALIZED;
00214   }
00215 
00216   *count = 2;
00217   nsIID** iids = NS_STATIC_CAST(nsIID**, nsMemory::Alloc(2 * sizeof(nsIID*)));
00218   if (!iids) {
00219     return NS_ERROR_OUT_OF_MEMORY;
00220   }
00221 
00222   iids[0] = NS_STATIC_CAST(nsIID *, nsMemory::Clone(mIID, sizeof(nsIID)));
00223   if (!iids[0]) {
00224     nsMemory::Free(iids);
00225     return NS_ERROR_OUT_OF_MEMORY;
00226   }
00227 
00228   const nsIID& wsiid = NS_GET_IID(nsIWebServicePropertyBagWrapper);
00229   iids[1] = NS_STATIC_CAST(nsIID *, nsMemory::Clone(&wsiid, sizeof(nsIID)));
00230   if (!iids[1]) {
00231     nsMemory::Free(iids[0]);
00232     nsMemory::Free(iids);
00233     return NS_ERROR_OUT_OF_MEMORY;
00234   }
00235 
00236   *array = iids;
00237 
00238   return NS_OK;
00239 }
00240 
00241 /* nsISupports getHelperForLanguage (in PRUint32 language); */
00242 NS_IMETHODIMP
00243 WSPPropertyBagWrapper::GetHelperForLanguage(PRUint32 language,
00244                                             nsISupports **_retval)
00245 {
00246   *_retval = nsnull;
00247   return NS_OK;
00248 }
00249 
00250 /* readonly attribute string contractID; */
00251 NS_IMETHODIMP
00252 WSPPropertyBagWrapper::GetContractID(char * *aContractID)
00253 {
00254   *aContractID = (char *)
00255     nsMemory::Clone(NS_WEBSERVICEPROPERTYBAGWRAPPER_CONTRACTID,
00256                     sizeof(NS_WEBSERVICEPROPERTYBAGWRAPPER_CONTRACTID));
00257   if (!*aContractID) {
00258     return NS_ERROR_OUT_OF_MEMORY;
00259   }
00260 
00261   return NS_OK;
00262 }
00263 
00264 /* readonly attribute string classDescription; */
00265 NS_IMETHODIMP
00266 WSPPropertyBagWrapper::GetClassDescription(char * *aClassDescription)
00267 {
00268   *aClassDescription = nsnull;
00269   return NS_OK;
00270 }
00271 
00272 /* readonly attribute nsCIDPtr classID; */
00273 NS_IMETHODIMP
00274 WSPPropertyBagWrapper::GetClassID(nsCID * *aClassID)
00275 {
00276   *aClassID = nsnull;
00277   return NS_OK;
00278 }
00279 
00280 /* readonly attribute PRUint32 implementationLanguage; */
00281 NS_IMETHODIMP
00282 WSPPropertyBagWrapper::GetImplementationLanguage(PRUint32 *aImplementationLanguage)
00283 {
00284   *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
00285   return NS_OK;
00286 }
00287 
00288 /* readonly attribute PRUint32 flags; */
00289 NS_IMETHODIMP
00290 WSPPropertyBagWrapper::GetFlags(PRUint32 *aFlags)
00291 {
00292   *aFlags = nsIClassInfo::DOM_OBJECT;
00293   return NS_OK;
00294 }
00295 
00296 /* [notxpcom] readonly attribute nsCID classIDNoAlloc; */
00297 NS_IMETHODIMP
00298 WSPPropertyBagWrapper::GetClassIDNoAlloc(nsCID *aClassIDNoAlloc)
00299 {
00300   return NS_ERROR_NOT_AVAILABLE;
00301 }
00302