CachedCSSStyleSheet.cpp [plain text]
#include "config.h"
#include "CachedCSSStyleSheet.h"
#include "CachedResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "HTTPParsers.h"
#include "TextResourceDecoder.h"
#include "SharedBuffer.h"
#include "loader.h"
#include <wtf/Vector.h>
namespace WebCore {
CachedCSSStyleSheet::CachedCSSStyleSheet(const String& url, const String& charset)
: CachedResource(url, CSSStyleSheet)
, m_decoder(TextResourceDecoder::create("text/css", charset))
{
setAccept("text/css,*/*;q=0.1");
}
CachedCSSStyleSheet::~CachedCSSStyleSheet()
{
}
void CachedCSSStyleSheet::didAddClient(CachedResourceClient *c)
{
if (!isLoading())
c->setCSSStyleSheet(m_url, m_response.url(), m_decoder->encoding().name(), this);
}
void CachedCSSStyleSheet::allClientsRemoved()
{
if (isSafeToMakePurgeable())
makePurgeable(true);
}
void CachedCSSStyleSheet::setEncoding(const String& chs)
{
m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
}
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))
return String();
if (!m_decodedSheetText.isNull())
return m_decodedSheetText;
String sheetText = m_decoder->decode(m_data->data(), m_data->size());
sheetText += m_decoder->flush();
return sheetText;
}
void CachedCSSStyleSheet::data(PassRefPtr<SharedBuffer> data, bool allDataReceived)
{
if (!allDataReceived)
return;
m_data = data;
setEncodedSize(m_data.get() ? m_data->size() : 0);
if (m_data) {
m_decodedSheetText = m_decoder->decode(m_data->data(), m_data->size());
m_decodedSheetText += m_decoder->flush();
}
setLoading(false);
checkNotify();
m_decodedSheetText = String();
}
void CachedCSSStyleSheet::checkNotify()
{
if (isLoading())
return;
CachedResourceClientWalker w(m_clients);
while (CachedResourceClient *c = w.next())
c->setCSSStyleSheet(m_url, m_response.url(), m_decoder->encoding().name(), this);
}
void CachedCSSStyleSheet::error(CachedResource::Status status)
{
setStatus(status);
ASSERT(errorOccurred());
setLoading(false);
checkNotify();
}
bool CachedCSSStyleSheet::canUseSheet(bool enforceMIMEType, bool* hasValidMIMEType) const
{
if (errorOccurred())
return false;
if (!enforceMIMEType && !hasValidMIMEType)
return true;
String mimeType = extractMIMETypeFromMediaType(response().httpHeaderField("Content-Type"));
bool typeOK = mimeType.isEmpty() || equalIgnoringCase(mimeType, "text/css") || equalIgnoringCase(mimeType, "application/x-unknown-content-type");
if (hasValidMIMEType)
*hasValidMIMEType = typeOK;
if (!enforceMIMEType)
return true;
return typeOK;
}
}