diff options
Diffstat (limited to 'Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp')
-rw-r--r-- | Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp | 101 |
1 files changed, 49 insertions, 52 deletions
diff --git a/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp b/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp index 69bce8f40..af40cc204 100644 --- a/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp +++ b/Source/WebCore/loader/cache/CachedCSSStyleSheet.cpp @@ -3,7 +3,7 @@ Copyright (C) 2001 Dirk Mueller (mueller@kde.org) Copyright (C) 2002 Waldo Bastian (bastian@kde.org) Copyright (C) 2006 Samuel Weinig (sam.weinig@gmail.com) - Copyright (C) 2004, 2005, 2006 Apple Computer, Inc. + Copyright (C) 2004-2017 Apple Inc. All rights reserved. This library is free software; you can redistribute it and/or modify it under the terms of the GNU Library General Public @@ -19,9 +19,6 @@ along with this library; see the file COPYING.LIB. If not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - This class provides all functionality needed for loading images, style sheets and html - pages from the web. It has a memory cache for these objects. */ #include "config.h" @@ -29,24 +26,22 @@ #include "CSSStyleSheet.h" #include "CachedResourceClientWalker.h" +#include "CachedResourceRequest.h" #include "CachedStyleSheetClient.h" +#include "HTTPHeaderNames.h" #include "HTTPParsers.h" #include "MemoryCache.h" -#include "ResourceBuffer.h" +#include "SharedBuffer.h" #include "StyleSheetContents.h" #include "TextResourceDecoder.h" #include <wtf/CurrentTime.h> -#include <wtf/Vector.h> namespace WebCore { -CachedCSSStyleSheet::CachedCSSStyleSheet(const ResourceRequest& resourceRequest, const String& charset) - : CachedResource(resourceRequest, CSSStyleSheet) - , m_decoder(TextResourceDecoder::create("text/css", charset)) +CachedCSSStyleSheet::CachedCSSStyleSheet(CachedResourceRequest&& request, SessionID sessionID) + : CachedResource(WTFMove(request), CSSStyleSheet, sessionID) + , m_decoder(TextResourceDecoder::create("text/css", request.charset())) { - // Prefer text/css but accept any type (dell.com serves a stylesheet - // as text/html; see <http://bugs.webkit.org/show_bug.cgi?id=11451>). - setAccept("text/css,*/*;q=0.1"); } CachedCSSStyleSheet::~CachedCSSStyleSheet() @@ -55,16 +50,16 @@ CachedCSSStyleSheet::~CachedCSSStyleSheet() m_parsedStyleSheetCache->removedFromMemoryCache(); } -void CachedCSSStyleSheet::didAddClient(CachedResourceClient* c) +void CachedCSSStyleSheet::didAddClient(CachedResourceClient& client) { - ASSERT(c->resourceClientType() == CachedStyleSheetClient::expectedType()); + ASSERT(client.resourceClientType() == CachedStyleSheetClient::expectedType()); // CachedResource::didAddClient() must be before setCSSStyleSheet(), // because setCSSStyleSheet() may cause scripts to be executed, which could destroy 'c' if it is an instance of HTMLLinkElement. // see the comment of HTMLLinkElement::setCSSStyleSheet. - CachedResource::didAddClient(c); + CachedResource::didAddClient(client); if (!isLoading()) - static_cast<CachedStyleSheetClient*>(c)->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this); + static_cast<CachedStyleSheetClient&>(client).setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this); } void CachedCSSStyleSheet::setEncoding(const String& chs) @@ -76,32 +71,39 @@ String CachedCSSStyleSheet::encoding() const { return m_decoder->encoding().name(); } - -const String CachedCSSStyleSheet::sheetText(bool enforceMIMEType, bool* hasValidMIMEType) const -{ - ASSERT(!isPurgeable()); - if (!m_data || m_data->isEmpty() || !canUseSheet(enforceMIMEType, hasValidMIMEType)) +const String CachedCSSStyleSheet::sheetText(MIMETypeCheck mimeTypeCheck, bool* hasValidMIMEType) const +{ + if (!m_data || m_data->isEmpty() || !canUseSheet(mimeTypeCheck, hasValidMIMEType)) return String(); - + if (!m_decodedSheetText.isNull()) return m_decodedSheetText; - + // Don't cache the decoded text, regenerating is cheap and it can use quite a bit of memory - String sheetText = m_decoder->decode(m_data->data(), m_data->size()); - sheetText.append(m_decoder->flush()); - return sheetText; + return m_decoder->decodeAndFlush(m_data->data(), m_data->size()); } -void CachedCSSStyleSheet::finishLoading(ResourceBuffer* data) +void CachedCSSStyleSheet::setBodyDataFrom(const CachedResource& resource) +{ + ASSERT(resource.type() == type()); + const CachedCSSStyleSheet& sheet = static_cast<const CachedCSSStyleSheet&>(resource); + + CachedResource::setBodyDataFrom(resource); + + m_decoder = sheet.m_decoder; + m_decodedSheetText = sheet.m_decodedSheetText; + if (sheet.m_parsedStyleSheetCache) + saveParsedStyleSheet(*sheet.m_parsedStyleSheetCache); +} + +void CachedCSSStyleSheet::finishLoading(SharedBuffer* data) { m_data = data; - setEncodedSize(m_data.get() ? m_data->size() : 0); + setEncodedSize(data ? data->size() : 0); // Decode the data to find out the encoding and keep the sheet text around during checkNotify() - if (m_data) { - m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size()); - m_decodedSheetText.append(m_decoder->flush()); - } + if (data) + m_decodedSheetText = m_decoder->decodeAndFlush(data->data(), data->size()); setLoading(false); checkNotify(); // Clear the decoded text as it is unlikely to be needed immediately again and is cheap to regenerate. @@ -118,12 +120,12 @@ void CachedCSSStyleSheet::checkNotify() c->setCSSStyleSheet(m_resourceRequest.url(), m_response.url(), m_decoder->encoding().name(), this); } -bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const +bool CachedCSSStyleSheet::canUseSheet(MIMETypeCheck mimeTypeCheck, bool* hasValidMIMEType) const { if (errorOccurred()) return false; - - if (!enforceMIMEType && !hasValidMIMEType) + + if (mimeTypeCheck == MIMETypeCheck::Lax) return true; // This check exactly matches Firefox. Note that we grab the Content-Type @@ -133,12 +135,10 @@ bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType, bool* hasValidMIMETy // // This code defaults to allowing the stylesheet for non-HTTP protocols so // folks can use standards mode for local HTML documents. - String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type")); - bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type"); + String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField(HTTPHeaderName::ContentType)); + bool typeOK = mimeType.isEmpty() || equalLettersIgnoringASCIICase(mimeType, "text/css") || equalLettersIgnoringASCIICase(mimeType, "application/x-unknown-content-type"); if (hasValidMIMEType) *hasValidMIMEType = typeOK; - if (!enforceMIMEType) - return true; return typeOK; } @@ -148,22 +148,19 @@ void CachedCSSStyleSheet::destroyDecodedData() return; m_parsedStyleSheetCache->removedFromMemoryCache(); - m_parsedStyleSheetCache.clear(); + m_parsedStyleSheetCache = nullptr; setDecodedSize(0); - - if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable()) - makePurgeable(true); } -PassRefPtr<StyleSheetContents> CachedCSSStyleSheet::restoreParsedStyleSheet(const CSSParserContext& context) +RefPtr<StyleSheetContents> CachedCSSStyleSheet::restoreParsedStyleSheet(const CSSParserContext& context, CachePolicy cachePolicy) { if (!m_parsedStyleSheetCache) - return 0; - if (m_parsedStyleSheetCache->hasFailedOrCanceledSubresources()) { + return nullptr; + if (!m_parsedStyleSheetCache->subresourcesAllowReuse(cachePolicy)) { m_parsedStyleSheetCache->removedFromMemoryCache(); - m_parsedStyleSheetCache.clear(); - return 0; + m_parsedStyleSheetCache = nullptr; + return nullptr; } ASSERT(m_parsedStyleSheetCache->isCacheable()); @@ -171,20 +168,20 @@ PassRefPtr<StyleSheetContents> CachedCSSStyleSheet::restoreParsedStyleSheet(cons // Contexts must be identical so we know we would get the same exact result if we parsed again. if (m_parsedStyleSheetCache->parserContext() != context) - return 0; + return nullptr; didAccessDecodedData(monotonicallyIncreasingTime()); return m_parsedStyleSheetCache; } -void CachedCSSStyleSheet::saveParsedStyleSheet(PassRef<StyleSheetContents> sheet) +void CachedCSSStyleSheet::saveParsedStyleSheet(Ref<StyleSheetContents>&& sheet) { - ASSERT(sheet.get().isCacheable()); + ASSERT(sheet->isCacheable()); if (m_parsedStyleSheetCache) m_parsedStyleSheetCache->removedFromMemoryCache(); - m_parsedStyleSheetCache = std::move(sheet); + m_parsedStyleSheetCache = WTFMove(sheet); m_parsedStyleSheetCache->addedToMemoryCache(); setDecodedSize(m_parsedStyleSheetCache->estimatedSizeInBytes()); |