29 #include <nsIClassInfoImpl.h>
30 #include <nsIObjectInputStream.h>
31 #include <nsIObjectOutputStream.h>
32 #include <nsIProgrammingLanguage.h>
33 #include <nsISimpleEnumerator.h>
34 #include <sbIPropertyManager.h>
35 #include <sbIPropertyInfo.h>
37 #include <nsArrayEnumerator.h>
39 #include <nsComponentManagerUtils.h>
41 #include <nsServiceManagerUtils.h>
50 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(
nsISupports, nsIMutableArray)
53 NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsIArray, nsIMutableArray)
55 NS_INTERFACE_MAP_ENTRY(nsIMutableArray)
67 sbPropertyArray::sbPropertyArray()
76 nsAutoLock::DestroyLock(mArrayLock);
83 mArrayLock = nsAutoLock::NewLock(
"sbPropertyArray::mArrayLock");
84 NS_ENSURE_TRUE(mArrayLock, NS_ERROR_OUT_OF_MEMORY);
93 sbPropertyArray::PropertyIsValid(
sbIProperty* aProperty,
96 NS_ASSERTION(aProperty,
"Don't hand me a null!");
97 NS_ASSERTION(_retval,
"Don't hand me a null!");
100 nsresult rv = aProperty->GetId(
id);
101 NS_ENSURE_SUCCESS(rv, rv);
104 rv = aProperty->GetValue(value);
105 NS_ENSURE_SUCCESS(rv, rv);
107 return ValueIsValid(
id, value, _retval);
115 sbPropertyArray::ValueIsValid(
const nsAString& aID,
119 NS_ASSERTION(!aID.IsEmpty(),
"Don't pass an empty property id!");
120 NS_ASSERTION(_retval,
"Don't hand me a null!");
122 if (aValue.IsVoid()) {
131 NS_ENSURE_SUCCESS(rv, rv);
136 nsCOMPtr<sbIPropertyInfo> propInfo;
137 rv = mPropManager->GetPropertyInfo(aID, getter_AddRefs(propInfo));
138 NS_ENSURE_SUCCESS(rv, rv);
141 rv = propInfo->Validate(aValue, &valid);
142 NS_ENSURE_SUCCESS(rv, rv);
152 sbPropertyArray::GetLength(PRUint32* aLength)
154 NS_ENSURE_ARG_POINTER(aLength);
156 nsAutoLock lock(mArrayLock);
157 *aLength = (PRUint32)mArray.Count();
165 sbPropertyArray::QueryElementAt(PRUint32 aIndex,
169 NS_ENSURE_ARG(aIndex < static_cast<PRUint32>(mArray.Count()));
170 NS_ENSURE_ARG_POINTER(_retval);
172 nsAutoLock lock(mArrayLock);
173 nsCOMPtr<nsISupports>
element = mArray.ObjectAt(aIndex);
174 NS_ENSURE_STATE(element);
176 return element->QueryInterface(aIID, _retval);
183 sbPropertyArray::IndexOf(PRUint32 aStartIndex,
187 NS_ENSURE_ARG_POINTER(aElement);
188 NS_ENSURE_ARG_POINTER(_retval);
195 NS_WARNING(
"sbPropertyArray::IndexOf ignores aStartIndex parameter!");
200 nsCOMPtr<sbIProperty>
property = do_QueryInterface(aElement, &rv);
201 NS_ENSURE_SUCCESS(rv, rv);
203 nsAutoLock lock(mArrayLock);
204 PRInt32 index = mArray.IndexOf(property);
205 NS_ENSURE_TRUE(index >= 0, NS_ERROR_NOT_AVAILABLE);
207 *_retval = (PRUint32)index;
217 NS_ENSURE_ARG_POINTER(_retval);
220 nsCOMPtr<nsIMutableArray>
array =
221 do_CreateInstance(
"@songbirdnest.com/moz/xpcom/threadsafe-array;1", &rv);
222 NS_ENSURE_TRUE(array, NS_ERROR_OUT_OF_MEMORY);
224 nsAutoLock lock(mArrayLock);
225 PRUint32 length = mArray.Count();
226 for (PRUint32
i = 0;
i < length;
i++) {
227 rv = array->AppendElement(mArray.ObjectAt(
i), PR_FALSE);
228 NS_ENSURE_SUCCESS(rv, rv);
231 return NS_NewArrayEnumerator(_retval, array);
238 sbPropertyArray::AppendElement(
nsISupports* aElement,
241 NS_ENSURE_ARG_POINTER(aElement);
244 NS_ENSURE_FALSE(aWeak, NS_ERROR_FAILURE);
247 nsCOMPtr<sbIProperty>
property = do_QueryInterface(aElement, &rv);
248 NS_ENSURE_SUCCESS(rv, rv);
250 nsAutoLock lock(mArrayLock);
254 rv = PropertyIsValid(property, &valid);
255 NS_ENSURE_SUCCESS(rv, rv);
257 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
260 PRBool success = mArray.AppendObject(property);
261 NS_ENSURE_STATE(success);
270 sbPropertyArray::RemoveElementAt(PRUint32 aIndex)
272 NS_ENSURE_ARG(aIndex < static_cast<PRUint32>(mArray.Count()));
274 nsAutoLock lock(mArrayLock);
275 PRBool success = mArray.RemoveObjectAt(aIndex);
276 NS_ENSURE_STATE(success);
284 sbPropertyArray::InsertElementAt(
nsISupports* aElement,
288 NS_ENSURE_ARG_POINTER(aElement);
289 NS_ENSURE_ARG_MAX((PRInt32)aIndex, mArray.Count());
292 NS_ENSURE_FALSE(aWeak, NS_ERROR_FAILURE);
295 nsCOMPtr<sbIProperty>
property = do_QueryInterface(aElement, &rv);
296 NS_ENSURE_SUCCESS(rv, rv);
298 nsAutoLock lock(mArrayLock);
302 rv = PropertyIsValid(property, &valid);
303 NS_ENSURE_SUCCESS(rv, rv);
305 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
308 PRBool success = mArray.InsertObjectAt(property, aIndex);
309 NS_ENSURE_STATE(success);
318 sbPropertyArray::ReplaceElementAt(
nsISupports* aElement,
322 NS_ENSURE_ARG_POINTER(aElement);
323 NS_ENSURE_ARG(aIndex < static_cast<PRUint32>(mArray.Count()));
326 NS_ENSURE_FALSE(aWeak, NS_ERROR_NOT_IMPLEMENTED);
329 nsCOMPtr<sbIProperty>
property = do_QueryInterface(aElement, &rv);
330 NS_ENSURE_SUCCESS(rv, rv);
332 nsAutoLock lock(mArrayLock);
336 rv = PropertyIsValid(property, &valid);
337 NS_ENSURE_SUCCESS(rv, rv);
339 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
342 PRBool success = mArray.ReplaceObjectAt(property, aIndex);
343 NS_ENSURE_STATE(success);
352 sbPropertyArray::Clear()
354 nsAutoLock lock(mArrayLock);
363 sbPropertyArray::AppendProperty(
const nsAString& aID,
364 const nsAString& aValue)
366 NS_ENSURE_TRUE(!aID.IsEmpty(), NS_ERROR_INVALID_ARG);
368 nsAutoLock lock(mArrayLock);
372 nsresult rv = ValueIsValid(aID, aValue, &valid);
373 NS_ENSURE_SUCCESS(rv, rv);
375 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
379 NS_ENSURE_TRUE(property, NS_ERROR_OUT_OF_MEMORY);
381 PRBool success = mArray.AppendObject(property);
382 NS_ENSURE_STATE(success);
392 PRBool aSkipDuplicates)
394 NS_ENSURE_ARG_POINTER(aPropertyArray);
396 PRUint32 propertyCount;
399 nsAutoLock lock(mArrayLock);
403 nsTArray<nsString> currentPropertyIDList;
404 if (aSkipDuplicates) {
405 propertyCount = mArray.Count();
406 NS_ENSURE_TRUE(currentPropertyIDList.SetCapacity(propertyCount),
407 NS_ERROR_OUT_OF_MEMORY);
408 for (PRUint32
i = 0;
i < propertyCount;
i++) {
409 nsCOMPtr<sbIProperty>
property = mArray[
i];
412 rv =
property->GetId(
id);
413 NS_ENSURE_SUCCESS(rv, rv);
414 NS_ENSURE_TRUE(currentPropertyIDList.AppendElement(
id),
415 NS_ERROR_OUT_OF_MEMORY);
420 rv = aPropertyArray->GetLength(&propertyCount);
421 NS_ENSURE_SUCCESS(rv, rv);
422 for (PRUint32
i = 0;
i < propertyCount;
i++) {
424 nsCOMPtr<sbIProperty> property;
425 rv = aPropertyArray->GetPropertyAt(
i, getter_AddRefs(property));
426 NS_ENSURE_SUCCESS(rv, rv);
431 rv =
property->GetId(
id);
432 NS_ENSURE_SUCCESS(rv, rv);
433 rv =
property->GetValue(value);
434 NS_ENSURE_SUCCESS(rv, rv);
439 rv = ValueIsValid(
id, value, &valid);
440 NS_ENSURE_SUCCESS(rv, rv);
441 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
445 if (aSkipDuplicates && currentPropertyIDList.Contains(
id)) {
451 PRBool success = mArray.AppendObject(property);
452 NS_ENSURE_STATE(success);
453 NS_ENSURE_TRUE(currentPropertyIDList.AppendElement(
id),
454 NS_ERROR_OUT_OF_MEMORY);
464 sbPropertyArray::SetStrict(PRBool aStrict)
466 nsAutoLock lock(mArrayLock);
468 if ((aStrict != mStrict) && mArray.Count()) {
469 NS_WARNING(
"Can't change strict setting of a non-empty sbPropertyArray!");
470 return NS_ERROR_FAILURE;
473 if (mStrict == aStrict) {
478 if (!aStrict && mPropManager) {
479 mPropManager = nsnull;
490 sbPropertyArray::GetStrict(PRBool* aStrict)
492 NS_ENSURE_ARG_POINTER(aStrict);
494 nsAutoLock lock(mArrayLock);
504 sbPropertyArray::GetPropertyAt(PRUint32 aIndex,
507 NS_ENSURE_ARG(aIndex < static_cast<PRUint32>(mArray.Count()));
508 NS_ENSURE_ARG_POINTER(_retval);
510 nsAutoLock lock(mArrayLock);
511 nsCOMPtr<sbIProperty>
property = mArray.ObjectAt(aIndex);
512 NS_ENSURE_STATE(property);
514 NS_ADDREF(*_retval = property);
522 sbPropertyArray::GetPropertyValue(
const nsAString& aID,
527 nsAutoLock lock(mArrayLock);
528 PRUint32 length = mArray.Count();
529 for (PRUint32
i = 0;
i < length;
i++) {
530 nsCOMPtr<sbIProperty>
property = mArray.ObjectAt(
i);
531 NS_ENSURE_STATE(property);
533 nsAutoString propertyID;
534 rv =
property->GetId(propertyID);
535 NS_ENSURE_SUCCESS(rv, rv);
537 if (propertyID.Equals(aID)) {
538 rv =
property->GetValue(_retval);
539 NS_ENSURE_SUCCESS(rv, rv);
544 return NS_ERROR_NOT_AVAILABLE;
555 nsAutoLock lock(mArrayLock);
558 buff.AssignLiteral(
"[");
560 PRUint32 length = mArray.Count();
561 for (PRUint32
i = 0;
i < length;
i++) {
562 nsCOMPtr<sbIProperty>
property = mArray.ObjectAt(
i);
563 NS_ENSURE_STATE(property);
565 nsAutoString propertyID;
566 rv =
property->GetId(propertyID);
567 NS_ENSURE_SUCCESS(rv, rv);
570 rv =
property->GetValue(value);
571 NS_ENSURE_SUCCESS(rv, rv);
573 buff.AppendLiteral(
"'");
574 buff.Append(propertyID);
575 buff.AppendLiteral(
"' => ");
577 buff.AppendLiteral(
"'");
579 buff.AppendLiteral(
"'");
581 if (
i + 1 < length) {
582 buff.AppendLiteral(
", ");
586 buff.AppendLiteral(
"]");
596 sbPropertyArray::GetValidated(PRBool* aValidated)
598 return GetStrict(aValidated);
603 sbPropertyArray::Read(nsIObjectInputStream* aStream)
605 NS_ENSURE_ARG_POINTER(aStream);
609 nsAutoLock lock(mArrayLock);
611 rv = aStream->ReadBoolean(&mStrict);
612 NS_ENSURE_SUCCESS(rv, rv);
615 rv = aStream->Read32(&length);
616 NS_ENSURE_SUCCESS(rv, rv);
619 for (PRUint32
i = 0;
i < length;
i++) {
622 rv = aStream->ReadString(
id);
623 NS_ENSURE_SUCCESS(rv, rv);
626 rv = aStream->ReadString(value);
627 NS_ENSURE_SUCCESS(rv, rv);
630 NS_ENSURE_TRUE(property, NS_ERROR_OUT_OF_MEMORY);
632 PRBool success = mArray.AppendObject(property);
633 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
640 sbPropertyArray::Write(nsIObjectOutputStream* aStream)
642 NS_ENSURE_ARG_POINTER(aStream);
646 nsAutoLock lock(mArrayLock);
648 rv = aStream->WriteBoolean(mStrict);
649 NS_ENSURE_SUCCESS(rv, rv);
651 PRUint32 length = mArray.Count();
652 rv = aStream->Write32(length);
653 NS_ENSURE_SUCCESS(rv, rv);
655 for (PRUint32
i = 0;
i < length;
i++) {
656 nsCOMPtr<sbIProperty>
property = mArray.ObjectAt(
i);
657 NS_ENSURE_STATE(property);
660 rv =
property->GetId(
id);
661 NS_ENSURE_SUCCESS(rv, rv);
664 rv =
property->GetValue(value);
665 NS_ENSURE_SUCCESS(rv, rv);
667 rv = aStream->WriteWStringZ(
id.BeginReading());
668 NS_ENSURE_SUCCESS(rv, rv);
670 rv = aStream->WriteWStringZ(value.BeginReading());
671 NS_ENSURE_SUCCESS(rv, rv);
679 sbPropertyArray::GetInterfaces(PRUint32*
count, nsIID*** array)
685 sbPropertyArray::GetHelperForLanguage(PRUint32
language,
693 sbPropertyArray::GetContractID(
char** aContractID)
695 *aContractID = nsnull;
700 sbPropertyArray::GetClassDescription(
char** aClassDescription)
702 *aClassDescription = nsnull;
707 sbPropertyArray::GetClassID(nsCID** aClassID)
714 sbPropertyArray::GetImplementationLanguage(PRUint32* aImplementationLanguage)
716 *aImplementationLanguage = nsIProgrammingLanguage::CPLUSPLUS;
721 sbPropertyArray::GetFlags(PRUint32 *aFlags)
728 sbPropertyArray::GetClassIDNoAlloc(nsCID* aClassIDNoAlloc)
730 NS_ENSURE_ARG_POINTER(aClassIDNoAlloc);
731 *aClassIDNoAlloc = kPropertyArrayCID;
#define SB_MUTABLEPROPERTYARRAY_CID
static nsresult ToString(const nsDiscriminatedUnion &data, nsACString &outString)
NS_IMPL_THREADSAFE_RELEASE(sbRequestItem)
NS_IMPL_THREADSAFE_ADDREF(sbRequestItem)
#define SB_PROPERTYMANAGER_CONTRACTID
An interface to carry around arrays of nsIProperty instances Note that implementations of the interfa...
NS_IMPL_CI_INTERFACE_GETTER6(sbPropertyArray, nsIArray, nsIMutableArray, nsISerializable, sbIPropertyArray, sbIMutablePropertyArray, nsIClassInfo) sbPropertyArray
static NS_INTERFACE_MAP_END NS_DEFINE_CID(kPropertyArrayCID,{0xc7e88d66, 0x5a02, 0x4ab2,{0xba, 0x0b, 0x37, 0x5e, 0x80, 0x2b, 0x05, 0xea}})
An interface to carry around arrays of nsIProperty instances. Users of this interface should only QI ...
_getSelectedPageStyle s i