netwerk/cache/nsCacheEntry.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsCacheEntry.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,311 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
     1.5 + *
     1.6 + * This Source Code Form is subject to the terms of the Mozilla Public
     1.7 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.8 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.9 +
    1.10 +#ifndef _nsCacheEntry_h_
    1.11 +#define _nsCacheEntry_h_
    1.12 +
    1.13 +#include "nsICache.h"
    1.14 +#include "nsICacheEntryDescriptor.h"
    1.15 +#include "nsIThread.h"
    1.16 +#include "nsCacheMetaData.h"
    1.17 +
    1.18 +#include "nspr.h"
    1.19 +#include "pldhash.h"
    1.20 +#include "nsAutoPtr.h"
    1.21 +#include "nscore.h"
    1.22 +#include "nsCOMPtr.h"
    1.23 +#include "nsString.h"
    1.24 +#include "nsAString.h"
    1.25 +
    1.26 +class nsCacheDevice;
    1.27 +class nsCacheMetaData;
    1.28 +class nsCacheRequest;
    1.29 +class nsCacheEntryDescriptor;
    1.30 +
    1.31 +/******************************************************************************
    1.32 +* nsCacheEntry
    1.33 +*******************************************************************************/
    1.34 +class nsCacheEntry : public PRCList
    1.35 +{
    1.36 +public:
    1.37 +
    1.38 +    nsCacheEntry(const nsACString &   key,
    1.39 +                 bool                 streamBased,
    1.40 +                 nsCacheStoragePolicy storagePolicy);
    1.41 +    ~nsCacheEntry();
    1.42 +
    1.43 +
    1.44 +    static nsresult  Create( const char *          key,
    1.45 +                             bool                  streamBased,
    1.46 +                             nsCacheStoragePolicy  storagePolicy,
    1.47 +                             nsCacheDevice *       device,
    1.48 +                             nsCacheEntry **       result);
    1.49 +                                      
    1.50 +    nsCString *  Key()  { return &mKey; }
    1.51 +
    1.52 +    int32_t  FetchCount()                              { return mFetchCount; }
    1.53 +    void     SetFetchCount( int32_t   count)           { mFetchCount = count; }
    1.54 +    void     Fetched();
    1.55 +
    1.56 +    uint32_t LastFetched()                             { return mLastFetched; }
    1.57 +    void     SetLastFetched( uint32_t  lastFetched)    { mLastFetched = lastFetched; }
    1.58 +
    1.59 +    uint32_t LastModified()                            { return mLastModified; }
    1.60 +    void     SetLastModified( uint32_t lastModified)   { mLastModified = lastModified; }
    1.61 +
    1.62 +    uint32_t ExpirationTime()                     { return mExpirationTime; }
    1.63 +    void     SetExpirationTime( uint32_t expires) { mExpirationTime = expires; }
    1.64 +
    1.65 +    uint32_t Size()                               
    1.66 +        { return mDataSize + mMetaData.Size() + mKey.Length() ; }
    1.67 +
    1.68 +    nsCacheDevice * CacheDevice()                            { return mCacheDevice; }
    1.69 +    void            SetCacheDevice( nsCacheDevice * device)  { mCacheDevice = device; }
    1.70 +    void            SetCustomCacheDevice( nsCacheDevice * device )
    1.71 +                                                             { mCustomDevice = device; }
    1.72 +    nsCacheDevice * CustomCacheDevice()                      { return mCustomDevice; }
    1.73 +    const char *    GetDeviceID();
    1.74 +
    1.75 +    /**
    1.76 +     * Data accessors
    1.77 +     */
    1.78 +    nsISupports *Data()                           { return mData; }
    1.79 +    void         SetData( nsISupports * data);
    1.80 +
    1.81 +    int64_t  PredictedDataSize()                  { return mPredictedDataSize; }
    1.82 +    void     SetPredictedDataSize(int64_t size)   { mPredictedDataSize = size; }
    1.83 +
    1.84 +    uint32_t DataSize()                           { return mDataSize; }
    1.85 +    void     SetDataSize( uint32_t  size)         { mDataSize = size; }
    1.86 +
    1.87 +    void     TouchData();
    1.88 +    
    1.89 +    /**
    1.90 +     * Meta data accessors
    1.91 +     */
    1.92 +    const char * GetMetaDataElement( const char *  key) { return mMetaData.GetElement(key); }
    1.93 +    nsresult     SetMetaDataElement( const char *  key,
    1.94 +                                     const char *  value) { return mMetaData.SetElement(key, value); }
    1.95 +    nsresult VisitMetaDataElements( nsICacheMetaDataVisitor * visitor) { return mMetaData.VisitElements(visitor); }
    1.96 +    nsresult FlattenMetaData(char * buffer, uint32_t bufSize) { return mMetaData.FlattenMetaData(buffer, bufSize); }
    1.97 +    nsresult UnflattenMetaData(const char * buffer, uint32_t bufSize) { return mMetaData.UnflattenMetaData(buffer, bufSize); }
    1.98 +    uint32_t MetaDataSize() { return mMetaData.Size(); }  
    1.99 +
   1.100 +    void     TouchMetaData();
   1.101 +
   1.102 +
   1.103 +    /**
   1.104 +     * Security Info accessors
   1.105 +     */
   1.106 +    nsISupports* SecurityInfo() { return mSecurityInfo; }
   1.107 +    void     SetSecurityInfo( nsISupports *  info) { mSecurityInfo = info; }
   1.108 +
   1.109 +
   1.110 +    // XXX enumerate MetaData method
   1.111 +
   1.112 +
   1.113 +    enum CacheEntryFlags {
   1.114 +        eStoragePolicyMask   = 0x000000FF,
   1.115 +        eDoomedMask          = 0x00000100,
   1.116 +        eEntryDirtyMask      = 0x00000200,
   1.117 +        eDataDirtyMask       = 0x00000400,
   1.118 +        eMetaDataDirtyMask   = 0x00000800,
   1.119 +        eStreamDataMask      = 0x00001000,
   1.120 +        eActiveMask          = 0x00002000,
   1.121 +        eInitializedMask     = 0x00004000,
   1.122 +        eValidMask           = 0x00008000,
   1.123 +        eBindingMask         = 0x00010000,
   1.124 +        ePrivateMask         = 0x00020000
   1.125 +    };
   1.126 +    
   1.127 +    void MarkBinding()         { mFlags |=  eBindingMask; }
   1.128 +    void ClearBinding()        { mFlags &= ~eBindingMask; }
   1.129 +    bool IsBinding()         { return (mFlags & eBindingMask) != 0; }
   1.130 +
   1.131 +    void MarkEntryDirty()      { mFlags |=  eEntryDirtyMask; }
   1.132 +    void MarkEntryClean()      { mFlags &= ~eEntryDirtyMask; }
   1.133 +    void MarkDataDirty()       { mFlags |=  eDataDirtyMask; }
   1.134 +    void MarkDataClean()       { mFlags &= ~eDataDirtyMask; }
   1.135 +    void MarkMetaDataDirty()   { mFlags |=  eMetaDataDirtyMask; }
   1.136 +    void MarkMetaDataClean()   { mFlags &= ~eMetaDataDirtyMask; }
   1.137 +    void MarkStreamData()      { mFlags |=  eStreamDataMask; }
   1.138 +    void MarkValid()           { mFlags |=  eValidMask; }
   1.139 +    void MarkInvalid()         { mFlags &= ~eValidMask; }
   1.140 +    void MarkPrivate()         { mFlags |=  ePrivateMask; }
   1.141 +    void MarkPublic()          { mFlags &= ~ePrivateMask; }
   1.142 +    //    void MarkAllowedInMemory() { mFlags |=  eAllowedInMemoryMask; }
   1.143 +    //    void MarkAllowedOnDisk()   { mFlags |=  eAllowedOnDiskMask; }
   1.144 +
   1.145 +    bool IsDoomed()          { return (mFlags & eDoomedMask) != 0; }
   1.146 +    bool IsEntryDirty()      { return (mFlags & eEntryDirtyMask) != 0; }
   1.147 +    bool IsDataDirty()       { return (mFlags & eDataDirtyMask) != 0; }
   1.148 +    bool IsMetaDataDirty()   { return (mFlags & eMetaDataDirtyMask) != 0; }
   1.149 +    bool IsStreamData()      { return (mFlags & eStreamDataMask) != 0; }
   1.150 +    bool IsActive()          { return (mFlags & eActiveMask) != 0; }
   1.151 +    bool IsInitialized()     { return (mFlags & eInitializedMask) != 0; }
   1.152 +    bool IsValid()           { return (mFlags & eValidMask) != 0; }
   1.153 +    bool IsInvalid()         { return (mFlags & eValidMask) == 0; }
   1.154 +    bool IsInUse()           { return IsBinding() ||
   1.155 +                                        !(PR_CLIST_IS_EMPTY(&mRequestQ) &&
   1.156 +                                          PR_CLIST_IS_EMPTY(&mDescriptorQ)); }
   1.157 +    bool IsNotInUse()        { return !IsInUse(); }
   1.158 +    bool IsPrivate()         { return (mFlags & ePrivateMask) != 0; }
   1.159 +
   1.160 +
   1.161 +    bool IsAllowedInMemory()
   1.162 +    {
   1.163 +        return (StoragePolicy() ==  nsICache::STORE_ANYWHERE) ||
   1.164 +            (StoragePolicy() == nsICache::STORE_IN_MEMORY);
   1.165 +    }
   1.166 +
   1.167 +    bool IsAllowedOnDisk()
   1.168 +    {
   1.169 +        return !IsPrivate() && ((StoragePolicy() == nsICache::STORE_ANYWHERE) ||
   1.170 +            (StoragePolicy() == nsICache::STORE_ON_DISK));
   1.171 +    }
   1.172 +
   1.173 +    bool IsAllowedOffline()
   1.174 +    {
   1.175 +        return (StoragePolicy() == nsICache::STORE_OFFLINE);
   1.176 +    }
   1.177 +
   1.178 +    nsCacheStoragePolicy  StoragePolicy()
   1.179 +    {
   1.180 +        return (nsCacheStoragePolicy)(mFlags & eStoragePolicyMask);
   1.181 +    }
   1.182 +
   1.183 +    void SetStoragePolicy(nsCacheStoragePolicy policy)
   1.184 +    {
   1.185 +        NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
   1.186 +        mFlags &= ~eStoragePolicyMask; // clear storage policy bits
   1.187 +        mFlags |= policy;
   1.188 +    }
   1.189 +
   1.190 +
   1.191 +    // methods for nsCacheService
   1.192 +    nsresult RequestAccess( nsCacheRequest * request, nsCacheAccessMode *accessGranted);
   1.193 +    nsresult CreateDescriptor( nsCacheRequest *           request,
   1.194 +                               nsCacheAccessMode          accessGranted,
   1.195 +                               nsICacheEntryDescriptor ** result);
   1.196 +
   1.197 +    bool     RemoveRequest( nsCacheRequest * request);
   1.198 +    bool     RemoveDescriptor( nsCacheEntryDescriptor * descriptor,
   1.199 +                               bool                   * doomEntry);
   1.200 +
   1.201 +    void     GetDescriptors(nsTArray<nsRefPtr<nsCacheEntryDescriptor> > &outDescriptors);
   1.202 +
   1.203 +private:
   1.204 +    friend class nsCacheEntryHashTable;
   1.205 +    friend class nsCacheService;
   1.206 +
   1.207 +    void     DetachDescriptors();
   1.208 +
   1.209 +    // internal methods
   1.210 +    void MarkDoomed()          { mFlags |=  eDoomedMask; }
   1.211 +    void MarkStreamBased()     { mFlags |=  eStreamDataMask; }
   1.212 +    void MarkInitialized()     { mFlags |=  eInitializedMask; }
   1.213 +    void MarkActive()          { mFlags |=  eActiveMask; }
   1.214 +    void MarkInactive()        { mFlags &= ~eActiveMask; }
   1.215 +
   1.216 +    nsCString               mKey;
   1.217 +    uint32_t                mFetchCount;     // 4
   1.218 +    uint32_t                mLastFetched;    // 4
   1.219 +    uint32_t                mLastModified;   // 4
   1.220 +    uint32_t                mLastValidated;  // 4
   1.221 +    uint32_t                mExpirationTime; // 4
   1.222 +    uint32_t                mFlags;          // 4
   1.223 +    int64_t                 mPredictedDataSize;  // Size given by ContentLength.
   1.224 +    uint32_t                mDataSize;       // 4
   1.225 +    nsCacheDevice *         mCacheDevice;    // 4
   1.226 +    nsCacheDevice *         mCustomDevice;   // 4
   1.227 +    nsCOMPtr<nsISupports>   mSecurityInfo;   // 
   1.228 +    nsISupports *           mData;           // strong ref
   1.229 +    nsCOMPtr<nsIThread>     mThread;
   1.230 +    nsCacheMetaData         mMetaData;       // 4
   1.231 +    PRCList                 mRequestQ;       // 8
   1.232 +    PRCList                 mDescriptorQ;    // 8
   1.233 +};
   1.234 +
   1.235 +
   1.236 +/******************************************************************************
   1.237 +* nsCacheEntryInfo
   1.238 +*******************************************************************************/
   1.239 +class nsCacheEntryInfo : public nsICacheEntryInfo {
   1.240 +public:
   1.241 +    NS_DECL_ISUPPORTS
   1.242 +    NS_DECL_NSICACHEENTRYINFO
   1.243 +
   1.244 +    nsCacheEntryInfo(nsCacheEntry* entry)
   1.245 +        :   mCacheEntry(entry)
   1.246 +    {
   1.247 +    }
   1.248 +
   1.249 +    virtual ~nsCacheEntryInfo() {}
   1.250 +    void    DetachEntry() { mCacheEntry = nullptr; }
   1.251 +    
   1.252 +private:
   1.253 +    nsCacheEntry * mCacheEntry;
   1.254 +};
   1.255 +
   1.256 +
   1.257 +/******************************************************************************
   1.258 +* nsCacheEntryHashTable
   1.259 +*******************************************************************************/
   1.260 +typedef struct {
   1.261 +    PLDHashNumber  keyHash;
   1.262 +    nsCacheEntry  *cacheEntry;
   1.263 +} nsCacheEntryHashTableEntry;
   1.264 +
   1.265 +
   1.266 +class nsCacheEntryHashTable
   1.267 +{
   1.268 +public:
   1.269 +    nsCacheEntryHashTable();
   1.270 +    ~nsCacheEntryHashTable();
   1.271 +
   1.272 +    nsresult      Init();
   1.273 +    void          Shutdown();
   1.274 +
   1.275 +    nsCacheEntry *GetEntry( const nsCString * key);
   1.276 +    nsresult      AddEntry( nsCacheEntry *entry);
   1.277 +    void          RemoveEntry( nsCacheEntry *entry);
   1.278 +    
   1.279 +    void          VisitEntries( PLDHashEnumerator etor, void *arg);
   1.280 +
   1.281 +private:
   1.282 +    // PLDHashTable operation callbacks
   1.283 +    static PLDHashNumber  HashKey( PLDHashTable *table, const void *key);
   1.284 +
   1.285 +    static bool           MatchEntry( PLDHashTable *           table,
   1.286 +                                      const PLDHashEntryHdr *  entry,
   1.287 +                                      const void *             key);
   1.288 +
   1.289 +    static void           MoveEntry( PLDHashTable *table,
   1.290 +                                     const PLDHashEntryHdr *from,
   1.291 +                                     PLDHashEntryHdr       *to);
   1.292 +
   1.293 +    static void           ClearEntry( PLDHashTable *table, PLDHashEntryHdr *entry);
   1.294 +
   1.295 +    static void           Finalize( PLDHashTable *table);
   1.296 +
   1.297 +    static
   1.298 +    PLDHashOperator       FreeCacheEntries(PLDHashTable *    table,
   1.299 +                                           PLDHashEntryHdr * hdr,
   1.300 +                                           uint32_t          number,
   1.301 +                                           void *            arg);
   1.302 +    static
   1.303 +    PLDHashOperator       VisitEntry(PLDHashTable *         table,
   1.304 +                                     PLDHashEntryHdr *      hdr,
   1.305 +                                     uint32_t               number,
   1.306 +                                     void *                 arg);
   1.307 +                                     
   1.308 +    // member variables
   1.309 +    static const PLDHashTableOps ops;
   1.310 +    PLDHashTable                 table;
   1.311 +    bool                         initialized;
   1.312 +};
   1.313 +
   1.314 +#endif // _nsCacheEntry_h_

mercurial