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 "ResourceHandleClient.h" // defines PlatformResponse and PlatformData
34 #include <wtf/HashSet.h>
35 #include <wtf/Vector.h>
39 #include "RetainPtr.h"
47 #endif // PLATFORM(MAC)
52 class CachedResourceClient;
55 // A resource that is held in the cache. Classes who want to use this object should derive
56 // from CachedResourceClient, to get the function calls in case the requested data has arrived.
57 // This class also does the actual communication with the loader to obtain the resource from the network.
58 class CachedResource {
73 NotCached, // this URL is not cached
74 Unknown, // let cache decide what to do with it
75 New, // inserting new item
76 Pending, // only partially loaded
77 Cached // regular case
80 CachedResource(const String& URL, Type type, CachePolicy cachePolicy, time_t expireDate, unsigned size = 0);
81 virtual ~CachedResource();
83 virtual void setEncoding(const String&) { }
84 virtual Vector<char>& bufferData(const char* bytes, int addedSize, Request*);
85 virtual void data(Vector<char>&, bool allDataReceived) = 0;
86 virtual void error() = 0;
88 const String &url() const { return m_url; }
89 Type type() const { return m_type; }
91 virtual void ref(CachedResourceClient*);
92 void deref(CachedResourceClient*);
93 bool referenced() const { return !m_clients.isEmpty(); }
95 unsigned count() const { return m_clients.size(); }
97 Status status() const { return m_status; }
99 unsigned size() const { return m_size; }
101 bool isLoaded() const { return !m_loading; }
102 void setLoading(bool b) { m_loading = b; }
104 virtual bool isImage() const { return false; }
106 unsigned accessCount() const { return m_accessCount; }
107 void increaseAccessCount() { m_accessCount++; }
109 // Computes the status of an object after loading.
110 // Updates the expire date on the cache entry file
113 // Called by the cache if the object has been removed from the cache
114 // while still being referenced. This means the object should delete itself
115 // if the number of clients observing it ever drops to 0.
116 void setInCache(bool b) { m_inCache = b; }
117 bool inCache() const { return m_inCache; }
119 CachePolicy cachePolicy() const { return m_cachePolicy; }
121 void setRequest(Request*);
123 PlatformResponse response() const { return m_response; }
124 void setResponse(PlatformResponse);
125 PlatformData allData() const { return m_allData; }
126 void setAllData(PlatformData);
128 bool canDelete() const { return !referenced() && !m_request; }
130 void setExpireDate(time_t expireDate, bool changeHttpCache);
132 bool isExpired() const;
134 virtual bool schedule() const { return false; }
136 // List of acceptable MIME types seperated by ",".
137 // A MIME type may contain a wildcard, e.g. "text/*".
138 String accept() const { return m_accept; }
139 void setAccept(const String& accept) { m_accept = accept; }
142 NSURLRequest* getNSURLRequest();
146 void setSize(unsigned size);
148 HashSet<CachedResourceClient*> m_clients;
155 RetainPtr<NSURLRequest> m_nsURLRequest;
158 PlatformResponse m_response;
159 PlatformData m_allData;
166 unsigned m_accessCount;
170 CachePolicy m_cachePolicy;
173 bool m_expireDateChanged;
180 CachedResource* m_nextInLRUList;
181 CachedResource* m_prevInLRUList;