Bug 707915 - Broken invalidation of mCachedUsage in nsDOMStoragePersistentDB
authorMichal Novotny <michal.novotny@gmail.com>
Fri, 16 Dec 2011 21:33:51 +0100
changeset 84452 61a46d539c69e9ff34a02074c1d83ae96ea84ab8
parent 84451 fb4d12d2a2dafb62a820dafda7e5fc231ffb5179
child 84453 fbf296760b0f3e79dc7ef2fcef70745a4e7ebc9b
push id519
push userakeybl@mozilla.com
push dateWed, 01 Feb 2012 00:38:35 +0000
treeherdermozilla-beta@788ea1ef610b [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
bugs707915
milestone11.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 707915 - Broken invalidation of mCachedUsage in nsDOMStoragePersistentDB
dom/src/storage/nsDOMStoragePersistentDB.cpp
dom/src/storage/nsDOMStoragePersistentDB.h
--- a/dom/src/storage/nsDOMStoragePersistentDB.cpp
+++ b/dom/src/storage/nsDOMStoragePersistentDB.cpp
@@ -739,17 +739,17 @@ nsDOMStoragePersistentDB::SetKey(DOMStor
 
   rv = binder.Add();
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = mInsertKeyStatement->Execute();
   NS_ENSURE_SUCCESS(rv, rv);
 
   if (!aStorage->GetQuotaDomainDBKey(!aExcludeOfflineFromUsage).IsEmpty()) {
-    mCachedOwner = aStorage->GetQuotaDomainDBKey(!aExcludeOfflineFromUsage);
+    // No need to set mCachedOwner since it was set by GetUsage()
     mCachedUsage = usage;
   }
 
   *aNewUsage = usage;
 
   MarkScopeDirty(aStorage);
 
   return NS_OK;
@@ -802,18 +802,20 @@ nsDOMStoragePersistentDB::RemoveKey(DOMS
 {
   nsresult rv;
 
   rv = MaybeCommitInsertTransaction();
   NS_ENSURE_SUCCESS(rv, rv);
 
   mozStorageStatementScoper scope(mRemoveKeyStatement);
 
-  if (aStorage->GetQuotaDomainDBKey(!aExcludeOfflineFromUsage) == mCachedOwner) {
-    mCachedUsage -= aKeyUsage;
+  if (DomainMaybeCached(
+      aStorage->GetQuotaDomainDBKey(!aExcludeOfflineFromUsage))) {
+    mCachedUsage = 0;
+    mCachedOwner.Truncate();
   }
 
   Binder binder(mRemoveKeyStatement, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = binder->BindUTF8StringByName(NS_LITERAL_CSTRING("scope"),
                                     aStorage->GetScopeDBKey());
   NS_ENSURE_SUCCESS(rv, rv);
@@ -872,25 +874,25 @@ nsDOMStoragePersistentDB::RemoveOwner(co
   rv = MaybeCommitInsertTransaction();
   NS_ENSURE_SUCCESS(rv, rv);
 
   mozStorageStatementScoper scope(mRemoveOwnerStatement);
 
   nsCAutoString subdomainsDBKey;
   nsDOMStorageDBWrapper::CreateDomainScopeDBKey(aOwner, subdomainsDBKey);
 
+  if (DomainMaybeCached(subdomainsDBKey)) {
+    mCachedUsage = 0;
+    mCachedOwner.Truncate();
+  }
+
   if (!aIncludeSubDomains)
     subdomainsDBKey.AppendLiteral(":");
   subdomainsDBKey.AppendLiteral("*");
 
-  if (subdomainsDBKey == mCachedOwner) {
-    mCachedUsage = 0;
-    mCachedOwner.Truncate();
-  }
-
   Binder binder(mRemoveOwnerStatement, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = binder->BindUTF8StringByName(NS_LITERAL_CSTRING("scope"),
                                     subdomainsDBKey);
   NS_ENSURE_SUCCESS(rv, rv);
 
   rv = binder.Add();
@@ -955,16 +957,21 @@ nsDOMStoragePersistentDB::RemoveOwners(c
   Binder binder(statement, &rv);
   NS_ENSURE_SUCCESS(rv, rv);
 
   for (PRUint32 i = 0; i < aOwners.Length(); i++) {
     nsCAutoString quotaKey;
     rv = nsDOMStorageDBWrapper::CreateDomainScopeDBKey(
       NS_ConvertUTF16toUTF8(aOwners[i]), quotaKey);
 
+    if (DomainMaybeCached(quotaKey)) {
+      mCachedUsage = 0;
+      mCachedOwner.Truncate();
+    }
+
     if (!aIncludeSubDomains)
       quotaKey.AppendLiteral(":");
     quotaKey.AppendLiteral("*");
 
     nsCAutoString paramName;
     paramName.Assign("scope");
     paramName.AppendInt(i);
 
@@ -1115,8 +1122,22 @@ nsDOMStoragePersistentDB::MaybeCommitIns
   if (NS_FAILED(rv) || !transactionInProgress)
     return NS_OK;
 
   rv = mConnection->CommitTransaction();
   NS_ENSURE_SUCCESS(rv, rv);
 
   return NS_OK;
 }
+
+bool
+nsDOMStoragePersistentDB::DomainMaybeCached(const nsACString& aDomain)
+{
+  if (mCachedOwner.IsEmpty())
+    return false;
+
+  // if cached owner contains colon we must ignore it
+  if (mCachedOwner[mCachedOwner.Length() - 1] == ':')
+    return StringBeginsWith(aDomain, Substring(mCachedOwner, 0,
+                                               mCachedOwner.Length() - 1));
+  else
+    return StringBeginsWith(aDomain, mCachedOwner);
+}
--- a/dom/src/storage/nsDOMStoragePersistentDB.h
+++ b/dom/src/storage/nsDOMStoragePersistentDB.h
@@ -214,11 +214,16 @@ protected:
   // If a record is present, the temp table has been loaded. If it is not
   // present, the table has not yet been loaded or has alrady been flushed.
   nsDataHashtable<nsCStringHashKey, TimeStamp> mTempTableLoads; 
 
   friend class nsDOMStorageDBWrapper;
   friend class nsDOMStorageMemoryDB;
   nsresult
   GetUsageInternal(const nsACString& aQuotaDomainDBKey, bool aExcludeOfflineFromUsage, PRInt32 *aUsage);
+
+  // Compares aDomain with the mCachedOwner and returns false if changes
+  // in aDomain don't affect mCachedUsage.
+  bool DomainMaybeCached(const nsACString& aDomain);
+
 };
 
 #endif /* nsDOMStorageDB_h___ */