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 "core/fetch/CSSStyleSheetResource.h"
29
30 #include "core/css/StyleSheetContents.h"
31 #include "core/fetch/ResourceClientWalker.h"
32 #include "core/fetch/StyleSheetResourceClient.h"
33 #include "platform/SharedBuffer.h"
34 #include "platform/network/HTTPParsers.h"
35 #include "wtf/CurrentTime.h"
36 #include "wtf/Vector.h"
37
38 namespace WebCore {
39
CSSStyleSheetResource(const ResourceRequest & resourceRequest,const String & charset)40 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest& resourceRequest, const String& charset)
41 : StyleSheetResource(resourceRequest, CSSStyleSheet, "text/css", charset)
42 {
43 DEFINE_STATIC_LOCAL(const AtomicString, acceptCSS, ("text/css,*/*;q=0.1", AtomicString::ConstructFromLiteral));
44
45 // Prefer text/css but accept any type (dell.com serves a stylesheet
46 // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
47 setAccept(acceptCSS);
48 }
49
~CSSStyleSheetResource()50 CSSStyleSheetResource::~CSSStyleSheetResource()
51 {
52 if (m_parsedStyleSheetCache)
53 m_parsedStyleSheetCache->removedFromMemoryCache();
54 }
55
didAddClient(ResourceClient * c)56 void CSSStyleSheetResource::didAddClient(ResourceClient* c)
57 {
58 ASSERT(c->resourceClientType() == StyleSheetResourceClient::expectedType());
59 // Resource::didAddClient() must be before setCSSStyleSheet(),
60 // because setCSSStyleSheet() may cause scripts to be executed, which could destroy 'c' if it is an instance of HTMLLinkElement.
61 // see the comment of HTMLLinkElement::setCSSStyleSheet.
62 Resource::didAddClient(c);
63
64 if (!isLoading())
65 static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), encoding(), this);
66 }
67
sheetText(bool enforceMIMEType,bool * hasValidMIMEType) const68 const String CSSStyleSheetResource::sheetText(bool enforceMIMEType, bool* hasValidMIMEType) const
69 {
70 ASSERT(!isPurgeable());
71
72 if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType, hasValidMIMEType))
73 return String();
74
75 if (!m_decodedSheetText.isNull())
76 return m_decodedSheetText;
77
78 // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
79 return decodedText();
80 }
81
checkNotify()82 void CSSStyleSheetResource::checkNotify()
83 {
84 // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
85 if (m_data)
86 m_decodedSheetText = decodedText();
87
88 ResourceClientWalker<StyleSheetResourceClient> w(m_clients);
89 while (StyleSheetResourceClient* c = w.next())
90 c->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), encoding(), this);
91 // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
92 m_decodedSheetText = String();
93 }
94
isSafeToUnlock() const95 bool CSSStyleSheetResource::isSafeToUnlock() const
96 {
97 return m_data->hasOneRef();
98 }
99
destroyDecodedDataIfPossible()100 void CSSStyleSheetResource::destroyDecodedDataIfPossible()
101 {
102 if (!m_parsedStyleSheetCache)
103 return;
104
105 m_parsedStyleSheetCache->removedFromMemoryCache();
106 m_parsedStyleSheetCache.clear();
107
108 setDecodedSize(0);
109 }
110
canUseSheet(bool enforceMIMEType,bool * hasValidMIMEType) const111 bool CSSStyleSheetResource::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const
112 {
113 if (errorOccurred())
114 return false;
115
116 if (!enforceMIMEType && !hasValidMIMEType)
117 return true;
118
119 // This check exactly matches Firefox. Note that we grab the Content-Type
120 // header directly because we want to see what the value is BEFORE content
121 // sniffing. Firefox does this by setting a "type hint" on the channel.
122 // This implementation should be observationally equivalent.
123 //
124 // This code defaults to allowing the stylesheet for non-HTTP protocols so
125 // folks can use standards mode for local HTML documents.
126 const AtomicString& mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
127 bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
128 if (hasValidMIMEType)
129 *hasValidMIMEType = typeOK;
130 if (!enforceMIMEType)
131 return true;
132 return typeOK;
133 }
134
restoreParsedStyleSheet(const CSSParserContext & context)135 PassRefPtrWillBeRawPtr<StyleSheetContents> CSSStyleSheetResource::restoreParsedStyleSheet(const CSSParserContext& context)
136 {
137 if (!m_parsedStyleSheetCache)
138 return nullptr;
139 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) {
140 m_parsedStyleSheetCache->removedFromMemoryCache();
141 m_parsedStyleSheetCache.clear();
142 return nullptr;
143 }
144
145 ASSERT(m_parsedStyleSheetCache->isCacheable());
146 ASSERT(m_parsedStyleSheetCache->isInMemoryCache());
147
148 // Contexts must be identical so we know we would get the same exact result if we parsed again.
149 if (m_parsedStyleSheetCache->parserContext() != context)
150 return nullptr;
151
152 didAccessDecodedData();
153
154 return m_parsedStyleSheetCache;
155 }
156
saveParsedStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet)157 void CSSStyleSheetResource::saveParsedStyleSheet(PassRefPtrWillBeRawPtr<StyleSheetContents> sheet)
158 {
159 ASSERT(sheet && sheet->isCacheable());
160
161 if (m_parsedStyleSheetCache)
162 m_parsedStyleSheetCache->removedFromMemoryCache();
163 m_parsedStyleSheetCache = sheet;
164 m_parsedStyleSheetCache->addedToMemoryCache();
165
166 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes());
167 }
168
169 }
170