Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #ifndef _nsCacheRequest_h_ |
michael@0 | 8 | #define _nsCacheRequest_h_ |
michael@0 | 9 | |
michael@0 | 10 | #include "nspr.h" |
michael@0 | 11 | #include "mozilla/CondVar.h" |
michael@0 | 12 | #include "mozilla/Mutex.h" |
michael@0 | 13 | #include "nsCOMPtr.h" |
michael@0 | 14 | #include "nsICache.h" |
michael@0 | 15 | #include "nsICacheListener.h" |
michael@0 | 16 | #include "nsCacheSession.h" |
michael@0 | 17 | #include "nsCacheService.h" |
michael@0 | 18 | |
michael@0 | 19 | |
michael@0 | 20 | class nsCacheRequest : public PRCList |
michael@0 | 21 | { |
michael@0 | 22 | typedef mozilla::CondVar CondVar; |
michael@0 | 23 | typedef mozilla::MutexAutoLock MutexAutoLock; |
michael@0 | 24 | typedef mozilla::Mutex Mutex; |
michael@0 | 25 | |
michael@0 | 26 | private: |
michael@0 | 27 | friend class nsCacheService; |
michael@0 | 28 | friend class nsCacheEntry; |
michael@0 | 29 | friend class nsProcessRequestEvent; |
michael@0 | 30 | |
michael@0 | 31 | nsCacheRequest( const nsACString & key, |
michael@0 | 32 | nsICacheListener * listener, |
michael@0 | 33 | nsCacheAccessMode accessRequested, |
michael@0 | 34 | bool blockingMode, |
michael@0 | 35 | nsCacheSession * session) |
michael@0 | 36 | : mKey(key), |
michael@0 | 37 | mInfo(0), |
michael@0 | 38 | mListener(listener), |
michael@0 | 39 | mLock("nsCacheRequest.mLock"), |
michael@0 | 40 | mCondVar(mLock, "nsCacheRequest.mCondVar"), |
michael@0 | 41 | mProfileDir(session->ProfileDir()) |
michael@0 | 42 | { |
michael@0 | 43 | MOZ_COUNT_CTOR(nsCacheRequest); |
michael@0 | 44 | PR_INIT_CLIST(this); |
michael@0 | 45 | SetAccessRequested(accessRequested); |
michael@0 | 46 | SetStoragePolicy(session->StoragePolicy()); |
michael@0 | 47 | if (session->IsStreamBased()) MarkStreamBased(); |
michael@0 | 48 | if (session->WillDoomEntriesIfExpired()) MarkDoomEntriesIfExpired(); |
michael@0 | 49 | if (session->IsPrivate()) MarkPrivate(); |
michael@0 | 50 | if (blockingMode == nsICache::BLOCKING) MarkBlockingMode(); |
michael@0 | 51 | MarkWaitingForValidation(); |
michael@0 | 52 | NS_IF_ADDREF(mListener); |
michael@0 | 53 | } |
michael@0 | 54 | |
michael@0 | 55 | ~nsCacheRequest() |
michael@0 | 56 | { |
michael@0 | 57 | MOZ_COUNT_DTOR(nsCacheRequest); |
michael@0 | 58 | NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "request still on a list"); |
michael@0 | 59 | |
michael@0 | 60 | if (mListener) |
michael@0 | 61 | nsCacheService::ReleaseObject_Locked(mListener, mThread); |
michael@0 | 62 | } |
michael@0 | 63 | |
michael@0 | 64 | /** |
michael@0 | 65 | * Simple Accessors |
michael@0 | 66 | */ |
michael@0 | 67 | enum CacheRequestInfo { |
michael@0 | 68 | eStoragePolicyMask = 0x000000FF, |
michael@0 | 69 | eStreamBasedMask = 0x00000100, |
michael@0 | 70 | ePrivateMask = 0x00000200, |
michael@0 | 71 | eDoomEntriesIfExpiredMask = 0x00001000, |
michael@0 | 72 | eBlockingModeMask = 0x00010000, |
michael@0 | 73 | eWaitingForValidationMask = 0x00100000, |
michael@0 | 74 | eAccessRequestedMask = 0xFF000000 |
michael@0 | 75 | }; |
michael@0 | 76 | |
michael@0 | 77 | void SetAccessRequested(nsCacheAccessMode mode) |
michael@0 | 78 | { |
michael@0 | 79 | NS_ASSERTION(mode <= 0xFF, "too many bits in nsCacheAccessMode"); |
michael@0 | 80 | mInfo &= ~eAccessRequestedMask; |
michael@0 | 81 | mInfo |= mode << 24; |
michael@0 | 82 | } |
michael@0 | 83 | |
michael@0 | 84 | nsCacheAccessMode AccessRequested() |
michael@0 | 85 | { |
michael@0 | 86 | return (nsCacheAccessMode)((mInfo >> 24) & 0xFF); |
michael@0 | 87 | } |
michael@0 | 88 | |
michael@0 | 89 | void MarkStreamBased() { mInfo |= eStreamBasedMask; } |
michael@0 | 90 | bool IsStreamBased() { return (mInfo & eStreamBasedMask) != 0; } |
michael@0 | 91 | |
michael@0 | 92 | |
michael@0 | 93 | void MarkDoomEntriesIfExpired() { mInfo |= eDoomEntriesIfExpiredMask; } |
michael@0 | 94 | bool WillDoomEntriesIfExpired() { return (0 != (mInfo & eDoomEntriesIfExpiredMask)); } |
michael@0 | 95 | |
michael@0 | 96 | void MarkBlockingMode() { mInfo |= eBlockingModeMask; } |
michael@0 | 97 | bool IsBlocking() { return (0 != (mInfo & eBlockingModeMask)); } |
michael@0 | 98 | bool IsNonBlocking() { return !(mInfo & eBlockingModeMask); } |
michael@0 | 99 | |
michael@0 | 100 | void SetStoragePolicy(nsCacheStoragePolicy policy) |
michael@0 | 101 | { |
michael@0 | 102 | NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy"); |
michael@0 | 103 | mInfo &= ~eStoragePolicyMask; // clear storage policy bits |
michael@0 | 104 | mInfo |= policy; // or in new bits |
michael@0 | 105 | } |
michael@0 | 106 | |
michael@0 | 107 | nsCacheStoragePolicy StoragePolicy() |
michael@0 | 108 | { |
michael@0 | 109 | return (nsCacheStoragePolicy)(mInfo & eStoragePolicyMask); |
michael@0 | 110 | } |
michael@0 | 111 | |
michael@0 | 112 | void MarkPrivate() { mInfo |= ePrivateMask; } |
michael@0 | 113 | void MarkPublic() { mInfo &= ~ePrivateMask; } |
michael@0 | 114 | bool IsPrivate() { return (mInfo & ePrivateMask) != 0; } |
michael@0 | 115 | |
michael@0 | 116 | void MarkWaitingForValidation() { mInfo |= eWaitingForValidationMask; } |
michael@0 | 117 | void DoneWaitingForValidation() { mInfo &= ~eWaitingForValidationMask; } |
michael@0 | 118 | bool WaitingForValidation() |
michael@0 | 119 | { |
michael@0 | 120 | return (mInfo & eWaitingForValidationMask) != 0; |
michael@0 | 121 | } |
michael@0 | 122 | |
michael@0 | 123 | nsresult |
michael@0 | 124 | WaitForValidation(void) |
michael@0 | 125 | { |
michael@0 | 126 | if (!WaitingForValidation()) { // flag already cleared |
michael@0 | 127 | MarkWaitingForValidation(); // set up for next time |
michael@0 | 128 | return NS_OK; // early exit; |
michael@0 | 129 | } |
michael@0 | 130 | { |
michael@0 | 131 | MutexAutoLock lock(mLock); |
michael@0 | 132 | while (WaitingForValidation()) { |
michael@0 | 133 | mCondVar.Wait(); |
michael@0 | 134 | } |
michael@0 | 135 | MarkWaitingForValidation(); // set up for next time |
michael@0 | 136 | } |
michael@0 | 137 | return NS_OK; |
michael@0 | 138 | } |
michael@0 | 139 | |
michael@0 | 140 | void WakeUp(void) { |
michael@0 | 141 | DoneWaitingForValidation(); |
michael@0 | 142 | MutexAutoLock lock(mLock); |
michael@0 | 143 | mCondVar.Notify(); |
michael@0 | 144 | } |
michael@0 | 145 | |
michael@0 | 146 | /** |
michael@0 | 147 | * Data members |
michael@0 | 148 | */ |
michael@0 | 149 | nsCString mKey; |
michael@0 | 150 | uint32_t mInfo; |
michael@0 | 151 | nsICacheListener * mListener; // strong ref |
michael@0 | 152 | nsCOMPtr<nsIThread> mThread; |
michael@0 | 153 | Mutex mLock; |
michael@0 | 154 | CondVar mCondVar; |
michael@0 | 155 | nsCOMPtr<nsIFile> mProfileDir; |
michael@0 | 156 | }; |
michael@0 | 157 | |
michael@0 | 158 | #endif // _nsCacheRequest_h_ |