2 This file is part of the KDE libraries
4 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
5 Copyright (C) 2001 Dirk Mueller <mueller@kde.org>
6 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
7 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Library General Public
11 License as published by the Free Software Foundation; either
12 version 2 of the License, or (at your option) any later version.
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Library General Public License for more details.
19 You should have received a copy of the GNU Library General Public License
20 along with this library; see the file COPYING.LIB. If not, write to
21 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
22 Boston, MA 02111-1307, USA.
24 This class provides all functionality needed for loading images, style sheets and html
25 pages from the web. It has a memory cache for these objects.
28 #ifndef CachedResource_h
29 #define CachedResource_h
31 #include "CachePolicy.h"
32 #include "PlatformString.h"
33 #include "ResourceLoaderClient.h" // defines PlatformResponse and PlatformData
34 #include <wtf/HashSet.h>
35 #include <wtf/Vector.h>
39 class CachedResourceClient;
44 * A cached object. Classes who want to use this object should derive
45 * from CachedResourceClient, to get the function calls in case the requested data has arrived.
47 * This class also does the actual communication with kio and loads the file.
49 class CachedResource {
64 NotCached, // this URL is not cached
65 Unknown, // let cache decide what to do with it
66 New, // inserting new item
67 Pending, // only partially loaded
68 Persistent, // never delete this
69 Cached, // regular case
70 Uncacheable // too big to be cached, will be destroyed as soon as possible
73 CachedResource(const String& URL, Type type, CachePolicy cachePolicy, time_t expireDate, int size = 0)
80 m_cachePolicy = cachePolicy;
86 m_expireDate = expireDate;
88 m_expireDateChanged = false;
95 virtual ~CachedResource();
97 virtual void setCharset(const String&) { }
98 virtual Vector<char>& bufferData(const char* bytes, int addedSize, Request*);
99 virtual void data(Vector<char>&, bool allDataReceived) = 0;
100 virtual void error() = 0;
102 const String &url() const { return m_url; }
103 Type type() const { return m_type; }
105 virtual void ref(CachedResourceClient*);
106 virtual void deref(CachedResourceClient*);
108 int count() const { return m_clients.size(); }
110 Status status() const { return m_status; }
112 int size() const { return m_size; }
114 bool isLoaded() const { return !m_loading; }
116 virtual bool isImage() const { return false; }
118 int accessCount() const { return m_accessCount; }
119 void increaseAccessCount() { m_accessCount++; }
122 * computes the status of an object after loading.
123 * the result depends on the objects size and the size of the cache
124 * also updates the expire date on the cache entry file
129 * Called by the cache if the object has been removed from the cache dict
130 * while still being referenced. This means the object should kill itself
131 * if its reference counter drops down to zero.
133 void setFree(bool b) { m_free = b; }
135 CachePolicy cachePolicy() const { return m_cachePolicy; }
137 void setRequest(Request*);
139 PlatformResponse response() const { return m_response; }
140 void setResponse(PlatformResponse);
141 PlatformData allData() const { return m_allData; }
142 void setAllData(PlatformData);
144 bool canDelete() const { return m_clients.isEmpty() && !m_request; }
146 void setExpireDate(time_t expireDate, bool changeHttpCache);
148 bool isExpired() const;
150 virtual bool schedule() const { return false; }
152 // List of acceptable MIME types seperated by ",".
153 // A MIME type may contain a wildcard, e.g. "text/*".
154 String accept() const { return m_accept; }
155 void setAccept(const String& accept) { m_accept = accept; }
158 void setSize(int size);
160 HashSet<CachedResourceClient*> m_clients;
166 PlatformResponse m_response;
167 PlatformData m_allData;
178 CachePolicy m_cachePolicy;
182 bool m_expireDateChanged : 1;
185 bool allowInLRUList() const { return canDelete() && status() != Persistent; }
187 CachedResource* m_nextInLRUList;
188 CachedResource* m_prevInLRUList;