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, unsigned size)
50 m_cachePolicy = cachePolicy;
52 m_platformResponse = 0;
54 m_expireDateChanged = false;
64 CachedResource::~CachedResource()
71 setPlatformResponse(0);
75 Vector<char>& CachedResource::bufferData(const char* bytes, int addedSize, Request* request)
77 // Add new bytes to the buffer in the Request object.
78 Vector<char>& buffer = request->buffer();
80 unsigned oldSize = buffer.size();
81 buffer.resize(oldSize + addedSize);
82 memcpy(buffer.data() + oldSize, bytes, addedSize);
87 void CachedResource::finish()
90 KURL url(m_url.deprecatedString());
91 if (m_expireDateChanged && url.protocol().startsWith("http"))
92 m_expireDateChanged = false;
95 bool CachedResource::isExpired() const
97 if (!m_response.expirationDate())
100 return (difftime(now, m_response.expirationDate()) >= 0);
103 void CachedResource::setRequest(Request* request)
105 if (request && !m_request)
108 if (canDelete() && !inCache())
112 void CachedResource::ref(CachedResourceClient *c)
114 if (!referenced() && inCache())
115 cache()->addToLiveObjectSize(size());
119 void CachedResource::deref(CachedResourceClient *c)
122 if (canDelete() && !inCache())
124 else if (!referenced() && inCache()) {
125 cache()->removeFromLiveObjectSize(size());
130 void CachedResource::setSize(unsigned size)
135 unsigned oldSize = m_size;
137 // The object must now be moved to a different queue, since its size has been changed.
138 // We have to remove explicitly before updating m_size, so that we find the correct previous
141 cache()->removeFromLRUList(this);
146 // Now insert into the new LRU list.
147 cache()->insertInLRUList(this);
149 // Update the cache's size totals.
150 cache()->adjustSize(referenced(), oldSize, size);