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