Back to index

lightning-sunbird  0.9+nobinonly
nsDiskCacheDeviceSQL.h
Go to the documentation of this file.
00001 /* vim:set ts=2 sw=2 sts=2 et cin: */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is IBM Corporation.
00018  * Portions created by IBM Corporation are Copyright (C) 2004
00019  * IBM Corporation. All Rights Reserved.
00020  *
00021  * Contributor(s):
00022  *   Darin Fisher <darin@meer.net>
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either the GNU General Public License Version 2 or later (the "GPL"), or
00026  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #ifndef nsDiskCacheDeviceSQL_h__
00039 #define nsDiskCacheDeviceSQL_h__
00040 
00041 #include "nsCacheDevice.h"
00042 #include "nsILocalFile.h"
00043 #include "nsIObserver.h"
00044 #include "mozIStorageConnection.h"
00045 #include "nsCOMPtr.h"
00046 
00047 class nsDiskCacheDevice : public nsCacheDevice
00048 {
00049 public:
00050   nsDiskCacheDevice();
00051 
00056   virtual ~nsDiskCacheDevice();
00057 
00058   virtual nsresult        Init();
00059   virtual nsresult        Shutdown();
00060 
00061   virtual const char *    GetDeviceID(void);
00062   virtual nsCacheEntry *  FindEntry(nsCString * key, PRBool *collision);
00063   virtual nsresult        DeactivateEntry(nsCacheEntry * entry);
00064   virtual nsresult        BindEntry(nsCacheEntry * entry);
00065   virtual void            DoomEntry( nsCacheEntry * entry );
00066 
00067   virtual nsresult OpenInputStreamForEntry(nsCacheEntry *    entry,
00068                                            nsCacheAccessMode mode,
00069                                            PRUint32          offset,
00070                                            nsIInputStream ** result);
00071 
00072   virtual nsresult OpenOutputStreamForEntry(nsCacheEntry *     entry,
00073                                             nsCacheAccessMode  mode,
00074                                             PRUint32           offset,
00075                                             nsIOutputStream ** result);
00076 
00077   virtual nsresult        GetFileForEntry(nsCacheEntry *    entry,
00078                                           nsIFile **        result);
00079 
00080   virtual nsresult        OnDataSizeChange(nsCacheEntry * entry, PRInt32 deltaSize);
00081   
00082   virtual nsresult        Visit(nsICacheVisitor * visitor);
00083 
00084   virtual nsresult        EvictEntries(const char * clientID);
00085 
00086 
00091   void                    SetCacheParentDirectory(nsILocalFile * parentDir);
00092   void                    SetCapacity(PRUint32  capacity);
00093 
00094   nsILocalFile *          CacheDirectory() { return mCacheDirectory; }
00095   PRUint32                CacheCapacity() { return mCacheCapacity; }
00096   PRUint32                CacheSize();
00097   PRUint32                EntryCount();
00098   
00099 
00100 private:    
00101   PRBool   Initialized() { return mDB != nsnull; }
00102   nsresult EvictDiskCacheEntries(PRUint32 targetCapacity);
00103   nsresult UpdateEntry(nsCacheEntry *entry);
00104   nsresult UpdateEntrySize(nsCacheEntry *entry, PRUint32 newSize);
00105   nsresult DeleteEntry(nsCacheEntry *entry, PRBool deleteData);
00106   nsresult DeleteData(nsCacheEntry *entry);
00107   nsresult EnableEvictionObserver();
00108   nsresult DisableEvictionObserver();
00109 
00110 #if 0
00111   // sqlite function for observing DELETE events
00112   static void EvictionObserver(struct sqlite3_context *, int, struct Mem **);
00113 #endif
00114 
00115   nsCOMPtr<mozIStorageConnection> mDB;
00116   nsCOMPtr<mozIStorageStatement>  mStatement_CacheSize;
00117   nsCOMPtr<mozIStorageStatement>  mStatement_EntryCount;
00118   nsCOMPtr<mozIStorageStatement>  mStatement_UpdateEntry;
00119   nsCOMPtr<mozIStorageStatement>  mStatement_UpdateEntrySize;
00120   nsCOMPtr<mozIStorageStatement>  mStatement_DeleteEntry;
00121   nsCOMPtr<mozIStorageStatement>  mStatement_FindEntry;
00122   nsCOMPtr<mozIStorageStatement>  mStatement_BindEntry;
00123 
00124   nsCOMPtr<nsILocalFile>          mCacheDirectory;
00125   PRUint32                        mCacheCapacity;     // XXX need soft/hard limits, currentTotal
00126   PRInt32                         mDeltaCounter;
00127 };
00128 
00129 #endif // nsDiskCacheDeviceSQL_h__