#include "config.h"
#include "CachedScript.h"
#include "CachedResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "HTTPParsers.h"
#include "MIMETypeRegistry.h"
#include "MemoryCache.h"
#include "ResourceBuffer.h"
#include "RuntimeApplicationChecks.h"
#include "TextResourceDecoder.h"
#include <wtf/Vector.h>
namespace WebCore {
CachedScript::CachedScript(const ResourceRequest& resourceRequest, const String& charset)
: CachedResource(resourceRequest, Script)
, m_decoder(TextResourceDecoder::create("application/javascript", charset))
{
setAccept("*/*");
}
CachedScript::~CachedScript()
{
}
void CachedScript::setEncoding(const String& chs)
{
m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
}
String CachedScript::encoding() const
{
return m_decoder->encoding().name();
}
String CachedScript::mimeType() const
{
return extractMIMETypeFromMediaType(m_response.httpHeaderField("Content-Type")).lower();
}
const String& CachedScript::script()
{
ASSERT(!isPurgeable());
if (!m_script && m_data) {
m_script = m_decoder->decode(m_data->data(), encodedSize());
m_script.append(m_decoder->flush());
setDecodedSize(m_script.sizeInBytes());
}
m_decodedDataDeletionTimer.restart();
return m_script;
}
void CachedScript::finishLoading(ResourceBuffer* data)
{
m_data = data;
setEncodedSize(m_data.get() ? m_data->size() : 0);
CachedResource::finishLoading(data);
}
void CachedScript::destroyDecodedData()
{
m_script = String();
setDecodedSize(0);
if (!MemoryCache::shouldMakeResourcePurgeableOnEviction() && isSafeToMakePurgeable())
makePurgeable(true);
}
#if ENABLE(NOSNIFF)
bool CachedScript::mimeTypeAllowedByNosniff() const
{
return !parseContentTypeOptionsHeader(m_response.httpHeaderField("X-Content-Type-Options")) == ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType());
}
#endif
bool CachedScript::shouldIgnoreHTTPStatusCodeErrors() const
{
if (applicationIsSolidStateNetworksDownloader())
return true;
return CachedResource::shouldIgnoreHTTPStatusCodeErrors();
}
}