| 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 | |
| 24 sheets and html pages from the web. It has a memory cache for these objects. | |
| 25 */ | |
| 26 | |
| 27 #include "core/fetch/CSSStyleSheetResource.h" | |
| 28 | |
| 29 #include "core/css/StyleSheetContents.h" | |
| 30 #include "core/fetch/FetchRequest.h" | |
| 31 #include "core/fetch/MemoryCache.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 "wtf/CurrentTime.h" | |
| 37 | |
| 38 namespace blink { | |
| 39 | |
| 40 CSSStyleSheetResource* CSSStyleSheetResource::fetch(FetchRequest& request, | |
| 41 ResourceFetcher* fetcher) { | |
| 42 DCHECK_EQ(request.resourceRequest().frameType(), | |
| 43 WebURLRequest::FrameTypeNone); | |
| 44 request.mutableResourceRequest().setRequestContext( | |
| 45 WebURLRequest::RequestContextStyle); | |
| 46 CSSStyleSheetResource* resource = toCSSStyleSheetResource( | |
| 47 fetcher->requestResource(request, CSSStyleSheetResourceFactory())); | |
| 48 // TODO(kouhei): Dedupe this logic w/ ScriptResource::fetch | |
| 49 if (resource && !request.integrityMetadata().isEmpty()) | |
| 50 resource->setIntegrityMetadata(request.integrityMetadata()); | |
| 51 return resource; | |
| 52 } | |
| 53 | |
| 54 CSSStyleSheetResource* CSSStyleSheetResource::createForTest( | |
| 55 const ResourceRequest& request, | |
| 56 const String& charset) { | |
| 57 return new CSSStyleSheetResource(request, ResourceLoaderOptions(), charset); | |
| 58 } | |
| 59 | |
| 60 CSSStyleSheetResource::CSSStyleSheetResource( | |
| 61 const ResourceRequest& resourceRequest, | |
| 62 const ResourceLoaderOptions& options, | |
| 63 const String& charset) | |
| 64 : StyleSheetResource(resourceRequest, | |
| 65 CSSStyleSheet, | |
| 66 options, | |
| 67 "text/css", | |
| 68 charset), | |
| 69 m_didNotifyFirstData(false) {} | |
| 70 | |
| 71 CSSStyleSheetResource::~CSSStyleSheetResource() {} | |
| 72 | |
| 73 void CSSStyleSheetResource::setParsedStyleSheetCache( | |
| 74 StyleSheetContents* newSheet) { | |
| 75 if (m_parsedStyleSheetCache) | |
| 76 m_parsedStyleSheetCache->clearReferencedFromResource(); | |
| 77 m_parsedStyleSheetCache = newSheet; | |
| 78 if (m_parsedStyleSheetCache) | |
| 79 m_parsedStyleSheetCache->setReferencedFromResource(this); | |
| 80 | |
| 81 // Updates the decoded size to take parsed stylesheet cache into account. | |
| 82 updateDecodedSize(); | |
| 83 } | |
| 84 | |
| 85 DEFINE_TRACE(CSSStyleSheetResource) { | |
| 86 visitor->trace(m_parsedStyleSheetCache); | |
| 87 StyleSheetResource::trace(visitor); | |
| 88 } | |
| 89 | |
| 90 void CSSStyleSheetResource::didAddClient(ResourceClient* c) { | |
| 91 DCHECK(StyleSheetResourceClient::isExpectedType(c)); | |
| 92 // Resource::didAddClient() must be before setCSSStyleSheet(), because | |
| 93 // setCSSStyleSheet() may cause scripts to be executed, which could destroy | |
| 94 // 'c' if it is an instance of HTMLLinkElement. see the comment of | |
| 95 // HTMLLinkElement::setCSSStyleSheet. | |
| 96 Resource::didAddClient(c); | |
| 97 | |
| 98 if (hasClient(c) && m_didNotifyFirstData) | |
| 99 static_cast<StyleSheetResourceClient*>(c)->didAppendFirstData(this); | |
| 100 | |
| 101 // |c| might be removed in didAppendFirstData, so ensure it is still a client. | |
| 102 if (hasClient(c) && !isLoading()) { | |
| 103 static_cast<StyleSheetResourceClient*>(c)->setCSSStyleSheet( | |
| 104 resourceRequest().url(), response().url(), encoding(), this); | |
| 105 } | |
| 106 } | |
| 107 | |
| 108 const String CSSStyleSheetResource::sheetText( | |
| 109 MIMETypeCheck mimeTypeCheck) const { | |
| 110 if (!canUseSheet(mimeTypeCheck)) | |
| 111 return String(); | |
| 112 | |
| 113 // Use cached decoded sheet text when available | |
| 114 if (!m_decodedSheetText.isNull()) { | |
| 115 // We should have the decoded sheet text cached when the resource is fully | |
| 116 // loaded. | |
| 117 DCHECK_EQ(getStatus(), Resource::Cached); | |
| 118 | |
| 119 return m_decodedSheetText; | |
| 120 } | |
| 121 | |
| 122 if (!data() || data()->isEmpty()) | |
| 123 return String(); | |
| 124 | |
| 125 return decodedText(); | |
| 126 } | |
| 127 | |
| 128 void CSSStyleSheetResource::appendData(const char* data, size_t length) { | |
| 129 Resource::appendData(data, length); | |
| 130 if (m_didNotifyFirstData) | |
| 131 return; | |
| 132 ResourceClientWalker<StyleSheetResourceClient> w(clients()); | |
| 133 while (StyleSheetResourceClient* c = w.next()) | |
| 134 c->didAppendFirstData(this); | |
| 135 m_didNotifyFirstData = true; | |
| 136 } | |
| 137 | |
| 138 void CSSStyleSheetResource::checkNotify() { | |
| 139 // Decode the data to find out the encoding and cache the decoded sheet text. | |
| 140 if (data()) | |
| 141 setDecodedSheetText(decodedText()); | |
| 142 | |
| 143 ResourceClientWalker<StyleSheetResourceClient> w(clients()); | |
| 144 while (StyleSheetResourceClient* c = w.next()) { | |
| 145 markClientFinished(c); | |
| 146 c->setCSSStyleSheet(resourceRequest().url(), response().url(), encoding(), | |
| 147 this); | |
| 148 } | |
| 149 | |
| 150 // Clear raw bytes as now we have the full decoded sheet text. | |
| 151 // We wait for all LinkStyle::setCSSStyleSheet to run (at least once) | |
| 152 // as SubresourceIntegrity checks require raw bytes. | |
| 153 // Note that LinkStyle::setCSSStyleSheet can be called from didAddClient too, | |
| 154 // but is safe as we should have a cached ResourceIntegrityDisposition. | |
| 155 clearData(); | |
| 156 } | |
| 157 | |
| 158 void CSSStyleSheetResource::destroyDecodedDataIfPossible() { | |
| 159 if (!m_parsedStyleSheetCache) | |
| 160 return; | |
| 161 | |
| 162 setParsedStyleSheetCache(nullptr); | |
| 163 } | |
| 164 | |
| 165 void CSSStyleSheetResource::destroyDecodedDataForFailedRevalidation() { | |
| 166 setDecodedSheetText(String()); | |
| 167 destroyDecodedDataIfPossible(); | |
| 168 } | |
| 169 | |
| 170 bool CSSStyleSheetResource::canUseSheet(MIMETypeCheck mimeTypeCheck) const { | |
| 171 if (errorOccurred()) | |
| 172 return false; | |
| 173 | |
| 174 // This check exactly matches Firefox. Note that we grab the Content-Type | |
| 175 // header directly because we want to see what the value is BEFORE content | |
| 176 // sniffing. Firefox does this by setting a "type hint" on the channel. This | |
| 177 // implementation should be observationally equivalent. | |
| 178 // | |
| 179 // This code defaults to allowing the stylesheet for non-HTTP protocols so | |
| 180 // folks can use standards mode for local HTML documents. | |
| 181 if (mimeTypeCheck == MIMETypeCheck::Lax) | |
| 182 return true; | |
| 183 AtomicString contentType = httpContentType(); | |
| 184 return contentType.isEmpty() || equalIgnoringCase(contentType, "text/css") || | |
| 185 equalIgnoringCase(contentType, "application/x-unknown-content-type"); | |
| 186 } | |
| 187 | |
| 188 StyleSheetContents* CSSStyleSheetResource::restoreParsedStyleSheet( | |
| 189 const CSSParserContext& context) { | |
| 190 if (!m_parsedStyleSheetCache) | |
| 191 return nullptr; | |
| 192 if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) { | |
| 193 setParsedStyleSheetCache(nullptr); | |
| 194 return nullptr; | |
| 195 } | |
| 196 | |
| 197 DCHECK(m_parsedStyleSheetCache->isCacheableForResource()); | |
| 198 DCHECK(m_parsedStyleSheetCache->isReferencedFromResource()); | |
| 199 | |
| 200 // Contexts must be identical so we know we would get the same exact result if | |
| 201 // we parsed again. | |
| 202 if (m_parsedStyleSheetCache->parserContext() != context) | |
| 203 return nullptr; | |
| 204 | |
| 205 return m_parsedStyleSheetCache; | |
| 206 } | |
| 207 | |
| 208 void CSSStyleSheetResource::saveParsedStyleSheet(StyleSheetContents* sheet) { | |
| 209 DCHECK(sheet); | |
| 210 DCHECK(sheet->isCacheableForResource()); | |
| 211 | |
| 212 if (!memoryCache()->contains(this)) { | |
| 213 // This stylesheet resource did conflict with another resource and was not | |
| 214 // added to the cache. | |
| 215 setParsedStyleSheetCache(nullptr); | |
| 216 return; | |
| 217 } | |
| 218 setParsedStyleSheetCache(sheet); | |
| 219 } | |
| 220 | |
| 221 void CSSStyleSheetResource::setDecodedSheetText( | |
| 222 const String& decodedSheetText) { | |
| 223 m_decodedSheetText = decodedSheetText; | |
| 224 updateDecodedSize(); | |
| 225 } | |
| 226 | |
| 227 void CSSStyleSheetResource::updateDecodedSize() { | |
| 228 size_t decodedSize = m_decodedSheetText.charactersSizeInBytes(); | |
| 229 if (m_parsedStyleSheetCache) | |
| 230 decodedSize += m_parsedStyleSheetCache->estimatedSizeInBytes(); | |
| 231 setDecodedSize(decodedSize); | |
| 232 } | |
| 233 | |
| 234 } // namespace blink | |
| OLD | NEW |