1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
|
/*
Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
Copyright (C) 2004, 2005, 2006, 2007 Apple Inc. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Library General Public
License as published by the Free Software Foundation; either
version 2 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Library General Public License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to
the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
Boston, MA 02110-1301, USA.
*/
#include "config.h"
#include "CachedResource.h"
#include "Cache.h"
#include "CachedResourceHandle.h"
#include "DocLoader.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "KURL.h"
#include "Request.h"
#include "SystemTime.h"
#include <wtf/RefCountedLeakCounter.h>
#include <wtf/Vector.h>
using namespace WTF;
namespace WebCore {
#ifndef NDEBUG
static RefCountedLeakCounter cachedResourceLeakCounter("CachedResource");
#endif
CachedResource::CachedResource(const String& url, Type type)
: m_url(url)
, m_lastDecodedAccessTime(0)
, m_sendResourceLoadCallbacks(true)
, m_preloadCount(0)
, m_preloadResult(PreloadNotReferenced)
, m_requestedFromNetworkingLayer(false)
, m_inCache(false)
, m_loading(false)
, m_docLoader(0)
, m_handleCount(0)
, m_resourceToRevalidate(0)
, m_isBeingRevalidated(false)
, m_expirationDate(0)
{
#ifndef NDEBUG
cachedResourceLeakCounter.increment();
#endif
m_type = type;
m_status = Pending;
m_encodedSize = 0;
m_decodedSize = 0;
m_request = 0;
m_accessCount = 0;
m_inLiveDecodedResourcesList = false;
m_nextInAllResourcesList = 0;
m_prevInAllResourcesList = 0;
m_nextInLiveResourcesList = 0;
m_prevInLiveResourcesList = 0;
#ifndef NDEBUG
m_deleted = false;
m_lruIndex = 0;
#endif
m_errorOccurred = false;
}
CachedResource::~CachedResource()
{
ASSERT(!inCache());
ASSERT(!m_deleted);
ASSERT(url().isNull() || cache()->resourceForURL(url()) != this);
#ifndef NDEBUG
m_deleted = true;
cachedResourceLeakCounter.decrement();
#endif
if (m_resourceToRevalidate)
m_resourceToRevalidate->m_isBeingRevalidated = false;
if (m_docLoader)
m_docLoader->removeCachedResource(this);
}
void CachedResource::load(DocLoader* docLoader, bool incremental, bool skipCanLoadCheck, bool sendResourceLoadCallbacks)
{
m_sendResourceLoadCallbacks = sendResourceLoadCallbacks;
cache()->loader()->load(docLoader, this, incremental, skipCanLoadCheck, sendResourceLoadCallbacks);
m_loading = true;
}
void CachedResource::finish()
{
m_status = Cached;
}
bool CachedResource::isExpired() const
{
if (!m_expirationDate)
return false;
time_t now = time(0);
return difftime(now, m_expirationDate) >= 0;
}
void CachedResource::setResponse(const ResourceResponse& response)
{
m_response = response;
m_expirationDate = response.expirationDate();
}
void CachedResource::setRequest(Request* request)
{
if (request && !m_request)
m_status = Pending;
m_request = request;
if (canDelete() && !inCache())
delete this;
}
void CachedResource::addClient(CachedResourceClient *c)
{
if (m_preloadResult == PreloadNotReferenced) {
if (isLoaded())
m_preloadResult = PreloadReferencedWhileComplete;
else if (m_requestedFromNetworkingLayer)
m_preloadResult = PreloadReferencedWhileLoading;
else
m_preloadResult = PreloadReferenced;
}
if (!hasClients() && inCache())
cache()->addToLiveResourcesSize(this);
m_clients.add(c);
}
void CachedResource::removeClient(CachedResourceClient *c)
{
ASSERT(m_clients.contains(c));
m_clients.remove(c);
if (canDelete() && !inCache())
delete this;
else if (!hasClients() && inCache()) {
cache()->removeFromLiveResourcesSize(this);
cache()->removeFromLiveDecodedResourcesList(this);
allClientsRemoved();
cache()->prune();
}
}
void CachedResource::deleteIfPossible()
{
if (canDelete() && !inCache())
delete this;
}
void CachedResource::setDecodedSize(unsigned size)
{
if (size == m_decodedSize)
return;
int delta = size - m_decodedSize;
// The object must now be moved to a different queue, since its size has been changed.
// We have to remove explicitly before updating m_decodedSize, so that we find the correct previous
// queue.
if (inCache())
cache()->removeFromLRUList(this);
m_decodedSize = size;
if (inCache()) {
// Now insert into the new LRU list.
cache()->insertInLRUList(this);
// Insert into or remove from the live decoded list if necessary.
if (m_decodedSize && !m_inLiveDecodedResourcesList && hasClients())
cache()->insertInLiveDecodedResourcesList(this);
else if (!m_decodedSize && m_inLiveDecodedResourcesList)
cache()->removeFromLiveDecodedResourcesList(this);
// Update the cache's size totals.
cache()->adjustSize(hasClients(), delta);
}
}
void CachedResource::setEncodedSize(unsigned size)
{
if (size == m_encodedSize)
return;
// The size cannot ever shrink (unless it is being nulled out because of an error). If it ever does, assert.
ASSERT(size == 0 || size >= m_encodedSize);
int delta = size - m_encodedSize;
// The object must now be moved to a different queue, since its size has been changed.
// We have to remove explicitly before updating m_encodedSize, so that we find the correct previous
// queue.
if (inCache())
cache()->removeFromLRUList(this);
m_encodedSize = size;
if (inCache()) {
// Now insert into the new LRU list.
cache()->insertInLRUList(this);
// Update the cache's size totals.
cache()->adjustSize(hasClients(), delta);
}
}
void CachedResource::didAccessDecodedData(double timeStamp)
{
m_lastDecodedAccessTime = timeStamp;
if (inCache()) {
if (m_inLiveDecodedResourcesList) {
cache()->removeFromLiveDecodedResourcesList(this);
cache()->insertInLiveDecodedResourcesList(this);
}
cache()->prune();
}
}
void CachedResource::setResourceToRevalidate(CachedResource* resource)
{
ASSERT(resource);
ASSERT(!m_resourceToRevalidate);
ASSERT(resource != this);
ASSERT(!resource->m_isBeingRevalidated);
ASSERT(m_handlesToRevalidate.isEmpty());
ASSERT(resource->type() == type());
resource->m_isBeingRevalidated = true;
m_resourceToRevalidate = resource;
}
void CachedResource::clearResourceToRevalidate()
{
ASSERT(m_resourceToRevalidate);
ASSERT(m_resourceToRevalidate->m_isBeingRevalidated);
m_resourceToRevalidate->m_isBeingRevalidated = false;
m_resourceToRevalidate->deleteIfPossible();
m_handlesToRevalidate.clear();
m_resourceToRevalidate = 0;
deleteIfPossible();
}
void CachedResource::switchClientsToRevalidatedResource()
{
ASSERT(m_resourceToRevalidate);
ASSERT(!inCache());
HashSet<CachedResourceHandleBase*>::iterator end = m_handlesToRevalidate.end();
for (HashSet<CachedResourceHandleBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
CachedResourceHandleBase* handle = *it;
handle->m_resource = m_resourceToRevalidate;
m_resourceToRevalidate->registerHandle(handle);
--m_handleCount;
}
ASSERT(!m_handleCount);
m_handlesToRevalidate.clear();
Vector<CachedResourceClient*> clientsToMove;
HashCountedSet<CachedResourceClient*>::iterator end2 = m_clients.end();
for (HashCountedSet<CachedResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
CachedResourceClient* client = it->first;
unsigned count = it->second;
while (count) {
clientsToMove.append(client);
--count;
}
}
// Equivalent of calling removeClient() for all clients
m_clients.clear();
unsigned moveCount = clientsToMove.size();
for (unsigned n = 0; n < moveCount; ++n)
m_resourceToRevalidate->addClient(clientsToMove[n]);
}
bool CachedResource::canUseCacheValidator() const
{
return !m_loading && (!m_response.httpHeaderField("Last-Modified").isEmpty() || !m_response.httpHeaderField("ETag").isEmpty());
}
bool CachedResource::mustRevalidate(CachePolicy cachePolicy) const
{
if (m_loading)
return false;
String cacheControl = m_response.httpHeaderField("Cache-Control");
// FIXME: It would be better to tokenize the field.
if (cachePolicy == CachePolicyCache)
return !cacheControl.isEmpty() && (cacheControl.contains("no-cache", false) || (isExpired() && cacheControl.contains("must-revalidate", false)));
return isExpired() || cacheControl.contains("no-cache", false);
}
}
|