2014-05-21 10:08:12 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
#include "ImportManager.h"
|
|
|
|
|
|
|
|
#include "mozilla/EventListenerManager.h"
|
|
|
|
#include "HTMLLinkElement.h"
|
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsContentUtils.h"
|
2014-07-16 05:31:59 -07:00
|
|
|
#include "nsCrossSiteListenerProxy.h"
|
2014-05-21 10:08:12 -07:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIContentPolicy.h"
|
|
|
|
#include "nsIContentSecurityPolicy.h"
|
|
|
|
#include "nsIDocument.h"
|
|
|
|
#include "nsIDOMDocument.h"
|
|
|
|
#include "nsIDOMEvent.h"
|
|
|
|
#include "nsIPrincipal.h"
|
|
|
|
#include "nsIScriptObjectPrincipal.h"
|
|
|
|
#include "nsScriptLoader.h"
|
|
|
|
#include "nsNetUtil.h"
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// AutoError
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
class AutoError {
|
|
|
|
public:
|
2014-09-01 17:49:25 -07:00
|
|
|
explicit AutoError(mozilla::dom::ImportLoader* loader, bool scriptsBlocked = true)
|
2014-05-21 10:08:12 -07:00
|
|
|
: mLoader(loader)
|
|
|
|
, mPassed(false)
|
2014-06-30 16:30:13 -07:00
|
|
|
, mScriptsBlocked(scriptsBlocked)
|
2014-05-21 10:08:12 -07:00
|
|
|
{}
|
|
|
|
|
|
|
|
~AutoError()
|
|
|
|
{
|
|
|
|
if (!mPassed) {
|
2014-06-30 16:30:13 -07:00
|
|
|
mLoader->Error(mScriptsBlocked);
|
2014-05-21 10:08:12 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void Pass() { mPassed = true; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
mozilla::dom::ImportLoader* mLoader;
|
|
|
|
bool mPassed;
|
2014-06-30 16:30:13 -07:00
|
|
|
bool mScriptsBlocked;
|
2014-05-21 10:08:12 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
namespace mozilla {
|
|
|
|
namespace dom {
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ImportLoader::Updater
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::Updater::GetReferrerChain(nsINode* aNode,
|
|
|
|
nsTArray<nsINode*>& aResult)
|
|
|
|
{
|
|
|
|
// We fill up the array backward. First the last link: aNode.
|
|
|
|
MOZ_ASSERT(mLoader->mLinks.Contains(aNode));
|
|
|
|
|
|
|
|
aResult.AppendElement(aNode);
|
|
|
|
nsINode* node = aNode;
|
|
|
|
nsRefPtr<ImportManager> manager = mLoader->Manager();
|
|
|
|
for (ImportLoader* referrersLoader = manager->Find(node->OwnerDoc());
|
|
|
|
referrersLoader;
|
|
|
|
referrersLoader = manager->Find(node->OwnerDoc()))
|
|
|
|
{
|
|
|
|
// Then walking up the main referrer chain and append each link
|
|
|
|
// to the array.
|
|
|
|
node = referrersLoader->GetMainReferrer();
|
|
|
|
MOZ_ASSERT(node);
|
|
|
|
aResult.AppendElement(node);
|
|
|
|
}
|
|
|
|
|
|
|
|
// The reversed order is more useful for consumers.
|
|
|
|
// XXX: This should probably go to nsTArray or some generic utility
|
|
|
|
// lib for our containers that we don't have... I would really like to
|
|
|
|
// get rid of this part...
|
|
|
|
uint32_t l = aResult.Length();
|
|
|
|
for (uint32_t i = 0; i < l / 2; i++) {
|
|
|
|
Swap(aResult[i], aResult[l - i - 1]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImportLoader::Updater::ShouldUpdate(nsTArray<nsINode*>& aNewPath)
|
|
|
|
{
|
2014-10-06 02:43:58 -07:00
|
|
|
if (mLoader->Manager()->GetNearestPredecessor(mLoader->GetMainReferrer()) !=
|
|
|
|
mLoader->mBlockingPredecessor) {
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-01 05:13:53 -07:00
|
|
|
// Let's walk down on the main referrer chains of both the current main and
|
|
|
|
// the new link, and find the last pair of links that are from the same
|
|
|
|
// document. This is the junction point between the two referrer chain. Their
|
|
|
|
// order in the subimport list of that document will determine if we have to
|
|
|
|
// update the spanning tree or this new edge changes nothing in the script
|
|
|
|
// execution order.
|
|
|
|
nsTArray<nsINode*> oldPath;
|
|
|
|
GetReferrerChain(mLoader->mLinks[mLoader->mMainReferrer], oldPath);
|
|
|
|
uint32_t max = std::min(oldPath.Length(), aNewPath.Length());
|
|
|
|
MOZ_ASSERT(max > 0);
|
|
|
|
uint32_t lastCommonImportAncestor = 0;
|
|
|
|
|
|
|
|
for (uint32_t i = 0;
|
|
|
|
i < max && oldPath[i]->OwnerDoc() == aNewPath[i]->OwnerDoc();
|
|
|
|
i++)
|
|
|
|
{
|
|
|
|
lastCommonImportAncestor = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(lastCommonImportAncestor < max);
|
|
|
|
nsINode* oldLink = oldPath[lastCommonImportAncestor];
|
|
|
|
nsINode* newLink = aNewPath[lastCommonImportAncestor];
|
|
|
|
|
|
|
|
if ((lastCommonImportAncestor == max - 1) &&
|
|
|
|
newLink == oldLink ) {
|
|
|
|
// If one chain contains the other entirely, then this is a simple cycle,
|
|
|
|
// nothing to be done here.
|
|
|
|
MOZ_ASSERT(oldPath.Length() != aNewPath.Length(),
|
|
|
|
"This would mean that new link == main referrer link");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aNewPath != oldPath,
|
|
|
|
"How could this happen?");
|
|
|
|
nsIDocument* doc = oldLink->OwnerDoc();
|
|
|
|
MOZ_ASSERT(doc->HasSubImportLink(newLink));
|
|
|
|
MOZ_ASSERT(doc->HasSubImportLink(oldLink));
|
|
|
|
|
|
|
|
return doc->IndexOfSubImportLink(newLink) < doc->IndexOfSubImportLink(oldLink);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::Updater::UpdateMainReferrer(uint32_t aNewIdx)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aNewIdx < mLoader->mLinks.Length());
|
|
|
|
nsINode* newMainReferrer = mLoader->mLinks[aNewIdx];
|
|
|
|
|
|
|
|
// This new link means we have to execute our scripts sooner...
|
|
|
|
// Let's make sure that unblocking a loader does not trigger a script execution.
|
|
|
|
// So we start with placing the new blockers and only then will we remove any
|
|
|
|
// blockers.
|
|
|
|
if (mLoader->IsBlocking()) {
|
|
|
|
// Our import parent is changed, let's block the new one and later unblock
|
|
|
|
// the old one.
|
|
|
|
newMainReferrer->OwnerDoc()->ScriptLoader()->AddExecuteBlocker();
|
2014-11-06 04:41:19 -08:00
|
|
|
newMainReferrer->OwnerDoc()->BlockDOMContentLoaded();
|
2014-10-01 05:13:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
if (mLoader->mDocument) {
|
|
|
|
// Our nearest predecessor has changed. So let's add the ScriptLoader to the
|
|
|
|
// new one if there is any. And remove it from the old one.
|
|
|
|
nsRefPtr<ImportManager> manager = mLoader->Manager();
|
|
|
|
nsScriptLoader* loader = mLoader->mDocument->ScriptLoader();
|
|
|
|
ImportLoader*& pred = mLoader->mBlockingPredecessor;
|
|
|
|
ImportLoader* newPred = manager->GetNearestPredecessor(newMainReferrer);
|
|
|
|
if (pred) {
|
|
|
|
if (newPred) {
|
|
|
|
newPred->AddBlockedScriptLoader(loader);
|
|
|
|
}
|
|
|
|
pred->RemoveBlockedScriptLoader(loader);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLoader->IsBlocking()) {
|
|
|
|
mLoader->mImportParent->ScriptLoader()->RemoveExecuteBlocker();
|
2014-11-06 04:41:19 -08:00
|
|
|
mLoader->mImportParent->UnblockDOMContentLoaded();
|
2014-10-01 05:13:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Finally update mMainReferrer to point to the newly added link.
|
|
|
|
mLoader->mMainReferrer = aNewIdx;
|
|
|
|
mLoader->mImportParent = newMainReferrer->OwnerDoc();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsINode*
|
|
|
|
ImportLoader::Updater::NextDependant(nsINode* aCurrentLink,
|
|
|
|
nsTArray<nsINode*>& aPath,
|
|
|
|
NodeTable& aVisitedNodes, bool aSkipChildren)
|
|
|
|
{
|
|
|
|
// Depth first graph traversal.
|
|
|
|
if (!aSkipChildren) {
|
|
|
|
// "first child"
|
|
|
|
ImportLoader* loader = mLoader->Manager()->Find(aCurrentLink);
|
|
|
|
if (loader && loader->GetDocument()) {
|
|
|
|
nsINode* firstSubImport = loader->GetDocument()->GetSubImportLink(0);
|
|
|
|
if (firstSubImport && !aVisitedNodes.Contains(firstSubImport)) {
|
|
|
|
aPath.AppendElement(aCurrentLink);
|
|
|
|
aVisitedNodes.PutEntry(firstSubImport);
|
|
|
|
return firstSubImport;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
aPath.AppendElement(aCurrentLink);
|
|
|
|
// "(parent's) next sibling"
|
|
|
|
while(aPath.Length() > 1) {
|
|
|
|
aCurrentLink = aPath[aPath.Length() - 1];
|
|
|
|
aPath.RemoveElementAt(aPath.Length() - 1);
|
|
|
|
|
|
|
|
// Let's find the next "sibling"
|
|
|
|
ImportLoader* loader = mLoader->Manager()->Find(aCurrentLink->OwnerDoc());
|
|
|
|
MOZ_ASSERT(loader && loader->GetDocument(), "How can this happend?");
|
|
|
|
nsIDocument* doc = loader->GetDocument();
|
|
|
|
MOZ_ASSERT(doc->HasSubImportLink(aCurrentLink));
|
|
|
|
uint32_t idx = doc->IndexOfSubImportLink(aCurrentLink);
|
|
|
|
nsINode* next = doc->GetSubImportLink(idx + 1);
|
|
|
|
if (next) {
|
|
|
|
// Note: If we found an already visited link that means the parent links has
|
|
|
|
// closed the circle it's always the "first child" section that should find
|
|
|
|
// the first already visited node. Let's just assert that.
|
|
|
|
MOZ_ASSERT(!aVisitedNodes.Contains(next));
|
|
|
|
aVisitedNodes.PutEntry(next);
|
|
|
|
return next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::Updater::UpdateDependants(nsINode* aNode,
|
|
|
|
nsTArray<nsINode*>& aPath)
|
|
|
|
{
|
|
|
|
NodeTable visitedNodes;
|
|
|
|
nsINode* current = aNode;
|
|
|
|
uint32_t initialLength = aPath.Length();
|
|
|
|
bool neededUpdate = true;
|
|
|
|
while ((current = NextDependant(current, aPath, visitedNodes, !neededUpdate))) {
|
|
|
|
if (!current || aPath.Length() <= initialLength) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
ImportLoader* loader = mLoader->Manager()->Find(current);
|
|
|
|
if (!loader) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
Updater& updater = loader->mUpdater;
|
|
|
|
neededUpdate = updater.ShouldUpdate(aPath);
|
|
|
|
if (neededUpdate) {
|
|
|
|
updater.UpdateMainReferrer(loader->mLinks.IndexOf(current));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::Updater::UpdateSpanningTree(nsINode* aNode)
|
|
|
|
{
|
|
|
|
if (mLoader->mReady || mLoader->mStopped) {
|
|
|
|
// Scripts already executed, nothing to be done here.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mLoader->mLinks.Length() == 1) {
|
|
|
|
// If this is the first referrer, let's mark it.
|
|
|
|
mLoader->mMainReferrer = 0;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<nsINode*> newReferrerChain;
|
|
|
|
GetReferrerChain(aNode, newReferrerChain);
|
|
|
|
if (ShouldUpdate(newReferrerChain)) {
|
|
|
|
UpdateMainReferrer(mLoader->mLinks.Length() - 1);
|
|
|
|
UpdateDependants(aNode, newReferrerChain);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ImportLoader
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
NS_INTERFACE_MAP_BEGIN(ImportLoader)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIStreamListener)
|
|
|
|
NS_INTERFACE_MAP_ENTRY(nsIRequestObserver)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(ImportLoader)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ImportLoader)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ImportLoader)
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTION(ImportLoader,
|
|
|
|
mDocument,
|
2014-07-16 05:31:58 -07:00
|
|
|
mImportParent,
|
2014-05-21 10:08:12 -07:00
|
|
|
mLinks)
|
|
|
|
|
|
|
|
ImportLoader::ImportLoader(nsIURI* aURI, nsIDocument* aImportParent)
|
|
|
|
: mURI(aURI)
|
|
|
|
, mImportParent(aImportParent)
|
2014-10-01 05:13:53 -07:00
|
|
|
, mBlockingPredecessor(nullptr)
|
2014-05-21 10:08:12 -07:00
|
|
|
, mReady(false)
|
|
|
|
, mStopped(false)
|
|
|
|
, mBlockingScripts(false)
|
2014-10-01 05:13:53 -07:00
|
|
|
, mUpdater(MOZ_THIS_IN_INITIALIZER_LIST())
|
|
|
|
{
|
|
|
|
}
|
2014-05-21 10:08:12 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::BlockScripts()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!mBlockingScripts);
|
|
|
|
mImportParent->ScriptLoader()->AddExecuteBlocker();
|
2014-11-06 04:41:19 -08:00
|
|
|
mImportParent->BlockDOMContentLoaded();
|
2014-05-21 10:08:12 -07:00
|
|
|
mBlockingScripts = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::UnblockScripts()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mBlockingScripts);
|
|
|
|
mImportParent->ScriptLoader()->RemoveExecuteBlocker();
|
2014-11-06 04:41:19 -08:00
|
|
|
mImportParent->UnblockDOMContentLoaded();
|
2014-10-01 05:13:53 -07:00
|
|
|
for (uint32_t i = 0; i < mBlockedScriptLoaders.Length(); i++) {
|
|
|
|
mBlockedScriptLoaders[i]->RemoveExecuteBlocker();
|
|
|
|
}
|
|
|
|
mBlockedScriptLoaders.Clear();
|
2014-05-21 10:08:12 -07:00
|
|
|
mBlockingScripts = false;
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
void
|
|
|
|
ImportLoader::SetBlockingPredecessor(ImportLoader* aLoader)
|
|
|
|
{
|
|
|
|
mBlockingPredecessor = aLoader;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
void
|
|
|
|
ImportLoader::DispatchEventIfFinished(nsINode* aNode)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(!(mReady && mStopped));
|
|
|
|
if (mReady) {
|
|
|
|
DispatchLoadEvent(aNode);
|
|
|
|
}
|
|
|
|
if (mStopped) {
|
|
|
|
DispatchErrorEvent(aNode);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
void
|
|
|
|
ImportLoader::AddBlockedScriptLoader(nsScriptLoader* aScriptLoader)
|
|
|
|
{
|
|
|
|
if (mBlockedScriptLoaders.Contains(aScriptLoader)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
aScriptLoader->AddExecuteBlocker();
|
|
|
|
|
|
|
|
// Let's keep track of the pending script loaders.
|
|
|
|
mBlockedScriptLoaders.AppendElement(aScriptLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImportLoader::RemoveBlockedScriptLoader(nsScriptLoader* aScriptLoader)
|
|
|
|
{
|
|
|
|
aScriptLoader->RemoveExecuteBlocker();
|
|
|
|
return mBlockedScriptLoaders.RemoveElement(aScriptLoader);
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
void
|
|
|
|
ImportLoader::AddLinkElement(nsINode* aNode)
|
|
|
|
{
|
|
|
|
// If a new link element is added to the import tree that
|
|
|
|
// refers to an import that is already finished loading or
|
|
|
|
// stopped trying, we need to fire the corresponding event
|
|
|
|
// on it.
|
2014-10-01 05:13:53 -07:00
|
|
|
mLinks.AppendElement(aNode);
|
|
|
|
mUpdater.UpdateSpanningTree(aNode);
|
2014-05-21 10:08:12 -07:00
|
|
|
DispatchEventIfFinished(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::RemoveLinkElement(nsINode* aNode)
|
|
|
|
{
|
2014-10-01 05:13:53 -07:00
|
|
|
mLinks.RemoveElement(aNode);
|
2014-05-21 10:08:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// Events has to be fired with a script runner, so mImport can
|
|
|
|
// be set on the link element before the load event is fired even
|
|
|
|
// if ImportLoader::Get returns an already loaded import and we
|
|
|
|
// fire the load event immediately on the new referring link element.
|
|
|
|
class AsyncEvent : public nsRunnable {
|
|
|
|
public:
|
|
|
|
AsyncEvent(nsINode* aNode, bool aSuccess)
|
|
|
|
: mNode(aNode)
|
|
|
|
, mSuccess(aSuccess)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() {
|
|
|
|
return nsContentUtils::DispatchTrustedEvent(mNode->OwnerDoc(),
|
|
|
|
mNode,
|
|
|
|
mSuccess ? NS_LITERAL_STRING("load")
|
|
|
|
: NS_LITERAL_STRING("error"),
|
2014-07-16 05:31:59 -07:00
|
|
|
/* aCanBubble = */ false,
|
|
|
|
/* aCancelable = */ false);
|
2014-05-21 10:08:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<nsINode> mNode;
|
|
|
|
bool mSuccess;
|
|
|
|
};
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::DispatchErrorEvent(nsINode* aNode)
|
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(new AsyncEvent(aNode, /* aSuccess = */ false));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::DispatchLoadEvent(nsINode* aNode)
|
|
|
|
{
|
|
|
|
nsContentUtils::AddScriptRunner(new AsyncEvent(aNode, /* aSuccess = */ true));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportLoader::Done()
|
|
|
|
{
|
|
|
|
mReady = true;
|
2014-10-01 05:13:53 -07:00
|
|
|
uint32_t l = mLinks.Length();
|
|
|
|
for (uint32_t i = 0; i < l; i++) {
|
2014-05-21 10:08:12 -07:00
|
|
|
DispatchLoadEvent(mLinks[i]);
|
|
|
|
}
|
|
|
|
UnblockScripts();
|
|
|
|
ReleaseResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-06-30 16:30:13 -07:00
|
|
|
ImportLoader::Error(bool aUnblockScripts)
|
2014-05-21 10:08:12 -07:00
|
|
|
{
|
|
|
|
mDocument = nullptr;
|
|
|
|
mStopped = true;
|
2014-10-01 05:13:53 -07:00
|
|
|
uint32_t l = mLinks.Length();
|
|
|
|
for (uint32_t i = 0; i < l; i++) {
|
2014-05-21 10:08:12 -07:00
|
|
|
DispatchErrorEvent(mLinks[i]);
|
|
|
|
}
|
2014-06-30 16:30:13 -07:00
|
|
|
if (aUnblockScripts) {
|
|
|
|
UnblockScripts();
|
|
|
|
}
|
2014-05-21 10:08:12 -07:00
|
|
|
ReleaseResources();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Release all the resources we don't need after there is no more
|
|
|
|
// data available on the channel, and the parser is done.
|
|
|
|
void ImportLoader::ReleaseResources()
|
|
|
|
{
|
|
|
|
mParserStreamListener = nullptr;
|
|
|
|
mImportParent = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-07-17 01:51:11 -07:00
|
|
|
nsIPrincipal*
|
|
|
|
ImportLoader::Principal()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mImportParent);
|
|
|
|
nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument();
|
|
|
|
nsCOMPtr<nsIScriptObjectPrincipal> sop = do_QueryInterface(master);
|
|
|
|
MOZ_ASSERT(sop);
|
|
|
|
return sop->GetPrincipal();
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
void
|
|
|
|
ImportLoader::Open()
|
|
|
|
{
|
2014-06-30 16:30:13 -07:00
|
|
|
AutoError ae(this, false);
|
2014-05-21 10:08:12 -07:00
|
|
|
// Imports should obey to the master documents CSP.
|
|
|
|
nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument();
|
2014-07-17 01:51:11 -07:00
|
|
|
nsIPrincipal* principal = Principal();
|
2014-07-16 05:31:59 -07:00
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
int16_t shouldLoad = nsIContentPolicy::ACCEPT;
|
2014-07-24 13:44:49 -07:00
|
|
|
nsresult rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_SUBDOCUMENT,
|
2014-05-21 10:08:12 -07:00
|
|
|
mURI,
|
|
|
|
principal,
|
|
|
|
mImportParent,
|
|
|
|
NS_LITERAL_CSTRING("text/html"),
|
|
|
|
/* extra = */ nullptr,
|
|
|
|
&shouldLoad,
|
|
|
|
nsContentUtils::GetContentPolicy(),
|
|
|
|
nsContentUtils::GetSecurityManager());
|
|
|
|
if (NS_FAILED(rv) || NS_CP_REJECTED(shouldLoad)) {
|
|
|
|
NS_WARN_IF_FALSE(NS_CP_ACCEPTED(shouldLoad), "ImportLoader rejected by CSP");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:31:59 -07:00
|
|
|
nsIScriptSecurityManager* secMan = nsContentUtils::GetSecurityManager();
|
|
|
|
rv = secMan->CheckLoadURIWithPrincipal(principal, mURI,
|
|
|
|
nsIScriptSecurityManager::STANDARD);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2014-10-15 03:02:30 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup = master->GetDocumentLoadGroup();
|
2014-07-16 05:31:59 -07:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
|
|
|
rv = NS_NewChannel(getter_AddRefs(channel),
|
2014-05-21 10:08:12 -07:00
|
|
|
mURI,
|
2014-09-21 09:36:50 -07:00
|
|
|
mImportParent,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_SUBDOCUMENT,
|
2014-05-21 10:08:12 -07:00
|
|
|
loadGroup,
|
2014-09-21 09:36:50 -07:00
|
|
|
nullptr, // aCallbacks
|
|
|
|
nsIRequest::LOAD_BACKGROUND);
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2014-07-16 05:31:59 -07:00
|
|
|
// Init CORSListenerProxy and omit credentials.
|
|
|
|
nsRefPtr<nsCORSListenerProxy> corsListener =
|
|
|
|
new nsCORSListenerProxy(this, principal,
|
|
|
|
/* aWithCredentials */ false);
|
|
|
|
rv = corsListener->Init(channel, true);
|
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
|
|
|
rv = channel->AsyncOpen(corsListener, nullptr);
|
2014-07-16 05:31:58 -07:00
|
|
|
NS_ENSURE_SUCCESS_VOID(rv);
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
BlockScripts();
|
|
|
|
ae.Pass();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImportLoader::OnDataAvailable(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsIInputStream* aStream,
|
|
|
|
uint64_t aOffset,
|
|
|
|
uint32_t aCount)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(mParserStreamListener);
|
|
|
|
|
|
|
|
AutoError ae(this);
|
2014-07-16 05:31:59 -07:00
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
|
|
|
|
rv = mParserStreamListener->OnDataAvailable(channel, aContext,
|
|
|
|
aStream, aOffset,
|
|
|
|
aCount);
|
2014-05-21 10:08:12 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
ae.Pass();
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImportLoader::HandleEvent(nsIDOMEvent *aEvent)
|
|
|
|
{
|
|
|
|
#ifdef DEBUG
|
|
|
|
nsAutoString type;
|
|
|
|
aEvent->GetType(type);
|
|
|
|
MOZ_ASSERT(type.EqualsLiteral("DOMContentLoaded"));
|
|
|
|
#endif
|
|
|
|
Done();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImportLoader::OnStopRequest(nsIRequest* aRequest,
|
|
|
|
nsISupports* aContext,
|
|
|
|
nsresult aStatus)
|
|
|
|
{
|
2014-07-01 10:50:47 -07:00
|
|
|
// OnStartRequest throws a special error code to let us know that we
|
|
|
|
// shouldn't do anything else.
|
|
|
|
if (aStatus == NS_ERROR_DOM_ABORT_ERR) {
|
|
|
|
// We failed in OnStartRequest, nothing more to do (we've already
|
|
|
|
// dispatched an error event) just return here.
|
2014-07-16 05:31:59 -07:00
|
|
|
MOZ_ASSERT(mStopped);
|
2014-07-01 10:50:47 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
if (mParserStreamListener) {
|
|
|
|
mParserStreamListener->OnStopRequest(aRequest, aContext, aStatus);
|
|
|
|
}
|
|
|
|
|
2014-07-16 05:31:59 -07:00
|
|
|
if (!mDocument) {
|
|
|
|
// If at this point we don't have a document, then the error was
|
|
|
|
// already reported.
|
|
|
|
return NS_ERROR_DOM_ABORT_ERR;
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
nsCOMPtr<EventTarget> eventTarget = do_QueryInterface(mDocument);
|
|
|
|
EventListenerManager* manager = eventTarget->GetOrCreateListenerManager();
|
|
|
|
manager->AddEventListenerByType(this,
|
|
|
|
NS_LITERAL_STRING("DOMContentLoaded"),
|
|
|
|
TrustedEventsAtSystemGroupBubble());
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
ImportLoader::OnStartRequest(nsIRequest* aRequest, nsISupports* aContext)
|
|
|
|
{
|
|
|
|
AutoError ae(this);
|
2014-07-17 01:51:11 -07:00
|
|
|
nsIPrincipal* principal = Principal();
|
2014-07-16 05:31:59 -07:00
|
|
|
|
|
|
|
nsCOMPtr<nsIChannel> channel = do_QueryInterface(aRequest);
|
|
|
|
if (!channel) {
|
|
|
|
return NS_ERROR_DOM_ABORT_ERR;
|
|
|
|
}
|
2014-07-16 05:32:00 -07:00
|
|
|
|
|
|
|
if (nsContentUtils::IsSystemPrincipal(principal)) {
|
|
|
|
// We should never import non-system documents and run their scripts with system principal!
|
|
|
|
nsCOMPtr<nsIPrincipal> channelPrincipal;
|
2014-08-06 16:05:40 -07:00
|
|
|
nsContentUtils::GetSecurityManager()->GetChannelResultPrincipal(channel,
|
|
|
|
getter_AddRefs(channelPrincipal));
|
2014-07-16 05:32:00 -07:00
|
|
|
if (!nsContentUtils::IsSystemPrincipal(channelPrincipal)) {
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2014-07-16 05:31:59 -07:00
|
|
|
channel->SetOwner(principal);
|
2014-05-21 10:08:12 -07:00
|
|
|
|
|
|
|
nsAutoCString type;
|
2014-07-16 05:31:59 -07:00
|
|
|
channel->GetContentType(type);
|
2014-05-21 10:08:12 -07:00
|
|
|
if (!type.EqualsLiteral("text/html")) {
|
|
|
|
NS_WARNING("ImportLoader wrong content type");
|
2014-07-01 10:50:47 -07:00
|
|
|
return NS_ERROR_DOM_ABORT_ERR;
|
2014-05-21 10:08:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// The scope object is same for all the imports in an import tree,
|
|
|
|
// let's get it form the import parent.
|
|
|
|
nsCOMPtr<nsIGlobalObject> global = mImportParent->GetScopeObject();
|
|
|
|
nsCOMPtr<nsIDOMDocument> importDoc;
|
|
|
|
nsCOMPtr<nsIURI> baseURI = mImportParent->GetBaseURI();
|
|
|
|
const nsAString& emptyStr = EmptyString();
|
|
|
|
nsresult rv = NS_NewDOMDocument(getter_AddRefs(importDoc),
|
|
|
|
emptyStr, emptyStr, nullptr, mURI,
|
|
|
|
baseURI, principal, false, global,
|
|
|
|
DocumentFlavorHTML);
|
2014-07-01 10:50:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
2014-05-21 10:08:12 -07:00
|
|
|
|
|
|
|
// The imported document must know which master document it belongs to.
|
|
|
|
mDocument = do_QueryInterface(importDoc);
|
|
|
|
nsCOMPtr<nsIDocument> master = mImportParent->MasterDocument();
|
|
|
|
mDocument->SetMasterDocument(master);
|
|
|
|
|
2014-07-16 05:31:59 -07:00
|
|
|
// We have to connect the blank document we created with the channel we opened,
|
|
|
|
// and create its own LoadGroup for it.
|
2014-05-21 10:08:12 -07:00
|
|
|
nsCOMPtr<nsIStreamListener> listener;
|
|
|
|
nsCOMPtr<nsILoadGroup> loadGroup;
|
2014-07-16 05:31:59 -07:00
|
|
|
channel->GetLoadGroup(getter_AddRefs(loadGroup));
|
2014-07-16 05:31:59 -07:00
|
|
|
nsCOMPtr<nsILoadGroup> newLoadGroup = do_CreateInstance(NS_LOADGROUP_CONTRACTID);
|
|
|
|
NS_ENSURE_TRUE(newLoadGroup, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
newLoadGroup->SetLoadGroup(loadGroup);
|
|
|
|
rv = mDocument->StartDocumentLoad("import", channel, newLoadGroup,
|
2014-05-21 10:08:12 -07:00
|
|
|
nullptr, getter_AddRefs(listener),
|
|
|
|
true);
|
2014-07-01 10:50:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
2014-05-21 10:08:12 -07:00
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
nsCOMPtr<nsIURI> originalURI;
|
|
|
|
rv = channel->GetOriginalURI(getter_AddRefs(originalURI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> URI;
|
|
|
|
rv = channel->GetURI(getter_AddRefs(URI));
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
MOZ_ASSERT(URI, "URI of a channel should never be null");
|
|
|
|
|
|
|
|
bool equals;
|
|
|
|
rv = URI->Equals(originalURI, &equals);
|
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
|
|
|
|
|
|
|
if (!equals) {
|
|
|
|
// In case of a redirection we must add the new URI to the import map.
|
|
|
|
Manager()->AddLoaderWithNewURI(this, URI);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Let's start the parser.
|
2014-05-21 10:08:12 -07:00
|
|
|
mParserStreamListener = listener;
|
|
|
|
rv = listener->OnStartRequest(aRequest, aContext);
|
2014-07-01 10:50:47 -07:00
|
|
|
NS_ENSURE_SUCCESS(rv, NS_ERROR_DOM_ABORT_ERR);
|
2014-05-21 10:08:12 -07:00
|
|
|
|
|
|
|
ae.Pass();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
// ImportManager
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
NS_IMPL_CYCLE_COLLECTION(ImportManager,
|
|
|
|
mImports)
|
|
|
|
|
|
|
|
NS_INTERFACE_MAP_BEGIN(ImportManager)
|
|
|
|
NS_INTERFACE_MAP_ENTRIES_CYCLE_COLLECTION(ImportManager)
|
|
|
|
NS_INTERFACE_MAP_END
|
|
|
|
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_ADDREF(ImportManager)
|
|
|
|
NS_IMPL_CYCLE_COLLECTING_RELEASE(ImportManager)
|
|
|
|
|
|
|
|
already_AddRefed<ImportLoader>
|
|
|
|
ImportManager::Get(nsIURI* aURI, nsINode* aNode, nsIDocument* aOrigDocument)
|
|
|
|
{
|
|
|
|
// Check if we have a loader for that URI, if not create one,
|
|
|
|
// and start it up.
|
|
|
|
nsRefPtr<ImportLoader> loader;
|
|
|
|
mImports.Get(aURI, getter_AddRefs(loader));
|
2014-10-01 05:13:53 -07:00
|
|
|
bool needToStart = false;
|
2014-05-21 10:08:12 -07:00
|
|
|
if (!loader) {
|
|
|
|
loader = new ImportLoader(aURI, aOrigDocument);
|
|
|
|
mImports.Put(aURI, loader);
|
2014-10-01 05:13:53 -07:00
|
|
|
needToStart = true;
|
2014-09-29 00:26:59 -07:00
|
|
|
}
|
2014-10-01 05:13:53 -07:00
|
|
|
|
2014-09-29 04:17:03 -07:00
|
|
|
MOZ_ASSERT(loader);
|
2014-10-01 05:13:53 -07:00
|
|
|
// Let's keep track of the sub imports links in each document. It will
|
|
|
|
// be used later for scrip execution order calculation. (see UpdateSpanningTree)
|
|
|
|
// NOTE: removing and adding back the link to the tree somewhere else will
|
|
|
|
// NOT have an effect on script execution order.
|
|
|
|
if (!aOrigDocument->HasSubImportLink(aNode)) {
|
|
|
|
aOrigDocument->AddSubImportLink(aNode);
|
|
|
|
}
|
|
|
|
|
|
|
|
loader->AddLinkElement(aNode);
|
|
|
|
|
|
|
|
if (needToStart) {
|
|
|
|
loader->Open();
|
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
return loader.forget();
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
ImportLoader*
|
|
|
|
ImportManager::Find(nsIDocument* aImport)
|
|
|
|
{
|
|
|
|
return mImports.GetWeak(aImport->GetDocumentURIObject());
|
|
|
|
}
|
|
|
|
|
|
|
|
ImportLoader*
|
|
|
|
ImportManager::Find(nsINode* aLink)
|
|
|
|
{
|
|
|
|
HTMLLinkElement* linkElement = static_cast<HTMLLinkElement*>(aLink);
|
|
|
|
nsCOMPtr<nsIURI> uri = linkElement->GetHrefURI();
|
|
|
|
return mImports.GetWeak(uri);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ImportManager::AddLoaderWithNewURI(ImportLoader* aLoader, nsIURI* aNewURI)
|
|
|
|
{
|
|
|
|
mImports.Put(aNewURI, aLoader);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRefPtr<ImportLoader> ImportManager::GetNearestPredecessor(nsINode* aNode)
|
|
|
|
{
|
|
|
|
// Return the previous link if there is any in the same document.
|
|
|
|
nsIDocument* doc = aNode->OwnerDoc();
|
|
|
|
int32_t idx = doc->IndexOfSubImportLink(aNode);
|
|
|
|
MOZ_ASSERT(idx != -1, "aNode must be a sub import link of its owner document");
|
2014-10-06 02:43:57 -07:00
|
|
|
|
|
|
|
for (; idx > 0; idx--) {
|
|
|
|
HTMLLinkElement* link =
|
|
|
|
static_cast<HTMLLinkElement*>(doc->GetSubImportLink(idx - 1));
|
|
|
|
nsCOMPtr<nsIURI> uri = link->GetHrefURI();
|
|
|
|
nsRefPtr<ImportLoader> ret;
|
|
|
|
mImports.Get(uri, getter_AddRefs(ret));
|
|
|
|
// Only main referrer links are interesting.
|
|
|
|
if (ret->GetMainReferrer() == link) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-10-01 05:13:53 -07:00
|
|
|
if (idx == 0) {
|
|
|
|
if (doc->IsMasterDocument()) {
|
|
|
|
// If there is no previous one, and it was the master document, then
|
|
|
|
// there is no predecessor.
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// Else we find the main referrer of the import parent of the link's document.
|
|
|
|
// And do a recursion.
|
|
|
|
ImportLoader* owner = Find(doc);
|
|
|
|
MOZ_ASSERT(owner);
|
|
|
|
nsCOMPtr<nsINode> mainReferrer = owner->GetMainReferrer();
|
|
|
|
return GetNearestPredecessor(mainReferrer);
|
|
|
|
}
|
2014-10-06 02:43:57 -07:00
|
|
|
|
|
|
|
return nullptr;
|
2014-10-01 05:13:53 -07:00
|
|
|
}
|
|
|
|
|
2014-05-21 10:08:12 -07:00
|
|
|
} // namespace dom
|
|
|
|
} // namespace mozilla
|