DocumentLoader.cpp [plain text]
#include "config.h"
#include "DocumentLoader.h"
#include "ApplicationCacheHost.h"
#include "ArchiveResourceCollection.h"
#include "CachedPage.h"
#include "CachedRawResource.h"
#include "CachedResourceLoader.h"
#include "DOMWindow.h"
#include "Document.h"
#include "DocumentParser.h"
#include "DocumentWriter.h"
#include "Event.h"
#include "FormState.h"
#include "FrameLoader.h"
#include "FrameLoaderClient.h"
#include "FrameTree.h"
#include "HTMLFormElement.h"
#include "HTMLFrameOwnerElement.h"
#include "HTTPHeaderNames.h"
#include "HistoryItem.h"
#include "IconController.h"
#include "InspectorInstrumentation.h"
#include "Logging.h"
#include "MainFrame.h"
#include "MemoryCache.h"
#include "Page.h"
#include "PolicyChecker.h"
#include "ProgressTracker.h"
#include "ResourceHandle.h"
#include "SchemeRegistry.h"
#include "ScriptController.h"
#include "SecurityPolicy.h"
#include "Settings.h"
#include "SubresourceLoader.h"
#include "TextResourceDecoder.h"
#include <wtf/Assertions.h>
#include <wtf/NeverDestroyed.h>
#include <wtf/Ref.h>
#include <wtf/text/CString.h>
#include <wtf/text/WTFString.h>
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
#include "ArchiveFactory.h"
#endif
#if ENABLE(CONTENT_FILTERING)
#include "ContentFilter.h"
#endif
namespace WebCore {
static void cancelAll(const ResourceLoaderMap& loaders)
{
Vector<RefPtr<ResourceLoader>> loadersCopy;
copyValuesToVector(loaders, loadersCopy);
for (auto& loader : loadersCopy)
loader->cancel();
}
static void setAllDefersLoading(const ResourceLoaderMap& loaders, bool defers)
{
Vector<RefPtr<ResourceLoader>> loadersCopy;
copyValuesToVector(loaders, loadersCopy);
for (auto& loader : loadersCopy)
loader->setDefersLoading(defers);
}
static bool areAllLoadersPageCacheAcceptable(const ResourceLoaderMap& loaders)
{
Vector<RefPtr<ResourceLoader>> loadersCopy;
copyValuesToVector(loaders, loadersCopy);
for (auto& loader : loadersCopy) {
if (!loader->frameLoader() || !loader->frameLoader()->frame().page())
return false;
CachedResource* cachedResource = MemoryCache::singleton().resourceForRequest(loader->request(), loader->frameLoader()->frame().page()->sessionID());
if (!cachedResource)
return false;
if (!cachedResource->isImage() && !cachedResource->areAllClientsXMLHttpRequests())
return false;
}
return true;
}
DocumentLoader::DocumentLoader(const ResourceRequest& req, const SubstituteData& substituteData)
: m_deferMainResourceDataLoad(true)
, m_frame(0)
, m_cachedResourceLoader(CachedResourceLoader::create(this))
, m_writer(m_frame)
, m_originalRequest(req)
, m_substituteData(substituteData)
, m_originalRequestCopy(req)
, m_request(req)
, m_originalSubstituteDataWasValid(substituteData.isValid())
, m_committed(false)
, m_isStopping(false)
, m_gotFirstByte(false)
, m_isClientRedirect(false)
, m_isLoadingMultipartContent(false)
, m_wasOnloadHandled(false)
, m_stopRecordingResponses(false)
, m_substituteResourceDeliveryTimer(*this, &DocumentLoader::substituteResourceDeliveryTimerFired)
, m_didCreateGlobalHistoryEntry(false)
, m_loadingMainResource(false)
, m_timeOfLastDataReceived(0.0)
, m_identifierForLoadWithoutResourceLoader(0)
, m_dataLoadTimer(*this, &DocumentLoader::handleSubstituteDataLoadNow)
, m_subresourceLoadersArePageCacheAcceptable(false)
, m_applicationCacheHost(std::make_unique<ApplicationCacheHost>(*this))
#if ENABLE(CONTENT_FILTERING)
, m_contentFilter(!substituteData.isValid() ? ContentFilter::create(*this) : nullptr)
#endif
{
}
FrameLoader* DocumentLoader::frameLoader() const
{
if (!m_frame)
return 0;
return &m_frame->loader();
}
ResourceLoader* DocumentLoader::mainResourceLoader() const
{
return m_mainResource ? m_mainResource->loader() : 0;
}
DocumentLoader::~DocumentLoader()
{
ASSERT(!m_frame || frameLoader()->activeDocumentLoader() != this || !isLoading());
ASSERT_WITH_MESSAGE(!m_waitingForContentPolicy, "The content policy callback should never outlive its DocumentLoader.");
ASSERT_WITH_MESSAGE(!m_waitingForNavigationPolicy, "The navigation policy callback should never outlive its DocumentLoader.");
if (m_iconLoadDecisionCallback)
m_iconLoadDecisionCallback->invalidate();
if (m_iconDataCallback)
m_iconDataCallback->invalidate();
m_cachedResourceLoader->clearDocumentLoader();
clearMainResource();
}
PassRefPtr<SharedBuffer> DocumentLoader::mainResourceData() const
{
if (m_substituteData.isValid())
return m_substituteData.content()->copy();
if (m_mainResource)
return m_mainResource->resourceBuffer();
return nullptr;
}
Document* DocumentLoader::document() const
{
if (m_frame && m_frame->loader().documentLoader() == this)
return m_frame->document();
return nullptr;
}
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 URL& DocumentLoader::url() const
{
return request().url();
}
void DocumentLoader::replaceRequestURLForSameDocumentNavigation(const URL& 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);
m_request = req;
}
void DocumentLoader::setMainDocumentError(const ResourceError& error)
{
m_mainDocumentError = error;
frameLoader()->client().setMainDocumentError(this, error);
}
void DocumentLoader::mainReceivedError(const ResourceError& error)
{
ASSERT(!error.isNull());
if (m_identifierForLoadWithoutResourceLoader) {
ASSERT(!mainResourceLoader());
frameLoader()->client().dispatchDidFailLoading(this, m_identifierForLoadWithoutResourceLoader, error);
}
#if !USE(CF)
ASSERT(!mainResourceLoader() || !mainResourceLoader()->defersLoading());
#endif
m_applicationCacheHost->failedLoadingMainResource();
if (!frameLoader())
return;
setMainDocumentError(error);
clearMainResourceLoader();
frameLoader()->receivedMainResourceError(error);
}
void DocumentLoader::stopLoading()
{
RefPtr<Frame> protectFrame(m_frame);
Ref<DocumentLoader> protectLoader(*this);
bool loading = isLoading();
if (loading && m_committed && !mainResourceLoader() && !m_subresourceLoaders.isEmpty())
m_subresourceLoadersArePageCacheAcceptable = areAllLoadersPageCacheAcceptable(m_subresourceLoaders);
if (m_committed) {
Document* doc = m_frame->document();
if (loading || doc->parsing())
m_frame->loader().stopLoading(UnloadEventPolicyNone);
}
cancelAll(m_multipartSubresourceLoaders);
m_applicationCacheHost->stopLoadingInFrame(m_frame);
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
clearArchiveResources();
#endif
if (!loading) {
ASSERT(!isLoading());
return;
}
if (m_isStopping)
return;
m_isStopping = true;
FrameLoader* frameLoader = DocumentLoader::frameLoader();
if (isLoadingMainResource()) {
cancelMainResourceLoad(frameLoader->cancelledError(m_request));
} else if (!m_subresourceLoaders.isEmpty() || !m_plugInStreamLoaders.isEmpty()) {
setMainDocumentError(frameLoader->cancelledError(m_request));
} else {
mainReceivedError(frameLoader->cancelledError(m_request));
}
if (Document* document = this->document())
document->cancelParsing();
stopLoadingSubresources();
stopLoadingPlugIns();
m_isStopping = false;
}
void DocumentLoader::commitIfReady()
{
if (!m_committed) {
m_committed = true;
frameLoader()->commitProvisionalLoad();
}
}
bool DocumentLoader::isLoading() const
{
return isLoadingMainResource() || !m_subresourceLoaders.isEmpty() || !m_plugInStreamLoaders.isEmpty();
}
void DocumentLoader::notifyFinished(CachedResource* resource)
{
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter && !m_contentFilter->continueAfterNotifyFinished(resource))
return;
#endif
ASSERT_UNUSED(resource, m_mainResource == resource);
ASSERT(m_mainResource);
if (!m_mainResource->errorOccurred() && !m_mainResource->wasCanceled()) {
finishedLoading(m_mainResource->loadFinishTime());
return;
}
if (m_request.cachePolicy() == ReturnCacheDataDontLoad && !m_mainResource->wasCanceled()) {
frameLoader()->retryAfterFailedCacheOnlyMainResourceLoad();
return;
}
mainReceivedError(m_mainResource->resourceError());
}
void DocumentLoader::finishedLoading(double finishTime)
{
#if !USE(CF)
ASSERT(!m_frame->page()->defersLoading() || InspectorInstrumentation::isDebuggerPaused(m_frame));
#endif
Ref<DocumentLoader> protect(*this);
if (m_identifierForLoadWithoutResourceLoader) {
unsigned long identifier = m_identifierForLoadWithoutResourceLoader;
m_identifierForLoadWithoutResourceLoader = 0;
frameLoader()->notifier().dispatchDidFinishLoading(this, identifier, finishTime);
}
maybeFinishLoadingMultipartContent();
double responseEndTime = finishTime;
if (!responseEndTime)
responseEndTime = m_timeOfLastDataReceived;
if (!responseEndTime)
responseEndTime = monotonicallyIncreasingTime();
timing().setResponseEnd(responseEndTime);
commitIfReady();
if (!frameLoader())
return;
if (!maybeCreateArchive()) {
if (!m_gotFirstByte)
commitData(0, 0);
frameLoader()->client().finishedLoading(this);
}
m_writer.end();
if (!m_mainDocumentError.isNull())
return;
clearMainResourceLoader();
if (!frameLoader()->stateMachine().creatingInitialEmptyDocument())
frameLoader()->checkLoadComplete();
if (m_frame) {
if (m_mainResource && m_frame->document()->hasManifest())
MemoryCache::singleton().remove(*m_mainResource);
}
m_applicationCacheHost->finishedLoadingMainResource();
}
bool DocumentLoader::isPostOrRedirectAfterPost(const ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
if (newRequest.httpMethod() == "POST")
return true;
int status = redirectResponse.httpStatusCode();
if (((status >= 301 && status <= 303) || status == 307)
&& m_originalRequest.httpMethod() == "POST")
return true;
return false;
}
void DocumentLoader::handleSubstituteDataLoadNow()
{
ResourceResponse response = m_substituteData.response();
if (response.url().isEmpty())
response = ResourceResponse(m_request.url(), m_substituteData.mimeType(), m_substituteData.content()->size(), m_substituteData.textEncoding());
responseReceived(0, response);
}
void DocumentLoader::startDataLoadTimer()
{
m_dataLoadTimer.startOneShot(0);
#if HAVE(RUNLOOP_TIMER)
if (SchedulePairHashSet* scheduledPairs = m_frame->page()->scheduledRunLoopPairs())
m_dataLoadTimer.schedule(*scheduledPairs);
#endif
}
void DocumentLoader::handleSubstituteDataLoadSoon()
{
if (!m_deferMainResourceDataLoad || frameLoader()->loadsSynchronously())
handleSubstituteDataLoadNow();
else
startDataLoadTimer();
}
void DocumentLoader::redirectReceived(CachedResource* resource, ResourceRequest& request, const ResourceResponse& redirectResponse)
{
ASSERT_UNUSED(resource, resource == m_mainResource);
willSendRequest(request, redirectResponse);
}
void DocumentLoader::willSendRequest(ResourceRequest& newRequest, const ResourceResponse& redirectResponse)
{
ASSERT(!newRequest.isNull());
if (!frameLoader()->checkIfFormActionAllowedByCSP(newRequest.url())) {
cancelMainResourceLoad(frameLoader()->cancelledError(newRequest));
return;
}
ASSERT(timing().fetchStart());
if (!redirectResponse.isNull()) {
Ref<SecurityOrigin> redirectingOrigin(SecurityOrigin::create(redirectResponse.url()));
if (!redirectingOrigin.get().canDisplay(newRequest.url())) {
FrameLoader::reportLocalLoadFailed(m_frame, newRequest.url().string());
cancelMainResourceLoad(frameLoader()->cancelledError(newRequest));
return;
}
if (!portAllowed(newRequest.url())) {
FrameLoader::reportBlockedPortFailed(m_frame, newRequest.url().string());
cancelMainResourceLoad(frameLoader()->blockedError(newRequest));
return;
}
timing().addRedirect(redirectResponse.url(), newRequest.url());
}
if (frameLoader()->frame().isMainFrame())
newRequest.setFirstPartyForCookies(newRequest.url());
if (newRequest.cachePolicy() == UseProtocolCachePolicy && isPostOrRedirectAfterPost(newRequest, redirectResponse))
newRequest.setCachePolicy(ReloadIgnoringCacheData);
Frame& topFrame = m_frame->tree().top();
if (&topFrame != m_frame) {
if (!frameLoader()->mixedContentChecker().canDisplayInsecureContent(topFrame.document()->securityOrigin(), MixedContentChecker::ContentType::Active, newRequest.url())) {
cancelMainResourceLoad(frameLoader()->cancelledError(newRequest));
return;
}
}
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter && !m_contentFilter->continueAfterWillSendRequest(newRequest, redirectResponse))
return;
#endif
setRequest(newRequest);
if (!redirectResponse.isNull()) {
ASSERT(!m_substituteData.isValid());
m_applicationCacheHost->maybeLoadMainResourceForRedirect(newRequest, m_substituteData);
if (m_substituteData.isValid()) {
RELEASE_ASSERT(m_mainResource);
ResourceLoader* loader = m_mainResource->loader();
m_identifierForLoadWithoutResourceLoader = loader ? loader->identifier() : m_mainResource->identifierForLoadWithoutResourceLoader();
}
}
if (redirectResponse.isNull())
return;
ASSERT(!m_waitingForNavigationPolicy);
m_waitingForNavigationPolicy = true;
frameLoader()->policyChecker().checkNavigationPolicy(newRequest, [this](const ResourceRequest& request, PassRefPtr<FormState>, bool shouldContinue) {
continueAfterNavigationPolicy(request, shouldContinue);
});
}
void DocumentLoader::continueAfterNavigationPolicy(const ResourceRequest&, bool shouldContinue)
{
ASSERT(m_waitingForNavigationPolicy);
m_waitingForNavigationPolicy = false;
if (!shouldContinue)
stopLoadingForPolicyChange();
else if (m_substituteData.isValid()) {
ASSERT(timing().redirectCount());
RefPtr<ResourceLoader> resourceLoader = mainResourceLoader();
if (resourceLoader) {
ASSERT(resourceLoader->shouldSendResourceLoadCallbacks());
resourceLoader->setSendCallbackPolicy(DoNotSendCallbacks);
}
clearMainResource();
if (resourceLoader)
resourceLoader->setSendCallbackPolicy(SendCallbacks);
handleSubstituteDataLoadSoon();
}
}
void DocumentLoader::responseReceived(CachedResource* resource, const ResourceResponse& response)
{
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter && !m_contentFilter->continueAfterResponseReceived(resource, response))
return;
#endif
ASSERT_UNUSED(resource, m_mainResource == resource);
Ref<DocumentLoader> protect(*this);
bool willLoadFallback = m_applicationCacheHost->maybeLoadFallbackForMainResponse(request(), response);
if (willLoadFallback)
MemoryCache::singleton().remove(*m_mainResource);
if (willLoadFallback)
return;
const auto& commonHeaders = response.httpHeaderFields().commonHeaders();
auto it = commonHeaders.find(HTTPHeaderName::XFrameOptions);
if (it != commonHeaders.end()) {
String content = it->value;
ASSERT(m_identifierForLoadWithoutResourceLoader || m_mainResource);
unsigned long identifier = m_identifierForLoadWithoutResourceLoader ? m_identifierForLoadWithoutResourceLoader : m_mainResource->identifier();
ASSERT(identifier);
if (frameLoader()->shouldInterruptLoadForXFrameOptions(content, response.url(), identifier)) {
InspectorInstrumentation::continueAfterXFrameOptionsDenied(m_frame, *this, identifier, response);
String message = "Refused to display '" + response.url().stringCenterEllipsizedToLength() + "' in a frame because it set 'X-Frame-Options' to '" + content + "'.";
frame()->document()->addConsoleMessage(MessageSource::Security, MessageLevel::Error, message, identifier);
frame()->document()->enforceSandboxFlags(SandboxOrigin);
if (HTMLFrameOwnerElement* ownerElement = frame()->ownerElement())
ownerElement->dispatchEvent(Event::create(eventNames().loadEvent, false, false));
if (frameLoader())
cancelMainResourceLoad(frameLoader()->cancelledError(m_request));
return;
}
}
#if !USE(CF)
ASSERT(!mainResourceLoader() || !mainResourceLoader()->defersLoading());
#endif
if (m_isLoadingMultipartContent) {
setupForReplace();
m_mainResource->clear();
} else if (response.isMultipart())
m_isLoadingMultipartContent = true;
m_response = response;
if (m_identifierForLoadWithoutResourceLoader) {
if (m_mainResource && m_mainResource->wasRedirected()) {
ASSERT(m_mainResource->status() == CachedResource::Status::Cached);
frameLoader()->client().dispatchDidReceiveServerRedirectForProvisionalLoad();
}
addResponse(m_response);
frameLoader()->notifier().dispatchDidReceiveResponse(this, m_identifierForLoadWithoutResourceLoader, m_response, 0);
}
ASSERT(!m_waitingForContentPolicy);
ASSERT(frameLoader());
m_waitingForContentPolicy = true;
if (m_substituteData.isValid()) {
continueAfterContentPolicy(PolicyUse);
return;
}
#if ENABLE(FTPDIR)
if (m_frame->settings().forceFTPDirectoryListings() && m_response.mimeType() == "application/x-ftp-directory") {
continueAfterContentPolicy(PolicyUse);
return;
}
#endif
frameLoader()->policyChecker().checkContentPolicy(m_response, [this](PolicyAction policy) {
continueAfterContentPolicy(policy);
});
}
void DocumentLoader::continueAfterContentPolicy(PolicyAction policy)
{
ASSERT(m_waitingForContentPolicy);
m_waitingForContentPolicy = false;
if (isStopping())
return;
URL url = m_request.url();
const String& mimeType = m_response.mimeType();
switch (policy) {
case PolicyUse: {
bool isRemoteWebArchive = (equalIgnoringCase("application/x-webarchive", mimeType)
|| equalIgnoringCase("application/x-mimearchive", mimeType)
#if PLATFORM(GTK)
|| equalIgnoringCase("message/rfc822", mimeType)
#endif
|| equalIgnoringCase("multipart/related", mimeType))
&& !m_substituteData.isValid() && !SchemeRegistry::shouldTreatURLSchemeAsLocal(url.protocol());
if (!frameLoader()->client().canShowMIMEType(mimeType) || isRemoteWebArchive) {
frameLoader()->policyChecker().cannotShowMIMEType(m_response);
stopLoadingForPolicyChange();
return;
}
break;
}
case PolicyDownload: {
if (!m_mainResource) {
mainReceivedError(frameLoader()->client().cannotShowURLError(m_request));
return;
}
if (ResourceLoader* mainResourceLoader = this->mainResourceLoader())
InspectorInstrumentation::continueWithPolicyDownload(m_frame, *this, mainResourceLoader->identifier(), m_response);
frameLoader()->setOriginalURLForDownloadRequest(m_request);
frameLoader()->client().convertMainResourceLoadToDownload(this, m_request, m_response);
if (mainResourceLoader())
mainResourceLoader()->didFail(interruptedForPolicyChangeError());
return;
}
case PolicyIgnore:
if (ResourceLoader* mainResourceLoader = this->mainResourceLoader())
InspectorInstrumentation::continueWithPolicyIgnore(m_frame, *this, mainResourceLoader->identifier(), m_response);
stopLoadingForPolicyChange();
return;
default:
ASSERT_NOT_REACHED();
}
if (m_response.isHTTP()) {
int status = m_response.httpStatusCode(); if (status && (status < 200 || status >= 300)) {
bool hostedByObject = frameLoader()->isHostedByObjectElement();
frameLoader()->handleFallbackContent();
if (hostedByObject)
cancelMainResourceLoad(frameLoader()->cancelledError(m_request));
}
}
if (!isStopping() && m_substituteData.isValid() && isLoadingMainResource()) {
if (m_substituteData.content()->size())
dataReceived(0, m_substituteData.content()->data(), m_substituteData.content()->size());
if (isLoadingMainResource())
finishedLoading(0);
}
}
void DocumentLoader::commitLoad(const char* data, int length)
{
RefPtr<Frame> protectFrame(m_frame);
Ref<DocumentLoader> protectLoader(*this);
commitIfReady();
FrameLoader* frameLoader = DocumentLoader::frameLoader();
if (!frameLoader)
return;
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
if (ArchiveFactory::isArchiveMimeType(response().mimeType()))
return;
#endif
frameLoader->client().committedLoad(this, data, length);
if (isMultipartReplacingLoad())
frameLoader->client().didReplaceMultipartContent();
}
ResourceError DocumentLoader::interruptedForPolicyChangeError() const
{
return frameLoader()->client().interruptedForPolicyChangeError(request());
}
void DocumentLoader::stopLoadingForPolicyChange()
{
ResourceError error = interruptedForPolicyChangeError();
error.setIsCancellation(true);
cancelMainResourceLoad(error);
}
void DocumentLoader::commitData(const char* bytes, size_t length)
{
if (!m_gotFirstByte) {
m_gotFirstByte = true;
m_writer.begin(documentURL(), false);
m_writer.setDocumentWasLoadedAsPartOfNavigation();
if (SecurityPolicy::allowSubstituteDataAccessToLocal() && m_originalSubstituteDataWasValid) {
m_frame->document()->securityOrigin()->grantLoadLocalResources();
}
if (frameLoader()->stateMachine().creatingInitialEmptyDocument())
return;
#if ENABLE(MHTML)
if (m_archive && m_archive->type() == Archive::MHTML)
m_frame->document()->setBaseURLOverride(m_archive->mainResource()->url());
#endif
if (!isMultipartReplacingLoad())
frameLoader()->receivedFirstData();
if (!isLoading())
return;
bool userChosen;
String encoding;
if (overrideEncoding().isNull()) {
userChosen = false;
encoding = response().textEncodingName();
#if ENABLE(WEB_ARCHIVE)
if (m_archive && m_archive->type() == Archive::WebArchive)
encoding = m_archive->mainResource()->textEncoding();
#endif
} else {
userChosen = true;
encoding = overrideEncoding();
}
m_writer.setEncoding(encoding, userChosen);
}
#if ENABLE(CONTENT_EXTENSIONS)
DocumentStyleSheetCollection& styleSheetCollection = m_frame->document()->styleSheetCollection();
for (auto& pendingStyleSheet : m_pendingNamedContentExtensionStyleSheets)
styleSheetCollection.maybeAddContentExtensionSheet(pendingStyleSheet.key, *pendingStyleSheet.value);
for (auto& pendingSelectorEntry : m_pendingContentExtensionDisplayNoneSelectors) {
for (const auto& pendingSelector : pendingSelectorEntry.value)
styleSheetCollection.addDisplayNoneSelector(pendingSelectorEntry.key, pendingSelector.first, pendingSelector.second);
}
m_pendingNamedContentExtensionStyleSheets.clear();
m_pendingContentExtensionDisplayNoneSelectors.clear();
#endif
ASSERT(m_frame->document()->parsing());
m_writer.addData(bytes, length);
}
void DocumentLoader::dataReceived(CachedResource* resource, const char* data, int length)
{
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter && !m_contentFilter->continueAfterDataReceived(resource, data, length))
return;
#endif
ASSERT(data);
ASSERT(length);
ASSERT_UNUSED(resource, resource == m_mainResource);
ASSERT(!m_response.isNull());
#if !USE(CF)
ASSERT(!mainResourceLoader() || !mainResourceLoader()->defersLoading());
#endif
if (m_identifierForLoadWithoutResourceLoader)
frameLoader()->notifier().dispatchDidReceiveData(this, m_identifierForLoadWithoutResourceLoader, data, length, -1);
m_applicationCacheHost->mainResourceDataReceived(data, length, -1, false);
m_timeOfLastDataReceived = monotonicallyIncreasingTime();
if (!isMultipartReplacingLoad())
commitLoad(data, length);
}
void DocumentLoader::setupForReplace()
{
if (!mainResourceData())
return;
frameLoader()->client().willReplaceMultipartContent();
maybeFinishLoadingMultipartContent();
maybeCreateArchive();
m_writer.end();
frameLoader()->setReplacing();
m_gotFirstByte = false;
stopLoadingSubresources();
stopLoadingPlugIns();
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
clearArchiveResources();
#endif
}
void DocumentLoader::checkLoadComplete()
{
if (!m_frame || isLoading())
return;
ASSERT(this == frameLoader()->activeDocumentLoader());
m_frame->document()->domWindow()->finishedLoading();
}
void DocumentLoader::attachToFrame(Frame& frame)
{
if (m_frame == &frame)
return;
ASSERT(!m_frame);
m_frame = &frame;
m_writer.setFrame(&frame);
attachToFrame();
#ifndef NDEBUG
m_hasEverBeenAttached = true;
#endif
}
void DocumentLoader::attachToFrame()
{
ASSERT(m_frame);
}
void DocumentLoader::detachFromFrame()
{
#ifndef NDEBUG
if (m_hasEverBeenAttached)
ASSERT_WITH_MESSAGE(m_frame, "detachFromFrame() is being called on a DocumentLoader twice without an attachToFrame() inbetween");
else
ASSERT_WITH_MESSAGE(m_frame, "detachFromFrame() is being called on a DocumentLoader that has never attached to any Frame");
#endif
RefPtr<Frame> protectFrame(m_frame);
Ref<DocumentLoader> protectLoader(*this);
stopLoading();
if (m_mainResource && m_mainResource->hasClient(this))
m_mainResource->removeClient(this);
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter)
m_contentFilter->stopFilteringMainResource();
#endif
m_applicationCacheHost->setDOMApplicationCache(nullptr);
cancelPolicyCheckIfNeeded();
if (m_frame)
InspectorInstrumentation::loaderDetachedFromFrame(*m_frame, *this);
m_frame = nullptr;
}
void DocumentLoader::clearMainResourceLoader()
{
m_loadingMainResource = false;
if (this == frameLoader()->activeDocumentLoader())
checkLoadComplete();
}
bool DocumentLoader::isLoadingInAPISense() const
{
if (frameLoader()->state() != FrameStateComplete) {
if (m_frame->settings().needsIsLoadingInAPISenseQuirk() && !m_subresourceLoaders.isEmpty())
return true;
Document* doc = m_frame->document();
if ((isLoadingMainResource() || !m_frame->document()->loadEventFinished()) && isLoading())
return true;
if (m_cachedResourceLoader->requestCount())
return true;
if (doc->processingLoadEvent())
return true;
if (doc->hasActiveParser())
return true;
}
return frameLoader()->subframeIsLoading();
}
bool DocumentLoader::maybeCreateArchive()
{
#if !ENABLE(WEB_ARCHIVE) && !ENABLE(MHTML)
return false;
#else
m_archive = ArchiveFactory::create(m_response.url(), mainResourceData().get(), m_response.mimeType());
if (!m_archive)
return false;
addAllArchiveResources(m_archive.get());
ArchiveResource* mainResource = m_archive->mainResource();
m_parsedArchiveData = mainResource->data();
m_writer.setMIMEType(mainResource->mimeType());
ASSERT(m_frame->document());
commitData(mainResource->data()->data(), mainResource->data()->size());
return true;
#endif // !ENABLE(WEB_ARCHIVE) && !ENABLE(MHTML)
}
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
void DocumentLoader::setArchive(PassRefPtr<Archive> archive)
{
m_archive = archive;
addAllArchiveResources(m_archive.get());
}
void DocumentLoader::addAllArchiveResources(Archive* archive)
{
if (!m_archiveResourceCollection)
m_archiveResourceCollection = std::make_unique<ArchiveResourceCollection>();
ASSERT(archive);
if (!archive)
return;
m_archiveResourceCollection->addAllResources(archive);
}
void DocumentLoader::addArchiveResource(PassRefPtr<ArchiveResource> resource)
{
if (!m_archiveResourceCollection)
m_archiveResourceCollection = std::make_unique<ArchiveResourceCollection>();
ASSERT(resource);
if (!resource)
return;
m_archiveResourceCollection->addResource(resource);
}
PassRefPtr<Archive> DocumentLoader::popArchiveForSubframe(const String& frameName, const URL& url)
{
return m_archiveResourceCollection ? m_archiveResourceCollection->popSubframeArchive(frameName, url) : PassRefPtr<Archive>(0);
}
void DocumentLoader::clearArchiveResources()
{
m_archiveResourceCollection = nullptr;
m_substituteResourceDeliveryTimer.stop();
}
SharedBuffer* DocumentLoader::parsedArchiveData() const
{
return m_parsedArchiveData.get();
}
#endif // ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
ArchiveResource* DocumentLoader::archiveResourceForURL(const URL& url) const
{
if (!m_archiveResourceCollection)
return nullptr;
ArchiveResource* resource = m_archiveResourceCollection->archiveResourceForURL(url);
if (!resource || resource->shouldIgnoreWhenUnarchiving())
return nullptr;
return resource;
}
PassRefPtr<ArchiveResource> DocumentLoader::mainResource() const
{
RefPtr<SharedBuffer> data = mainResourceData();
if (!data)
data = SharedBuffer::create();
auto& response = this->response();
return ArchiveResource::create(data, response.url(), response.mimeType(), response.textEncodingName(), frame()->tree().uniqueName());
}
PassRefPtr<ArchiveResource> DocumentLoader::subresource(const URL& url) const
{
if (!isCommitted())
return nullptr;
CachedResource* resource = m_cachedResourceLoader->cachedResource(url);
if (!resource || !resource->isLoaded())
return archiveResourceForURL(url);
if (resource->type() == CachedResource::MainResource)
return nullptr;
auto* data = resource->resourceBuffer();
if (!data)
return nullptr;
return ArchiveResource::create(data, url, resource->response());
}
Vector<RefPtr<ArchiveResource>> DocumentLoader::subresources() const
{
if (!isCommitted())
return { };
Vector<RefPtr<ArchiveResource>> subresources;
for (auto& cachedResourceHandle : m_cachedResourceLoader->allCachedResources().values()) {
if (RefPtr<ArchiveResource> subresource = this->subresource(URL(ParsedURLString, cachedResourceHandle->url())))
subresources.append(WTF::move(subresource));
}
return subresources;
}
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()
{
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->key;
SubstituteResource* resource = it->value.get();
if (resource)
resource->deliver(*loader);
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();
}
#if ENABLE(WEB_ARCHIVE) || ENABLE(MHTML)
bool DocumentLoader::scheduleArchiveLoad(ResourceLoader* loader, const ResourceRequest& request)
{
if (ArchiveResource* resource = archiveResourceForURL(request.url())) {
scheduleSubstituteResourceLoad(*loader, *resource);
return true;
}
if (!m_archive)
return false;
switch (m_archive->type()) {
#if ENABLE(WEB_ARCHIVE)
case Archive::WebArchive:
return m_frame->settings().webArchiveDebugModeEnabled() && ArchiveFactory::isArchiveMimeType(responseMIMEType());
#endif
#if ENABLE(MHTML)
case Archive::MHTML:
return true; #endif
default:
return false;
}
}
#endif // ENABLE(WEB_ARCHIVE)
void DocumentLoader::scheduleSubstituteResourceLoad(ResourceLoader& loader, SubstituteResource& resource)
{
m_pendingSubstituteResources.set(&loader, &resource);
deliverSubstituteResourcesAfterDelay();
}
void DocumentLoader::addResponse(const ResourceResponse& r)
{
if (!m_stopRecordingResponses)
m_responses.append(r);
}
void DocumentLoader::stopRecordingResponses()
{
m_stopRecordingResponses = true;
m_responses.shrinkToFit();
}
void DocumentLoader::setTitle(const StringWithDirection& title)
{
if (m_pageTitle == title)
return;
frameLoader()->willChangeTitle(this);
m_pageTitle = title;
frameLoader()->didChangeTitle(this);
}
URL DocumentLoader::urlForHistory() const
{
if (m_substituteData.isValid() && !m_substituteData.shouldRevealToSessionHistory())
return unreachableURL();
return m_originalRequestCopy.url();
}
bool DocumentLoader::urlForHistoryReflectsFailure() const
{
return m_substituteData.isValid() || m_response.httpStatusCode() >= 400;
}
const URL& DocumentLoader::originalURL() const
{
return m_originalRequestCopy.url();
}
const URL& DocumentLoader::responseURL() const
{
return m_response.url();
}
URL DocumentLoader::documentURL() const
{
URL url = substituteData().response().url();
#if ENABLE(WEB_ARCHIVE)
if (url.isEmpty() && m_archive && m_archive->type() == Archive::WebArchive)
url = m_archive->mainResource()->url();
#endif
if (url.isEmpty())
url = m_request.url();
if (url.isEmpty())
url = m_response.url();
return url;
}
const String& DocumentLoader::responseMIMEType() const
{
return m_response.mimeType();
}
const String& DocumentLoader::currentContentType() const
{
return m_writer.mimeType();
}
#if PLATFORM(IOS)
void DocumentLoader::setResponseMIMEType(const String& responseMimeType)
{
m_response.setMimeType(responseMimeType);
}
#endif
const URL& DocumentLoader::unreachableURL() const
{
return m_substituteData.failingURL();
}
void DocumentLoader::setDefersLoading(bool defers)
{
if (mainResourceLoader() && mainResourceLoader()->documentLoader() == this)
mainResourceLoader()->setDefersLoading(defers);
setAllDefersLoading(m_subresourceLoaders, defers);
setAllDefersLoading(m_plugInStreamLoaders, defers);
if (!defers)
deliverSubstituteResourcesAfterDelay();
}
void DocumentLoader::setMainResourceDataBufferingPolicy(DataBufferingPolicy dataBufferingPolicy)
{
if (m_mainResource)
m_mainResource->setDataBufferingPolicy(dataBufferingPolicy);
}
void DocumentLoader::stopLoadingPlugIns()
{
cancelAll(m_plugInStreamLoaders);
}
void DocumentLoader::stopLoadingSubresources()
{
cancelAll(m_subresourceLoaders);
ASSERT(m_subresourceLoaders.isEmpty());
}
void DocumentLoader::addSubresourceLoader(ResourceLoader* loader)
{
if (!m_gotFirstByte)
return;
ASSERT(loader->identifier());
ASSERT(!m_subresourceLoaders.contains(loader->identifier()));
ASSERT(!mainResourceLoader() || mainResourceLoader() != loader);
ASSERT_WITH_SECURITY_IMPLICATION(!document() || !document()->inPageCache());
m_subresourceLoaders.add(loader->identifier(), loader);
}
void DocumentLoader::removeSubresourceLoader(ResourceLoader* loader)
{
ASSERT(loader->identifier());
if (!m_subresourceLoaders.remove(loader->identifier()))
return;
checkLoadComplete();
if (Frame* frame = m_frame)
frame->loader().checkLoadComplete();
}
void DocumentLoader::addPlugInStreamLoader(ResourceLoader* loader)
{
ASSERT(loader->identifier());
ASSERT(!m_plugInStreamLoaders.contains(loader->identifier()));
m_plugInStreamLoaders.add(loader->identifier(), loader);
}
void DocumentLoader::removePlugInStreamLoader(ResourceLoader* loader)
{
ASSERT(loader->identifier());
ASSERT(loader == m_plugInStreamLoaders.get(loader->identifier()));
m_plugInStreamLoaders.remove(loader->identifier());
checkLoadComplete();
}
bool DocumentLoader::isMultipartReplacingLoad() const
{
return isLoadingMultipartContent() && frameLoader()->isReplacing();
}
bool DocumentLoader::maybeLoadEmpty()
{
bool shouldLoadEmpty = !m_substituteData.isValid() && (m_request.url().isEmpty() || SchemeRegistry::shouldLoadURLSchemeAsEmptyDocument(m_request.url().protocol()));
if (!shouldLoadEmpty && !frameLoader()->client().representationExistsForURLScheme(m_request.url().protocol()))
return false;
if (m_request.url().isEmpty() && !frameLoader()->stateMachine().creatingInitialEmptyDocument()) {
m_request.setURL(blankURL());
if (isLoadingMainResource())
frameLoader()->client().dispatchDidChangeProvisionalURL();
}
String mimeType = shouldLoadEmpty ? "text/html" : frameLoader()->client().generatedMIMETypeForURLScheme(m_request.url().protocol());
m_response = ResourceResponse(m_request.url(), mimeType, 0, String());
finishedLoading(monotonicallyIncreasingTime());
return true;
}
void DocumentLoader::startLoadingMainResource()
{
m_mainDocumentError = ResourceError();
timing().markNavigationStart();
ASSERT(!m_mainResource);
ASSERT(!m_loadingMainResource);
m_loadingMainResource = true;
if (maybeLoadEmpty())
return;
frameLoader()->addExtraFieldsToMainResourceRequest(m_request);
ASSERT(timing().navigationStart());
ASSERT(!timing().fetchStart());
timing().markFetchStart();
willSendRequest(m_request, ResourceResponse());
if (!m_frame || m_request.isNull())
return;
m_applicationCacheHost->maybeLoadMainResource(m_request, m_substituteData);
if (m_substituteData.isValid()) {
m_identifierForLoadWithoutResourceLoader = m_frame->page()->progress().createUniqueIdentifier();
frameLoader()->notifier().assignIdentifierToInitialRequest(m_identifierForLoadWithoutResourceLoader, this, m_request);
frameLoader()->notifier().dispatchWillSendRequest(this, m_identifierForLoadWithoutResourceLoader, m_request, ResourceResponse());
handleSubstituteDataLoadSoon();
return;
}
ResourceRequest request(m_request);
request.setRequester(ResourceRequest::Requester::Main);
request.makeUnconditional();
static NeverDestroyed<ResourceLoaderOptions> mainResourceLoadOptions(SendCallbacks, SniffContent, BufferData, AllowStoredCredentials, AskClientForAllCredentials, SkipSecurityCheck, UseDefaultOriginRestrictionsForType, IncludeCertificateInfo, ContentSecurityPolicyImposition::DoPolicyCheck);
CachedResourceRequest cachedResourceRequest(request, mainResourceLoadOptions);
cachedResourceRequest.setInitiator(*this);
m_mainResource = m_cachedResourceLoader->requestMainResource(cachedResourceRequest);
if (!m_mainResource) {
setRequest(ResourceRequest());
m_applicationCacheHost = std::make_unique<ApplicationCacheHost>(*this);
maybeLoadEmpty();
return;
}
if (!mainResourceLoader()) {
m_identifierForLoadWithoutResourceLoader = m_frame->page()->progress().createUniqueIdentifier();
frameLoader()->notifier().assignIdentifierToInitialRequest(m_identifierForLoadWithoutResourceLoader, this, request);
frameLoader()->notifier().dispatchWillSendRequest(this, m_identifierForLoadWithoutResourceLoader, request, ResourceResponse());
}
becomeMainResourceClient();
if (mainResourceLoader())
request = mainResourceLoader()->originalRequest();
if (equalIgnoringFragmentIdentifier(m_request.url(), request.url()))
request.setURL(m_request.url());
setRequest(request);
}
void DocumentLoader::cancelPolicyCheckIfNeeded()
{
if ((m_waitingForContentPolicy || m_waitingForNavigationPolicy) && frameLoader()) {
frameLoader()->policyChecker().cancelCheck();
m_waitingForContentPolicy = false;
m_waitingForNavigationPolicy = false;
}
}
void DocumentLoader::cancelMainResourceLoad(const ResourceError& resourceError)
{
Ref<DocumentLoader> protect(*this);
ResourceError error = resourceError.isNull() ? frameLoader()->cancelledError(m_request) : resourceError;
m_dataLoadTimer.stop();
cancelPolicyCheckIfNeeded();
if (mainResourceLoader())
mainResourceLoader()->cancel(error);
clearMainResource();
mainReceivedError(error);
}
void DocumentLoader::clearMainResource()
{
if (m_mainResource && m_mainResource->hasClient(this))
m_mainResource->removeClient(this);
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter)
m_contentFilter->stopFilteringMainResource();
#endif
m_mainResource = 0;
}
void DocumentLoader::subresourceLoaderFinishedLoadingOnePart(ResourceLoader* loader)
{
unsigned long identifier = loader->identifier();
ASSERT(identifier);
if (!m_multipartSubresourceLoaders.add(identifier, loader).isNewEntry) {
ASSERT(m_multipartSubresourceLoaders.get(identifier) == loader);
ASSERT(!m_subresourceLoaders.contains(identifier));
} else {
ASSERT(m_subresourceLoaders.contains(identifier));
m_subresourceLoaders.remove(identifier);
}
checkLoadComplete();
if (Frame* frame = m_frame)
frame->loader().checkLoadComplete();
}
void DocumentLoader::maybeFinishLoadingMultipartContent()
{
if (!isMultipartReplacingLoad())
return;
frameLoader()->setupForReplace();
m_committed = false;
RefPtr<SharedBuffer> resourceData = mainResourceData();
commitLoad(resourceData->data(), resourceData->size());
}
void DocumentLoader::iconLoadDecisionAvailable()
{
if (m_frame)
m_frame->loader().icon().loadDecisionReceived(iconDatabase().synchronousLoadDecisionForIconURL(frameLoader()->icon().url(), this));
}
static void iconLoadDecisionCallback(IconLoadDecision decision, void* context)
{
static_cast<DocumentLoader*>(context)->continueIconLoadWithDecision(decision);
}
void DocumentLoader::getIconLoadDecisionForIconURL(const String& urlString)
{
if (m_iconLoadDecisionCallback)
m_iconLoadDecisionCallback->invalidate();
m_iconLoadDecisionCallback = IconLoadDecisionCallback::create(this, iconLoadDecisionCallback);
iconDatabase().loadDecisionForIconURL(urlString, m_iconLoadDecisionCallback);
}
void DocumentLoader::continueIconLoadWithDecision(IconLoadDecision decision)
{
ASSERT(m_iconLoadDecisionCallback);
m_iconLoadDecisionCallback = nullptr;
if (m_frame)
m_frame->loader().icon().continueLoadWithDecision(decision);
}
static void iconDataCallback(SharedBuffer*, void*)
{
}
void DocumentLoader::getIconDataForIconURL(const String& urlString)
{
if (m_iconDataCallback)
m_iconDataCallback->invalidate();
m_iconDataCallback = IconDataCallback::create(this, iconDataCallback);
iconDatabase().iconDataForIconURL(urlString, m_iconDataCallback);
}
void DocumentLoader::handledOnloadEvents()
{
m_wasOnloadHandled = true;
applicationCacheHost()->stopDeferringEvents();
}
void DocumentLoader::setTriggeringAction(const NavigationAction& action)
{
m_triggeringAction = action.copyWithShouldOpenExternalURLsPolicy(m_frame ? shouldOpenExternalURLsPolicyToPropagate() : m_shouldOpenExternalURLsPolicy);
}
ShouldOpenExternalURLsPolicy DocumentLoader::shouldOpenExternalURLsPolicyToPropagate() const
{
if (!m_frame || !m_frame->isMainFrame())
return ShouldOpenExternalURLsPolicy::ShouldNotAllow;
return m_shouldOpenExternalURLsPolicy;
}
void DocumentLoader::becomeMainResourceClient()
{
#if ENABLE(CONTENT_FILTERING)
if (m_contentFilter)
m_contentFilter->startFilteringMainResource(*m_mainResource);
#endif
m_mainResource->addClient(this);
}
#if ENABLE(CONTENT_EXTENSIONS)
void DocumentLoader::addPendingContentExtensionSheet(const String& identifier, StyleSheetContents& sheet)
{
ASSERT(!m_gotFirstByte);
m_pendingNamedContentExtensionStyleSheets.set(identifier, &sheet);
}
void DocumentLoader::addPendingContentExtensionDisplayNoneSelector(const String& identifier, const String& selector, uint32_t selectorID)
{
ASSERT(!m_gotFirstByte);
auto addResult = m_pendingContentExtensionDisplayNoneSelectors.add(identifier, Vector<std::pair<String, uint32_t>>());
addResult.iterator->value.append(std::make_pair(selector, selectorID));
}
#endif
#if ENABLE(CONTENT_FILTERING)
void DocumentLoader::installContentFilterUnblockHandler(ContentFilter& contentFilter)
{
ContentFilterUnblockHandler unblockHandler { contentFilter.unblockHandler() };
unblockHandler.setUnreachableURL(documentURL());
RefPtr<Frame> frame { this->frame() };
String unblockRequestDeniedScript { contentFilter.unblockRequestDeniedScript() };
if (!unblockRequestDeniedScript.isEmpty() && frame) {
static_assert(std::is_base_of<ThreadSafeRefCounted<Frame>, Frame>::value, "Frame must be ThreadSafeRefCounted.");
StringCapture capturedScript { unblockRequestDeniedScript };
unblockHandler.wrapWithDecisionHandler([frame, capturedScript](bool unblocked) {
if (!unblocked)
frame->script().executeScript(capturedScript.string());
});
}
frameLoader()->client().contentFilterDidBlockLoad(WTF::move(unblockHandler));
}
void DocumentLoader::contentFilterDidBlock()
{
ASSERT(m_contentFilter);
installContentFilterUnblockHandler(*m_contentFilter);
URL blockedURL;
blockedURL.setProtocol(ContentFilter::urlScheme());
blockedURL.setHost(ASCIILiteral("blocked-page"));
auto replacementData = m_contentFilter->replacementData();
ResourceResponse response(URL(), ASCIILiteral("text/html"), replacementData->size(), ASCIILiteral("UTF-8"));
SubstituteData substituteData { adoptRef(&replacementData.leakRef()), documentURL(), response, SubstituteData::SessionHistoryVisibility::Hidden };
frame()->navigationScheduler().scheduleSubstituteDataLoad(blockedURL, substituteData);
}
#endif
}