Bug 704320 - Referrer policy support in nsSyncLoadService. (r=peterv)

This commit is contained in:
Owen Chu 2014-11-18 08:47:20 -05:00
parent 63f8cd4e13
commit 97f0aa29c2
9 changed files with 69 additions and 27 deletions

View File

@ -26,6 +26,8 @@
#include "nsCrossSiteListenerProxy.h"
#include <algorithm>
using mozilla::net::ReferrerPolicy;
/**
* This class manages loading a single XML document
*/
@ -42,6 +44,7 @@ public:
nsresult LoadDocument(nsIChannel* aChannel, nsIPrincipal *aLoaderPrincipal,
bool aChannelIsSync, bool aForceToXML,
ReferrerPolicy aReferrerPolicy,
nsIDOMDocument** aResult);
NS_FORWARD_NSISTREAMLISTENER(mListener->)
@ -130,6 +133,7 @@ nsSyncLoader::LoadDocument(nsIChannel* aChannel,
nsIPrincipal *aLoaderPrincipal,
bool aChannelIsSync,
bool aForceToXML,
ReferrerPolicy aReferrerPolicy,
nsIDOMDocument **aResult)
{
NS_ENSURE_ARG_POINTER(aResult);
@ -144,11 +148,11 @@ nsSyncLoader::LoadDocument(nsIChannel* aChannel,
mChannel = aChannel;
nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(mChannel);
if (http) {
http->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
http->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
NS_LITERAL_CSTRING("text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"),
false);
if (loaderUri) {
http->SetReferrer(loaderUri);
http->SetReferrerWithPolicy(loaderUri, aReferrerPolicy);
}
}
@ -307,6 +311,7 @@ nsSyncLoader::GetInterface(const nsIID & aIID,
nsresult
nsSyncLoadService::LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal,
nsILoadGroup *aLoadGroup, bool aForceToXML,
ReferrerPolicy aReferrerPolicy,
nsIDOMDocument** aResult)
{
nsCOMPtr<nsIChannel> channel;
@ -330,7 +335,7 @@ nsSyncLoadService::LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal,
nsRefPtr<nsSyncLoader> loader = new nsSyncLoader();
return loader->LoadDocument(channel, aLoaderPrincipal, isSync,
aForceToXML, aResult);
aForceToXML, aReferrerPolicy, aResult);
}

View File

