Home | History | Annotate | Download | only in cache
      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) 2004, 2005, 2006 Apple Computer, Inc.
      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 sheets and html
     23     pages from the web. It has a memory cache for these objects.
     24 */
     25 
     26 #ifndef CachedResourceLoader_h
     27 #define CachedResourceLoader_h
     28 
     29 #include "CachedResource.h"
     30 #include "CachedResourceHandle.h"
     31 #include "CachePolicy.h"
     32 #include "ResourceLoadPriority.h"
     33 #include "Timer.h"
     34 #include <wtf/Deque.h>
     35 #include <wtf/HashMap.h>
     36 #include <wtf/HashSet.h>
     37 #include <wtf/ListHashSet.h>
     38 #include <wtf/text/StringHash.h>
     39 
     40 namespace WebCore {
     41 
     42 class CachedCSSStyleSheet;
     43 class CachedFont;
     44 class CachedImage;
     45 class CachedResourceRequest;
     46 class CachedScript;
     47 class CachedXSLStyleSheet;
     48 class Document;
     49 class Frame;
     50 class ImageLoader;
     51 class KURL;
     52 
     53 // The CachedResourceLoader manages the loading of scripts/images/stylesheets for a single document.
     54 class CachedResourceLoader {
     55     WTF_MAKE_NONCOPYABLE(CachedResourceLoader); WTF_MAKE_FAST_ALLOCATED;
     56 friend class ImageLoader;
     57 
     58 public:
     59     CachedResourceLoader(Document*);
     60     ~CachedResourceLoader();
     61 
     62     CachedImage* requestImage(const String& url);
     63     CachedCSSStyleSheet* requestCSSStyleSheet(const String& url, const String& charset, ResourceLoadPriority priority = ResourceLoadPriorityUnresolved);
     64     CachedCSSStyleSheet* requestUserCSSStyleSheet(const String& url, const String& charset);
     65     CachedScript* requestScript(const String& url, const String& charset);
     66     CachedFont* requestFont(const String& url);
     67 
     68 #if ENABLE(XSLT)
     69     CachedXSLStyleSheet* requestXSLStyleSheet(const String& url);
     70 #endif
     71 #if ENABLE(LINK_PREFETCH)
     72     CachedResource* requestLinkResource(const String &url, ResourceLoadPriority priority = ResourceLoadPriorityUnresolved);
     73 #endif
     74 
     75     // Logs an access denied message to the console for the specified URL.
     76     void printAccessDeniedMessage(const KURL& url) const;
     77 
     78     CachedResource* cachedResource(const String& url) const;
     79     CachedResource* cachedResource(const KURL& url) const;
     80 
     81     typedef HashMap<String, CachedResourceHandle<CachedResource> > DocumentResourceMap;
     82     const DocumentResourceMap& allCachedResources() const { return m_documentResources; }
     83 
     84     bool autoLoadImages() const { return m_autoLoadImages; }
     85     void setAutoLoadImages(bool);
     86 
     87 #ifdef ANDROID_BLOCK_NETWORK_IMAGE
     88     bool blockNetworkImage() const { return m_blockNetworkImage; }
     89     void setBlockNetworkImage(bool);
     90     bool shouldBlockNetworkImage(const String& url) const;
     91 #endif
     92 
     93     CachePolicy cachePolicy() const;
     94 
     95     Frame* frame() const; // Can be NULL
     96     Document* document() const { return m_document; }
     97 
     98     void removeCachedResource(CachedResource*) const;
     99 
    100     void load(CachedResource*, bool incremental = false, SecurityCheckPolicy = DoSecurityCheck, bool sendResourceLoadCallbacks = true);
    101     void loadFinishing() { m_loadFinishing = true; }
    102     void loadDone(CachedResourceRequest*);
    103     void cancelRequests();
    104 
    105     void setAllowStaleResources(bool allowStaleResources) { m_allowStaleResources = allowStaleResources; }
    106 
    107     void incrementRequestCount(const CachedResource*);
    108     void decrementRequestCount(const CachedResource*);
    109     int requestCount();
    110 
    111     void clearPreloads();
    112     void clearPendingPreloads();
    113     void preload(CachedResource::Type, const String& url, const String& charset, bool referencedFromBody);
    114     void checkForPendingPreloads();
    115     void printPreloadStats();
    116 
    117 private:
    118     CachedResource* requestResource(CachedResource::Type, const String& url, const String& charset, ResourceLoadPriority priority = ResourceLoadPriorityUnresolved, bool isPreload = false);
    119     CachedResource* revalidateResource(CachedResource*, ResourceLoadPriority priority);
    120     CachedResource* loadResource(CachedResource::Type, const KURL&, const String& charset, ResourceLoadPriority priority);
    121     void requestPreload(CachedResource::Type, const String& url, const String& charset);
    122 
    123     enum RevalidationPolicy { Use, Revalidate, Reload, Load };
    124     RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, bool forPreload, CachedResource* existingResource) const;
    125 
    126     void notifyLoadedFromMemoryCache(CachedResource*);
    127     bool canRequest(CachedResource::Type, const KURL&);
    128 
    129     void loadDoneActionTimerFired(Timer<CachedResourceLoader>*);
    130 
    131     void performPostLoadActions();
    132 
    133     HashSet<String> m_validatedURLs;
    134     mutable DocumentResourceMap m_documentResources;
    135     Document* m_document;
    136 
    137     typedef HashSet<RefPtr<CachedResourceRequest> > RequestSet;
    138     RequestSet m_requests;
    139 
    140     int m_requestCount;
    141 
    142     OwnPtr<ListHashSet<CachedResource*> > m_preloads;
    143     struct PendingPreload {
    144         CachedResource::Type m_type;
    145         String m_url;
    146         String m_charset;
    147     };
    148     Deque<PendingPreload> m_pendingPreloads;
    149 
    150     Timer<CachedResourceLoader> m_loadDoneActionTimer;
    151 
    152     //29 bits left
    153     bool m_autoLoadImages : 1;
    154     bool m_loadFinishing : 1;
    155     bool m_allowStaleResources : 1;
    156 #ifdef ANDROID_BLOCK_NETWORK_IMAGE
    157     bool m_blockNetworkImage : 1;
    158 #endif
    159 };
    160 
    161 }
    162 
    163 #endif
    164