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 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 CachedResource_h | |
24 #define CachedResource_h | |
25 | |
26 #include "PlatformString.h" | |
27 #include "ResourceResponse.h" | |
28 #include "SharedBuffer.h" | |
29 #include <wtf/HashCountedSet.h> | |
30 #include <wtf/Vector.h> | |
31 #include <time.h> | |
32 | |
33 namespace WebCore { | |
34 | |
35 class Cache; | |
36 class CachedResourceClient; | |
37 class CacheHandleBase; | |
38 class DocLoader; | |
39 class Request; | |
40 | |
41 // A resource that is held in the cache. Classes who want to use this object sho
uld derive | |
42 // from CachedResourceClient, to get the function calls in case the requested da
ta has arrived. | |
43 // This class also does the actual communication with the loader to obtain the r
esource from the network. | |
44 class CachedResource { | |
45 friend class Cache; | |
46 | |
47 public: | |
48 enum Type { | |
49 ImageResource, | |
50 CSSStyleSheet, | |
51 Script, | |
52 FontResource | |
53 #if ENABLE(XSLT) | |
54 , XSLStyleSheet | |
55 #endif | |
56 #if ENABLE(XBL) | |
57 , XBL | |
58 #endif | |
59 }; | |
60 | |
61 enum Status { | |
62 NotCached, // this URL is not cached | |
63 Unknown, // let cache decide what to do with it | |
64 New, // inserting new item | |
65 Pending, // only partially loaded | |
66 Cached // regular case | |
67 }; | |
68 | |
69 CachedResource(const String& url, Type); | |
70 virtual ~CachedResource(); | |
71 | |
72 virtual void load(DocLoader* docLoader) { load(docLoader, false, false, tru
e); } | |
73 void load(DocLoader*, bool incremental, bool skipCanLoadCheck, bool sendReso
urceLoadCallbacks); | |
74 | |
75 virtual void setEncoding(const String&) { } | |
76 virtual String encoding() const { return String(); } | |
77 virtual void data(PassRefPtr<SharedBuffer> data, bool allDataReceived) = 0; | |
78 virtual void error() = 0; | |
79 | |
80 const String &url() const { return m_url; } | |
81 Type type() const { return m_type; } | |
82 | |
83 virtual void addClient(CachedResourceClient*); | |
84 void removeClient(CachedResourceClient*); | |
85 bool hasClients() const { return !m_clients.isEmpty(); } | |
86 | |
87 enum PreloadResult { | |
88 PreloadNotReferenced, | |
89 PreloadReferenced, | |
90 PreloadReferencedWhileLoading, | |
91 PreloadReferencedWhileComplete | |
92 }; | |
93 PreloadResult preloadResult() const { return m_preloadResult; } | |
94 void setRequestedFromNetworkingLayer() { m_requestedFromNetworkingLayer = tr
ue; } | |
95 | |
96 virtual void allClientsRemoved() { }; | |
97 | |
98 unsigned count() const { return m_clients.size(); } | |
99 | |
100 Status status() const { return m_status; } | |
101 | |
102 unsigned size() const { return encodedSize() + decodedSize(); } | |
103 unsigned encodedSize() const { return m_encodedSize; } | |
104 unsigned decodedSize() const { return m_decodedSize; } | |
105 | |
106 bool isLoaded() const { return !m_loading; } | |
107 void setLoading(bool b) { m_loading = b; } | |
108 | |
109 virtual bool isImage() const { return false; } | |
110 | |
111 unsigned accessCount() const { return m_accessCount; } | |
112 void increaseAccessCount() { m_accessCount++; } | |
113 | |
114 // Computes the status of an object after loading. | |
115 // Updates the expire date on the cache entry file | |
116 void finish(); | |
117 | |
118 // Called by the cache if the object has been removed from the cache | |
119 // while still being referenced. This means the object should delete itself | |
120 // if the number of clients observing it ever drops to 0. | |
121 void setInCache(bool b) { m_inCache = b; } | |
122 bool inCache() const { return m_inCache; } | |
123 | |
124 void setInLiveDecodedResourcesList(bool b) { m_inLiveDecodedResourcesList =
b; } | |
125 bool inLiveDecodedResourcesList() { return m_inLiveDecodedResourcesList; } | |
126 | |
127 void setRequest(Request*); | |
128 | |
129 void setResponse(const ResourceResponse& response) { m_response = response;
} | |
130 const ResourceResponse& response() const { return m_response; } | |
131 | |
132 bool canDelete() const { return !hasClients() && !m_request && !m_preloadCou
nt; } | |
133 | |
134 bool isExpired() const; | |
135 | |
136 virtual bool schedule() const { return false; } | |
137 | |
138 // List of acceptable MIME types seperated by ",". | |
139 // A MIME type may contain a wildcard, e.g. "text/*". | |
140 String accept() const { return m_accept; } | |
141 void setAccept(const String& accept) { m_accept = accept; } | |
142 | |
143 bool errorOccurred() const { return m_errorOccurred; } | |
144 bool sendResourceLoadCallbacks() const { return m_sendResourceLoadCallbacks;
} | |
145 | |
146 virtual void destroyDecodedData() {}; | |
147 | |
148 void setDocLoader(DocLoader* docLoader) { m_docLoader = docLoader; } | |
149 | |
150 #if PLATFORM(MAC) | |
151 SharedBuffer* data() const { return m_data.get(); } | |
152 #endif | |
153 | |
154 bool isPreloaded() const { return m_preloadCount; } | |
155 void increasePreloadCount() { ++m_preloadCount; } | |
156 void decreasePreloadCount() { ASSERT(m_preloadCount); --m_preloadCount; } | |
157 | |
158 protected: | |
159 void setEncodedSize(unsigned); | |
160 void setDecodedSize(unsigned); | |
161 void didAccessDecodedData(double timeStamp); | |
162 | |
163 HashCountedSet<CachedResourceClient*> m_clients; | |
164 | |
165 String m_url; | |
166 String m_accept; | |
167 Request* m_request; | |
168 | |
169 ResourceResponse m_response; | |
170 RefPtr<SharedBuffer> m_data; | |
171 | |
172 Type m_type; | |
173 Status m_status; | |
174 | |
175 bool m_errorOccurred; | |
176 | |
177 private: | |
178 unsigned m_encodedSize; | |
179 unsigned m_decodedSize; | |
180 unsigned m_accessCount; | |
181 unsigned m_inLiveDecodedResourcesList; | |
182 double m_lastDecodedAccessTime; // Used as a "thrash guard" in the cache | |
183 | |
184 bool m_sendResourceLoadCallbacks; | |
185 | |
186 unsigned m_preloadCount; | |
187 PreloadResult m_preloadResult; | |
188 bool m_requestedFromNetworkingLayer; | |
189 | |
190 protected: | |
191 bool m_inCache; | |
192 bool m_loading; | |
193 bool m_expireDateChanged; | |
194 #ifndef NDEBUG | |
195 bool m_deleted; | |
196 unsigned m_lruIndex; | |
197 #endif | |
198 | |
199 private: | |
200 CachedResource* m_nextInAllResourcesList; | |
201 CachedResource* m_prevInAllResourcesList; | |
202 | |
203 CachedResource* m_nextInLiveResourcesList; | |
204 CachedResource* m_prevInLiveResourcesList; | |
205 | |
206 DocLoader* m_docLoader; // only non-0 for resources that are not in the cach
e | |
207 }; | |
208 | |
209 } | |
210 | |
211 #endif | |
OLD | NEW |