Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(72)

Side by Side Diff: third_party/WebKit/Source/platform/loader/fetch/ResourceFetcher.cpp

Issue 2714383003: Reduce FetchContext::getCachePolicy() callers (Closed)
Patch Set: rebase Created 3 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org) 3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org) 4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All 5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
6 rights reserved. 6 rights reserved.
7 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ 7 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
8 8
9 This library is free software; you can redistribute it and/or 9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public 10 modify it under the terms of the GNU Library General Public
(...skipping 665 matching lines...) Expand 10 before | Expand all | Expand 10 after
676 DCHECK(resource->isLoaded()); 676 DCHECK(resource->isLoaded());
677 DCHECK(resource->canUseCacheValidator()); 677 DCHECK(resource->canUseCacheValidator());
678 DCHECK(!resource->isCacheValidator()); 678 DCHECK(!resource->isCacheValidator());
679 DCHECK(!context().isControlledByServiceWorker()); 679 DCHECK(!context().isControlledByServiceWorker());
680 680
681 const AtomicString& lastModified = 681 const AtomicString& lastModified =
682 resource->response().httpHeaderField(HTTPNames::Last_Modified); 682 resource->response().httpHeaderField(HTTPNames::Last_Modified);
683 const AtomicString& eTag = 683 const AtomicString& eTag =
684 resource->response().httpHeaderField(HTTPNames::ETag); 684 resource->response().httpHeaderField(HTTPNames::ETag);
685 if (!lastModified.isEmpty() || !eTag.isEmpty()) { 685 if (!lastModified.isEmpty() || !eTag.isEmpty()) {
686 DCHECK_NE(context().getCachePolicy(), CachePolicyReload); 686 DCHECK_NE(WebCachePolicy::BypassingCache,
687 if (context().getCachePolicy() == CachePolicyRevalidate) { 687 revalidatingRequest.getCachePolicy());
688 if (revalidatingRequest.getCachePolicy() ==
689 WebCachePolicy::ValidatingCacheData) {
688 revalidatingRequest.setHTTPHeaderField(HTTPNames::Cache_Control, 690 revalidatingRequest.setHTTPHeaderField(HTTPNames::Cache_Control,
689 "max-age=0"); 691 "max-age=0");
690 } 692 }
691 } 693 }
692 if (!lastModified.isEmpty()) { 694 if (!lastModified.isEmpty()) {
693 revalidatingRequest.setHTTPHeaderField(HTTPNames::If_Modified_Since, 695 revalidatingRequest.setHTTPHeaderField(HTTPNames::If_Modified_Since,
694 lastModified); 696 lastModified);
695 } 697 }
696 if (!eTag.isEmpty()) 698 if (!eTag.isEmpty())
697 revalidatingRequest.setHTTPHeaderField(HTTPNames::If_None_Match, eTag); 699 revalidatingRequest.setHTTPHeaderField(HTTPNames::If_None_Match, eTag);
(...skipping 158 matching lines...) Expand 10 before | Expand all | Expand 10 after
856 // with resourceNeedsLoad() so that it never actually goes to the network. 858 // with resourceNeedsLoad() so that it never actually goes to the network.
857 // 859 //
858 // 2. Images are enabled, but not loaded automatically. In this case, we will 860 // 2. Images are enabled, but not loaded automatically. In this case, we will
859 // Use cached resources or data: urls, but will similarly fall back to a 861 // Use cached resources or data: urls, but will similarly fall back to a
860 // deferred network load if we don't have the data available without a network 862 // deferred network load if we don't have the data available without a network
861 // request. We check allowImage() here, which is affected by m_imagesEnabled 863 // request. We check allowImage() here, which is affected by m_imagesEnabled
862 // but not m_autoLoadImages, in order to allow for this differing behavior. 864 // but not m_autoLoadImages, in order to allow for this differing behavior.
863 // 865 //
864 // TODO(japhet): Can we get rid of one of these settings? 866 // TODO(japhet): Can we get rid of one of these settings?
865 if (existingResource->isImage() && 867 if (existingResource->isImage() &&
866 !context().allowImage(m_imagesEnabled, existingResource->url())) 868 !context().allowImage(m_imagesEnabled, existingResource->url())) {
867 return Reload; 869 return Reload;
870 }
868 871
869 // Never use cache entries for downloadToFile / useStreamOnResponse requests. 872 // Never use cache entries for downloadToFile / useStreamOnResponse requests.
870 // The data will be delivered through other paths. 873 // The data will be delivered through other paths.
871 if (request.downloadToFile() || request.useStreamOnResponse()) 874 if (request.downloadToFile() || request.useStreamOnResponse())
872 return Reload; 875 return Reload;
873 876
874 // Never reuse opaque responses from a service worker for requests that are 877 // Never reuse opaque responses from a service worker for requests that are
875 // not no-cors. https://crbug.com/625575 878 // not no-cors. https://crbug.com/625575
876 if (existingResource->response().wasFetchedViaServiceWorker() && 879 if (existingResource->response().wasFetchedViaServiceWorker() &&
877 existingResource->response().serviceWorkerResponseType() == 880 existingResource->response().serviceWorkerResponseType() ==
878 WebServiceWorkerResponseTypeOpaque && 881 WebServiceWorkerResponseTypeOpaque &&
879 request.fetchRequestMode() != WebURLRequest::FetchRequestModeNoCORS) 882 request.fetchRequestMode() != WebURLRequest::FetchRequestModeNoCORS) {
880 return Reload; 883 return Reload;
884 }
881 885
882 // If resource was populated from a SubstituteData load or data: url, use it. 886 // If resource was populated from a SubstituteData load or data: url, use it.
883 if (isStaticData) 887 if (isStaticData)
884 return Use; 888 return Use;
885 889
886 if (!existingResource->canReuse(request)) 890 if (!existingResource->canReuse(request))
887 return Reload; 891 return Reload;
888 892
889 // Certain requests (e.g., XHRs) might have manually set headers that require 893 // Certain requests (e.g., XHRs) might have manually set headers that require
890 // revalidation. In theory, this should be a Revalidate case. In practice, the 894 // revalidation. In theory, this should be a Revalidate case. In practice, the
891 // MemoryCache revalidation path assumes a whole bunch of things about how 895 // MemoryCache revalidation path assumes a whole bunch of things about how
892 // revalidation works that manual headers violate, so punt to Reload instead. 896 // revalidation works that manual headers violate, so punt to Reload instead.
893 // 897 //
894 // Similarly, a request with manually added revalidation headers can lead to a 898 // Similarly, a request with manually added revalidation headers can lead to a
895 // 304 response for a request that wasn't flagged as a revalidation attempt. 899 // 304 response for a request that wasn't flagged as a revalidation attempt.
896 // Normally, successful revalidation will maintain the original response's 900 // Normally, successful revalidation will maintain the original response's
897 // status code, but for a manual revalidation the response code remains 304. 901 // status code, but for a manual revalidation the response code remains 304.
898 // In this case, the Resource likely has insufficient context to provide a 902 // In this case, the Resource likely has insufficient context to provide a
899 // useful cache hit or revalidation. See http://crbug.com/643659 903 // useful cache hit or revalidation. See http://crbug.com/643659
900 if (request.isConditional() || 904 if (request.isConditional() ||
901 existingResource->response().httpStatusCode() == 304) 905 existingResource->response().httpStatusCode() == 304) {
902 return Reload; 906 return Reload;
907 }
903 908
904 // Don't reload resources while pasting. 909 // Don't reload resources while pasting.
905 if (m_allowStaleResources) 910 if (m_allowStaleResources)
906 return Use; 911 return Use;
907 912
908 if (!fetchRequest.options().canReuseRequest(existingResource->options())) 913 if (!fetchRequest.options().canReuseRequest(existingResource->options()))
909 return Reload; 914 return Reload;
910 915
911 // Always use preloads. 916 // Always use preloads.
912 if (existingResource->isPreloaded()) 917 if (existingResource->isPreloaded())
913 return Use; 918 return Use;
914 919
915 // CachePolicyHistoryBuffer uses the cache no matter what. 920 // WebCachePolicy::ReturnCacheDataElseLoad uses the cache no matter what.
916 CachePolicy cachePolicy = context().getCachePolicy(); 921 if (request.getCachePolicy() == WebCachePolicy::ReturnCacheDataElseLoad)
917 if (cachePolicy == CachePolicyHistoryBuffer)
918 return Use; 922 return Use;
919 923
920 // Don't reuse resources with Cache-control: no-store. 924 // Don't reuse resources with Cache-control: no-store.
921 if (existingResource->hasCacheControlNoStoreHeader()) { 925 if (existingResource->hasCacheControlNoStoreHeader()) {
922 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy " 926 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
923 "reloading due to Cache-control: no-store."; 927 "reloading due to Cache-control: no-store.";
924 return Reload; 928 return Reload;
925 } 929 }
926 930
927 // If credentials were sent with the previous request and won't be with this 931 // If credentials were sent with the previous request and won't be with this
(...skipping 16 matching lines...) Expand all
944 // XHRs fall into this category and may have user-set Cache-Control: headers 948 // XHRs fall into this category and may have user-set Cache-Control: headers
945 // or other factors that require separate requests. 949 // or other factors that require separate requests.
946 if (type != Resource::Raw) { 950 if (type != Resource::Raw) {
947 if (!context().isLoadComplete() && 951 if (!context().isLoadComplete() &&
948 m_validatedURLs.contains(existingResource->url())) 952 m_validatedURLs.contains(existingResource->url()))
949 return Use; 953 return Use;
950 if (existingResource->isLoading()) 954 if (existingResource->isLoading())
951 return Use; 955 return Use;
952 } 956 }
953 957
954 if (request.getCachePolicy() == WebCachePolicy::BypassingCache) 958 // WebCachePolicy::BypassingCache always reloads
955 return Reload; 959 if (request.getCachePolicy() == WebCachePolicy::BypassingCache) {
956
957 // CachePolicyReload always reloads
958 if (cachePolicy == CachePolicyReload) {
959 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy " 960 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
960 "reloading due to CachePolicyReload."; 961 "reloading due to "
962 "WebCachePolicy::BypassingCache.";
961 return Reload; 963 return Reload;
962 } 964 }
963 965
964 // We'll try to reload the resource if it failed last time. 966 // We'll try to reload the resource if it failed last time.
965 if (existingResource->errorOccurred()) { 967 if (existingResource->errorOccurred()) {
966 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::" 968 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
967 "determineRevalidationPolicye reloading due " 969 "reloading due to resource being in the error "
968 "to resource being in the error state"; 970 "state";
969 return Reload; 971 return Reload;
970 } 972 }
971 973
972 // List of available images logic allows images to be re-used without cache 974 // List of available images logic allows images to be re-used without cache
973 // validation. We restrict this only to images from memory cache which are the 975 // validation. We restrict this only to images from memory cache which are the
974 // same as the version in the current document. 976 // same as the version in the current document.
975 if (type == Resource::Image && 977 if (type == Resource::Image &&
976 existingResource == cachedResource(request.url())) 978 existingResource == cachedResource(request.url())) {
977 return Use; 979 return Use;
980 }
978 981
979 if (existingResource->mustReloadDueToVaryHeader(request)) 982 if (existingResource->mustReloadDueToVaryHeader(request))
980 return Reload; 983 return Reload;
981 984
982 // If any of the redirects in the chain to loading the resource were not 985 // If any of the redirects in the chain to loading the resource were not
983 // cacheable, we cannot reuse our cached resource. 986 // cacheable, we cannot reuse our cached resource.
984 if (!existingResource->canReuseRedirectChain()) { 987 if (!existingResource->canReuseRedirectChain()) {
985 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy " 988 RESOURCE_LOADING_DVLOG(1) << "ResourceFetcher::determineRevalidationPolicy "
986 "reloading due to an uncacheable redirect"; 989 "reloading due to an uncacheable redirect";
987 return Reload; 990 return Reload;
988 } 991 }
989 992
990 // Check if the cache headers requires us to revalidate (cache expiration for 993 // Check if the cache headers requires us to revalidate (cache expiration for
991 // example). 994 // example).
992 if (cachePolicy == CachePolicyRevalidate || 995 if (request.getCachePolicy() == WebCachePolicy::ValidatingCacheData ||
993 existingResource->mustRevalidateDueToCacheHeaders() || 996 existingResource->mustRevalidateDueToCacheHeaders() ||
994 request.cacheControlContainsNoCache()) { 997 request.cacheControlContainsNoCache()) {
995 // See if the resource has usable ETag or Last-modified headers. If the page 998 // See if the resource has usable ETag or Last-modified headers. If the page
996 // is controlled by the ServiceWorker, we choose the Reload policy because 999 // is controlled by the ServiceWorker, we choose the Reload policy because
997 // the revalidation headers should not be exposed to the 1000 // the revalidation headers should not be exposed to the
998 // ServiceWorker.(crbug.com/429570) 1001 // ServiceWorker.(crbug.com/429570)
999 if (existingResource->canUseCacheValidator() && 1002 if (existingResource->canUseCacheValidator() &&
1000 !context().isControlledByServiceWorker()) { 1003 !context().isControlledByServiceWorker()) {
1001 // If the resource is already a cache validator but not started yet, the 1004 // If the resource is already a cache validator but not started yet, the
1002 // |Use| policy should be applied to subsequent requests. 1005 // |Use| policy should be applied to subsequent requests.
(...skipping 546 matching lines...) Expand 10 before | Expand all | Expand 10 after
1549 visitor->trace(m_context); 1552 visitor->trace(m_context);
1550 visitor->trace(m_archive); 1553 visitor->trace(m_archive);
1551 visitor->trace(m_loaders); 1554 visitor->trace(m_loaders);
1552 visitor->trace(m_nonBlockingLoaders); 1555 visitor->trace(m_nonBlockingLoaders);
1553 visitor->trace(m_documentResources); 1556 visitor->trace(m_documentResources);
1554 visitor->trace(m_preloads); 1557 visitor->trace(m_preloads);
1555 visitor->trace(m_resourceTimingInfoMap); 1558 visitor->trace(m_resourceTimingInfoMap);
1556 } 1559 }
1557 1560
1558 } // namespace blink 1561 } // namespace blink
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698