netwerk/cache2/CacheFileChunk.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 CacheFileChunk__h__
     6 #define CacheFileChunk__h__
     8 #include "CacheFileIOManager.h"
     9 #include "CacheStorageService.h"
    10 #include "CacheHashUtils.h"
    11 #include "nsAutoPtr.h"
    12 #include "mozilla/Mutex.h"
    14 namespace mozilla {
    15 namespace net {
    17 #define kChunkSize        (256 * 1024)
    18 #define kEmptyChunkHash   0x1826
    20 class CacheFileChunk;
    21 class CacheFile;
    22 class ValidityPair;
    25 #define CACHEFILECHUNKLISTENER_IID \
    26 { /* baf16149-2ab5-499c-a9c2-5904eb95c288 */       \
    27   0xbaf16149,                                      \
    28   0x2ab5,                                          \
    29   0x499c,                                          \
    30   {0xa9, 0xc2, 0x59, 0x04, 0xeb, 0x95, 0xc2, 0x88} \
    31 }
    33 class CacheFileChunkListener : public nsISupports
    34 {
    35 public:
    36   NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILECHUNKLISTENER_IID)
    38   NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk *aChunk) = 0;
    39   NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk *aChunk) = 0;
    40   NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx,
    41                               CacheFileChunk *aChunk) = 0;
    42   NS_IMETHOD OnChunkUpdated(CacheFileChunk *aChunk) = 0;
    43 };
    45 NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileChunkListener,
    46                               CACHEFILECHUNKLISTENER_IID)
    49 class ChunkListenerItem {
    50 public:
    51   ChunkListenerItem()  { MOZ_COUNT_CTOR(ChunkListenerItem); }
    52   ~ChunkListenerItem() { MOZ_COUNT_DTOR(ChunkListenerItem); }
    54   nsCOMPtr<nsIEventTarget>         mTarget;
    55   nsCOMPtr<CacheFileChunkListener> mCallback;
    56 };
    58 class ChunkListeners {
    59 public:
    60   ChunkListeners()  { MOZ_COUNT_CTOR(ChunkListeners); }
    61   ~ChunkListeners() { MOZ_COUNT_DTOR(ChunkListeners); }
    63   nsTArray<ChunkListenerItem *> mItems;
    64 };
    66 class CacheFileChunk : public CacheFileIOListener
    67                      , public CacheMemoryConsumer
    68 {
    69 public:
    70   NS_DECL_THREADSAFE_ISUPPORTS
    72   CacheFileChunk(CacheFile *aFile, uint32_t aIndex);
    74   void     InitNew(CacheFileChunkListener *aCallback);
    75   nsresult Read(CacheFileHandle *aHandle, uint32_t aLen,
    76                 CacheHash::Hash16_t aHash,
    77                 CacheFileChunkListener *aCallback);
    78   nsresult Write(CacheFileHandle *aHandle, CacheFileChunkListener *aCallback);
    79   void     WaitForUpdate(CacheFileChunkListener *aCallback);
    80   nsresult CancelWait(CacheFileChunkListener *aCallback);
    81   nsresult NotifyUpdateListeners();
    83   uint32_t            Index();
    84   CacheHash::Hash16_t Hash();
    85   uint32_t            DataSize();
    86   void                UpdateDataSize(uint32_t aOffset, uint32_t aLen,
    87                                      bool aEOF);
    89   NS_IMETHOD OnFileOpened(CacheFileHandle *aHandle, nsresult aResult);
    90   NS_IMETHOD OnDataWritten(CacheFileHandle *aHandle, const char *aBuf,
    91                            nsresult aResult);
    92   NS_IMETHOD OnDataRead(CacheFileHandle *aHandle, char *aBuf, nsresult aResult);
    93   NS_IMETHOD OnFileDoomed(CacheFileHandle *aHandle, nsresult aResult);
    94   NS_IMETHOD OnEOFSet(CacheFileHandle *aHandle, nsresult aResult);
    95   NS_IMETHOD OnFileRenamed(CacheFileHandle *aHandle, nsresult aResult);
    97   bool   IsReady() const;
    98   bool   IsDirty() const;
   100   nsresult GetStatus();
   101   void     SetError(nsresult aStatus);
   103   char *       BufForWriting() const;
   104   const char * BufForReading() const;
   105   void         EnsureBufSize(uint32_t aBufSize);
   106   uint32_t     MemorySize() const { return sizeof(CacheFileChunk) + mRWBufSize + mBufSize; }
   108   // Memory reporting
   109   size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
   110   size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
   112 private:
   113   friend class CacheFileInputStream;
   114   friend class CacheFileOutputStream;
   115   friend class CacheFile;
   117   virtual ~CacheFileChunk();
   119   enum EState {
   120     INITIAL = 0,
   121     READING = 1,
   122     WRITING = 2,
   123     READY   = 3,
   124     ERROR   = 4
   125   };
   127   uint32_t mIndex;
   128   EState   mState;
   129   nsresult mStatus;
   130   bool     mIsDirty;
   131   bool     mRemovingChunk;
   132   uint32_t mDataSize;
   134   char    *mBuf;
   135   uint32_t mBufSize;
   137   char               *mRWBuf;
   138   uint32_t            mRWBufSize;
   139   CacheHash::Hash16_t mReadHash;
   141   nsRefPtr<CacheFile>              mFile; // is null if chunk is cached to
   142                                           // prevent reference cycles
   143   nsCOMPtr<CacheFileChunkListener> mListener;
   144   nsTArray<ChunkListenerItem *>    mUpdateListeners;
   145   nsTArray<ValidityPair>           mValidityMap;
   146 };
   149 } // net
   150 } // mozilla
   152 #endif

mercurial