Bug 458861. Validate TrueType headers before activating downloaded font. r=roc, sr...
[wine-gecko.git] / netwerk / cache / src / nsCacheRequest.h
blob19b869625fc47c15d8755ce38942178d86087595
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is nsCacheRequest.h, released
17 * February 22, 2001.
19 * The Initial Developer of the Original Code is
20 * Netscape Communications Corporation.
21 * Portions created by the Initial Developer are Copyright (C) 2001
22 * the Initial Developer. All Rights Reserved.
24 * Contributor(s):
25 * Gordon Sheridan, 22-February-2001
27 * Alternatively, the contents of this file may be used under the terms of
28 * either the GNU General Public License Version 2 or later (the "GPL"), or
29 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 * in which case the provisions of the GPL or the LGPL are applicable instead
31 * of those above. If you wish to allow use of your version of this file only
32 * under the terms of either the GPL or the LGPL, and not to allow others to
33 * use your version of this file under the terms of the MPL, indicate your
34 * decision by deleting the provisions above and replace them with the notice
35 * and other provisions required by the GPL or the LGPL. If you do not delete
36 * the provisions above, a recipient may use your version of this file under
37 * the terms of any one of the MPL, the GPL or the LGPL.
39 * ***** END LICENSE BLOCK ***** */
41 #ifndef _nsCacheRequest_h_
42 #define _nsCacheRequest_h_
44 #include "nspr.h"
45 #include "nsCOMPtr.h"
46 #include "nsICache.h"
47 #include "nsICacheListener.h"
48 #include "nsCacheSession.h"
49 #include "nsCacheService.h"
52 class nsCacheRequest : public PRCList
54 private:
55 friend class nsCacheService;
56 friend class nsCacheEntry;
58 nsCacheRequest( nsCString * key,
59 nsICacheListener * listener,
60 nsCacheAccessMode accessRequested,
61 PRBool blockingMode,
62 nsCacheSession * session)
63 : mKey(key),
64 mInfo(0),
65 mListener(listener),
66 mLock(nsnull),
67 mCondVar(nsnull)
69 MOZ_COUNT_CTOR(nsCacheRequest);
70 PR_INIT_CLIST(this);
71 SetAccessRequested(accessRequested);
72 SetStoragePolicy(session->StoragePolicy());
73 if (session->IsStreamBased()) MarkStreamBased();
74 if (session->WillDoomEntriesIfExpired()) MarkDoomEntriesIfExpired();
75 if (blockingMode == nsICache::BLOCKING) MarkBlockingMode();
76 MarkWaitingForValidation();
77 NS_IF_ADDREF(mListener);
80 ~nsCacheRequest()
82 MOZ_COUNT_DTOR(nsCacheRequest);
83 delete mKey;
84 if (mLock) PR_DestroyLock(mLock);
85 if (mCondVar) PR_DestroyCondVar(mCondVar);
86 NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "request still on a list");
88 if (mListener)
89 nsCacheService::ReleaseObject_Locked(mListener, mThread);
92 /**
93 * Simple Accessors
95 enum CacheRequestInfo {
96 eStoragePolicyMask = 0x000000FF,
97 eStreamBasedMask = 0x00000100,
98 eDoomEntriesIfExpiredMask = 0x00001000,
99 eBlockingModeMask = 0x00010000,
100 eWaitingForValidationMask = 0x00100000,
101 eAccessRequestedMask = 0xFF000000
104 void SetAccessRequested(nsCacheAccessMode mode)
106 NS_ASSERTION(mode <= 0xFF, "too many bits in nsCacheAccessMode");
107 mInfo &= ~eAccessRequestedMask;
108 mInfo |= mode << 24;
111 nsCacheAccessMode AccessRequested()
113 return (nsCacheAccessMode)((mInfo >> 24) & 0xFF);
116 void MarkStreamBased() { mInfo |= eStreamBasedMask; }
117 PRBool IsStreamBased() { return (mInfo & eStreamBasedMask) != 0; }
120 void MarkDoomEntriesIfExpired() { mInfo |= eDoomEntriesIfExpiredMask; }
121 PRBool WillDoomEntriesIfExpired() { return (0 != (mInfo & eDoomEntriesIfExpiredMask)); }
123 void MarkBlockingMode() { mInfo |= eBlockingModeMask; }
124 PRBool IsBlocking() { return (0 != (mInfo & eBlockingModeMask)); }
125 PRBool IsNonBlocking() { return !(mInfo & eBlockingModeMask); }
127 void SetStoragePolicy(nsCacheStoragePolicy policy)
129 NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
130 mInfo &= ~eStoragePolicyMask; // clear storage policy bits
131 mInfo |= policy; // or in new bits
134 nsCacheStoragePolicy StoragePolicy()
136 return (nsCacheStoragePolicy)(mInfo & 0xFF);
139 void MarkWaitingForValidation() { mInfo |= eWaitingForValidationMask; }
140 void DoneWaitingForValidation() { mInfo &= ~eWaitingForValidationMask; }
141 PRBool WaitingForValidation()
143 return (mInfo & eWaitingForValidationMask) != 0;
146 nsresult
147 WaitForValidation(void)
149 if (!WaitingForValidation()) { // flag already cleared
150 MarkWaitingForValidation(); // set up for next time
151 return NS_OK; // early exit;
154 if (!mLock) {
155 mLock = PR_NewLock();
156 if (!mLock) return NS_ERROR_OUT_OF_MEMORY;
158 NS_ASSERTION(!mCondVar,"we have mCondVar, but didn't have mLock?");
159 mCondVar = PR_NewCondVar(mLock);
160 if (!mCondVar) {
161 PR_DestroyLock(mLock);
162 return NS_ERROR_OUT_OF_MEMORY;
165 PRStatus status = PR_SUCCESS;
166 PR_Lock(mLock);
167 while (WaitingForValidation() && (status == PR_SUCCESS) ) {
168 status = PR_WaitCondVar(mCondVar, PR_INTERVAL_NO_TIMEOUT);
170 MarkWaitingForValidation(); // set up for next time
171 PR_Unlock(mLock);
173 NS_ASSERTION(status == PR_SUCCESS, "PR_WaitCondVar() returned PR_FAILURE?");
174 if (status == PR_FAILURE)
175 return NS_ERROR_UNEXPECTED;
177 return NS_OK;
180 void WakeUp(void) {
181 DoneWaitingForValidation();
182 if (mLock) {
183 PR_Lock(mLock);
184 PR_NotifyCondVar(mCondVar);
185 PR_Unlock(mLock);
190 * Data members
192 nsCString * mKey;
193 PRUint32 mInfo;
194 nsICacheListener * mListener; // strong ref
195 nsCOMPtr<nsIThread> mThread;
196 PRLock * mLock;
197 PRCondVar * mCondVar;
200 #endif // _nsCacheRequest_h_