diff --git a/netwerk/cookie/nsCookie.cpp b/netwerk/cookie/nsCookie.cpp index 8b8fa011abe..522b7b9e773 100644 --- a/netwerk/cookie/nsCookie.cpp +++ b/netwerk/cookie/nsCookie.cpp @@ -83,6 +83,20 @@ StrBlockCopy(const nsACString &aSource1, // check each id to enforce monotonicity. static PRInt64 gLastCreationID; +PRInt64 +nsCookie::GenerateCreationID(PRInt64 aCreationTime) +{ + // Check if the creation time given to us is greater than the running maximum + // (it should always be monotonically increasing). + if (aCreationTime > gLastCreationID) { + gLastCreationID = aCreationTime; + return aCreationTime; + } + + // Make up our own. + return ++gLastCreationID; +} + nsCookie * nsCookie::Create(const nsACString &aName, const nsACString &aValue, @@ -111,12 +125,9 @@ nsCookie::Create(const nsACString &aName, StrBlockCopy(aName, aValue, aHost, aPath, name, value, host, path, end); - // check if the creation id given to us is greater than the running maximum - // (it should always be monotonically increasing). if it's not, make up our own. + // If the creationID given to us is higher than the running maximum, update it. if (aCreationID > gLastCreationID) gLastCreationID = aCreationID; - else - aCreationID = ++gLastCreationID; // construct the cookie. placement new, oh yeah! return new (place) nsCookie(name, value, host, path, end, diff --git a/netwerk/cookie/nsCookie.h b/netwerk/cookie/nsCookie.h index 08a1ca48997..dde12e178de 100644 --- a/netwerk/cookie/nsCookie.h +++ b/netwerk/cookie/nsCookie.h @@ -90,6 +90,11 @@ class nsCookie : public nsICookie2 } public: + // Generate a unique creationID. This will usually be the same as the + // creation time, but with a guarantee of monotonicity such that it can + // be used as a sqlite rowid. + static PRInt64 GenerateCreationID(PRInt64 aCreationTime); + // public helper to create an nsCookie object. use |operator delete| // to destroy an object created by this method. static nsCookie * Create(const nsACString &aName, diff --git a/netwerk/cookie/nsCookieService.cpp b/netwerk/cookie/nsCookieService.cpp index 821fead5301..b5a8fb789bb 100644 --- a/netwerk/cookie/nsCookieService.cpp +++ b/netwerk/cookie/nsCookieService.cpp @@ -852,6 +852,7 @@ nsCookieService::TryInitDB(PRBool aDeleteExistingDB) "host, " "path, " "expiry, " + "lastAccessed, " "isSecure, " "isHttpOnly " "FROM moz_cookies"), getter_AddRefs(stmt)); @@ -1337,7 +1338,7 @@ nsCookieService::Add(const nsACString &aHost, nsCookie::Create(aName, aValue, host, aPath, aExpiry, currentTimeInUsec, - currentTimeInUsec, + nsCookie::GenerateCreationID(currentTimeInUsec), aIsSession, aIsSecure, aIsHttpOnly); @@ -1454,7 +1455,8 @@ nsCookieService::GetCookieFromRow(T &aRow) PRBool isSecure = 0 != aRow->AsInt32(7); PRBool isHttpOnly = 0 != aRow->AsInt32(8); - // create a new nsCookie and assign the data. + // Create a new nsCookie and assign the data. We are guaranteed that the + // creationID is unique, since we're reading it from the db itself. return nsCookie::Create(name, value, host, path, expiry, lastAccessed, @@ -1784,10 +1786,9 @@ nsCookieService::ImportCookies(nsIFile *aCookieFile) if (NS_FAILED(rv)) continue; - // create a new nsCookie and assign the data. - // we don't know the cookie creation time, so just use the current time; - // this is okay, since nsCookie::Create() will make sure the creation id - // ends up monotonically increasing. + // Create a new nsCookie and assign the data. + // We don't know the cookie creation time, so just use the current time + // to generate a unique creationID. nsRefPtr newCookie = nsCookie::Create(Substring(buffer, nameIndex, cookieIndex - nameIndex - 1), Substring(buffer, cookieIndex, buffer.Length() - cookieIndex), @@ -1795,7 +1796,7 @@ nsCookieService::ImportCookies(nsIFile *aCookieFile) Substring(buffer, pathIndex, secureIndex - pathIndex - 1), expires, lastAccessedCounter, - currentTimeInUsec, + nsCookie::GenerateCreationID(currentTimeInUsec), PR_FALSE, Substring(buffer, secureIndex, expiresIndex - secureIndex - 1).EqualsLiteral(kTrue), isHttpOnly); @@ -2119,7 +2120,7 @@ nsCookieService::SetCookieInternal(nsIURI *aHostURI, cookieAttributes.path, cookieAttributes.expiryTime, currentTimeInUsec, - currentTimeInUsec, + nsCookie::GenerateCreationID(currentTimeInUsec), cookieAttributes.isSession, cookieAttributes.isSecure, cookieAttributes.isHttpOnly);