#include "config.h"
#include "CachedScript.h"
#include "CachedResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "HTTPHeaderNames.h"
#include "HTTPParsers.h"
#include "MIMETypeRegistry.h"
#include "MemoryCache.h"
#include "RuntimeApplicationChecks.h"
#include "SharedBuffer.h"
#include "TextResourceDecoder.h"
#include <wtf/Vector.h>
namespace WebCore {
CachedScript::CachedScript(const ResourceRequest& resourceRequest, const String& charset, SessionID sessionID)
: CachedResource(resourceRequest, Script, sessionID)
, m_decoder(TextResourceDecoder::create(ASCIILiteral("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(HTTPHeaderName::ContentType)).lower();
}
const String& CachedScript::script()
{
if (!m_script && m_data) {
m_script = m_decoder->decodeAndFlush(m_data->data(), encodedSize());
setDecodedSize(m_script.sizeInBytes());
}
m_decodedDataDeletionTimer.restart();
return m_script;
}
void CachedScript::finishLoading(SharedBuffer* data)
{
m_data = data;
setEncodedSize(data ? data->size() : 0);
CachedResource::finishLoading(data);
}
void CachedScript::destroyDecodedData()
{
m_script = String();
setDecodedSize(0);
}
#if ENABLE(NOSNIFF)
bool CachedScript::mimeTypeAllowedByNosniff() const
{
return parseContentTypeOptionsHeader(m_response.httpHeaderField(HTTPHeaderName::XContentTypeOptions)) != ContentTypeOptionsNosniff || MIMETypeRegistry::isSupportedJavaScriptMIMEType(mimeType());
}
#endif
bool CachedScript::shouldIgnoreHTTPStatusCodeErrors() const
{
if (applicationIsSolidStateNetworksDownloader())
return true;
return CachedResource::shouldIgnoreHTTPStatusCodeErrors();
}
}