1 /*
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.
7
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.
12
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.
17
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.
22
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.
25 */
26
27 #include "config.h"
28 #include "CachedCSSStyleSheet.h"
29
30 #include "CachedResourceClient.h"
31 #include "CachedResourceClientWalker.h"
32 #include "HTTPParsers.h"
33 #include "TextResourceDecoder.h"
34 #include "loader.h"
35 #include <wtf/Vector.h>
36
37 namespace WebCore {
38
CachedCSSStyleSheet(const String & url,const String & charset)39 CachedCSSStyleSheet::CachedCSSStyleSheet(const String& url, const String& charset)
40 : CachedResource(url, CSSStyleSheet)
41 , m_decoder(TextResourceDecoder::create("text/css", charset))
42 {
43 // Prefer text/css but accept any type (dell.com serves a stylesheet
44 // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
45 setAccept("text/css,*/*;q=0.1");
46 }
47
~CachedCSSStyleSheet()48 CachedCSSStyleSheet::~CachedCSSStyleSheet()
49 {
50 }
51
didAddClient(CachedResourceClient * c)52 void CachedCSSStyleSheet::didAddClient(CachedResourceClient *c)
53 {
54 if (!m_loading)
55 c->setCSSStyleSheet(m_url, m_decoder->encoding().name(), this);
56 }
57
allClientsRemoved()58 void CachedCSSStyleSheet::allClientsRemoved()
59 {
60 if (isSafeToMakePurgeable())
61 makePurgeable(true);
62 }
63
setEncoding(const String & chs)64 void CachedCSSStyleSheet::setEncoding(const String& chs)
65 {
66 m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
67 }
68
encoding() const69 String CachedCSSStyleSheet::encoding() const
70 {
71 return m_decoder->encoding().name();
72 }
73
sheetText(bool enforceMIMEType) const74 const String CachedCSSStyleSheet::sheetText(bool enforceMIMEType) const
75 {
76 ASSERT(!isPurgeable());
77
78 if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType))
79 return String();
80
81 if (!m_decodedSheetText.isNull())
82 return m_decodedSheetText;
83
84 // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
85 String sheetText = m_decoder->decode(m_data->data(), m_data->size());
86 sheetText += m_decoder->flush();
87 return sheetText;
88 }
89
data(PassRefPtr<SharedBuffer> data,bool allDataReceived)90 void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
91 {
92 if (!allDataReceived)
93 return;
94
95 m_data = data;
96 setEncodedSize(m_data.get() ? m_data->size() : 0);
97 // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
98 if (m_data) {
99 m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
100 m_decodedSheetText += m_decoder->flush();
101 }
102 m_loading = false;
103 checkNotify();
104 // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
105 m_decodedSheetText = String();
106 }
107
checkNotify()108 void CachedCSSStyleSheet::checkNotify()
109 {
110 if (m_loading)
111 return;
112
113 CachedResourceClientWalker w(m_clients);
114 while (CachedResourceClient *c = w.next())
115 c->setCSSStyleSheet(m_response.url().string(), m_decoder->encoding().name(), this);
116 }
117
error()118 void CachedCSSStyleSheet::error()
119 {
120 m_loading = false;
121 m_errorOccurred = true;
122 checkNotify();
123 }
124
canUseSheet(bool enforceMIMEType) const125 bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType) const
126 {
127 if (errorOccurred())
128 return false;
129
130 if (!enforceMIMEType)
131 return true;
132
133 // This check exactly matches Firefox. Note that we grab the Content-Type
134 // header directly because we want to see what the value is BEFORE content
135 // sniffing. Firefox does this by setting a "type hint" on the channel.
136 // This implementation should be observationally equivalent.
137 //
138 // This code defaults to allowing the stylesheet for non-HTTP protocols so
139 // folks can use standards mode for local HTML documents.
140 String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
141 return mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
142 }
143
144 }
145