1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 #ifndef CacheFileMetadata__h__
6 #define CacheFileMetadata__h__
8 #include "CacheFileIOManager.h"
9 #include "CacheStorageService.h"
10 #include "CacheHashUtils.h"
11 #include "CacheObserver.h"
12 #include "mozilla/EndianUtils.h"
13 #include "mozilla/BasePrincipal.h"
14 #include "mozilla/NotNull.h"
17 class nsICacheEntryMetaDataVisitor
;
22 namespace CacheFileUtils
{
26 // Flags stored in CacheFileMetadataHeader.mFlags
28 // Whether an entry is a pinned entry (created with
29 // nsICacheStorageService.pinningCacheStorage.)
30 static const uint32_t kCacheEntryIsPinned
= 1 << 0;
32 // By multiplying with the current half-life we convert the frecency
33 // to time independent of half-life value. The range fits 32bits.
34 // When decay time changes on next run of the browser, we convert
35 // the frecency value to a correct internal representation again.
36 // It might not be 100% accurate, but for the purpose it suffice.
37 #define FRECENCY2INT(aFrecency) \
38 ((uint32_t)((aFrecency) * CacheObserver::HalfLifeSeconds()))
39 #define INT2FRECENCY(aInt) \
40 ((double)(aInt) / (double)CacheObserver::HalfLifeSeconds())
42 #define kCacheEntryVersion 3
47 class CacheFileMetadataHeader
{
51 uint32_t mLastFetched
;
52 uint32_t mLastModified
;
54 uint32_t mExpirationTime
;
58 void WriteToBuf(void* aBuf
) {
59 EnsureCorrectClassSize();
61 uint8_t* ptr
= static_cast<uint8_t*>(aBuf
);
62 MOZ_ASSERT(mVersion
== kCacheEntryVersion
);
63 NetworkEndian::writeUint32(ptr
, mVersion
);
64 ptr
+= sizeof(uint32_t);
65 NetworkEndian::writeUint32(ptr
, mFetchCount
);
66 ptr
+= sizeof(uint32_t);
67 NetworkEndian::writeUint32(ptr
, mLastFetched
);
68 ptr
+= sizeof(uint32_t);
69 NetworkEndian::writeUint32(ptr
, mLastModified
);
70 ptr
+= sizeof(uint32_t);
71 NetworkEndian::writeUint32(ptr
, mFrecency
);
72 ptr
+= sizeof(uint32_t);
73 NetworkEndian::writeUint32(ptr
, mExpirationTime
);
74 ptr
+= sizeof(uint32_t);
75 NetworkEndian::writeUint32(ptr
, mKeySize
);
76 ptr
+= sizeof(uint32_t);
77 NetworkEndian::writeUint32(ptr
, mFlags
);
80 void ReadFromBuf(const void* aBuf
) {
81 EnsureCorrectClassSize();
83 const uint8_t* ptr
= static_cast<const uint8_t*>(aBuf
);
84 mVersion
= BigEndian::readUint32(ptr
);
85 ptr
+= sizeof(uint32_t);
86 mFetchCount
= BigEndian::readUint32(ptr
);
87 ptr
+= sizeof(uint32_t);
88 mLastFetched
= BigEndian::readUint32(ptr
);
89 ptr
+= sizeof(uint32_t);
90 mLastModified
= BigEndian::readUint32(ptr
);
91 ptr
+= sizeof(uint32_t);
92 mFrecency
= BigEndian::readUint32(ptr
);
93 ptr
+= sizeof(uint32_t);
94 mExpirationTime
= BigEndian::readUint32(ptr
);
95 ptr
+= sizeof(uint32_t);
96 mKeySize
= BigEndian::readUint32(ptr
);
97 ptr
+= sizeof(uint32_t);
99 mFlags
= BigEndian::readUint32(ptr
);
105 inline void EnsureCorrectClassSize() {
107 (sizeof(mVersion
) + sizeof(mFetchCount
) + sizeof(mLastFetched
) +
108 sizeof(mLastModified
) + sizeof(mFrecency
) + sizeof(mExpirationTime
) +
111 sizeof(CacheFileMetadataHeader
),
112 "Unexpected sizeof(CacheFileMetadataHeader)!");
118 #define CACHEFILEMETADATALISTENER_IID \
119 { /* a9e36125-3f01-4020-9540-9dafa8d31ba7 */ \
120 0xa9e36125, 0x3f01, 0x4020, { \
121 0x95, 0x40, 0x9d, 0xaf, 0xa8, 0xd3, 0x1b, 0xa7 \
125 class CacheFileMetadataListener
: public nsISupports
{
127 NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILEMETADATALISTENER_IID
)
129 NS_IMETHOD
OnMetadataRead(nsresult aResult
) = 0;
130 NS_IMETHOD
OnMetadataWritten(nsresult aResult
) = 0;
131 virtual bool IsKilled() = 0;
134 NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileMetadataListener
,
135 CACHEFILEMETADATALISTENER_IID
)
137 class CacheFileMetadata final
: public CacheFileIOListener
,
138 public CacheMemoryConsumer
{
140 NS_DECL_THREADSAFE_ISUPPORTS
142 CacheFileMetadata(CacheFileHandle
* aHandle
, const nsACString
& aKey
,
143 NotNull
<CacheFileUtils::CacheFileLock
*> aLock
);
144 CacheFileMetadata(bool aMemoryOnly
, bool aPinned
, const nsACString
& aKey
,
145 NotNull
<CacheFileUtils::CacheFileLock
*> aLock
);
148 void SetHandle(CacheFileHandle
* aHandle
);
150 const nsACString
& GetKey() const { return mKey
; }
152 void ReadMetadata(CacheFileMetadataListener
* aListener
);
153 uint32_t CalcMetadataSize(uint32_t aElementsSize
, uint32_t aHashCount
);
154 nsresult
WriteMetadata(uint32_t aOffset
,
155 CacheFileMetadataListener
* aListener
);
156 nsresult
SyncReadMetadata(nsIFile
* aFile
);
158 bool IsAnonymous() const { return mAnonymous
; }
159 mozilla::OriginAttributes
const& OriginAttributes() const {
160 return mOriginAttributes
;
162 bool Pinned() const { return !!(mMetaHdr
.mFlags
& kCacheEntryIsPinned
); }
164 const char* GetElement(const char* aKey
);
165 nsresult
SetElement(const char* aKey
, const char* aValue
);
166 void Visit(nsICacheEntryMetaDataVisitor
* aVisitor
);
168 CacheHash::Hash16_t
GetHash(uint32_t aIndex
);
169 nsresult
SetHash(uint32_t aIndex
, CacheHash::Hash16_t aHash
);
170 nsresult
RemoveHash(uint32_t aIndex
);
172 void AddFlags(uint32_t aFlags
);
173 void RemoveFlags(uint32_t aFlags
);
174 uint32_t GetFlags() const { return mMetaHdr
.mFlags
; }
175 void SetExpirationTime(uint32_t aExpirationTime
);
176 uint32_t GetExpirationTime() const { return mMetaHdr
.mExpirationTime
; }
177 void SetFrecency(uint32_t aFrecency
);
178 uint32_t GetFrecency() const { return mMetaHdr
.mFrecency
; }
179 uint32_t GetLastModified() const { return mMetaHdr
.mLastModified
; }
180 uint32_t GetLastFetched() const { return mMetaHdr
.mLastFetched
; }
181 uint32_t GetFetchCount() const { return mMetaHdr
.mFetchCount
; }
182 // Called by upper layers to indicate the entry this metadata belongs
183 // with has been fetched, i.e. delivered to the consumer.
186 int64_t Offset() { return mOffset
; }
187 uint32_t ElementsSize() { return mElementsSize
; }
188 void MarkDirty(bool aUpdateLastModified
= true);
189 bool IsDirty() { return mIsDirty
; }
190 uint32_t MemoryUsage() {
191 return sizeof(CacheFileMetadata
) + mHashArraySize
+ mBufSize
;
194 NS_IMETHOD
OnFileOpened(CacheFileHandle
* aHandle
, nsresult aResult
) override
;
195 NS_IMETHOD
OnDataWritten(CacheFileHandle
* aHandle
, const char* aBuf
,
196 nsresult aResult
) override
;
197 NS_IMETHOD
OnDataRead(CacheFileHandle
* aHandle
, char* aBuf
,
198 nsresult aResult
) override
;
199 NS_IMETHOD
OnFileDoomed(CacheFileHandle
* aHandle
, nsresult aResult
) override
;
200 NS_IMETHOD
OnEOFSet(CacheFileHandle
* aHandle
, nsresult aResult
) override
;
201 NS_IMETHOD
OnFileRenamed(CacheFileHandle
* aHandle
, nsresult aResult
) override
;
202 virtual bool IsKilled() override
{
203 return mListener
&& mListener
->IsKilled();
205 void InitEmptyMetadata();
208 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
209 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
212 virtual ~CacheFileMetadata();
214 nsresult
ParseMetadata(uint32_t aMetaOffset
, uint32_t aBufOffset
,
216 nsresult
CheckElements(const char* aBuf
, uint32_t aSize
);
217 nsresult
EnsureBuffer(uint32_t aSize
);
218 nsresult
ParseKey(const nsACString
& aKey
);
220 RefPtr
<CacheFileHandle
> mHandle
;
222 CacheHash::Hash16_t
* mHashArray
{nullptr};
223 uint32_t mHashArraySize
{0};
224 uint32_t mHashCount
{0};
226 // used for parsing, then points to elements
228 uint32_t mBufSize
{0};
229 char* mWriteBuf
{nullptr};
230 CacheFileMetadataHeader mMetaHdr
{0};
231 uint32_t mElementsSize
{0};
234 bool mAllocExactSize
: 1;
236 mozilla::OriginAttributes mOriginAttributes
;
237 mozilla::TimeStamp mReadStart
;
238 nsCOMPtr
<CacheFileMetadataListener
> mListener
;
239 RefPtr
<CacheFileUtils::CacheFileLock
> mLock
;
243 } // namespace mozilla