1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache2/CacheFileMetadata.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,195 @@ 1.4 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.5 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.6 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.7 + 1.8 +#ifndef CacheFileMetadata__h__ 1.9 +#define CacheFileMetadata__h__ 1.10 + 1.11 +#include "CacheFileIOManager.h" 1.12 +#include "CacheStorageService.h" 1.13 +#include "CacheHashUtils.h" 1.14 +#include "CacheObserver.h" 1.15 +#include "mozilla/Endian.h" 1.16 +#include "nsAutoPtr.h" 1.17 +#include "nsString.h" 1.18 + 1.19 +namespace mozilla { 1.20 +namespace net { 1.21 + 1.22 +// By multiplying with the current half-life we convert the frecency 1.23 +// to time independent of half-life value. The range fits 32bits. 1.24 +// When decay time changes on next run of the browser, we convert 1.25 +// the frecency value to a correct internal representation again. 1.26 +// It might not be 100% accurate, but for the purpose it suffice. 1.27 +#define FRECENCY2INT(aFrecency) \ 1.28 + ((uint32_t)(aFrecency * CacheObserver::HalfLifeSeconds())) 1.29 +#define INT2FRECENCY(aInt) \ 1.30 + ((double)(aInt) / (double)CacheObserver::HalfLifeSeconds()) 1.31 + 1.32 + 1.33 +#pragma pack(push) 1.34 +#pragma pack(1) 1.35 + 1.36 +class CacheFileMetadataHeader { 1.37 +public: 1.38 + uint32_t mVersion; 1.39 + uint32_t mFetchCount; 1.40 + uint32_t mLastFetched; 1.41 + uint32_t mLastModified; 1.42 + uint32_t mFrecency; 1.43 + uint32_t mExpirationTime; 1.44 + uint32_t mKeySize; 1.45 + 1.46 + void WriteToBuf(void *aBuf) 1.47 + { 1.48 + EnsureCorrectClassSize(); 1.49 + 1.50 + uint8_t* ptr = static_cast<uint8_t*>(aBuf); 1.51 + NetworkEndian::writeUint32(ptr, mVersion); ptr += sizeof(uint32_t); 1.52 + NetworkEndian::writeUint32(ptr, mFetchCount); ptr += sizeof(uint32_t); 1.53 + NetworkEndian::writeUint32(ptr, mLastFetched); ptr += sizeof(uint32_t); 1.54 + NetworkEndian::writeUint32(ptr, mLastModified); ptr += sizeof(uint32_t); 1.55 + NetworkEndian::writeUint32(ptr, mFrecency); ptr += sizeof(uint32_t); 1.56 + NetworkEndian::writeUint32(ptr, mExpirationTime); ptr += sizeof(uint32_t); 1.57 + NetworkEndian::writeUint32(ptr, mKeySize); 1.58 + } 1.59 + 1.60 + void ReadFromBuf(const void *aBuf) 1.61 + { 1.62 + EnsureCorrectClassSize(); 1.63 + 1.64 + const uint8_t* ptr = static_cast<const uint8_t*>(aBuf); 1.65 + mVersion = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.66 + mFetchCount = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.67 + mLastFetched = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.68 + mLastModified = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.69 + mFrecency = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.70 + mExpirationTime = BigEndian::readUint32(ptr); ptr += sizeof(uint32_t); 1.71 + mKeySize = BigEndian::readUint32(ptr); 1.72 + } 1.73 + 1.74 + inline void EnsureCorrectClassSize() 1.75 + { 1.76 + static_assert((sizeof(mVersion) + sizeof(mFetchCount) + 1.77 + sizeof(mLastFetched) + sizeof(mLastModified) + sizeof(mFrecency) + 1.78 + sizeof(mExpirationTime) + sizeof(mKeySize)) == 1.79 + sizeof(CacheFileMetadataHeader), 1.80 + "Unexpected sizeof(CacheFileMetadataHeader)!"); 1.81 + } 1.82 +}; 1.83 + 1.84 +#pragma pack(pop) 1.85 + 1.86 + 1.87 +#define CACHEFILEMETADATALISTENER_IID \ 1.88 +{ /* a9e36125-3f01-4020-9540-9dafa8d31ba7 */ \ 1.89 + 0xa9e36125, \ 1.90 + 0x3f01, \ 1.91 + 0x4020, \ 1.92 + {0x95, 0x40, 0x9d, 0xaf, 0xa8, 0xd3, 0x1b, 0xa7} \ 1.93 +} 1.94 + 1.95 +class CacheFileMetadataListener : public nsISupports 1.96 +{ 1.97 +public: 1.98 + NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILEMETADATALISTENER_IID) 1.99 + 1.100 + NS_IMETHOD OnMetadataRead(nsresult aResult) = 0; 1.101 + NS_IMETHOD OnMetadataWritten(nsresult aResult) = 0; 1.102 +}; 1.103 + 1.104 +NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileMetadataListener, 1.105 + CACHEFILEMETADATALISTENER_IID) 1.106 + 1.107 + 1.108 +class CacheFileMetadata : public CacheFileIOListener 1.109 + , public CacheMemoryConsumer 1.110 +{ 1.111 +public: 1.112 + NS_DECL_THREADSAFE_ISUPPORTS 1.113 + 1.114 + CacheFileMetadata(CacheFileHandle *aHandle, 1.115 + const nsACString &aKey); 1.116 + CacheFileMetadata(bool aMemoryOnly, 1.117 + const nsACString &aKey); 1.118 + CacheFileMetadata(); 1.119 + 1.120 + void SetHandle(CacheFileHandle *aHandle); 1.121 + 1.122 + nsresult GetKey(nsACString &_retval); 1.123 + 1.124 + nsresult ReadMetadata(CacheFileMetadataListener *aListener); 1.125 + nsresult WriteMetadata(uint32_t aOffset, 1.126 + CacheFileMetadataListener *aListener); 1.127 + nsresult SyncReadMetadata(nsIFile *aFile); 1.128 + 1.129 + bool IsAnonymous() { return mAnonymous; } 1.130 + bool IsInBrowser() { return mInBrowser; } 1.131 + uint32_t AppId() { return mAppId; } 1.132 + 1.133 + const char * GetElement(const char *aKey); 1.134 + nsresult SetElement(const char *aKey, const char *aValue); 1.135 + 1.136 + CacheHash::Hash16_t GetHash(uint32_t aIndex); 1.137 + nsresult SetHash(uint32_t aIndex, CacheHash::Hash16_t aHash); 1.138 + 1.139 + nsresult SetExpirationTime(uint32_t aExpirationTime); 1.140 + nsresult GetExpirationTime(uint32_t *_retval); 1.141 + nsresult SetLastModified(uint32_t aLastModified); 1.142 + nsresult GetLastModified(uint32_t *_retval); 1.143 + nsresult SetFrecency(uint32_t aFrecency); 1.144 + nsresult GetFrecency(uint32_t *_retval); 1.145 + nsresult GetLastFetched(uint32_t *_retval); 1.146 + nsresult GetFetchCount(uint32_t *_retval); 1.147 + 1.148 + int64_t Offset() { return mOffset; } 1.149 + uint32_t ElementsSize() { return mElementsSize; } 1.150 + void MarkDirty() { mIsDirty = true; } 1.151 + bool IsDirty() { return mIsDirty; } 1.152 + uint32_t MemoryUsage() { return sizeof(CacheFileMetadata) + mHashArraySize + mBufSize; } 1.153 + 1.154 + NS_IMETHOD OnFileOpened(CacheFileHandle *aHandle, nsresult aResult); 1.155 + NS_IMETHOD OnDataWritten(CacheFileHandle *aHandle, const char *aBuf, 1.156 + nsresult aResult); 1.157 + NS_IMETHOD OnDataRead(CacheFileHandle *aHandle, char *aBuf, nsresult aResult); 1.158 + NS_IMETHOD OnFileDoomed(CacheFileHandle *aHandle, nsresult aResult); 1.159 + NS_IMETHOD OnEOFSet(CacheFileHandle *aHandle, nsresult aResult); 1.160 + NS_IMETHOD OnFileRenamed(CacheFileHandle *aHandle, nsresult aResult); 1.161 + 1.162 + // Memory reporting 1.163 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.164 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.165 + 1.166 +private: 1.167 + virtual ~CacheFileMetadata(); 1.168 + 1.169 + void InitEmptyMetadata(); 1.170 + nsresult ParseMetadata(uint32_t aMetaOffset, uint32_t aBufOffset, bool aHaveKey); 1.171 + nsresult CheckElements(const char *aBuf, uint32_t aSize); 1.172 + void EnsureBuffer(uint32_t aSize); 1.173 + nsresult ParseKey(const nsACString &aKey); 1.174 + 1.175 + nsRefPtr<CacheFileHandle> mHandle; 1.176 + nsCString mKey; 1.177 + CacheHash::Hash16_t *mHashArray; 1.178 + uint32_t mHashArraySize; 1.179 + uint32_t mHashCount; 1.180 + int64_t mOffset; 1.181 + char *mBuf; // used for parsing, then points 1.182 + // to elements 1.183 + uint32_t mBufSize; 1.184 + char *mWriteBuf; 1.185 + CacheFileMetadataHeader mMetaHdr; 1.186 + uint32_t mElementsSize; 1.187 + bool mIsDirty; 1.188 + bool mAnonymous; 1.189 + bool mInBrowser; 1.190 + uint32_t mAppId; 1.191 + nsCOMPtr<CacheFileMetadataListener> mListener; 1.192 +}; 1.193 + 1.194 + 1.195 +} // net 1.196 +} // mozilla 1.197 + 1.198 +#endif