2 Copyright (C) 1998 Lars Knoll (knoll@mpi-hd.mpg.de)
3 Copyright (C) 2001 Dirk Mueller (mueller@kde.org)
4 Copyright (C) 2002 Waldo Bastian (bastian@kde.org)
5 Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com)
6 Copyright (C) 2004, 2005, 2006 Apple Computer, Inc.
8 This library is free software; you can redistribute it and/or
9 modify it under the terms of the GNU Library General Public
10 License as published by the Free Software Foundation; either
11 version 2 of the License, or (at your option) any later version.
13 This library is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Library General Public License for more details.
18 You should have received a copy of the GNU Library General Public License
19 along with this library; see the file COPYING.LIB. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA.
23 This class provides all functionality needed for loading images, style sheets and html
24 pages from the web. It has a memory cache for these objects.
28 #include "CachedCSSStyleSheet.h"
30 #include "CSSStyleSheet.h"
31 #include "CachedResourceClientWalker.h"
32 #include "CachedStyleSheetClient.h"
33 #include "HTTPParsers.h"
34 #include "MemoryCache.h"
35 #include "SharedBuffer.h"
36 #include "StyleSheetContents.h"
37 #include "TextResourceDecoder.h"
38 #include <wtf/CurrentTime.h>
39 #include <wtf/Vector.h>
43 CachedCSSStyleSheet::CachedCSSStyleSheet(const ResourceRequest& resourceRequest, const String& charset)
44 : CachedResource(resourceRequest, CSSStyleSheet)
45 , m_decoder(TextResourceDecoder::create("text/css", charset))
47 // Prefer text/css but accept any type (dell.com serves a stylesheet
48 // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
49 setAccept("text/css,*/*;q=0.1");
52 CachedCSSStyleSheet::~CachedCSSStyleSheet()
54 if (m_parsedStyleSheetCache)
55 m_parsedStyleSheetCache->removedFromMemoryCache();
58 void CachedCSSStyleSheet::didAddClient(CachedResourceClient* c)
60 ASSERT(c->resourceClientType() == CachedStyleSheetClient::expectedType());
62 static_cast<CachedStyleSheetClient*>(c)->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
64 CachedResource::didAddClient(c);
67 void CachedCSSStyleSheet::setEncoding(const String& chs)
69 m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
72 String CachedCSSStyleSheet::encoding() const
74 return m_decoder->encoding().name();
77 const String CachedCSSStyleSheet::sheetText(bool enforceMIMEType, bool* hasValidMIMEType) const
79 ASSERT(!isPurgeable());
81 if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType, hasValidMIMEType))
84 if (!m_decodedSheetText.isNull())
85 return m_decodedSheetText;
87 // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
88 String sheetText = m_decoder->decode(m_data->data(), m_data->size());
89 sheetText += m_decoder->flush();
93 void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
99 setEncodedSize(m_data.get() ? m_data->size() : 0);
100 // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
102 m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
103 m_decodedSheetText += m_decoder->flush();
107 // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
108 m_decodedSheetText = String();
111 void CachedCSSStyleSheet::checkNotify()
116 CachedResourceClientWalker<CachedStyleSheetClient> w(m_clients);
117 while (CachedStyleSheetClient* c = w.next())
118 c->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
121 void CachedCSSStyleSheet::error(CachedResource::Status status)
124 ASSERT(errorOccurred());
129 bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const
134 if (!enforceMIMEType && !hasValidMIMEType)
137 // This check exactly matches Firefox. Note that we grab the Content-Type
138 // header directly because we want to see what the value is BEFORE content
139 // sniffing. Firefox does this by setting a "type hint" on the channel.
140 // This implementation should be observationally equivalent.
142 // This code defaults to allowing the stylesheet for non-HTTP protocols so
143 // folks can use standards mode for local HTML documents.
144 String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
145 bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
146 if (hasValidMIMEType)
147 *hasValidMIMEType = typeOK;
148 if (!enforceMIMEType)
153 void CachedCSSStyleSheet::destroyDecodedData()
155 if (!m_parsedStyleSheetCache)
158 m_parsedStyleSheetCache->removedFromMemoryCache();
159 m_parsedStyleSheetCache.clear();
163 if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
167 PassRefPtr<StyleSheetContents> CachedCSSStyleSheet::restoreParsedStyleSheet(const CSSParserContext& context)
169 if (!m_parsedStyleSheetCache)
171 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) {
172 m_parsedStyleSheetCache->removedFromMemoryCache();
173 m_parsedStyleSheetCache.clear();
177 ASSERT(m_parsedStyleSheetCache->isCacheable());
178 ASSERT(m_parsedStyleSheetCache->isInMemoryCache());
180 // Contexts must be identical so we know we would get the same exact result if we parsed again.
181 if (m_parsedStyleSheetCache->parserContext() != context)
184 didAccessDecodedData(currentTime());
186 return m_parsedStyleSheetCache;
189 void CachedCSSStyleSheet::saveParsedStyleSheet(PassRefPtr<StyleSheetContents> sheet)
191 ASSERT(sheet && sheet->isCacheable());
193 if (m_parsedStyleSheetCache)
194 m_parsedStyleSheetCache->removedFromMemoryCache();
195 m_parsedStyleSheetCache = sheet;
196 m_parsedStyleSheetCache->addedToMemoryCache();
198 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes());