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 Apple Computer, Inc.
5 Copyright (C) 2009 Torch Mobile Inc. http://www.torchmobile.com/
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.
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.
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.
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.
26 #ifndef CachedResourceLoader_h
27 #define CachedResourceLoader_h
29 #include "CachedResource.h"
30 #include "CachedResourceHandle.h"
31 #include "CachePolicy.h"
32 #include "ResourceLoadPriority.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>
42 class CachedCSSStyleSheet;
45 class CachedResourceRequest;
47 class CachedXSLStyleSheet;
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;
59 CachedResourceLoader(Document*);
60 ~CachedResourceLoader();
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);
69 CachedXSLStyleSheet* requestXSLStyleSheet(const String& url);
71 #if ENABLE(LINK_PREFETCH)
72 CachedResource* requestLinkPrefetch(const String &url);
75 // Logs an access denied message to the console for the specified URL.
76 void printAccessDeniedMessage(const KURL& url) const;
78 CachedResource* cachedResource(const String& url) const;
79 CachedResource* cachedResource(const KURL& url) const;
81 typedef HashMap<String, CachedResourceHandle<CachedResource> > DocumentResourceMap;
82 const DocumentResourceMap& allCachedResources() const { return m_documentResources; }
84 bool autoLoadImages() const { return m_autoLoadImages; }
85 void setAutoLoadImages(bool);
87 #ifdef ANDROID_BLOCK_NETWORK_IMAGE
88 bool blockNetworkImage() const { return m_blockNetworkImage; }
89 void setBlockNetworkImage(bool);
90 bool shouldBlockNetworkImage(const String& url) const;
93 CachePolicy cachePolicy() const;
95 Frame* frame() const; // Can be NULL
96 Document* document() const { return m_document; }
98 void removeCachedResource(CachedResource*) const;
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();
105 void setAllowStaleResources(bool allowStaleResources) { m_allowStaleResources = allowStaleResources; }
107 void incrementRequestCount(const CachedResource*);
108 void decrementRequestCount(const CachedResource*);
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();
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);
123 enum RevalidationPolicy { Use, Revalidate, Reload, Load };
124 RevalidationPolicy determineRevalidationPolicy(CachedResource::Type, bool forPreload, CachedResource* existingResource) const;
126 void notifyLoadedFromMemoryCache(CachedResource*);
127 bool canRequest(CachedResource::Type, const KURL&);
129 void loadDoneActionTimerFired(Timer<CachedResourceLoader>*);
131 void performPostLoadActions();
133 HashSet<String> m_validatedURLs;
134 mutable DocumentResourceMap m_documentResources;
135 Document* m_document;
137 typedef HashSet<RefPtr<CachedResourceRequest> > RequestSet;
138 RequestSet m_requests;
142 OwnPtr<ListHashSet<CachedResource*> > m_preloads;
143 struct PendingPreload {
144 CachedResource::Type m_type;
148 Deque<PendingPreload> m_pendingPreloads;
150 Timer<CachedResourceLoader> m_loadDoneActionTimer;
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;