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