CacheExpirationTime.h (1950B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #ifndef mozilla_dom_CacheExpirationTime_h___ 8 #define mozilla_dom_CacheExpirationTime_h___ 9 10 #include <stdint.h> // uint32_t 11 12 #include "mozilla/Assertions.h" // MOZ_ASSERT 13 #include "nsICacheEntry.h" // nsICacheEntry 14 #include "prtime.h" // PRTime, PR_USEC_PER_SEC 15 16 /* 17 * The expiration time for sub resource cache. 18 */ 19 struct CacheExpirationTime { 20 private: 21 uint32_t mTime; 22 23 static constexpr uint32_t kAlreadyExpired = 0; 24 static constexpr uint32_t kNever = nsICacheEntry::NO_EXPIRATION_TIME; 25 26 constexpr CacheExpirationTime() : mTime(kNever) {} 27 28 explicit constexpr CacheExpirationTime(uint32_t aTime) : mTime(aTime) {} 29 30 static uint32_t SecondsFromPRTime(PRTime aTime) { 31 return uint32_t(int64_t(aTime) / int64_t(PR_USEC_PER_SEC)); 32 } 33 34 public: 35 static constexpr CacheExpirationTime AlreadyExpired() { 36 return CacheExpirationTime(kAlreadyExpired); 37 } 38 39 static constexpr CacheExpirationTime Never() { 40 return CacheExpirationTime(kNever); 41 } 42 43 static constexpr CacheExpirationTime ExpireAt(uint32_t aTime) { 44 return CacheExpirationTime(aTime); 45 } 46 47 bool IsExpired() const { 48 if (IsNever()) { 49 return false; 50 } 51 return mTime <= SecondsFromPRTime(PR_Now()); 52 } 53 54 bool IsNever() const { return mTime == kNever; } 55 56 bool IsShorterThan(const CacheExpirationTime& aOther) const { 57 return mTime < aOther.mTime; 58 } 59 60 void SetMinimum(const CacheExpirationTime& aOther) { 61 if (aOther.IsNever()) { 62 return; 63 } 64 65 if (IsNever() || aOther.IsShorterThan(*this)) { 66 mTime = aOther.mTime; 67 } 68 } 69 }; 70 71 #endif /* mozilla_dom_CacheExpirationTime_h___ */