2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
|
|
|
|
#ifndef _nsCacheEntryDescriptor_h_
|
|
|
|
#define _nsCacheEntryDescriptor_h_
|
|
|
|
|
|
|
|
#include "nsICacheEntryDescriptor.h"
|
|
|
|
#include "nsCacheEntry.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIOutputStream.h"
|
2010-12-28 17:25:14 -08:00
|
|
|
#include "nsCacheService.h"
|
2011-12-17 02:30:29 -08:00
|
|
|
#include "zlib.h"
|
2012-09-17 14:31:46 -07:00
|
|
|
#include "mozilla/Mutex.h"
|
2012-12-14 09:03:01 -08:00
|
|
|
#include "nsVoidArray.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/******************************************************************************
|
|
|
|
* nsCacheEntryDescriptor
|
|
|
|
*******************************************************************************/
|
|
|
|
class nsCacheEntryDescriptor :
|
|
|
|
public PRCList,
|
|
|
|
public nsICacheEntryDescriptor
|
|
|
|
{
|
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSICACHEENTRYDESCRIPTOR
|
|
|
|
NS_DECL_NSICACHEENTRYINFO
|
2012-09-17 14:31:46 -07:00
|
|
|
|
|
|
|
friend class nsAsyncDoomEvent;
|
2012-12-14 09:03:01 -08:00
|
|
|
friend class nsCacheService;
|
2012-09-17 14:31:46 -07:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* utility method to attempt changing data size of associated entry
|
|
|
|
*/
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult RequestDataSizeChange(int32_t deltaSize);
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
/**
|
|
|
|
* methods callbacks for nsCacheService
|
|
|
|
*/
|
|
|
|
nsCacheEntry * CacheEntry(void) { return mCacheEntry; }
|
2012-12-14 09:03:01 -08:00
|
|
|
bool ClearCacheEntry(void)
|
2012-09-17 14:31:46 -07:00
|
|
|
{
|
2012-12-14 09:03:01 -08:00
|
|
|
NS_ASSERTION(mInputWrappers.Count() == 0, "Bad state");
|
|
|
|
NS_ASSERTION(!mOutputWrapper, "Bad state");
|
|
|
|
|
|
|
|
bool doomEntry = false;
|
2012-09-17 14:31:46 -07:00
|
|
|
bool asyncDoomPending;
|
|
|
|
{
|
|
|
|
mozilla::MutexAutoLock lock(mLock);
|
|
|
|
asyncDoomPending = mAsyncDoomPending;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (asyncDoomPending && mCacheEntry) {
|
2012-12-14 09:03:01 -08:00
|
|
|
doomEntry = true;
|
2012-09-17 14:31:46 -07:00
|
|
|
mDoomedOnClose = true;
|
|
|
|
}
|
|
|
|
mCacheEntry = nullptr;
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-12-14 09:03:01 -08:00
|
|
|
return doomEntry;
|
2011-06-22 01:49:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-06-24 09:36:44 -07:00
|
|
|
virtual ~nsCacheEntryDescriptor();
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*************************************************************************
|
|
|
|
* input stream wrapper class -
|
|
|
|
*
|
|
|
|
* The input stream wrapper references the descriptor, but the descriptor
|
|
|
|
* doesn't need any references to the stream wrapper.
|
|
|
|
*************************************************************************/
|
|
|
|
class nsInputStreamWrapper : public nsIInputStream {
|
2012-12-14 09:03:01 -08:00
|
|
|
friend class nsCacheEntryDescriptor;
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
nsCacheEntryDescriptor * mDescriptor;
|
|
|
|
nsCOMPtr<nsIInputStream> mInput;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mStartOffset;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2012-12-14 09:03:01 -08:00
|
|
|
mozilla::Mutex mLock;
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIINPUTSTREAM
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsInputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mDescriptor(desc)
|
|
|
|
, mStartOffset(off)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mInitialized(false)
|
2012-12-14 09:03:01 -08:00
|
|
|
, mLock("nsInputStreamWrapper.mLock")
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ADDREF(mDescriptor);
|
|
|
|
}
|
2014-06-24 09:36:44 -07:00
|
|
|
|
|
|
|
private:
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~nsInputStreamWrapper()
|
|
|
|
{
|
2013-01-03 11:24:27 -08:00
|
|
|
NS_IF_RELEASE(mDescriptor);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult LazyInit();
|
2013-01-03 11:24:27 -08:00
|
|
|
nsresult EnsureInit();
|
2012-12-14 09:03:01 -08:00
|
|
|
nsresult Read_Locked(char *buf, uint32_t count, uint32_t *countRead);
|
|
|
|
nsresult Close_Locked();
|
|
|
|
void CloseInternal();
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
2011-12-17 02:30:29 -08:00
|
|
|
class nsDecompressInputStreamWrapper : public nsInputStreamWrapper {
|
|
|
|
private:
|
|
|
|
unsigned char* mReadBuffer;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mReadBufferLen;
|
2011-12-17 02:30:29 -08:00
|
|
|
z_stream mZstream;
|
2012-04-04 02:15:10 -07:00
|
|
|
bool mStreamInitialized;
|
|
|
|
bool mStreamEnded;
|
2011-12-17 02:30:29 -08:00
|
|
|
public:
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-12-17 02:30:29 -08:00
|
|
|
|
|
|
|
nsDecompressInputStreamWrapper(nsCacheEntryDescriptor * desc,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t off)
|
2011-12-17 02:30:29 -08:00
|
|
|
: nsInputStreamWrapper(desc, off)
|
|
|
|
, mReadBuffer(0)
|
|
|
|
, mReadBufferLen(0)
|
2012-04-04 02:15:10 -07:00
|
|
|
, mStreamInitialized(false)
|
|
|
|
, mStreamEnded(false)
|
2011-12-17 02:30:29 -08:00
|
|
|
{
|
|
|
|
}
|
2014-06-24 09:36:44 -07:00
|
|
|
NS_IMETHOD Read(char* buf, uint32_t count, uint32_t * result);
|
|
|
|
NS_IMETHOD Close();
|
|
|
|
private:
|
2011-12-17 02:30:29 -08:00
|
|
|
virtual ~nsDecompressInputStreamWrapper()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
nsresult InitZstream();
|
|
|
|
nsresult EndZstream();
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
/*************************************************************************
|
|
|
|
* output stream wrapper class -
|
|
|
|
*
|
|
|
|
* The output stream wrapper references the descriptor, but the descriptor
|
|
|
|
* doesn't need any references to the stream wrapper.
|
|
|
|
*************************************************************************/
|
|
|
|
class nsOutputStreamWrapper : public nsIOutputStream {
|
2012-12-14 09:03:01 -08:00
|
|
|
friend class nsCacheEntryDescriptor;
|
|
|
|
|
2011-12-17 02:30:29 -08:00
|
|
|
protected:
|
2007-03-22 10:30:00 -07:00
|
|
|
nsCacheEntryDescriptor * mDescriptor;
|
|
|
|
nsCOMPtr<nsIOutputStream> mOutput;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mStartOffset;
|
2011-09-28 23:19:26 -07:00
|
|
|
bool mInitialized;
|
2012-12-14 09:03:01 -08:00
|
|
|
mozilla::Mutex mLock;
|
2007-03-22 10:30:00 -07:00
|
|
|
public:
|
2013-07-18 19:24:13 -07:00
|
|
|
NS_DECL_THREADSAFE_ISUPPORTS
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_NSIOUTPUTSTREAM
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, uint32_t off)
|
2007-03-22 10:30:00 -07:00
|
|
|
: mDescriptor(desc)
|
|
|
|
, mStartOffset(off)
|
2011-10-17 07:59:28 -07:00
|
|
|
, mInitialized(false)
|
2012-12-14 09:03:01 -08:00
|
|
|
, mLock("nsOutputStreamWrapper.mLock")
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
NS_ADDREF(mDescriptor); // owning ref
|
|
|
|
}
|
2014-06-24 09:36:44 -07:00
|
|
|
|
|
|
|
private:
|
2007-03-22 10:30:00 -07:00
|
|
|
virtual ~nsOutputStreamWrapper()
|
2013-01-03 11:24:27 -08:00
|
|
|
{
|
2007-03-22 10:30:00 -07:00
|
|
|
Close();
|
2013-01-03 11:24:27 -08:00
|
|
|
|
|
|
|
NS_ASSERTION(!mOutput, "Bad state");
|
|
|
|
NS_ASSERTION(!mDescriptor, "Bad state");
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
nsresult LazyInit();
|
2013-01-03 11:24:27 -08:00
|
|
|
nsresult EnsureInit();
|
2012-08-22 08:56:38 -07:00
|
|
|
nsresult OnWrite(uint32_t count);
|
2012-12-14 09:03:01 -08:00
|
|
|
nsresult Write_Locked(const char * buf,
|
|
|
|
uint32_t count,
|
|
|
|
uint32_t * result);
|
|
|
|
nsresult Close_Locked();
|
|
|
|
void CloseInternal();
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
2012-12-14 09:03:01 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2011-12-17 02:30:29 -08:00
|
|
|
class nsCompressOutputStreamWrapper : public nsOutputStreamWrapper {
|
|
|
|
private:
|
|
|
|
unsigned char* mWriteBuffer;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mWriteBufferLen;
|
2011-12-17 02:30:29 -08:00
|
|
|
z_stream mZstream;
|
2012-04-04 02:15:10 -07:00
|
|
|
bool mStreamInitialized;
|
2012-12-14 09:03:01 -08:00
|
|
|
bool mStreamEnded;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t mUncompressedCount;
|
2011-12-17 02:30:29 -08:00
|
|
|
public:
|
2014-07-14 12:21:34 -07:00
|
|
|
NS_DECL_ISUPPORTS_INHERITED
|
2011-12-17 02:30:29 -08:00
|
|
|
|
|
|
|
nsCompressOutputStreamWrapper(nsCacheEntryDescriptor * desc,
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t off)
|
2011-12-17 02:30:29 -08:00
|
|
|
: nsOutputStreamWrapper(desc, off)
|
|
|
|
, mWriteBuffer(0)
|
|
|
|
, mWriteBufferLen(0)
|
2012-04-04 02:15:10 -07:00
|
|
|
, mStreamInitialized(false)
|
2012-12-14 09:03:01 -08:00
|
|
|
, mStreamEnded(false)
|
2011-12-17 02:30:29 -08:00
|
|
|
, mUncompressedCount(0)
|
|
|
|
{
|
|
|
|
}
|
2014-06-24 09:36:44 -07:00
|
|
|
NS_IMETHOD Write(const char* buf, uint32_t count, uint32_t * result);
|
|
|
|
NS_IMETHOD Close();
|
|
|
|
private:
|
2011-12-17 02:30:29 -08:00
|
|
|
virtual ~nsCompressOutputStreamWrapper()
|
|
|
|
{
|
|
|
|
Close();
|
|
|
|
}
|
|
|
|
nsresult InitZstream();
|
|
|
|
nsresult WriteBuffer();
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
private:
|
|
|
|
/**
|
|
|
|
* nsCacheEntryDescriptor data members
|
|
|
|
*/
|
|
|
|
nsCacheEntry * mCacheEntry; // we are a child of the entry
|
|
|
|
nsCacheAccessMode mAccessGranted;
|
2012-12-14 09:03:01 -08:00
|
|
|
nsVoidArray mInputWrappers;
|
|
|
|
nsOutputStreamWrapper * mOutputWrapper;
|
2012-09-17 14:31:46 -07:00
|
|
|
mozilla::Mutex mLock;
|
|
|
|
bool mAsyncDoomPending;
|
|
|
|
bool mDoomedOnClose;
|
2012-12-14 09:03:01 -08:00
|
|
|
bool mClosingDescriptor;
|
2007-03-22 10:30:00 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
#endif // _nsCacheEntryDescriptor_h_
|