@ -11,6 +11,7 @@
#define nsSyncLoadService_h__
#include "nscore.h"
#include "mozilla/net/ReferrerPolicy.h"
class nsIInputStream;
class nsILoadGroup;
@ -33,10 +34,12 @@ public:
* @param aLoadGroup The loadgroup to use for loading the document.
* @param aForceToXML Whether to parse the document as XML, regardless of
* content type.
* @param referrerPolicy Referrer policy.
* @param aResult [out] The document loaded from the URI.
*/
static nsresult LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal,
nsILoadGroup *aLoadGroup, bool aForceToXML,
mozilla::net::ReferrerPolicy aReferrerPolicy,
nsIDOMDocument** aResult);
/**

View File

@ -102,7 +102,7 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocument* aDocument,
nsCOMPtr<nsIDOMDocument> xslDocument;
rv = nsSyncLoadService::LoadDocument(xslUri, nsContentUtils::GetSystemPrincipal(),
nullptr, true,
nullptr, true, mozilla::net::RP_Default,
getter_AddRefs(xslDocument));
NS_ENSURE_SUCCESS(rv, rv);

View File

@ -36,7 +36,9 @@ txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader,
nsAutoSyncOperation sync(loaderDocument);
rv = nsSyncLoadService::LoadDocument(documentURI,
loaderDocument->NodePrincipal(),
loadGroup, true, &theDocument);
loadGroup, true,
loaderDocument->GetReferrerPolicy(),
&theDocument);
if (NS_FAILED(rv)) {
aErrMsg.AppendLiteral("Document load of ");

View File

@ -42,6 +42,7 @@
using namespace mozilla;
using mozilla::dom::EncodingUtils;
using mozilla::net::ReferrerPolicy;
static NS_DEFINE_CID(kCParserCID, NS_PARSER_CID);
@ -374,7 +375,8 @@ public:
NS_INLINE_DECL_REFCOUNTING(txCompileObserver)
nsresult startLoad(nsIURI* aUri, txStylesheetCompiler* aCompiler,
nsIPrincipal* aSourcePrincipal);
nsIPrincipal* aSourcePrincipal,
ReferrerPolicy aReferrerPolicy);
private:
nsRefPtr<txMozillaXSLTProcessor> mProcessor;
@ -399,6 +401,7 @@ txCompileObserver::txCompileObserver(txMozillaXSLTProcessor* aProcessor,
nsresult
txCompileObserver::loadURI(const nsAString& aUri,
const nsAString& aReferrerUri,
ReferrerPolicy aReferrerPolicy,
txStylesheetCompiler* aCompiler)
{
if (mProcessor->IsLoadDisabled()) {
@ -433,7 +436,7 @@ txCompileObserver::loadURI(const nsAString& aUri,
return NS_ERROR_DOM_BAD_URI;
}
return startLoad(uri, aCompiler, referrerPrincipal);
return startLoad(uri, aCompiler, referrerPrincipal, aReferrerPolicy);
}
void
@ -452,7 +455,8 @@ txCompileObserver::onDoneCompiling(txStylesheetCompiler* aCompiler,
nsresult
txCompileObserver::startLoad(nsIURI* aUri, txStylesheetCompiler* aCompiler,
nsIPrincipal* aReferrerPrincipal)
nsIPrincipal* aReferrerPrincipal,
ReferrerPolicy aReferrerPolicy)
{
nsCOMPtr<nsILoadGroup> loadGroup = mLoaderDocument->GetDocumentLoadGroup();
if (!loadGroup) {
@ -482,7 +486,7 @@ txCompileObserver::startLoad(nsIURI* aUri, txStylesheetCompiler* aCompiler,
nsCOMPtr<nsIURI> referrerURI;
aReferrerPrincipal->GetURI(getter_AddRefs(referrerURI));
if (referrerURI) {
httpChannel->SetReferrer(referrerURI);
httpChannel->SetReferrerWithPolicy(referrerURI, aReferrerPolicy);
}
}
@ -509,7 +513,7 @@ txCompileObserver::startLoad(nsIURI* aUri, txStylesheetCompiler* aCompiler,
nsresult
TX_LoadSheet(nsIURI* aUri, txMozillaXSLTProcessor* aProcessor,
nsIDocument* aLoaderDocument)
nsIDocument* aLoaderDocument, ReferrerPolicy aReferrerPolicy)
{
nsIPrincipal* principal = aLoaderDocument->NodePrincipal();
@ -537,10 +541,11 @@ TX_LoadSheet(nsIURI* aUri, txMozillaXSLTProcessor* aProcessor,
NS_ENSURE_TRUE(observer, NS_ERROR_OUT_OF_MEMORY);
nsRefPtr<txStylesheetCompiler> compiler =
new txStylesheetCompiler(NS_ConvertUTF8toUTF16(spec), observer);
new txStylesheetCompiler(NS_ConvertUTF8toUTF16(spec), aReferrerPolicy,
observer);
NS_ENSURE_TRUE(compiler, NS_ERROR_OUT_OF_MEMORY);
return observer->startLoad(aUri, compiler, principal);
return observer->startLoad(aUri, compiler, principal, aReferrerPolicy);
}
/**
@ -638,6 +643,7 @@ txSyncCompileObserver::txSyncCompileObserver(txMozillaXSLTProcessor* aProcessor)
nsresult
txSyncCompileObserver::loadURI(const nsAString& aUri,
const nsAString& aReferrerUri,
ReferrerPolicy aReferrerPolicy,
txStylesheetCompiler* aCompiler)
{
if (mProcessor->IsLoadDisabled()) {
@ -682,7 +688,8 @@ txSyncCompileObserver::loadURI(const nsAString& aUri,
nsAutoSyncOperation sync(source ? source->OwnerDoc() : nullptr);
nsCOMPtr<nsIDOMDocument> document;
rv = nsSyncLoadService::LoadDocument(uri, referrerPrincipal, nullptr,
false, getter_AddRefs(document));
false, aReferrerPolicy,
getter_AddRefs(document));
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
@ -742,7 +749,7 @@ TX_CompileStylesheet(nsINode* aNode, txMozillaXSLTProcessor* aProcessor,
NS_ENSURE_TRUE(obs, NS_ERROR_OUT_OF_MEMORY);
nsRefPtr<txStylesheetCompiler> compiler =
new txStylesheetCompiler(stylesheetURI, obs);
new txStylesheetCompiler(stylesheetURI, doc->GetReferrerPolicy(), obs);
NS_ENSURE_TRUE(compiler, NS_ERROR_OUT_OF_MEMORY);
compiler->setBaseURI(baseURI);

View File

@ -1040,7 +1040,12 @@ NS_IMETHODIMP
txMozillaXSLTProcessor::LoadStyleSheet(nsIURI* aUri,
nsIDocument* aLoaderDocument)
{
nsresult rv = TX_LoadSheet(aUri, this, aLoaderDocument);
mozilla::net::ReferrerPolicy refpol = mozilla::net::RP_Default;
if (mStylesheetDocument) {
refpol = mStylesheetDocument->GetReferrerPolicy();
}
nsresult rv = TX_LoadSheet(aUri, this, aLoaderDocument, refpol);
if (NS_FAILED(rv) && mObserver) {
// This is most likely a network or security error, just
// use the uri as context.

View File

@ -17,6 +17,7 @@
#include "nsWrapperCache.h"
#include "mozilla/Attributes.h"
#include "mozilla/ErrorResult.h"
#include "mozilla/net/ReferrerPolicy.h"
class nsINode;
class nsIDOMNode;
@ -185,7 +186,8 @@ private:
};
extern nsresult TX_LoadSheet(nsIURI* aUri, txMozillaXSLTProcessor* aProcessor,
nsIDocument* aLoaderDocument);
nsIDocument* aLoaderDocument,
mozilla::net::ReferrerPolicy aReferrerPolicy);
extern nsresult TX_CompileStylesheet(nsINode* aNode,
txMozillaXSLTProcessor* aProcessor,

View File

@ -24,21 +24,25 @@
#include "nsTArray.h"
using namespace mozilla;
using mozilla::net::ReferrerPolicy;
txStylesheetCompiler::txStylesheetCompiler(const nsAString& aStylesheetURI,
ReferrerPolicy aReferrerPolicy,
txACompileObserver* aObserver)
: txStylesheetCompilerState(aObserver)
{
mStatus = init(aStylesheetURI, nullptr, nullptr);
mStatus = init(aStylesheetURI, aReferrerPolicy, nullptr, nullptr);
}
txStylesheetCompiler::txStylesheetCompiler(const nsAString& aStylesheetURI,
txStylesheet* aStylesheet,
txListIterator* aInsertPosition,
ReferrerPolicy aReferrerPolicy,
txACompileObserver* aObserver)
: txStylesheetCompilerState(aObserver)
{
mStatus = init(aStylesheetURI, aStylesheet, aInsertPosition);
mStatus = init(aStylesheetURI, aReferrerPolicy, aStylesheet,
aInsertPosition);
}
void
@ -415,6 +419,7 @@ txStylesheetCompiler::getStylesheet()
nsresult
txStylesheetCompiler::loadURI(const nsAString& aUri,
const nsAString& aReferrerUri,
ReferrerPolicy aReferrerPolicy,
txStylesheetCompiler* aCompiler)
{
PR_LOG(txLog::xslt, PR_LOG_ALWAYS,
@ -424,8 +429,9 @@ txStylesheetCompiler::loadURI(const nsAString& aUri,
if (mStylesheetURI.Equals(aUri)) {
return NS_ERROR_XSLT_LOAD_RECURSION;
}
return mObserver ? mObserver->loadURI(aUri, aReferrerUri, aCompiler) :
NS_ERROR_FAILURE;
return mObserver ?
mObserver->loadURI(aUri, aReferrerUri, aReferrerPolicy, aCompiler) :
NS_ERROR_FAILURE;
}
void
@ -536,12 +542,14 @@ txStylesheetCompilerState::txStylesheetCompilerState(txACompileObserver* aObserv
nsresult
txStylesheetCompilerState::init(const nsAString& aStylesheetURI,
ReferrerPolicy aReferrerPolicy,
txStylesheet* aStylesheet,
txListIterator* aInsertPosition)
{
NS_ASSERTION(!aStylesheet || aInsertPosition,
"must provide insertposition if loading subsheet");
mStylesheetURI = aStylesheetURI;
mReferrerPolicy = aReferrerPolicy;
// Check for fragment identifier of an embedded stylesheet.
int32_t fragment = aStylesheetURI.FindChar('#') + 1;
if (fragment > 0) {
@ -765,7 +773,7 @@ txStylesheetCompilerState::loadIncludedStylesheet(const nsAString& aURI)
nsRefPtr<txStylesheetCompiler> compiler =
new txStylesheetCompiler(aURI, mStylesheet, &mToplevelIterator,
observer);
mReferrerPolicy, observer);
NS_ENSURE_TRUE(compiler, NS_ERROR_OUT_OF_MEMORY);
// step forward before calling the observer in case of syncronous loading
@ -775,7 +783,7 @@ txStylesheetCompilerState::loadIncludedStylesheet(const nsAString& aURI)
return NS_ERROR_OUT_OF_MEMORY;
}
rv = mObserver->loadURI(aURI, mStylesheetURI, compiler);
rv = mObserver->loadURI(aURI, mStylesheetURI, mReferrerPolicy, compiler);
if (NS_FAILED(rv)) {
mChildCompilerList.RemoveElement(compiler);
}
@ -801,14 +809,16 @@ txStylesheetCompilerState::loadImportedStylesheet(const nsAString& aURI,
txACompileObserver* observer = static_cast<txStylesheetCompiler*>(this);
nsRefPtr<txStylesheetCompiler> compiler =
new txStylesheetCompiler(aURI, mStylesheet, &iter, observer);
new txStylesheetCompiler(aURI, mStylesheet, &iter, mReferrerPolicy,
observer);
NS_ENSURE_TRUE(compiler, NS_ERROR_OUT_OF_MEMORY);
if (mChildCompilerList.AppendElement(compiler) == nullptr) {
return NS_ERROR_OUT_OF_MEMORY;
}
nsresult rv = mObserver->loadURI(aURI, mStylesheetURI, compiler);
nsresult rv = mObserver->loadURI(aURI, mStylesheetURI, mReferrerPolicy,
compiler);
if (NS_FAILED(rv)) {
mChildCompilerList.RemoveElement(compiler);
}
@ -1070,7 +1080,8 @@ extern bool
TX_XSLTFunctionAvailable(nsIAtom* aName, int32_t aNameSpaceID)
{
nsRefPtr<txStylesheetCompiler> compiler =
new txStylesheetCompiler(EmptyString(), nullptr);
new txStylesheetCompiler(EmptyString(),
mozilla::net::RP_Default, nullptr);
NS_ENSURE_TRUE(compiler, false);
nsAutoPtr<FunctionCall> fnCall;

View File

@ -14,6 +14,7 @@
#include "nsAutoPtr.h"
#include "txStylesheet.h"
#include "nsTArray.h"
#include "mozilla/net/ReferrerPolicy.h"
extern bool
TX_XSLTFunctionAvailable(nsIAtom* aName, int32_t aNameSpaceID);
@ -50,6 +51,7 @@ public:
virtual nsresult loadURI(const nsAString& aUri,
const nsAString& aReferrerUri,
mozilla::net::ReferrerPolicy aReferrerPolicy,
txStylesheetCompiler* aCompiler) = 0;
virtual void onDoneCompiling(txStylesheetCompiler* aCompiler,
nsresult aResult,
@ -59,6 +61,7 @@ public:
#define TX_DECL_ACOMPILEOBSERVER \
nsresult loadURI(const nsAString& aUri, const nsAString& aReferrerUri, \
mozilla::net::ReferrerPolicy aReferrerPolicy, \
txStylesheetCompiler* aCompiler); \
void onDoneCompiling(txStylesheetCompiler* aCompiler, nsresult aResult, \
const char16_t *aErrorText = nullptr, \
@ -70,8 +73,9 @@ public:
explicit txStylesheetCompilerState(txACompileObserver* aObserver);
~txStylesheetCompilerState();
nsresult init(const nsAString& aStylesheetURI, txStylesheet* aStylesheet,
txListIterator* aInsertPosition);
nsresult init(const nsAString& aStylesheetURI,
mozilla::net::ReferrerPolicy aReferrerPolicy,
txStylesheet* aStylesheet, txListIterator* aInsertPosition);
// Embedded stylesheets state
bool handleEmbeddedSheet()
@ -171,6 +175,7 @@ private:
txInstruction** mNextInstrPtr;
txListIterator mToplevelIterator;
nsTArray<txInstruction**> mGotoTargetPointers;
mozilla::net::ReferrerPolicy mReferrerPolicy;
};
struct txStylesheetAttr
@ -189,10 +194,12 @@ public:
friend bool TX_XSLTFunctionAvailable(nsIAtom* aName,
int32_t aNameSpaceID);
txStylesheetCompiler(const nsAString& aStylesheetURI,
mozilla::net::ReferrerPolicy aReferrerPolicy,
txACompileObserver* aObserver);
txStylesheetCompiler(const nsAString& aStylesheetURI,
txStylesheet* aStylesheet,
txListIterator* aInsertPosition,
mozilla::net::ReferrerPolicy aReferrerPolicy,
txACompileObserver* aObserver);
void setBaseURI(const nsString& aBaseURI);