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) 2002 Waldo Bastian (bastian@kde.org)
7 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
8 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
10 This library is free software; you can redistribute it and/or
11 modify it under the terms of the GNU Library General Public
12 License as published by the Free Software Foundation; either
13 version 2 of the License, or (at your option) any later version.
15 This library is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Library General Public License for more details.
20 You should have received a copy of the GNU Library General Public License
21 along with this library; see the file COPYING.LIB. If not, write to
22 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 Boston, MA 02111-1307, USA.
25 This class provides all functionality needed for loading images, style sheets and html
26 pages from the web. It has a memory cache for these objects.
30 #include "CachedResource.h"
35 #include <wtf/Vector.h>
38 #include <CoreFoundation/CoreFoundation.h>
43 CachedResource::CachedResource(const String& URL, Type type, CachePolicy cachePolicy, time_t expireDate, unsigned size)
50 m_cachePolicy = cachePolicy;
54 m_expireDate = expireDate;
55 m_expireDateChanged = false;
65 CachedResource::~CachedResource()
76 Vector<char>& CachedResource::bufferData(const char* bytes, int addedSize, Request* request)
78 // Add new bytes to the buffer in the Request object.
79 Vector<char>& buffer = request->buffer();
81 unsigned oldSize = buffer.size();
82 buffer.resize(oldSize + addedSize);
83 memcpy(buffer.data() + oldSize, bytes, addedSize);
88 void CachedResource::finish()
91 KURL url(m_url.deprecatedString());
92 if (m_expireDateChanged && url.protocol().startsWith("http"))
93 m_expireDateChanged = false;
96 void CachedResource::setExpireDate(time_t expireDate, bool changeHttpCache)
98 if (expireDate == m_expireDate)
101 if (m_status == Cached)
104 m_expireDate = expireDate;
105 if (changeHttpCache && m_expireDate)
106 m_expireDateChanged = true;
109 bool CachedResource::isExpired() const
113 time_t now = time(0);
114 return (difftime(now, m_expireDate) >= 0);
117 void CachedResource::setRequest(Request* request)
119 if (request && !m_request)
122 if (canDelete() && !inCache())
126 void CachedResource::ref(CachedResourceClient *c)
128 if (!referenced() && inCache())
129 cache()->addToLiveObjectSize(size());
133 void CachedResource::deref(CachedResourceClient *c)
136 if (canDelete() && !inCache())
138 else if (!referenced() && inCache()) {
139 cache()->removeFromLiveObjectSize(size());
144 void CachedResource::setSize(unsigned size)
149 unsigned oldSize = m_size;
151 // The object must now be moved to a different queue, since its size has been changed.
152 // We have to remove explicitly before updating m_size, so that we find the correct previous
155 cache()->removeFromLRUList(this);
160 // Now insert into the new LRU list.
161 cache()->insertInLRUList(this);
163 // Update the cache's size totals.
164 cache()->adjustSize(referenced(), oldSize, size);