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 "MemoryCache.h"
31 #include "CachedResourceClientWalker.h"
32 #include "CachedStyleSheetClient.h"
33 #include "HTTPParsers.h"
34 #include "TextResourceDecoder.h"
35 #include "SharedBuffer.h"
36 #include <wtf/Vector.h>
40 CachedCSSStyleSheet::CachedCSSStyleSheet(const ResourceRequest& resourceRequest, const String& charset)
41 : CachedResource(resourceRequest, CSSStyleSheet)
42 , m_decoder(TextResourceDecoder::create("text/css", charset))
44 // Prefer text/css but accept any type (dell.com serves a stylesheet
45 // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
46 setAccept("text/css,*/*;q=0.1");
49 CachedCSSStyleSheet::~CachedCSSStyleSheet()
53 void CachedCSSStyleSheet::didAddClient(CachedResourceClient* c)
55 ASSERT(c->resourceClientType() == CachedStyleSheetClient::expectedType());
57 static_cast<CachedStyleSheetClient*>(c)->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
60 void CachedCSSStyleSheet::allClientsRemoved()
62 if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
66 void CachedCSSStyleSheet::setEncoding(const String& chs)
68 m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
71 String CachedCSSStyleSheet::encoding() const
73 return m_decoder->encoding().name();
76 const String CachedCSSStyleSheet::sheetText(bool enforceMIMEType, bool* hasValidMIMEType) const
78 ASSERT(!isPurgeable());
80 if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType, hasValidMIMEType))
83 if (!m_decodedSheetText.isNull())
84 return m_decodedSheetText;
86 // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
87 String sheetText = m_decoder->decode(m_data->data(), m_data->size());
88 sheetText += m_decoder->flush();
92 void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
98 setEncodedSize(m_data.get() ? m_data->size() : 0);
99 // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
101 m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
102 m_decodedSheetText += m_decoder->flush();
106 // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
107 m_decodedSheetText = String();
110 void CachedCSSStyleSheet::checkNotify()
115 CachedResourceClientWalker<CachedStyleSheetClient> w(m_clients);
116 while (CachedStyleSheetClient* c = w.next())
117 c->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this);
120 void CachedCSSStyleSheet::error(CachedResource::Status status)
123 ASSERT(errorOccurred());
128 bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const
133 if (!enforceMIMEType && !hasValidMIMEType)
136 // This check exactly matches Firefox. Note that we grab the Content-Type
137 // header directly because we want to see what the value is BEFORE content
138 // sniffing. Firefox does this by setting a "type hint" on the channel.
139 // This implementation should be observationally equivalent.
141 // This code defaults to allowing the stylesheet for non-HTTP protocols so
142 // folks can use standards mode for local HTML documents.
143 String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
144 bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
145 if (hasValidMIMEType)
146 *hasValidMIMEType = typeOK;
147 if (!enforceMIMEType)