This source file includes following definitions.
- startLoading
- responseReceived
- dataReceived
- notifyFinished
- startWritingAndParsing
- finishWriting
- finishParsing
- finishLoading
- setState
- didFinishParsing
- didRemoveAllPendingStylesheet
- hasPendingResources
- importedDocument
- didFinishLoading
- addImport
- removeImport
#include "config.h"
#include "core/html/imports/HTMLImportLoader.h"
#include "core/dom/Document.h"
#include "core/dom/StyleEngine.h"
#include "core/html/HTMLDocument.h"
#include "core/html/imports/HTMLImport.h"
#include "core/html/imports/HTMLImportChild.h"
#include "core/loader/DocumentWriter.h"
#include "platform/network/ContentSecurityPolicyResponseHeaders.h"
namespace WebCore {
HTMLImportLoader::HTMLImportLoader()
: m_state(StateLoading)
{
}
HTMLImportLoader::~HTMLImportLoader()
{
if (m_importedDocument)
m_importedDocument->setImport(0);
}
void HTMLImportLoader::startLoading(const ResourcePtr<RawResource>& resource)
{
setResource(resource);
}
void HTMLImportLoader::responseReceived(Resource* resource, const ResourceResponse& response)
{
if (resource->loadFailedOrCanceled() || response.httpStatusCode() >= 400) {
setState(StateError);
return;
}
setState(startWritingAndParsing(response));
}
void HTMLImportLoader::dataReceived(Resource*, const char* data, int length)
{
RefPtr<DocumentWriter> protectingWriter(m_writer);
m_writer->addData(data, length);
}
void HTMLImportLoader::notifyFinished(Resource* resource)
{
if (resource->loadFailedOrCanceled() && !m_writer) {
setState(StateError);
return;
}
setState(finishWriting());
}
HTMLImportLoader::State HTMLImportLoader::startWritingAndParsing(const ResourceResponse& response)
{
ASSERT(!m_imports.isEmpty());
HTMLImport* firstImport = m_imports[0];
DocumentInit init = DocumentInit(response.url(), 0, firstImport->master()->contextDocument(), firstImport)
.withRegistrationContext(firstImport->master()->registrationContext());
m_importedDocument = HTMLDocument::create(init);
m_writer = DocumentWriter::create(m_importedDocument.get(), response.mimeType(), response.textEncodingName());
return StateLoading;
}
HTMLImportLoader::State HTMLImportLoader::finishWriting()
{
return StateWritten;
}
HTMLImportLoader::State HTMLImportLoader::finishParsing()
{
return StateParsed;
}
HTMLImportLoader::State HTMLImportLoader::finishLoading()
{
return StateLoaded;
}
void HTMLImportLoader::setState(State state)
{
if (m_state == state)
return;
m_state = state;
if (m_state == StateParsed || m_state == StateError || m_state == StateWritten) {
if (RefPtr<DocumentWriter> writer = m_writer.release())
writer->end();
}
if (state == StateLoaded || state == StateError)
didFinishLoading();
}
void HTMLImportLoader::didFinishParsing()
{
setState(finishParsing());
if (!hasPendingResources())
setState(finishLoading());
}
void HTMLImportLoader::didRemoveAllPendingStylesheet()
{
if (m_state == StateParsed)
setState(finishLoading());
}
bool HTMLImportLoader::hasPendingResources() const
{
return m_importedDocument && m_importedDocument->styleEngine()->hasPendingSheets();
}
Document* HTMLImportLoader::importedDocument() const
{
if (m_state == StateError)
return 0;
return m_importedDocument.get();
}
void HTMLImportLoader::didFinishLoading()
{
for (size_t i = 0; i < m_imports.size(); ++i)
m_imports[i]->didFinishLoading();
clearResource();
ASSERT(!m_importedDocument || !m_importedDocument->parsing());
}
void HTMLImportLoader::addImport(HTMLImportChild* client)
{
ASSERT(kNotFound == m_imports.find(client));
m_imports.append(client);
if (isDone())
client->didFinishLoading();
}
void HTMLImportLoader::removeImport(HTMLImportChild* client)
{
ASSERT(kNotFound != m_imports.find(client));
m_imports.remove(m_imports.find(client));
}
}