| 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
rights reserved. | |
| 5 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/ | |
| 6 | |
| 7 This library is free software; you can redistribute it and/or | |
| 8 modify it under the terms of the GNU Library General Public | |
| 9 License as published by the Free Software Foundation; either | |
| 10 version 2 of the License, or (at your option) any later version. | |
| 11 | |
| 12 This library is distributed in the hope that it will be useful, | |
| 13 but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 15 Library General Public License for more details. | |
| 16 | |
| 17 You should have received a copy of the GNU Library General Public License | |
| 18 along with this library; see the file COPYING.LIB. If not, write to | |
| 19 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 20 Boston, MA 02110-1301, USA. | |
| 21 | |
| 22 This class provides all functionality needed for loading images, style sheet
s and html | |
| 23 pages from the web. It has a memory cache for these objects. | |
| 24 */ | |
| 25 | |
| 26 #ifndef SKY_ENGINE_CORE_FETCH_RESOURCEFETCHER_H_ | |
| 27 #define SKY_ENGINE_CORE_FETCH_RESOURCEFETCHER_H_ | |
| 28 | |
| 29 #include "sky/engine/core/fetch/CachePolicy.h" | |
| 30 #include "sky/engine/core/fetch/FetchInitiatorInfo.h" | |
| 31 #include "sky/engine/core/fetch/FetchRequest.h" | |
| 32 #include "sky/engine/core/fetch/Resource.h" | |
| 33 #include "sky/engine/core/fetch/ResourceLoaderHost.h" | |
| 34 #include "sky/engine/core/fetch/ResourceLoaderOptions.h" | |
| 35 #include "sky/engine/core/fetch/ResourcePtr.h" | |
| 36 #include "sky/engine/platform/Timer.h" | |
| 37 #include "sky/engine/wtf/Deque.h" | |
| 38 #include "sky/engine/wtf/HashMap.h" | |
| 39 #include "sky/engine/wtf/HashSet.h" | |
| 40 #include "sky/engine/wtf/ListHashSet.h" | |
| 41 #include "sky/engine/wtf/text/StringHash.h" | |
| 42 | |
| 43 namespace blink { | |
| 44 | |
| 45 class FetchContext; | |
| 46 class FontResource; | |
| 47 class ImageResource; | |
| 48 class RawResource; | |
| 49 class Document; | |
| 50 class LocalFrame; | |
| 51 class FrameLoader; | |
| 52 class KURL; | |
| 53 class ResourceLoaderSet; | |
| 54 | |
| 55 // The ResourceFetcher provides a per-context interface to the MemoryCache | |
| 56 // and enforces a bunch of security checks and rules for resource revalidation. | |
| 57 // Its lifetime is roughly per-DocumentLoader, in that it is generally created | |
| 58 // in the DocumentLoader constructor and loses its ability to generate network | |
| 59 // requests when the DocumentLoader is destroyed. Documents also hold a | |
| 60 // RefPtr<ResourceFetcher> for their lifetime (and will create one if they | |
| 61 // are initialized without a LocalFrame), so a Document can keep a ResourceFetch
er | |
| 62 // alive past detach if scripts still reference the Document. | |
| 63 class ResourceFetcher final : public RefCounted<ResourceFetcher>, public Resourc
eLoaderHost { | |
| 64 WTF_MAKE_NONCOPYABLE(ResourceFetcher); WTF_MAKE_FAST_ALLOCATED; | |
| 65 friend class ResourceCacheValidationSuppressor; | |
| 66 | |
| 67 public: | |
| 68 static PassRefPtr<ResourceFetcher> create(Document* document) { return adopt
Ref(new ResourceFetcher(document)); } | |
| 69 virtual ~ResourceFetcher(); | |
| 70 | |
| 71 #if !ENABLE(OILPAN) | |
| 72 using RefCounted<ResourceFetcher>::ref; | |
| 73 using RefCounted<ResourceFetcher>::deref; | |
| 74 #endif | |
| 75 | |
| 76 ResourcePtr<ImageResource> fetchImage(FetchRequest&); | |
| 77 ResourcePtr<FontResource> fetchFont(FetchRequest&); | |
| 78 | |
| 79 // Logs an access denied message to the console for the specified URL. | |
| 80 void printAccessDeniedMessage(const KURL&) const; | |
| 81 | |
| 82 Resource* cachedResource(const KURL&) const; | |
| 83 | |
| 84 typedef HashMap<String, ResourcePtr<Resource> > DocumentResourceMap; | |
| 85 const DocumentResourceMap& allResources() const { return m_documentResources
; } | |
| 86 | |
| 87 bool autoLoadImages() const { return m_autoLoadImages; } | |
| 88 void setAutoLoadImages(bool); | |
| 89 | |
| 90 void setImagesEnabled(bool); | |
| 91 | |
| 92 bool shouldDeferImageLoad(const KURL&) const; | |
| 93 | |
| 94 LocalFrame* frame() const; // Can be null | |
| 95 FetchContext& context() const; | |
| 96 Document* document() const { return m_document; } // Can be null | |
| 97 | |
| 98 void garbageCollectDocumentResources(); | |
| 99 | |
| 100 int requestCount() const { return m_requestCount; } | |
| 101 | |
| 102 void stopFetching(); | |
| 103 bool isFetching() const; | |
| 104 | |
| 105 // ResourceLoaderHost | |
| 106 virtual void incrementRequestCount(const Resource*) override; | |
| 107 virtual void decrementRequestCount(const Resource*) override; | |
| 108 virtual void didLoadResource(Resource*) override; | |
| 109 virtual void didFinishLoading(const Resource*, double finishTime, int64_t en
codedDataLength) override; | |
| 110 virtual void didChangeLoadingPriority(const Resource*, ResourceLoadPriority,
int intraPriorityValue) override; | |
| 111 virtual void didFailLoading(const Resource*, const ResourceError&) override; | |
| 112 virtual void willSendRequest(unsigned long identifier, ResourceRequest&, con
st ResourceResponse& redirectResponse, const FetchInitiatorInfo&) override; | |
| 113 virtual void didReceiveResponse(const Resource*, const ResourceResponse&) ov
erride; | |
| 114 virtual void didReceiveData(const Resource*, const char* data, int dataLengt
h, int encodedDataLength) override; | |
| 115 virtual void didDownloadData(const Resource*, int dataLength, int encodedDat
aLength) override; | |
| 116 virtual void subresourceLoaderFinishedLoadingOnePart(ResourceLoader*) overri
de; | |
| 117 virtual void didInitializeResourceLoader(ResourceLoader*) override; | |
| 118 virtual void willTerminateResourceLoader(ResourceLoader*) override; | |
| 119 virtual void willStartLoadingResource(Resource*, ResourceRequest&) override; | |
| 120 virtual bool isLoadedBy(ResourceLoaderHost*) const override; | |
| 121 | |
| 122 #if !ENABLE(OILPAN) | |
| 123 virtual void refResourceLoaderHost() override; | |
| 124 virtual void derefResourceLoaderHost() override; | |
| 125 #endif | |
| 126 | |
| 127 enum ResourceLoadStartType { | |
| 128 ResourceLoadingFromNetwork, | |
| 129 ResourceLoadingFromCache | |
| 130 }; | |
| 131 void requestLoadStarted(Resource*, const FetchRequest&, ResourceLoadStartTyp
e); | |
| 132 static const ResourceLoaderOptions& defaultResourceOptions(); | |
| 133 private: | |
| 134 | |
| 135 explicit ResourceFetcher(Document*); | |
| 136 | |
| 137 bool shouldLoadNewResource(Resource::Type) const; | |
| 138 | |
| 139 ResourcePtr<Resource> requestResource(Resource::Type, FetchRequest&); | |
| 140 ResourcePtr<Resource> createResourceForRevalidation(const FetchRequest&, Res
ource*); | |
| 141 ResourcePtr<Resource> createResourceForLoading(Resource::Type, FetchRequest&
, const String& charset); | |
| 142 | |
| 143 enum RevalidationPolicy { Use, Revalidate, Reload, Load }; | |
| 144 RevalidationPolicy determineRevalidationPolicy(Resource::Type, const FetchRe
quest&, Resource* existingResource) const; | |
| 145 | |
| 146 void determineRequestContext(ResourceRequest&, Resource::Type); | |
| 147 ResourceRequestCachePolicy resourceRequestCachePolicy(const ResourceRequest&
, Resource::Type); | |
| 148 void addAdditionalRequestHeaders(ResourceRequest&, Resource::Type); | |
| 149 | |
| 150 bool canRequest(Resource::Type, const KURL&, const ResourceLoaderOptions&, F
etchRequest::OriginRestriction) const; | |
| 151 | |
| 152 static bool resourceNeedsLoad(Resource*, const FetchRequest&, RevalidationPo
licy); | |
| 153 | |
| 154 void notifyLoadedFromMemoryCache(Resource*); | |
| 155 | |
| 156 void garbageCollectDocumentResourcesTimerFired(Timer<ResourceFetcher>*); | |
| 157 void scheduleDocumentResourcesGC(); | |
| 158 | |
| 159 bool clientDefersImage(const KURL&) const; | |
| 160 void reloadImagesIfNotDeferred(); | |
| 161 | |
| 162 HashSet<String> m_validatedURLs; | |
| 163 mutable DocumentResourceMap m_documentResources; | |
| 164 Document* m_document; | |
| 165 | |
| 166 int m_requestCount; | |
| 167 | |
| 168 Timer<ResourceFetcher> m_garbageCollectDocumentResourcesTimer; | |
| 169 | |
| 170 OwnPtr<ResourceLoaderSet> m_loaders; | |
| 171 OwnPtr<ResourceLoaderSet> m_multipartLoaders; | |
| 172 | |
| 173 // Used in hit rate histograms. | |
| 174 class DeadResourceStatsRecorder { | |
| 175 public: | |
| 176 DeadResourceStatsRecorder(); | |
| 177 ~DeadResourceStatsRecorder(); | |
| 178 | |
| 179 void update(RevalidationPolicy); | |
| 180 | |
| 181 private: | |
| 182 int m_useCount; | |
| 183 int m_revalidateCount; | |
| 184 int m_loadCount; | |
| 185 }; | |
| 186 DeadResourceStatsRecorder m_deadStatsRecorder; | |
| 187 | |
| 188 // 29 bits left | |
| 189 bool m_autoLoadImages : 1; | |
| 190 bool m_imagesEnabled : 1; | |
| 191 bool m_allowStaleResources : 1; | |
| 192 }; | |
| 193 | |
| 194 class ResourceCacheValidationSuppressor { | |
| 195 WTF_MAKE_NONCOPYABLE(ResourceCacheValidationSuppressor); | |
| 196 WTF_MAKE_FAST_ALLOCATED; | |
| 197 public: | |
| 198 ResourceCacheValidationSuppressor(ResourceFetcher* loader) | |
| 199 : m_loader(loader) | |
| 200 , m_previousState(false) | |
| 201 { | |
| 202 if (m_loader) { | |
| 203 m_previousState = m_loader->m_allowStaleResources; | |
| 204 m_loader->m_allowStaleResources = true; | |
| 205 } | |
| 206 } | |
| 207 ~ResourceCacheValidationSuppressor() | |
| 208 { | |
| 209 if (m_loader) | |
| 210 m_loader->m_allowStaleResources = m_previousState; | |
| 211 } | |
| 212 private: | |
| 213 ResourceFetcher* m_loader; | |
| 214 bool m_previousState; | |
| 215 }; | |
| 216 | |
| 217 } // namespace blink | |
| 218 | |
| 219 #endif // SKY_ENGINE_CORE_FETCH_RESOURCEFETCHER_H_ | |
| OLD | NEW |