sbPropertyBag.cpp
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 50; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /* vim:set ts=4 sw=4 sts=4: */
3 /* ***** BEGIN LICENSE BLOCK *****
4  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5  *
6  * The contents of this file are subject to the Mozilla Public License Version
7  * 1.1 (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  * http://www.mozilla.org/MPL/
10  *
11  * Software distributed under the License is distributed on an "AS IS" basis,
12  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13  * for the specific language governing rights and limitations under the
14  * License.
15  *
16  * The Original Code is Oracle Corporation code.
17  *
18  * The Initial Developer of the Original Code is
19  * Oracle Corporation
20  * Portions created by the Initial Developer are Copyright (C) 2004
21  * the Initial Developer. All Rights Reserved.
22  *
23  * Contributor(s):
24  * Vladimir Vukicevic <vladimir.vukicevic@oracle.com>
25  *
26  * Alternatively, the contents of this file may be used under the terms of
27  * either the GNU General Public License Version 2 or later (the "GPL"), or
28  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29  * in which case the provisions of the GPL or the LGPL are applicable instead
30  * of those above. If you wish to allow use of your version of this file only
31  * under the terms of either the GPL or the LGPL, and not to allow others to
32  * use your version of this file under the terms of the MPL, indicate your
33  * decision by deleting the provisions above and replace them with the notice
34  * and other provisions required by the GPL or the LGPL. If you do not delete
35  * the provisions above, a recipient may use your version of this file under
36  * the terms of any one of the MPL, the GPL or the LGPL.
37  *
38  * ***** END LICENSE BLOCK ***** */
39 
40 #include "sbPropertyBag.h"
41 #include "sbArray.h"
42 #include "nsArrayEnumerator.h"
43 #include "nsComponentManagerUtils.h"
44 #include "nsIVariant.h"
45 #include "nsIProperty.h"
46 
47 nsresult
48 SB_NewHashPropertyBag(nsIWritablePropertyBag* *_retval)
49 {
50  sbPropertyBag *hpb = new sbPropertyBag();
51  if (!hpb)
52  return NS_ERROR_OUT_OF_MEMORY;
53 
54  NS_ADDREF(hpb);
55 
56  nsresult rv = hpb->Init();
57  if (NS_FAILED(rv)) {
58  NS_RELEASE(hpb);
59  return rv;
60  }
61 
62  *_retval = hpb;
63  return NS_OK;
64 }
65 
66 /*
67  * sbPropertyBag impl
68  */
69 
72 NS_INTERFACE_MAP_BEGIN(sbPropertyBag)
73  NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag)
74  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIPropertyBag, nsIWritablePropertyBag)
75  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIWritablePropertyBag)
76  NS_INTERFACE_MAP_ENTRY(nsIPropertyBag2)
77  NS_INTERFACE_MAP_ENTRY(nsIWritablePropertyBag2)
78 NS_INTERFACE_MAP_END
79 
80 nsresult
82 {
83  // we can only assume that Init will fail only due to OOM.
84  if (!mPropertyHash.Init())
85  return NS_ERROR_OUT_OF_MEMORY;
86  return NS_OK;
87 }
88 
89 NS_IMETHODIMP
90 sbPropertyBag::HasKey(const nsAString& name, PRBool *aResult)
91 {
92  *aResult = mPropertyHash.Get(name, nsnull);
93 
94  return NS_OK;
95 }
96 
97 NS_IMETHODIMP
98 sbPropertyBag::Get(const nsAString& name, nsIVariant* *_retval)
99 {
100  mPropertyHash.Get(name, _retval);
101 
102  return NS_OK;
103 }
104 
105 NS_IMETHODIMP
106 sbPropertyBag::GetProperty(const nsAString& name, nsIVariant* *_retval)
107 {
108  PRBool isFound = mPropertyHash.Get(name, _retval);
109  if (!isFound)
110  return NS_ERROR_FAILURE;
111 
112  return NS_OK;
113 }
114 
115 NS_IMETHODIMP
116 sbPropertyBag::SetProperty(const nsAString& name, nsIVariant *value)
117 {
118  NS_ENSURE_ARG_POINTER(value);
119 
120  PRBool success = mPropertyHash.Put(name, value);
121  if (!success)
122  return NS_ERROR_FAILURE;
123 
124  return NS_OK;
125 }
126 
127 NS_IMETHODIMP
128 sbPropertyBag::DeleteProperty(const nsAString& name)
129 {
130  // is it too much to ask for ns*Hashtable to return
131  // a boolean indicating whether RemoveEntry succeeded
132  // or not?!?!
133  PRBool isFound = mPropertyHash.Get(name, nsnull);
134  if (!isFound)
135  return NS_ERROR_FAILURE;
136 
137  // then from the hash
138  mPropertyHash.Remove(name);
139 
140  return NS_OK;
141 }
142 
143 
144 //
145 // nsSimpleProperty class and impl; used for GetEnumerator
146 //
147 
148 class nsSimpleProperty : public nsIProperty {
149 public:
150  nsSimpleProperty(const nsAString& aName, nsIVariant* aValue)
151  : mName(aName), mValue(aValue)
152  {
153  }
154 
156  NS_DECL_NSIPROPERTY
157 protected:
158  nsString mName;
159  nsCOMPtr<nsIVariant> mValue;
160 };
161 
163 
164 NS_IMETHODIMP
165 nsSimpleProperty::GetName(nsAString& aName)
166 {
167  aName.Assign(mName);
168  return NS_OK;
169 }
170 
171 NS_IMETHODIMP
172 nsSimpleProperty::GetValue(nsIVariant* *aValue)
173 {
174  NS_IF_ADDREF(*aValue = mValue);
175  return NS_OK;
176 }
177 
178 // end nsSimpleProperty
179 
180 PR_STATIC_CALLBACK(PLDHashOperator)
181 PropertyHashToArrayFunc (const nsAString &aKey,
182  nsIVariant* aData,
183  void *userArg)
184 {
185  nsIMutableArray *propertyArray =
186  static_cast<nsIMutableArray *>(userArg);
187  nsSimpleProperty *sprop = new nsSimpleProperty(aKey, aData);
188  propertyArray->AppendElement(sprop, PR_FALSE);
189  return PL_DHASH_NEXT;
190 }
191 
192 
193 NS_IMETHODIMP
194 sbPropertyBag::GetEnumerator(nsISimpleEnumerator* *_retval)
195 {
196  nsresult rv;
197  nsCOMPtr<nsIMutableArray> propertyArray =
198  do_CreateInstance("@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
199  NS_ENSURE_SUCCESS(rv, rv);
200  if (!propertyArray)
201  return NS_ERROR_OUT_OF_MEMORY;
202 
203  mPropertyHash.EnumerateRead(PropertyHashToArrayFunc, propertyArray.get());
204 
205  return NS_NewArrayEnumerator(_retval, propertyArray);
206 }
207 
208 #define IMPL_GETSETPROPERTY_AS(Name, Type) \
209 NS_IMETHODIMP \
210 sbPropertyBag::GetPropertyAs ## Name (const nsAString & prop, Type *_retval) \
211 { \
212  nsCOMPtr<nsIVariant> v; \
213  nsresult rv = mPropertyHash.Get(prop, getter_AddRefs(v)); \
214  NS_ENSURE_SUCCESS(rv, rv); \
215  if (!v) \
216  return NS_ERROR_NOT_AVAILABLE; \
217  return v->GetAs ## Name(_retval); \
218 } \
219 \
220 NS_IMETHODIMP \
221 sbPropertyBag::SetPropertyAs ## Name (const nsAString & prop, Type value) \
222 { \
223  nsresult rv; \
224  nsCOMPtr<nsIWritableVariant> var = \
225  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv); \
226  NS_ENSURE_SUCCESS(rv, rv); \
227  if (!var) \
228  return NS_ERROR_OUT_OF_MEMORY; \
229  var->SetAs ## Name(value); \
230  return SetProperty(prop, var); \
231 }
232 
233 IMPL_GETSETPROPERTY_AS(Int32, PRInt32)
234 IMPL_GETSETPROPERTY_AS(Uint32, PRUint32)
235 IMPL_GETSETPROPERTY_AS(Int64, PRInt64)
236 IMPL_GETSETPROPERTY_AS(Uint64, PRUint64)
237 IMPL_GETSETPROPERTY_AS(Double, double)
238 IMPL_GETSETPROPERTY_AS(Bool, PRBool)
239 
240 
241 NS_IMETHODIMP
242 sbPropertyBag::GetPropertyAsAString(const nsAString & prop, nsAString & _retval)
243 {
244  nsCOMPtr<nsIVariant> v;
245  nsresult rv = mPropertyHash.Get(prop, getter_AddRefs(v));
246  NS_ENSURE_SUCCESS(rv, rv);
247  if (!v)
248  return NS_ERROR_NOT_AVAILABLE;
249  return v->GetAsAString(_retval);
250 }
251 
252 NS_IMETHODIMP
253 sbPropertyBag::GetPropertyAsACString(const nsAString & prop, nsACString & _retval)
254 {
255  nsCOMPtr<nsIVariant> v;
256  nsresult rv = mPropertyHash.Get(prop, getter_AddRefs(v));
257  NS_ENSURE_SUCCESS(rv, rv);
258  if (!v)
259  return NS_ERROR_NOT_AVAILABLE;
260  return v->GetAsACString(_retval);
261 }
262 
263 NS_IMETHODIMP
264 sbPropertyBag::GetPropertyAsAUTF8String(const nsAString & prop, nsACString & _retval)
265 {
266  nsCOMPtr<nsIVariant> v;
267  nsresult rv = mPropertyHash.Get(prop, getter_AddRefs(v));
268  NS_ENSURE_SUCCESS(rv, rv);
269  if (!v)
270  return NS_ERROR_NOT_AVAILABLE;
271  return v->GetAsAUTF8String(_retval);
272 }
273 
274 NS_IMETHODIMP
275 sbPropertyBag::GetPropertyAsInterface(const nsAString & prop,
276  const nsIID & aIID,
277  void** _retval)
278 {
279  nsCOMPtr<nsIVariant> v;
280  nsresult rv = mPropertyHash.Get(prop, getter_AddRefs(v));
281  NS_ENSURE_SUCCESS(rv, rv);
282  if (!v)
283  return NS_ERROR_NOT_AVAILABLE;
284  nsCOMPtr<nsISupports> val;
285  rv = v->GetAsISupports(getter_AddRefs(val));
286  if (NS_FAILED(rv))
287  return rv;
288  if (!val) {
289  // We have a value, but it's null
290  *_retval = nsnull;
291  return NS_OK;
292  }
293  return val->QueryInterface(aIID, _retval);
294 }
295 
296 NS_IMETHODIMP
297 sbPropertyBag::SetPropertyAsAString(const nsAString & prop, const nsAString & value)
298 {
299  nsresult rv;
300  nsCOMPtr<nsIWritableVariant> var =
301  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
302  NS_ENSURE_SUCCESS(rv, rv);
303  if (!var)
304  return NS_ERROR_OUT_OF_MEMORY;
305  var->SetAsAString(value);
306  return SetProperty(prop, var);
307 }
308 
309 NS_IMETHODIMP
310 sbPropertyBag::SetPropertyAsACString(const nsAString & prop, const nsACString & value)
311 {
312  nsresult rv;
313  nsCOMPtr<nsIWritableVariant> var =
314  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
315  NS_ENSURE_SUCCESS(rv, rv);
316  if (!var)
317  return NS_ERROR_OUT_OF_MEMORY;
318  var->SetAsACString(value);
319  return SetProperty(prop, var);
320 }
321 
322 NS_IMETHODIMP
323 sbPropertyBag::SetPropertyAsAUTF8String(const nsAString & prop, const nsACString & value)
324 {
325  nsresult rv;
326  nsCOMPtr<nsIWritableVariant> var =
327  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
328  NS_ENSURE_SUCCESS(rv, rv);
329  if (!var)
330  return NS_ERROR_OUT_OF_MEMORY;
331  var->SetAsAUTF8String(value);
332  return SetProperty(prop, var);
333 }
334 
335 NS_IMETHODIMP
336 sbPropertyBag::SetPropertyAsInterface(const nsAString & prop, nsISupports* value)
337 {
338  nsresult rv;
339  nsCOMPtr<nsIWritableVariant> var =
340  do_CreateInstance("@songbirdnest.com/Songbird/Variant;1", &rv);
341  NS_ENSURE_SUCCESS(rv, rv);
342  if (!var)
343  return NS_ERROR_OUT_OF_MEMORY;
344  var->SetAsISupports(value);
345  return SetProperty(prop, var);
346 }
return NS_OK
onPageChanged aValue
Definition: FeedWriter.js:1395
PR_STATIC_CALLBACK(PRBool) FindElementCallback(void *aElement
NS_IMPL_ISUPPORTS1(sbDeviceCapabilitiesUtils, sbIDeviceCapabilitiesUtils) sbDeviceCapabilitiesUtils
nsSimpleProperty(const nsAString &aName, nsIVariant *aValue)
nsCOMPtr< nsIVariant > mValue
nsresult SB_NewHashPropertyBag(nsIWritablePropertyBag **_retval)
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
nsInterfaceHashtableMT< nsStringHashKey, nsIVariant > mPropertyHash
Definition: sbPropertyBag.h:78
PropertyHashToArrayFunc(const nsAString &aKey, nsIVariant *aData, void *userArg)
this _dialogInput val(dateText)
static nsresult GetProperty(nsIPropertyBag2 *aProperties, nsAString const &aProp, nsAString &aValue)
function Init()
#define IMPL_GETSETPROPERTY_AS(Name, Type)
_updateCookies aName
countRef value
Definition: FeedWriter.js:1423
nsresult Init()
_updateTextAndScrollDataForFrame aData