OLD | NEW |
| (Empty) |
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 sheet
s 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/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" | |
38 | |
39 namespace blink { | |
40 | |
41 ResourcePtr<CSSStyleSheetResource> CSSStyleSheetResource::fetch(FetchRequest& re
quest, ResourceFetcher* fetcher) | |
42 { | |
43 ASSERT(request.resourceRequest().frameType() == WebURLRequest::FrameTypeNone
); | |
44 request.mutableResourceRequest().setRequestContext(WebURLRequest::RequestCon
textStyle); | |
45 return toCSSStyleSheetResource(fetcher->requestResource(request, CSSStyleShe
etResourceFactory())); | |
46 } | |
47 | |
48 CSSStyleSheetResource::CSSStyleSheetResource(const ResourceRequest& resourceRequ
est, const String& charset) | |
49 : StyleSheetResource(resourceRequest, CSSStyleSheet, "text/css", charset) | |
50 { | |
51 DEFINE_STATIC_LOCAL(const AtomicString, acceptCSS, ("text/css,*/*;q=0.1", At
omicString::ConstructFromLiteral)); | |
52 | |
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>). | |
55 setAccept(acceptCSS); | |
56 } | |
57 | |
58 CSSStyleSheetResource::~CSSStyleSheetResource() | |
59 { | |
60 // Make sure dispose() was cllaed before destruction. | |
61 ASSERT(!m_parsedStyleSheetCache); | |
62 } | |
63 | |
64 void CSSStyleSheetResource::dispose() | |
65 { | |
66 if (m_parsedStyleSheetCache) | |
67 m_parsedStyleSheetCache->removedFromMemoryCache(); | |
68 m_parsedStyleSheetCache.clear(); | |
69 } | |
70 | |
71 DEFINE_TRACE(CSSStyleSheetResource) | |
72 { | |
73 visitor->trace(m_parsedStyleSheetCache); | |
74 StyleSheetResource::trace(visitor); | |
75 } | |
76 | |
77 void CSSStyleSheetResource::didAddClient(ResourceClient* c) | |
78 { | |
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); | |
84 | |
85 if (!isLoading()) | |
86 static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet(m_resourceRe
quest.url(), m_response.url(), encoding(), this); | |
87 } | |
88 | |
89 const String CSSStyleSheetResource::sheetText(MIMETypeCheck mimeTypeCheck) const | |
90 { | |
91 ASSERT(!isPurgeable()); | |
92 | |
93 if (!m_data || m_data->isEmpty() || !canUseSheet(mimeTypeCheck)) | |
94 return String(); | |
95 | |
96 if (!m_decodedSheetText.isNull()) | |
97 return m_decodedSheetText; | |
98 | |
99 // Don't cache the decoded text, regenerating is cheap and it can use quite
a bit of memory | |
100 return decodedText(); | |
101 } | |
102 | |
103 const AtomicString CSSStyleSheetResource::mimeType() const | |
104 { | |
105 return extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type
")).lower(); | |
106 } | |
107 | |
108 void CSSStyleSheetResource::checkNotify() | |
109 { | |
110 // Decode the data to find out the encoding and keep the sheet text around d
uring checkNotify() | |
111 if (m_data) | |
112 m_decodedSheetText = decodedText(); | |
113 | |
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 a
nd is cheap to regenerate. | |
118 m_decodedSheetText = String(); | |
119 } | |
120 | |
121 bool CSSStyleSheetResource::isSafeToUnlock() const | |
122 { | |
123 return m_data->hasOneRef(); | |
124 } | |
125 | |
126 void CSSStyleSheetResource::destroyDecodedDataIfPossible() | |
127 { | |
128 if (!m_parsedStyleSheetCache) | |
129 return; | |
130 | |
131 m_parsedStyleSheetCache->removedFromMemoryCache(); | |
132 m_parsedStyleSheetCache.clear(); | |
133 | |
134 setDecodedSize(0); | |
135 } | |
136 | |
137 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const | |
138 { | |
139 if (errorOccurred()) | |
140 return false; | |
141 | |
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. | |
146 // | |
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) | |
150 return true; | |
151 return mimeType().isEmpty() || equalIgnoringCase(mimeType(), "text/css") ||
equalIgnoringCase(mimeType(), "application/x-unknown-content-type"); | |
152 } | |
153 | |
154 PassRefPtrWillBeRawPtr<StyleSheetContents> CSSStyleSheetResource::restoreParsedS
tyleSheet(const CSSParserContext& context) | |
155 { | |
156 if (!m_parsedStyleSheetCache) | |
157 return nullptr; | |
158 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) { | |
159 m_parsedStyleSheetCache->removedFromMemoryCache(); | |
160 m_parsedStyleSheetCache.clear(); | |
161 return nullptr; | |
162 } | |
163 | |
164 ASSERT(m_parsedStyleSheetCache->isCacheable()); | |
165 ASSERT(m_parsedStyleSheetCache->isInMemoryCache()); | |
166 | |
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) | |
169 return nullptr; | |
170 | |
171 didAccessDecodedData(); | |
172 | |
173 return m_parsedStyleSheetCache; | |
174 } | |
175 | |
176 void CSSStyleSheetResource::saveParsedStyleSheet(PassRefPtrWillBeRawPtr<StyleShe
etContents> sheet) | |
177 { | |
178 ASSERT(sheet && sheet->isCacheable()); | |
179 | |
180 if (m_parsedStyleSheetCache) | |
181 m_parsedStyleSheetCache->removedFromMemoryCache(); | |
182 m_parsedStyleSheetCache = sheet; | |
183 m_parsedStyleSheetCache->addedToMemoryCache(); | |
184 | |
185 setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes()); | |
186 } | |
187 | |
188 } | |
OLD | NEW |