Coverage Report

Created: 2018-09-25 14:53

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