DocumentLoader.cpp [plain text]
#include "config.h"
#include "DocumentLoader.h"
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
#include "ApplicationCache.h"
#include "ApplicationCacheGroup.h"
#include "ApplicationCacheResource.h"
#endif
#include "ArchiveFactory.h"
#include "ArchiveResourceCollection.h"
#include "CachedPage.h"
#include "DocLoader.h"
#include "Document.h"
#include "Event.h"
#include "Frame.h"
#include "FrameLoader.h"
#include "FrameTree.h"
#include "HistoryItem.h"
#include "Logging.h"
#include "MainResourceLoader.h"
#include "Page.h"
#include "PlatformString.h"
#include "Settings.h"
#include "SharedBuffer.h"
#include "StringBuffer.h"
#include "XMLTokenizer.h"
#include <wtf/Assertions.h>
#include <wtf/unicode/Unicode.h>
namespace WebCore {
static inline String canonicalizedTitle(const String& title, Frame* frame)
{
ASSERT(!title.isEmpty());
const UChar* characters = title.characters();
unsigned length = title.length();
unsigned i;
StringBuffer buffer(length);
unsigned builderIndex = 0;
for (i = 0; i < length; ++i) {
UChar c = characters[i];
if (!(c <= 0x20 || c == 0x7F))
break;
}
if (i == length)
return "";
bool previousCharWasWS = false;
for (; i < length; ++i) {
UChar c = characters[i];
if (c <= 0x20 || c == 0x7F || (WTF::Unicode::category(c) & (WTF::Unicode::Separator_Line | WTF::Unicode::Separator_Paragraph))) {
if (previousCharWasWS)
continue;
buffer[builderIndex++] = ' ';
previousCharWasWS = true;
} else {
buffer[builderIndex++] = c;
previousCharWasWS = false;
}
}
while (builderIndex > 0) {
--builderIndex;
if (buffer[builderIndex] != ' ')
break;
}
if (!builderIndex && buffer[builderIndex] == ' ')
return "";
buffer.shrink(builderIndex + 1);
frame->document()->displayBufferModifiedByEncoding(buffer.characters(), buffer.length());
return String::adopt(buffer);
}
static void cancelAll(const ResourceLoaderSet& loaders)
{
const ResourceLoaderSet copy = loaders;
ResourceLoaderSet::const_iterator end = copy.end();
for (ResourceLoaderSet::const_iterator it = copy.begin(); it != end; ++it)
(*it)->cancel();
}
static void setAllDefersLoading(const ResourceLoaderSet& loaders, bool defers)
{
const ResourceLoaderSet copy = loaders;
ResourceLoaderSet::const_iterator end = copy.end();
for (ResourceLoaderSet::const_iterator it = copy.begin(); it != end; ++it)
(*it)->setDefersLoading(defers);
}
DocumentLoader::DocumentLoader(const ResourceRequest& req, const SubstituteData& substituteData)
: m_deferMainResourceDataLoad(true)
, m_frame(0)
, m_originalRequest(req)
, m_substituteData(substituteData)
, m_originalRequestCopy(req)
, m_request(req)
, m_committed(false)
, m_isStopping(false)
, m_loading(false)
, m_gotFirstByte(false)
, m_primaryLoadComplete(false)
, m_isClientRedirect(false)
, m_stopRecordingResponses(false)
, m_substituteResourceDeliveryTimer(this, &DocumentLoader::substituteResourceDeliveryTimerFired)
, m_didCreateGlobalHistoryEntry(false)
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
, m_candidateApplicationCacheGroup(0)
#endif
{
}
FrameLoader* DocumentLoader::frameLoader() const
{
if (!m_frame)
return 0;
return m_frame->loader();
}
DocumentLoader::~DocumentLoader()
{
ASSERT(!m_frame || frameLoader()->activeDocumentLoader() != this || !frameLoader()->isLoading());
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
if (m_applicationCache)
m_applicationCache->group()->disassociateDocumentLoader(this);
else if (m_candidateApplicationCacheGroup)
m_candidateApplicationCacheGroup->disassociateDocumentLoader(this);
#endif
}
PassRefPtr<SharedBuffer> DocumentLoader::mainResourceData() const
{
if (m_mainResourceData)
return m_mainResourceData;
if (m_mainResourceLoader)
return m_mainResourceLoader->resourceData();
return 0;
}
const ResourceRequest& DocumentLoader::originalRequest() const
{
return m_originalRequest;
}
const ResourceRequest& DocumentLoader::originalRequestCopy() const
{
return m_originalRequestCopy;
}
const ResourceRequest& DocumentLoader::request() const
{
return m_request;
}
ResourceRequest& DocumentLoader::request()
{
return m_request;
}
const KURL& DocumentLoader::url() const
{
return request().url();
}
void DocumentLoader::replaceRequestURLForAnchorScroll(const KURL& url)
{
m_originalRequestCopy.setURL(url);
m_request.setURL(url);
}
void DocumentLoader::setRequest(const ResourceRequest& req)
{
bool handlingUnreachableURL = false;
handlingUnreachableURL = m_substituteData.isValid() && !m_substituteData.failingURL().isEmpty();
if (handlingUnreachableURL)
m_committed = false;
ASSERT(!m_committed);
KURL oldURL = m_request.url();
m_request = req;
if (!handlingUnreachableURL && oldURL != req.url())
frameLoader()->didReceiveServerRedirectForProvisionalLoadForFrame();
}
void DocumentLoader::setMainDocumentError(const ResourceError& error)
{
m_mainDocumentError = error;
frameLoader()->setMainDocumentError(this, error);
}
void DocumentLoader::clearErrors()
{
m_mainDocumentError = ResourceError();
}
void DocumentLoader::mainReceivedError(const ResourceError& error, bool isComplete)
{
ASSERT(!error.isNull());
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
ApplicationCacheGroup* group = m_candidateApplicationCacheGroup;
if (!group && m_applicationCache) {
ASSERT(!mainResourceApplicationCache()); group = m_applicationCache->group();
}
if (group)
group->failedLoadingMainResource(this);
#endif
if (!frameLoader())
return;
setMainDocumentError(error);
if (isComplete)
frameLoader()->mainReceivedCompleteError(this, error);
}
void DocumentLoader::stopLoading(DatabasePolicy databasePolicy)
{
bool loading = m_loading;
if (m_committed) {
Document* doc = m_frame->document();
if (loading || doc->parsing())
m_frame->loader()->stopLoading(UnloadEventPolicyNone, databasePolicy);
}
cancelAll(m_multipartSubresourceLoaders);
if (!loading)
return;
RefPtr<Frame> protectFrame(m_frame);
RefPtr<DocumentLoader> protectLoader(this);
m_isStopping = true;
FrameLoader* frameLoader = DocumentLoader::frameLoader();
if (m_mainResourceLoader)
m_mainResourceLoader->cancel();
else if (!m_subresourceLoaders.isEmpty())
setMainDocumentError(frameLoader->cancelledError(m_request));
else
mainReceivedError(frameLoader->cancelledError(m_request), true);
stopLoadingSubresources();
stopLoadingPlugIns();
m_isStopping = false;
}
void DocumentLoader::setupForReplace()
{
frameLoader()->setupForReplace();
m_committed = false;
}
void DocumentLoader::commitIfReady()
{
if (m_gotFirstByte && !m_committed) {
m_committed = true;
frameLoader()->commitProvisionalLoad(0);
}
}
void DocumentLoader::finishedLoading()
{
m_gotFirstByte = true;
commitIfReady();
if (FrameLoader* loader = frameLoader()) {
loader->finishedLoadingDocument(this);
loader->end();
}
}
void DocumentLoader::commitLoad(const char* data, int length)
{
RefPtr<DocumentLoader> protect(this);
commitIfReady();
if (FrameLoader* frameLoader = DocumentLoader::frameLoader())
frameLoader->committedLoad(this, data, length);
}
bool DocumentLoader::doesProgressiveLoad(const String& MIMEType) const
{
return !frameLoader()->isReplacing() || MIMEType == "text/html";
}
void DocumentLoader::receivedData(const char* data, int length)
{
m_gotFirstByte = true;
if (doesProgressiveLoad(m_response.mimeType()))
commitLoad(data, length);
}
void DocumentLoader::setupForReplaceByMIMEType(const String& newMIMEType)
{
if (!m_gotFirstByte)
return;
String oldMIMEType = m_response.mimeType();
if (!doesProgressiveLoad(oldMIMEType)) {
frameLoader()->revertToProvisional(this);
setupForReplace();
RefPtr<SharedBuffer> resourceData = mainResourceData();
commitLoad(resourceData->data(), resourceData->size());
}
frameLoader()->finishedLoadingDocument(this);
m_frame->loader()->end();
frameLoader()->setReplacing();
m_gotFirstByte = false;
if (doesProgressiveLoad(newMIMEType)) {
frameLoader()->revertToProvisional(this);
setupForReplace();
}
stopLoadingSubresources();
stopLoadingPlugIns();
clearArchiveResources();
}
void DocumentLoader::updateLoading()
{
if (!m_frame) {
setLoading(false);
return;
}
ASSERT(this == frameLoader()->activeDocumentLoader());
setLoading(frameLoader()->isLoading());
}
void DocumentLoader::setFrame(Frame* frame)
{
if (m_frame == frame)
return;
ASSERT(frame && !m_frame);
m_frame = frame;
attachToFrame();
}
void DocumentLoader::attachToFrame()
{
ASSERT(m_frame);
}
void DocumentLoader::detachFromFrame()
{
ASSERT(m_frame);
m_frame = 0;
}
void DocumentLoader::prepareForLoadStart()
{
ASSERT(!m_isStopping);
setPrimaryLoadComplete(false);
ASSERT(frameLoader());
clearErrors();
setLoading(true);
frameLoader()->prepareForLoadStart();
}
void DocumentLoader::setPrimaryLoadComplete(bool flag)
{
m_primaryLoadComplete = flag;
if (flag) {
if (m_mainResourceLoader) {
m_mainResourceData = m_mainResourceLoader->resourceData();
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
m_mainResourceApplicationCache = m_mainResourceLoader->applicationCache();
#endif
m_mainResourceLoader = 0;
}
if (this == frameLoader()->activeDocumentLoader())
updateLoading();
}
}
bool DocumentLoader::isLoadingInAPISense() const
{
if (frameLoader()->state() != FrameStateComplete) {
if (!m_primaryLoadComplete && isLoading())
return true;
if (!m_subresourceLoaders.isEmpty())
return true;
Document* doc = m_frame->document();
if (doc->docLoader()->requestCount())
return true;
if (Tokenizer* tok = doc->tokenizer())
if (tok->processingData())
return true;
}
return frameLoader()->subframeIsLoading();
}
void DocumentLoader::addAllArchiveResources(Archive* archive)
{
if (!m_archiveResourceCollection)
m_archiveResourceCollection.set(new ArchiveResourceCollection);
ASSERT(archive);
if (!archive)
return;
m_archiveResourceCollection->addAllResources(archive);
}
void DocumentLoader::addArchiveResource(PassRefPtr<ArchiveResource> resource)
{
if (!m_archiveResourceCollection)
m_archiveResourceCollection.set(new ArchiveResourceCollection);
ASSERT(resource);
if (!resource)
return;
m_archiveResourceCollection->addResource(resource);
}
ArchiveResource* DocumentLoader::archiveResourceForURL(const KURL& url) const
{
if (!m_archiveResourceCollection)
return 0;
ArchiveResource* resource = m_archiveResourceCollection->archiveResourceForURL(url);
return resource && !resource->shouldIgnoreWhenUnarchiving() ? resource : 0;
}
PassRefPtr<Archive> DocumentLoader::popArchiveForSubframe(const String& frameName)
{
return m_archiveResourceCollection ? m_archiveResourceCollection->popSubframeArchive(frameName) : 0;
}
void DocumentLoader::clearArchiveResources()
{
m_archiveResourceCollection.clear();
m_substituteResourceDeliveryTimer.stop();
}
void DocumentLoader::setParsedArchiveData(PassRefPtr<SharedBuffer> data)
{
m_parsedArchiveData = data;
}
SharedBuffer* DocumentLoader::parsedArchiveData() const
{
return m_parsedArchiveData.get();
}
PassRefPtr<ArchiveResource> DocumentLoader::mainResource() const
{
const ResourceResponse& r = response();
RefPtr<SharedBuffer> mainResourceBuffer = mainResourceData();
if (!mainResourceBuffer)
mainResourceBuffer = SharedBuffer::create();
return ArchiveResource::create(mainResourceBuffer, r.url(), r.mimeType(), r.textEncodingName(), frame()->tree()->name());
}
PassRefPtr<ArchiveResource> DocumentLoader::subresource(const KURL& url) const
{
if (!isCommitted())
return 0;
CachedResource* resource = m_frame->document()->docLoader()->cachedResource(url);
if (!resource || !resource->isLoaded())
return archiveResourceForURL(url);
if (!resource->makePurgeable(false))
return 0;
RefPtr<SharedBuffer> data = resource->data();
if (!data)
return 0;
return ArchiveResource::create(data.release(), url, resource->response());
}
void DocumentLoader::getSubresources(Vector<PassRefPtr<ArchiveResource> >& subresources) const
{
if (!isCommitted())
return;
Document* document = m_frame->document();
const DocLoader::DocumentResourceMap& allResources = document->docLoader()->allCachedResources();
DocLoader::DocumentResourceMap::const_iterator end = allResources.end();
for (DocLoader::DocumentResourceMap::const_iterator it = allResources.begin(); it != end; ++it) {
RefPtr<ArchiveResource> subresource = this->subresource(KURL(it->second->url()));
if (subresource)
subresources.append(subresource.release());
}
return;
}
void DocumentLoader::deliverSubstituteResourcesAfterDelay()
{
if (m_pendingSubstituteResources.isEmpty())
return;
ASSERT(m_frame && m_frame->page());
if (m_frame->page()->defersLoading())
return;
if (!m_substituteResourceDeliveryTimer.isActive())
m_substituteResourceDeliveryTimer.startOneShot(0);
}
void DocumentLoader::substituteResourceDeliveryTimerFired(Timer<DocumentLoader>*)
{
if (m_pendingSubstituteResources.isEmpty())
return;
ASSERT(m_frame && m_frame->page());
if (m_frame->page()->defersLoading())
return;
SubstituteResourceMap copy;
copy.swap(m_pendingSubstituteResources);
SubstituteResourceMap::const_iterator end = copy.end();
for (SubstituteResourceMap::const_iterator it = copy.begin(); it != end; ++it) {
RefPtr<ResourceLoader> loader = it->first;
SubstituteResource* resource = it->second.get();
if (resource) {
SharedBuffer* data = resource->data();
loader->didReceiveResponse(resource->response());
loader->didReceiveData(data->data(), data->size(), data->size(), true);
loader->didFinishLoading();
} else {
loader->didFail(loader->cannotShowURLError());
}
}
}
#ifndef NDEBUG
bool DocumentLoader::isSubstituteLoadPending(ResourceLoader* loader) const
{
return m_pendingSubstituteResources.contains(loader);
}
#endif
void DocumentLoader::cancelPendingSubstituteLoad(ResourceLoader* loader)
{
if (m_pendingSubstituteResources.isEmpty())
return;
m_pendingSubstituteResources.remove(loader);
if (m_pendingSubstituteResources.isEmpty())
m_substituteResourceDeliveryTimer.stop();
}
bool DocumentLoader::scheduleArchiveLoad(ResourceLoader* loader, const ResourceRequest& request, const KURL& originalURL)
{
ArchiveResource* resource = 0;
if (request.url() == originalURL)
resource = archiveResourceForURL(originalURL);
if (!resource) {
bool shouldFailLoad = m_frame->settings()->webArchiveDebugModeEnabled() && ArchiveFactory::isArchiveMimeType(responseMIMEType());
if (!shouldFailLoad)
return false;
}
m_pendingSubstituteResources.set(loader, resource);
deliverSubstituteResourcesAfterDelay();
return true;
}
void DocumentLoader::addResponse(const ResourceResponse& r)
{
if (!m_stopRecordingResponses)
m_responses.append(r);
}
void DocumentLoader::stopRecordingResponses()
{
m_stopRecordingResponses = true;
}
void DocumentLoader::setTitle(const String& title)
{
if (title.isEmpty())
return;
String trimmed = canonicalizedTitle(title, m_frame);
if (!trimmed.isEmpty() && m_pageTitle != trimmed) {
frameLoader()->willChangeTitle(this);
m_pageTitle = trimmed;
frameLoader()->didChangeTitle(this);
}
}
KURL DocumentLoader::urlForHistory() const
{
if (m_substituteData.isValid())
return unreachableURL();
return m_originalRequestCopy.url();
}
bool DocumentLoader::urlForHistoryReflectsFailure() const
{
return m_substituteData.isValid() || m_response.httpStatusCode() >= 400;
}
const KURL& DocumentLoader::originalURL() const
{
return m_originalRequestCopy.url();
}
const KURL& DocumentLoader::requestURL() const
{
return request().url();
}
const KURL& DocumentLoader::responseURL() const
{
return m_response.url();
}
const String& DocumentLoader::responseMIMEType() const
{
return m_response.mimeType();
}
const KURL& DocumentLoader::unreachableURL() const
{
return m_substituteData.failingURL();
}
void DocumentLoader::setDefersLoading(bool defers)
{
if (m_mainResourceLoader)
m_mainResourceLoader->setDefersLoading(defers);
setAllDefersLoading(m_subresourceLoaders, defers);
setAllDefersLoading(m_plugInStreamLoaders, defers);
if (!defers)
deliverSubstituteResourcesAfterDelay();
}
void DocumentLoader::stopLoadingPlugIns()
{
cancelAll(m_plugInStreamLoaders);
}
void DocumentLoader::stopLoadingSubresources()
{
cancelAll(m_subresourceLoaders);
}
void DocumentLoader::addSubresourceLoader(ResourceLoader* loader)
{
m_subresourceLoaders.add(loader);
setLoading(true);
}
void DocumentLoader::removeSubresourceLoader(ResourceLoader* loader)
{
m_subresourceLoaders.remove(loader);
updateLoading();
if (Frame* frame = m_frame)
frame->loader()->checkLoadComplete();
}
void DocumentLoader::addPlugInStreamLoader(ResourceLoader* loader)
{
m_plugInStreamLoaders.add(loader);
setLoading(true);
}
void DocumentLoader::removePlugInStreamLoader(ResourceLoader* loader)
{
m_plugInStreamLoaders.remove(loader);
updateLoading();
}
bool DocumentLoader::isLoadingMainResource() const
{
return !!m_mainResourceLoader;
}
bool DocumentLoader::isLoadingSubresources() const
{
return !m_subresourceLoaders.isEmpty();
}
bool DocumentLoader::isLoadingPlugIns() const
{
return !m_plugInStreamLoaders.isEmpty();
}
bool DocumentLoader::isLoadingMultipartContent() const
{
return m_mainResourceLoader && m_mainResourceLoader->isLoadingMultipartContent();
}
bool DocumentLoader::startLoadingMainResource(unsigned long identifier)
{
ASSERT(!m_mainResourceLoader);
m_mainResourceLoader = MainResourceLoader::create(m_frame);
m_mainResourceLoader->setIdentifier(identifier);
frameLoader()->addExtraFieldsToMainResourceRequest(m_request);
if (!m_mainResourceLoader->load(m_request, m_substituteData)) {
LOG_ERROR("could not create WebResourceHandle for URL %s -- should be caught by policy handler level", m_request.url().string().ascii().data());
m_mainResourceLoader = 0;
return false;
}
return true;
}
void DocumentLoader::cancelMainResourceLoad(const ResourceError& error)
{
m_mainResourceLoader->cancel(error);
}
void DocumentLoader::subresourceLoaderFinishedLoadingOnePart(ResourceLoader* loader)
{
m_multipartSubresourceLoaders.add(loader);
m_subresourceLoaders.remove(loader);
updateLoading();
if (Frame* frame = m_frame)
frame->loader()->checkLoadComplete();
}
void DocumentLoader::iconLoadDecisionAvailable()
{
if (m_frame)
m_frame->loader()->iconLoadDecisionAvailable();
}
#if ENABLE(OFFLINE_WEB_APPLICATIONS)
void DocumentLoader::setCandidateApplicationCacheGroup(ApplicationCacheGroup* group)
{
ASSERT(!m_applicationCache);
m_candidateApplicationCacheGroup = group;
}
void DocumentLoader::setApplicationCache(PassRefPtr<ApplicationCache> applicationCache)
{
if (m_candidateApplicationCacheGroup) {
ASSERT(!m_applicationCache);
m_candidateApplicationCacheGroup = 0;
}
m_applicationCache = applicationCache;
}
ApplicationCache* DocumentLoader::mainResourceApplicationCache() const
{
if (m_mainResourceApplicationCache)
return m_mainResourceApplicationCache.get();
if (m_mainResourceLoader)
return m_mainResourceLoader->applicationCache();
return 0;
}
bool DocumentLoader::shouldLoadResourceFromApplicationCache(const ResourceRequest& request, ApplicationCacheResource*& resource)
{
ApplicationCache* cache = applicationCache();
if (!cache || !cache->isComplete())
return false;
if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request))
return false;
resource = cache->resourceForURL(request.url());
if (!resource && (cache->urlMatchesFallbackNamespace(request.url()) || cache->isURLInOnlineWhitelist(request.url())))
return false;
return true;
}
bool DocumentLoader::getApplicationCacheFallbackResource(const ResourceRequest& request, ApplicationCacheResource*& resource, ApplicationCache* cache)
{
if (!cache) {
cache = applicationCache();
if (!cache)
return false;
}
if (!cache->isComplete())
return false;
if (!ApplicationCache::requestIsHTTPOrHTTPSGet(request))
return false;
KURL fallbackURL;
if (!cache->urlMatchesFallbackNamespace(request.url(), &fallbackURL))
return false;
resource = cache->resourceForURL(fallbackURL);
ASSERT(resource);
return true;
}
bool DocumentLoader::scheduleApplicationCacheLoad(ResourceLoader* loader, const ResourceRequest& request, const KURL& originalURL)
{
if (!frameLoader()->frame()->settings() || !frameLoader()->frame()->settings()->offlineWebApplicationCacheEnabled())
return false;
if (request.url() != originalURL)
return false;
ApplicationCacheResource* resource;
if (!shouldLoadResourceFromApplicationCache(request, resource))
return false;
m_pendingSubstituteResources.set(loader, resource);
deliverSubstituteResourcesAfterDelay();
return true;
}
bool DocumentLoader::scheduleLoadFallbackResourceFromApplicationCache(ResourceLoader* loader, const ResourceRequest& request, ApplicationCache* cache)
{
if (!frameLoader()->frame()->settings() || !frameLoader()->frame()->settings()->offlineWebApplicationCacheEnabled())
return false;
ApplicationCacheResource* resource;
if (!getApplicationCacheFallbackResource(request, resource, cache))
return false;
m_pendingSubstituteResources.set(loader, resource);
deliverSubstituteResourcesAfterDelay();
return true;
}
#endif // ENABLE(OFFLINE_WEB_APPLICATIONS)
}