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 "FetchInitiatorTypeNames.h"
28 #include "core/fetch/CachedMetadata.h"
29 #include "core/fetch/CrossOriginAccessControl.h"
30 #include "core/fetch/MemoryCache.h"
31 #include "core/fetch/ResourceClient.h"
32 #include "core/fetch/ResourceClientWalker.h"
33 #include "core/fetch/ResourceFetcher.h"
34 #include "core/fetch/ResourceLoader.h"
35 #include "core/fetch/ResourcePtr.h"
36 #include "core/inspector/InspectorInstrumentation.h"
37 #include "platform/Logging.h"
38 #include "platform/SharedBuffer.h"
39 #include "platform/weborigin/KURL.h"
40 #include "public/platform/Platform.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)
107 , m_protectorCount(0)
108 , m_preloadResult(PreloadNotReferenced)
109 , m_cacheLiveResourcePriority(CacheLiveResourcePriorityLow)
110 , m_requestedFromNetworkingLayer(false)
112 , m_switchingClientsToRevalidatedResource(false)
116 , m_needsSynchronousCacheHit(false)
117 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
120 , m_resourceToRevalidate(0)
123 ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
125 cachedResourceLeakCounter.increment();
128 if (!m_resourceRequest.url().hasFragmentIdentifier())
130 KURL urlForCache = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
131 if (urlForCache.hasFragmentIdentifier())
133 m_fragmentIdentifierForRequest = m_resourceRequest.url().fragmentIdentifier();
134 m_resourceRequest.setURL(urlForCache);
137 Resource::~Resource()
139 ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
141 RELEASE_ASSERT(!memoryCache()->contains(this));
142 RELEASE_ASSERT(!ResourceCallback::callbackHandler()->isScheduled(this));
143 ASSERT(url().isNull() || memoryCache()->resourceForURL(KURL(ParsedURLString, url())) != this);
146 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
150 cachedResourceLeakCounter.decrement();
154 void Resource::failBeforeStarting()
156 WTF_LOG(ResourceLoading, "Cannot start loading '%s'", url().string().latin1().data());
157 error(Resource::LoadError);
160 void Resource::load(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
162 if (!fetcher->frame()) {
163 failBeforeStarting();
170 if (!accept().isEmpty())
171 m_resourceRequest.setHTTPAccept(accept());
173 // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
174 // We should look into removing the expectation of that knowledge from the platform network stacks.
175 ResourceRequest request(m_resourceRequest);
176 if (!m_fragmentIdentifierForRequest.isNull()) {
177 KURL url = request.url();
178 url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
180 m_fragmentIdentifierForRequest = String();
184 RELEASE_ASSERT(m_options.synchronousPolicy == RequestSynchronously);
185 m_loader->changeToSynchronous();
188 m_loader = ResourceLoader::create(fetcher, this, request, options);
192 void Resource::checkNotify()
197 ResourceClientWalker<ResourceClient> w(m_clients);
198 while (ResourceClient* c = w.next())
199 c->notifyFinished(this);
202 void Resource::appendData(const char* data, int length)
204 TRACE_EVENT0("webkit", "Resource::appendData");
205 ASSERT(!m_resourceToRevalidate);
206 ASSERT(!errorOccurred());
207 if (m_options.dataBufferingPolicy == DoNotBufferData)
210 m_data->append(data, length);
212 m_data = SharedBuffer::createPurgeable(data, length);
213 setEncodedSize(m_data->size());
216 void Resource::setResourceBuffer(PassRefPtr<SharedBuffer> resourceBuffer)
218 ASSERT(!m_resourceToRevalidate);
219 ASSERT(!errorOccurred());
220 ASSERT(m_options.dataBufferingPolicy == BufferData);
221 m_data = resourceBuffer;
222 setEncodedSize(m_data->size());
225 void Resource::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
227 m_options.dataBufferingPolicy = dataBufferingPolicy;
232 void Resource::error(Resource::Status status)
234 if (m_resourceToRevalidate)
235 revalidationFailed();
237 if (!m_error.isNull() && (m_error.isCancellation() || !isPreloaded()))
238 memoryCache()->remove(this);
241 ASSERT(errorOccurred());
248 void Resource::finishOnePart()
254 void Resource::finish(double finishTime)
256 ASSERT(!m_resourceToRevalidate);
257 ASSERT(!errorOccurred());
258 m_loadFinishTime = finishTime;
260 if (!errorOccurred())
264 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin)
266 String ignoredErrorDescription;
267 return passesAccessControlCheck(securityOrigin, ignoredErrorDescription);
270 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin, String& errorDescription)
272 return WebCore::passesAccessControlCheck(m_response, resourceRequest().allowStoredCredentials() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription);
275 static double currentAge(const ResourceResponse& response, double responseTimestamp)
278 // No compensation for latency as that is not terribly important in practice
279 double dateValue = response.date();
280 double apparentAge = std::isfinite(dateValue) ? std::max(0., responseTimestamp - dateValue) : 0;
281 double ageValue = response.age();
282 double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
283 double residentTime = currentTime() - responseTimestamp;
284 return correctedReceivedAge + residentTime;
287 static double freshnessLifetime(const ResourceResponse& response, double responseTimestamp)
290 // On desktop, local files should be reloaded in case they change.
291 if (response.url().isLocalFile())
295 // Cache other non-http / non-filesystem resources liberally.
296 if (!response.url().protocolIsInHTTPFamily()
297 && !response.url().protocolIs("filesystem"))
298 return std::numeric_limits<double>::max();
301 double maxAgeValue = response.cacheControlMaxAge();
302 if (std::isfinite(maxAgeValue))
304 double expiresValue = response.expires();
305 double dateValue = response.date();
306 double creationTime = std::isfinite(dateValue) ? dateValue : responseTimestamp;
307 if (std::isfinite(expiresValue))
308 return expiresValue - creationTime;
309 double lastModifiedValue = response.lastModified();
310 if (std::isfinite(lastModifiedValue))
311 return (creationTime - lastModifiedValue) * 0.1;
312 // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
316 static bool canUseResponse(const ResourceResponse& response, double responseTimestamp)
318 if (response.isNull())
321 // FIXME: Why isn't must-revalidate considered a reason we can't use the response?
322 if (response.cacheControlContainsNoCache() || response.cacheControlContainsNoStore())
325 if (response.httpStatusCode() == 303) {
326 // Must not be cached.
330 if (response.httpStatusCode() == 302 || response.httpStatusCode() == 307) {
331 // Default to not cacheable.
332 // FIXME: Consider allowing these to be cached if they have headers permitting caching.
336 return currentAge(response, responseTimestamp) <= freshnessLifetime(response, responseTimestamp);
339 const ResourceRequest& Resource::lastResourceRequest()
341 if (!m_redirectChain.size())
342 return m_resourceRequest;
343 return m_redirectChain.last().m_request;
346 void Resource::willSendRequest(ResourceRequest& request, const ResourceResponse& response)
348 m_redirectChain.append(RedirectPair(request, response));
349 m_requestedFromNetworkingLayer = true;
352 bool Resource::unlock()
357 if (!m_data->isLocked())
360 if (!memoryCache()->contains(this) || hasClients() || m_handleCount > 1 || m_proxyResource || m_resourceToRevalidate || !m_loadFinishTime || !isSafeToUnlock())
367 void Resource::responseReceived(const ResourceResponse& response)
369 setResponse(response);
370 m_responseTimestamp = currentTime();
371 String encoding = response.textEncodingName();
372 if (!encoding.isNull())
373 setEncoding(encoding);
375 if (!m_resourceToRevalidate)
377 if (response.httpStatusCode() == 304)
378 revalidationSucceeded(response);
380 revalidationFailed();
383 void Resource::setSerializedCachedMetadata(const char* data, size_t size)
385 // We only expect to receive cached metadata from the platform once.
386 // If this triggers, it indicates an efficiency problem which is most
387 // likely unexpected in code designed to improve performance.
388 ASSERT(!m_cachedMetadata);
389 ASSERT(!m_resourceToRevalidate);
391 m_cachedMetadata = CachedMetadata::deserialize(data, size);
394 void Resource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size)
396 // Currently, only one type of cached metadata per resource is supported.
397 // If the need arises for multiple types of metadata per resource this could
398 // be enhanced to store types of metadata in a map.
399 ASSERT(!m_cachedMetadata);
401 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
402 const Vector<char>& serializedData = m_cachedMetadata->serialize();
403 blink::Platform::current()->cacheMetadata(m_response.url(), m_response.responseTime(), serializedData.data(), serializedData.size());
406 CachedMetadata* Resource::cachedMetadata(unsigned dataTypeID) const
408 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
410 return m_cachedMetadata.get();
413 void Resource::setCacheLiveResourcePriority(CacheLiveResourcePriority priority)
415 if (memoryCache()->contains(this) && memoryCache()->isInLiveDecodedResourcesList(this) && cacheLiveResourcePriority() != static_cast<unsigned>(priority)) {
416 memoryCache()->removeFromLiveDecodedResourcesList(this);
417 m_cacheLiveResourcePriority = priority;
418 memoryCache()->insertInLiveDecodedResourcesList(this);
419 memoryCache()->prune();
423 void Resource::clearLoader()
428 void Resource::addClient(ResourceClient* client)
430 if (addClientToSet(client))
431 didAddClient(client);
434 void Resource::didAddClient(ResourceClient* c)
436 if (!isLoading() && !stillNeedsLoad())
437 c->notifyFinished(this);
440 static bool shouldSendCachedDataSynchronouslyForType(Resource::Type type)
442 // Some resources types default to return data synchronously.
443 // For most of these, it's because there are layout tests that
444 // expect data to return synchronously in case of cache hit. In
445 // the case of fonts, there was a performance regression.
446 // FIXME: Get to the point where we don't need to special-case sync/async
447 // behavior for different resource types.
448 if (type == Resource::Image)
450 if (type == Resource::CSSStyleSheet)
452 if (type == Resource::Script)
454 if (type == Resource::Font)
459 bool Resource::addClientToSet(ResourceClient* client)
461 ASSERT(!isPurgeable());
463 if (m_preloadResult == PreloadNotReferenced) {
465 m_preloadResult = PreloadReferencedWhileComplete;
466 else if (m_requestedFromNetworkingLayer)
467 m_preloadResult = PreloadReferencedWhileLoading;
469 m_preloadResult = PreloadReferenced;
471 if (!hasClients() && memoryCache()->contains(this))
472 memoryCache()->addToLiveResourcesSize(this);
474 // If we have existing data to send to the new client and the resource type supprts it, send it asynchronously.
475 if (!m_response.isNull() && !m_proxyResource && !shouldSendCachedDataSynchronouslyForType(type()) && !m_needsSynchronousCacheHit) {
476 m_clientsAwaitingCallback.add(client);
477 ResourceCallback::callbackHandler()->schedule(this);
481 m_clients.add(client);
485 void Resource::removeClient(ResourceClient* client)
487 if (m_clientsAwaitingCallback.contains(client)) {
488 ASSERT(!m_clients.contains(client));
489 m_clientsAwaitingCallback.remove(client);
490 if (m_clientsAwaitingCallback.isEmpty())
491 ResourceCallback::callbackHandler()->cancel(this);
493 ASSERT(m_clients.contains(client));
494 m_clients.remove(client);
495 didRemoveClient(client);
498 bool deleted = deleteIfPossible();
499 if (!deleted && !hasClients()) {
500 if (memoryCache()->contains(this)) {
501 memoryCache()->removeFromLiveResourcesSize(this);
502 memoryCache()->removeFromLiveDecodedResourcesList(this);
504 if (!m_switchingClientsToRevalidatedResource)
508 // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
509 // "... History buffers MAY store such responses as part of their normal operation."
510 // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
511 if (response().cacheControlContainsNoStore() && url().protocolIs("https")) {
512 memoryCache()->remove(this);
513 memoryCache()->prune();
515 memoryCache()->prune(this);
518 // This object may be dead here.
521 void Resource::allClientsRemoved()
525 if (m_type == MainResource || m_type == Raw)
526 cancelTimerFired(&m_cancelTimer);
527 else if (!m_cancelTimer.isActive())
528 m_cancelTimer.startOneShot(0, FROM_HERE);
533 void Resource::cancelTimerFired(Timer<Resource>* timer)
535 ASSERT_UNUSED(timer, timer == &m_cancelTimer);
536 if (hasClients() || !m_loader)
538 ResourcePtr<Resource> protect(this);
539 m_loader->cancelIfNotFinishing();
540 if (m_status != Cached)
541 memoryCache()->remove(this);
544 bool Resource::deleteIfPossible()
546 if (canDelete() && !memoryCache()->contains(this)) {
547 InspectorInstrumentation::willDestroyResource(this);
554 void Resource::setDecodedSize(size_t decodedSize)
556 if (decodedSize == m_decodedSize)
558 size_t oldSize = size();
559 m_decodedSize = decodedSize;
561 if (memoryCache()->contains(this)) {
562 // Insert into or remove from the live decoded list if necessary.
563 // When inserting into the LiveDecodedResourcesList it is possible
564 // that the m_lastDecodedAccessTime is still zero or smaller than
565 // the m_lastDecodedAccessTime of the current list head. This is a
566 // violation of the invariant that the list is to be kept sorted
567 // by access time. The weakening of the invariant does not pose
568 // a problem. For more details please see: https://bugs.webkit.org/show_bug.cgi?id=30209
569 if (m_decodedSize && !memoryCache()->isInLiveDecodedResourcesList(this) && hasClients())
570 memoryCache()->insertInLiveDecodedResourcesList(this);
571 else if (!m_decodedSize && memoryCache()->isInLiveDecodedResourcesList(this))
572 memoryCache()->removeFromLiveDecodedResourcesList(this);
574 // Update the cache's size totals.
575 memoryCache()->update(this, oldSize, size());
579 void Resource::setEncodedSize(size_t encodedSize)
581 if (encodedSize == m_encodedSize)
583 size_t oldSize = size();
584 m_encodedSize = encodedSize;
585 if (memoryCache()->contains(this))
586 memoryCache()->update(this, oldSize, size());
589 void Resource::didAccessDecodedData(double timeStamp)
591 m_lastDecodedAccessTime = timeStamp;
592 if (memoryCache()->contains(this)) {
593 if (memoryCache()->isInLiveDecodedResourcesList(this)) {
594 memoryCache()->removeFromLiveDecodedResourcesList(this);
595 memoryCache()->insertInLiveDecodedResourcesList(this);
597 memoryCache()->prune();
601 void Resource::finishPendingClients()
603 while (!m_clientsAwaitingCallback.isEmpty()) {
604 ResourceClient* client = m_clientsAwaitingCallback.begin()->key;
605 m_clientsAwaitingCallback.remove(client);
606 m_clients.add(client);
607 didAddClient(client);
611 void Resource::prune()
613 destroyDecodedDataIfPossible();
617 void Resource::setResourceToRevalidate(Resource* resource)
620 ASSERT(!m_resourceToRevalidate);
621 ASSERT(resource != this);
622 ASSERT(m_handlesToRevalidate.isEmpty());
623 ASSERT(resource->type() == type());
625 WTF_LOG(ResourceLoading, "Resource %p setResourceToRevalidate %p", this, resource);
627 // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
628 // https://bugs.webkit.org/show_bug.cgi?id=28604.
629 // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in Resource::clearResourceToRevalidate.
630 ASSERT(!resource->m_proxyResource);
632 resource->m_proxyResource = this;
633 m_resourceToRevalidate = resource;
636 void Resource::clearResourceToRevalidate()
638 ASSERT(m_resourceToRevalidate);
639 if (m_switchingClientsToRevalidatedResource)
642 // 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.
643 if (m_resourceToRevalidate->m_proxyResource == this) {
644 m_resourceToRevalidate->m_proxyResource = 0;
645 m_resourceToRevalidate->deleteIfPossible();
647 m_handlesToRevalidate.clear();
648 m_resourceToRevalidate = 0;
652 void Resource::switchClientsToRevalidatedResource()
654 ASSERT(m_resourceToRevalidate);
655 ASSERT(memoryCache()->contains(m_resourceToRevalidate));
656 ASSERT(!memoryCache()->contains(this));
658 WTF_LOG(ResourceLoading, "Resource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate);
660 m_resourceToRevalidate->m_identifier = m_identifier;
662 m_switchingClientsToRevalidatedResource = true;
663 HashSet<ResourcePtrBase*>::iterator end = m_handlesToRevalidate.end();
664 for (HashSet<ResourcePtrBase*>::iterator it = m_handlesToRevalidate.begin(); it != end; ++it) {
665 ResourcePtrBase* handle = *it;
666 handle->m_resource = m_resourceToRevalidate;
667 m_resourceToRevalidate->registerHandle(handle);
670 ASSERT(!m_handleCount);
671 m_handlesToRevalidate.clear();
673 Vector<ResourceClient*> clientsToMove;
674 HashCountedSet<ResourceClient*>::iterator end2 = m_clients.end();
675 for (HashCountedSet<ResourceClient*>::iterator it = m_clients.begin(); it != end2; ++it) {
676 ResourceClient* client = it->key;
677 unsigned count = it->value;
679 clientsToMove.append(client);
684 unsigned moveCount = clientsToMove.size();
685 for (unsigned n = 0; n < moveCount; ++n)
686 removeClient(clientsToMove[n]);
687 ASSERT(m_clients.isEmpty());
689 for (unsigned n = 0; n < moveCount; ++n)
690 m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
691 for (unsigned n = 0; n < moveCount; ++n) {
692 // Calling didAddClient may do anything, including trying to cancel revalidation.
693 // Assert that it didn't succeed.
694 ASSERT(m_resourceToRevalidate);
695 // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
696 if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
697 m_resourceToRevalidate->didAddClient(clientsToMove[n]);
699 m_switchingClientsToRevalidatedResource = false;
702 void Resource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
704 m_responseTimestamp = currentTime();
707 // Update cached headers from the 304 response
708 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
709 HTTPHeaderMap::const_iterator end = newHeaders.end();
710 for (HTTPHeaderMap::const_iterator it = newHeaders.begin(); it != end; ++it) {
711 // Entity headers should not be sent by servers when generating a 304
712 // response; misconfigured servers send them anyway. We shouldn't allow
713 // such headers to update the original request. We'll base this on the
714 // list defined by RFC2616 7.1, with a few additions for extension headers
716 if (!shouldUpdateHeaderAfterRevalidation(it->key))
718 m_response.setHTTPHeaderField(it->key, it->value);
722 void Resource::revalidationSucceeded(const ResourceResponse& response)
724 ASSERT(m_resourceToRevalidate);
725 ASSERT(!memoryCache()->contains(m_resourceToRevalidate));
726 ASSERT(m_resourceToRevalidate->isLoaded());
727 ASSERT(memoryCache()->contains(this));
729 // Calling evict() can potentially delete revalidatingResource, which we use
730 // below. This mustn't be the case since revalidation means it is loaded
731 // and so canDelete() is false.
732 ASSERT(!canDelete());
734 m_resourceToRevalidate->updateResponseAfterRevalidation(response);
735 memoryCache()->replace(m_resourceToRevalidate, this);
737 switchClientsToRevalidatedResource();
739 // clearResourceToRevalidate deletes this.
740 clearResourceToRevalidate();
743 void Resource::revalidationFailed()
745 ASSERT(WTF::isMainThread());
746 WTF_LOG(ResourceLoading, "Revalidation failed for %p", this);
747 ASSERT(resourceToRevalidate());
748 clearResourceToRevalidate();
751 void Resource::registerHandle(ResourcePtrBase* h)
755 if (m_resourceToRevalidate)
756 m_handlesToRevalidate.add(h);
759 void Resource::unregisterHandle(ResourcePtrBase* h)
762 ASSERT(m_handleCount > 0);
765 if (m_resourceToRevalidate)
766 m_handlesToRevalidate.remove(h);
768 if (!m_handleCount) {
769 if (deleteIfPossible())
772 } else if (m_handleCount == 1 && memoryCache()->contains(this)) {
777 bool Resource::canReuseRedirectChain() const
779 for (size_t i = 0; i < m_redirectChain.size(); ++i) {
780 if (!canUseResponse(m_redirectChain[i].m_redirectResponse, m_responseTimestamp))
786 bool Resource::mustRevalidateDueToCacheHeaders() const
788 return !canUseResponse(m_response, m_responseTimestamp);
791 bool Resource::canUseCacheValidator() const
793 if (m_loading || errorOccurred())
796 if (m_response.cacheControlContainsNoStore())
798 return m_response.hasCacheValidatorFields();
801 bool Resource::isPurgeable() const
803 return m_data && !m_data->isLocked();
806 bool Resource::wasPurged() const
811 bool Resource::lock()
815 if (m_data->isLocked())
818 ASSERT(!hasClients());
820 if (!m_data->lock()) {
827 size_t Resource::overheadSize() const
829 static const int kAverageClientsHashMapSize = 384;
830 return sizeof(Resource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
833 void Resource::didChangePriority(ResourceLoadPriority loadPriority, int intraPriorityValue)
836 m_loader->didChangePriority(loadPriority, intraPriorityValue);
839 Resource::ResourceCallback* Resource::ResourceCallback::callbackHandler()
841 DEFINE_STATIC_LOCAL(ResourceCallback, callbackHandler, ());
842 return &callbackHandler;
845 Resource::ResourceCallback::ResourceCallback()
846 : m_callbackTimer(this, &ResourceCallback::timerFired)
850 void Resource::ResourceCallback::schedule(Resource* resource)
852 if (!m_callbackTimer.isActive())
853 m_callbackTimer.startOneShot(0, FROM_HERE);
854 resource->assertAlive();
855 m_resourcesWithPendingClients.add(resource);
858 void Resource::ResourceCallback::cancel(Resource* resource)
860 resource->assertAlive();
861 m_resourcesWithPendingClients.remove(resource);
862 if (m_callbackTimer.isActive() && m_resourcesWithPendingClients.isEmpty())
863 m_callbackTimer.stop();
866 bool Resource::ResourceCallback::isScheduled(Resource* resource) const
868 return m_resourcesWithPendingClients.contains(resource);
871 void Resource::ResourceCallback::timerFired(Timer<ResourceCallback>*)
873 HashSet<Resource*>::iterator end = m_resourcesWithPendingClients.end();
874 Vector<ResourcePtr<Resource> > resources;
875 for (HashSet<Resource*>::iterator it = m_resourcesWithPendingClients.begin(); it != end; ++it)
876 resources.append(*it);
877 m_resourcesWithPendingClients.clear();
879 for (size_t i = 0; i < resources.size(); i++) {
880 resources[i]->assertAlive();
881 resources[i]->finishPendingClients();
882 resources[i]->assertAlive();
885 for (size_t i = 0; i < resources.size(); i++)
886 resources[i]->assertAlive();
889 static const char* initatorTypeNameToString(const AtomicString& initiatorTypeName)
891 if (initiatorTypeName == FetchInitiatorTypeNames::css)
892 return "CSS resource";
893 if (initiatorTypeName == FetchInitiatorTypeNames::document)
895 if (initiatorTypeName == FetchInitiatorTypeNames::icon)
897 if (initiatorTypeName == FetchInitiatorTypeNames::internal)
898 return "Internal resource";
899 if (initiatorTypeName == FetchInitiatorTypeNames::link)
900 return "Link element resource";
901 if (initiatorTypeName == FetchInitiatorTypeNames::processinginstruction)
902 return "Processing instruction";
903 if (initiatorTypeName == FetchInitiatorTypeNames::texttrack)
905 if (initiatorTypeName == FetchInitiatorTypeNames::xml)
906 return "XML resource";
907 if (initiatorTypeName == FetchInitiatorTypeNames::xmlhttprequest)
908 return "XMLHttpRequest";
913 const char* Resource::resourceTypeToString(Type type, const FetchInitiatorInfo& initiatorInfo)
916 case Resource::MainResource:
917 return "Main resource";
918 case Resource::Image:
920 case Resource::CSSStyleSheet:
921 return "CSS stylesheet";
922 case Resource::Script:
927 return initatorTypeNameToString(initiatorInfo.name);
928 case Resource::SVGDocument:
929 return "SVG document";
930 case Resource::XSLStyleSheet:
931 return "XSL stylesheet";
932 case Resource::LinkPrefetch:
933 return "Link prefetch resource";
934 case Resource::LinkSubresource:
935 return "Link subresource";
936 case Resource::TextTrack:
938 case Resource::Shader:
940 case Resource::ImportResource:
941 return "Imported resource";
943 ASSERT_NOT_REACHED();
944 return initatorTypeNameToString(initiatorInfo.name);
948 const char* ResourceTypeName(Resource::Type type)
951 case Resource::MainResource:
952 return "MainResource";
953 case Resource::Image:
955 case Resource::CSSStyleSheet:
956 return "CSSStyleSheet";
957 case Resource::Script:
963 case Resource::SVGDocument:
964 return "SVGDocument";
965 case Resource::XSLStyleSheet:
966 return "XSLStyleSheet";
967 case Resource::LinkPrefetch:
968 return "LinkPrefetch";
969 case Resource::LinkSubresource:
970 return "LinkSubresource";
971 case Resource::TextTrack:
973 case Resource::Shader:
975 case Resource::ImportResource:
976 return "ImportResource";
978 ASSERT_NOT_REACHED();
981 #endif // !LOG_DISABLED