/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- * * The contents of this file are subject to the Mozilla Public * License Version 1.1 (the "License"); you may not use this file * except in compliance with the License. You may obtain a copy of * the License at http://www.mozilla.org/MPL/ * * Software distributed under the License is distributed on an "AS * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or * implied. See the License for the specific language governing * rights and limitations under the License. * * The Original Code is nsCacheEntryDescriptor.h, released February 22, 2001. * * The Initial Developer of the Original Code is Netscape Communications * Corporation. Portions created by Netscape are * Copyright (C) 2001 Netscape Communications Corporation. All * Rights Reserved. * * Contributor(s): * Gordon Sheridan, 22-February-2001 */ #ifndef _nsCacheEntryDescriptor_h_ #define _nsCacheEntryDescriptor_h_ #include "nsICacheEntryDescriptor.h" #include "nsCacheEntry.h" #include "nsIInputStream.h" #include "nsIOutputStream.h" /****************************************************************************** * nsCacheEntryDescriptor *******************************************************************************/ class nsCacheEntryDescriptor : public PRCList, public nsICacheEntryDescriptor { public: NS_DECL_ISUPPORTS NS_DECL_NSICACHEENTRYDESCRIPTOR NS_DECL_NSICACHEENTRYINFO nsCacheEntryDescriptor(nsCacheEntry * entry, nsCacheAccessMode mode); virtual ~nsCacheEntryDescriptor(); static nsresult Create(nsCacheEntry * entry, nsCacheAccessMode accessGranted, nsICacheEntryDescriptor ** result); /** * utility method to attempt changing data size of associated entry */ nsresult RequestDataSizeChange(PRInt32 deltaSize); /** * methods callbacks for nsCacheService */ nsCacheEntry * CacheEntry(void) { return mCacheEntry; } void ClearCacheEntry(void) { mCacheEntry = nsnull; } private: /************************************************************************* * 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 { private: nsCacheEntryDescriptor * mDescriptor; nsCOMPtr mInput; PRUint32 mStartOffset; PRBool mInitialized; public: NS_DECL_ISUPPORTS NS_DECL_NSIINPUTSTREAM nsInputStreamWrapper(nsCacheEntryDescriptor * desc, PRUint32 off) : mDescriptor(desc) , mStartOffset(off) , mInitialized(PR_FALSE) { NS_ADDREF(mDescriptor); } virtual ~nsInputStreamWrapper() { NS_RELEASE(mDescriptor); } private: nsresult LazyInit(); nsresult EnsureInit() { return mInitialized ? NS_OK : LazyInit(); } }; friend class nsInputStreamWrapper; /************************************************************************* * 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 { private: nsCacheEntryDescriptor * mDescriptor; nsCOMPtr mOutput; PRUint32 mStartOffset; PRBool mInitialized; public: NS_DECL_ISUPPORTS NS_DECL_NSIOUTPUTSTREAM nsOutputStreamWrapper(nsCacheEntryDescriptor * desc, PRUint32 off) : mDescriptor(desc) , mStartOffset(off) , mInitialized(PR_FALSE) { NS_ADDREF(mDescriptor); // owning ref } virtual ~nsOutputStreamWrapper() { // XXX _HACK_ the storage stream needs this! Close(); NS_RELEASE(mDescriptor); } private: nsresult LazyInit(); nsresult EnsureInit() { return mInitialized ? NS_OK : LazyInit(); } nsresult OnWrite(PRUint32 count); }; friend class nsOutputStreamWrapper; private: /** * nsCacheEntryDescriptor data members */ nsCacheEntry * mCacheEntry; // we are a child of the entry nsCacheAccessMode mAccessGranted; }; #endif // _nsCacheEntryDescriptor_h_