author Owen Chu <>
Tue, 18 Nov 2014 08:47:20 -0500
changeset 228125 52dfc1a43464bc0bd150b8ef50a2620e4c75bace
parent 184348 eb77a9a1ee9b00f4b85f20bca52c9d0f50739b4a
child 276135 91c69276782a255aaaff1e27c3fe339b63134472
permissions -rw-r--r--
Bug 704320 - Referrer policy support in nsSyncLoadService. (r=peterv)

/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* 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 */

#include "txXMLParser.h"
#include "txURIUtils.h"
#include "txXPathTreeWalker.h"

#include "nsIDocument.h"
#include "nsIDOMDocument.h"
#include "nsSyncLoadService.h"
#include "nsNetUtil.h"
#include "nsIPrincipal.h"

txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader,
                       nsAString& aErrMsg, txXPathNode** aResult)
    *aResult = nullptr;
    nsCOMPtr<nsIURI> documentURI;
    nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref);
    NS_ENSURE_SUCCESS(rv, rv);

    nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader);

    nsCOMPtr<nsILoadGroup> loadGroup = loaderDocument->GetDocumentLoadGroup();

    // For the system principal loaderUri will be null here, which is good
    // since that means that chrome documents can load any uri.

    // Raw pointer, we want the resulting txXPathNode to hold a reference to
    // the document.
    nsIDOMDocument* theDocument = nullptr;
    nsAutoSyncOperation sync(loaderDocument);
    rv = nsSyncLoadService::LoadDocument(documentURI,
                                         loadGroup, true,

    if (NS_FAILED(rv)) {
        aErrMsg.AppendLiteral("Document load of ");
        aErrMsg.AppendLiteral(" failed.");
        return NS_FAILED(rv) ? rv : NS_ERROR_FAILURE;

    *aResult = txXPathNativeNode::createXPathNode(theDocument);
    if (!*aResult) {
        return NS_ERROR_FAILURE;

    return NS_OK;