| 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) 2006 Samuel Weinig (sam.weinig@gmail.com) | |
| 5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All
rights reserved. | |
| 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 | |
| 23 #ifndef SKY_ENGINE_CORE_FETCH_RESOURCE_H_ | |
| 24 #define SKY_ENGINE_CORE_FETCH_RESOURCE_H_ | |
| 25 | |
| 26 #include "sky/engine/core/fetch/ResourceLoaderOptions.h" | |
| 27 #include "sky/engine/platform/Timer.h" | |
| 28 #include "sky/engine/platform/network/ResourceError.h" | |
| 29 #include "sky/engine/platform/network/ResourceLoadPriority.h" | |
| 30 #include "sky/engine/platform/network/ResourceRequest.h" | |
| 31 #include "sky/engine/platform/network/ResourceResponse.h" | |
| 32 #include "sky/engine/wtf/HashCountedSet.h" | |
| 33 #include "sky/engine/wtf/HashSet.h" | |
| 34 #include "sky/engine/wtf/OwnPtr.h" | |
| 35 #include "sky/engine/wtf/text/WTFString.h" | |
| 36 | |
| 37 // FIXME(crbug.com/352043): This is temporarily enabled even on RELEASE to diagn
ose a wild crash. | |
| 38 #define ENABLE_RESOURCE_IS_DELETED_CHECK | |
| 39 | |
| 40 namespace blink { | |
| 41 | |
| 42 struct FetchInitiatorInfo; | |
| 43 class MemoryCache; | |
| 44 class ResourceClient; | |
| 45 class ResourcePtrBase; | |
| 46 class ResourceFetcher; | |
| 47 class InspectorResource; | |
| 48 class ResourceLoader; | |
| 49 class SharedBuffer; | |
| 50 | |
| 51 // A resource that is held in the cache. Classes who want to use this object sho
uld derive | |
| 52 // from ResourceClient, to get the function calls in case the requested data has
arrived. | |
| 53 // This class also does the actual communication with the loader to obtain the r
esource from the network. | |
| 54 class Resource { | |
| 55 WTF_MAKE_NONCOPYABLE(Resource); WTF_MAKE_FAST_ALLOCATED; | |
| 56 friend class InspectorResource; | |
| 57 | |
| 58 public: | |
| 59 enum Type { | |
| 60 MainResource, | |
| 61 Image, | |
| 62 Font, | |
| 63 Raw, | |
| 64 LinkPrefetch, | |
| 65 LinkSubresource, | |
| 66 ImportResource, | |
| 67 Media // Audio or video file requested by a HTML5 media element | |
| 68 }; | |
| 69 | |
| 70 enum Status { | |
| 71 Unknown, // let cache decide what to do with it | |
| 72 Pending, // only partially loaded | |
| 73 Cached, // regular case | |
| 74 LoadError, | |
| 75 DecodeError | |
| 76 }; | |
| 77 | |
| 78 Resource(const ResourceRequest&, Type); | |
| 79 #if ENABLE(OILPAN) | |
| 80 virtual ~Resource(); | |
| 81 #else | |
| 82 protected: | |
| 83 // Only deleteIfPossible should delete this. | |
| 84 virtual ~Resource(); | |
| 85 public: | |
| 86 #endif | |
| 87 virtual void dispose(); | |
| 88 static unsigned instanceCount() { return s_instanceCount; } | |
| 89 | |
| 90 virtual void load(ResourceFetcher*, const ResourceLoaderOptions&); | |
| 91 | |
| 92 virtual void setEncoding(const String&) { } | |
| 93 virtual String encoding() const { return String(); } | |
| 94 virtual void appendData(const char*, int); | |
| 95 virtual void error(Resource::Status); | |
| 96 | |
| 97 void setNeedsSynchronousCacheHit(bool needsSynchronousCacheHit) { m_needsSyn
chronousCacheHit = needsSynchronousCacheHit; } | |
| 98 | |
| 99 void setResourceError(const ResourceError& error) { m_error = error; } | |
| 100 const ResourceError& resourceError() const { return m_error; } | |
| 101 | |
| 102 void setIdentifier(unsigned long identifier) { m_identifier = identifier; } | |
| 103 unsigned long identifier() const { return m_identifier; } | |
| 104 | |
| 105 virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; } | |
| 106 | |
| 107 ResourceRequest& mutableResourceRequest() { return m_resourceRequest; } | |
| 108 const ResourceRequest& resourceRequest() const { return m_resourceRequest; } | |
| 109 | |
| 110 const KURL& url() const { return m_resourceRequest.url();} | |
| 111 Type type() const { return static_cast<Type>(m_type); } | |
| 112 const ResourceLoaderOptions& options() const { return m_options; } | |
| 113 void setOptions(const ResourceLoaderOptions& options) { m_options = options;
} | |
| 114 | |
| 115 void didChangePriority(ResourceLoadPriority, int intraPriorityValue); | |
| 116 | |
| 117 void addClient(ResourceClient*); | |
| 118 void removeClient(ResourceClient*); | |
| 119 bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingC
allback.isEmpty(); } | |
| 120 bool deleteIfPossible(); | |
| 121 | |
| 122 virtual void didAddClient(ResourceClient*); | |
| 123 virtual void didRemoveClient(ResourceClient*) { } | |
| 124 virtual void allClientsRemoved(); | |
| 125 | |
| 126 unsigned count() const { return m_clients.size(); } | |
| 127 | |
| 128 Status status() const { return static_cast<Status>(m_status); } | |
| 129 void setStatus(Status status) { m_status = status; } | |
| 130 | |
| 131 size_t size() const { return encodedSize() + decodedSize() + overheadSize();
} | |
| 132 size_t encodedSize() const { return m_encodedSize; } | |
| 133 size_t decodedSize() const { return m_decodedSize; } | |
| 134 size_t overheadSize() const; | |
| 135 | |
| 136 bool isLoaded() const { return !m_loading; } // FIXME. Method name is inaccu
rate. Loading might not have started yet. | |
| 137 | |
| 138 bool isLoading() const { return m_loading; } | |
| 139 void setLoading(bool b) { m_loading = b; } | |
| 140 virtual bool stillNeedsLoad() const { return false; } | |
| 141 | |
| 142 ResourceLoader* loader() const { return m_loader.get(); } | |
| 143 | |
| 144 virtual bool isImage() const { return false; } | |
| 145 bool ignoreForRequestCount() const | |
| 146 { | |
| 147 return type() == MainResource | |
| 148 || type() == LinkPrefetch | |
| 149 || type() == LinkSubresource | |
| 150 || type() == Media | |
| 151 || type() == Raw; | |
| 152 } | |
| 153 | |
| 154 // Computes the status of an object after loading. | |
| 155 // Updates the expire date on the cache entry file | |
| 156 void finish(double finishTime = 0.0); | |
| 157 | |
| 158 void clearLoader(); | |
| 159 | |
| 160 SharedBuffer* resourceBuffer() const { return m_data.get(); } | |
| 161 void setResourceBuffer(PassRefPtr<SharedBuffer>); | |
| 162 | |
| 163 virtual void willSendRequest(ResourceRequest&, const ResourceResponse&); | |
| 164 | |
| 165 virtual void updateRequest(const ResourceRequest&) { } | |
| 166 virtual void responseReceived(const ResourceResponse&); | |
| 167 void setResponse(const ResourceResponse& response) { m_response = response;
} | |
| 168 const ResourceResponse& response() const { return m_response; } | |
| 169 | |
| 170 bool hasOneHandle() const; | |
| 171 bool canDelete() const; | |
| 172 | |
| 173 // List of acceptable MIME types separated by ",". | |
| 174 // A MIME type may contain a wildcard, e.g. "text/*". | |
| 175 AtomicString accept() const { return m_accept; } | |
| 176 void setAccept(const AtomicString& accept) { m_accept = accept; } | |
| 177 | |
| 178 bool wasCanceled() const { return m_error.isCancellation(); } | |
| 179 bool errorOccurred() const { return m_status == LoadError || m_status == Dec
odeError; } | |
| 180 bool loadFailedOrCanceled() { return !m_error.isNull(); } | |
| 181 | |
| 182 DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBuffe
ringPolicy; } | |
| 183 void setDataBufferingPolicy(DataBufferingPolicy); | |
| 184 | |
| 185 void registerHandle(ResourcePtrBase* h); | |
| 186 void unregisterHandle(ResourcePtrBase* h); | |
| 187 | |
| 188 bool mustRevalidateDueToCacheHeaders(); | |
| 189 bool canUseCacheValidator(); | |
| 190 bool isCacheValidator() const { return m_resourceToRevalidate; } | |
| 191 Resource* resourceToRevalidate() const { return m_resourceToRevalidate; } | |
| 192 void setResourceToRevalidate(Resource*); | |
| 193 bool hasCacheControlNoStoreHeader(); | |
| 194 | |
| 195 bool isPurgeable() const; | |
| 196 bool wasPurged() const; | |
| 197 bool lock(); | |
| 198 | |
| 199 virtual void didSendData(unsigned long long /* bytesSent */, unsigned long l
ong /* totalBytesToBeSent */) { } | |
| 200 virtual void didDownloadData(int) { } | |
| 201 | |
| 202 double loadFinishTime() const { return m_loadFinishTime; } | |
| 203 | |
| 204 virtual bool canReuse(const ResourceRequest&) const { return true; } | |
| 205 | |
| 206 // Used by the MemoryCache to reduce the memory consumption of the entry. | |
| 207 void prune(); | |
| 208 | |
| 209 static const char* resourceTypeToString(Type, const FetchInitiatorInfo&); | |
| 210 | |
| 211 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK | |
| 212 void assertAlive() const { RELEASE_ASSERT(!m_deleted); } | |
| 213 #else | |
| 214 void assertAlive() const { } | |
| 215 #endif | |
| 216 | |
| 217 protected: | |
| 218 virtual void checkNotify(); | |
| 219 virtual void finishOnePart(); | |
| 220 | |
| 221 // Normal resource pointers will silently switch what Resource* they referen
ce when we | |
| 222 // successfully revalidated the resource. We need a way to guarantee that th
e Resource | |
| 223 // that received the 304 response survives long enough to switch everything
over to the | |
| 224 // revalidatedresource. The normal mechanisms for keeping a Resource alive e
xternally | |
| 225 // (ResourcePtrs and ResourceClients registering themselves) don't work in t
his case, so | |
| 226 // have a separate internal protector). | |
| 227 class InternalResourcePtr { | |
| 228 public: | |
| 229 explicit InternalResourcePtr(Resource* resource) | |
| 230 : m_resource(resource) | |
| 231 { | |
| 232 m_resource->incrementProtectorCount(); | |
| 233 } | |
| 234 | |
| 235 ~InternalResourcePtr() | |
| 236 { | |
| 237 m_resource->decrementProtectorCount(); | |
| 238 m_resource->deleteIfPossible(); | |
| 239 } | |
| 240 private: | |
| 241 Resource* m_resource; | |
| 242 }; | |
| 243 | |
| 244 void incrementProtectorCount() { m_protectorCount++; } | |
| 245 void decrementProtectorCount() { m_protectorCount--; } | |
| 246 | |
| 247 void setEncodedSize(size_t); | |
| 248 void setDecodedSize(size_t); | |
| 249 void didAccessDecodedData(); | |
| 250 | |
| 251 virtual void switchClientsToRevalidatedResource(); | |
| 252 void clearResourceToRevalidate(); | |
| 253 void updateResponseAfterRevalidation(const ResourceResponse& validatingRespo
nse); | |
| 254 | |
| 255 void finishPendingClients(); | |
| 256 | |
| 257 HashCountedSet<ResourceClient*> m_clients; | |
| 258 HashCountedSet<ResourceClient*> m_clientsAwaitingCallback; | |
| 259 | |
| 260 class ResourceCallback { | |
| 261 public: | |
| 262 static ResourceCallback* callbackHandler(); | |
| 263 void schedule(Resource*); | |
| 264 void cancel(Resource*); | |
| 265 bool isScheduled(Resource*) const; | |
| 266 private: | |
| 267 ResourceCallback(); | |
| 268 void timerFired(Timer<ResourceCallback>*); | |
| 269 Timer<ResourceCallback> m_callbackTimer; | |
| 270 HashSet<Resource*> m_resourcesWithPendingClients; | |
| 271 }; | |
| 272 | |
| 273 bool hasClient(ResourceClient* client) { return m_clients.contains(client) |
| m_clientsAwaitingCallback.contains(client); } | |
| 274 | |
| 275 virtual bool isSafeToUnlock() const { return false; } | |
| 276 virtual void destroyDecodedDataIfPossible() { } | |
| 277 | |
| 278 ResourceRequest m_resourceRequest; | |
| 279 AtomicString m_accept; | |
| 280 RefPtr<ResourceLoader> m_loader; | |
| 281 ResourceLoaderOptions m_options; | |
| 282 | |
| 283 ResourceResponse m_response; | |
| 284 double m_responseTimestamp; | |
| 285 | |
| 286 RefPtr<SharedBuffer> m_data; | |
| 287 Timer<Resource> m_cancelTimer; | |
| 288 | |
| 289 private: | |
| 290 bool addClientToSet(ResourceClient*); | |
| 291 void cancelTimerFired(Timer<Resource>*); | |
| 292 | |
| 293 void revalidationSucceeded(const ResourceResponse&); | |
| 294 void revalidationFailed(); | |
| 295 | |
| 296 bool unlock(); | |
| 297 | |
| 298 bool hasRightHandleCountApartFromCache(unsigned targetCount) const; | |
| 299 | |
| 300 void failBeforeStarting(); | |
| 301 | |
| 302 String m_fragmentIdentifierForRequest; | |
| 303 | |
| 304 ResourceError m_error; | |
| 305 | |
| 306 double m_loadFinishTime; | |
| 307 | |
| 308 unsigned long m_identifier; | |
| 309 | |
| 310 size_t m_encodedSize; | |
| 311 size_t m_decodedSize; | |
| 312 unsigned m_handleCount; | |
| 313 unsigned m_protectorCount; | |
| 314 | |
| 315 unsigned m_requestedFromNetworkingLayer : 1; | |
| 316 | |
| 317 unsigned m_loading : 1; | |
| 318 | |
| 319 unsigned m_switchingClientsToRevalidatedResource : 1; | |
| 320 | |
| 321 unsigned m_type : 4; // Type | |
| 322 unsigned m_status : 3; // Status | |
| 323 | |
| 324 unsigned m_wasPurged : 1; | |
| 325 | |
| 326 unsigned m_needsSynchronousCacheHit : 1; | |
| 327 | |
| 328 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK | |
| 329 bool m_deleted; | |
| 330 #endif | |
| 331 | |
| 332 // If this field is non-null we are using the resource as a proxy for checki
ng whether an existing resource is still up to date | |
| 333 // using HTTP If-Modified-Since/If-None-Match headers. If the response is 30
4 all clients of this resource are moved | |
| 334 // to to be clients of m_resourceToRevalidate and the resource is deleted. I
f not, the field is zeroed and this | |
| 335 // resources becomes normal resource load. | |
| 336 RawPtr<Resource> m_resourceToRevalidate; | |
| 337 | |
| 338 // If this field is non-null, the resource has a proxy for checking whether
it is still up to date (see m_resourceToRevalidate). | |
| 339 RawPtr<Resource> m_proxyResource; | |
| 340 | |
| 341 // These handles will need to be updated to point to the m_resourceToRevalid
ate in case we get 304 response. | |
| 342 HashSet<ResourcePtrBase*> m_handlesToRevalidate; | |
| 343 | |
| 344 static unsigned s_instanceCount; | |
| 345 }; | |
| 346 | |
| 347 #if !LOG_DISABLED | |
| 348 // Intended to be used in LOG statements. | |
| 349 const char* ResourceTypeName(Resource::Type); | |
| 350 #endif | |
| 351 | |
| 352 #define DEFINE_RESOURCE_TYPE_CASTS(typeName) \ | |
| 353 DEFINE_TYPE_CASTS(typeName##Resource, Resource, resource, resource->type() =
= Resource::typeName, resource.type() == Resource::typeName); \ | |
| 354 inline typeName##Resource* to##typeName##Resource(const ResourcePtr<Resource
>& ptr) { return to##typeName##Resource(ptr.get()); } | |
| 355 | |
| 356 } | |
| 357 | |
| 358 #endif // SKY_ENGINE_CORE_FETCH_RESOURCE_H_ | |
| OLD | NEW |