Back to index

lightning-sunbird  0.9+nobinonly
nsSOAPPropertyBag.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  *   Harshal Pradhan <hpradhan@hotpop.com>
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 "nsString.h"
00040 #include "nsIProperty.h"
00041 #include "nsSOAPPropertyBag.h"
00042 #include "nsCOMArray.h"
00043 #include "nsInterfaceHashtable.h"
00044 #include "nsHashKeys.h"
00045 #include "jsapi.h"
00046 #include "nsIXPCScriptable.h"
00047 
00048 class nsSOAPPropertyBagEnumerator;
00049 class nsSOAPPropertyBag:public nsIPropertyBag, public nsIXPCScriptable {
00050 public:
00051   nsSOAPPropertyBag();
00052   virtual ~nsSOAPPropertyBag();
00053 
00054   NS_DECL_ISUPPORTS
00055   NS_DECL_NSIPROPERTYBAG
00056   NS_DECL_NSIXPCSCRIPTABLE
00057 
00058   nsresult Init();
00059   nsresult SetProperty(const nsAString& aName, nsIVariant *aValue);
00060 
00061 protected:
00062   nsInterfaceHashtable<nsStringHashKey, nsIVariant> mProperties;
00063 
00064   friend class nsSOAPPropertyBagEnumerator;
00065 };
00066 
00067 
00068 class nsSOAPProperty:public nsIProperty {
00069 public:
00070   nsSOAPProperty(const nsAString & aName, nsIVariant * aValue);
00071   virtual ~nsSOAPProperty();
00072   NS_DECL_ISUPPORTS
00073   NS_DECL_NSIPROPERTY
00074 
00075 protected:
00076   nsString mName;
00077   nsCOMPtr<nsIVariant> mValue;
00078 };
00079 
00080 class nsSOAPPropertyBagEnumerator:public nsISimpleEnumerator {
00081 public:
00082   NS_DECL_ISUPPORTS
00083   NS_DECL_NSISIMPLEENUMERATOR
00084 
00085   nsSOAPPropertyBagEnumerator();
00086   virtual ~nsSOAPPropertyBagEnumerator();
00087 
00088   nsresult Init(nsSOAPPropertyBag*);
00089 
00090 protected:
00091   nsCOMArray<nsISupports> mProperties;
00092   PRUint32 mCurrent;
00093 };
00094 
00095 NS_IMPL_ISUPPORTS2_CI(nsSOAPPropertyBag, nsIPropertyBag, nsIXPCScriptable) 
00096 
00097 nsSOAPPropertyBag::nsSOAPPropertyBag()
00098 {
00099 }
00100 
00101 nsSOAPPropertyBag::~nsSOAPPropertyBag()
00102 {
00103 }
00104 
00105 nsresult
00106 nsSOAPPropertyBag::Init()
00107 {
00108   return mProperties.Init() ? NS_OK : NS_ERROR_FAILURE;
00109 }
00110 
00111 /* nsIVariant getProperty (in AString name); */
00112 NS_IMETHODIMP
00113 nsSOAPPropertyBag::GetProperty(const nsAString & aName,
00114                                nsIVariant ** _retval)
00115 {
00116   NS_ENSURE_ARG_POINTER(_retval);
00117   return mProperties.Get(aName, _retval) ? NS_OK : NS_ERROR_FAILURE;
00118 }
00119 
00120 nsresult
00121 nsSOAPPropertyBag::SetProperty(const nsAString & aName,
00122                                nsIVariant * aValue)
00123 {
00124   NS_ENSURE_ARG_POINTER(aValue);
00125   return mProperties.Put(aName, aValue);
00126 }
00127 
00128 // The nsIXPCScriptable map declaration that will generate stubs for us...
00129 #define XPC_MAP_CLASSNAME           nsSOAPPropertyBag
00130 #define XPC_MAP_QUOTED_CLASSNAME   "SOAPPropertyBag"
00131 #define                             XPC_MAP_WANT_GETPROPERTY
00132 #define XPC_MAP_FLAGS               0
00133 #include "xpc_map_end.h"        /* This will #undef the above */
00134 
00135 NS_IMETHODIMP
00136 nsSOAPPropertyBag::GetProperty(nsIXPConnectWrappedNative * wrapper,
00137                                JSContext * cx, JSObject * obj,
00138                                jsval id, jsval * vp, PRBool * _retval)
00139 {
00140   nsresult rv = NS_OK;
00141   if (JSVAL_IS_STRING(id)) {
00142     JSString *str = JSVAL_TO_STRING(id);
00143     const PRUnichar *name = NS_REINTERPRET_CAST(const PRUnichar *,
00144                                                 JS_GetStringChars(str));
00145     nsCOMPtr<nsIVariant> value;
00146     mProperties.Get(nsDependentString(name), getter_AddRefs(value));
00147     if (!value)
00148       return rv;
00149     rv = NS_SUCCESS_I_DID_SOMETHING;
00150     void *mark;
00151     jsval *argv = JS_PushArguments(cx, &mark, "%iv", value.get());
00152     *vp = *argv;
00153     JS_PopArguments(cx, mark);
00154   }
00155   return rv;
00156 }
00157 
00158 PLDHashOperator PR_CALLBACK
00159 PropertyBagEnumFunc(const nsAString& aKey, nsIVariant *aData, void *aClosure)
00160 {
00161   nsCOMArray<nsISupports>* properties =
00162       NS_STATIC_CAST(nsCOMArray<nsISupports>*, aClosure);
00163   
00164   nsSOAPProperty* prop = new nsSOAPProperty(aKey, aData);
00165   NS_ENSURE_TRUE(prop, PL_DHASH_STOP);
00166 
00167   properties->AppendObject(prop);
00168 
00169   return PL_DHASH_NEXT;
00170 }
00171 
00172 nsSOAPPropertyBagEnumerator::nsSOAPPropertyBagEnumerator() 
00173   : mCurrent(0)
00174 {
00175 }
00176 
00177 nsSOAPPropertyBagEnumerator::~nsSOAPPropertyBagEnumerator()
00178 {
00179 }
00180 
00181 NS_IMPL_ISUPPORTS1_CI(nsSOAPPropertyBagEnumerator, nsISimpleEnumerator)
00182 
00183 nsresult
00184 nsSOAPPropertyBagEnumerator::Init(nsSOAPPropertyBag* aPropertyBag)
00185 {
00186   PRUint32 count = aPropertyBag->mProperties.EnumerateRead(PropertyBagEnumFunc,
00187                                                            &mProperties);
00188   // Check if all the properties got copied to the array.
00189   if (count != aPropertyBag->mProperties.Count()) {
00190     return NS_ERROR_FAILURE;
00191   }
00192 
00193   return NS_OK;
00194   
00195 }
00196 
00197 NS_IMETHODIMP
00198 nsSOAPPropertyBagEnumerator::GetNext(nsISupports ** aItem)
00199 {
00200   NS_ENSURE_ARG_POINTER(aItem);
00201   PRUint32 count = mProperties.Count();
00202   if (mCurrent < count) {
00203     NS_ADDREF(*aItem = mProperties[mCurrent++]);
00204     return NS_OK;
00205   }
00206   return NS_ERROR_FAILURE;
00207 }
00208 
00209 NS_IMETHODIMP
00210 nsSOAPPropertyBagEnumerator::HasMoreElements(PRBool * aResult)
00211 {
00212   NS_ENSURE_ARG_POINTER(aResult);
00213   *aResult = mCurrent < (PRUint32) mProperties.Count();
00214   return NS_OK;
00215 }
00216 
00217 NS_IMPL_ISUPPORTS1_CI(nsSOAPProperty, nsIProperty)
00218 
00219 nsSOAPProperty::nsSOAPProperty(const nsAString& aName, nsIVariant *aValue) 
00220   : mName(aName), mValue(aValue) 
00221 {
00222 }
00223 
00224 nsSOAPProperty::~nsSOAPProperty()
00225 {
00226 }
00227 
00228 NS_IMETHODIMP
00229 nsSOAPProperty::GetName(nsAString& _retval)
00230 {
00231   _retval.Assign(mName);
00232   return NS_OK;
00233 }
00234 
00235 NS_IMETHODIMP
00236 nsSOAPProperty::GetValue(nsIVariant** _retval)
00237 {
00238   NS_ENSURE_ARG_POINTER(_retval);
00239   *_retval = mValue;
00240   NS_ADDREF(*_retval);
00241   return NS_OK;
00242 }
00243 
00244 /* readonly attribute nsISimpleEnumerator enumerator; */
00245 NS_IMETHODIMP
00246 nsSOAPPropertyBag::GetEnumerator(nsISimpleEnumerator * *aEnumerator)
00247 {
00248   NS_ENSURE_ARG_POINTER(aEnumerator);
00249 
00250   nsRefPtr<nsSOAPPropertyBagEnumerator> enumerator = new nsSOAPPropertyBagEnumerator();
00251   NS_ENSURE_TRUE(enumerator, NS_ERROR_OUT_OF_MEMORY);
00252 
00253   nsresult rv = enumerator->Init(this);
00254   NS_ENSURE_SUCCESS(rv, rv);
00255   
00256   NS_ADDREF(*aEnumerator = enumerator);
00257 
00258   return NS_OK;
00259 }
00260 
00261 nsSOAPPropertyBagMutator::nsSOAPPropertyBagMutator()
00262 {
00263 }
00264 
00265 nsSOAPPropertyBagMutator::~nsSOAPPropertyBagMutator()
00266 {
00267 }
00268 
00269 NS_IMPL_ISUPPORTS1_CI(nsSOAPPropertyBagMutator, nsISOAPPropertyBagMutator)
00270 
00271 nsresult
00272 nsSOAPPropertyBagMutator::Init()
00273 {
00274   mSOAPBag = new nsSOAPPropertyBag();
00275   NS_ENSURE_TRUE(mSOAPBag, NS_ERROR_OUT_OF_MEMORY);
00276   
00277   return mSOAPBag->Init();
00278 }
00279 
00280 NS_IMETHODIMP
00281 nsSOAPPropertyBagMutator::GetPropertyBag(nsIPropertyBag ** aPropertyBag)
00282 {
00283   NS_ENSURE_ARG_POINTER(aPropertyBag);
00284 
00285   NS_ADDREF(*aPropertyBag = mSOAPBag);
00286   return NS_OK;
00287 }
00288 
00289 NS_IMETHODIMP
00290 nsSOAPPropertyBagMutator::AddProperty(const nsAString& aName,
00291                                       nsIVariant *aValue)
00292 {
00293   NS_ENSURE_ARG_POINTER(aValue);
00294 
00295   return mSOAPBag->SetProperty(aName, aValue);
00296 }