Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / core / fetch / Resource.cpp
blobf1aaf35ded75c5e9d21ed0548344ec9bd3c24798
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, 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.
24 #include "config.h"
25 #include "core/fetch/Resource.h"
27 #include "core/fetch/CachedMetadata.h"
28 #include "core/fetch/CrossOriginAccessControl.h"
29 #include "core/fetch/FetchInitiatorTypeNames.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/TraceEvent.h"
40 #include "platform/weborigin/KURL.h"
41 #include "public/platform/Platform.h"
42 #include "wtf/CurrentTime.h"
43 #include "wtf/MathExtras.h"
44 #include "wtf/StdLibExtras.h"
45 #include "wtf/Vector.h"
46 #include "wtf/text/CString.h"
48 using namespace WTF;
50 namespace blink {
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[] = {
56 "allow",
57 "connection",
58 "etag",
59 "expires",
60 "keep-alive",
61 "last-modified"
62 "proxy-authenticate",
63 "proxy-connection",
64 "trailer",
65 "transfer-encoding",
66 "upgrade",
67 "www-authenticate",
68 "x-frame-options",
69 "x-xss-protection",
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[] = {
76 "content-",
77 "x-content-",
78 "x-webkit-"
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]))
85 return false;
87 for (size_t i = 0; i < WTF_ARRAY_LENGTH(headerPrefixesToIgnoreAfterRevalidation); i++) {
88 if (header.startsWith(headerPrefixesToIgnoreAfterRevalidation[i], TextCaseInsensitive))
89 return false;
91 return true;
94 class Resource::CacheHandler : public CachedMetadataHandler {
95 public:
96 static PassOwnPtrWillBeRawPtr<CacheHandler> create(Resource* resource)
98 return adoptPtrWillBeNoop(new CacheHandler(resource));
100 ~CacheHandler() override { }
101 DECLARE_VIRTUAL_TRACE();
102 void setCachedMetadata(unsigned, const char*, size_t, CacheType) override;
103 void clearCachedMetadata(CacheType) override;
104 CachedMetadata* cachedMetadata(unsigned) const override;
105 String encoding() const override;
107 private:
108 explicit CacheHandler(Resource*);
109 RawPtrWillBeMember<Resource> m_resource;
112 Resource::CacheHandler::CacheHandler(Resource* resource)
113 : m_resource(resource)
117 DEFINE_TRACE(Resource::CacheHandler)
119 #if ENABLE(OILPAN)
120 visitor->trace(m_resource);
121 #endif
122 CachedMetadataHandler::trace(visitor);
125 void Resource::CacheHandler::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size, CacheType type)
127 m_resource->setCachedMetadata(dataTypeID, data, size, type);
130 void Resource::CacheHandler::clearCachedMetadata(CacheType type)
132 m_resource->clearCachedMetadata(type);
135 CachedMetadata* Resource::CacheHandler::cachedMetadata(unsigned dataTypeID) const
137 return m_resource->cachedMetadata(dataTypeID);
140 String Resource::CacheHandler::encoding() const
142 return m_resource->encoding();
145 Resource::Resource(const ResourceRequest& request, Type type)
146 : m_resourceRequest(request)
147 , m_responseTimestamp(currentTime())
148 , m_cancelTimer(this, &Resource::cancelTimerFired)
149 , m_loadFinishTime(0)
150 , m_identifier(0)
151 , m_encodedSize(0)
152 , m_decodedSize(0)
153 , m_handleCount(0)
154 , m_preloadCount(0)
155 , m_protectorCount(0)
156 , m_cacheIdentifier(MemoryCache::defaultCacheIdentifier())
157 , m_preloadResult(PreloadNotReferenced)
158 , m_requestedFromNetworkingLayer(false)
159 , m_loading(false)
160 , m_switchingClientsToRevalidatedResource(false)
161 , m_type(type)
162 , m_status(Pending)
163 , m_wasPurged(false)
164 , m_needsSynchronousCacheHit(false)
165 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
166 , m_deleted(false)
167 #endif
168 , m_resourceToRevalidate(nullptr)
169 , m_proxyResource(nullptr)
171 ASSERT(m_type == unsigned(type)); // m_type is a bitfield, so this tests careless updates of the enum.
172 InstanceCounters::incrementCounter(InstanceCounters::ResourceCounter);
173 memoryCache()->registerLiveResource(*this);
175 // Currently we support the metadata caching only for HTTP family.
176 if (m_resourceRequest.url().protocolIsInHTTPFamily())
177 m_cacheHandler = CacheHandler::create(this);
179 if (!m_resourceRequest.url().hasFragmentIdentifier())
180 return;
181 KURL urlForCache = MemoryCache::removeFragmentIdentifierIfNeeded(m_resourceRequest.url());
182 if (urlForCache.hasFragmentIdentifier())
183 return;
184 m_fragmentIdentifierForRequest = m_resourceRequest.url().fragmentIdentifier();
185 m_resourceRequest.setURL(urlForCache);
188 Resource::~Resource()
190 ASSERT(!m_resourceToRevalidate); // Should be true because canDelete() checks this.
191 ASSERT(canDelete());
192 RELEASE_ASSERT(!memoryCache()->contains(this));
193 RELEASE_ASSERT(!ResourceCallback::callbackHandler()->isScheduled(this));
194 assertAlive();
196 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
197 m_deleted = true;
198 #endif
199 InstanceCounters::decrementCounter(InstanceCounters::ResourceCounter);
202 void Resource::dispose()
206 DEFINE_TRACE(Resource)
208 visitor->trace(m_loader);
209 visitor->trace(m_resourceToRevalidate);
210 visitor->trace(m_proxyResource);
211 #if ENABLE(OILPAN)
212 visitor->trace(m_cacheHandler);
213 #endif
216 void Resource::load(ResourceFetcher* fetcher, const ResourceLoaderOptions& options)
218 m_options = options;
219 m_loading = true;
221 if (!accept().isEmpty())
222 m_resourceRequest.setHTTPAccept(accept());
224 // FIXME: It's unfortunate that the cache layer and below get to know anything about fragment identifiers.
225 // We should look into removing the expectation of that knowledge from the platform network stacks.
226 ResourceRequest request(m_resourceRequest);
227 if (!m_fragmentIdentifierForRequest.isNull()) {
228 KURL url = request.url();
229 url.setFragmentIdentifier(m_fragmentIdentifierForRequest);
230 request.setURL(url);
231 m_fragmentIdentifierForRequest = String();
233 m_status = Pending;
234 if (m_loader) {
235 RELEASE_ASSERT(m_options.synchronousPolicy == RequestSynchronously);
236 m_loader->changeToSynchronous();
237 return;
239 m_loader = ResourceLoader::create(fetcher, this, request, options);
240 m_loader->start();
243 void Resource::checkNotify()
245 if (isLoading())
246 return;
248 ResourceClientWalker<ResourceClient> w(m_clients);
249 while (ResourceClient* c = w.next())
250 c->notifyFinished(this);
253 void Resource::appendData(const char* data, unsigned length)
255 TRACE_EVENT0("blink", "Resource::appendData");
256 ASSERT(!m_resourceToRevalidate);
257 ASSERT(!errorOccurred());
258 if (m_options.dataBufferingPolicy == DoNotBufferData)
259 return;
260 if (m_data)
261 m_data->append(data, length);
262 else
263 m_data = SharedBuffer::createPurgeable(data, length);
264 setEncodedSize(m_data->size());
267 void Resource::setResourceBuffer(PassRefPtr<SharedBuffer> resourceBuffer)
269 ASSERT(!m_resourceToRevalidate);
270 ASSERT(!errorOccurred());
271 ASSERT(m_options.dataBufferingPolicy == BufferData);
272 m_data = resourceBuffer;
273 setEncodedSize(m_data->size());
276 void Resource::setDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
278 m_options.dataBufferingPolicy = dataBufferingPolicy;
279 m_data.clear();
280 setEncodedSize(0);
283 void Resource::error(Resource::Status status)
285 if (m_resourceToRevalidate)
286 revalidationFailed();
288 if (!m_error.isNull() && (m_error.isCancellation() || !isPreloaded()))
289 memoryCache()->remove(this);
291 setStatus(status);
292 ASSERT(errorOccurred());
293 m_data.clear();
295 setLoading(false);
296 checkNotify();
299 void Resource::finishOnePart()
301 setLoading(false);
302 checkNotify();
305 void Resource::finish()
307 ASSERT(!m_resourceToRevalidate);
308 ASSERT(!errorOccurred());
309 finishOnePart();
310 if (!errorOccurred())
311 m_status = Cached;
314 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin) const
316 String ignoredErrorDescription;
317 return passesAccessControlCheck(securityOrigin, ignoredErrorDescription);
320 bool Resource::passesAccessControlCheck(SecurityOrigin* securityOrigin, String& errorDescription) const
322 return blink::passesAccessControlCheck(m_response, resourceRequest().allowStoredCredentials() ? AllowStoredCredentials : DoNotAllowStoredCredentials, securityOrigin, errorDescription, resourceRequest().requestContext());
325 bool Resource::isEligibleForIntegrityCheck(SecurityOrigin* securityOrigin) const
327 String ignoredErrorDescription;
328 return securityOrigin->canRequest(resourceRequest().url()) || passesAccessControlCheck(securityOrigin, ignoredErrorDescription);
331 static double currentAge(const ResourceResponse& response, double responseTimestamp)
333 // RFC2616 13.2.3
334 // No compensation for latency as that is not terribly important in practice
335 double dateValue = response.date();
336 double apparentAge = std::isfinite(dateValue) ? std::max(0., responseTimestamp - dateValue) : 0;
337 double ageValue = response.age();
338 double correctedReceivedAge = std::isfinite(ageValue) ? std::max(apparentAge, ageValue) : apparentAge;
339 double residentTime = currentTime() - responseTimestamp;
340 return correctedReceivedAge + residentTime;
343 double Resource::currentAge() const
345 return blink::currentAge(m_response, m_responseTimestamp);
348 static double freshnessLifetime(ResourceResponse& response, double responseTimestamp)
350 #if !OS(ANDROID)
351 // On desktop, local files should be reloaded in case they change.
352 if (response.url().isLocalFile())
353 return 0;
354 #endif
356 // Cache other non-http / non-filesystem resources liberally.
357 if (!response.url().protocolIsInHTTPFamily()
358 && !response.url().protocolIs("filesystem"))
359 return std::numeric_limits<double>::max();
361 // RFC2616 13.2.4
362 double maxAgeValue = response.cacheControlMaxAge();
363 if (std::isfinite(maxAgeValue))
364 return maxAgeValue;
365 double expiresValue = response.expires();
366 double dateValue = response.date();
367 double creationTime = std::isfinite(dateValue) ? dateValue : responseTimestamp;
368 if (std::isfinite(expiresValue))
369 return expiresValue - creationTime;
370 double lastModifiedValue = response.lastModified();
371 if (std::isfinite(lastModifiedValue))
372 return (creationTime - lastModifiedValue) * 0.1;
373 // If no cache headers are present, the specification leaves the decision to the UA. Other browsers seem to opt for 0.
374 return 0;
377 double Resource::freshnessLifetime()
379 return blink::freshnessLifetime(m_response, m_responseTimestamp);
382 double Resource::stalenessLifetime()
384 return m_response.cacheControlStaleWhileRevalidate();
387 static bool canUseResponse(ResourceResponse& response, double responseTimestamp)
389 if (response.isNull())
390 return false;
392 // FIXME: Why isn't must-revalidate considered a reason we can't use the response?
393 if (response.cacheControlContainsNoCache() || response.cacheControlContainsNoStore())
394 return false;
396 if (response.httpStatusCode() == 303) {
397 // Must not be cached.
398 return false;
401 if (response.httpStatusCode() == 302 || response.httpStatusCode() == 307) {
402 // Default to not cacheable unless explicitly allowed.
403 bool hasMaxAge = std::isfinite(response.cacheControlMaxAge());
404 bool hasExpires = std::isfinite(response.expires());
405 // TODO: consider catching Cache-Control "private" and "public" here.
406 if (!hasMaxAge && !hasExpires)
407 return false;
410 return currentAge(response, responseTimestamp) <= freshnessLifetime(response, responseTimestamp);
413 const ResourceRequest& Resource::lastResourceRequest() const
415 if (!m_redirectChain.size())
416 return m_resourceRequest;
417 return m_redirectChain.last().m_request;
420 void Resource::willFollowRedirect(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
422 m_redirectChain.append(RedirectPair(newRequest, redirectResponse));
423 m_requestedFromNetworkingLayer = true;
426 bool Resource::unlock()
428 if (!m_data)
429 return false;
431 if (!m_data->isLocked())
432 return true;
434 if (!memoryCache()->contains(this) || hasClients() || m_handleCount > 1 || m_proxyResource || m_resourceToRevalidate || !m_loadFinishTime || !isSafeToUnlock())
435 return false;
437 m_data->unlock();
438 return true;
441 bool Resource::hasRightHandleCountApartFromCache(unsigned targetCount) const
443 return m_handleCount == targetCount + (memoryCache()->contains(this) ? 1 : 0);
446 void Resource::responseReceived(const ResourceResponse& response, PassOwnPtr<WebDataConsumerHandle>)
448 setResponse(response);
449 m_responseTimestamp = currentTime();
450 String encoding = response.textEncodingName();
451 if (!encoding.isNull())
452 setEncoding(encoding);
454 if (!m_resourceToRevalidate)
455 return;
456 if (response.httpStatusCode() == 304)
457 revalidationSucceeded(response);
458 else
459 revalidationFailed();
462 void Resource::setSerializedCachedMetadata(const char* data, size_t size)
464 // We only expect to receive cached metadata from the platform once.
465 // If this triggers, it indicates an efficiency problem which is most
466 // likely unexpected in code designed to improve performance.
467 ASSERT(!m_cachedMetadata);
468 ASSERT(!m_resourceToRevalidate);
470 m_cachedMetadata = CachedMetadata::deserialize(data, size);
473 CachedMetadataHandler* Resource::cacheHandler()
475 return m_cacheHandler.get();
478 void Resource::setCachedMetadata(unsigned dataTypeID, const char* data, size_t size, CachedMetadataHandler::CacheType cacheType)
480 // Currently, only one type of cached metadata per resource is supported.
481 // If the need arises for multiple types of metadata per resource this could
482 // be enhanced to store types of metadata in a map.
483 ASSERT(!m_cachedMetadata);
485 m_cachedMetadata = CachedMetadata::create(dataTypeID, data, size);
487 // We don't support sending the metadata to the platform when the response
488 // was fetched via a ServiceWorker to prevent an attacker's Service Worker
489 // from poisoning the metadata cache.
490 // FIXME: Support sending the metadata even if the response was fetched via
491 // a ServiceWorker. https://crbug.com/448706
492 if (cacheType == CachedMetadataHandler::SendToPlatform && !m_response.wasFetchedViaServiceWorker()) {
493 const Vector<char>& serializedData = m_cachedMetadata->serialize();
494 Platform::current()->cacheMetadata(m_response.url(), m_response.responseTime(), serializedData.data(), serializedData.size());
498 void Resource::clearCachedMetadata(CachedMetadataHandler::CacheType cacheType)
500 m_cachedMetadata.clear();
502 if (cacheType == CachedMetadataHandler::SendToPlatform)
503 Platform::current()->cacheMetadata(m_response.url(), m_response.responseTime(), 0, 0);
506 bool Resource::canDelete() const
508 return !hasClients() && !m_loader && !m_preloadCount && hasRightHandleCountApartFromCache(0)
509 && !m_protectorCount && !m_resourceToRevalidate && !m_proxyResource;
512 bool Resource::hasOneHandle() const
514 return hasRightHandleCountApartFromCache(1);
517 CachedMetadata* Resource::cachedMetadata(unsigned dataTypeID) const
519 if (!m_cachedMetadata || m_cachedMetadata->dataTypeID() != dataTypeID)
520 return nullptr;
521 return m_cachedMetadata.get();
524 void Resource::clearLoader()
526 m_loader = nullptr;
529 void Resource::addClient(ResourceClient* client)
531 if (addClientToSet(client))
532 didAddClient(client);
535 void Resource::didAddClient(ResourceClient* c)
537 if (!isLoading() && !stillNeedsLoad())
538 c->notifyFinished(this);
541 static bool shouldSendCachedDataSynchronouslyForType(Resource::Type type)
543 // Some resources types default to return data synchronously.
544 // For most of these, it's because there are layout tests that
545 // expect data to return synchronously in case of cache hit. In
546 // the case of fonts, there was a performance regression.
547 // FIXME: Get to the point where we don't need to special-case sync/async
548 // behavior for different resource types.
549 if (type == Resource::Image)
550 return true;
551 if (type == Resource::CSSStyleSheet)
552 return true;
553 if (type == Resource::Script)
554 return true;
555 if (type == Resource::Font)
556 return true;
557 return false;
560 bool Resource::addClientToSet(ResourceClient* client)
562 ASSERT(!isPurgeable());
564 if (m_preloadResult == PreloadNotReferenced) {
565 if (isLoaded())
566 m_preloadResult = PreloadReferencedWhileComplete;
567 else if (m_requestedFromNetworkingLayer)
568 m_preloadResult = PreloadReferencedWhileLoading;
569 else
570 m_preloadResult = PreloadReferenced;
572 if (!hasClients())
573 memoryCache()->makeLive(this);
575 // If we have existing data to send to the new client and the resource type supprts it, send it asynchronously.
576 if (!m_response.isNull() && !m_proxyResource && !shouldSendCachedDataSynchronouslyForType(type()) && !m_needsSynchronousCacheHit) {
577 m_clientsAwaitingCallback.add(client);
578 ResourceCallback::callbackHandler()->schedule(this);
579 return false;
582 m_clients.add(client);
583 return true;
586 void Resource::removeClient(ResourceClient* client)
588 if (m_clientsAwaitingCallback.contains(client)) {
589 ASSERT(!m_clients.contains(client));
590 m_clientsAwaitingCallback.remove(client);
591 } else {
592 ASSERT(m_clients.contains(client));
593 m_clients.remove(client);
594 didRemoveClient(client);
597 if (m_clientsAwaitingCallback.isEmpty())
598 ResourceCallback::callbackHandler()->cancel(this);
600 bool deleted = deleteIfPossible();
601 if (!deleted && !hasClients()) {
602 memoryCache()->makeDead(this);
603 if (!m_switchingClientsToRevalidatedResource)
604 allClientsRemoved();
606 // RFC2616 14.9.2:
607 // "no-store: ... MUST make a best-effort attempt to remove the information from volatile storage as promptly as possible"
608 // "... History buffers MAY store such responses as part of their normal operation."
609 // We allow non-secure content to be reused in history, but we do not allow secure content to be reused.
610 if (hasCacheControlNoStoreHeader() && url().protocolIs("https")) {
611 memoryCache()->remove(this);
612 memoryCache()->prune();
613 } else {
614 memoryCache()->prune(this);
617 // This object may be dead here.
620 void Resource::allClientsRemoved()
622 if (!m_loader)
623 return;
624 if (m_type == MainResource || m_type == Raw)
625 cancelTimerFired(&m_cancelTimer);
626 else if (!m_cancelTimer.isActive())
627 m_cancelTimer.startOneShot(0, FROM_HERE);
629 unlock();
632 void Resource::cancelTimerFired(Timer<Resource>* timer)
634 ASSERT_UNUSED(timer, timer == &m_cancelTimer);
635 if (hasClients() || !m_loader)
636 return;
637 ResourcePtr<Resource> protect(this);
638 m_loader->cancelIfNotFinishing();
639 if (m_status != Cached)
640 memoryCache()->remove(this);
643 bool Resource::deleteIfPossible()
645 if (canDelete() && !memoryCache()->contains(this)) {
646 InspectorInstrumentation::willDestroyResource(this);
647 dispose();
648 memoryCache()->unregisterLiveResource(*this);
649 #if !ENABLE(OILPAN)
650 delete this;
651 #endif
652 return true;
654 return false;
657 void Resource::setDecodedSize(size_t decodedSize)
659 if (decodedSize == m_decodedSize)
660 return;
661 size_t oldSize = size();
662 m_decodedSize = decodedSize;
663 memoryCache()->update(this, oldSize, size());
664 memoryCache()->updateDecodedResource(this, UpdateForPropertyChange);
667 void Resource::setEncodedSize(size_t encodedSize)
669 if (encodedSize == m_encodedSize)
670 return;
671 size_t oldSize = size();
672 m_encodedSize = encodedSize;
673 memoryCache()->update(this, oldSize, size());
676 void Resource::didAccessDecodedData()
678 memoryCache()->updateDecodedResource(this, UpdateForAccess);
679 memoryCache()->prune();
682 void Resource::finishPendingClients()
684 // We're going to notify clients one by one. It is simple if the client does nothing.
685 // However there are a couple other things that can happen.
687 // 1. Clients can be added during the loop. Make sure they are not processed.
688 // 2. Clients can be removed during the loop. Make sure they are always available to be
689 // removed. Also don't call removed clients or add them back.
691 // Handle case (1) by saving a list of clients to notify. A separate list also ensure
692 // a client is either in m_clients or m_clientsAwaitingCallback.
693 Vector<ResourceClient*> clientsToNotify;
694 copyToVector(m_clientsAwaitingCallback, clientsToNotify);
696 for (const auto& client : clientsToNotify) {
697 // Handle case (2) to skip removed clients.
698 if (!m_clientsAwaitingCallback.remove(client))
699 continue;
700 m_clients.add(client);
701 didAddClient(client);
704 // It is still possible for the above loop to finish a new client synchronously.
705 // If there's no client waiting we should deschedule.
706 bool scheduled = ResourceCallback::callbackHandler()->isScheduled(this);
707 if (scheduled && m_clientsAwaitingCallback.isEmpty())
708 ResourceCallback::callbackHandler()->cancel(this);
710 // Prevent the case when there are clients waiting but no callback scheduled.
711 ASSERT(m_clientsAwaitingCallback.isEmpty() || scheduled);
714 void Resource::prune()
716 destroyDecodedDataIfPossible();
717 unlock();
720 void Resource::setResourceToRevalidate(Resource* resource)
722 ASSERT(resource);
723 ASSERT(!m_resourceToRevalidate);
724 ASSERT(resource != this);
725 ASSERT(m_handlesToRevalidate.isEmpty());
726 ASSERT(resource->type() == type());
728 WTF_LOG(ResourceLoading, "Resource %p setResourceToRevalidate %p", this, resource);
730 // The following assert should be investigated whenever it occurs. Although it should never fire, it currently does in rare circumstances.
731 // https://bugs.webkit.org/show_bug.cgi?id=28604.
732 // So the code needs to be robust to this assert failing thus the "if (m_resourceToRevalidate->m_proxyResource == this)" in Resource::clearResourceToRevalidate.
733 ASSERT(!resource->m_proxyResource);
735 resource->m_proxyResource = this;
736 m_resourceToRevalidate = resource;
739 void Resource::clearResourceToRevalidate()
741 ASSERT(m_resourceToRevalidate);
742 if (m_switchingClientsToRevalidatedResource)
743 return;
745 // 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.
746 if (m_resourceToRevalidate->m_proxyResource == this) {
747 m_resourceToRevalidate->m_proxyResource = nullptr;
748 m_resourceToRevalidate->deleteIfPossible();
750 m_handlesToRevalidate.clear();
751 m_resourceToRevalidate = nullptr;
752 deleteIfPossible();
755 void Resource::switchClientsToRevalidatedResource()
757 ASSERT(m_resourceToRevalidate);
758 ASSERT(memoryCache()->contains(m_resourceToRevalidate));
759 ASSERT(!memoryCache()->contains(this));
761 WTF_LOG(ResourceLoading, "Resource %p switchClientsToRevalidatedResource %p", this, m_resourceToRevalidate.get());
763 m_resourceToRevalidate->m_identifier = m_identifier;
765 m_switchingClientsToRevalidatedResource = true;
766 for (ResourcePtrBase* handle : m_handlesToRevalidate) {
767 handle->m_resource = m_resourceToRevalidate;
768 m_resourceToRevalidate->registerHandle(handle);
769 --m_handleCount;
771 ASSERT(!m_handleCount);
772 m_handlesToRevalidate.clear();
774 Vector<ResourceClient*> clientsToMove;
775 for (const auto& clientHashEntry : m_clients) {
776 unsigned count = clientHashEntry.value;
777 while (count--)
778 clientsToMove.append(clientHashEntry.key);
781 unsigned moveCount = clientsToMove.size();
782 for (unsigned n = 0; n < moveCount; ++n)
783 removeClient(clientsToMove[n]);
784 ASSERT(m_clients.isEmpty());
786 for (unsigned n = 0; n < moveCount; ++n)
787 m_resourceToRevalidate->addClientToSet(clientsToMove[n]);
788 for (unsigned n = 0; n < moveCount; ++n) {
789 // Calling didAddClient may do anything, including trying to cancel revalidation.
790 // Assert that it didn't succeed.
791 ASSERT(m_resourceToRevalidate);
792 // Calling didAddClient for a client may end up removing another client. In that case it won't be in the set anymore.
793 if (m_resourceToRevalidate->m_clients.contains(clientsToMove[n]))
794 m_resourceToRevalidate->didAddClient(clientsToMove[n]);
796 m_switchingClientsToRevalidatedResource = false;
799 void Resource::updateResponseAfterRevalidation(const ResourceResponse& validatingResponse)
801 m_responseTimestamp = currentTime();
803 // RFC2616 10.3.5
804 // Update cached headers from the 304 response
805 const HTTPHeaderMap& newHeaders = validatingResponse.httpHeaderFields();
806 for (const auto& header : newHeaders) {
807 // Entity headers should not be sent by servers when generating a 304
808 // response; misconfigured servers send them anyway. We shouldn't allow
809 // such headers to update the original request. We'll base this on the
810 // list defined by RFC2616 7.1, with a few additions for extension headers
811 // we care about.
812 if (!shouldUpdateHeaderAfterRevalidation(header.key))
813 continue;
814 m_response.setHTTPHeaderField(header.key, header.value);
818 void Resource::revalidationSucceeded(const ResourceResponse& response)
820 ASSERT(m_resourceToRevalidate);
821 ASSERT(!memoryCache()->contains(m_resourceToRevalidate));
822 ASSERT(m_resourceToRevalidate->isLoaded());
824 // Calling evict() can potentially delete revalidatingResource, which we use
825 // below. This mustn't be the case since revalidation means it is loaded
826 // and so canDelete() is false.
827 ASSERT(!canDelete());
829 m_resourceToRevalidate->updateResponseAfterRevalidation(response);
830 memoryCache()->replace(m_resourceToRevalidate, this);
832 switchClientsToRevalidatedResource();
833 assertAlive();
834 // clearResourceToRevalidate deletes this.
835 clearResourceToRevalidate();
838 void Resource::revalidationFailed()
840 ASSERT(WTF::isMainThread());
841 WTF_LOG(ResourceLoading, "Revalidation failed for %p", this);
842 ASSERT(resourceToRevalidate());
843 clearResourceToRevalidate();
846 void Resource::registerHandle(ResourcePtrBase* h)
848 assertAlive();
849 ++m_handleCount;
850 if (m_resourceToRevalidate)
851 m_handlesToRevalidate.add(h);
854 void Resource::unregisterHandle(ResourcePtrBase* h)
856 assertAlive();
857 ASSERT(m_handleCount > 0);
858 --m_handleCount;
860 if (m_resourceToRevalidate)
861 m_handlesToRevalidate.remove(h);
863 if (!m_handleCount) {
864 if (deleteIfPossible())
865 return;
866 unlock();
867 } else if (m_handleCount == 1 && memoryCache()->contains(this)) {
868 unlock();
869 if (!hasClients())
870 memoryCache()->prune(this);
874 bool Resource::canReuseRedirectChain()
876 for (auto& redirect : m_redirectChain) {
877 if (!canUseResponse(redirect.m_redirectResponse, m_responseTimestamp))
878 return false;
879 if (redirect.m_request.cacheControlContainsNoCache() || redirect.m_request.cacheControlContainsNoStore())
880 return false;
882 return true;
885 bool Resource::hasCacheControlNoStoreHeader()
887 return m_response.cacheControlContainsNoStore() || m_resourceRequest.cacheControlContainsNoStore();
890 bool Resource::hasVaryHeader() const
892 return !m_response.httpHeaderField("Vary").isNull();
895 bool Resource::mustRevalidateDueToCacheHeaders()
897 return !canUseResponse(m_response, m_responseTimestamp) || m_resourceRequest.cacheControlContainsNoCache() || m_resourceRequest.cacheControlContainsNoStore();
900 bool Resource::canUseCacheValidator()
902 if (m_loading || errorOccurred())
903 return false;
905 if (hasCacheControlNoStoreHeader())
906 return false;
907 return m_response.hasCacheValidatorFields() || m_resourceRequest.hasCacheValidatorFields();
910 bool Resource::isPurgeable() const
912 return m_data && !m_data->isLocked();
915 bool Resource::wasPurged() const
917 return m_wasPurged;
920 bool Resource::lock()
922 if (!m_data)
923 return true;
924 if (m_data->isLocked())
925 return true;
927 ASSERT(!hasClients());
929 if (!m_data->lock()) {
930 m_wasPurged = true;
931 return false;
933 return true;
936 size_t Resource::overheadSize() const
938 static const int kAverageClientsHashMapSize = 384;
939 return sizeof(Resource) + m_response.memoryUsage() + kAverageClientsHashMapSize + m_resourceRequest.url().string().length() * 2;
942 void Resource::didChangePriority(ResourceLoadPriority loadPriority, int intraPriorityValue)
944 if (m_loader)
945 m_loader->didChangePriority(loadPriority, intraPriorityValue);
948 Resource::ResourceCallback* Resource::ResourceCallback::callbackHandler()
950 DEFINE_STATIC_LOCAL(OwnPtrWillBePersistent<ResourceCallback>, callbackHandler, (adoptPtrWillBeNoop(new ResourceCallback)));
951 return callbackHandler.get();
954 DEFINE_TRACE(Resource::ResourceCallback)
956 #if ENABLE(OILPAN)
957 visitor->trace(m_resourcesWithPendingClients);
958 #endif
961 Resource::ResourceCallback::ResourceCallback()
962 : m_callbackTimer(this, &ResourceCallback::timerFired)
966 void Resource::ResourceCallback::schedule(Resource* resource)
968 if (!m_callbackTimer.isActive())
969 m_callbackTimer.startOneShot(0, FROM_HERE);
970 resource->assertAlive();
971 m_resourcesWithPendingClients.add(resource);
974 void Resource::ResourceCallback::cancel(Resource* resource)
976 resource->assertAlive();
977 m_resourcesWithPendingClients.remove(resource);
978 if (m_callbackTimer.isActive() && m_resourcesWithPendingClients.isEmpty())
979 m_callbackTimer.stop();
982 bool Resource::ResourceCallback::isScheduled(Resource* resource) const
984 return m_resourcesWithPendingClients.contains(resource);
987 void Resource::ResourceCallback::timerFired(Timer<ResourceCallback>*)
989 Vector<ResourcePtr<Resource>> resources;
990 for (const RawPtrWillBeMember<Resource>& resource : m_resourcesWithPendingClients)
991 resources.append(resource.get());
992 m_resourcesWithPendingClients.clear();
994 for (const auto& resource : resources) {
995 resource->assertAlive();
996 resource->finishPendingClients();
997 resource->assertAlive();
1000 for (const auto& resource : resources)
1001 resource->assertAlive();
1004 static const char* initatorTypeNameToString(const AtomicString& initiatorTypeName)
1006 if (initiatorTypeName == FetchInitiatorTypeNames::css)
1007 return "CSS resource";
1008 if (initiatorTypeName == FetchInitiatorTypeNames::document)
1009 return "Document";
1010 if (initiatorTypeName == FetchInitiatorTypeNames::icon)
1011 return "Icon";
1012 if (initiatorTypeName == FetchInitiatorTypeNames::internal)
1013 return "Internal resource";
1014 if (initiatorTypeName == FetchInitiatorTypeNames::link)
1015 return "Link element resource";
1016 if (initiatorTypeName == FetchInitiatorTypeNames::processinginstruction)
1017 return "Processing instruction";
1018 if (initiatorTypeName == FetchInitiatorTypeNames::texttrack)
1019 return "Text track";
1020 if (initiatorTypeName == FetchInitiatorTypeNames::xml)
1021 return "XML resource";
1022 if (initiatorTypeName == FetchInitiatorTypeNames::xmlhttprequest)
1023 return "XMLHttpRequest";
1025 return "Resource";
1028 const char* Resource::resourceTypeToString(Type type, const FetchInitiatorInfo& initiatorInfo)
1030 switch (type) {
1031 case Resource::MainResource:
1032 return "Main resource";
1033 case Resource::Image:
1034 return "Image";
1035 case Resource::CSSStyleSheet:
1036 return "CSS stylesheet";
1037 case Resource::Script:
1038 return "Script";
1039 case Resource::Font:
1040 return "Font";
1041 case Resource::Raw:
1042 return initatorTypeNameToString(initiatorInfo.name);
1043 case Resource::SVGDocument:
1044 return "SVG document";
1045 case Resource::XSLStyleSheet:
1046 return "XSL stylesheet";
1047 case Resource::LinkPrefetch:
1048 return "Link prefetch resource";
1049 case Resource::LinkSubresource:
1050 return "Link subresource";
1051 case Resource::LinkPreload:
1052 return "Link preload";
1053 case Resource::TextTrack:
1054 return "Text track";
1055 case Resource::ImportResource:
1056 return "Imported resource";
1057 case Resource::Media:
1058 return "Media";
1060 ASSERT_NOT_REACHED();
1061 return initatorTypeNameToString(initiatorInfo.name);
1064 #if !LOG_DISABLED
1065 const char* ResourceTypeName(Resource::Type type)
1067 switch (type) {
1068 case Resource::MainResource:
1069 return "MainResource";
1070 case Resource::Image:
1071 return "Image";
1072 case Resource::CSSStyleSheet:
1073 return "CSSStyleSheet";
1074 case Resource::Script:
1075 return "Script";
1076 case Resource::Font:
1077 return "Font";
1078 case Resource::Raw:
1079 return "Raw";
1080 case Resource::SVGDocument:
1081 return "SVGDocument";
1082 case Resource::XSLStyleSheet:
1083 return "XSLStyleSheet";
1084 case Resource::LinkPrefetch:
1085 return "LinkPrefetch";
1086 case Resource::LinkSubresource:
1087 return "LinkSubresource";
1088 case Resource::LinkPreload:
1089 return "LinkPreload";
1090 case Resource::TextTrack:
1091 return "TextTrack";
1092 case Resource::ImportResource:
1093 return "ImportResource";
1094 case Resource::Media:
1095 return "Media";
1097 ASSERT_NOT_REACHED();
1098 return "Unknown";
1100 #endif // !LOG_DISABLED
1102 } // namespace blink