commit 5b5e2828847907898a837f41b1b6e26a6c2ca20f parent 559e03e9894672752ad434f6578950d9f45e5456 Author: Andrea Marchesini <amarchesini@mozilla.com> Date: Mon, 20 Oct 2025 11:44:22 +0000 Bug 1995027 - Add `USec` or `MSec` suffix to any time-related variable in the cookie code, r=valentin,cookie-reviewers Differential Revision: https://phabricator.services.mozilla.com/D269075 Diffstat:
17 files changed, 173 insertions(+), 155 deletions(-)
diff --git a/dom/base/Document.cpp b/dom/base/Document.cpp @@ -6877,7 +6877,7 @@ void Document::GetCookie(nsAString& aCookie, ErrorResult& aRv) { nsTArray<RefPtr<Cookie>> cookieList; bool stale = false; int64_t currentTimeInUsec = PR_Now(); - int64_t currentTime = currentTimeInUsec / PR_USEC_PER_MSEC; + int64_t currentTimeInMSec = currentTimeInUsec / PR_USEC_PER_MSEC; // not having a cookie service isn't an error nsCOMPtr<nsICookieService> service = @@ -6954,7 +6954,7 @@ void Document::GetCookie(nsAString& aCookie, ErrorResult& aRv) { } // check if the cookie has expired - if (cookie->Expiry() <= currentTime) { + if (cookie->ExpiryInMSec() <= currentTimeInMSec) { continue; } diff --git a/dom/cookiestore/CookieStore.cpp b/dom/cookiestore/CookieStore.cpp @@ -910,7 +910,7 @@ void CookieStore::CookieStructToItem(const CookieStruct& aData, } if (!aData.isSession()) { - aItem->mExpires.Construct(aData.expiry()); + aItem->mExpires.Construct(aData.expiryInMSec()); } else { aItem->mExpires.Construct(nullptr); } diff --git a/netwerk/cookie/Cookie.cpp b/netwerk/cookie/Cookie.cpp @@ -29,18 +29,18 @@ namespace net { // necessary to enforce ordering among cookies whose creation times would // otherwise overlap, since it's possible two cookies may be created at the // same time, or that the system clock isn't monotonic. -static int64_t gLastCreationTime; +static int64_t gLastCreationTimeInUSec; -int64_t Cookie::GenerateUniqueCreationTime(int64_t aCreationTime) { +int64_t Cookie::GenerateUniqueCreationTimeInUSec(int64_t aCreationTimeInUSec) { // Check if the creation time given to us is greater than the running maximum // (it should always be monotonically increasing). - if (aCreationTime > gLastCreationTime) { - gLastCreationTime = aCreationTime; - return aCreationTime; + if (aCreationTimeInUSec > gLastCreationTimeInUSec) { + gLastCreationTimeInUSec = aCreationTimeInUSec; + return aCreationTimeInUSec; } // Make up our own. - return ++gLastCreationTime; + return ++gLastCreationTimeInUSec; } already_AddRefed<Cookie> Cookie::Create( @@ -49,10 +49,10 @@ already_AddRefed<Cookie> Cookie::Create( RefPtr<Cookie> cookie = Cookie::FromCookieStruct(aCookieData, aOriginAttributes); - // If the creationTime given to us is higher than the running maximum, + // If the creationTimeInUSec given to us is higher than the running maximum, // update our maximum. - if (cookie->mData.creationTime() > gLastCreationTime) { - gLastCreationTime = cookie->mData.creationTime(); + if (cookie->mData.creationTimeInUSec() > gLastCreationTimeInUSec) { + gLastCreationTimeInUSec = cookie->mData.creationTimeInUSec(); } return cookie.forget(); @@ -99,34 +99,36 @@ already_AddRefed<Cookie> Cookie::CreateValidated( int64_t currentTimeInUsec = PR_Now(); // Assert that the last creation time is not higher than the current time. // The 10000 wiggle room accounts for the fact that calling - // GenerateUniqueCreationTime might go over the value of PR_Now(), but we'd - // most likely not add 10000 cookies in a row. - MOZ_ASSERT(gLastCreationTime < currentTimeInUsec + 10000, + // GenerateUniqueCreationTimeInUSec might go over the value of PR_Now(), but + // we'd most likely not add 10000 cookies in a row. + MOZ_ASSERT(gLastCreationTimeInUSec < currentTimeInUsec + 10000, "Last creation time must not be higher than NOW"); - // If the creationTime given to us is higher than the current time then + // If the creationTimeInUSec given to us is higher than the current time then // update the creation time to now. - if (cookie->mData.creationTime() > currentTimeInUsec) { + if (cookie->mData.creationTimeInUSec() > currentTimeInUsec) { uint64_t diffInSeconds = - (cookie->mData.creationTime() - currentTimeInUsec) / PR_USEC_PER_SEC; + (cookie->mData.creationTimeInUSec() - currentTimeInUsec) / + PR_USEC_PER_SEC; mozilla::glean::networking::cookie_creation_fixup_diff .AccumulateSingleSample(diffInSeconds); glean::networking::cookie_timestamp_fixed_count.Get("creationTime"_ns) .Add(1); - cookie->mData.creationTime() = - GenerateUniqueCreationTime(currentTimeInUsec); + cookie->mData.creationTimeInUSec() = + GenerateUniqueCreationTimeInUSec(currentTimeInUsec); } - if (cookie->mData.lastAccessed() > currentTimeInUsec) { + if (cookie->mData.lastAccessedInUSec() > currentTimeInUsec) { uint64_t diffInSeconds = - (cookie->mData.lastAccessed() - currentTimeInUsec) / PR_USEC_PER_SEC; + (cookie->mData.lastAccessedInUSec() - currentTimeInUsec) / + PR_USEC_PER_SEC; mozilla::glean::networking::cookie_access_fixup_diff.AccumulateSingleSample( diffInSeconds); glean::networking::cookie_timestamp_fixed_count.Get("lastAccessed"_ns) .Add(1); - cookie->mData.lastAccessed() = currentTimeInUsec; + cookie->mData.lastAccessedInUSec() = currentTimeInUsec; } return cookie.forget(); @@ -143,7 +145,7 @@ size_t Cookie::SizeOfIncludingThis(mozilla::MallocSizeOf aMallocSizeOf) const { bool Cookie::IsStale() const { int64_t currentTimeInUsec = PR_Now(); - return currentTimeInUsec - LastAccessed() > + return currentTimeInUsec - LastAccessedInUSec() > StaticPrefs::network_cookie_staleThreshold() * PR_USEC_PER_SEC; } @@ -174,7 +176,7 @@ NS_IMETHODIMP Cookie::GetPath(nsACString& aPath) { return NS_OK; } NS_IMETHODIMP Cookie::GetExpiry(int64_t* aExpiry) { - *aExpiry = Expiry(); + *aExpiry = ExpiryInMSec(); return NS_OK; } NS_IMETHODIMP Cookie::GetIsSession(bool* aIsSession) { @@ -198,11 +200,11 @@ NS_IMETHODIMP Cookie::GetIsPartitioned(bool* aPartitioned) { return NS_OK; } NS_IMETHODIMP Cookie::GetCreationTime(int64_t* aCreation) { - *aCreation = CreationTime(); + *aCreation = CreationTimeInUSec(); return NS_OK; } NS_IMETHODIMP Cookie::GetLastAccessed(int64_t* aTime) { - *aTime = LastAccessed(); + *aTime = LastAccessedInUSec(); return NS_OK; } NS_IMETHODIMP Cookie::GetSameSite(int32_t* aSameSite) { @@ -235,7 +237,7 @@ Cookie::GetExpires(uint64_t* aExpires) { if (IsSession()) { *aExpires = 0; } else { - *aExpires = Expiry() > 0 ? Expiry() : 1; + *aExpires = ExpiryInMSec() > 0 ? ExpiryInMSec() : 1; } return NS_OK; } diff --git a/netwerk/cookie/Cookie.h b/netwerk/cookie/Cookie.h @@ -59,16 +59,16 @@ class Cookie final : public nsICookie { public: // Generate a unique and monotonically increasing creation time. See comment // in Cookie.cpp. - static int64_t GenerateUniqueCreationTime(int64_t aCreationTime); + static int64_t GenerateUniqueCreationTimeInUSec(int64_t aCreationTimeInUSec); // public helper to create an Cookie object. static already_AddRefed<Cookie> Create( const CookieStruct& aCookieData, const OriginAttributes& aOriginAttributes); - // Same as Cookie::Create but fixes the lastAccessed and creationDates - // if they are set in the future. - // Should only get called from CookiePersistentStorage::InitDBConn + // Same as Cookie::Create but fixes the last accessed and creation time + // attributes if they are set in the future. Should only get called from + // CookiePersistentStorage::InitDBConn static already_AddRefed<Cookie> CreateValidated( const CookieStruct& aCookieData, const OriginAttributes& aOriginAttributes); @@ -83,13 +83,13 @@ class Cookie final : public nsICookie { return nsDependentCSubstring(mData.host(), IsDomain() ? 1 : 0); } inline const nsCString& Path() const { return mData.path(); } - inline int64_t Expiry() const { return mData.expiry(); } // in milliseconds - inline int64_t LastAccessed() const { - return mData.lastAccessed(); - } // in microseconds - inline int64_t CreationTime() const { - return mData.creationTime(); - } // in microseconds + inline int64_t ExpiryInMSec() const { return mData.expiryInMSec(); } + inline int64_t LastAccessedInUSec() const { + return mData.lastAccessedInUSec(); + } + inline int64_t CreationTimeInUSec() const { + return mData.creationTimeInUSec(); + } inline bool IsSession() const { return mData.isSession(); } inline bool IsDomain() const { return *mData.host().get() == '.'; } inline bool IsSecure() const { return mData.isSecure(); } @@ -105,15 +105,21 @@ class Cookie final : public nsICookie { inline uint8_t SchemeMap() const { return mData.schemeMap(); } // setters - inline void SetExpiry(int64_t aExpiry) { mData.expiry() = aExpiry; } - inline void SetLastAccessed(int64_t aTime) { mData.lastAccessed() = aTime; } + inline void SetExpiryInMSec(int64_t aExpiryInMSec) { + mData.expiryInMSec() = aExpiryInMSec; + } + inline void SetLastAccessedInUSec(int64_t aTimeInUSec) { + mData.lastAccessedInUSec() = aTimeInUSec; + } inline void SetIsSession(bool aIsSession) { mData.isSession() = aIsSession; } inline bool SetIsHttpOnly(bool aIsHttpOnly) { return mData.isHttpOnly() = aIsHttpOnly; } // Set the creation time manually, overriding the monotonicity checks in // Create(). Use with caution! - inline void SetCreationTime(int64_t aTime) { mData.creationTime() = aTime; } + inline void SetCreationTimeInUSec(int64_t aTimeInUSec) { + mData.creationTimeInUSec() = aTimeInUSec; + } inline void SetSchemeMap(uint8_t aSchemeMap) { mData.schemeMap() = aSchemeMap; } @@ -145,8 +151,8 @@ class Cookie final : public nsICookie { class CompareCookiesForSending { public: bool Equals(const nsICookie* aCookie1, const nsICookie* aCookie2) const { - return Cookie::Cast(aCookie1)->CreationTime() == - Cookie::Cast(aCookie2)->CreationTime() && + return Cookie::Cast(aCookie1)->CreationTimeInUSec() == + Cookie::Cast(aCookie2)->CreationTimeInUSec() && Cookie::Cast(aCookie2)->Path().Length() == Cookie::Cast(aCookie1)->Path().Length(); } @@ -161,8 +167,8 @@ class CompareCookiesForSending { // required for backwards compatibility since some websites erroneously // depend on receiving cookies in the order in which they were sent to the // browser! see bug 236772. - return Cookie::Cast(aCookie1)->CreationTime() < - Cookie::Cast(aCookie2)->CreationTime(); + return Cookie::Cast(aCookie1)->CreationTimeInUSec() < + Cookie::Cast(aCookie2)->CreationTimeInUSec(); } }; diff --git a/netwerk/cookie/CookieCommons.cpp b/netwerk/cookie/CookieCommons.cpp @@ -425,9 +425,9 @@ already_AddRefed<Cookie> CookieCommons::CreateCookieFromDocument( aCookieParser.CookieData(), cookiePrincipal->OriginAttributesRef()); MOZ_ASSERT(cookie); - cookie->SetLastAccessed(currentTimeInUsec); - cookie->SetCreationTime( - Cookie::GenerateUniqueCreationTime(currentTimeInUsec)); + cookie->SetLastAccessedInUSec(currentTimeInUsec); + cookie->SetCreationTimeInUSec( + Cookie::GenerateUniqueCreationTimeInUSec(currentTimeInUsec)); aBaseDomain = baseDomain; aAttrs = cookiePrincipal->OriginAttributesRef(); diff --git a/netwerk/cookie/CookieLogging.cpp b/netwerk/cookie/CookieLogging.cpp @@ -113,7 +113,7 @@ void CookieLogging::LogCookie(Cookie* aCookie) { aCookie->Host().get())); MOZ_LOG(gCookieLog, LogLevel::Debug, ("path: %s\n", aCookie->Path().get())); - PR_ExplodeTime(aCookie->Expiry() * int64_t(PR_USEC_PER_MSEC), + PR_ExplodeTime(aCookie->ExpiryInMSec() * int64_t(PR_USEC_PER_MSEC), PR_GMTParameters, &explodedTime); PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", &explodedTime); @@ -121,7 +121,8 @@ void CookieLogging::LogCookie(Cookie* aCookie) { ("expires: %s%s", timeString, aCookie->IsSession() ? " (at end of session)" : "")); - PR_ExplodeTime(aCookie->CreationTime(), PR_GMTParameters, &explodedTime); + PR_ExplodeTime(aCookie->CreationTimeInUSec(), PR_GMTParameters, + &explodedTime); PR_FormatTimeUSEnglish(timeString, TIME_STRING_LENGTH, "%c GMT", &explodedTime); MOZ_LOG(gCookieLog, LogLevel::Debug, ("created: %s", timeString)); diff --git a/netwerk/cookie/CookieParser.cpp b/netwerk/cookie/CookieParser.cpp @@ -501,7 +501,7 @@ bool CookieParser::GetExpiry(CookieStruct& aCookieData, const nsACString& aDateHeader, bool aFromHttp) { int64_t maxageCap = StaticPrefs::network_cookie_maxageCap(); int64_t creationTimeInMSec = - aCookieData.creationTime() / int64_t(PR_USEC_PER_MSEC); + aCookieData.creationTimeInUSec() / int64_t(PR_USEC_PER_MSEC); /* Determine when the cookie should expire. This is done by taking the * difference between the server time and the time the server wants the cookie @@ -515,12 +515,12 @@ bool CookieParser::GetExpiry(CookieStruct& aCookieData, int64_t maxage = 0; if (ParseMaxAgeAttribute(aMaxage, &maxage)) { if (maxage == INT64_MIN) { - aCookieData.expiry() = maxage; + aCookieData.expiryInMSec() = maxage; } else { CheckedInt<int64_t> value(creationTimeInMSec); value += (maxageCap ? std::min(maxage, maxageCap) : maxage) * 1000; - aCookieData.expiry() = value.isValid() ? value.value() : INT64_MAX; + aCookieData.expiryInMSec() = value.isValid() ? value.value() : INT64_MAX; } return false; @@ -561,7 +561,7 @@ bool CookieParser::GetExpiry(CookieStruct& aCookieData, // time be set less than current time and more than server time. // The cookie item have to be used to the expired cookie. - aCookieData.expiry() = + aCookieData.expiryInMSec() = CookieCommons::MaybeCapExpiry(creationTimeInMSec, expiresInMSec); return false; } @@ -661,9 +661,9 @@ void CookieParser::Parse(const nsACString& aBaseDomain, bool aRequireHostMatch, MOZ_ASSERT(!mValidation); // init expiryTime such that session cookies won't prematurely expire - mCookieData.expiry() = INT64_MAX; - mCookieData.creationTime() = - Cookie::GenerateUniqueCreationTime(aCurrentTimeInUSec); + mCookieData.expiryInMSec() = INT64_MAX; + mCookieData.creationTimeInUSec() = + Cookie::GenerateUniqueCreationTimeInUSec(aCurrentTimeInUSec); mCookieData.schemeMap() = CookieCommons::URIToSchemeType(mHostURI); diff --git a/netwerk/cookie/CookiePersistentStorage.cpp b/netwerk/cookie/CookiePersistentStorage.cpp @@ -35,9 +35,9 @@ constexpr auto IDX_NAME = 0; constexpr auto IDX_VALUE = 1; constexpr auto IDX_HOST = 2; constexpr auto IDX_PATH = 3; -constexpr auto IDX_EXPIRY = 4; -constexpr auto IDX_LAST_ACCESSED = 5; -constexpr auto IDX_CREATION_TIME = 6; +constexpr auto IDX_EXPIRY_INMSEC = 4; +constexpr auto IDX_LAST_ACCESSED_INUSEC = 5; +constexpr auto IDX_CREATION_TIME_INUSEC = 6; constexpr auto IDX_SECURE = 7; constexpr auto IDX_HTTPONLY = 8; constexpr auto IDX_ORIGIN_ATTRIBUTES = 9; @@ -82,13 +82,15 @@ void BindCookieParameters(mozIStorageBindingParamsArray* aParamsArray, rv = params->BindUTF8StringByName("path"_ns, aCookie->Path()); MOZ_ASSERT(NS_SUCCEEDED(rv)); - rv = params->BindInt64ByName("expiry"_ns, aCookie->Expiry()); + rv = params->BindInt64ByName("expiry"_ns, aCookie->ExpiryInMSec()); MOZ_ASSERT(NS_SUCCEEDED(rv)); - rv = params->BindInt64ByName("lastAccessed"_ns, aCookie->LastAccessed()); + rv = + params->BindInt64ByName("lastAccessed"_ns, aCookie->LastAccessedInUSec()); MOZ_ASSERT(NS_SUCCEEDED(rv)); - rv = params->BindInt64ByName("creationTime"_ns, aCookie->CreationTime()); + rv = + params->BindInt64ByName("creationTime"_ns, aCookie->CreationTimeInUSec()); MOZ_ASSERT(NS_SUCCEEDED(rv)); rv = params->BindInt32ByName("isSecure"_ns, aCookie->IsSecure()); @@ -784,12 +786,12 @@ void CookiePersistentStorage::StaleCookies( } void CookiePersistentStorage::UpdateCookieInList( - Cookie* aCookie, int64_t aLastAccessed, + Cookie* aCookie, int64_t aLastAccessedInUSec, mozIStorageBindingParamsArray* aParamsArray) { MOZ_ASSERT(aCookie); - // udpate the lastAccessed timestamp - aCookie->SetLastAccessed(aLastAccessed); + // udpate the lastAccessedInUSec timestamp + aCookie->SetLastAccessedInUSec(aLastAccessedInUSec); // if it's a non-session cookie, update it in the db too if (!aCookie->IsSession() && aParamsArray) { @@ -799,7 +801,7 @@ void CookiePersistentStorage::UpdateCookieInList( // Bind our parameters. DebugOnly<nsresult> rv = - params->BindInt64ByName("lastAccessed"_ns, aLastAccessed); + params->BindInt64ByName("lastAccessed"_ns, aLastAccessedInUSec); MOZ_ASSERT(NS_SUCCEEDED(rv)); rv = params->BindUTF8StringByName("name"_ns, aCookie->Name()); @@ -1920,9 +1922,9 @@ UniquePtr<CookieStruct> CookiePersistentStorage::GetCookieFromRow( rv = aRow->GetUTF8String(IDX_PATH, path); MOZ_ASSERT(NS_SUCCEEDED(rv)); - int64_t expiry = aRow->AsInt64(IDX_EXPIRY); - int64_t lastAccessed = aRow->AsInt64(IDX_LAST_ACCESSED); - int64_t creationTime = aRow->AsInt64(IDX_CREATION_TIME); + int64_t expiryInMSec = aRow->AsInt64(IDX_EXPIRY_INMSEC); + int64_t lastAccessedInUSec = aRow->AsInt64(IDX_LAST_ACCESSED_INUSEC); + int64_t creationTimeInUSec = aRow->AsInt64(IDX_CREATION_TIME_INUSEC); bool isSecure = 0 != aRow->AsInt32(IDX_SECURE); bool isHttpOnly = 0 != aRow->AsInt32(IDX_HTTPONLY); int32_t sameSite = aRow->AsInt32(IDX_SAME_SITE); @@ -1932,8 +1934,9 @@ UniquePtr<CookieStruct> CookiePersistentStorage::GetCookieFromRow( // Create a new constCookie and assign the data. return MakeUnique<CookieStruct>( - name, value, host, path, expiry, lastAccessed, creationTime, isHttpOnly, - false, isSecure, isPartitionedAttributeSet, sameSite, + name, value, host, path, expiryInMSec, lastAccessedInUSec, + creationTimeInUSec, isHttpOnly, false, isSecure, + isPartitionedAttributeSet, sameSite, static_cast<nsICookie::schemeType>(schemeMap)); } @@ -2029,7 +2032,7 @@ void CookiePersistentStorage::InitDBConn() { // CreateValidated fixes up the creation and lastAccessed times. // If the DB is corrupted and the timestaps are far away in the future - // we don't want the creation timestamp to update gLastCreationTime + // we don't want the creation timestamp to update gLastCreationTimeInUSec // as that would contaminate all the next creation times. // We fix up these dates to not be later than the current time. // The downside is that if the user sets the date far away in the past @@ -2414,7 +2417,7 @@ void CookiePersistentStorage::RecordValidationTelemetry() { MOZ_ASSERT(newCookie); newCookie->SetSameSite(nsICookie::SAMESITE_UNSET); - newCookie->SetCreationTime(cookie->CreationTime()); + newCookie->SetCreationTimeInUSec(cookie->CreationTimeInUSec()); listToAdd.AppendElement(CookieToAddOrRemove{ entry.mBaseDomain, entry.mOriginAttributes, newCookie}); @@ -2428,9 +2431,9 @@ void CookiePersistentStorage::RecordValidationTelemetry() { int64_t currentTimeInMSec = PR_Now() / PR_USEC_PER_MSEC; - newCookie->SetExpiry(CookieCommons::MaybeCapExpiry(currentTimeInMSec, - cookie->Expiry())); - newCookie->SetCreationTime(cookie->CreationTime()); + newCookie->SetExpiryInMSec(CookieCommons::MaybeCapExpiry( + currentTimeInMSec, cookie->ExpiryInMSec())); + newCookie->SetCreationTimeInUSec(cookie->CreationTimeInUSec()); listToAdd.AppendElement(CookieToAddOrRemove{ entry.mBaseDomain, entry.mOriginAttributes, newCookie}); @@ -2455,7 +2458,7 @@ void CookiePersistentStorage::RecordValidationTelemetry() { for (CookieToAddOrRemove& data : listToAdd) { AddCookie(nullptr, data.mBaseDomain, data.mOriginAttributes, data.mCookie, - data.mCookie->CreationTime(), nullptr, VoidCString(), true, + data.mCookie->CreationTimeInUSec(), nullptr, VoidCString(), true, !data.mOriginAttributes.mPartitionKey.IsEmpty(), nullptr, nullptr); } diff --git a/netwerk/cookie/CookiePrivateStorage.cpp b/netwerk/cookie/CookiePrivateStorage.cpp @@ -24,7 +24,7 @@ void CookiePrivateStorage::StaleCookies( Cookie* cookie = aCookieList.ElementAt(i); if (cookie->IsStale()) { - cookie->SetLastAccessed(aCurrentTimeInUsec); + cookie->SetLastAccessedInUSec(aCurrentTimeInUsec); } } } diff --git a/netwerk/cookie/CookieService.cpp b/netwerk/cookie/CookieService.cpp @@ -172,7 +172,7 @@ bool ProcessSameSiteCookieForForeignRequest(nsIChannel* aChannel, // without a SameSite value when used for unsafe http methods. if (aLaxByDefault && aCookie->SameSite() == nsICookie::SAMESITE_UNSET && StaticPrefs::network_cookie_sameSite_laxPlusPOST_timeout() > 0 && - currentTimeInUsec - aCookie->CreationTime() <= + currentTimeInUsec - aCookie->CreationTimeInUSec() <= (StaticPrefs::network_cookie_sameSite_laxPlusPOST_timeout() * PR_USEC_PER_SEC) && !NS_IsSafeMethodNav(aChannel)) { @@ -626,9 +626,9 @@ CookieService::SetCookieStringFromHttp(nsIURI* aHostURI, MOZ_ASSERT(cookie); int64_t currentTimeInUsec = PR_Now(); - cookie->SetLastAccessed(currentTimeInUsec); - cookie->SetCreationTime( - Cookie::GenerateUniqueCreationTime(currentTimeInUsec)); + cookie->SetLastAccessedInUSec(currentTimeInUsec); + cookie->SetCreationTimeInUSec( + Cookie::GenerateUniqueCreationTimeInUSec(currentTimeInUsec)); // Use TargetBrowsingContext to also take frame loads into account. RefPtr<BrowsingContext> bc = loadInfo->GetTargetBrowsingContext(); @@ -822,11 +822,11 @@ nsresult CookieService::AddInternal( NS_ENSURE_SUCCESS(rv, rv); int64_t currentTimeInUsec = PR_Now(); - CookieStruct cookieData(nsCString(aName), nsCString(aValue), host, - nsCString(aPath), aExpiry, currentTimeInUsec, - Cookie::GenerateUniqueCreationTime(currentTimeInUsec), - aIsHttpOnly, aIsSession, aIsSecure, aIsPartitioned, - aSameSite, aSchemeMap); + CookieStruct cookieData( + nsCString(aName), nsCString(aValue), host, nsCString(aPath), aExpiry, + currentTimeInUsec, + Cookie::GenerateUniqueCreationTimeInUSec(currentTimeInUsec), aIsHttpOnly, + aIsSession, aIsSecure, aIsPartitioned, aSameSite, aSchemeMap); RefPtr<CookieValidation> cv = CookieValidation::Validate(cookieData); @@ -1012,7 +1012,7 @@ void CookieService::GetCookiesForURI( nsMixedContentBlocker::IsPotentiallyTrustworthyOrigin(aHostURI); int64_t currentTimeInUsec = PR_Now(); - int64_t currentTime = currentTimeInUsec / PR_USEC_PER_MSEC; + int64_t currentTimeInMSec = currentTimeInUsec / PR_USEC_PER_MSEC; bool stale = false; nsTArray<RefPtr<Cookie>> cookies; @@ -1054,7 +1054,7 @@ void CookieService::GetCookiesForURI( } // check if the cookie has expired - if (cookie->Expiry() <= currentTime) { + if (cookie->ExpiryInMSec() <= currentTimeInMSec) { continue; } @@ -1092,8 +1092,8 @@ void CookieService::GetCookiesForURI( } } - // all checks passed - add to list and check if lastAccessed stamp needs - // updating + // all checks passed - add to list and check if lastAccessedInUSec stamp + // needs updating aCookieList.AppendElement(cookie); if (cookie->IsStale()) { stale = true; @@ -1104,8 +1104,8 @@ void CookieService::GetCookiesForURI( continue; } - // update lastAccessed timestamps. we only do this if the timestamp is stale - // by a certain amount, to avoid thrashing the db during pageload. + // update lastAccessedInUSec timestamps. we only do this if the timestamp is + // stale by a certain amount, to avoid thrashing the db during pageload. if (stale) { storage->StaleCookies(aCookieList, currentTimeInUsec); } @@ -1552,7 +1552,7 @@ class RemoveAllSinceRunnable : public Runnable { for (CookieArray::size_type iter = 0; iter < kYieldPeriod && mIndex < mList.Length(); ++mIndex, ++iter) { auto* cookie = static_cast<Cookie*>(mList[mIndex].get()); - if (cookie->CreationTime() > mSinceWhen && + if (cookie->CreationTimeInUSec() > mSinceWhen && NS_FAILED(mSelf->Remove(cookie->Host(), cookie->OriginAttributesRef(), cookie->Name(), cookie->Path(), /* from http: */ true, nullptr))) { @@ -1606,13 +1606,13 @@ namespace { class CompareCookiesCreationTime { public: static bool Equals(const nsICookie* aCookie1, const nsICookie* aCookie2) { - return static_cast<const Cookie*>(aCookie1)->CreationTime() == - static_cast<const Cookie*>(aCookie2)->CreationTime(); + return static_cast<const Cookie*>(aCookie1)->CreationTimeInUSec() == + static_cast<const Cookie*>(aCookie2)->CreationTimeInUSec(); } static bool LessThan(const nsICookie* aCookie1, const nsICookie* aCookie2) { - return static_cast<const Cookie*>(aCookie1)->CreationTime() < - static_cast<const Cookie*>(aCookie2)->CreationTime(); + return static_cast<const Cookie*>(aCookie1)->CreationTimeInUSec() < + static_cast<const Cookie*>(aCookie2)->CreationTimeInUSec(); } }; @@ -1632,7 +1632,8 @@ CookieService::GetCookiesSince(int64_t aSinceWhen, mPersistentStorage->GetAll(cookieList); for (RefPtr<nsICookie>& cookie : cookieList) { - if (static_cast<Cookie*>(cookie.get())->CreationTime() >= aSinceWhen) { + if (static_cast<Cookie*>(cookie.get())->CreationTimeInUSec() >= + aSinceWhen) { aResult.AppendElement(cookie); } } @@ -1727,9 +1728,9 @@ nsICookieValidation::ValidationError CookieService::SetCookiesFromIPC( continue; } - cookie->SetLastAccessed(currentTimeInUsec); - cookie->SetCreationTime( - Cookie::GenerateUniqueCreationTime(currentTimeInUsec)); + cookie->SetLastAccessedInUSec(currentTimeInUsec); + cookie->SetCreationTimeInUSec( + Cookie::GenerateUniqueCreationTimeInUSec(currentTimeInUsec)); storage->AddCookie(nullptr, aBaseDomain, aAttrs, cookie, currentTimeInUsec, aHostURI, ""_ns, aFromHttp, aIsThirdParty, diff --git a/netwerk/cookie/CookieServiceChild.cpp b/netwerk/cookie/CookieServiceChild.cpp @@ -225,7 +225,7 @@ void CookieServiceChild::RemoveSingleCookie(const CookieStruct& aCookie, if (cookie->Name().Equals(aCookie.name()) && cookie->Host().Equals(aCookie.host()) && cookie->Path().Equals(aCookie.path()) && - cookie->Expiry() <= aCookie.expiry()) { + cookie->ExpiryInMSec() <= aCookie.expiryInMSec()) { cookiesList->RemoveElementAt(i); NotifyObservers(cookie, aAttrs, CookieNotificationAction::CookieDeleted, aOperationID); @@ -345,12 +345,12 @@ CookieServiceChild::RecordDocumentCookie(Cookie* aCookie, cookie->Host().Equals(aCookie->Host()) && cookie->Path().Equals(aCookie->Path())) { if (cookie->Value().Equals(aCookie->Value()) && - cookie->Expiry() == aCookie->Expiry() && + cookie->ExpiryInMSec() == aCookie->ExpiryInMSec() && cookie->IsSecure() == aCookie->IsSecure() && cookie->SameSite() == aCookie->SameSite() && cookie->IsSession() == aCookie->IsSession() && cookie->IsHttpOnly() == aCookie->IsHttpOnly()) { - cookie->SetLastAccessed(aCookie->LastAccessed()); + cookie->SetLastAccessedInUSec(aCookie->LastAccessedInUSec()); return CookieNotificationAction::NoActionNeeded; } cookiesList->RemoveElementAt(i); @@ -359,8 +359,8 @@ CookieServiceChild::RecordDocumentCookie(Cookie* aCookie, } } - int64_t currentTime = PR_Now() / PR_USEC_PER_MSEC; - if (aCookie->Expiry() <= currentTime) { + int64_t currentTimeInMSec = PR_Now() / PR_USEC_PER_MSEC; + if (aCookie->ExpiryInMSec() <= currentTimeInMSec) { return cookieFound ? CookieNotificationAction::CookieDeleted : CookieNotificationAction::NoActionNeeded; } diff --git a/netwerk/cookie/CookieStorage.cpp b/netwerk/cookie/CookieStorage.cpp @@ -44,18 +44,20 @@ namespace net { class CookieStorage::CompareCookiesByAge { public: static bool Equals(const CookieListIter& a, const CookieListIter& b) { - return a.Cookie()->LastAccessed() == b.Cookie()->LastAccessed() && - a.Cookie()->CreationTime() == b.Cookie()->CreationTime(); + return a.Cookie()->LastAccessedInUSec() == + b.Cookie()->LastAccessedInUSec() && + a.Cookie()->CreationTimeInUSec() == b.Cookie()->CreationTimeInUSec(); } static bool LessThan(const CookieListIter& a, const CookieListIter& b) { - // compare by lastAccessed time, and tiebreak by creationTime. - int64_t result = a.Cookie()->LastAccessed() - b.Cookie()->LastAccessed(); + // compare by lastAccessedInUSec time, and tiebreak by creationTimeInUSec. + int64_t result = + a.Cookie()->LastAccessedInUSec() - b.Cookie()->LastAccessedInUSec(); if (result != 0) { return result < 0; } - return a.Cookie()->CreationTime() < b.Cookie()->CreationTime(); + return a.Cookie()->CreationTimeInUSec() < b.Cookie()->CreationTimeInUSec(); } }; @@ -64,14 +66,15 @@ class CookieStorage::CompareCookiesByAge { // Other non-expired cookies are sorted by their age. class CookieStorage::CookieIterComparator { private: - int64_t mCurrentTime; + int64_t mCurrentTimeInMSec; public: - explicit CookieIterComparator(int64_t aTime) : mCurrentTime(aTime) {} + explicit CookieIterComparator(int64_t aTimeInMSec) + : mCurrentTimeInMSec(aTimeInMSec) {} bool LessThan(const CookieListIter& lhs, const CookieListIter& rhs) { - bool lExpired = lhs.Cookie()->Expiry() <= mCurrentTime; - bool rExpired = rhs.Cookie()->Expiry() <= mCurrentTime; + bool lExpired = lhs.Cookie()->ExpiryInMSec() <= mCurrentTimeInMSec; + bool rExpired = rhs.Cookie()->ExpiryInMSec() <= mCurrentTimeInMSec; if (lExpired && !rExpired) { return true; } @@ -662,7 +665,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, return; } - int64_t currentTime = aCurrentTimeInUsec / PR_USEC_PER_MSEC; + int64_t currentTimeInMSec = aCurrentTimeInUsec / PR_USEC_PER_MSEC; CookieListIter exactIter{}; bool foundCookie = false; @@ -708,8 +711,8 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, // need to be careful about the semantics of removing it and adding the new // cookie: we want the behavior wrt adding the new cookie to be the same as // if it didn't exist, but we still want to fire a removal notification. - if (oldCookie->Expiry() <= currentTime) { - if (aCookie->Expiry() <= currentTime) { + if (oldCookie->ExpiryInMSec() <= currentTimeInMSec) { + if (aCookie->ExpiryInMSec() <= currentTimeInMSec) { // The new cookie has expired and the old one is stale. Nothing to do. COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader, "cookie has already expired"); @@ -745,7 +748,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, // isHttpOnly and SameSite flags then we can just keep the old one. // Only if any of these differ we would want to override the cookie. if (oldCookie->Value().Equals(aCookie->Value()) && - oldCookie->Expiry() == aCookie->Expiry() && + oldCookie->ExpiryInMSec() == aCookie->ExpiryInMSec() && oldCookie->IsSecure() == aCookie->IsSecure() && oldCookie->IsSession() == aCookie->IsSession() && oldCookie->IsHttpOnly() == aCookie->IsHttpOnly() && @@ -756,7 +759,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, // database. !oldCookie->IsStale()) { // Update the last access time on the old cookie. - oldCookie->SetLastAccessed(aCookie->LastAccessed()); + oldCookie->SetLastAccessedInUSec(aCookie->LastAccessedInUSec()); UpdateCookieOldestTime(oldCookie); return; } @@ -770,7 +773,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, // If the new cookie has expired -- i.e. the intent was simply to delete // the old cookie -- then we're done. - if (aCookie->Expiry() <= currentTime) { + if (aCookie->ExpiryInMSec() <= currentTimeInMSec) { COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader, "previously stored cookie was deleted"); NotifyChanged(oldCookie, nsICookieNotification::COOKIE_DELETED, @@ -780,7 +783,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, } // Preserve creation time of cookie for ordering purposes. - aCookie->SetCreationTime(oldCookie->CreationTime()); + aCookie->SetCreationTimeInUSec(oldCookie->CreationTimeInUSec()); } // check for CHIPS-partitioned exceeding byte limit @@ -808,7 +811,7 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, } } else { // check if cookie has already expired - if (aCookie->Expiry() <= currentTime) { + if (aCookie->ExpiryInMSec() <= currentTimeInMSec) { COOKIE_LOGFAILURE(SET_COOKIE, aHostURI, aCookieHeader, "cookie has already expired"); return; @@ -828,11 +831,12 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, uint32_t limit = mMaxCookiesPerHost - mCookieQuotaPerHost + excess; // Prioritize evicting insecure cookies. // (draft-ietf-httpbis-cookie-alone section 3.3) - FindStaleCookies(entry, currentTime, false, removedIterList, limit); + FindStaleCookies(entry, currentTimeInMSec, false, removedIterList, limit); if (removedIterList.Length() == 0) { if (aCookie->IsSecure()) { // It's valid to evict a secure cookie for another secure cookie. - FindStaleCookies(entry, currentTime, true, removedIterList, limit); + FindStaleCookies(entry, currentTimeInMSec, true, removedIterList, + limit); } else { COOKIE_LOGEVICTED(aCookie, "Too many cookies for this domain and the new " @@ -920,8 +924,8 @@ void CookieStorage::AddCookie(CookieParser* aCookieParser, } void CookieStorage::UpdateCookieOldestTime(Cookie* aCookie) { - if (aCookie->LastAccessed() < mCookieOldestTime) { - mCookieOldestTime = aCookie->LastAccessed(); + if (aCookie->LastAccessedInUSec() < mCookieOldestTime) { + mCookieOldestTime = aCookie->LastAccessedInUSec(); } } @@ -959,10 +963,10 @@ already_AddRefed<nsIArray> CookieStorage::CreatePurgeList(nsICookie* aCookie) { } // Given the output iter array and the count limit, find cookies -// sort by expiry and lastAccessed time. +// sort by expiry and lastAccessedInUSec time. // static -void CookieStorage::FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime, - bool aIsSecure, +void CookieStorage::FindStaleCookies(CookieEntry* aEntry, + int64_t aCurrentTimeInMSec, bool aIsSecure, nsTArray<CookieListIter>& aOutput, uint32_t aLimit) { MOZ_ASSERT(aLimit); @@ -970,13 +974,13 @@ void CookieStorage::FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime, const CookieEntry::ArrayType& cookies = aEntry->GetCookies(); aOutput.Clear(); - CookieIterComparator comp(aCurrentTime); + CookieIterComparator comp(aCurrentTimeInMSec); nsTPriorityQueue<CookieListIter, CookieIterComparator> queue(comp); for (CookieEntry::IndexType i = 0; i < cookies.Length(); ++i) { Cookie* cookie = cookies[i]; - if (cookie->Expiry() <= aCurrentTime) { + if (cookie->ExpiryInMSec() <= aCurrentTimeInMSec) { queue.Push(CookieListIter(aEntry, i)); continue; } @@ -1037,7 +1041,7 @@ already_AddRefed<nsIArray> CookieStorage::PurgeCookiesWithCallbacks( nsCOMPtr<nsIMutableArray> removedList = do_CreateInstance(NS_ARRAY_CONTRACTID); - int64_t currentTime = aCurrentTimeInUsec / PR_USEC_PER_MSEC; + int64_t currentTimeInMSec = aCurrentTimeInUsec / PR_USEC_PER_MSEC; int64_t purgeTime = aCurrentTimeInUsec - aCookiePurgeAge; int64_t oldestTime = INT64_MAX; @@ -1051,7 +1055,7 @@ already_AddRefed<nsIArray> CookieStorage::PurgeCookiesWithCallbacks( Cookie* cookie = cookies[i]; // check if the cookie has expired - if (cookie->Expiry() <= currentTime) { + if (cookie->ExpiryInMSec() <= currentTimeInMSec) { removedList->AppendElement(cookie); COOKIE_LOGEVICTED(cookie, "Cookie expired"); @@ -1063,12 +1067,12 @@ already_AddRefed<nsIArray> CookieStorage::PurgeCookiesWithCallbacks( } } else { // check if the cookie is over the age limit - if (cookie->LastAccessed() <= purgeTime) { + if (cookie->LastAccessedInUSec() <= purgeTime) { purgeList.AppendElement(iter); - } else if (cookie->LastAccessed() < oldestTime) { + } else if (cookie->LastAccessedInUSec() < oldestTime) { // reset our indicator - oldestTime = cookie->LastAccessed(); + oldestTime = cookie->LastAccessedInUSec(); } ++i; @@ -1089,7 +1093,7 @@ already_AddRefed<nsIArray> CookieStorage::PurgeCookiesWithCallbacks( : 0; if (purgeList.Length() > excess) { // We're not purging everything in the list, so update our indicator. - oldestTime = purgeList[excess].Cookie()->LastAccessed(); + oldestTime = purgeList[excess].Cookie()->LastAccessedInUSec(); purgeList.SetLength(excess); } diff --git a/netwerk/cookie/CookieStorage.h b/netwerk/cookie/CookieStorage.h @@ -232,7 +232,7 @@ class CookieStorage : public nsIObserver, public nsSupportsWeakReference { const OriginAttributes& aOriginAttributes, Cookie* aCookie); - static void FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTime, + static void FindStaleCookies(CookieEntry* aEntry, int64_t aCurrentTimeInMSec, bool aIsSecure, nsTArray<CookieListIter>& aOutput, uint32_t aLimit); diff --git a/netwerk/cookie/CookieValidation.cpp b/netwerk/cookie/CookieValidation.cpp @@ -115,9 +115,10 @@ void CookieValidation::ValidateInternal() { // This part checks if the caleers have set the expiry value to max 400 days. if (!mCookieData.isSession()) { int64_t maxageCap = StaticPrefs::network_cookie_maxageCap(); - int64_t currentTimeInMSec = mCookieData.creationTime() / PR_USEC_PER_MSEC; - int64_t expiry = mCookieData.expiry(); - if (maxageCap && expiry > currentTimeInMSec + maxageCap * 1000) { + int64_t currentTimeInMSec = + mCookieData.creationTimeInUSec() / PR_USEC_PER_MSEC; + int64_t expiryInMSec = mCookieData.expiryInMSec(); + if (maxageCap && expiryInMSec > currentTimeInMSec + maxageCap * 1000) { mResult = eRejectedAttributeExpiryOversize; return; } diff --git a/netwerk/ipc/NeckoChannelParams.ipdlh b/netwerk/ipc/NeckoChannelParams.ipdlh @@ -496,9 +496,9 @@ struct CookieStruct nsCString value; nsCString host; nsCString path; - int64_t expiry; - int64_t lastAccessed; - int64_t creationTime; + int64_t expiryInMSec; + int64_t lastAccessedInUSec; + int64_t creationTimeInUSec; bool isHttpOnly; bool isSession; bool isSecure; diff --git a/toolkit/components/cookiebanners/nsCookieInjector.cpp b/toolkit/components/cookiebanners/nsCookieInjector.cpp @@ -328,7 +328,7 @@ nsresult nsCookieInjector::InjectCookiesFromRules( nsCOMPtr<nsICookieValidation> validation; rv = cookieManager->AddNative( nullptr, c.Host(), c.Path(), c.Name(), c.Value(), c.IsSecure(), - c.IsHttpOnly(), c.IsSession(), c.Expiry(), &aOriginAttributes, + c.IsHttpOnly(), c.IsSession(), c.ExpiryInMSec(), &aOriginAttributes, c.SameSite(), static_cast<nsICookie::schemeType>(c.SchemeMap()), /* is partitioned: */ false, /* is from http: */ true, nullptr, getter_AddRefs(validation)); diff --git a/toolkit/components/cookiebanners/nsCookieRule.cpp b/toolkit/components/cookiebanners/nsCookieRule.cpp @@ -87,12 +87,12 @@ NS_IMETHODIMP nsCookieRule::GetCookie(nsICookie** aCookie) { // Copy cookie and update expiry, creation and last accessed time. RefPtr<net::Cookie> cookieNative = mCookie->Clone(); - int64_t currentTimeInUsec = PR_Now(); - cookieNative->SetCreationTime( - net::Cookie::GenerateUniqueCreationTime(currentTimeInUsec)); - cookieNative->SetLastAccessed(currentTimeInUsec); - cookieNative->SetExpiry((currentTimeInUsec / PR_USEC_PER_MSEC) + - mExpiryRelative * PR_MSEC_PER_SEC); + int64_t currentTimeInUSec = PR_Now(); + cookieNative->SetCreationTimeInUSec( + net::Cookie::GenerateUniqueCreationTimeInUSec(currentTimeInUSec)); + cookieNative->SetLastAccessedInUSec(currentTimeInUSec); + cookieNative->SetExpiryInMSec((currentTimeInUSec / PR_USEC_PER_MSEC) + + mExpiryRelative * PR_MSEC_PER_SEC); cookieNative.forget(aCookie); return NS_OK;