1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/netwerk/cache2/CacheFileChunk.h Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,152 @@ 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 CacheFileChunk__h__ 1.9 +#define CacheFileChunk__h__ 1.10 + 1.11 +#include "CacheFileIOManager.h" 1.12 +#include "CacheStorageService.h" 1.13 +#include "CacheHashUtils.h" 1.14 +#include "nsAutoPtr.h" 1.15 +#include "mozilla/Mutex.h" 1.16 + 1.17 +namespace mozilla { 1.18 +namespace net { 1.19 + 1.20 +#define kChunkSize (256 * 1024) 1.21 +#define kEmptyChunkHash 0x1826 1.22 + 1.23 +class CacheFileChunk; 1.24 +class CacheFile; 1.25 +class ValidityPair; 1.26 + 1.27 + 1.28 +#define CACHEFILECHUNKLISTENER_IID \ 1.29 +{ /* baf16149-2ab5-499c-a9c2-5904eb95c288 */ \ 1.30 + 0xbaf16149, \ 1.31 + 0x2ab5, \ 1.32 + 0x499c, \ 1.33 + {0xa9, 0xc2, 0x59, 0x04, 0xeb, 0x95, 0xc2, 0x88} \ 1.34 +} 1.35 + 1.36 +class CacheFileChunkListener : public nsISupports 1.37 +{ 1.38 +public: 1.39 + NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILECHUNKLISTENER_IID) 1.40 + 1.41 + NS_IMETHOD OnChunkRead(nsresult aResult, CacheFileChunk *aChunk) = 0; 1.42 + NS_IMETHOD OnChunkWritten(nsresult aResult, CacheFileChunk *aChunk) = 0; 1.43 + NS_IMETHOD OnChunkAvailable(nsresult aResult, uint32_t aChunkIdx, 1.44 + CacheFileChunk *aChunk) = 0; 1.45 + NS_IMETHOD OnChunkUpdated(CacheFileChunk *aChunk) = 0; 1.46 +}; 1.47 + 1.48 +NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileChunkListener, 1.49 + CACHEFILECHUNKLISTENER_IID) 1.50 + 1.51 + 1.52 +class ChunkListenerItem { 1.53 +public: 1.54 + ChunkListenerItem() { MOZ_COUNT_CTOR(ChunkListenerItem); } 1.55 + ~ChunkListenerItem() { MOZ_COUNT_DTOR(ChunkListenerItem); } 1.56 + 1.57 + nsCOMPtr<nsIEventTarget> mTarget; 1.58 + nsCOMPtr<CacheFileChunkListener> mCallback; 1.59 +}; 1.60 + 1.61 +class ChunkListeners { 1.62 +public: 1.63 + ChunkListeners() { MOZ_COUNT_CTOR(ChunkListeners); } 1.64 + ~ChunkListeners() { MOZ_COUNT_DTOR(ChunkListeners); } 1.65 + 1.66 + nsTArray<ChunkListenerItem *> mItems; 1.67 +}; 1.68 + 1.69 +class CacheFileChunk : public CacheFileIOListener 1.70 + , public CacheMemoryConsumer 1.71 +{ 1.72 +public: 1.73 + NS_DECL_THREADSAFE_ISUPPORTS 1.74 + 1.75 + CacheFileChunk(CacheFile *aFile, uint32_t aIndex); 1.76 + 1.77 + void InitNew(CacheFileChunkListener *aCallback); 1.78 + nsresult Read(CacheFileHandle *aHandle, uint32_t aLen, 1.79 + CacheHash::Hash16_t aHash, 1.80 + CacheFileChunkListener *aCallback); 1.81 + nsresult Write(CacheFileHandle *aHandle, CacheFileChunkListener *aCallback); 1.82 + void WaitForUpdate(CacheFileChunkListener *aCallback); 1.83 + nsresult CancelWait(CacheFileChunkListener *aCallback); 1.84 + nsresult NotifyUpdateListeners(); 1.85 + 1.86 + uint32_t Index(); 1.87 + CacheHash::Hash16_t Hash(); 1.88 + uint32_t DataSize(); 1.89 + void UpdateDataSize(uint32_t aOffset, uint32_t aLen, 1.90 + bool aEOF); 1.91 + 1.92 + NS_IMETHOD OnFileOpened(CacheFileHandle *aHandle, nsresult aResult); 1.93 + NS_IMETHOD OnDataWritten(CacheFileHandle *aHandle, const char *aBuf, 1.94 + nsresult aResult); 1.95 + NS_IMETHOD OnDataRead(CacheFileHandle *aHandle, char *aBuf, nsresult aResult); 1.96 + NS_IMETHOD OnFileDoomed(CacheFileHandle *aHandle, nsresult aResult); 1.97 + NS_IMETHOD OnEOFSet(CacheFileHandle *aHandle, nsresult aResult); 1.98 + NS_IMETHOD OnFileRenamed(CacheFileHandle *aHandle, nsresult aResult); 1.99 + 1.100 + bool IsReady() const; 1.101 + bool IsDirty() const; 1.102 + 1.103 + nsresult GetStatus(); 1.104 + void SetError(nsresult aStatus); 1.105 + 1.106 + char * BufForWriting() const; 1.107 + const char * BufForReading() const; 1.108 + void EnsureBufSize(uint32_t aBufSize); 1.109 + uint32_t MemorySize() const { return sizeof(CacheFileChunk) + mRWBufSize + mBufSize; } 1.110 + 1.111 + // Memory reporting 1.112 + size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.113 + size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const; 1.114 + 1.115 +private: 1.116 + friend class CacheFileInputStream; 1.117 + friend class CacheFileOutputStream; 1.118 + friend class CacheFile; 1.119 + 1.120 + virtual ~CacheFileChunk(); 1.121 + 1.122 + enum EState { 1.123 + INITIAL = 0, 1.124 + READING = 1, 1.125 + WRITING = 2, 1.126 + READY = 3, 1.127 + ERROR = 4 1.128 + }; 1.129 + 1.130 + uint32_t mIndex; 1.131 + EState mState; 1.132 + nsresult mStatus; 1.133 + bool mIsDirty; 1.134 + bool mRemovingChunk; 1.135 + uint32_t mDataSize; 1.136 + 1.137 + char *mBuf; 1.138 + uint32_t mBufSize; 1.139 + 1.140 + char *mRWBuf; 1.141 + uint32_t mRWBufSize; 1.142 + CacheHash::Hash16_t mReadHash; 1.143 + 1.144 + nsRefPtr<CacheFile> mFile; // is null if chunk is cached to 1.145 + // prevent reference cycles 1.146 + nsCOMPtr<CacheFileChunkListener> mListener; 1.147 + nsTArray<ChunkListenerItem *> mUpdateListeners; 1.148 + nsTArray<ValidityPair> mValidityMap; 1.149 +}; 1.150 + 1.151 + 1.152 +} // net 1.153 +} // mozilla 1.154 + 1.155 +#endif