Back to index

lightning-sunbird  0.9+nobinonly
nsDiskCacheEntry.h
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is nsMemoryCacheDevice.cpp, released
00017  * February 22, 2001.
00018  *
00019  * The Initial Developer of the Original Code is
00020  * Netscape Communications Corporation.
00021  * Portions created by the Initial Developer are Copyright (C) 2001
00022  * the Initial Developer. All Rights Reserved.
00023  *
00024  * Contributor(s):
00025  *   Gordon Sheridan  <gordon@netscape.com>
00026  *   Patrick C. Beard <beard@netscape.com>
00027  *
00028  * Alternatively, the contents of this file may be used under the terms of
00029  * either the GNU General Public License Version 2 or later (the "GPL"), or
00030  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00031  * in which case the provisions of the GPL or the LGPL are applicable instead
00032  * of those above. If you wish to allow use of your version of this file only
00033  * under the terms of either the GPL or the LGPL, and not to allow others to
00034  * use your version of this file under the terms of the MPL, indicate your
00035  * decision by deleting the provisions above and replace them with the notice
00036  * and other provisions required by the GPL or the LGPL. If you do not delete
00037  * the provisions above, a recipient may use your version of this file under
00038  * the terms of any one of the MPL, the GPL or the LGPL.
00039  *
00040  * ***** END LICENSE BLOCK ***** */
00041 
00042 #ifndef _nsDiskCacheEntry_h_
00043 #define _nsDiskCacheEntry_h_
00044 
00045 #include "nsDiskCacheMap.h"
00046 
00047 #include "nsCacheEntry.h"
00048 
00049 #include "nsICacheVisitor.h"
00050 
00051 #include "nspr.h"
00052 #include "nscore.h"
00053 #include "nsError.h"
00054 
00055 
00056 
00057 /******************************************************************************
00058  *  nsDiskCacheEntry
00059  *****************************************************************************/
00060 struct nsDiskCacheEntry {
00061     PRUint32        mHeaderVersion; // useful for stand-alone metadata files
00062     PRUint32        mMetaLocation;  // for verification
00063     PRInt32         mFetchCount;
00064     PRUint32        mLastFetched;
00065     PRUint32        mLastModified;
00066     PRUint32        mExpirationTime;
00067     PRUint32        mDataSize;
00068     PRUint32        mKeySize;       // includes terminating null byte
00069     PRUint32        mMetaDataSize;  // includes terminating null byte
00070     char            mKeyStart[1];   // start of key data
00071     //              mMetaDataStart = mKeyStart[mKeySize];
00072 
00073     PRUint32        Size()    { return offsetof(nsDiskCacheEntry,mKeyStart) + 
00074                                     mKeySize + mMetaDataSize;
00075                               }
00076 
00077     nsCacheEntry *  CreateCacheEntry(nsCacheDevice *  device);
00078                                      
00079     PRBool          CheckConsistency(PRUint32  size);
00080 
00081     void Swap()         // host to network (memory to disk)
00082     {
00083 #if defined(IS_LITTLE_ENDIAN)   
00084         mHeaderVersion      = ::PR_htonl(mHeaderVersion);
00085         mMetaLocation       = ::PR_htonl(mMetaLocation);
00086         mFetchCount         = ::PR_htonl(mFetchCount);
00087         mLastFetched        = ::PR_htonl(mLastFetched);
00088         mLastModified       = ::PR_htonl(mLastModified);
00089         mExpirationTime     = ::PR_htonl(mExpirationTime);
00090         mDataSize           = ::PR_htonl(mDataSize);
00091         mKeySize            = ::PR_htonl(mKeySize);
00092         mMetaDataSize       = ::PR_htonl(mMetaDataSize);
00093 #endif
00094     }
00095     
00096     void Unswap()       // network to host (disk to memory)
00097     {
00098 #if defined(IS_LITTLE_ENDIAN)
00099         mHeaderVersion      = ::PR_ntohl(mHeaderVersion);
00100         mMetaLocation       = ::PR_ntohl(mMetaLocation);
00101         mFetchCount         = ::PR_ntohl(mFetchCount);
00102         mLastFetched        = ::PR_ntohl(mLastFetched);
00103         mLastModified       = ::PR_ntohl(mLastModified);
00104         mExpirationTime     = ::PR_ntohl(mExpirationTime);
00105         mDataSize           = ::PR_ntohl(mDataSize);
00106         mKeySize            = ::PR_ntohl(mKeySize);
00107         mMetaDataSize       = ::PR_ntohl(mMetaDataSize);
00108 #endif
00109     }
00110 };
00111 
00112 nsDiskCacheEntry *  CreateDiskCacheEntry(nsDiskCacheBinding *  binding);
00113 
00114 
00115 
00116 /******************************************************************************
00117  *  nsDiskCacheEntryInfo
00118  *****************************************************************************/
00119 class nsDiskCacheEntryInfo : public nsICacheEntryInfo {
00120 public:
00121     NS_DECL_ISUPPORTS
00122     NS_DECL_NSICACHEENTRYINFO
00123 
00124     nsDiskCacheEntryInfo(const char * deviceID, nsDiskCacheEntry * diskEntry)
00125         : mDeviceID(deviceID)
00126         , mDiskEntry(diskEntry)
00127     {
00128     }
00129 
00130     virtual ~nsDiskCacheEntryInfo() {}
00131     
00132     const char* Key() { return mDiskEntry->mKeyStart; }
00133     
00134 private:
00135     const char *        mDeviceID;
00136     nsDiskCacheEntry *  mDiskEntry;
00137 };
00138 
00139 
00140 #endif /* _nsDiskCacheEntry_h_ */