| OLD | NEW |
| (Empty) |
| 1 /* | |
| 2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de) | |
| 3 Copyright (C) 2001 Dirk Mueller <mueller@kde.org> | |
| 4 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All | |
| 5 rights reserved. | |
| 6 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ | |
| 7 | |
| 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. | |
| 12 | |
| 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. | |
| 17 | |
| 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. | |
| 22 | |
| 23 This class provides all functionality needed for loading images, style | |
| 24 sheets and html pages from the web. It has a memory cache for these objects. | |
| 25 */ | |
| 26 | |
| 27 #ifndef ResourceFetcher_h | |
| 28 #define ResourceFetcher_h | |
| 29 | |
| 30 #include "core/CoreExport.h" | |
| 31 #include "core/fetch/CachePolicy.h" | |
| 32 #include "core/fetch/FetchContext.h" | |
| 33 #include "core/fetch/FetchInitiatorInfo.h" | |
| 34 #include "core/fetch/FetchRequest.h" | |
| 35 #include "core/fetch/Resource.h" | |
| 36 #include "core/fetch/ResourceLoaderOptions.h" | |
| 37 #include "core/fetch/SubstituteData.h" | |
| 38 #include "platform/Timer.h" | |
| 39 #include "platform/network/ResourceError.h" | |
| 40 #include "platform/network/ResourceLoadPriority.h" | |
| 41 #include "wtf/HashMap.h" | |
| 42 #include "wtf/HashSet.h" | |
| 43 #include "wtf/ListHashSet.h" | |
| 44 #include "wtf/text/StringHash.h" | |
| 45 #include <memory> | |
| 46 | |
| 47 namespace blink { | |
| 48 | |
| 49 class ArchiveResource; | |
| 50 class MHTMLArchive; | |
| 51 class KURL; | |
| 52 class ResourceTimingInfo; | |
| 53 | |
| 54 // The ResourceFetcher provides a per-context interface to the MemoryCache and | |
| 55 // enforces a bunch of security checks and rules for resource revalidation. Its | |
| 56 // lifetime is roughly per-DocumentLoader, in that it is generally created in | |
| 57 // the DocumentLoader constructor and loses its ability to generate network | |
| 58 // requests when the DocumentLoader is destroyed. Documents also hold a pointer | |
| 59 // to ResourceFetcher for their lifetime (and will create one if they are | |
| 60 // initialized without a LocalFrame), so a Document can keep a ResourceFetcher | |
| 61 // alive past detach if scripts still reference the Document. | |
| 62 class CORE_EXPORT ResourceFetcher | |
| 63 : public GarbageCollectedFinalized<ResourceFetcher> { | |
| 64 WTF_MAKE_NONCOPYABLE(ResourceFetcher); | |
| 65 USING_PRE_FINALIZER(ResourceFetcher, clearPreloads); | |
| 66 | |
| 67 public: | |
| 68 static ResourceFetcher* create(FetchContext* context) { | |
| 69 return new ResourceFetcher(context); | |
| 70 } | |
| 71 virtual ~ResourceFetcher(); | |
| 72 DECLARE_VIRTUAL_TRACE(); | |
| 73 | |
| 74 Resource* requestResource(FetchRequest&, | |
| 75 const ResourceFactory&, | |
| 76 const SubstituteData& = SubstituteData()); | |
| 77 | |
| 78 Resource* cachedResource(const KURL&) const; | |
| 79 | |
| 80 using DocumentResourceMap = HeapHashMap<String, WeakMember<Resource>>; | |
| 81 const DocumentResourceMap& allResources() const { | |
| 82 return m_documentResources; | |
| 83 } | |
| 84 | |
| 85 // Actually starts loading a Resource if it wasn't started during | |
| 86 // requestResource(). | |
| 87 bool startLoad(Resource*); | |
| 88 | |
| 89 void setAutoLoadImages(bool); | |
| 90 void setImagesEnabled(bool); | |
| 91 | |
| 92 FetchContext& context() const { | |
| 93 return m_context ? *m_context.get() : FetchContext::nullInstance(); | |
| 94 } | |
| 95 void clearContext(); | |
| 96 | |
| 97 int requestCount() const; | |
| 98 bool hasPendingRequest() const; | |
| 99 | |
| 100 enum ClearPreloadsPolicy { ClearAllPreloads, ClearSpeculativeMarkupPreloads }; | |
| 101 | |
| 102 void enableIsPreloadedForTest(); | |
| 103 bool isPreloadedForTest(const KURL&) const; | |
| 104 | |
| 105 int countPreloads() const { return m_preloads ? m_preloads->size() : 0; } | |
| 106 void clearPreloads(ClearPreloadsPolicy = ClearAllPreloads); | |
| 107 void preloadStarted(Resource*); | |
| 108 void logPreloadStats(ClearPreloadsPolicy); | |
| 109 void warnUnusedPreloads(); | |
| 110 | |
| 111 MHTMLArchive* archive() const { return m_archive.get(); } | |
| 112 ArchiveResource* createArchive(Resource*); | |
| 113 | |
| 114 void setDefersLoading(bool); | |
| 115 void stopFetching(); | |
| 116 bool isFetching() const; | |
| 117 | |
| 118 bool shouldDeferImageLoad(const KURL&) const; | |
| 119 | |
| 120 void recordResourceTimingOnRedirect(Resource*, const ResourceResponse&, bool); | |
| 121 | |
| 122 enum LoaderFinishType { DidFinishLoading, DidFinishFirstPartInMultipart }; | |
| 123 void handleLoaderFinish(Resource*, double finishTime, LoaderFinishType); | |
| 124 void handleLoaderError(Resource*, const ResourceError&); | |
| 125 bool isControlledByServiceWorker() const; | |
| 126 | |
| 127 enum ResourceLoadStartType { | |
| 128 ResourceLoadingFromNetwork, | |
| 129 ResourceLoadingFromCache | |
| 130 }; | |
| 131 static const ResourceLoaderOptions& defaultResourceOptions(); | |
| 132 | |
| 133 String getCacheIdentifier() const; | |
| 134 | |
| 135 static void determineRequestContext(ResourceRequest&, | |
| 136 Resource::Type, | |
| 137 bool isMainFrame); | |
| 138 void determineRequestContext(ResourceRequest&, Resource::Type); | |
| 139 | |
| 140 void updateAllImageResourcePriorities(); | |
| 141 | |
| 142 void reloadLoFiImages(); | |
| 143 | |
| 144 // Calling this method before main document resource is fetched is invalid. | |
| 145 ResourceTimingInfo* getNavigationTimingInfo(); | |
| 146 | |
| 147 // This is only exposed for testing purposes. | |
| 148 HeapListHashSet<Member<Resource>>* preloads() { return m_preloads.get(); } | |
| 149 | |
| 150 // Workaround for https://crbug.com/666214. | |
| 151 // TODO(hiroshige): Remove this hack. | |
| 152 void emulateLoadStartedForInspector(Resource*, | |
| 153 const KURL&, | |
| 154 WebURLRequest::RequestContext, | |
| 155 const AtomicString& initiatorName); | |
| 156 | |
| 157 private: | |
| 158 friend class ResourceCacheValidationSuppressor; | |
| 159 | |
| 160 explicit ResourceFetcher(FetchContext*); | |
| 161 | |
| 162 void initializeRevalidation(ResourceRequest&, Resource*); | |
| 163 Resource* createResourceForLoading(FetchRequest&, | |
| 164 const String& charset, | |
| 165 const ResourceFactory&); | |
| 166 void storePerformanceTimingInitiatorInformation(Resource*); | |
| 167 ResourceLoadPriority computeLoadPriority(Resource::Type, | |
| 168 const FetchRequest&, | |
| 169 ResourcePriority::VisibilityStatus); | |
| 170 | |
| 171 Resource* resourceForStaticData(const FetchRequest&, | |
| 172 const ResourceFactory&, | |
| 173 const SubstituteData&); | |
| 174 Resource* resourceForBlockedRequest(const FetchRequest&, | |
| 175 const ResourceFactory&, | |
| 176 ResourceRequestBlockedReason); | |
| 177 | |
| 178 // RevalidationPolicy enum values are used in UMAs https://crbug.com/579496. | |
| 179 enum RevalidationPolicy { Use, Revalidate, Reload, Load }; | |
| 180 RevalidationPolicy determineRevalidationPolicy(Resource::Type, | |
| 181 const FetchRequest&, | |
| 182 Resource* existingResource, | |
| 183 bool isStaticData) const; | |
| 184 | |
| 185 void moveCachedNonBlockingResourceToBlocking(Resource*, const FetchRequest&); | |
| 186 void moveResourceLoaderToNonBlocking(ResourceLoader*); | |
| 187 void removeResourceLoader(ResourceLoader*); | |
| 188 void handleLoadCompletion(Resource*); | |
| 189 | |
| 190 void initializeResourceRequest(ResourceRequest&, | |
| 191 Resource::Type, | |
| 192 FetchRequest::DeferOption); | |
| 193 void requestLoadStarted(unsigned long identifier, | |
| 194 Resource*, | |
| 195 const FetchRequest&, | |
| 196 ResourceLoadStartType, | |
| 197 bool isStaticData = false); | |
| 198 | |
| 199 bool resourceNeedsLoad(Resource*, const FetchRequest&, RevalidationPolicy); | |
| 200 | |
| 201 void resourceTimingReportTimerFired(TimerBase*); | |
| 202 | |
| 203 void reloadImagesIfNotDeferred(); | |
| 204 | |
| 205 void updateMemoryCacheStats(Resource*, | |
| 206 RevalidationPolicy, | |
| 207 const FetchRequest&, | |
| 208 const ResourceFactory&, | |
| 209 bool isStaticData) const; | |
| 210 | |
| 211 Member<FetchContext> m_context; | |
| 212 | |
| 213 HashSet<String> m_validatedURLs; | |
| 214 mutable DocumentResourceMap m_documentResources; | |
| 215 | |
| 216 Member<HeapListHashSet<Member<Resource>>> m_preloads; | |
| 217 Member<MHTMLArchive> m_archive; | |
| 218 | |
| 219 TaskRunnerTimer<ResourceFetcher> m_resourceTimingReportTimer; | |
| 220 | |
| 221 using ResourceTimingInfoMap = | |
| 222 HeapHashMap<Member<Resource>, std::unique_ptr<ResourceTimingInfo>>; | |
| 223 ResourceTimingInfoMap m_resourceTimingInfoMap; | |
| 224 | |
| 225 std::unique_ptr<ResourceTimingInfo> m_navigationTimingInfo; | |
| 226 | |
| 227 Vector<std::unique_ptr<ResourceTimingInfo>> m_scheduledResourceTimingReports; | |
| 228 | |
| 229 HeapHashSet<Member<ResourceLoader>> m_loaders; | |
| 230 HeapHashSet<Member<ResourceLoader>> m_nonBlockingLoaders; | |
| 231 | |
| 232 // Used in hit rate histograms. | |
| 233 class DeadResourceStatsRecorder { | |
| 234 DISALLOW_NEW(); | |
| 235 | |
| 236 public: | |
| 237 DeadResourceStatsRecorder(); | |
| 238 ~DeadResourceStatsRecorder(); | |
| 239 | |
| 240 void update(RevalidationPolicy); | |
| 241 | |
| 242 private: | |
| 243 int m_useCount; | |
| 244 int m_revalidateCount; | |
| 245 int m_loadCount; | |
| 246 }; | |
| 247 DeadResourceStatsRecorder m_deadStatsRecorder; | |
| 248 | |
| 249 std::unique_ptr<HashSet<String>> m_preloadedURLsForTest; | |
| 250 | |
| 251 // 28 bits left | |
| 252 bool m_autoLoadImages : 1; | |
| 253 bool m_imagesEnabled : 1; | |
| 254 bool m_allowStaleResources : 1; | |
| 255 bool m_imageFetched : 1; | |
| 256 }; | |
| 257 | |
| 258 class ResourceCacheValidationSuppressor { | |
| 259 WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor); | |
| 260 STACK_ALLOCATED(); | |
| 261 | |
| 262 public: | |
| 263 explicit ResourceCacheValidationSuppressor(ResourceFetcher* loader) | |
| 264 : m_loader(loader), m_previousState(false) { | |
| 265 if (m_loader) { | |
| 266 m_previousState = m_loader->m_allowStaleResources; | |
| 267 m_loader->m_allowStaleResources = true; | |
| 268 } | |
| 269 } | |
| 270 ~ResourceCacheValidationSuppressor() { | |
| 271 if (m_loader) | |
| 272 m_loader->m_allowStaleResources = m_previousState; | |
| 273 } | |
| 274 | |
| 275 private: | |
| 276 Member<ResourceFetcher> m_loader; | |
| 277 bool m_previousState; | |
| 278 }; | |
| 279 | |
| 280 } // namespace blink | |
| 281 | |
| 282 #endif // ResourceFetcher_h | |
| OLD | NEW |