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

Side by Side Diff: third_party/WebKit/Source/core/fetch/Resource.h

Issue 1667843003: Make Resource RefCountedWillBeGarbageCollectedFinalized, attempt #2 (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Rebase + address review comments Created 4 years, 10 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) 2006 Samuel Weinig (sam.weinig@gmail.com) 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. 5 Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011 Apple Inc. All rights reserved.
6 6
7 This library is free software; you can redistribute it and/or 7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public 8 modify it under the terms of the GNU Library General Public
9 License as published by the Free Software Foundation; either 9 License as published by the Free Software Foundation; either
10 version 2 of the License, or (at your option) any later version. 10 version 2 of the License, or (at your option) any later version.
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
42 42
43 // FIXME(crbug.com/352043): This is temporarily enabled even on RELEASE to diagn ose a wild crash. 43 // FIXME(crbug.com/352043): This is temporarily enabled even on RELEASE to diagn ose a wild crash.
44 #define ENABLE_RESOURCE_IS_DELETED_CHECK 44 #define ENABLE_RESOURCE_IS_DELETED_CHECK
45 45
46 namespace blink { 46 namespace blink {
47 47
48 struct FetchInitiatorInfo; 48 struct FetchInitiatorInfo;
49 class CachedMetadata; 49 class CachedMetadata;
50 class FetchRequest; 50 class FetchRequest;
51 class ResourceClient; 51 class ResourceClient;
52 class ResourcePtrBase;
53 class ResourceFetcher; 52 class ResourceFetcher;
54 class ResourceTimingInfo; 53 class ResourceTimingInfo;
55 class InspectorResource; 54 class InspectorResource;
56 class ResourceLoader; 55 class ResourceLoader;
57 class SecurityOrigin; 56 class SecurityOrigin;
58 class SharedBuffer; 57 class SharedBuffer;
59 58
60 // A resource that is held in the cache. Classes who want to use this object sho uld derive 59 // A resource that is held in the cache. Classes who want to use this object sho uld derive
61 // from ResourceClient, to get the function calls in case the requested data has arrived. 60 // from ResourceClient, to get the function calls in case the requested data has arrived.
62 // This class also does the actual communication with the loader to obtain the r esource from the network. 61 // This class also does the actual communication with the loader to obtain the r esource from the network.
63 class CORE_EXPORT Resource : public NoBaseWillBeGarbageCollectedFinalized<Resour ce> { 62 class CORE_EXPORT Resource : public RefCountedWillBeGarbageCollectedFinalized<Re source> {
64 WTF_MAKE_NONCOPYABLE(Resource); 63 WTF_MAKE_NONCOPYABLE(Resource);
65 USING_FAST_MALLOC_WITH_TYPE_NAME_WILL_BE_REMOVED(blink::Resource); 64 USING_FAST_MALLOC_WITH_TYPE_NAME_WILL_BE_REMOVED(blink::Resource);
66 friend class InspectorResource; 65 friend class InspectorResource;
67 66
68 public: 67 public:
69 enum Type { 68 enum Type {
70 MainResource, 69 MainResource,
71 Image, 70 Image,
72 CSSStyleSheet, 71 CSSStyleSheet,
73 Script, 72 Script,
(...skipping 11 matching lines...) Expand all
85 84
86 enum Status { 85 enum Status {
87 Unknown, // let cache decide what to do with it 86 Unknown, // let cache decide what to do with it
88 Pending, // only partially loaded 87 Pending, // only partially loaded
89 Cached, // regular case 88 Cached, // regular case
90 LoadError, 89 LoadError,
91 DecodeError 90 DecodeError
92 }; 91 };
93 92
94 // Exposed for testing. 93 // Exposed for testing.
95 Resource(const ResourceRequest&, Type); 94 static PassRefPtrWillBeRawPtr<Resource> create(const ResourceRequest& reques t, Type type)
96 #if ENABLE(OILPAN) 95 {
96 return adoptRefWillBeNoop(new Resource(request, type));
97 }
97 virtual ~Resource(); 98 virtual ~Resource();
98 #else 99
99 protected: 100 virtual void removedFromMemoryCache();
100 // Only deleteIfPossible should delete this.
101 virtual ~Resource();
102 public:
103 #endif
104 virtual void dispose();
105 DECLARE_VIRTUAL_TRACE(); 101 DECLARE_VIRTUAL_TRACE();
106 102
107 virtual void load(ResourceFetcher*, const ResourceLoaderOptions&); 103 virtual void load(ResourceFetcher*, const ResourceLoaderOptions&);
108 104
109 virtual void setEncoding(const String&) { } 105 virtual void setEncoding(const String&) { }
110 virtual String encoding() const { return String(); } 106 virtual String encoding() const { return String(); }
111 virtual void appendData(const char*, size_t); 107 virtual void appendData(const char*, size_t);
112 virtual void error(Resource::Status); 108 virtual void error(Resource::Status);
113 virtual void setCORSFailed() { } 109 virtual void setCORSFailed() { }
114 110
(...skipping 19 matching lines...) Expand all
134 Type type() const { return static_cast<Type>(m_type); } 130 Type type() const { return static_cast<Type>(m_type); }
135 const ResourceLoaderOptions& options() const { return m_options; } 131 const ResourceLoaderOptions& options() const { return m_options; }
136 void setOptions(const ResourceLoaderOptions& options) { m_options = options; } 132 void setOptions(const ResourceLoaderOptions& options) { m_options = options; }
137 133
138 void didChangePriority(ResourceLoadPriority, int intraPriorityValue); 134 void didChangePriority(ResourceLoadPriority, int intraPriorityValue);
139 ResourcePriority priorityFromClients(); 135 ResourcePriority priorityFromClients();
140 136
141 void addClient(ResourceClient*); 137 void addClient(ResourceClient*);
142 void removeClient(ResourceClient*); 138 void removeClient(ResourceClient*);
143 bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingC allback.isEmpty() || !m_finishedClients.isEmpty(); } 139 bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingC allback.isEmpty() || !m_finishedClients.isEmpty(); }
144 bool deleteIfPossible();
145 140
146 enum PreloadResult { 141 enum PreloadResult {
147 PreloadNotReferenced, 142 PreloadNotReferenced,
148 PreloadReferenced, 143 PreloadReferenced,
149 PreloadReferencedWhileLoading, 144 PreloadReferencedWhileLoading,
150 PreloadReferencedWhileComplete 145 PreloadReferencedWhileComplete
151 }; 146 };
152 PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_pr eloadResult); } 147 PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_pr eloadResult); }
153 148
154 virtual void didAddClient(ResourceClient*); 149 virtual void didAddClient(ResourceClient*);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
201 const ResourceResponse& response() const { return m_response; } 196 const ResourceResponse& response() const { return m_response; }
202 197
203 virtual void reportResourceTimingToClients(const ResourceTimingInfo&) { } 198 virtual void reportResourceTimingToClients(const ResourceTimingInfo&) { }
204 199
205 // Sets the serialized metadata retrieved from the platform's cache. 200 // Sets the serialized metadata retrieved from the platform's cache.
206 virtual void setSerializedCachedMetadata(const char*, size_t); 201 virtual void setSerializedCachedMetadata(const char*, size_t);
207 202
208 // This may return nullptr when the resource isn't cacheable. 203 // This may return nullptr when the resource isn't cacheable.
209 CachedMetadataHandler* cacheHandler(); 204 CachedMetadataHandler* cacheHandler();
210 205
211 bool hasOneHandle() const;
212 bool canDelete() const;
213 String reasonNotDeletable() const; 206 String reasonNotDeletable() const;
214 207
215 // List of acceptable MIME types separated by ",". 208 // List of acceptable MIME types separated by ",".
216 // A MIME type may contain a wildcard, e.g. "text/*". 209 // A MIME type may contain a wildcard, e.g. "text/*".
217 AtomicString accept() const { return m_accept; } 210 AtomicString accept() const { return m_accept; }
218 void setAccept(const AtomicString& accept) { m_accept = accept; } 211 void setAccept(const AtomicString& accept) { m_accept = accept; }
219 212
220 bool wasCanceled() const { return m_error.isCancellation(); } 213 bool wasCanceled() const { return m_error.isCancellation(); }
221 bool errorOccurred() const { return m_status == LoadError || m_status == Dec odeError; } 214 bool errorOccurred() const { return m_status == LoadError || m_status == Dec odeError; }
222 bool loadFailedOrCanceled() { return !m_error.isNull(); } 215 bool loadFailedOrCanceled() { return !m_error.isNull(); }
223 216
224 DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBuffe ringPolicy; } 217 DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBuffe ringPolicy; }
225 void setDataBufferingPolicy(DataBufferingPolicy); 218 void setDataBufferingPolicy(DataBufferingPolicy);
226 219
227 bool isUnusedPreload() const { return isPreloaded() && preloadResult() == Pr eloadNotReferenced; } 220 bool isUnusedPreload() const { return isPreloaded() && preloadResult() == Pr eloadNotReferenced; }
228 bool isPreloaded() const { return m_preloadCount; } 221 bool isPreloaded() const { return m_preloadCount; }
229 void increasePreloadCount() { ++m_preloadCount; } 222 void increasePreloadCount() { ++m_preloadCount; }
230 void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; } 223 void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
231 224
232 void registerHandle(ResourcePtrBase* h);
233 void unregisterHandle(ResourcePtrBase* h);
234
235 bool canReuseRedirectChain(); 225 bool canReuseRedirectChain();
236 bool mustRevalidateDueToCacheHeaders(); 226 bool mustRevalidateDueToCacheHeaders();
237 bool canUseCacheValidator(); 227 bool canUseCacheValidator();
238 bool isCacheValidator() const { return !m_revalidatingRequest.isNull(); } 228 bool isCacheValidator() const { return !m_revalidatingRequest.isNull(); }
239 bool hasCacheControlNoStoreHeader(); 229 bool hasCacheControlNoStoreHeader();
240 bool hasVaryHeader() const; 230 bool hasVaryHeader() const;
241 virtual bool mustRefetchDueToIntegrityMetadata(const FetchRequest& request) const { return false; } 231 virtual bool mustRefetchDueToIntegrityMetadata(const FetchRequest& request) const { return false; }
242 232
243 double currentAge() const; 233 double currentAge() const;
244 double freshnessLifetime(); 234 double freshnessLifetime();
245 double stalenessLifetime(); 235 double stalenessLifetime();
246 236
247 bool isPurgeable() const; 237 bool isPurgeable() const;
248 bool wasPurged() const;
249 bool lock(); 238 bool lock();
250 239
251 void setCacheIdentifier(const String& cacheIdentifier) { m_cacheIdentifier = cacheIdentifier; } 240 void setCacheIdentifier(const String& cacheIdentifier) { m_cacheIdentifier = cacheIdentifier; }
252 String cacheIdentifier() const { return m_cacheIdentifier; } 241 String cacheIdentifier() const { return m_cacheIdentifier; }
253 242
254 virtual void didSendData(unsigned long long /* bytesSent */, unsigned long l ong /* totalBytesToBeSent */) { } 243 virtual void didSendData(unsigned long long /* bytesSent */, unsigned long l ong /* totalBytesToBeSent */) { }
255 virtual void didDownloadData(int) { } 244 virtual void didDownloadData(int) { }
256 245
257 double loadFinishTime() const { return m_loadFinishTime; } 246 double loadFinishTime() const { return m_loadFinishTime; }
258 247
(...skipping 10 matching lines...) Expand all
269 // TODO(japhet): Remove once oilpan ships, it doesn't need the WeakPtr. 258 // TODO(japhet): Remove once oilpan ships, it doesn't need the WeakPtr.
270 WeakPtrWillBeRawPtr<Resource> asWeakPtr(); 259 WeakPtrWillBeRawPtr<Resource> asWeakPtr();
271 260
272 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK 261 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
273 void assertAlive() const { RELEASE_ASSERT(!m_deleted); } 262 void assertAlive() const { RELEASE_ASSERT(!m_deleted); }
274 #else 263 #else
275 void assertAlive() const { } 264 void assertAlive() const { }
276 #endif 265 #endif
277 266
278 protected: 267 protected:
268 Resource(const ResourceRequest&, Type);
269
279 virtual void checkNotify(); 270 virtual void checkNotify();
280 virtual void finishOnePart(); 271 virtual void finishOnePart();
281 272
282 virtual void destroyDecodedDataForFailedRevalidation() { } 273 virtual void destroyDecodedDataForFailedRevalidation() { }
283 274
284 // Normal resource pointers will silently switch what Resource* they referen ce when we
285 // successfully revalidated the resource. We need a way to guarantee that th e Resource
286 // that received the 304 response survives long enough to switch everything over to the
287 // revalidatedresource. The normal mechanisms for keeping a Resource alive e xternally
288 // (ResourcePtrs and ResourceClients registering themselves) don't work in t his case, so
289 // have a separate internal protector).
290 class InternalResourcePtr {
291 STACK_ALLOCATED();
292 public:
293 explicit InternalResourcePtr(Resource* resource)
294 : m_resource(resource)
295 {
296 m_resource->incrementProtectorCount();
297 }
298
299 ~InternalResourcePtr()
300 {
301 m_resource->decrementProtectorCount();
302 m_resource->deleteIfPossible();
303 }
304 private:
305 RawPtrWillBeMember<Resource> m_resource;
306 };
307
308 void incrementProtectorCount() { m_protectorCount++; }
309 void decrementProtectorCount() { m_protectorCount--; }
310
311 void setEncodedSize(size_t); 275 void setEncodedSize(size_t);
312 void setDecodedSize(size_t); 276 void setDecodedSize(size_t);
313 void didAccessDecodedData(); 277 void didAccessDecodedData();
314 278
315 void finishPendingClients(); 279 void finishPendingClients();
316 280
317 HashCountedSet<ResourceClient*> m_clients; 281 HashCountedSet<ResourceClient*> m_clients;
318 HashCountedSet<ResourceClient*> m_clientsAwaitingCallback; 282 HashCountedSet<ResourceClient*> m_clientsAwaitingCallback;
319 HashCountedSet<ResourceClient*> m_finishedClients; 283 HashCountedSet<ResourceClient*> m_finishedClients;
320 284
321 class ResourceCallback : public NoBaseWillBeGarbageCollectedFinalized<Resour ceCallback> { 285 class ResourceCallback : public NoBaseWillBeGarbageCollectedFinalized<Resour ceCallback> {
322 public: 286 public:
323 static ResourceCallback* callbackHandler(); 287 static ResourceCallback* callbackHandler();
324 DECLARE_TRACE(); 288 DECLARE_TRACE();
325 void schedule(Resource*); 289 void schedule(Resource*);
326 void cancel(Resource*); 290 void cancel(Resource*);
327 bool isScheduled(Resource*) const; 291 bool isScheduled(Resource*) const;
328 private: 292 private:
329 ResourceCallback(); 293 ResourceCallback();
330 void runTask(); 294 void runTask();
331 OwnPtr<CancellableTaskFactory> m_callbackTaskFactory; 295 OwnPtr<CancellableTaskFactory> m_callbackTaskFactory;
332 WillBeHeapHashSet<RawPtrWillBeMember<Resource>> m_resourcesWithPendingCl ients; 296 WillBeHeapHashSet<RefPtrWillBeMember<Resource>> m_resourcesWithPendingCl ients;
333 }; 297 };
334 298
335 bool hasClient(ResourceClient* client) { return m_clients.contains(client) | | m_clientsAwaitingCallback.contains(client) || m_finishedClients.contains(clien t); } 299 bool hasClient(ResourceClient* client) { return m_clients.contains(client) | | m_clientsAwaitingCallback.contains(client) || m_finishedClients.contains(clien t); }
336 300
337 struct RedirectPair { 301 struct RedirectPair {
338 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW(); 302 DISALLOW_NEW_EXCEPT_PLACEMENT_NEW();
339 public: 303 public:
340 explicit RedirectPair(const ResourceRequest& request, const ResourceResp onse& redirectResponse) 304 explicit RedirectPair(const ResourceRequest& request, const ResourceResp onse& redirectResponse)
341 : m_request(request) 305 : m_request(request)
342 , m_redirectResponse(redirectResponse) 306 , m_redirectResponse(redirectResponse)
(...skipping 28 matching lines...) Expand all
371 private: 335 private:
372 class CacheHandler; 336 class CacheHandler;
373 void cancelTimerFired(Timer<Resource>*); 337 void cancelTimerFired(Timer<Resource>*);
374 338
375 void switchClientsToRevalidatedResource(); 339 void switchClientsToRevalidatedResource();
376 void revalidationSucceeded(const ResourceResponse&); 340 void revalidationSucceeded(const ResourceResponse&);
377 void revalidationFailed(); 341 void revalidationFailed();
378 342
379 bool unlock(); 343 bool unlock();
380 344
381 bool hasRightHandleCountApartFromCache(unsigned targetCount) const;
382
383 void setCachedMetadata(unsigned dataTypeID, const char*, size_t, CachedMetad ataHandler::CacheType); 345 void setCachedMetadata(unsigned dataTypeID, const char*, size_t, CachedMetad ataHandler::CacheType);
384 void clearCachedMetadata(CachedMetadataHandler::CacheType); 346 void clearCachedMetadata(CachedMetadataHandler::CacheType);
385 CachedMetadata* cachedMetadata(unsigned dataTypeID) const; 347 CachedMetadata* cachedMetadata(unsigned dataTypeID) const;
386 348
387 String m_fragmentIdentifierForRequest; 349 String m_fragmentIdentifierForRequest;
388 350
389 #if !ENABLE(OILPAN) 351 #if !ENABLE(OILPAN)
390 WeakPtrFactory<Resource> m_weakPtrFactory; 352 WeakPtrFactory<Resource> m_weakPtrFactory;
391 #endif 353 #endif
392 354
393 RefPtr<CachedMetadata> m_cachedMetadata; 355 RefPtr<CachedMetadata> m_cachedMetadata;
394 OwnPtrWillBeMember<CacheHandler> m_cacheHandler; 356 OwnPtrWillBeMember<CacheHandler> m_cacheHandler;
395 357
396 ResourceError m_error; 358 ResourceError m_error;
397 359
398 double m_loadFinishTime; 360 double m_loadFinishTime;
399 361
400 unsigned long m_identifier; 362 unsigned long m_identifier;
401 363
402 size_t m_encodedSize; 364 size_t m_encodedSize;
403 size_t m_decodedSize; 365 size_t m_decodedSize;
404 unsigned m_handleCount;
405 unsigned m_preloadCount; 366 unsigned m_preloadCount;
406 unsigned m_protectorCount;
407 367
408 String m_cacheIdentifier; 368 String m_cacheIdentifier;
409 369
410 unsigned m_preloadResult : 2; // PreloadResult 370 unsigned m_preloadResult : 2; // PreloadResult
411 unsigned m_requestedFromNetworkingLayer : 1; 371 unsigned m_requestedFromNetworkingLayer : 1;
412 372
413 unsigned m_loading : 1; 373 unsigned m_loading : 1;
414 374
415 unsigned m_switchingClientsToRevalidatedResource : 1;
416
417 unsigned m_type : 4; // Type 375 unsigned m_type : 4; // Type
418 unsigned m_status : 3; // Status 376 unsigned m_status : 3; // Status
419 377
420 unsigned m_wasPurged : 1;
421
422 unsigned m_needsSynchronousCacheHit : 1; 378 unsigned m_needsSynchronousCacheHit : 1;
423 unsigned m_linkPreload : 1; 379 unsigned m_linkPreload : 1;
424 380
425 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK 381 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
426 bool m_deleted; 382 bool m_deleted;
427 #endif 383 #endif
428 384
429 // Ordered list of all redirects followed while fetching this resource. 385 // Ordered list of all redirects followed while fetching this resource.
430 Vector<RedirectPair> m_redirectChain; 386 Vector<RedirectPair> m_redirectChain;
431 }; 387 };
432 388
433 class ResourceFactory { 389 class ResourceFactory {
434 STACK_ALLOCATED(); 390 STACK_ALLOCATED();
435 public: 391 public:
436 virtual Resource* create(const ResourceRequest&, const String&) const = 0; 392 virtual PassRefPtrWillBeRawPtr<Resource> create(const ResourceRequest&, cons t String&) const = 0;
437 Resource::Type type() const { return m_type; } 393 Resource::Type type() const { return m_type; }
438 394
439 protected: 395 protected:
440 ResourceFactory(Resource::Type type) : m_type(type) { } 396 ResourceFactory(Resource::Type type) : m_type(type) { }
441 397
442 Resource::Type m_type; 398 Resource::Type m_type;
443 }; 399 };
444 400
445 #define DEFINE_RESOURCE_TYPE_CASTS(typeName) \ 401 #define DEFINE_RESOURCE_TYPE_CASTS(typeName) \
446 DEFINE_TYPE_CASTS(typeName##Resource, Resource, resource, resource->type() = = Resource::typeName, resource.type() == Resource::typeName); \ 402 DEFINE_TYPE_CASTS(typeName##Resource, Resource, resource, resource->type() = = Resource::typeName, resource.type() == Resource::typeName); \
447 inline typeName##Resource* to##typeName##Resource(const ResourcePtr<Resource >& ptr) { return to##typeName##Resource(ptr.get()); } 403 inline typeName##Resource* to##typeName##Resource(const RefPtrWillBeRawPtr<R esource>& ptr) { return to##typeName##Resource(ptr.get()); }
448 404
449 } // namespace blink 405 } // namespace blink
450 406
451 #endif 407 #endif
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/core/fetch/RawResourceTest.cpp ('k') | third_party/WebKit/Source/core/fetch/Resource.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698