#include "config.h"
#include "CachedScript.h"
#include "CachedResourceClient.h"
#include "CachedResourceClientWalker.h"
#include "CachedResourceRequest.h"
#include "RuntimeApplicationChecks.h"
#include "SharedBuffer.h"
#include "TextResourceDecoder.h"
namespace WebCore {
CachedScript::CachedScript(CachedResourceRequest&& request, PAL::SessionID sessionID)
: CachedResource(WTFMove(request), Type::Script, sessionID)
, m_decoder(TextResourceDecoder::create("application/javascript"_s, request.charset()))
{
}
CachedScript::~CachedScript() = default;
void CachedScript::setEncoding(const String& chs)
{
m_decoder->setEncoding(chs, TextResourceDecoder::EncodingFromHTTPHeader);
}
String CachedScript::encoding() const
{
return m_decoder->encoding().name();
}
StringView CachedScript::script()
{
if (!m_data)
return { };
if (m_decodingState == NeverDecoded
&& TextEncoding(encoding()).isByteBasedEncoding()
&& m_data->size()
&& charactersAreAllASCII(reinterpret_cast<const LChar*>(m_data->data()), m_data->size())) {
m_decodingState = DataAndDecodedStringHaveSameBytes;
setDecodedSize(0);
m_decodedDataDeletionTimer.stop();
m_scriptHash = StringHasher::computeHashAndMaskTop8Bits(reinterpret_cast<const LChar*>(m_data->data()), m_data->size());
}
if (m_decodingState == DataAndDecodedStringHaveSameBytes)
return { reinterpret_cast<const LChar*>(m_data->data()), static_cast<unsigned>(m_data->size()) };
if (!m_script) {
m_script = m_decoder->decodeAndFlush(m_data->data(), encodedSize());
ASSERT(!m_scriptHash || m_scriptHash == m_script.impl()->hash());
if (m_decodingState == NeverDecoded)
m_scriptHash = m_script.impl()->hash();
m_decodingState = DataAndDecodedStringHaveDifferentBytes;
setDecodedSize(m_script.sizeInBytes());
}
m_decodedDataDeletionTimer.restart();
return m_script;
}
unsigned CachedScript::scriptHash()
{
if (m_decodingState == NeverDecoded)
script();
return m_scriptHash;
}
void CachedScript::finishLoading(SharedBuffer* data)
{
m_data = data;
setEncodedSize(data ? data->size() : 0);
CachedResource::finishLoading(data);
}
void CachedScript::destroyDecodedData()
{
m_script = String();
setDecodedSize(0);
}
void CachedScript::setBodyDataFrom(const CachedResource& resource)
{
ASSERT(resource.type() == type());
auto& script = static_cast<const CachedScript&>(resource);
CachedResource::setBodyDataFrom(resource);
m_script = script.m_script;
m_scriptHash = script.m_scriptHash;
m_decodingState = script.m_decodingState;
m_decoder = script.m_decoder;
}
bool CachedScript::shouldIgnoreHTTPStatusCodeErrors() const
{
#if PLATFORM(MAC)
if (MacApplication::isSolidStateNetworksDownloader())
return true;
#endif
return CachedResource::shouldIgnoreHTTPStatusCodeErrors();
}
}