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 "core/fetch/CSSStyleSheetResource.h"
30 #include "core/css/StyleSheetContents.h"
31 #include "core/fetch/FetchRequest.h"
32 #include "core/fetch/ResourceClientWalker.h"
33 #include "core/fetch/ResourceFetcher.h"
34 #include "core/fetch/StyleSheetResourceClient.h"
35 #include "platform/SharedBuffer.h"
36 #include "platform/network/HTTPParsers.h"
37 #include "wtf/CurrentTime.h"
41 ResourcePtr
<CSSStyleSheetResource
> CSSStyleSheetResource::fetch(FetchRequest
& request
, ResourceFetcher
* fetcher
)
43 ASSERT(request
.resourceRequest().frameType() == WebURLRequest::FrameTypeNone
);
44 request
.mutableResourceRequest().setRequestContext(WebURLRequest::RequestContextStyle
);
45 return toCSSStyleSheetResource(fetcher
->requestResource(request
, CSSStyleSheetResourceFactory()));
48 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest
& resourceRequest
, const String
& charset
)
49 : StyleSheetResource(resourceRequest
, CSSStyleSheet
, "text/css", charset
)
51 DEFINE_STATIC_LOCAL(const AtomicString
, acceptCSS
, ("text/css,*/*;q=0.1", AtomicString::ConstructFromLiteral
));
53 // Prefer text/css but accept any type (dell.com serves a stylesheet
54 // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>).
58 CSSStyleSheetResource::~CSSStyleSheetResource()
60 // Make sure dispose() was cllaed before destruction.
61 ASSERT(!m_parsedStyleSheetCache
);
64 void CSSStyleSheetResource::dispose()
66 if (m_parsedStyleSheetCache
)
67 m_parsedStyleSheetCache
->removedFromMemoryCache();
68 m_parsedStyleSheetCache
.clear();
71 DEFINE_TRACE(CSSStyleSheetResource
)
73 visitor
->trace(m_parsedStyleSheetCache
);
74 StyleSheetResource::trace(visitor
);
77 void CSSStyleSheetResource::didAddClient(ResourceClient
* c
)
79 ASSERT(c
->resourceClientType() == StyleSheetResourceClient::expectedType());
80 // Resource::didAddClient() must be before setCSSStyleSheet(),
81 // because setCSSStyleSheet() may cause scripts to be executed, which could destroy 'c' if it is an instance of HTMLLinkElement.
82 // see the comment of HTMLLinkElement::setCSSStyleSheet.
83 Resource::didAddClient(c
);
86 static_cast<StyleSheetResourceClient
*>(c
)->setCSSStyleSheet(m_resourceRequest
.url(), m_response
.url(), encoding(), this);
89 const String
CSSStyleSheetResource::sheetText(MIMETypeCheck mimeTypeCheck
) const
91 ASSERT(!isPurgeable());
93 if (!m_data
|| m_data
->isEmpty() || !canUseSheet(mimeTypeCheck
))
96 if (!m_decodedSheetText
.isNull())
97 return m_decodedSheetText
;
99 // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory
100 return decodedText();
103 const AtomicString
CSSStyleSheetResource::mimeType() const
105 return extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type")).lower();
108 void CSSStyleSheetResource::checkNotify()
110 // Decode the data to find out the encoding and keep the sheet text around during checkNotify()
112 m_decodedSheetText
= decodedText();
114 ResourceClientWalker
<StyleSheetResourceClient
> w(m_clients
);
115 while (StyleSheetResourceClient
* c
= w
.next())
116 c
->setCSSStyleSheet(m_resourceRequest
.url(), m_response
.url(), encoding(), this);
117 // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate.
118 m_decodedSheetText
= String();
121 bool CSSStyleSheetResource::isSafeToUnlock() const
123 return m_data
->hasOneRef();
126 void CSSStyleSheetResource::destroyDecodedDataIfPossible()
128 if (!m_parsedStyleSheetCache
)
131 m_parsedStyleSheetCache
->removedFromMemoryCache();
132 m_parsedStyleSheetCache
.clear();
137 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck
) const
142 // This check exactly matches Firefox. Note that we grab the Content-Type
143 // header directly because we want to see what the value is BEFORE content
144 // sniffing. Firefox does this by setting a "type hint" on the channel.
145 // This implementation should be observationally equivalent.
147 // This code defaults to allowing the stylesheet for non-HTTP protocols so
148 // folks can use standards mode for local HTML documents.
149 if (mimeTypeCheck
== MIMETypeCheck::Lax
)
151 return mimeType().isEmpty() || equalIgnoringCase(mimeType(), "text/css") || equalIgnoringCase(mimeType(), "application/x-unknown-content-type");
154 PassRefPtrWillBeRawPtr
<StyleSheetContents
> CSSStyleSheetResource::restoreParsedStyleSheet(const CSSParserContext
& context
)
156 if (!m_parsedStyleSheetCache
)
158 if (m_parsedStyleSheetCache
->hasFailedOrCanceledSubresources()) {
159 m_parsedStyleSheetCache
->removedFromMemoryCache();
160 m_parsedStyleSheetCache
.clear();
164 ASSERT(m_parsedStyleSheetCache
->isCacheable());
165 ASSERT(m_parsedStyleSheetCache
->isInMemoryCache());
167 // Contexts must be identical so we know we would get the same exact result if we parsed again.
168 if (m_parsedStyleSheetCache
->parserContext() != context
)
171 didAccessDecodedData();
173 return m_parsedStyleSheetCache
;
176 void CSSStyleSheetResource::saveParsedStyleSheet(PassRefPtrWillBeRawPtr
<StyleSheetContents
> sheet
)
178 ASSERT(sheet
&& sheet
->isCacheable());
180 if (m_parsedStyleSheetCache
)
181 m_parsedStyleSheetCache
->removedFromMemoryCache();
182 m_parsedStyleSheetCache
= sheet
;
183 m_parsedStyleSheetCache
->addedToMemoryCache();
185 setDecodedSize(m_parsedStyleSheetCache
->estimatedSizeInBytes());