nsCacheEntryDescriptor.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
  2. *
  3. * This Source Code Form is subject to the terms of the Mozilla Public
  4. * License, v. 2.0. If a copy of the MPL was not distributed with this
  5. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  6. #ifndef _nsCacheEntryDescriptor_h_
  7. #define _nsCacheEntryDescriptor_h_
  8. #include "nsICacheEntryDescriptor.h"
  9. #include "nsCacheEntry.h"
  10. #include "nsIInputStream.h"
  11. #include "nsIOutputStream.h"
  12. #include "nsCacheService.h"
  13. #include "zlib.h"
  14. #include "mozilla/Mutex.h"
  15. /******************************************************************************
  16. * nsCacheEntryDescriptor
  17. *******************************************************************************/
  18. class nsCacheEntryDescriptor final :
  19. public PRCList,
  20. public nsICacheEntryDescriptor
  21. {
  22. public:
  23. NS_DECL_THREADSAFE_ISUPPORTS
  24. NS_DECL_NSICACHEENTRYDESCRIPTOR
  25. NS_DECL_NSICACHEENTRYINFO
  26. friend class nsAsyncDoomEvent;
  27. friend class nsCacheService;
  28. nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode);
  29. /**
  30. * utility method to attempt changing data size of associated entry
  31. */
  32. nsresult RequestDataSizeChange(int32_t deltaSize);
  33. /**
  34. * methods callbacks for nsCacheService
  35. */
  36. nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
  37. bool ClearCacheEntry(void)
  38. {
  39. NS_ASSERTION(mInputWrappers.IsEmpty(), "Bad state");
  40. NS_ASSERTION(!mOutputWrapper, "Bad state");
  41. bool doomEntry = false;
  42. bool asyncDoomPending;
  43. {
  44. mozilla::MutexAutoLock lock(mLock);
  45. asyncDoomPending = mAsyncDoomPending;
  46. }
  47. if (asyncDoomPending && mCacheEntry) {
  48. doomEntry = true;
  49. mDoomedOnClose = true;
  50. }
  51. mCacheEntry = nullptr;
  52. return doomEntry;
  53. }
  54. private:
  55. virtual ~nsCacheEntryDescriptor();
  56. /*************************************************************************
  57. * input stream wrapper class -
  58. *
  59. * The input stream wrapper references the descriptor, but the descriptor
  60. * doesn't need any references to the stream wrapper.
  61. *************************************************************************/
  62. class nsInputStreamWrapper : public nsIInputStream {
  63. friend class nsCacheEntryDescriptor;
  64. private:
  65. nsCacheEntryDescriptor * mDescriptor;
  66. nsCOMPtr<nsIInputStream> mInput;
  67. uint32_t mStartOffset;
  68. bool mInitialized;
  69. mozilla::Mutex mLock;
  70. public:
  71. NS_DECL_THREADSAFE_ISUPPORTS
  72. NS_DECL_NSIINPUTSTREAM
  73. nsInputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
  74. : mDescriptor(desc)
  75. , mStartOffset(off)
  76. , mInitialized(false)
  77. , mLock("nsInputStreamWrapper.mLock")
  78. {
  79. NS_ADDREF(mDescriptor);
  80. }
  81. private:
  82. virtual ~nsInputStreamWrapper()
  83. {
  84. NS_IF_RELEASE(mDescriptor);
  85. }
  86. nsresult LazyInit();
  87. nsresult EnsureInit();
  88. nsresult Read_Locked(char *buf, uint32_t count, uint32_t *countRead);
  89. nsresult Close_Locked();
  90. void CloseInternal();
  91. };
  92. class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
  93. private:
  94. unsigned char* mReadBuffer;
  95. uint32_t mReadBufferLen;
  96. z_stream mZstream;
  97. bool mStreamInitialized;
  98. bool mStreamEnded;
  99. public:
  100. NS_DECL_ISUPPORTS_INHERITED
  101. nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
  102. uint32_t off)
  103. : nsInputStreamWrapper(desc, off)
  104. , mReadBuffer(0)
  105. , mReadBufferLen(0)
  106. , mStreamInitialized(false)
  107. , mStreamEnded(false)
  108. {
  109. }
  110. NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result) override;
  111. NS_IMETHOD Close() override;
  112. private:
  113. virtual ~nsDecompressInputStreamWrapper()
  114. {
  115. Close();
  116. }
  117. nsresult InitZstream();
  118. nsresult EndZstream();
  119. };
  120. /*************************************************************************
  121. * output stream wrapper class -
  122. *
  123. * The output stream wrapper references the descriptor, but the descriptor
  124. * doesn't need any references to the stream wrapper.
  125. *************************************************************************/
  126. class nsOutputStreamWrapper : public nsIOutputStream {
  127. friend class nsCacheEntryDescriptor;
  128. protected:
  129. nsCacheEntryDescriptor * mDescriptor;
  130. nsCOMPtr<nsIOutputStream> mOutput;
  131. uint32_t mStartOffset;
  132. bool mInitialized;
  133. mozilla::Mutex mLock;
  134. public:
  135. NS_DECL_THREADSAFE_ISUPPORTS
  136. NS_DECL_NSIOUTPUTSTREAM
  137. nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
  138. : mDescriptor(desc)
  139. , mStartOffset(off)
  140. , mInitialized(false)
  141. , mLock("nsOutputStreamWrapper.mLock")
  142. {
  143. NS_ADDREF(mDescriptor); // owning ref
  144. }
  145. private:
  146. virtual ~nsOutputStreamWrapper()
  147. {
  148. Close();
  149. NS_ASSERTION(!mOutput, "Bad state");
  150. NS_ASSERTION(!mDescriptor, "Bad state");
  151. }
  152. nsresult LazyInit();
  153. nsresult EnsureInit();
  154. nsresult OnWrite(uint32_t count);
  155. nsresult Write_Locked(const char * buf,
  156. uint32_t count,
  157. uint32_t * result);
  158. nsresult Close_Locked();
  159. void CloseInternal();
  160. };
  161. class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
  162. private:
  163. unsigned char* mWriteBuffer;
  164. uint32_t mWriteBufferLen;
  165. z_stream mZstream;
  166. bool mStreamInitialized;
  167. bool mStreamEnded;
  168. uint32_t mUncompressedCount;
  169. public:
  170. NS_DECL_ISUPPORTS_INHERITED
  171. nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc,
  172. uint32_t off)
  173. : nsOutputStreamWrapper(desc, off)
  174. , mWriteBuffer(0)
  175. , mWriteBufferLen(0)
  176. , mStreamInitialized(false)
  177. , mStreamEnded(false)
  178. , mUncompressedCount(0)
  179. {
  180. }
  181. NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t * result) override;
  182. NS_IMETHOD Close() override;
  183. private:
  184. virtual ~nsCompressOutputStreamWrapper()
  185. {
  186. Close();
  187. }
  188. nsresult InitZstream();
  189. nsresult WriteBuffer();
  190. };
  191. private:
  192. /**
  193. * nsCacheEntryDescriptor data members
  194. */
  195. nsCacheEntry * mCacheEntry; // we are a child of the entry
  196. nsCacheAccessMode mAccessGranted;
  197. nsTArray<nsInputStreamWrapper*> mInputWrappers;
  198. nsOutputStreamWrapper * mOutputWrapper;
  199. mozilla::Mutex mLock;
  200. bool mAsyncDoomPending;
  201. bool mDoomedOnClose;
  202. bool mClosingDescriptor;
  203. };
  204. #endif // _nsCacheEntryDescriptor_h_