netwerk/cache/nsCacheRequest.h

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/netwerk/cache/nsCacheRequest.h	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,158 @@
     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 _nsCacheRequest_h_
    1.11 +#define _nsCacheRequest_h_
    1.12 +
    1.13 +#include "nspr.h"
    1.14 +#include "mozilla/CondVar.h"
    1.15 +#include "mozilla/Mutex.h"
    1.16 +#include "nsCOMPtr.h"
    1.17 +#include "nsICache.h"
    1.18 +#include "nsICacheListener.h"
    1.19 +#include "nsCacheSession.h"
    1.20 +#include "nsCacheService.h"
    1.21 +
    1.22 +
    1.23 +class nsCacheRequest : public PRCList
    1.24 +{
    1.25 +    typedef mozilla::CondVar CondVar;
    1.26 +    typedef mozilla::MutexAutoLock MutexAutoLock;
    1.27 +    typedef mozilla::Mutex Mutex;
    1.28 +
    1.29 +private:
    1.30 +    friend class nsCacheService;
    1.31 +    friend class nsCacheEntry;
    1.32 +    friend class nsProcessRequestEvent;
    1.33 +
    1.34 +    nsCacheRequest( const nsACString &    key,
    1.35 +                    nsICacheListener *    listener,
    1.36 +                    nsCacheAccessMode     accessRequested,
    1.37 +                    bool                  blockingMode,
    1.38 +                    nsCacheSession *      session)
    1.39 +        : mKey(key),
    1.40 +          mInfo(0),
    1.41 +          mListener(listener),
    1.42 +          mLock("nsCacheRequest.mLock"),
    1.43 +          mCondVar(mLock, "nsCacheRequest.mCondVar"),
    1.44 +          mProfileDir(session->ProfileDir())
    1.45 +    {
    1.46 +        MOZ_COUNT_CTOR(nsCacheRequest);
    1.47 +        PR_INIT_CLIST(this);
    1.48 +        SetAccessRequested(accessRequested);
    1.49 +        SetStoragePolicy(session->StoragePolicy());
    1.50 +        if (session->IsStreamBased())             MarkStreamBased();
    1.51 +        if (session->WillDoomEntriesIfExpired())  MarkDoomEntriesIfExpired();
    1.52 +        if (session->IsPrivate())                 MarkPrivate();
    1.53 +        if (blockingMode == nsICache::BLOCKING)    MarkBlockingMode();
    1.54 +        MarkWaitingForValidation();
    1.55 +        NS_IF_ADDREF(mListener);
    1.56 +    }
    1.57 +    
    1.58 +    ~nsCacheRequest()
    1.59 +    {
    1.60 +        MOZ_COUNT_DTOR(nsCacheRequest);
    1.61 +        NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "request still on a list");
    1.62 +
    1.63 +        if (mListener)
    1.64 +            nsCacheService::ReleaseObject_Locked(mListener, mThread);
    1.65 +    }
    1.66 +    
    1.67 +    /**
    1.68 +     * Simple Accessors
    1.69 +     */
    1.70 +    enum CacheRequestInfo {
    1.71 +        eStoragePolicyMask         = 0x000000FF,
    1.72 +        eStreamBasedMask           = 0x00000100,
    1.73 +        ePrivateMask               = 0x00000200,
    1.74 +        eDoomEntriesIfExpiredMask  = 0x00001000,
    1.75 +        eBlockingModeMask          = 0x00010000,
    1.76 +        eWaitingForValidationMask  = 0x00100000,
    1.77 +        eAccessRequestedMask       = 0xFF000000
    1.78 +    };
    1.79 +
    1.80 +    void SetAccessRequested(nsCacheAccessMode mode)
    1.81 +    {
    1.82 +        NS_ASSERTION(mode <= 0xFF, "too many bits in nsCacheAccessMode");
    1.83 +        mInfo &= ~eAccessRequestedMask;
    1.84 +        mInfo |= mode << 24;
    1.85 +    }
    1.86 +
    1.87 +    nsCacheAccessMode AccessRequested()
    1.88 +    {
    1.89 +        return (nsCacheAccessMode)((mInfo >> 24) & 0xFF);
    1.90 +    }
    1.91 +
    1.92 +    void MarkStreamBased()      { mInfo |=  eStreamBasedMask; }
    1.93 +    bool IsStreamBased()      { return (mInfo & eStreamBasedMask) != 0; }
    1.94 +
    1.95 +
    1.96 +    void   MarkDoomEntriesIfExpired()   { mInfo |=  eDoomEntriesIfExpiredMask; }
    1.97 +    bool WillDoomEntriesIfExpired()   { return (0 != (mInfo & eDoomEntriesIfExpiredMask)); }
    1.98 +    
    1.99 +    void   MarkBlockingMode()           { mInfo |= eBlockingModeMask; }
   1.100 +    bool IsBlocking()                 { return (0 != (mInfo & eBlockingModeMask)); }
   1.101 +    bool IsNonBlocking()              { return !(mInfo & eBlockingModeMask); }
   1.102 +
   1.103 +    void SetStoragePolicy(nsCacheStoragePolicy policy)
   1.104 +    {
   1.105 +        NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
   1.106 +        mInfo &= ~eStoragePolicyMask;  // clear storage policy bits
   1.107 +        mInfo |= policy;         // or in new bits
   1.108 +    }
   1.109 +
   1.110 +    nsCacheStoragePolicy StoragePolicy()
   1.111 +    {
   1.112 +        return (nsCacheStoragePolicy)(mInfo & eStoragePolicyMask);
   1.113 +    }
   1.114 +
   1.115 +    void   MarkPrivate() { mInfo |= ePrivateMask; }
   1.116 +    void   MarkPublic() { mInfo &= ~ePrivateMask; }
   1.117 +    bool   IsPrivate() { return (mInfo & ePrivateMask) != 0; }
   1.118 +
   1.119 +    void   MarkWaitingForValidation() { mInfo |=  eWaitingForValidationMask; }
   1.120 +    void   DoneWaitingForValidation() { mInfo &= ~eWaitingForValidationMask; }
   1.121 +    bool WaitingForValidation()
   1.122 +    {
   1.123 +        return (mInfo & eWaitingForValidationMask) != 0;
   1.124 +    }
   1.125 +
   1.126 +    nsresult
   1.127 +    WaitForValidation(void)
   1.128 +    {
   1.129 +        if (!WaitingForValidation()) {   // flag already cleared
   1.130 +            MarkWaitingForValidation();  // set up for next time
   1.131 +            return NS_OK;                // early exit;
   1.132 +        }
   1.133 +        {
   1.134 +            MutexAutoLock lock(mLock);
   1.135 +            while (WaitingForValidation()) {
   1.136 +                mCondVar.Wait();
   1.137 +            }
   1.138 +            MarkWaitingForValidation();  // set up for next time
   1.139 +        }       
   1.140 +        return NS_OK;
   1.141 +    }
   1.142 +
   1.143 +    void WakeUp(void) {
   1.144 +        DoneWaitingForValidation();
   1.145 +        MutexAutoLock lock(mLock);
   1.146 +        mCondVar.Notify();
   1.147 +    }
   1.148 +
   1.149 +    /**
   1.150 +     * Data members
   1.151 +     */
   1.152 +    nsCString                  mKey;
   1.153 +    uint32_t                   mInfo;
   1.154 +    nsICacheListener *         mListener;  // strong ref
   1.155 +    nsCOMPtr<nsIThread>        mThread;
   1.156 +    Mutex                      mLock;
   1.157 +    CondVar                    mCondVar;
   1.158 +    nsCOMPtr<nsIFile>          mProfileDir;
   1.159 +};
   1.160 +
   1.161 +#endif // _nsCacheRequest_h_

mercurial