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 "CacheFileUtils.h"
12 #include "nsAutoPtr.h"
13 #include "mozilla/Mutex.h"
18 #define kChunkSize (256 * 1024)
19 #define kEmptyChunkHash 0x1826
25 #define CACHEFILECHUNKLISTENER_IID \
26 { /* baf16149-2ab5-499c-a9c2-5904eb95c288 */ \
30 {0xa9, 0xc2, 0x59, 0x04, 0xeb, 0x95, 0xc2, 0x88} \
33 class CacheFileChunkListener
: public nsISupports
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;
45 NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileChunkListener
,
46 CACHEFILECHUNKLISTENER_IID
)
49 class ChunkListenerItem
{
51 ChunkListenerItem() { MOZ_COUNT_CTOR(ChunkListenerItem
); }
52 ~ChunkListenerItem() { MOZ_COUNT_DTOR(ChunkListenerItem
); }
54 nsCOMPtr
<nsIEventTarget
> mTarget
;
55 nsCOMPtr
<CacheFileChunkListener
> mCallback
;
58 class ChunkListeners
{
60 ChunkListeners() { MOZ_COUNT_CTOR(ChunkListeners
); }
61 ~ChunkListeners() { MOZ_COUNT_DTOR(ChunkListeners
); }
63 nsTArray
<ChunkListenerItem
*> mItems
;
66 class CacheFileChunk
: public CacheFileIOListener
67 , public CacheMemoryConsumer
70 NS_DECL_THREADSAFE_ISUPPORTS
71 bool DispatchRelease();
73 CacheFileChunk(CacheFile
*aFile
, uint32_t aIndex
, bool aInitByWriter
);
76 nsresult
Read(CacheFileHandle
*aHandle
, uint32_t aLen
,
77 CacheHash::Hash16_t aHash
,
78 CacheFileChunkListener
*aCallback
);
79 nsresult
Write(CacheFileHandle
*aHandle
, CacheFileChunkListener
*aCallback
);
80 void WaitForUpdate(CacheFileChunkListener
*aCallback
);
81 nsresult
CancelWait(CacheFileChunkListener
*aCallback
);
82 nsresult
NotifyUpdateListeners();
85 CacheHash::Hash16_t
Hash();
87 void UpdateDataSize(uint32_t aOffset
, uint32_t aLen
,
90 NS_IMETHOD
OnFileOpened(CacheFileHandle
*aHandle
, nsresult aResult
);
91 NS_IMETHOD
OnDataWritten(CacheFileHandle
*aHandle
, const char *aBuf
,
93 NS_IMETHOD
OnDataRead(CacheFileHandle
*aHandle
, char *aBuf
, nsresult aResult
);
94 NS_IMETHOD
OnFileDoomed(CacheFileHandle
*aHandle
, nsresult aResult
);
95 NS_IMETHOD
OnEOFSet(CacheFileHandle
*aHandle
, nsresult aResult
);
96 NS_IMETHOD
OnFileRenamed(CacheFileHandle
*aHandle
, nsresult aResult
);
101 nsresult
GetStatus();
102 void SetError(nsresult aStatus
);
104 char * BufForWriting() const;
105 const char * BufForReading() const;
106 nsresult
EnsureBufSize(uint32_t aBufSize
);
107 uint32_t MemorySize() const { return sizeof(CacheFileChunk
) + mRWBufSize
+ mBufSize
; }
110 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
111 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf
) const;
114 friend class CacheFileInputStream
;
115 friend class CacheFileOutputStream
;
116 friend class CacheFile
;
118 virtual ~CacheFileChunk();
120 bool CanAllocate(uint32_t aSize
);
121 void ChunkAllocationChanged();
122 mozilla::Atomic
<uint32_t>& ChunksMemoryUsage();
135 bool mActiveChunk
; // Is true iff the chunk is in CacheFile::mChunks.
136 // Adding/removing chunk to/from mChunks as well as
137 // changing this member happens under the CacheFile's
141 uint32_t mReportedAllocation
;
142 bool const mLimitAllocation
: 1; // Whether this chunk respects limit for disk
143 // chunks memory usage.
144 bool const mIsPriority
: 1;
151 CacheHash::Hash16_t mReadHash
;
153 nsRefPtr
<CacheFile
> mFile
; // is null if chunk is cached to
154 // prevent reference cycles
155 nsCOMPtr
<CacheFileChunkListener
> mListener
;
156 nsTArray
<ChunkListenerItem
*> mUpdateListeners
;
157 CacheFileUtils::ValidityMap mValidityMap
;