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 "core/fetch/Resource.h"
27 #include "core/fetch/CachedMetadata.h"
28 #include "core/fetch/CrossOriginAccessControl.h"
29 #include "core/fetch/MemoryCache.h"
30 #include "core/fetch/ResourceClient.h"
31 #include "core/fetch/ResourceClientWalker.h"
32 #include "core/fetch/ResourceFetcher.h"
33 #include "core/fetch/ResourceLoader.h"
34 #include "core/fetch/ResourcePtr.h"
35 #include "core/inspector/InspectorInstrumentation.h"
36 #include "platform/Logging.h"
37 #include "platform/PurgeableBuffer.h"
38 #include "platform/SharedBuffer.h"
39 #include "public/platform/Platform.h"
40 #include "weborigin/KURL.h"
41 #include "wtf/CurrentTime.h"
42 #include "wtf/MathExtras.h"
43 #include "wtf/RefCountedLeakCounter.h"
44 #include "wtf/StdLibExtras.h"
45 #include "wtf/Vector.h"
46 #include "wtf/text/CString.h"
52 // These response headers are not copied from a revalidated response to the
53 // cached response headers. For compatibility, this list is based on Chromium's
54 // net/http/http_response_headers.cc.
55 const char* const headersToIgnoreAfterRevalidation[] = {
72 // Some header prefixes mean "Don't copy this header from a 304 response.".
73 // Rather than listing all the relevant headers, we can consolidate them into
74 // this list, also grabbed from Chromium's net/http/http_response_headers.cc.
75 const char* const headerPrefixesToIgnoreAfterRevalidation[] = {
81 static inline bool shouldUpdateHeaderAfterRevalidation(const AtomicString& header)
83 for (size_t i = 0; i < WTF_ARRAY_LENGTH(headersToIgnoreAfterRevalidation); i++) {
84 if (equalIgnoringCase(header, headersToIgnoreAfterRevalidation[i]))
87 for (size_t i = 0; i < WTF_ARRAY_LENGTH(headerPrefixesToIgnoreAfterRevalidation); i++) {
88 if (header.startsWith(headerPrefixesToIgnoreAfterRevalidation[i], false))
94 DEFINE_DEBUG_ONLY_GLOBAL(RefCountedLeakCounter, cachedResourceLeakCounter, ("Resource"));
96 Resource::Resource(const ResourceRequest& request, Type type)
97 : m_resourceRequest(request)
98 , m_responseTimestamp(currentTime())
99 , m_cancelTimer(this, &Resource::cancelTimerFired)
100 , m_lastDecodedAccessTime(0)
101 , m_loadFinishTime(0)
108 , m_preloadResult(PreloadNotReferenced)
109 , m_cacheLiveResourcePriority(CacheLiveResourcePriorityLow)
110 , m_inLiveDecodedResourcesList(false)
111 , m_requestedFromNetworkingLayer(false)
114 , m_switchingClientsToRevalidatedResource(false)
121 , m_nextInAllResourcesList(0)
122 , m_prevInAllResourcesList(0)
123 , m_nextInLiveResourcesList(0)
124 , m_prevInLiveResourcesList(0)
125 , m_resourceToRevalidate(0)
128 ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
130 cachedResourceLeakCounter.increment();
133 if (!m_resourceRequest.url().hasFragmentIdentifier())
135 KURL urlForCache = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
136 if (urlForCache.hasFragmentIdentifier())
138 m_fragmentIdentifierForRequest = m_resourceRequest.url().fragmentIdentifier();
139 m_resourceRequest.setURL(urlForCache);
142 Resource::~Resource()
144 ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
148 ASSERT(url().isNull() || memoryCache()->resourceForURL(KURL(ParsedURLString, url())) != this);
152 cachedResourceLeakCounter.decrement();
156 void Resource::failBeforeStarting()
158 LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
159 error(Resource::LoadError);
162 void Resource::load(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
164 if (!fetcher->frame()) {
165 failBeforeStarting();
172 if (!accept().isEmpty())
173 m_resourceRequest.setHTTPAccept(accept());
175 // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
176 // We should look into removing the expectation of that knowledge from the platform network stacks.
177 ResourceRequest request(m_resourceRequest);
178 if (!m_fragmentIdentifierForRequest.isNull()) {
179 KURL url = request.url();
180 url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
182 m_fragmentIdentifierForRequest = String();
186 RELEASE_ASSERT(m_options.synchronousPolicy == RequestSynchronously);
187 m_loader->changeToSynchronous();
190 m_loader = ResourceLoader::create(fetcher, this, request, options);
194 void Resource::checkNotify()
199 ResourceClientWalker<ResourceClient> w(m_clients);
200 while (ResourceClient* c = w.next())
201 c->notifyFinished(this);
204 void Resource::appendData(const char* data, int length)
206 TRACE_EVENT0("webkit", "Resource::appendData");
207 ASSERT(!m_resourceToRevalidate);
208 ASSERT(!errorOccurred());
209 if (m_options.dataBufferingPolicy == DoNotBufferData)
212 m_data->append(data, length);
214 m_data = SharedBuffer::create(data, length);
215 setEncodedSize(m_data->size());
218 void Resource::setResourceBuffer(PassRefPtr<SharedBuffer> resourceBuffer)
220 ASSERT(!m_resourceToRevalidate);
221 ASSERT(!errorOccurred());
222 ASSERT(m_options.dataBufferingPolicy == BufferData);
223 m_data = resourceBuffer;
224 setEncodedSize(m_data->size());
227 void Resource::error(Resource::Status status)
229 if (m_resourceToRevalidate)
230 revalidationFailed();
232 if (!m_error.isNull() && (m_error.isCancellation() || !isPreloaded()))
233 memoryCache()->remove(this);
236 ASSERT(errorOccurred());
243 void Resource::finishOnePart()
249 void Resource::finish(double finishTime)
251 ASSERT(!m_resourceToRevalidate);
252 ASSERT(!errorOccurred());
253 m_loadFinishTime = finishTime;
255 if (!errorOccurred())
259 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin)
261 String ignoredErrorDescription;
262 return passesAccessControlCheck(securityOrigin, ignoredErrorDescription);
265 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin, String& errorDescription)
267 return WebCore::passesAccessControlCheck(m_response, resourceRequest().allowCookies() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription);
270 bool Resource::isExpired() const
272 if (m_response.isNull())
275 return currentAge() > freshnessLifetime();
278 double Resource::currentAge() const
281 // No compensation for latency as that is not terribly important in practice
282 double dateValue = m_response.date();
283 double apparentAge = std::isfinite(dateValue) ? std::max(0., m_responseTimestamp - dateValue) : 0;
284 double ageValue = m_response.age();
285 double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
286 double residentTime = currentTime() - m_responseTimestamp;
287 return correctedReceivedAge + residentTime;
290 double Resource::freshnessLifetime() const
293 // On desktop, local files should be reloaded in case they change.
294 if (m_response.url().isLocalFile())
298 // Cache other non-http resources liberally.
299 if (!m_response.url().protocolIsInHTTPFamily())
300 return std::numeric_limits<double>::max();
303 double maxAgeValue = m_response.cacheControlMaxAge();
304 if (std::isfinite(maxAgeValue))
306 double expiresValue = m_response.expires();
307 double dateValue = m_response.date();
308 double creationTime = std::isfinite(dateValue) ? dateValue : m_responseTimestamp;
309 if (std::isfinite(expiresValue))
310 return expiresValue - creationTime;
311 double lastModifiedValue = m_response.lastModified();
312 if (std::isfinite(lastModifiedValue))
313 return (creationTime - lastModifiedValue) * 0.1;
314 // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
318 void Resource::responseReceived(const ResourceResponse& response)
320 setResponse(response);
321 m_responseTimestamp = currentTime();
322 String encoding = response.textEncodingName();
323 if (!encoding.isNull())
324 setEncoding(encoding);
326 if (!m_resourceToRevalidate)
328 if (response.httpStatusCode() == 304)
329 revalidationSucceeded(response);
331 revalidationFailed();
334 void Resource::setSerializedCachedMetadata(const char* data, size_t size)
336 // We only expect to receive cached metadata from the platform once.
337 // If this triggers, it indicates an efficiency problem which is most
338 // likely unexpected in code designed to improve performance.
339 ASSERT(!m_cachedMetadata);
340 ASSERT(!m_resourceToRevalidate);
342 m_cachedMetadata = CachedMetadata::deserialize(data, size);
345 void Resource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size)
347 // Currently, only one type of cached metadata per resource is supported.
348 // If the need arises for multiple types of metadata per resource this could
349 // be enhanced to store types of metadata in a map.
350 ASSERT(!m_cachedMetadata);
352 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
353 const Vector<char>& serializedData = m_cachedMetadata->serialize();
354 WebKit::Platform::current()->cacheMetadata(m_response.url(), m_response.responseTime(), serializedData.data(), serializedData.size());
357 CachedMetadata* Resource::cachedMetadata(unsigned dataTypeID) const
359 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
361 return m_cachedMetadata.get();
364 void Resource::setCacheLiveResourcePriority(CacheLiveResourcePriority priority)
366 if (inCache() && m_inLiveDecodedResourcesList && m_cacheLiveResourcePriority != priority) {
367 memoryCache()->removeFromLiveDecodedResourcesList(this);
368 m_cacheLiveResourcePriority = priority;
369 memoryCache()->insertInLiveDecodedResourcesList(this);
370 memoryCache()->prune();
374 void Resource::clearLoader()
379 void Resource::addClient(ResourceClient* client)
381 if (addClientToSet(client))
382 didAddClient(client);
385 void Resource::didAddClient(ResourceClient* c)
387 if (!isLoading() && !stillNeedsLoad())
388 c->notifyFinished(this);
391 static bool shouldSendCachedDataSynchronouslyForType(Resource::Type type)
393 // Some resources types default to return data synchronously.
394 // For most of these, it's because there are layout tests that
395 // expect data to return synchronously in case of cache hit. In
396 // the case of fonts, there was a performance regression.
397 // FIXME: Get to the point where we don't need to special-case sync/async
398 // behavior for different resource types.
399 if (type == Resource::Image)
401 if (type == Resource::CSSStyleSheet)
403 if (type == Resource::Script)
405 if (type == Resource::Font)
410 bool Resource::addClientToSet(ResourceClient* client)
412 ASSERT(!isPurgeable());
414 if (m_preloadResult == PreloadNotReferenced) {
416 m_preloadResult = PreloadReferencedWhileComplete;
417 else if (m_requestedFromNetworkingLayer)
418 m_preloadResult = PreloadReferencedWhileLoading;
420 m_preloadResult = PreloadReferenced;
422 if (!hasClients() && inCache())
423 memoryCache()->addToLiveResourcesSize(this);
425 // If we have existing data to send to the new client and the resource type supprts it, send it asynchronously.
426 if (!m_response.isNull() && !m_proxyResource && !shouldSendCachedDataSynchronouslyForType(type())) {
427 m_clientsAwaitingCallback.add(client);
428 ResourceCallback::callbackHandler()->schedule(this);
432 m_clients.add(client);
436 void Resource::removeClient(ResourceClient* client)
438 if (m_clientsAwaitingCallback.contains(client)) {
439 ASSERT(!m_clients.contains(client));
440 m_clientsAwaitingCallback.remove(client);
441 if (m_clientsAwaitingCallback.isEmpty())
442 ResourceCallback::callbackHandler()->cancel(this);
444 ASSERT(m_clients.contains(client));
445 m_clients.remove(client);
446 didRemoveClient(client);
449 bool deleted = deleteIfPossible();
450 if (!deleted && !hasClients()) {
452 memoryCache()->removeFromLiveResourcesSize(this);
453 memoryCache()->removeFromLiveDecodedResourcesList(this);
455 if (!m_switchingClientsToRevalidatedResource)
457 if (response().cacheControlContainsNoStore()) {
459 // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
460 // "... History buffers MAY store such responses as part of their normal operation."
461 // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
462 if (url().protocolIs("https"))
463 memoryCache()->remove(this);
465 memoryCache()->prune(this);
468 // This object may be dead here.
471 void Resource::allClientsRemoved()
475 if (m_type == MainResource || m_type == Raw)
476 cancelTimerFired(&m_cancelTimer);
477 else if (!m_cancelTimer.isActive())
478 m_cancelTimer.startOneShot(0);
481 void Resource::cancelTimerFired(Timer<Resource>* timer)
483 ASSERT_UNUSED(timer, timer == &m_cancelTimer);
484 if (hasClients() || !m_loader)
486 ResourcePtr<Resource> protect(this);
487 m_loader->cancelIfNotFinishing();
488 if (m_status != Cached)
489 memoryCache()->remove(this);
492 bool Resource::deleteIfPossible()
494 if (canDelete() && !inCache()) {
495 InspectorInstrumentation::willDestroyResource(this);
502 void Resource::setDecodedSize(unsigned size)
504 if (size == m_decodedSize)
507 int delta = size - m_decodedSize;
509 // The object must now be moved to a different queue, since its size has been changed.
510 // We have to remove explicitly before updating m_decodedSize, so that we find the correct previous
513 memoryCache()->removeFromLRUList(this);
515 m_decodedSize = size;
518 // Now insert into the new LRU list.
519 memoryCache()->insertInLRUList(this);
521 // Insert into or remove from the live decoded list if necessary.
522 // When inserting into the LiveDecodedResourcesList it is possible
523 // that the m_lastDecodedAccessTime is still zero or smaller than
524 // the m_lastDecodedAccessTime of the current list head. This is a
525 // violation of the invariant that the list is to be kept sorted
526 // by access time. The weakening of the invariant does not pose
527 // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
528 if (m_decodedSize && !m_inLiveDecodedResourcesList && hasClients())
529 memoryCache()->insertInLiveDecodedResourcesList(this);
530 else if (!m_decodedSize && m_inLiveDecodedResourcesList)
531 memoryCache()->removeFromLiveDecodedResourcesList(this);
533 // Update the cache's size totals.
534 memoryCache()->adjustSize(hasClients(), delta);
538 void Resource::setEncodedSize(unsigned size)
540 if (size == m_encodedSize)
543 int delta = size - m_encodedSize;
545 // The object must now be moved to a different queue, since its size has been changed.
546 // We have to remove explicitly before updating m_encodedSize, so that we find the correct previous
549 memoryCache()->removeFromLRUList(this);
551 m_encodedSize = size;
554 // Now insert into the new LRU list.
555 memoryCache()->insertInLRUList(this);
557 // Update the cache's size totals.
558 memoryCache()->adjustSize(hasClients(), delta);
562 void Resource::didAccessDecodedData(double timeStamp)
564 m_lastDecodedAccessTime = timeStamp;
566 if (m_inLiveDecodedResourcesList) {
567 memoryCache()->removeFromLiveDecodedResourcesList(this);
568 memoryCache()->insertInLiveDecodedResourcesList(this);
570 memoryCache()->prune();
574 void Resource::finishPendingClients()
576 while (!m_clientsAwaitingCallback.isEmpty()) {
577 ResourceClient* client = m_clientsAwaitingCallback.begin()->key;
578 m_clientsAwaitingCallback.remove(client);
579 m_clients.add(client);
580 didAddClient(client);
584 void Resource::setResourceToRevalidate(Resource* resource)
587 ASSERT(!m_resourceToRevalidate);
588 ASSERT(resource != this);
589 ASSERT(m_handlesToRevalidate.isEmpty());
590 ASSERT(resource->type() == type());
592 LOG(ResourceLoading, "Resource %p setResourceToRevalidate %p", this, resource);
594 // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
595 // https://bugs.webkit.org/show_bug.cgi?id=28604.
596 // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in Resource::clearResourceToRevalidate.
597 ASSERT(!resource->m_proxyResource);
599 resource->m_proxyResource = this;
600 m_resourceToRevalidate = resource;
603 void Resource::clearResourceToRevalidate()
605 ASSERT(m_resourceToRevalidate);
606 if (m_switchingClientsToRevalidatedResource)
609 // 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.
610 if (m_resourceToRevalidate->m_proxyResource == this) {
611 m_resourceToRevalidate->m_proxyResource = 0;
612 m_resourceToRevalidate->deleteIfPossible();
614 m_handlesToRevalidate.clear();
615 m_resourceToRevalidate = 0;
619 void Resource::switchClientsToRevalidatedResource()
621 ASSERT(m_resourceToRevalidate);
622 ASSERT(m_resourceToRevalidate->inCache());
625 LOG(ResourceLoading, "Resource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
627 m_resourceToRevalidate->m_identifier = m_identifier;
629 m_switchingClientsToRevalidatedResource = true;
630 HashSet<ResourcePtrBase*>::iterator end = m_handlesToRevalidate.end();
631 for (HashSet<ResourcePtrBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
632 ResourcePtrBase* handle = *it;
633 handle->m_resource = m_resourceToRevalidate;
634 m_resourceToRevalidate->registerHandle(handle);
637 ASSERT(!m_handleCount);
638 m_handlesToRevalidate.clear();
640 Vector<ResourceClient*> clientsToMove;
641 HashCountedSet<ResourceClient*>::iterator end2 = m_clients.end();
642 for (HashCountedSet<ResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
643 ResourceClient* client = it->key;
644 unsigned count = it->value;
646 clientsToMove.append(client);
651 unsigned moveCount = clientsToMove.size();
652 for (unsigned n = 0; n < moveCount; ++n)
653 removeClient(clientsToMove[n]);
654 ASSERT(m_clients.isEmpty());
656 for (unsigned n = 0; n < moveCount; ++n)
657 m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
658 for (unsigned n = 0; n < moveCount; ++n) {
659 // Calling didAddClient may do anything, including trying to cancel revalidation.
660 // Assert that it didn't succeed.
661 ASSERT(m_resourceToRevalidate);
662 // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
663 if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
664 m_resourceToRevalidate->didAddClient(clientsToMove[n]);
666 m_switchingClientsToRevalidatedResource = false;
669 void Resource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
671 m_responseTimestamp = currentTime();
674 // Update cached headers from the 304 response
675 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
676 HTTPHeaderMap::const_iterator end = newHeaders.end();
677 for (HTTPHeaderMap::const_iterator it = newHeaders.begin(); it != end; ++it) {
678 // Entity headers should not be sent by servers when generating a 304
679 // response; misconfigured servers send them anyway. We shouldn't allow
680 // such headers to update the original request. We'll base this on the
681 // list defined by RFC2616 7.1, with a few additions for extension headers
683 if (!shouldUpdateHeaderAfterRevalidation(it->key))
685 m_response.setHTTPHeaderField(it->key, it->value);
689 void Resource::revalidationSucceeded(const ResourceResponse& response)
691 ASSERT(m_resourceToRevalidate);
692 ASSERT(!m_resourceToRevalidate->inCache());
693 ASSERT(m_resourceToRevalidate->isLoaded());
696 // Calling evict() can potentially delete revalidatingResource, which we use
697 // below. This mustn't be the case since revalidation means it is loaded
698 // and so canDelete() is false.
699 ASSERT(!canDelete());
701 m_resourceToRevalidate->updateResponseAfterRevalidation(response);
702 memoryCache()->replace(m_resourceToRevalidate, this);
704 switchClientsToRevalidatedResource();
706 // clearResourceToRevalidate deletes this.
707 clearResourceToRevalidate();
710 void Resource::revalidationFailed()
712 ASSERT(WTF::isMainThread());
713 LOG(ResourceLoading, "Revalidation failed for %p", this);
714 ASSERT(resourceToRevalidate());
715 clearResourceToRevalidate();
718 void Resource::updateForAccess()
722 // Need to make sure to remove before we increase the access count, since
723 // the queue will possibly change.
724 memoryCache()->removeFromLRUList(this);
726 // If this is the first time the resource has been accessed, adjust the size of the cache to account for its initial size.
728 memoryCache()->adjustSize(hasClients(), size());
731 memoryCache()->insertInLRUList(this);
734 void Resource::registerHandle(ResourcePtrBase* h)
737 if (m_resourceToRevalidate)
738 m_handlesToRevalidate.add(h);
741 void Resource::unregisterHandle(ResourcePtrBase* h)
743 ASSERT(m_handleCount > 0);
746 if (m_resourceToRevalidate)
747 m_handlesToRevalidate.remove(h);
753 bool Resource::canUseCacheValidator() const
755 if (m_loading || errorOccurred())
758 if (m_response.cacheControlContainsNoStore())
760 return m_response.hasCacheValidatorFields();
763 bool Resource::mustRevalidateDueToCacheHeaders(CachePolicy cachePolicy) const
765 ASSERT(cachePolicy == CachePolicyRevalidate || cachePolicy == CachePolicyCache || cachePolicy == CachePolicyVerify);
767 if (cachePolicy == CachePolicyRevalidate)
770 if (m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()) {
771 LOG(ResourceLoading, "Resource %p mustRevalidate because of m_response.cacheControlContainsNoCache() || m_response.cacheControlContainsNoStore()\n", this);
775 if (cachePolicy == CachePolicyCache) {
776 if (m_response.cacheControlContainsMustRevalidate() && isExpired()) {
777 LOG(ResourceLoading, "Resource %p mustRevalidate because of cachePolicy == CachePolicyCache and m_response.cacheControlContainsMustRevalidate() && isExpired()\n", this);
785 LOG(ResourceLoading, "Resource %p mustRevalidate because of isExpired()\n", this);
792 bool Resource::isSafeToMakePurgeable() const
794 return !hasClients() && !m_proxyResource && !m_resourceToRevalidate;
797 bool Resource::makePurgeable(bool purgeable)
800 ASSERT(isSafeToMakePurgeable());
802 if (m_purgeableData) {
809 // Should not make buffer purgeable if it has refs other than this since we don't want two copies.
810 if (!m_data->hasOneRef())
813 m_data->createPurgeableBuffer();
814 if (!m_data->hasPurgeableBuffer())
817 m_purgeableData = m_data->releasePurgeableBuffer();
818 m_purgeableData->unlock();
823 if (!m_purgeableData)
827 ASSERT(!hasClients());
829 if (!m_purgeableData->lock())
832 m_data = SharedBuffer::adoptPurgeableBuffer(m_purgeableData.release());
836 bool Resource::isPurgeable() const
838 return m_purgeableData && m_purgeableData->isPurgeable();
841 bool Resource::wasPurged() const
843 return m_purgeableData && m_purgeableData->wasPurged();
846 unsigned Resource::overheadSize() const
848 static const int kAverageClientsHashMapSize = 384;
849 return sizeof(Resource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
852 void Resource::didChangePriority(ResourceLoadPriority loadPriority)
855 m_loader->didChangePriority(loadPriority);
858 Resource::ResourceCallback* Resource::ResourceCallback::callbackHandler()
860 DEFINE_STATIC_LOCAL(ResourceCallback, callbackHandler, ());
861 return &callbackHandler;
864 Resource::ResourceCallback::ResourceCallback()
865 : m_callbackTimer(this, &ResourceCallback::timerFired)
869 void Resource::ResourceCallback::schedule(Resource* resource)
871 if (!m_callbackTimer.isActive())
872 m_callbackTimer.startOneShot(0);
873 m_resourcesWithPendingClients.add(resource);
876 void Resource::ResourceCallback::cancel(Resource* resource)
878 m_resourcesWithPendingClients.remove(resource);
879 if (m_callbackTimer.isActive() && m_resourcesWithPendingClients.isEmpty())
880 m_callbackTimer.stop();
883 void Resource::ResourceCallback::timerFired(Timer<ResourceCallback>*)
885 HashSet<Resource*>::iterator end = m_resourcesWithPendingClients.end();
886 Vector<ResourcePtr<Resource> > resources;
887 for (HashSet<Resource*>::iterator it = m_resourcesWithPendingClients.begin(); it != end; ++it)
888 resources.append(*it);
889 m_resourcesWithPendingClients.clear();
890 for (size_t i = 0; i < resources.size(); i++)
891 resources[i]->finishPendingClients();
895 const char* ResourceTypeName(Resource::Type type)
898 case Resource::MainResource:
899 return "MainResource";
900 case Resource::Image:
902 case Resource::CSSStyleSheet:
903 return "CSSStyleSheet";
904 case Resource::Script:
910 case Resource::SVGDocument:
911 return "SVGDocument";
912 case Resource::XSLStyleSheet:
913 return "XSLStyleSheet";
914 case Resource::LinkPrefetch:
915 return "LinkPrefetch";
916 case Resource::LinkSubresource:
917 return "LinkSubresource";
918 case Resource::TextTrack:
920 case Resource::Shader:
922 case Resource::ImportResource:
923 return "ImportResource";
925 ASSERT_NOT_REACHED();
928 #endif // !LOG_DISABLED