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, 2009, 2010, 2011 Apple Inc. All rights reserved.
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.
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.
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.
25
#include "CachedResource.h"
27
#include "MemoryCache.h"
28
#include "CachedMetadata.h"
29
#include "CachedResourceClient.h"
30
#include "CachedResourceClientWalker.h"
31
#include "CachedResourceHandle.h"
32
#include "CachedResourceLoader.h"
33
#include "CrossOriginAccessControl.h"
35
#include "DocumentLoader.h"
36
#include "FrameLoaderClient.h"
37
#include "InspectorInstrumentation.h"
39
#include "LoaderStrategy.h"
41
#include "PlatformStrategies.h"
42
#include "PurgeableBuffer.h"
43
#include "ResourceBuffer.h"
44
#include "ResourceHandle.h"
45
#include "ResourceLoadScheduler.h"
46
#include "SecurityOrigin.h"
47
#include "SecurityPolicy.h"
48
#include "SubresourceLoader.h"
49
#include "WebCoreMemoryInstrumentation.h"
50
#include <wtf/CurrentTime.h>
51
#include <wtf/MathExtras.h>
52
#include <wtf/MemoryInstrumentationHashCountedSet.h>
53
#include <wtf/MemoryInstrumentationHashSet.h>
54
#include <wtf/RefCountedLeakCounter.h>
55
#include <wtf/StdLibExtras.h>
56
#include <wtf/text/CString.h>
57
#include <wtf/Vector.h>
61
template<> struct SequenceMemoryInstrumentationTraits<WebCore::CachedResourceClient*> {
62
template <typename I> static void reportMemoryUsage(I, I, MemoryClassInfo&) { }
71
static ResourceLoadPriority defaultPriorityForResourceType(CachedResource::Type type)
74
case CachedResource::MainResource:
75
return ResourceLoadPriorityVeryHigh;
76
case CachedResource::CSSStyleSheet:
77
return ResourceLoadPriorityHigh;
78
case CachedResource::Script:
79
case CachedResource::FontResource:
80
case CachedResource::RawResource:
81
return ResourceLoadPriorityMedium;
82
case CachedResource::ImageResource:
83
return ResourceLoadPriorityLow;
85
case CachedResource::XSLStyleSheet:
86
return ResourceLoadPriorityHigh;
89
case CachedResource::SVGDocumentResource:
90
return ResourceLoadPriorityLow;
92
#if ENABLE(LINK_PREFETCH)
93
case CachedResource::LinkPrefetch:
94
return ResourceLoadPriorityVeryLow;
95
case CachedResource::LinkSubresource:
96
return ResourceLoadPriorityVeryLow;
98
#if ENABLE(VIDEO_TRACK)
99
case CachedResource::TextTrackResource:
100
return ResourceLoadPriorityLow;
102
#if ENABLE(CSS_SHADERS)
103
case CachedResource::ShaderResource:
104
return ResourceLoadPriorityMedium;
107
ASSERT_NOT_REACHED();
108
return ResourceLoadPriorityLow;
111
#if PLATFORM(CHROMIUM) || PLATFORM(BLACKBERRY)
112
static ResourceRequest::TargetType cachedResourceTypeToTargetType(CachedResource::Type type)
115
case CachedResource::MainResource:
116
return ResourceRequest::TargetIsMainFrame;
117
case CachedResource::CSSStyleSheet:
119
case CachedResource::XSLStyleSheet:
121
return ResourceRequest::TargetIsStyleSheet;
122
case CachedResource::Script:
123
return ResourceRequest::TargetIsScript;
124
case CachedResource::FontResource:
125
return ResourceRequest::TargetIsFontResource;
126
case CachedResource::ImageResource:
127
return ResourceRequest::TargetIsImage;
128
#if ENABLE(CSS_SHADERS)
129
case CachedResource::ShaderResource:
131
case CachedResource::RawResource:
132
return ResourceRequest::TargetIsSubresource;
133
#if ENABLE(LINK_PREFETCH)
134
case CachedResource::LinkPrefetch:
135
return ResourceRequest::TargetIsPrefetch;
136
case CachedResource::LinkSubresource:
137
return ResourceRequest::TargetIsSubresource;
139
#if ENABLE(VIDEO_TRACK)
140
case CachedResource::TextTrackResource:
141
return ResourceRequest::TargetIsTextTrack;
144
case CachedResource::SVGDocumentResource:
145
return ResourceRequest::TargetIsImage;
148
ASSERT_NOT_REACHED();
149
return ResourceRequest::TargetIsSubresource;
153
DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("CachedResource"));
155
CachedResource::CachedResource(const ResourceRequest& request, Type type)
156
: m_resourceRequest(request)
157
, m_loadPriority(defaultPriorityForResourceType(type))
158
, m_responseTimestamp(currentTime())
159
, m_decodedDataDeletionTimer(this, &CachedResource::decodedDataDeletionTimerFired)
160
, m_lastDecodedAccessTime(0)
161
, m_loadFinishTime(0)
167
, m_preloadResult(PreloadNotReferenced)
168
, m_inLiveDecodedResourcesList(false)
169
, m_requestedFromNetworkingLayer(false)
172
, m_switchingClientsToRevalidatedResource(false)
179
, m_nextInAllResourcesList(0)
180
, m_prevInAllResourcesList(0)
181
, m_nextInLiveResourcesList(0)
182
, m_prevInLiveResourcesList(0)
183
, m_owningCachedResourceLoader(0)
184
, m_resourceToRevalidate(0)
187
ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
189
cachedResourceLeakCounter.increment();
193
CachedResource::~CachedResource()
195
ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
199
ASSERT(url().isNull() || memoryCache()->resourceForURL(KURL(ParsedURLString, url())) != this);
203
cachedResourceLeakCounter.decrement();
206
if (m_owningCachedResourceLoader)
207
m_owningCachedResourceLoader->removeCachedResource(this);
210
void CachedResource::failBeforeStarting()
212
// FIXME: What if resources in other frames were waiting for this revalidation?
213
LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
214
if (m_resourceToRevalidate)
215
memoryCache()->revalidationFailed(this);
216
error(CachedResource::LoadError);
219
void CachedResource::addAdditionalRequestHeaders(CachedResourceLoader* cachedResourceLoader)
221
// Note: We skip the Content-Security-Policy check here because we check
222
// the Content-Security-Policy at the CachedResourceLoader layer so we can
223
// handle different resource types differently.
225
FrameLoader* frameLoader = cachedResourceLoader->frame()->loader();
226
String outgoingReferrer;
227
String outgoingOrigin;
228
if (m_resourceRequest.httpReferrer().isNull()) {
229
outgoingReferrer = frameLoader->outgoingReferrer();
230
outgoingOrigin = frameLoader->outgoingOrigin();
232
outgoingReferrer = m_resourceRequest.httpReferrer();
233
outgoingOrigin = SecurityOrigin::createFromString(outgoingReferrer)->toString();
236
outgoingReferrer = SecurityPolicy::generateReferrerHeader(cachedResourceLoader->document()->referrerPolicy(), m_resourceRequest.url(), outgoingReferrer);
237
if (outgoingReferrer.isEmpty())
238
m_resourceRequest.clearHTTPReferrer();
239
else if (!m_resourceRequest.httpReferrer())
240
m_resourceRequest.setHTTPReferrer(outgoingReferrer);
241
FrameLoader::addHTTPOriginIfNeeded(m_resourceRequest, outgoingOrigin);
243
frameLoader->addExtraFieldsToSubresourceRequest(m_resourceRequest);
246
void CachedResource::load(CachedResourceLoader* cachedResourceLoader, const ResourceLoaderOptions& options)
248
if (!cachedResourceLoader->frame()) {
249
failBeforeStarting();
253
FrameLoader* frameLoader = cachedResourceLoader->frame()->loader();
254
if (options.securityCheck == DoSecurityCheck && (frameLoader->state() == FrameStateProvisional || !frameLoader->activeDocumentLoader() || frameLoader->activeDocumentLoader()->isStopping())) {
255
failBeforeStarting();
262
#if PLATFORM(CHROMIUM) || PLATFORM(BLACKBERRY)
263
if (m_resourceRequest.targetType() == ResourceRequest::TargetIsUnspecified)
264
m_resourceRequest.setTargetType(cachedResourceTypeToTargetType(type()));
267
if (!accept().isEmpty())
268
m_resourceRequest.setHTTPAccept(accept());
270
if (isCacheValidator()) {
271
CachedResource* resourceToRevalidate = m_resourceToRevalidate;
272
ASSERT(resourceToRevalidate->canUseCacheValidator());
273
ASSERT(resourceToRevalidate->isLoaded());
274
const String& lastModified = resourceToRevalidate->response().httpHeaderField("Last-Modified");
275
const String& eTag = resourceToRevalidate->response().httpHeaderField("ETag");
276
if (!lastModified.isEmpty() || !eTag.isEmpty()) {
277
ASSERT(cachedResourceLoader->cachePolicy() != CachePolicyReload);
278
if (cachedResourceLoader->cachePolicy() == CachePolicyRevalidate)
279
m_resourceRequest.setHTTPHeaderField("Cache-Control", "max-age=0");
280
if (!lastModified.isEmpty())
281
m_resourceRequest.setHTTPHeaderField("If-Modified-Since", lastModified);
283
m_resourceRequest.setHTTPHeaderField("If-None-Match", eTag);
287
#if ENABLE(LINK_PREFETCH)
288
if (type() == CachedResource::LinkPrefetch || type() == CachedResource::LinkSubresource)
289
m_resourceRequest.setHTTPHeaderField("Purpose", "prefetch");
291
m_resourceRequest.setPriority(loadPriority());
293
if (type() != MainResource)
294
addAdditionalRequestHeaders(cachedResourceLoader);
296
#if USE(PLATFORM_STRATEGIES)
297
m_loader = platformStrategies()->loaderStrategy()->resourceLoadScheduler()->scheduleSubresourceLoad(cachedResourceLoader->frame(), this, m_resourceRequest, m_resourceRequest.priority(), options);
299
m_loader = resourceLoadScheduler()->scheduleSubresourceLoad(cachedResourceLoader->frame(), this, m_resourceRequest, m_resourceRequest.priority(), options);
303
failBeforeStarting();
310
void CachedResource::checkNotify()
315
CachedResourceClientWalker<CachedResourceClient> w(m_clients);
316
while (CachedResourceClient* c = w.next())
317
c->notifyFinished(this);
320
void CachedResource::data(PassRefPtr<ResourceBuffer>, bool allDataReceived)
322
if (!allDataReceived)
329
void CachedResource::error(CachedResource::Status status)
332
ASSERT(errorOccurred());
339
void CachedResource::finish()
341
if (!errorOccurred())
345
bool CachedResource::passesAccessControlCheck(SecurityOrigin* securityOrigin)
347
String errorDescription;
348
return WebCore::passesAccessControlCheck(m_response, resourceRequest().allowCookies() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription);
351
bool CachedResource::isExpired() const
353
if (m_response.isNull())
356
return currentAge() > freshnessLifetime();
359
double CachedResource::currentAge() const
362
// No compensation for latency as that is not terribly important in practice
363
double dateValue = m_response.date();
364
double apparentAge = isfinite(dateValue) ? std::max(0., m_responseTimestamp - dateValue) : 0;
365
double ageValue = m_response.age();
366
double correctedReceivedAge = isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
367
double residentTime = currentTime() - m_responseTimestamp;
368
return correctedReceivedAge + residentTime;
371
double CachedResource::freshnessLifetime() const
373
// Cache non-http resources liberally
374
if (!m_response.url().protocolIsInHTTPFamily())
375
return std::numeric_limits<double>::max();
378
double maxAgeValue = m_response.cacheControlMaxAge();
379
if (isfinite(maxAgeValue))
381
double expiresValue = m_response.expires();
382
double dateValue = m_response.date();
383
double creationTime = isfinite(dateValue) ? dateValue : m_responseTimestamp;
384
if (isfinite(expiresValue))
385
return expiresValue - creationTime;
386
double lastModifiedValue = m_response.lastModified();
387
if (isfinite(lastModifiedValue))
388
return (creationTime - lastModifiedValue) * 0.1;
389
// If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
393
void CachedResource::setResponse(const ResourceResponse& response)
395
m_response = response;
396
m_responseTimestamp = currentTime();
397
String encoding = response.textEncodingName();
398
if (!encoding.isNull())
399
setEncoding(encoding);
402
void CachedResource::setSerializedCachedMetadata(const char* data, size_t size)
404
// We only expect to receive cached metadata from the platform once.
405
// If this triggers, it indicates an efficiency problem which is most
406
// likely unexpected in code designed to improve performance.
407
ASSERT(!m_cachedMetadata);
409
m_cachedMetadata = CachedMetadata::deserialize(data, size);
412
void CachedResource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size)
414
// Currently, only one type of cached metadata per resource is supported.
415
// If the need arises for multiple types of metadata per resource this could
416
// be enhanced to store types of metadata in a map.
417
ASSERT(!m_cachedMetadata);
419
m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
420
ResourceHandle::cacheMetadata(m_response, m_cachedMetadata->serialize());
423
CachedMetadata* CachedResource::cachedMetadata(unsigned dataTypeID) const
425
if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
427
return m_cachedMetadata.get();
430
void CachedResource::stopLoading()
435
CachedResourceHandle<CachedResource> protect(this);
437
// All loads finish with data(allDataReceived = true) or error(), except for
438
// canceled loads, which silently set our request to 0. Be sure to notify our
439
// client in that case, so we don't seem to continue loading forever.
441
ASSERT(!m_error.isNull());
443
setStatus(LoadError);
448
void CachedResource::addClient(CachedResourceClient* client)
450
if (addClientToSet(client))
451
didAddClient(client);
454
void CachedResource::didAddClient(CachedResourceClient* c)
456
if (m_decodedDataDeletionTimer.isActive())
457
m_decodedDataDeletionTimer.stop();
459
if (m_clientsAwaitingCallback.contains(c)) {
461
m_clientsAwaitingCallback.remove(c);
463
if (!isLoading() && !stillNeedsLoad())
464
c->notifyFinished(this);
467
bool CachedResource::addClientToSet(CachedResourceClient* client)
469
ASSERT(!isPurgeable());
471
if (m_preloadResult == PreloadNotReferenced) {
473
m_preloadResult = PreloadReferencedWhileComplete;
474
else if (m_requestedFromNetworkingLayer)
475
m_preloadResult = PreloadReferencedWhileLoading;
477
m_preloadResult = PreloadReferenced;
479
if (!hasClients() && inCache())
480
memoryCache()->addToLiveResourcesSize(this);
482
if (m_type == RawResource && !m_response.isNull() && !m_proxyResource) {
483
// Certain resources (especially XHRs) do crazy things if an asynchronous load returns
484
// synchronously (e.g., scripts may not have set all the state they need to handle the load).
485
// Therefore, rather than immediately sending callbacks on a cache hit like other CachedResources,
486
// we schedule the callbacks and ensure we never finish synchronously.
487
ASSERT(!m_clientsAwaitingCallback.contains(client));
488
m_clientsAwaitingCallback.add(client, CachedResourceCallback::schedule(this, client));
492
m_clients.add(client);
496
void CachedResource::removeClient(CachedResourceClient* client)
498
OwnPtr<CachedResourceCallback> callback = m_clientsAwaitingCallback.take(client);
500
ASSERT(!m_clients.contains(client));
504
ASSERT(m_clients.contains(client));
505
m_clients.remove(client);
506
didRemoveClient(client);
509
bool deleted = deleteIfPossible();
510
if (!deleted && !hasClients() && inCache()) {
511
memoryCache()->removeFromLiveResourcesSize(this);
512
memoryCache()->removeFromLiveDecodedResourcesList(this);
514
destroyDecodedDataIfNeeded();
515
if (response().cacheControlContainsNoStore()) {
517
// "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
518
// "... History buffers MAY store such responses as part of their normal operation."
519
// We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
520
if (url().protocolIs("https"))
521
memoryCache()->remove(this);
523
memoryCache()->prune();
525
// This object may be dead here.
528
void CachedResource::destroyDecodedDataIfNeeded()
533
if (double interval = memoryCache()->deadDecodedDataDeletionInterval())
534
m_decodedDataDeletionTimer.startOneShot(interval);
537
void CachedResource::decodedDataDeletionTimerFired(Timer<CachedResource>*)
539
destroyDecodedData();
542
bool CachedResource::deleteIfPossible()
544
if (canDelete() && !inCache()) {
545
InspectorInstrumentation::willDestroyCachedResource(this);
552
void CachedResource::setDecodedSize(unsigned size)
554
if (size == m_decodedSize)
557
int delta = size - m_decodedSize;
559
// The object must now be moved to a different queue, since its size has been changed.
560
// We have to remove explicitly before updating m_decodedSize, so that we find the correct previous
563
memoryCache()->removeFromLRUList(this);
565
m_decodedSize = size;
568
// Now insert into the new LRU list.
569
memoryCache()->insertInLRUList(this);
571
// Insert into or remove from the live decoded list if necessary.
572
// When inserting into the LiveDecodedResourcesList it is possible
573
// that the m_lastDecodedAccessTime is still zero or smaller than
574
// the m_lastDecodedAccessTime of the current list head. This is a
575
// violation of the invariant that the list is to be kept sorted
576
// by access time. The weakening of the invariant does not pose
577
// a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
578
if (m_decodedSize && !m_inLiveDecodedResourcesList && hasClients())
579
memoryCache()->insertInLiveDecodedResourcesList(this);
580
else if (!m_decodedSize && m_inLiveDecodedResourcesList)
581
memoryCache()->removeFromLiveDecodedResourcesList(this);
583
// Update the cache's size totals.
584
memoryCache()->adjustSize(hasClients(), delta);
588
void CachedResource::setEncodedSize(unsigned size)
590
if (size == m_encodedSize)
593
// The size cannot ever shrink (unless it is being nulled out because of an error). If it ever does, assert.
594
ASSERT(size == 0 || size >= m_encodedSize);
596
int delta = size - m_encodedSize;
598
// The object must now be moved to a different queue, since its size has been changed.
599
// We have to remove explicitly before updating m_encodedSize, so that we find the correct previous
602
memoryCache()->removeFromLRUList(this);
604
m_encodedSize = size;
607
// Now insert into the new LRU list.
608
memoryCache()->insertInLRUList(this);
610
// Update the cache's size totals.
611
memoryCache()->adjustSize(hasClients(), delta);
615
void CachedResource::didAccessDecodedData(double timeStamp)
617
m_lastDecodedAccessTime = timeStamp;
620
if (m_inLiveDecodedResourcesList) {
621
memoryCache()->removeFromLiveDecodedResourcesList(this);
622
memoryCache()->insertInLiveDecodedResourcesList(this);
624
memoryCache()->prune();
628
void CachedResource::setResourceToRevalidate(CachedResource* resource)
631
ASSERT(!m_resourceToRevalidate);
632
ASSERT(resource != this);
633
ASSERT(m_handlesToRevalidate.isEmpty());
634
ASSERT(resource->type() == type());
636
LOG(ResourceLoading, "CachedResource %p setResourceToRevalidate %p", this, resource);
638
// The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
639
// https://bugs.webkit.org/show_bug.cgi?id=28604.
640
// So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in CachedResource::clearResourceToRevalidate.
641
ASSERT(!resource->m_proxyResource);
643
resource->m_proxyResource = this;
644
m_resourceToRevalidate = resource;
647
void CachedResource::clearResourceToRevalidate()
649
ASSERT(m_resourceToRevalidate);
650
if (m_switchingClientsToRevalidatedResource)
653
// 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.
654
if (m_resourceToRevalidate->m_proxyResource == this) {
655
m_resourceToRevalidate->m_proxyResource = 0;
656
m_resourceToRevalidate->deleteIfPossible();
658
m_handlesToRevalidate.clear();
659
m_resourceToRevalidate = 0;
663
void CachedResource::switchClientsToRevalidatedResource()
665
ASSERT(m_resourceToRevalidate);
666
ASSERT(m_resourceToRevalidate->inCache());
669
LOG(ResourceLoading, "CachedResource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
671
m_switchingClientsToRevalidatedResource = true;
672
HashSet<CachedResourceHandleBase*>::iterator end = m_handlesToRevalidate.end();
673
for (HashSet<CachedResourceHandleBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
674
CachedResourceHandleBase* handle = *it;
675
handle->m_resource = m_resourceToRevalidate;
676
m_resourceToRevalidate->registerHandle(handle);
679
ASSERT(!m_handleCount);
680
m_handlesToRevalidate.clear();
682
Vector<CachedResourceClient*> clientsToMove;
683
HashCountedSet<CachedResourceClient*>::iterator end2 = m_clients.end();
684
for (HashCountedSet<CachedResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
685
CachedResourceClient* client = it->key;
686
unsigned count = it->value;
688
clientsToMove.append(client);
692
// Equivalent of calling removeClient() for all clients
695
unsigned moveCount = clientsToMove.size();
696
for (unsigned n = 0; n < moveCount; ++n)
697
m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
698
for (unsigned n = 0; n < moveCount; ++n) {
699
// Calling didAddClient may do anything, including trying to cancel revalidation.
700
// Assert that it didn't succeed.
701
ASSERT(m_resourceToRevalidate);
702
// Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
703
if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
704
m_resourceToRevalidate->didAddClient(clientsToMove[n]);
706
m_switchingClientsToRevalidatedResource = false;
709
void CachedResource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
711
m_responseTimestamp = currentTime();
713
DEFINE_STATIC_LOCAL(const AtomicString, contentHeaderPrefix, ("content-", AtomicString::ConstructFromLiteral));
715
// Update cached headers from the 304 response
716
const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
717
HTTPHeaderMap::const_iterator end = newHeaders.end();
718
for (HTTPHeaderMap::const_iterator it = newHeaders.begin(); it != end; ++it) {
719
// Don't allow 304 response to update content headers, these can't change but some servers send wrong values.
720
if (it->key.startsWith(contentHeaderPrefix, false))
722
m_response.setHTTPHeaderField(it->key, it->value);
726
void CachedResource::registerHandle(CachedResourceHandleBase* h)
729
if (m_resourceToRevalidate)
730
m_handlesToRevalidate.add(h);
733
void CachedResource::unregisterHandle(CachedResourceHandleBase* h)
735
ASSERT(m_handleCount > 0);
738
if (m_resourceToRevalidate)
739
m_handlesToRevalidate.remove(h);
745
bool CachedResource::canUseCacheValidator() const
747
if (m_loading || errorOccurred())
750
if (m_response.cacheControlContainsNoStore())
752
return m_response.hasCacheValidatorFields();
755
bool CachedResource::mustRevalidateDueToCacheHeaders(CachePolicy cachePolicy) const
757
ASSERT(cachePolicy == CachePolicyRevalidate || cachePolicy == CachePolicyCache || cachePolicy == CachePolicyVerify);
759
if (cachePolicy == CachePolicyRevalidate)
762
if (m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()) {
763
LOG(ResourceLoading, "CachedResource %p mustRevalidate because of m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()\n", this);
767
if (cachePolicy == CachePolicyCache) {
768
if (m_response.cacheControlContainsMustRevalidate() && isExpired()) {
769
LOG(ResourceLoading, "CachedResource %p mustRevalidate because of cachePolicy == CachePolicyCache and m_response.cacheControlContainsMustRevalidate() && isExpired()\n", this);
777
LOG(ResourceLoading, "CachedResource %p mustRevalidate because of isExpired()\n", this);
784
bool CachedResource::isSafeToMakePurgeable() const
786
return !hasClients() && !m_proxyResource && !m_resourceToRevalidate;
789
bool CachedResource::makePurgeable(bool purgeable)
792
ASSERT(isSafeToMakePurgeable());
794
if (m_purgeableData) {
801
// Should not make buffer purgeable if it has refs other than this since we don't want two copies.
802
if (!m_data->hasOneRef())
805
m_data->createPurgeableBuffer();
806
if (!m_data->hasPurgeableBuffer())
809
m_purgeableData = m_data->releasePurgeableBuffer();
810
m_purgeableData->setPurgePriority(purgePriority());
811
m_purgeableData->makePurgeable(true);
816
if (!m_purgeableData)
819
ASSERT(!hasClients());
821
if (!m_purgeableData->makePurgeable(false))
824
m_data = ResourceBuffer::adoptSharedBuffer(SharedBuffer::adoptPurgeableBuffer(m_purgeableData.release()));
828
bool CachedResource::isPurgeable() const
830
return m_purgeableData && m_purgeableData->isPurgeable();
833
bool CachedResource::wasPurged() const
835
return m_purgeableData && m_purgeableData->wasPurged();
838
unsigned CachedResource::overheadSize() const
840
static const int kAverageClientsHashMapSize = 384;
841
return sizeof(CachedResource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
844
void CachedResource::setLoadPriority(ResourceLoadPriority loadPriority)
846
if (loadPriority == ResourceLoadPriorityUnresolved)
848
m_loadPriority = loadPriority;
852
CachedResource::CachedResourceCallback::CachedResourceCallback(CachedResource* resource, CachedResourceClient* client)
853
: m_resource(resource)
855
, m_callbackTimer(this, &CachedResourceCallback::timerFired)
857
m_callbackTimer.startOneShot(0);
860
void CachedResource::CachedResourceCallback::cancel()
862
if (m_callbackTimer.isActive())
863
m_callbackTimer.stop();
866
void CachedResource::CachedResourceCallback::timerFired(Timer<CachedResourceCallback>*)
868
m_resource->didAddClient(m_client);
871
void CachedResource::reportMemoryUsage(MemoryObjectInfo* memoryObjectInfo) const
873
MemoryClassInfo info(memoryObjectInfo, this, WebCoreMemoryTypes::CachedResource);
874
info.addMember(m_resourceRequest);
875
info.addMember(m_clients);
876
info.addMember(m_accept);
877
info.addMember(m_loader);
878
info.addMember(m_response);
879
info.addMember(m_data);
880
info.addMember(m_cachedMetadata);
881
info.addMember(m_nextInAllResourcesList);
882
info.addMember(m_prevInAllResourcesList);
883
info.addMember(m_nextInLiveResourcesList);
884
info.addMember(m_prevInLiveResourcesList);
885
info.addMember(m_owningCachedResourceLoader);
886
info.addMember(m_resourceToRevalidate);
887
info.addMember(m_proxyResource);
888
info.addMember(m_handlesToRevalidate);
890
if (m_purgeableData && !m_purgeableData->wasPurged())
891
info.addRawBuffer(m_purgeableData.get(), m_purgeableData->size());