Source code

Revision control

Other Tools

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/. */
4
5
#ifndef CacheFileMetadata__h__
6
#define CacheFileMetadata__h__
7
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 "nsAutoPtr.h"
15
#include "nsString.h"
16
17
class nsICacheEntryMetaDataVisitor;
18
19
namespace mozilla {
20
namespace net {
21
22
// Flags stored in CacheFileMetadataHeader.mFlags
23
24
// Whether an entry is a pinned entry (created with
25
// nsICacheStorageService.pinningCacheStorage.)
26
static const uint32_t kCacheEntryIsPinned = 1 << 0;
27
28
// By multiplying with the current half-life we convert the frecency
29
// to time independent of half-life value. The range fits 32bits.
30
// When decay time changes on next run of the browser, we convert
31
// the frecency value to a correct internal representation again.
32
// It might not be 100% accurate, but for the purpose it suffice.
33
#define FRECENCY2INT(aFrecency) \
34
((uint32_t)((aFrecency)*CacheObserver::HalfLifeSeconds()))
35
#define INT2FRECENCY(aInt) \
36
((double)(aInt) / (double)CacheObserver::HalfLifeSeconds())
37
38
#define kCacheEntryVersion 3
39
40
#pragma pack(push)
41
#pragma pack(1)
42
43
class CacheFileMetadataHeader {
44
public:
45
uint32_t mVersion;
46
uint32_t mFetchCount;
47
uint32_t mLastFetched;
48
uint32_t mLastModified;
49
uint32_t mFrecency;
50
uint32_t mExpirationTime;
51
uint32_t mKeySize;
52
uint32_t mFlags;
53
54
void WriteToBuf(void* aBuf) {
55
EnsureCorrectClassSize();
56
57
uint8_t* ptr = static_cast<uint8_t*>(aBuf);
58
MOZ_ASSERT(mVersion == kCacheEntryVersion);
59
NetworkEndian::writeUint32(ptr, mVersion);
60
ptr += sizeof(uint32_t);
61
NetworkEndian::writeUint32(ptr, mFetchCount);
62
ptr += sizeof(uint32_t);
63
NetworkEndian::writeUint32(ptr, mLastFetched);
64
ptr += sizeof(uint32_t);
65
NetworkEndian::writeUint32(ptr, mLastModified);
66
ptr += sizeof(uint32_t);
67
NetworkEndian::writeUint32(ptr, mFrecency);
68
ptr += sizeof(uint32_t);
69
NetworkEndian::writeUint32(ptr, mExpirationTime);
70
ptr += sizeof(uint32_t);
71
NetworkEndian::writeUint32(ptr, mKeySize);
72
ptr += sizeof(uint32_t);
73
NetworkEndian::writeUint32(ptr, mFlags);
74
}
75
76
void ReadFromBuf(const void* aBuf) {
77
EnsureCorrectClassSize();
78
79
const uint8_t* ptr = static_cast<const uint8_t*>(aBuf);
80
mVersion = BigEndian::readUint32(ptr);
81
ptr += sizeof(uint32_t);
82
mFetchCount = BigEndian::readUint32(ptr);
83
ptr += sizeof(uint32_t);
84
mLastFetched = BigEndian::readUint32(ptr);
85
ptr += sizeof(uint32_t);
86
mLastModified = BigEndian::readUint32(ptr);
87
ptr += sizeof(uint32_t);
88
mFrecency = BigEndian::readUint32(ptr);
89
ptr += sizeof(uint32_t);
90
mExpirationTime = BigEndian::readUint32(ptr);
91
ptr += sizeof(uint32_t);
92
mKeySize = BigEndian::readUint32(ptr);
93
ptr += sizeof(uint32_t);
94
if (mVersion >= 2) {
95
mFlags = BigEndian::readUint32(ptr);
96
} else {
97
mFlags = 0;
98
}
99
}
100
101
inline void EnsureCorrectClassSize() {
102
static_assert(
103
(sizeof(mVersion) + sizeof(mFetchCount) + sizeof(mLastFetched) +
104
sizeof(mLastModified) + sizeof(mFrecency) + sizeof(mExpirationTime) +
105
sizeof(mKeySize)) +
106
sizeof(mFlags) ==
107
sizeof(CacheFileMetadataHeader),
108
"Unexpected sizeof(CacheFileMetadataHeader)!");
109
}
110
};
111
112
#pragma pack(pop)
113
114
#define CACHEFILEMETADATALISTENER_IID \
115
{ /* a9e36125-3f01-4020-9540-9dafa8d31ba7 */ \
116
0xa9e36125, 0x3f01, 0x4020, { \
117
0x95, 0x40, 0x9d, 0xaf, 0xa8, 0xd3, 0x1b, 0xa7 \
118
} \
119
}
120
121
class CacheFileMetadataListener : public nsISupports {
122
public:
123
NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILEMETADATALISTENER_IID)
124
125
NS_IMETHOD OnMetadataRead(nsresult aResult) = 0;
126
NS_IMETHOD OnMetadataWritten(nsresult aResult) = 0;
127
virtual bool IsKilled() = 0;
128
};
129
130
NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileMetadataListener,
131
CACHEFILEMETADATALISTENER_IID)
132
133
class CacheFileMetadata final : public CacheFileIOListener,
134
public CacheMemoryConsumer {
135
public:
136
NS_DECL_THREADSAFE_ISUPPORTS
137
138
CacheFileMetadata(CacheFileHandle* aHandle, const nsACString& aKey);
139
CacheFileMetadata(bool aMemoryOnly, bool aPinned, const nsACString& aKey);
140
CacheFileMetadata();
141
142
void SetHandle(CacheFileHandle* aHandle);
143
144
nsresult GetKey(nsACString& _retval);
145
146
nsresult ReadMetadata(CacheFileMetadataListener* aListener);
147
uint32_t CalcMetadataSize(uint32_t aElementsSize, uint32_t aHashCount);
148
nsresult WriteMetadata(uint32_t aOffset,
149
CacheFileMetadataListener* aListener);
150
nsresult SyncReadMetadata(nsIFile* aFile);
151
152
bool IsAnonymous() const { return mAnonymous; }
153
mozilla::OriginAttributes const& OriginAttributes() const {
154
return mOriginAttributes;
155
}
156
bool Pinned() const { return !!(mMetaHdr.mFlags & kCacheEntryIsPinned); }
157
158
const char* GetElement(const char* aKey);
159
nsresult SetElement(const char* aKey, const char* aValue);
160
nsresult Visit(nsICacheEntryMetaDataVisitor* aVisitor);
161
162
CacheHash::Hash16_t GetHash(uint32_t aIndex);
163
nsresult SetHash(uint32_t aIndex, CacheHash::Hash16_t aHash);
164
nsresult RemoveHash(uint32_t aIndex);
165
166
nsresult AddFlags(uint32_t aFlags);
167
nsresult RemoveFlags(uint32_t aFlags);
168
nsresult GetFlags(uint32_t* _retval);
169
nsresult SetExpirationTime(uint32_t aExpirationTime);
170
nsresult GetExpirationTime(uint32_t* _retval);
171
nsresult SetFrecency(uint32_t aFrecency);
172
nsresult GetFrecency(uint32_t* _retval);
173
nsresult GetLastModified(uint32_t* _retval);
174
nsresult GetLastFetched(uint32_t* _retval);
175
nsresult GetFetchCount(uint32_t* _retval);
176
// Called by upper layers to indicate the entry this metadata belongs
177
// with has been fetched, i.e. delivered to the consumer.
178
nsresult OnFetched();
179
180
int64_t Offset() { return mOffset; }
181
uint32_t ElementsSize() { return mElementsSize; }
182
void MarkDirty(bool aUpdateLastModified = true);
183
bool IsDirty() { return mIsDirty; }
184
uint32_t MemoryUsage() {
185
return sizeof(CacheFileMetadata) + mHashArraySize + mBufSize;
186
}
187
188
NS_IMETHOD OnFileOpened(CacheFileHandle* aHandle, nsresult aResult) override;
189
NS_IMETHOD OnDataWritten(CacheFileHandle* aHandle, const char* aBuf,
190
nsresult aResult) override;
191
NS_IMETHOD OnDataRead(CacheFileHandle* aHandle, char* aBuf,
192
nsresult aResult) override;
193
NS_IMETHOD OnFileDoomed(CacheFileHandle* aHandle, nsresult aResult) override;
194
NS_IMETHOD OnEOFSet(CacheFileHandle* aHandle, nsresult aResult) override;
195
NS_IMETHOD OnFileRenamed(CacheFileHandle* aHandle, nsresult aResult) override;
196
virtual bool IsKilled() override {
197
return mListener && mListener->IsKilled();
198
}
199
void InitEmptyMetadata();
200
201
// Memory reporting
202
size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
203
size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
204
205
private:
206
virtual ~CacheFileMetadata();
207
208
nsresult ParseMetadata(uint32_t aMetaOffset, uint32_t aBufOffset,
209
bool aHaveKey);
210
nsresult CheckElements(const char* aBuf, uint32_t aSize);
211
nsresult EnsureBuffer(uint32_t aSize);
212
nsresult ParseKey(const nsACString& aKey);
213
214
RefPtr<CacheFileHandle> mHandle;
215
nsCString mKey;
216
CacheHash::Hash16_t* mHashArray;
217
uint32_t mHashArraySize;
218
uint32_t mHashCount;
219
int64_t mOffset;
220
char* mBuf; // used for parsing, then points
221
// to elements
222
uint32_t mBufSize;
223
char* mWriteBuf;
224
CacheFileMetadataHeader mMetaHdr;
225
uint32_t mElementsSize;
226
bool mIsDirty : 1;
227
bool mAnonymous : 1;
228
bool mAllocExactSize : 1;
229
bool mFirstRead : 1;
230
mozilla::OriginAttributes mOriginAttributes;
231
mozilla::TimeStamp mReadStart;
232
nsCOMPtr<CacheFileMetadataListener> mListener;
233
};
234
235
} // namespace net
236
} // namespace mozilla
237
238
#endif