|
1 /* |
|
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) |
|
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) |
|
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) |
|
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) |
|
6 Copyright (C) 2004, 2005, 2006, 2007, 2008 Apple Inc. All rights reserved. |
|
7 |
|
8 This library is free software; you can redistribute it and/or |
|
9 modify it under the terms of the GNU Library General Public |
|
10 License as published by the Free Software Foundation; either |
|
11 version 2 of the License, or (at your option) any later version. |
|
12 |
|
13 This library is distributed in the hope that it will be useful, |
|
14 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
16 Library General Public License for more details. |
|
17 |
|
18 You should have received a copy of the GNU Library General Public License |
|
19 along with this library; see the file COPYING.LIB. If not, write to |
|
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
|
21 Boston, MA 02110-1301, USA. |
|
22 */ |
|
23 |
|
24 #include "config.h" |
|
25 #include "CachedResource.h" |
|
26 |
|
27 #include "Cache.h" |
|
28 #include "CachedMetadata.h" |
|
29 #include "CachedResourceClient.h" |
|
30 #include "CachedResourceClientWalker.h" |
|
31 #include "CachedResourceHandle.h" |
|
32 #include "DocLoader.h" |
|
33 #include "Frame.h" |
|
34 #include "FrameLoaderClient.h" |
|
35 #include "KURL.h" |
|
36 #include "PurgeableBuffer.h" |
|
37 #include "Request.h" |
|
38 #include "ResourceHandle.h" |
|
39 #include "SharedBuffer.h" |
|
40 #include <wtf/CurrentTime.h> |
|
41 #include <wtf/MathExtras.h> |
|
42 #include <wtf/RefCountedLeakCounter.h> |
|
43 #include <wtf/StdLibExtras.h> |
|
44 #include <wtf/Vector.h> |
|
45 |
|
46 using namespace WTF; |
|
47 |
|
48 namespace WebCore { |
|
49 |
|
50 #ifndef NDEBUG |
|
51 static RefCountedLeakCounter cachedResourceLeakCounter("CachedResource"); |
|
52 #endif |
|
53 |
|
54 CachedResource::CachedResource(const String& url, Type type) |
|
55 : m_url(url) |
|
56 , m_request(0) |
|
57 , m_responseTimestamp(currentTime()) |
|
58 , m_lastDecodedAccessTime(0) |
|
59 , m_encodedSize(0) |
|
60 , m_decodedSize(0) |
|
61 , m_accessCount(0) |
|
62 , m_handleCount(0) |
|
63 , m_preloadCount(0) |
|
64 , m_preloadResult(PreloadNotReferenced) |
|
65 , m_inLiveDecodedResourcesList(false) |
|
66 , m_requestedFromNetworkingLayer(false) |
|
67 , m_sendResourceLoadCallbacks(true) |
|
68 , m_errorOccurred(false) |
|
69 , m_inCache(false) |
|
70 , m_loading(false) |
|
71 , m_type(type) |
|
72 , m_status(Pending) |
|
73 #ifndef NDEBUG |
|
74 , m_deleted(false) |
|
75 , m_lruIndex(0) |
|
76 #endif |
|
77 , m_nextInAllResourcesList(0) |
|
78 , m_prevInAllResourcesList(0) |
|
79 , m_nextInLiveResourcesList(0) |
|
80 , m_prevInLiveResourcesList(0) |
|
81 , m_docLoader(0) |
|
82 , m_resourceToRevalidate(0) |
|
83 , m_proxyResource(0) |
|
84 { |
|
85 #ifndef NDEBUG |
|
86 cachedResourceLeakCounter.increment(); |
|
87 #endif |
|
88 } |
|
89 |
|
90 CachedResource::~CachedResource() |
|
91 { |
|
92 ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this. |
|
93 ASSERT(canDelete()); |
|
94 ASSERT(!inCache()); |
|
95 ASSERT(!m_deleted); |
|
96 ASSERT(url().isNull() || cache()->resourceForURL(url()) != this); |
|
97 #ifndef NDEBUG |
|
98 m_deleted = true; |
|
99 cachedResourceLeakCounter.decrement(); |
|
100 #endif |
|
101 |
|
102 if (m_docLoader) |
|
103 m_docLoader->removeCachedResource(this); |
|
104 } |
|
105 |
|
106 void CachedResource::load(DocLoader* docLoader, bool incremental, SecurityCheckPolicy securityCheck, bool sendResourceLoadCallbacks) |
|
107 { |
|
108 m_sendResourceLoadCallbacks = sendResourceLoadCallbacks; |
|
109 cache()->loader()->load(docLoader, this, incremental, securityCheck, sendResourceLoadCallbacks); |
|
110 m_loading = true; |
|
111 } |
|
112 |
|
113 void CachedResource::data(PassRefPtr<SharedBuffer>, bool allDataReceived) |
|
114 { |
|
115 if (!allDataReceived) |
|
116 return; |
|
117 |
|
118 CachedResourceClientWalker w(m_clients); |
|
119 while (CachedResourceClient* c = w.next()) |
|
120 c->notifyFinished(this); |
|
121 } |
|
122 |
|
123 void CachedResource::finish() |
|
124 { |
|
125 m_status = Cached; |
|
126 } |
|
127 |
|
128 bool CachedResource::isExpired() const |
|
129 { |
|
130 if (m_response.isNull()) |
|
131 return false; |
|
132 |
|
133 return currentAge() > freshnessLifetime(); |
|
134 } |
|
135 |
|
136 double CachedResource::currentAge() const |
|
137 { |
|
138 // RFC2616 13.2.3 |
|
139 // No compensation for latency as that is not terribly important in practice |
|
140 double dateValue = m_response.date(); |
|
141 double apparentAge = isfinite(dateValue) ? max(0., m_responseTimestamp - dateValue) : 0; |
|
142 double ageValue = m_response.age(); |
|
143 double correctedReceivedAge = isfinite(ageValue) ? max(apparentAge, ageValue) : apparentAge; |
|
144 double residentTime = currentTime() - m_responseTimestamp; |
|
145 return correctedReceivedAge + residentTime; |
|
146 } |
|
147 |
|
148 double CachedResource::freshnessLifetime() const |
|
149 { |
|
150 // Cache non-http resources liberally |
|
151 if (!m_response.url().protocolInHTTPFamily()) |
|
152 return std::numeric_limits<double>::max(); |
|
153 |
|
154 // RFC2616 13.2.4 |
|
155 double maxAgeValue = m_response.cacheControlMaxAge(); |
|
156 if (isfinite(maxAgeValue)) |
|
157 return maxAgeValue; |
|
158 double expiresValue = m_response.expires(); |
|
159 double dateValue = m_response.date(); |
|
160 double creationTime = isfinite(dateValue) ? dateValue : m_responseTimestamp; |
|
161 if (isfinite(expiresValue)) |
|
162 return expiresValue - creationTime; |
|
163 double lastModifiedValue = m_response.lastModified(); |
|
164 if (isfinite(lastModifiedValue)) |
|
165 return (creationTime - lastModifiedValue) * 0.1; |
|
166 // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0. |
|
167 return 0; |
|
168 } |
|
169 |
|
170 void CachedResource::setResponse(const ResourceResponse& response) |
|
171 { |
|
172 m_response = response; |
|
173 m_responseTimestamp = currentTime(); |
|
174 } |
|
175 |
|
176 void CachedResource::setSerializedCachedMetadata(const char* data, size_t size) |
|
177 { |
|
178 // We only expect to receive cached metadata from the platform once. |
|
179 // If this triggers, it indicates an efficiency problem which is most |
|
180 // likely unexpected in code designed to improve performance. |
|
181 ASSERT(!m_cachedMetadata); |
|
182 |
|
183 m_cachedMetadata = CachedMetadata::deserialize(data, size); |
|
184 } |
|
185 |
|
186 void CachedResource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size) |
|
187 { |
|
188 // Currently, only one type of cached metadata per resource is supported. |
|
189 // If the need arises for multiple types of metadata per resource this could |
|
190 // be enhanced to store types of metadata in a map. |
|
191 ASSERT(!m_cachedMetadata); |
|
192 |
|
193 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size); |
|
194 ResourceHandle::cacheMetadata(m_response, m_cachedMetadata->serialize()); |
|
195 } |
|
196 |
|
197 CachedMetadata* CachedResource::cachedMetadata(unsigned dataTypeID) const |
|
198 { |
|
199 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID) |
|
200 return 0; |
|
201 return m_cachedMetadata.get(); |
|
202 } |
|
203 |
|
204 void CachedResource::setRequest(Request* request) |
|
205 { |
|
206 if (request && !m_request) |
|
207 m_status = Pending; |
|
208 m_request = request; |
|
209 if (canDelete() && !inCache()) |
|
210 delete this; |
|
211 } |
|
212 |
|
213 void CachedResource::addClient(CachedResourceClient* client) |
|
214 { |
|
215 addClientToSet(client); |
|
216 didAddClient(client); |
|
217 } |
|
218 |
|
219 void CachedResource::didAddClient(CachedResourceClient* c) |
|
220 { |
|
221 if (!isLoading()) |
|
222 c->notifyFinished(this); |
|
223 } |
|
224 |
|
225 void CachedResource::addClientToSet(CachedResourceClient* client) |
|
226 { |
|
227 ASSERT(!isPurgeable()); |
|
228 |
|
229 if (m_preloadResult == PreloadNotReferenced) { |
|
230 if (isLoaded()) |
|
231 m_preloadResult = PreloadReferencedWhileComplete; |
|
232 else if (m_requestedFromNetworkingLayer) |
|
233 m_preloadResult = PreloadReferencedWhileLoading; |
|
234 else |
|
235 m_preloadResult = PreloadReferenced; |
|
236 } |
|
237 if (!hasClients() && inCache()) |
|
238 cache()->addToLiveResourcesSize(this); |
|
239 m_clients.add(client); |
|
240 } |
|
241 |
|
242 void CachedResource::removeClient(CachedResourceClient* client) |
|
243 { |
|
244 ASSERT(m_clients.contains(client)); |
|
245 m_clients.remove(client); |
|
246 |
|
247 if (canDelete() && !inCache()) |
|
248 delete this; |
|
249 else if (!hasClients() && inCache()) { |
|
250 cache()->removeFromLiveResourcesSize(this); |
|
251 cache()->removeFromLiveDecodedResourcesList(this); |
|
252 allClientsRemoved(); |
|
253 if (response().cacheControlContainsNoStore()) { |
|
254 // RFC2616 14.9.2: |
|
255 // "no-store: ...MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible" |
|
256 cache()->remove(this); |
|
257 } else |
|
258 cache()->prune(); |
|
259 } |
|
260 // This object may be dead here. |
|
261 } |
|
262 |
|
263 void CachedResource::deleteIfPossible() |
|
264 { |
|
265 if (canDelete() && !inCache()) |
|
266 delete this; |
|
267 } |
|
268 |
|
269 void CachedResource::setDecodedSize(unsigned size) |
|
270 { |
|
271 if (size == m_decodedSize) |
|
272 return; |
|
273 |
|
274 int delta = size - m_decodedSize; |
|
275 |
|
276 // The object must now be moved to a different queue, since its size has been changed. |
|
277 // We have to remove explicitly before updating m_decodedSize, so that we find the correct previous |
|
278 // queue. |
|
279 if (inCache()) |
|
280 cache()->removeFromLRUList(this); |
|
281 |
|
282 m_decodedSize = size; |
|
283 |
|
284 if (inCache()) { |
|
285 // Now insert into the new LRU list. |
|
286 cache()->insertInLRUList(this); |
|
287 |
|
288 // Insert into or remove from the live decoded list if necessary. |
|
289 // When inserting into the LiveDecodedResourcesList it is possible |
|
290 // that the m_lastDecodedAccessTime is still zero or smaller than |
|
291 // the m_lastDecodedAccessTime of the current list head. This is a |
|
292 // violation of the invariant that the list is to be kept sorted |
|
293 // by access time. The weakening of the invariant does not pose |
|
294 // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209 |
|
295 if (m_decodedSize && !m_inLiveDecodedResourcesList && hasClients()) |
|
296 cache()->insertInLiveDecodedResourcesList(this); |
|
297 else if (!m_decodedSize && m_inLiveDecodedResourcesList) |
|
298 cache()->removeFromLiveDecodedResourcesList(this); |
|
299 |
|
300 // Update the cache's size totals. |
|
301 cache()->adjustSize(hasClients(), delta); |
|
302 } |
|
303 } |
|
304 |
|
305 void CachedResource::setEncodedSize(unsigned size) |
|
306 { |
|
307 if (size == m_encodedSize) |
|
308 return; |
|
309 |
|
310 // The size cannot ever shrink (unless it is being nulled out because of an error). If it ever does, assert. |
|
311 ASSERT(size == 0 || size >= m_encodedSize); |
|
312 |
|
313 int delta = size - m_encodedSize; |
|
314 |
|
315 // The object must now be moved to a different queue, since its size has been changed. |
|
316 // We have to remove explicitly before updating m_encodedSize, so that we find the correct previous |
|
317 // queue. |
|
318 if (inCache()) |
|
319 cache()->removeFromLRUList(this); |
|
320 |
|
321 m_encodedSize = size; |
|
322 |
|
323 if (inCache()) { |
|
324 // Now insert into the new LRU list. |
|
325 cache()->insertInLRUList(this); |
|
326 |
|
327 // Update the cache's size totals. |
|
328 cache()->adjustSize(hasClients(), delta); |
|
329 } |
|
330 } |
|
331 |
|
332 void CachedResource::didAccessDecodedData(double timeStamp) |
|
333 { |
|
334 m_lastDecodedAccessTime = timeStamp; |
|
335 |
|
336 if (inCache()) { |
|
337 if (m_inLiveDecodedResourcesList) { |
|
338 cache()->removeFromLiveDecodedResourcesList(this); |
|
339 cache()->insertInLiveDecodedResourcesList(this); |
|
340 } |
|
341 cache()->prune(); |
|
342 } |
|
343 } |
|
344 |
|
345 void CachedResource::setResourceToRevalidate(CachedResource* resource) |
|
346 { |
|
347 ASSERT(resource); |
|
348 ASSERT(!m_resourceToRevalidate); |
|
349 ASSERT(resource != this); |
|
350 ASSERT(m_handlesToRevalidate.isEmpty()); |
|
351 ASSERT(resource->type() == type()); |
|
352 |
|
353 // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances. |
|
354 // https://bugs.webkit.org/show_bug.cgi?id=28604. |
|
355 // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in CachedResource::clearResourceToRevalidate. |
|
356 ASSERT(!resource->m_proxyResource); |
|
357 |
|
358 resource->m_proxyResource = this; |
|
359 m_resourceToRevalidate = resource; |
|
360 } |
|
361 |
|
362 void CachedResource::clearResourceToRevalidate() |
|
363 { |
|
364 ASSERT(m_resourceToRevalidate); |
|
365 // A resource may start revalidation before this method has been called, so check that this resource is still the proxy resource before clearing it out. |
|
366 if (m_resourceToRevalidate->m_proxyResource == this) { |
|
367 m_resourceToRevalidate->m_proxyResource = 0; |
|
368 m_resourceToRevalidate->deleteIfPossible(); |
|
369 } |
|
370 m_handlesToRevalidate.clear(); |
|
371 m_resourceToRevalidate = 0; |
|
372 deleteIfPossible(); |
|
373 } |
|
374 |
|
375 void CachedResource::switchClientsToRevalidatedResource() |
|
376 { |
|
377 ASSERT(m_resourceToRevalidate); |
|
378 ASSERT(m_resourceToRevalidate->inCache()); |
|
379 ASSERT(!inCache()); |
|
380 |
|
381 HashSet<CachedResourceHandleBase*>::iterator end = m_handlesToRevalidate.end(); |
|
382 for (HashSet<CachedResourceHandleBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) { |
|
383 CachedResourceHandleBase* handle = *it; |
|
384 handle->m_resource = m_resourceToRevalidate; |
|
385 m_resourceToRevalidate->registerHandle(handle); |
|
386 --m_handleCount; |
|
387 } |
|
388 ASSERT(!m_handleCount); |
|
389 m_handlesToRevalidate.clear(); |
|
390 |
|
391 Vector<CachedResourceClient*> clientsToMove; |
|
392 HashCountedSet<CachedResourceClient*>::iterator end2 = m_clients.end(); |
|
393 for (HashCountedSet<CachedResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) { |
|
394 CachedResourceClient* client = it->first; |
|
395 unsigned count = it->second; |
|
396 while (count) { |
|
397 clientsToMove.append(client); |
|
398 --count; |
|
399 } |
|
400 } |
|
401 // Equivalent of calling removeClient() for all clients |
|
402 m_clients.clear(); |
|
403 |
|
404 unsigned moveCount = clientsToMove.size(); |
|
405 for (unsigned n = 0; n < moveCount; ++n) |
|
406 m_resourceToRevalidate->addClientToSet(clientsToMove[n]); |
|
407 for (unsigned n = 0; n < moveCount; ++n) { |
|
408 // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore. |
|
409 if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n])) |
|
410 m_resourceToRevalidate->didAddClient(clientsToMove[n]); |
|
411 } |
|
412 } |
|
413 |
|
414 void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse) |
|
415 { |
|
416 m_responseTimestamp = currentTime(); |
|
417 |
|
418 DEFINE_STATIC_LOCAL(const AtomicString, contentHeaderPrefix, ("content-")); |
|
419 // RFC2616 10.3.5 |
|
420 // Update cached headers from the 304 response |
|
421 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields(); |
|
422 HTTPHeaderMap::const_iterator end = newHeaders.end(); |
|
423 for (HTTPHeaderMap::const_iterator it = newHeaders.begin(); it != end; ++it) { |
|
424 // Don't allow 304 response to update content headers, these can't change but some servers send wrong values. |
|
425 if (it->first.startsWith(contentHeaderPrefix, false)) |
|
426 continue; |
|
427 m_response.setHTTPHeaderField(it->first, it->second); |
|
428 } |
|
429 } |
|
430 |
|
431 bool CachedResource::canUseCacheValidator() const |
|
432 { |
|
433 if (m_loading || m_errorOccurred) |
|
434 return false; |
|
435 |
|
436 if (m_response.cacheControlContainsNoStore()) |
|
437 return false; |
|
438 |
|
439 DEFINE_STATIC_LOCAL(const AtomicString, lastModifiedHeader, ("last-modified")); |
|
440 DEFINE_STATIC_LOCAL(const AtomicString, eTagHeader, ("etag")); |
|
441 return !m_response.httpHeaderField(lastModifiedHeader).isEmpty() || !m_response.httpHeaderField(eTagHeader).isEmpty(); |
|
442 } |
|
443 |
|
444 bool CachedResource::mustRevalidate(CachePolicy cachePolicy) const |
|
445 { |
|
446 if (m_errorOccurred) |
|
447 return true; |
|
448 |
|
449 if (m_loading) |
|
450 return false; |
|
451 |
|
452 if (m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()) |
|
453 return true; |
|
454 |
|
455 if (cachePolicy == CachePolicyCache) |
|
456 return m_response.cacheControlContainsMustRevalidate() && isExpired(); |
|
457 |
|
458 return isExpired(); |
|
459 } |
|
460 |
|
461 bool CachedResource::isSafeToMakePurgeable() const |
|
462 { |
|
463 return !hasClients() && !m_proxyResource && !m_resourceToRevalidate; |
|
464 } |
|
465 |
|
466 bool CachedResource::makePurgeable(bool purgeable) |
|
467 { |
|
468 if (purgeable) { |
|
469 ASSERT(isSafeToMakePurgeable()); |
|
470 |
|
471 if (m_purgeableData) { |
|
472 ASSERT(!m_data); |
|
473 return true; |
|
474 } |
|
475 if (!m_data) |
|
476 return false; |
|
477 |
|
478 // Should not make buffer purgeable if it has refs other than this since we don't want two copies. |
|
479 if (!m_data->hasOneRef()) |
|
480 return false; |
|
481 |
|
482 // Purgeable buffers are allocated in multiples of the page size (4KB in common CPUs) so it does not make sense for very small buffers. |
|
483 const size_t purgeableThreshold = 4 * 4096; |
|
484 if (m_data->size() < purgeableThreshold) |
|
485 return false; |
|
486 |
|
487 if (m_data->hasPurgeableBuffer()) { |
|
488 m_purgeableData = m_data->releasePurgeableBuffer(); |
|
489 } else { |
|
490 m_purgeableData = PurgeableBuffer::create(m_data->data(), m_data->size()); |
|
491 if (!m_purgeableData) |
|
492 return false; |
|
493 } |
|
494 |
|
495 m_purgeableData->makePurgeable(true); |
|
496 m_data.clear(); |
|
497 return true; |
|
498 } |
|
499 |
|
500 if (!m_purgeableData) |
|
501 return true; |
|
502 ASSERT(!m_data); |
|
503 ASSERT(!hasClients()); |
|
504 |
|
505 if (!m_purgeableData->makePurgeable(false)) |
|
506 return false; |
|
507 |
|
508 m_data = SharedBuffer::adoptPurgeableBuffer(m_purgeableData.release()); |
|
509 return true; |
|
510 } |
|
511 |
|
512 bool CachedResource::isPurgeable() const |
|
513 { |
|
514 return m_purgeableData && m_purgeableData->isPurgeable(); |
|
515 } |
|
516 |
|
517 bool CachedResource::wasPurged() const |
|
518 { |
|
519 return m_purgeableData && m_purgeableData->wasPurged(); |
|
520 } |
|
521 |
|
522 unsigned CachedResource::overheadSize() const |
|
523 { |
|
524 return sizeof(CachedResource) + m_response.memoryUsage() + 576; |
|
525 /* |
|
526 576 = 192 + // average size of m_url |
|
527 384; // average size of m_clients hash map |
|
528 */ |
|
529 } |
|
530 |
|
531 } |