1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is nsCacheRequest.h, released
17 : * February 22, 2001.
18 : *
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.
23 : *
24 : * Contributor(s):
25 : * Gordon Sheridan, 22-February-2001
26 : *
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.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef _nsCacheRequest_h_
42 : #define _nsCacheRequest_h_
43 :
44 : #include "nspr.h"
45 : #include "mozilla/CondVar.h"
46 : #include "mozilla/Mutex.h"
47 : #include "nsCOMPtr.h"
48 : #include "nsICache.h"
49 : #include "nsICacheListener.h"
50 : #include "nsCacheSession.h"
51 : #include "nsCacheService.h"
52 :
53 :
54 : class nsCacheRequest : public PRCList
55 : {
56 : typedef mozilla::CondVar CondVar;
57 : typedef mozilla::MutexAutoLock MutexAutoLock;
58 : typedef mozilla::Mutex Mutex;
59 :
60 : private:
61 : friend class nsCacheService;
62 : friend class nsCacheEntry;
63 : friend class nsProcessRequestEvent;
64 :
65 2739 : nsCacheRequest( nsCString * key,
66 : nsICacheListener * listener,
67 : nsCacheAccessMode accessRequested,
68 : bool blockingMode,
69 : nsCacheSession * session)
70 : : mKey(key),
71 : mInfo(0),
72 : mListener(listener),
73 : mLock("nsCacheRequest.mLock"),
74 2739 : mCondVar(mLock, "nsCacheRequest.mCondVar")
75 : {
76 2739 : MOZ_COUNT_CTOR(nsCacheRequest);
77 2739 : PR_INIT_CLIST(this);
78 2739 : SetAccessRequested(accessRequested);
79 2739 : SetStoragePolicy(session->StoragePolicy());
80 2739 : if (session->IsStreamBased()) MarkStreamBased();
81 2739 : if (session->WillDoomEntriesIfExpired()) MarkDoomEntriesIfExpired();
82 2739 : if (blockingMode == nsICache::BLOCKING) MarkBlockingMode();
83 2739 : MarkWaitingForValidation();
84 2739 : NS_IF_ADDREF(mListener);
85 2739 : }
86 :
87 2739 : ~nsCacheRequest()
88 2739 : {
89 2739 : MOZ_COUNT_DTOR(nsCacheRequest);
90 2739 : delete mKey;
91 2739 : NS_ASSERTION(PR_CLIST_IS_EMPTY(this), "request still on a list");
92 :
93 2739 : if (mListener)
94 262 : nsCacheService::ReleaseObject_Locked(mListener, mThread);
95 2739 : }
96 :
97 : /**
98 : * Simple Accessors
99 : */
100 : enum CacheRequestInfo {
101 : eStoragePolicyMask = 0x000000FF,
102 : eStreamBasedMask = 0x00000100,
103 : eDoomEntriesIfExpiredMask = 0x00001000,
104 : eBlockingModeMask = 0x00010000,
105 : eWaitingForValidationMask = 0x00100000,
106 : eAccessRequestedMask = 0xFF000000
107 : };
108 :
109 2739 : void SetAccessRequested(nsCacheAccessMode mode)
110 : {
111 2739 : NS_ASSERTION(mode <= 0xFF, "too many bits in nsCacheAccessMode");
112 2739 : mInfo &= ~eAccessRequestedMask;
113 2739 : mInfo |= mode << 24;
114 2739 : }
115 :
116 7046 : nsCacheAccessMode AccessRequested()
117 : {
118 7046 : return (nsCacheAccessMode)((mInfo >> 24) & 0xFF);
119 : }
120 :
121 2739 : void MarkStreamBased() { mInfo |= eStreamBasedMask; }
122 4005 : bool IsStreamBased() { return (mInfo & eStreamBasedMask) != 0; }
123 :
124 :
125 108 : void MarkDoomEntriesIfExpired() { mInfo |= eDoomEntriesIfExpiredMask; }
126 396 : bool WillDoomEntriesIfExpired() { return (0 != (mInfo & eDoomEntriesIfExpiredMask)); }
127 :
128 1912 : void MarkBlockingMode() { mInfo |= eBlockingModeMask; }
129 3 : bool IsBlocking() { return (0 != (mInfo & eBlockingModeMask)); }
130 : bool IsNonBlocking() { return !(mInfo & eBlockingModeMask); }
131 :
132 2739 : void SetStoragePolicy(nsCacheStoragePolicy policy)
133 : {
134 2739 : NS_ASSERTION(policy <= 0xFF, "too many bits in nsCacheStoragePolicy");
135 2739 : mInfo &= ~eStoragePolicyMask; // clear storage policy bits
136 2739 : mInfo |= policy; // or in new bits
137 2739 : }
138 :
139 7901 : nsCacheStoragePolicy StoragePolicy()
140 : {
141 7901 : return (nsCacheStoragePolicy)(mInfo & 0xFF);
142 : }
143 :
144 2739 : void MarkWaitingForValidation() { mInfo |= eWaitingForValidationMask; }
145 0 : void DoneWaitingForValidation() { mInfo &= ~eWaitingForValidationMask; }
146 0 : bool WaitingForValidation()
147 : {
148 0 : return (mInfo & eWaitingForValidationMask) != 0;
149 : }
150 :
151 : nsresult
152 0 : WaitForValidation(void)
153 : {
154 0 : if (!WaitingForValidation()) { // flag already cleared
155 0 : MarkWaitingForValidation(); // set up for next time
156 0 : return NS_OK; // early exit;
157 : }
158 : {
159 0 : MutexAutoLock lock(mLock);
160 0 : while (WaitingForValidation()) {
161 0 : mCondVar.Wait();
162 : }
163 0 : MarkWaitingForValidation(); // set up for next time
164 : }
165 0 : return NS_OK;
166 : }
167 :
168 0 : void WakeUp(void) {
169 0 : DoneWaitingForValidation();
170 0 : MutexAutoLock lock(mLock);
171 0 : mCondVar.Notify();
172 0 : }
173 :
174 : /**
175 : * Data members
176 : */
177 : nsCString * mKey;
178 : PRUint32 mInfo;
179 : nsICacheListener * mListener; // strong ref
180 : nsCOMPtr<nsIThread> mThread;
181 : Mutex mLock;
182 : CondVar mCondVar;
183 : };
184 :
185 : #endif // _nsCacheRequest_h_
|