netwerk/cache/nsCacheEntryDescriptor.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsCacheEntryDescriptor.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,235 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +
    1.11 +#ifndef _nsCacheEntryDescriptor_h_
    1.12 +#define _nsCacheEntryDescriptor_h_
    1.13 +
    1.14 +#include "nsICacheEntryDescriptor.h"
    1.15 +#include "nsCacheEntry.h"
    1.16 +#include "nsIInputStream.h"
    1.17 +#include "nsIOutputStream.h"
    1.18 +#include "nsCacheService.h"
    1.19 +#include "zlib.h"
    1.20 +#include "mozilla/Mutex.h"
    1.21 +#include "nsVoidArray.h"
    1.22 +
    1.23 +/******************************************************************************
    1.24 +* nsCacheEntryDescriptor
    1.25 +*******************************************************************************/
    1.26 +class nsCacheEntryDescriptor :
    1.27 +    public PRCList,
    1.28 +    public nsICacheEntryDescriptor
    1.29 +{
    1.30 +public:
    1.31 +    NS_DECL_THREADSAFE_ISUPPORTS
    1.32 +    NS_DECL_NSICACHEENTRYDESCRIPTOR
    1.33 +    NS_DECL_NSICACHEENTRYINFO
    1.34 +
    1.35 +    friend class nsAsyncDoomEvent;
    1.36 +    friend class nsCacheService;
    1.37 +
    1.38 +    nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode  mode);
    1.39 +    virtual ~nsCacheEntryDescriptor();
    1.40 +    
    1.41 +    /**
    1.42 +     * utility method to attempt changing data size of associated entry
    1.43 +     */
    1.44 +    nsresult  RequestDataSizeChange(int32_t deltaSize);
    1.45 +    
    1.46 +    /**
    1.47 +     * methods callbacks for nsCacheService
    1.48 +     */
    1.49 +    nsCacheEntry * CacheEntry(void)      { return mCacheEntry; }
    1.50 +    bool           ClearCacheEntry(void)
    1.51 +    {
    1.52 +      NS_ASSERTION(mInputWrappers.Count() == 0, "Bad state");
    1.53 +      NS_ASSERTION(!mOutputWrapper, "Bad state");
    1.54 +
    1.55 +      bool doomEntry = false;
    1.56 +      bool asyncDoomPending;
    1.57 +      {
    1.58 +        mozilla::MutexAutoLock lock(mLock);
    1.59 +        asyncDoomPending = mAsyncDoomPending;
    1.60 +      }
    1.61 +
    1.62 +      if (asyncDoomPending && mCacheEntry) {
    1.63 +        doomEntry = true;
    1.64 +        mDoomedOnClose = true;
    1.65 +      }
    1.66 +      mCacheEntry = nullptr;
    1.67 +
    1.68 +      return doomEntry;
    1.69 +    }
    1.70 +
    1.71 +private:
    1.72 +     /*************************************************************************
    1.73 +      * input stream wrapper class -
    1.74 +      *
    1.75 +      * The input stream wrapper references the descriptor, but the descriptor
    1.76 +      * doesn't need any references to the stream wrapper.
    1.77 +      *************************************************************************/
    1.78 +     class nsInputStreamWrapper : public nsIInputStream {
    1.79 +         friend class nsCacheEntryDescriptor;
    1.80 +
    1.81 +     private:
    1.82 +         nsCacheEntryDescriptor    * mDescriptor;
    1.83 +         nsCOMPtr<nsIInputStream>    mInput;
    1.84 +         uint32_t                    mStartOffset;
    1.85 +         bool                        mInitialized;
    1.86 +         mozilla::Mutex              mLock;
    1.87 +     public:
    1.88 +         NS_DECL_THREADSAFE_ISUPPORTS
    1.89 +         NS_DECL_NSIINPUTSTREAM
    1.90 +
    1.91 +         nsInputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
    1.92 +             : mDescriptor(desc)
    1.93 +             , mStartOffset(off)
    1.94 +             , mInitialized(false)
    1.95 +             , mLock("nsInputStreamWrapper.mLock")
    1.96 +         {
    1.97 +             NS_ADDREF(mDescriptor);
    1.98 +         }
    1.99 +         virtual ~nsInputStreamWrapper()
   1.100 +         {
   1.101 +             NS_IF_RELEASE(mDescriptor);
   1.102 +         }
   1.103 +
   1.104 +     private:
   1.105 +         nsresult LazyInit();
   1.106 +         nsresult EnsureInit();
   1.107 +         nsresult Read_Locked(char *buf, uint32_t count, uint32_t *countRead);
   1.108 +         nsresult Close_Locked();
   1.109 +         void CloseInternal();
   1.110 +     };
   1.111 +
   1.112 +
   1.113 +     class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
   1.114 +     private:
   1.115 +         unsigned char* mReadBuffer;
   1.116 +         uint32_t mReadBufferLen;
   1.117 +         z_stream mZstream;
   1.118 +         bool mStreamInitialized;
   1.119 +         bool mStreamEnded;
   1.120 +     public:
   1.121 +         NS_DECL_THREADSAFE_ISUPPORTS
   1.122 +
   1.123 +         nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
   1.124 +                                      uint32_t off)
   1.125 +          : nsInputStreamWrapper(desc, off)
   1.126 +          , mReadBuffer(0)
   1.127 +          , mReadBufferLen(0)
   1.128 +          , mStreamInitialized(false)
   1.129 +          , mStreamEnded(false)
   1.130 +         {
   1.131 +         }
   1.132 +         virtual ~nsDecompressInputStreamWrapper()
   1.133 +         {
   1.134 +             Close();
   1.135 +         }
   1.136 +         NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result);
   1.137 +         NS_IMETHOD Close();
   1.138 +     private:
   1.139 +         nsresult InitZstream();
   1.140 +         nsresult EndZstream();
   1.141 +     };
   1.142 +
   1.143 +
   1.144 +     /*************************************************************************
   1.145 +      * output stream wrapper class -
   1.146 +      *
   1.147 +      * The output stream wrapper references the descriptor, but the descriptor
   1.148 +      * doesn't need any references to the stream wrapper.
   1.149 +      *************************************************************************/
   1.150 +     class nsOutputStreamWrapper : public nsIOutputStream {
   1.151 +         friend class nsCacheEntryDescriptor;
   1.152 +
   1.153 +     protected:
   1.154 +         nsCacheEntryDescriptor *    mDescriptor;
   1.155 +         nsCOMPtr<nsIOutputStream>   mOutput;
   1.156 +         uint32_t                    mStartOffset;
   1.157 +         bool                        mInitialized;
   1.158 +         mozilla::Mutex              mLock;
   1.159 +     public:
   1.160 +         NS_DECL_THREADSAFE_ISUPPORTS
   1.161 +         NS_DECL_NSIOUTPUTSTREAM
   1.162 +
   1.163 +         nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
   1.164 +             : mDescriptor(desc)
   1.165 +             , mStartOffset(off)
   1.166 +             , mInitialized(false)
   1.167 +             , mLock("nsOutputStreamWrapper.mLock")
   1.168 +         {
   1.169 +             NS_ADDREF(mDescriptor); // owning ref
   1.170 +         }
   1.171 +         virtual ~nsOutputStreamWrapper()
   1.172 +         {
   1.173 +             Close();
   1.174 +
   1.175 +             NS_ASSERTION(!mOutput, "Bad state");
   1.176 +             NS_ASSERTION(!mDescriptor, "Bad state");
   1.177 +         }
   1.178 +
   1.179 +     private:
   1.180 +         nsresult LazyInit();
   1.181 +         nsresult EnsureInit();
   1.182 +         nsresult OnWrite(uint32_t count);
   1.183 +         nsresult Write_Locked(const char * buf,
   1.184 +                               uint32_t count,
   1.185 +                               uint32_t * result);
   1.186 +         nsresult Close_Locked();
   1.187 +         void CloseInternal();
   1.188 +     };
   1.189 +
   1.190 +
   1.191 +     class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
   1.192 +     private:
   1.193 +         unsigned char* mWriteBuffer;
   1.194 +         uint32_t mWriteBufferLen;
   1.195 +         z_stream mZstream;
   1.196 +         bool mStreamInitialized;
   1.197 +         bool mStreamEnded;
   1.198 +         uint32_t mUncompressedCount;
   1.199 +     public:
   1.200 +         NS_DECL_THREADSAFE_ISUPPORTS
   1.201 +
   1.202 +         nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc, 
   1.203 +                                       uint32_t off)
   1.204 +          : nsOutputStreamWrapper(desc, off)
   1.205 +          , mWriteBuffer(0)
   1.206 +          , mWriteBufferLen(0)
   1.207 +          , mStreamInitialized(false)
   1.208 +          , mStreamEnded(false)
   1.209 +          , mUncompressedCount(0)
   1.210 +         {
   1.211 +         }
   1.212 +         virtual ~nsCompressOutputStreamWrapper()
   1.213 +         { 
   1.214 +             Close();
   1.215 +         }
   1.216 +         NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t * result);
   1.217 +         NS_IMETHOD Close();
   1.218 +     private:
   1.219 +         nsresult InitZstream();
   1.220 +         nsresult WriteBuffer();
   1.221 +     };
   1.222 +
   1.223 + private:
   1.224 +     /**
   1.225 +      * nsCacheEntryDescriptor data members
   1.226 +      */
   1.227 +     nsCacheEntry          * mCacheEntry; // we are a child of the entry
   1.228 +     nsCacheAccessMode       mAccessGranted;
   1.229 +     nsVoidArray             mInputWrappers;
   1.230 +     nsOutputStreamWrapper * mOutputWrapper;
   1.231 +     mozilla::Mutex          mLock;
   1.232 +     bool                    mAsyncDoomPending;
   1.233 +     bool                    mDoomedOnClose;
   1.234 +     bool                    mClosingDescriptor;
   1.235 +};
   1.236 +
   1.237 +
   1.238 +#endif // _nsCacheEntryDescriptor_h_

mercurial