Bug 1861709 replace AudioCallbackDriver::ThreadRunning() assertions that mean to...
[gecko.git] / netwerk / cache2 / CacheFileMetadata.h
blob3bec23d6c4b74f8de762ee2d9466c6bea41b2038
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"
15 #include "nsString.h"
17 class nsICacheEntryMetaDataVisitor;
19 namespace mozilla {
20 namespace net {
22 namespace CacheFileUtils {
23 class CacheFileLock;
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
44 #pragma pack(push)
45 #pragma pack(1)
47 class CacheFileMetadataHeader {
48 public:
49 uint32_t mVersion;
50 uint32_t mFetchCount;
51 uint32_t mLastFetched;
52 uint32_t mLastModified;
53 uint32_t mFrecency;
54 uint32_t mExpirationTime;
55 uint32_t mKeySize;
56 uint32_t mFlags;
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);
98 if (mVersion >= 2) {
99 mFlags = BigEndian::readUint32(ptr);
100 } else {
101 mFlags = 0;
105 inline void EnsureCorrectClassSize() {
106 static_assert(
107 (sizeof(mVersion) + sizeof(mFetchCount) + sizeof(mLastFetched) +
108 sizeof(mLastModified) + sizeof(mFrecency) + sizeof(mExpirationTime) +
109 sizeof(mKeySize)) +
110 sizeof(mFlags) ==
111 sizeof(CacheFileMetadataHeader),
112 "Unexpected sizeof(CacheFileMetadataHeader)!");
116 #pragma pack(pop)
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 {
126 public:
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 {
139 public:
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);
146 CacheFileMetadata();
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.
184 void OnFetched();
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();
207 // Memory reporting
208 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
209 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
211 private:
212 virtual ~CacheFileMetadata();
214 nsresult ParseMetadata(uint32_t aMetaOffset, uint32_t aBufOffset,
215 bool aHaveKey);
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;
221 nsCString mKey;
222 CacheHash::Hash16_t* mHashArray{nullptr};
223 uint32_t mHashArraySize{0};
224 uint32_t mHashCount{0};
225 int64_t mOffset{0};
226 // used for parsing, then points to elements
227 char* mBuf{nullptr};
228 uint32_t mBufSize{0};
229 char* mWriteBuf{nullptr};
230 CacheFileMetadataHeader mMetaHdr{0};
231 uint32_t mElementsSize{0};
232 bool mIsDirty : 1;
233 bool mAnonymous : 1;
234 bool mAllocExactSize : 1;
235 bool mFirstRead : 1;
236 mozilla::OriginAttributes mOriginAttributes;
237 mozilla::TimeStamp mReadStart;
238 nsCOMPtr<CacheFileMetadataListener> mListener;
239 RefPtr<CacheFileUtils::CacheFileLock> mLock;
242 } // namespace net
243 } // namespace mozilla
245 #endif