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