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