31 #include <nsAutoLock.h>
32 #include <nsComponentManagerUtils.h>
33 #include <nsIObserverService.h>
34 #include <nsIProxyObjectManager.h>
35 #include <nsServiceManagerUtils.h>
36 #include <nsUnicharUtils.h>
38 #include <nsXPCOMCIDInternal.h>
41 #include <sbIPropertyInfo.h>
42 #include <sbIPropertyManager.h>
61 PRUint32 aMediaItemId,
62 const nsAString &aGuid)
65 , mMediaItemId(aMediaItemId)
80 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
83 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
86 NS_ENSURE_SUCCESS(rv, rv);
89 do_GetService(
"@songbirdnest.com/Songbird/IdentityService;1", &rv);
90 NS_ENSURE_SUCCESS(rv, rv);
95 PLDHashOperator PR_CALLBACK
96 sbLocalDatabaseResourcePropertyBag::PropertyBagKeysToArray(
const PRUint32& aPropertyID,
100 nsTArray<PRUint32>* propertyIDs =
static_cast<nsTArray<PRUint32>*
>(aArg);
101 if (propertyIDs->AppendElement(aPropertyID)) {
102 return PL_DHASH_NEXT;
105 return PL_DHASH_STOP;
110 sbLocalDatabaseResourcePropertyBag::GetGuid(nsAString &aGuid)
117 sbLocalDatabaseResourcePropertyBag::GetMediaItemId(PRUint32* aMediaItemId)
119 NS_ENSURE_ARG_POINTER(aMediaItemId);
120 *aMediaItemId = mMediaItemId;
127 NS_ENSURE_ARG_POINTER(aIDs);
129 nsTArray<PRUint32> propertyDBIDs;
130 mValueMap.EnumerateRead(PropertyBagKeysToArray, &propertyDBIDs);
132 PRUint32 len = mValueMap.Count();
133 if (propertyDBIDs.Length() < len) {
134 return NS_ERROR_OUT_OF_MEMORY;
137 nsTArray<nsString> propertyIDs;
138 for (PRUint32
i = 0;
i < len;
i++) {
140 PRBool success = mCache->
GetPropertyID(propertyDBIDs[
i], propertyID);
141 NS_ENSURE_TRUE(success, NS_ERROR_UNEXPECTED);
142 propertyIDs.AppendElement(propertyID);
146 NS_ENSURE_TRUE(*aIDs, NS_ERROR_OUT_OF_MEMORY);
156 PRUint32 propertyDBID = mCache->GetPropertyDBIDInternal(aPropertyID);
157 return GetPropertyByID(propertyDBID, _retval);
161 sbLocalDatabaseResourcePropertyBag::GetPropertyByID(PRUint32 aPropertyDBID,
164 if(aPropertyDBID > 0) {
165 nsAutoMonitor mon(mCache->mMonitor);
169 if (mValueMap.Get(aPropertyDBID, &data)) {
170 _retval.Assign(data->
value);
176 _retval.SetIsVoid(PR_TRUE);
181 sbLocalDatabaseResourcePropertyBag::GetSortablePropertyByID(PRUint32 aPropertyDBID,
184 if(aPropertyDBID > 0) {
185 nsAutoMonitor mon(mCache->mMonitor);
188 if (mValueMap.Get(aPropertyDBID, &data)) {
194 PRBool success = mCache->
GetPropertyID(aPropertyDBID, propertyID);
195 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
196 nsCOMPtr<sbIPropertyInfo> propertyInfo;
197 nsresult rv = mPropertyManager->GetPropertyInfo(propertyID,
198 getter_AddRefs(propertyInfo));
199 NS_ENSURE_SUCCESS(rv, rv);
202 NS_ENSURE_SUCCESS(rv, rv);
210 _retval.SetIsVoid(PR_TRUE);
215 sbLocalDatabaseResourcePropertyBag::
216 GetSearchablePropertyByID(PRUint32 aPropertyDBID,
219 if(aPropertyDBID > 0) {
220 nsAutoMonitor mon(mCache->mMonitor);
223 if (mValueMap.Get(aPropertyDBID, &data)) {
229 PRBool success = mCache->
GetPropertyID(aPropertyDBID, propertyID);
230 NS_ENSURE_TRUE(success, NS_ERROR_FAILURE);
231 nsCOMPtr<sbIPropertyInfo> propertyInfo;
232 nsresult rv = mPropertyManager->GetPropertyInfo(propertyID,
233 getter_AddRefs(propertyInfo));
234 NS_ENSURE_SUCCESS(rv, rv);
237 NS_ENSURE_SUCCESS(rv, rv);
245 _retval.SetIsVoid(PR_TRUE);
250 sbLocalDatabaseResourcePropertyBag::SetProperty(
const nsAString & aPropertyID,
255 PRUint32 propertyDBID = mCache->GetPropertyDBIDInternal(aPropertyID);
256 if(propertyDBID == 0) {
257 rv = mCache->InsertPropertyIDInLibrary(aPropertyID, &propertyDBID);
258 NS_ENSURE_SUCCESS(rv, rv);
261 nsCOMPtr<sbIPropertyInfo> propertyInfo;
262 rv = mPropertyManager->GetPropertyInfo(aPropertyID,
263 getter_AddRefs(propertyInfo));
264 NS_ENSURE_SUCCESS(rv, rv);
266 PRBool valid = PR_FALSE;
267 rv = propertyInfo->Validate(aValue, &valid);
268 NS_ENSURE_SUCCESS(rv, rv);
270 #if defined(PR_LOGGING)
271 if(NS_UNLIKELY(!valid)) {
272 LOG(
"Failed to set property id %s with value %s",
273 NS_ConvertUTF16toUTF8(aPropertyID).
get(),
274 NS_ConvertUTF16toUTF8(aValue).
get());
278 NS_ENSURE_TRUE(valid, NS_ERROR_ILLEGAL_VALUE);
282 nsCOMPtr<sbIPropertyArray> dependentProperties;
283 rv = mPropertyManager->GetDependentProperties(aPropertyID,
284 getter_AddRefs(dependentProperties));
285 NS_ENSURE_SUCCESS(rv, rv);
286 PRUint32 dependentPropertyCount;
287 rv = dependentProperties->GetLength(&dependentPropertyCount);
288 NS_ENSURE_SUCCESS(rv, rv);
290 PRUint32 previousDirtyCount = 0;
292 nsAutoMonitor mon(mCache->mMonitor);
294 rv =
PutValue(propertyDBID, aValue);
295 NS_ENSURE_SUCCESS(rv, rv);
297 previousDirtyCount = mDirty.Count();
300 mDirty.PutEntry(propertyDBID);
302 mDirtyForInvalidation.insert(propertyDBID);
306 if (dependentPropertyCount > 0) {
307 for (PRUint32
i = 0;
i < dependentPropertyCount;
i++) {
308 nsCOMPtr<sbIProperty> property;
309 rv = dependentProperties->GetPropertyAt(
i, getter_AddRefs(property));
310 NS_ASSERTION(NS_SUCCEEDED(rv),
311 "Property cache failed to update dependent properties!");
312 if (NS_SUCCEEDED(rv)) {
314 rv =
property->GetId(propertyID);
315 NS_ASSERTION(NS_SUCCEEDED(rv),
316 "Property cache failed to update dependent properties!");
317 if (NS_SUCCEEDED(rv)) {
318 PRUint32 depPropDBID = mCache->GetPropertyDBIDInternal(propertyID);
319 mDirty.PutEntry(depPropDBID);
320 mDirtyForInvalidation.insert(depPropDBID);
329 if (previousDirtyCount == 0) {
330 rv = mCache->AddDirty(mGuid,
this);
331 NS_ENSURE_SUCCESS(rv, rv);
338 PRBool userEditable = PR_FALSE;
339 rv = propertyInfo->GetUserEditable(&userEditable);
340 NS_ENSURE_SUCCESS(rv, rv);
346 NS_ERROR_UNEXPECTED);
351 NS_ENSURE_SUCCESS(rv, rv);
357 PRBool usedInIdentity = PR_FALSE;
358 rv = propertyInfo->GetUsedInIdentity(&usedInIdentity);
359 NS_ENSURE_SUCCESS(rv, rv);
361 if (usedInIdentity) {
365 rv = mIdService->CalculateIdentityForBag(
this, identity);
367 if (rv == NS_ERROR_NOT_AVAILABLE) {
370 NS_ENSURE_SUCCESS(rv, rv);
373 rv = mIdService->SaveIdentityToBag(
this, identity);
374 NS_ENSURE_SUCCESS(rv, rv);
380 NS_IMETHODIMP sbLocalDatabaseResourcePropertyBag::Write()
384 if(mDirty.Count() > 0) {
385 rv = mCache->Write();
386 NS_ENSURE_SUCCESS(rv, rv);
394 const nsAString& aValue)
399 nsAutoMonitor mon(mCache->mMonitor);
400 PRBool success = mValueMap.Put(aPropertyID, data);
401 NS_ENSURE_TRUE(success, NS_ERROR_OUT_OF_MEMORY);
410 if(mDirty.IsInitialized() && mDirty.GetEntry(aPropertyDBID)) {
420 PRUint32 *aDirtyCount)
422 NS_ENSURE_ARG_POINTER(aClosure);
423 NS_ENSURE_ARG_POINTER(aDirtyCount);
425 *aDirtyCount = mDirty.EnumerateEntries(aEnumFunc, aClosure);
432 nsAutoMonitor mon(mCache->mMonitor);
442 if(!mDirtyForInvalidation.empty()) {
443 aDirty = mDirtyForInvalidation;
444 mDirtyForInvalidation.clear();
nsresult EnumerateDirty(nsTHashtable< nsUint32HashKey >::Enumerator aEnumFunc, void *aClosure, PRUint32 *aDirtyCount)
#define SB_PRLOG_SETUP(x)
PRBool IsPropertyDirty(PRUint32 aPropertyDBID)
#define SB_PROPERTYMANAGER_CONTRACTID
PRBool GetPropertyID(PRUint32 aPropertyDBID, nsAString &aPropertyID)
#define SB_PROPERTY_UPDATED
function Enumerator(aItemsList)
static nsresult GetProperty(nsIPropertyBag2 *aProperties, nsAString const &aProp, nsAString &aValue)
[USER CODE SHOULD NOT REFERENCE THIS CLASS]
~sbLocalDatabaseResourcePropertyBag()
NS_IMPL_THREADSAFE_ISUPPORTS1(sbLocalDatabaseResourcePropertyBag, sbILocalDatabaseResourcePropertyBag) sbLocalDatabaseResourcePropertyBag
PRUint32 const BAG_HASHTABLE_SIZE
_getSelectedPageStyle s i
nsresult PutValue(PRUint32 aPropertyID, const nsAString &aValue)
nsresult GetDirtyForInvalidation(std::set< PRUint32 > &aDirty)