netwerk/cache2/CacheFileMetadata.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

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

mercurial