netwerk/cache2/CacheFileChunk.h

changeset 0
6474c204b198
equal deleted inserted replaced
-1:000000000000 0:b47ff619ec53
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/. */
4
5 #ifndef CacheFileChunk__h__
6 #define CacheFileChunk__h__
7
8 #include "CacheFileIOManager.h"
9 #include "CacheStorageService.h"
10 #include "CacheHashUtils.h"
11 #include "nsAutoPtr.h"
12 #include "mozilla/Mutex.h"
13
14 namespace mozilla {
15 namespace net {
16
17 #define kChunkSize (256 * 1024)
18 #define kEmptyChunkHash 0x1826
19
20 class CacheFileChunk;
21 class CacheFile;
22 class ValidityPair;
23
24
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 }
32
33 class CacheFileChunkListener : public nsISupports
34 {
35 public:
36 NS_DECLARE_STATIC_IID_ACCESSOR(CACHEFILECHUNKLISTENER_IID)
37
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 };
44
45 NS_DEFINE_STATIC_IID_ACCESSOR(CacheFileChunkListener,
46 CACHEFILECHUNKLISTENER_IID)
47
48
49 class ChunkListenerItem {
50 public:
51 ChunkListenerItem() { MOZ_COUNT_CTOR(ChunkListenerItem); }
52 ~ChunkListenerItem() { MOZ_COUNT_DTOR(ChunkListenerItem); }
53
54 nsCOMPtr<nsIEventTarget> mTarget;
55 nsCOMPtr<CacheFileChunkListener> mCallback;
56 };
57
58 class ChunkListeners {
59 public:
60 ChunkListeners() { MOZ_COUNT_CTOR(ChunkListeners); }
61 ~ChunkListeners() { MOZ_COUNT_DTOR(ChunkListeners); }
62
63 nsTArray<ChunkListenerItem *> mItems;
64 };
65
66 class CacheFileChunk : public CacheFileIOListener
67 , public CacheMemoryConsumer
68 {
69 public:
70 NS_DECL_THREADSAFE_ISUPPORTS
71
72 CacheFileChunk(CacheFile *aFile, uint32_t aIndex);
73
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();
82
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);
88
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);
96
97 bool IsReady() const;
98 bool IsDirty() const;
99
100 nsresult GetStatus();
101 void SetError(nsresult aStatus);
102
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; }
107
108 // Memory reporting
109 size_t SizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
110 size_t SizeOfIncludingThis(mozilla::MallocSizeOf mallocSizeOf) const;
111
112 private:
113 friend class CacheFileInputStream;
114 friend class CacheFileOutputStream;
115 friend class CacheFile;
116
117 virtual ~CacheFileChunk();
118
119 enum EState {
120 INITIAL = 0,
121 READING = 1,
122 WRITING = 2,
123 READY = 3,
124 ERROR = 4
125 };
126
127 uint32_t mIndex;
128 EState mState;
129 nsresult mStatus;
130 bool mIsDirty;
131 bool mRemovingChunk;
132 uint32_t mDataSize;
133
134 char *mBuf;
135 uint32_t mBufSize;
136
137 char *mRWBuf;
138 uint32_t mRWBufSize;
139 CacheHash::Hash16_t mReadHash;
140
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 };
147
148
149 } // net
150 } // mozilla
151
152 #endif

mercurial