Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 2 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 3 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 4 | |
michael@0 | 5 | #ifndef CacheFileMetadata__h__ |
michael@0 | 6 | #define CacheFileMetadata__h__ |
michael@0 | 7 | |
michael@0 | 8 | #include "CacheFileIOManager.h" |
michael@0 | 9 | #include "CacheStorageService.h" |
michael@0 | 10 | #include "CacheHashUtils.h" |
michael@0 | 11 | #include "CacheObserver.h" |
michael@0 | 12 | #include "mozilla/Endian.h" |
michael@0 | 13 | #include "nsAutoPtr.h" |
michael@0 | 14 | #include "nsString.h" |
michael@0 | 15 | |
michael@0 | 16 | namespace mozilla { |
michael@0 | 17 | namespace net { |
michael@0 | 18 | |
michael@0 | 19 | // By multiplying with the current half-life we convert the frecency |
michael@0 | 20 | // to time independent of half-life value. The range fits 32bits. |
michael@0 | 21 | // When decay time changes on next run of the browser, we convert |
michael@0 | 22 | // the frecency value to a correct internal representation again. |
michael@0 | 23 | // It might not be 100% accurate, but for the purpose it suffice. |
michael@0 | 24 | #define FRECENCY2INT(aFrecency) \ |
michael@0 | 25 | ((uint32_t)(aFrecency * CacheObserver::HalfLifeSeconds())) |
michael@0 | 26 | #define INT2FRECENCY(aInt) \ |
michael@0 | 27 | ((double)(aInt) / (double)CacheObserver::HalfLifeSeconds()) |
michael@0 | 28 | |
michael@0 | 29 | |
michael@0 | 30 | #pragma pack(push) |
michael@0 | 31 | #pragma pack(1) |
michael@0 | 32 | |
michael@0 | 33 | class CacheFileMetadataHeader { |
michael@0 | 34 | public: |
michael@0 | 35 | uint32_t mVersion; |
michael@0 | 36 | uint32_t mFetchCount; |
michael@0 | 37 | uint32_t mLastFetched; |
michael@0 | 38 | uint32_t mLastModified; |
michael@0 | 39 | uint32_t mFrecency; |
michael@0 | 40 | uint32_t mExpirationTime; |
michael@0 | 41 | uint32_t mKeySize; |
michael@0 | 42 | |
michael@0 | 43 | void WriteToBuf(void *aBuf) |
michael@0 | 44 | { |
michael@0 | 45 | EnsureCorrectClassSize(); |
michael@0 | 46 | |
michael@0 | 47 | uint8_t* ptr = static_cast<uint8_t*>(aBuf); |
michael@0 | 48 | NetworkEndian::writeUint32(ptr, mVersion); ptr += sizeof(uint32_t); |
michael@0 | 49 | NetworkEndian::writeUint32(ptr, mFetchCount); ptr += sizeof(uint32_t); |
michael@0 | 50 | NetworkEndian::writeUint32(ptr, mLastFetched); ptr += sizeof(uint32_t); |
michael@0 | 51 | NetworkEndian::writeUint32(ptr, mLastModified); ptr += sizeof(uint32_t); |
michael@0 | 52 | NetworkEndian::writeUint32(ptr, mFrecency); ptr += sizeof(uint32_t); |
michael@0 | 53 | NetworkEndian::writeUint32(ptr, mExpirationTime); ptr += sizeof(uint32_t); |
michael@0 | 54 | NetworkEndian::writeUint32(ptr, mKeySize); |
michael@0 | 55 | } |
michael@0 | 56 | |
michael@0 | 57 | void ReadFromBuf(const void *aBuf) |
michael@0 | 58 | { |
michael@0 | 59 | EnsureCorrectClassSize(); |
michael@0 | 60 | |
michael@0 | 61 | const uint8_t* ptr = static_cast<const uint8_t*>(aBuf); |
michael@0 | 62 | mVersion = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 63 | mFetchCount = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 64 | mLastFetched = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 65 | mLastModified = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 66 | mFrecency = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 67 | mExpirationTime = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); |
michael@0 | 68 | mKeySize = BigEndian::readUint32(ptr); |
michael@0 | 69 | } |
michael@0 | 70 | |
michael@0 | 71 | inline void EnsureCorrectClassSize() |
michael@0 | 72 | { |
michael@0 | 73 | static_assert((sizeof(mVersion) + sizeof(mFetchCount) + |
michael@0 | 74 | sizeof(mLastFetched) + sizeof(mLastModified) + sizeof(mFrecency) + |
michael@0 | 75 | sizeof(mExpirationTime) + sizeof(mKeySize)) == |
michael@0 | 76 | sizeof(CacheFileMetadataHeader), |
michael@0 | 77 | "Unexpected sizeof(CacheFileMetadataHeader)!"); |
michael@0 | 78 | } |
michael@0 | 79 | }; |
michael@0 | 80 | |
michael@0 | 81 | #pragma pack(pop) |
michael@0 | 82 | |
michael@0 | 83 | |
michael@0 | 84 | #define CACHEFILEMETADATALISTENER_IID \ |
michael@0 | 85 | { /* a9e36125-3f01-4020-9540-9dafa8d31ba7 */ \ |
michael@0 | 86 | 0xa9e36125, \ |
michael@0 | 87 | 0x3f01, \ |
michael@0 | 88 | 0x4020, \ |
michael@0 | 89 | {0x95, 0x40, 0x9d, 0xaf, 0xa8, 0xd3, 0x1b, 0xa7} \ |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | class CacheFileMetadataListener : public nsISupports |
michael@0 | 93 | { |
michael@0 | 94 | public: |
michael@0 | 95 | NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILEMETADATALISTENER_IID) |
michael@0 | 96 | |
michael@0 | 97 | NS_IMETHOD OnMetadataRead(nsresult aResult) = 0; |
michael@0 | 98 | NS_IMETHOD OnMetadataWritten(nsresult aResult) = 0; |
michael@0 | 99 | }; |
michael@0 | 100 | |
michael@0 | 101 | NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileMetadataListener, |
michael@0 | 102 | CACHEFILEMETADATALISTENER_IID) |
michael@0 | 103 | |
michael@0 | 104 | |
michael@0 | 105 | class CacheFileMetadata : public CacheFileIOListener |
michael@0 | 106 | , public CacheMemoryConsumer |
michael@0 | 107 | { |
michael@0 | 108 | public: |
michael@0 | 109 | NS_DECL_THREADSAFE_ISUPPORTS |
michael@0 | 110 | |
michael@0 | 111 | CacheFileMetadata(CacheFileHandle *aHandle, |
michael@0 | 112 | const nsACString &aKey); |
michael@0 | 113 | CacheFileMetadata(bool aMemoryOnly, |
michael@0 | 114 | const nsACString &aKey); |
michael@0 | 115 | CacheFileMetadata(); |
michael@0 | 116 | |
michael@0 | 117 | void SetHandle(CacheFileHandle *aHandle); |
michael@0 | 118 | |
michael@0 | 119 | nsresult GetKey(nsACString &_retval); |
michael@0 | 120 | |
michael@0 | 121 | nsresult ReadMetadata(CacheFileMetadataListener *aListener); |
michael@0 | 122 | nsresult WriteMetadata(uint32_t aOffset, |
michael@0 | 123 | CacheFileMetadataListener *aListener); |
michael@0 | 124 | nsresult SyncReadMetadata(nsIFile *aFile); |
michael@0 | 125 | |
michael@0 | 126 | bool IsAnonymous() { return mAnonymous; } |
michael@0 | 127 | bool IsInBrowser() { return mInBrowser; } |
michael@0 | 128 | uint32_t AppId() { return mAppId; } |
michael@0 | 129 | |
michael@0 | 130 | const char * GetElement(const char *aKey); |
michael@0 | 131 | nsresult SetElement(const char *aKey, const char *aValue); |
michael@0 | 132 | |
michael@0 | 133 | CacheHash::Hash16_t GetHash(uint32_t aIndex); |
michael@0 | 134 | nsresult SetHash(uint32_t aIndex, CacheHash::Hash16_t aHash); |
michael@0 | 135 | |
michael@0 | 136 | nsresult SetExpirationTime(uint32_t aExpirationTime); |
michael@0 | 137 | nsresult GetExpirationTime(uint32_t *_retval); |
michael@0 | 138 | nsresult SetLastModified(uint32_t aLastModified); |
michael@0 | 139 | nsresult GetLastModified(uint32_t *_retval); |
michael@0 | 140 | nsresult SetFrecency(uint32_t aFrecency); |
michael@0 | 141 | nsresult GetFrecency(uint32_t *_retval); |
michael@0 | 142 | nsresult GetLastFetched(uint32_t *_retval); |
michael@0 | 143 | nsresult GetFetchCount(uint32_t *_retval); |
michael@0 | 144 | |
michael@0 | 145 | int64_t Offset() { return mOffset; } |
michael@0 | 146 | uint32_t ElementsSize() { return mElementsSize; } |
michael@0 | 147 | void MarkDirty() { mIsDirty = true; } |
michael@0 | 148 | bool IsDirty() { return mIsDirty; } |
michael@0 | 149 | uint32_t MemoryUsage() { return sizeof(CacheFileMetadata) + mHashArraySize + mBufSize; } |
michael@0 | 150 | |
michael@0 | 151 | NS_IMETHOD OnFileOpened(CacheFileHandle *aHandle, nsresult aResult); |
michael@0 | 152 | NS_IMETHOD OnDataWritten(CacheFileHandle *aHandle, const char *aBuf, |
michael@0 | 153 | nsresult aResult); |
michael@0 | 154 | NS_IMETHOD OnDataRead(CacheFileHandle *aHandle, char *aBuf, nsresult aResult); |
michael@0 | 155 | NS_IMETHOD OnFileDoomed(CacheFileHandle *aHandle, nsresult aResult); |
michael@0 | 156 | NS_IMETHOD OnEOFSet(CacheFileHandle *aHandle, nsresult aResult); |
michael@0 | 157 | NS_IMETHOD OnFileRenamed(CacheFileHandle *aHandle, nsresult aResult); |
michael@0 | 158 | |
michael@0 | 159 | // Memory reporting |
michael@0 | 160 | size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
michael@0 | 161 | size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; |
michael@0 | 162 | |
michael@0 | 163 | private: |
michael@0 | 164 | virtual ~CacheFileMetadata(); |
michael@0 | 165 | |
michael@0 | 166 | void InitEmptyMetadata(); |
michael@0 | 167 | nsresult ParseMetadata(uint32_t aMetaOffset, uint32_t aBufOffset, bool aHaveKey); |
michael@0 | 168 | nsresult CheckElements(const char *aBuf, uint32_t aSize); |
michael@0 | 169 | void EnsureBuffer(uint32_t aSize); |
michael@0 | 170 | nsresult ParseKey(const nsACString &aKey); |
michael@0 | 171 | |
michael@0 | 172 | nsRefPtr<CacheFileHandle> mHandle; |
michael@0 | 173 | nsCString mKey; |
michael@0 | 174 | CacheHash::Hash16_t *mHashArray; |
michael@0 | 175 | uint32_t mHashArraySize; |
michael@0 | 176 | uint32_t mHashCount; |
michael@0 | 177 | int64_t mOffset; |
michael@0 | 178 | char *mBuf; // used for parsing, then points |
michael@0 | 179 | // to elements |
michael@0 | 180 | uint32_t mBufSize; |
michael@0 | 181 | char *mWriteBuf; |
michael@0 | 182 | CacheFileMetadataHeader mMetaHdr; |
michael@0 | 183 | uint32_t mElementsSize; |
michael@0 | 184 | bool mIsDirty; |
michael@0 | 185 | bool mAnonymous; |
michael@0 | 186 | bool mInBrowser; |
michael@0 | 187 | uint32_t mAppId; |
michael@0 | 188 | nsCOMPtr<CacheFileMetadataListener> mListener; |
michael@0 | 189 | }; |
michael@0 | 190 | |
michael@0 | 191 | |
michael@0 | 192 | } // net |
michael@0 | 193 | } // mozilla |
michael@0 | 194 | |
michael@0 | 195 | #endif |