Home | History | Annotate | Download | only in fetch
      1 /*
      2     Copyright (C) 1998 Lars Knoll (knoll (at) mpi-hd.mpg.de)
      3     Copyright (C) 2001 Dirk Mueller <mueller (at) kde.org>
      4     Copyright (C) 2006 Samuel Weinig (sam.weinig (at) 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 Resource_h
     24 #define Resource_h
     25 
     26 #include "core/fetch/ResourceLoaderOptions.h"
     27 #include "platform/Timer.h"
     28 #include "platform/network/ResourceError.h"
     29 #include "platform/network/ResourceLoadPriority.h"
     30 #include "platform/network/ResourceRequest.h"
     31 #include "platform/network/ResourceResponse.h"
     32 #include "wtf/HashCountedSet.h"
     33 #include "wtf/HashSet.h"
     34 #include "wtf/OwnPtr.h"
     35 #include "wtf/text/WTFString.h"
     36 
     37 // FIXME(crbug.com/352043): This is temporarily enabled even on RELEASE to diagnose a wild crash.
     38 #define ENABLE_RESOURCE_IS_DELETED_CHECK
     39 
     40 namespace WebCore {
     41 
     42 struct FetchInitiatorInfo;
     43 class MemoryCache;
     44 class CachedMetadata;
     45 class ResourceClient;
     46 class ResourcePtrBase;
     47 class ResourceFetcher;
     48 class InspectorResource;
     49 class ResourceLoader;
     50 class SecurityOrigin;
     51 class SharedBuffer;
     52 
     53 // A resource that is held in the cache. Classes who want to use this object should derive
     54 // from ResourceClient, to get the function calls in case the requested data has arrived.
     55 // This class also does the actual communication with the loader to obtain the resource from the network.
     56 class Resource {
     57     WTF_MAKE_NONCOPYABLE(Resource); WTF_MAKE_FAST_ALLOCATED;
     58     friend class InspectorResource;
     59 
     60 public:
     61     enum Type {
     62         MainResource,
     63         Image,
     64         CSSStyleSheet,
     65         Script,
     66         Font,
     67         Raw,
     68         SVGDocument,
     69         XSLStyleSheet,
     70         LinkPrefetch,
     71         LinkSubresource,
     72         TextTrack,
     73         ImportResource,
     74         Media // Audio or video file requested by a HTML5 media element
     75     };
     76 
     77     enum Status {
     78         Unknown, // let cache decide what to do with it
     79         Pending, // only partially loaded
     80         Cached, // regular case
     81         LoadError,
     82         DecodeError
     83     };
     84 
     85     Resource(const ResourceRequest&, Type);
     86     virtual ~Resource();
     87 
     88     virtual void load(ResourceFetcher*, const ResourceLoaderOptions&);
     89 
     90     virtual void setEncoding(const String&) { }
     91     virtual String encoding() const { return String(); }
     92     virtual void appendData(const char*, int);
     93     virtual void error(Resource::Status);
     94 
     95     void setNeedsSynchronousCacheHit(bool needsSynchronousCacheHit) { m_needsSynchronousCacheHit = needsSynchronousCacheHit; }
     96 
     97     void setResourceError(const ResourceError& error) { m_error = error; }
     98     const ResourceError& resourceError() const { return m_error; }
     99 
    100     void setIdentifier(unsigned long identifier) { m_identifier = identifier; }
    101     unsigned long identifier() const { return m_identifier; }
    102 
    103     virtual bool shouldIgnoreHTTPStatusCodeErrors() const { return false; }
    104 
    105     ResourceRequest& resourceRequest() { return m_resourceRequest; }
    106     const ResourceRequest& lastResourceRequest();
    107     const KURL& url() const { return m_resourceRequest.url();}
    108     Type type() const { return static_cast<Type>(m_type); }
    109     const ResourceLoaderOptions& options() const { return m_options; }
    110     void setOptions(const ResourceLoaderOptions& options) { m_options = options; }
    111 
    112     void didChangePriority(ResourceLoadPriority, int intraPriorityValue);
    113 
    114     void addClient(ResourceClient*);
    115     void removeClient(ResourceClient*);
    116     bool hasClients() const { return !m_clients.isEmpty() || !m_clientsAwaitingCallback.isEmpty(); }
    117     bool deleteIfPossible();
    118 
    119     enum PreloadResult {
    120         PreloadNotReferenced,
    121         PreloadReferenced,
    122         PreloadReferencedWhileLoading,
    123         PreloadReferencedWhileComplete
    124     };
    125     PreloadResult preloadResult() const { return static_cast<PreloadResult>(m_preloadResult); }
    126 
    127     virtual void didAddClient(ResourceClient*);
    128     virtual void didRemoveClient(ResourceClient*) { }
    129     virtual void allClientsRemoved();
    130 
    131     unsigned count() const { return m_clients.size(); }
    132 
    133     Status status() const { return static_cast<Status>(m_status); }
    134     void setStatus(Status status) { m_status = status; }
    135 
    136     size_t size() const { return encodedSize() + decodedSize() + overheadSize(); }
    137     size_t encodedSize() const { return m_encodedSize; }
    138     size_t decodedSize() const { return m_decodedSize; }
    139     size_t overheadSize() const;
    140 
    141     bool isLoaded() const { return !m_loading; } // FIXME. Method name is inaccurate. Loading might not have started yet.
    142 
    143     bool isLoading() const { return m_loading; }
    144     void setLoading(bool b) { m_loading = b; }
    145     virtual bool stillNeedsLoad() const { return false; }
    146 
    147     ResourceLoader* loader() const { return m_loader.get(); }
    148 
    149     virtual bool isImage() const { return false; }
    150     bool ignoreForRequestCount() const
    151     {
    152         return type() == MainResource
    153             || type() == LinkPrefetch
    154             || type() == LinkSubresource
    155             || type() == Media
    156             || type() == Raw
    157             || type() == TextTrack;
    158     }
    159 
    160     // Computes the status of an object after loading.
    161     // Updates the expire date on the cache entry file
    162     void finish(double finishTime = 0.0);
    163 
    164     // FIXME: Remove the stringless variant once all the callsites' error messages are updated.
    165     bool passesAccessControlCheck(SecurityOrigin*);
    166     bool passesAccessControlCheck(SecurityOrigin*, String& errorDescription);
    167 
    168     void clearLoader();
    169 
    170     SharedBuffer* resourceBuffer() const { return m_data.get(); }
    171     void setResourceBuffer(PassRefPtr<SharedBuffer>);
    172 
    173     virtual void willSendRequest(ResourceRequest&, const ResourceResponse&);
    174 
    175     virtual void updateRequest(const ResourceRequest&) { }
    176     virtual void responseReceived(const ResourceResponse&);
    177     void setResponse(const ResourceResponse& response) { m_response = response; }
    178     const ResourceResponse& response() const { return m_response; }
    179 
    180     // Sets the serialized metadata retrieved from the platform's cache.
    181     void setSerializedCachedMetadata(const char*, size_t);
    182 
    183     // Caches the given metadata in association with this resource and suggests
    184     // that the platform persist it. The dataTypeID is a pseudo-randomly chosen
    185     // identifier that is used to distinguish data generated by the caller.
    186     void setCachedMetadata(unsigned dataTypeID, const char*, size_t);
    187 
    188     // Returns cached metadata of the given type associated with this resource.
    189     CachedMetadata* cachedMetadata(unsigned dataTypeID) const;
    190 
    191     bool hasOneHandle() const;
    192     bool canDelete() const;
    193 
    194     // List of acceptable MIME types separated by ",".
    195     // A MIME type may contain a wildcard, e.g. "text/*".
    196     AtomicString accept() const { return m_accept; }
    197     void setAccept(const AtomicString& accept) { m_accept = accept; }
    198 
    199     bool wasCanceled() const { return m_error.isCancellation(); }
    200     bool errorOccurred() const { return m_status == LoadError || m_status == DecodeError; }
    201     bool loadFailedOrCanceled() { return !m_error.isNull(); }
    202 
    203     DataBufferingPolicy dataBufferingPolicy() const { return m_options.dataBufferingPolicy; }
    204     void setDataBufferingPolicy(DataBufferingPolicy);
    205 
    206     bool isPreloaded() const { return m_preloadCount; }
    207     void increasePreloadCount() { ++m_preloadCount; }
    208     void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; }
    209 
    210     void registerHandle(ResourcePtrBase* h);
    211     void unregisterHandle(ResourcePtrBase* h);
    212 
    213     bool canReuseRedirectChain();
    214     bool mustRevalidateDueToCacheHeaders();
    215     bool canUseCacheValidator();
    216     bool isCacheValidator() const { return m_resourceToRevalidate; }
    217     Resource* resourceToRevalidate() const { return m_resourceToRevalidate; }
    218     void setResourceToRevalidate(Resource*);
    219     bool hasCacheControlNoStoreHeader();
    220 
    221     bool isPurgeable() const;
    222     bool wasPurged() const;
    223     bool lock();
    224 
    225     virtual void didSendData(unsigned long long /* bytesSent */, unsigned long long /* totalBytesToBeSent */) { }
    226     virtual void didDownloadData(int) { }
    227 
    228     double loadFinishTime() const { return m_loadFinishTime; }
    229 
    230     virtual bool canReuse(const ResourceRequest&) const { return true; }
    231 
    232     // Used by the MemoryCache to reduce the memory consumption of the entry.
    233     void prune();
    234 
    235     static const char* resourceTypeToString(Type, const FetchInitiatorInfo&);
    236 
    237 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
    238     void assertAlive() const { RELEASE_ASSERT(!m_deleted); }
    239 #else
    240     void assertAlive() const { }
    241 #endif
    242 
    243 protected:
    244     virtual void checkNotify();
    245     virtual void finishOnePart();
    246 
    247     // Normal resource pointers will silently switch what Resource* they reference when we
    248     // successfully revalidated the resource. We need a way to guarantee that the Resource
    249     // that received the 304 response survives long enough to switch everything over to the
    250     // revalidatedresource. The normal mechanisms for keeping a Resource alive externally
    251     // (ResourcePtrs and ResourceClients registering themselves) don't work in this case, so
    252     // have a separate internal protector).
    253     class InternalResourcePtr {
    254     public:
    255         explicit InternalResourcePtr(Resource* resource)
    256             : m_resource(resource)
    257         {
    258             m_resource->incrementProtectorCount();
    259         }
    260 
    261         ~InternalResourcePtr()
    262         {
    263             m_resource->decrementProtectorCount();
    264             m_resource->deleteIfPossible();
    265         }
    266     private:
    267         Resource* m_resource;
    268     };
    269 
    270     void incrementProtectorCount() { m_protectorCount++; }
    271     void decrementProtectorCount() { m_protectorCount--; }
    272 
    273     void setEncodedSize(size_t);
    274     void setDecodedSize(size_t);
    275     void didAccessDecodedData();
    276 
    277     virtual void switchClientsToRevalidatedResource();
    278     void clearResourceToRevalidate();
    279     void updateResponseAfterRevalidation(const ResourceResponse& validatingResponse);
    280 
    281     void finishPendingClients();
    282 
    283     HashCountedSet<ResourceClient*> m_clients;
    284     HashCountedSet<ResourceClient*> m_clientsAwaitingCallback;
    285 
    286     class ResourceCallback {
    287     public:
    288         static ResourceCallback* callbackHandler();
    289         void schedule(Resource*);
    290         void cancel(Resource*);
    291         bool isScheduled(Resource*) const;
    292     private:
    293         ResourceCallback();
    294         void timerFired(Timer<ResourceCallback>*);
    295         Timer<ResourceCallback> m_callbackTimer;
    296         HashSet<Resource*> m_resourcesWithPendingClients;
    297     };
    298 
    299     bool hasClient(ResourceClient* client) { return m_clients.contains(client) || m_clientsAwaitingCallback.contains(client); }
    300 
    301     struct RedirectPair {
    302     public:
    303         explicit RedirectPair(const ResourceRequest& request, const ResourceResponse& redirectResponse)
    304             : m_request(request)
    305             , m_redirectResponse(redirectResponse)
    306         {
    307         }
    308 
    309         ResourceRequest m_request;
    310         ResourceResponse m_redirectResponse;
    311     };
    312     const Vector<RedirectPair>& redirectChain() const { return m_redirectChain; }
    313 
    314     virtual bool isSafeToUnlock() const { return false; }
    315     virtual void destroyDecodedDataIfPossible() { }
    316 
    317     ResourceRequest m_resourceRequest;
    318     AtomicString m_accept;
    319     RefPtr<ResourceLoader> m_loader;
    320     ResourceLoaderOptions m_options;
    321 
    322     ResourceResponse m_response;
    323     double m_responseTimestamp;
    324 
    325     RefPtr<SharedBuffer> m_data;
    326     Timer<Resource> m_cancelTimer;
    327 
    328 private:
    329     bool addClientToSet(ResourceClient*);
    330     void cancelTimerFired(Timer<Resource>*);
    331 
    332     void revalidationSucceeded(const ResourceResponse&);
    333     void revalidationFailed();
    334 
    335     bool unlock();
    336 
    337     bool hasRightHandleCountApartFromCache(unsigned targetCount) const;
    338 
    339     void failBeforeStarting();
    340 
    341     String m_fragmentIdentifierForRequest;
    342 
    343     RefPtr<CachedMetadata> m_cachedMetadata;
    344 
    345     ResourceError m_error;
    346 
    347     double m_loadFinishTime;
    348 
    349     unsigned long m_identifier;
    350 
    351     size_t m_encodedSize;
    352     size_t m_decodedSize;
    353     unsigned m_handleCount;
    354     unsigned m_preloadCount;
    355     unsigned m_protectorCount;
    356 
    357     unsigned m_preloadResult : 2; // PreloadResult
    358     unsigned m_requestedFromNetworkingLayer : 1;
    359 
    360     unsigned m_loading : 1;
    361 
    362     unsigned m_switchingClientsToRevalidatedResource : 1;
    363 
    364     unsigned m_type : 4; // Type
    365     unsigned m_status : 3; // Status
    366 
    367     unsigned m_wasPurged : 1;
    368 
    369     unsigned m_needsSynchronousCacheHit : 1;
    370 
    371 #ifdef ENABLE_RESOURCE_IS_DELETED_CHECK
    372     bool m_deleted;
    373 #endif
    374 
    375     // If this field is non-null we are using the resource as a proxy for checking whether an existing resource is still up to date
    376     // using HTTP If-Modified-Since/If-None-Match headers. If the response is 304 all clients of this resource are moved
    377     // to to be clients of m_resourceToRevalidate and the resource is deleted. If not, the field is zeroed and this
    378     // resources becomes normal resource load.
    379     Resource* m_resourceToRevalidate;
    380 
    381     // If this field is non-null, the resource has a proxy for checking whether it is still up to date (see m_resourceToRevalidate).
    382     Resource* m_proxyResource;
    383 
    384     // These handles will need to be updated to point to the m_resourceToRevalidate in case we get 304 response.
    385     HashSet<ResourcePtrBase*> m_handlesToRevalidate;
    386 
    387     // Ordered list of all redirects followed while fetching this resource.
    388     Vector<RedirectPair> m_redirectChain;
    389 };
    390 
    391 #if !LOG_DISABLED
    392 // Intended to be used in LOG statements.
    393 const char* ResourceTypeName(Resource::Type);
    394 #endif
    395 
    396 #define DEFINE_RESOURCE_TYPE_CASTS(typeName) \
    397     DEFINE_TYPE_CASTS(typeName##Resource, Resource, resource, resource->type() == Resource::typeName, resource.type() == Resource::typeName); \
    398     inline typeName##Resource* to##typeName##Resource(const ResourcePtr<Resource>& ptr) { return to##typeName##Resource(ptr.get()); }
    399 
    400 }
    401 
    402 #endif
    403