Bug 1191645 - Use channel->asycnOpen2 in dom/base/nsSyncLoadService.cpp. r=sicking
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Mon, 26 Oct 2015 14:22:59 -0700
changeset 303071 5d5c5ddddff156f2f3f23e3da041ddd091c3dabc
parent 303070 57acb7a0a53f66359887e8315b68b638eb96d273
child 303072 f5d4aa81f62f3959f0dac78c7c382a6be02675cc
push idunknown
push userunknown
push dateunknown
reviewerssicking
bugs1191645
milestone44.0a1
Bug 1191645 - Use channel->asycnOpen2 in dom/base/nsSyncLoadService.cpp. r=sicking
dom/base/nsSyncLoadService.cpp
dom/base/nsSyncLoadService.h
dom/security/nsContentSecurityManager.cpp
dom/xml/nsXMLPrettyPrinter.cpp
dom/xslt/xml/txXMLParser.cpp
dom/xslt/xslt/txMozillaStylesheetCompiler.cpp
--- a/dom/base/nsSyncLoadService.cpp
+++ b/dom/base/nsSyncLoadService.cpp
@@ -18,17 +18,16 @@
 #include "nsIDocument.h"
 #include "nsIDOMDocument.h"
 #include "nsIPrincipal.h"
 #include "nsContentUtils.h" // for kLoadAsData
 #include "nsThreadUtils.h"
 #include "nsNetUtil.h"
 #include "nsAutoPtr.h"
 #include "nsStreamUtils.h"
-#include "nsCORSListenerProxy.h"
 #include <algorithm>
 
 using mozilla::net::ReferrerPolicy;
 
 /**
  * This class manages loading a single XML document
  */
 
@@ -37,17 +36,17 @@ class nsSyncLoader : public nsIStreamLis
                      public nsIInterfaceRequestor,
                      public nsSupportsWeakReference
 {
 public:
     nsSyncLoader() : mLoading(false) {}
 
     NS_DECL_ISUPPORTS
 
-    nsresult LoadDocument(nsIChannel* aChannel, nsIPrincipal *aLoaderPrincipal,
+    nsresult LoadDocument(nsIChannel* aChannel,
                           bool aChannelIsSync, bool aForceToXML,
                           ReferrerPolicy aReferrerPolicy,
                           nsIDOMDocument** aResult);
 
     NS_FORWARD_NSISTREAMLISTENER(mListener->)
     NS_DECL_NSIREQUESTOBSERVER
 
     NS_DECL_NSICHANNELEVENTSINK
@@ -125,39 +124,39 @@ NS_IMPL_ISUPPORTS(nsSyncLoader,
                   nsIStreamListener,
                   nsIRequestObserver,
                   nsIChannelEventSink,
                   nsIInterfaceRequestor,
                   nsISupportsWeakReference)
 
 nsresult
 nsSyncLoader::LoadDocument(nsIChannel* aChannel,
-                           nsIPrincipal *aLoaderPrincipal,
                            bool aChannelIsSync,
                            bool aForceToXML,
                            ReferrerPolicy aReferrerPolicy,
                            nsIDOMDocument **aResult)
 {
+    NS_ENSURE_ARG(aChannel);
     NS_ENSURE_ARG_POINTER(aResult);
     *aResult = nullptr;
     nsresult rv = NS_OK;
 
-    nsCOMPtr<nsIURI> loaderUri;
-    if (aLoaderPrincipal) {
-        aLoaderPrincipal->GetURI(getter_AddRefs(loaderUri));
-    }
-
     mChannel = aChannel;
     nsCOMPtr<nsIHttpChannel> http = do_QueryInterface(mChannel);
     if (http) {
         http->SetRequestHeader(NS_LITERAL_CSTRING("Accept"),
                                NS_LITERAL_CSTRING("text/xml,application/xml,application/xhtml+xml,*/*;q=0.1"),
                                false);
-        if (loaderUri) {
-            http->SetReferrerWithPolicy(loaderUri, aReferrerPolicy);
+        nsCOMPtr<nsILoadInfo> loadInfo = aChannel->GetLoadInfo();
+        if (loadInfo) {
+            nsCOMPtr<nsIURI> loaderUri;
+            loadInfo->TriggeringPrincipal()->GetURI(getter_AddRefs(loaderUri));
+            if (loaderUri) {
+              http->SetReferrerWithPolicy(loaderUri, aReferrerPolicy);
+            }
         }
     }
 
     // Hook us up to listen to redirects and the like.
     // Do this before setting up the cross-site proxy since
     // that installs its own proxies.
     mChannel->SetNotificationCallbacks(this);
 
@@ -181,24 +180,16 @@ nsSyncLoader::LoadDocument(nsIChannel* a
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (aForceToXML) {
         nsCOMPtr<nsIStreamListener> forceListener =
             new nsForceXMLListener(listener);
         listener.swap(forceListener);
     }
 
-    if (aLoaderPrincipal) {
-        RefPtr<nsCORSListenerProxy> corsListener =
-          new nsCORSListenerProxy(listener, aLoaderPrincipal, false);
-        rv = corsListener->Init(mChannel, DataURIHandling::Disallow);
-        NS_ENSURE_SUCCESS(rv, rv);
-        listener = corsListener;
-    }
-
     if (aChannelIsSync) {
         rv = PushSyncStream(listener);
     }
     else {
         rv = PushAsyncStream(listener);
     }
 
     http = do_QueryInterface(mChannel);
@@ -221,17 +212,17 @@ nsSyncLoader::LoadDocument(nsIChannel* a
 nsresult
 nsSyncLoader::PushAsyncStream(nsIStreamListener* aListener)
 {
     mListener = aListener;
 
     mAsyncLoadStatus = NS_OK;
 
     // Start reading from the channel
-    nsresult rv = mChannel->AsyncOpen(this, nullptr);
+    nsresult rv = mChannel->AsyncOpen2(this);
 
     if (NS_SUCCEEDED(rv)) {
         // process events until we're finished.
         mLoading = true;
         nsIThread *thread = NS_GetCurrentThread();
         while (mLoading && NS_SUCCEEDED(rv)) {
             bool processedEvent; 
             rv = thread->ProcessNextEvent(true, &processedEvent);
@@ -249,17 +240,17 @@ nsSyncLoader::PushAsyncStream(nsIStreamL
 
     return mAsyncLoadStatus;
 }
 
 nsresult
 nsSyncLoader::PushSyncStream(nsIStreamListener* aListener)
 {
     nsCOMPtr<nsIInputStream> in;
-    nsresult rv = mChannel->Open(getter_AddRefs(in));
+    nsresult rv = mChannel->Open2(getter_AddRefs(in));
     NS_ENSURE_SUCCESS(rv, rv);
 
     mLoading = true;
     rv = nsSyncLoadService::PushSyncStreamToListener(in, aListener, mChannel);
     mLoading = false;
     
     return rv;
 }
@@ -304,44 +295,45 @@ NS_IMETHODIMP
 nsSyncLoader::GetInterface(const nsIID & aIID,
                            void **aResult)
 {
     return QueryInterface(aIID, aResult);
 }
 
 /* static */
 nsresult
-nsSyncLoadService::LoadDocument(nsIURI *aURI, nsIPrincipal *aLoaderPrincipal,
-                                nsILoadGroup *aLoadGroup, bool aForceToXML,
+nsSyncLoadService::LoadDocument(nsIURI *aURI,
+                                nsContentPolicyType aContentPolicyType,
+                                nsIPrincipal *aLoaderPrincipal,
+                                nsSecurityFlags aSecurityFlags,
+                                nsILoadGroup *aLoadGroup,
+                                bool aForceToXML,
                                 ReferrerPolicy aReferrerPolicy,
                                 nsIDOMDocument** aResult)
 {
     nsCOMPtr<nsIChannel> channel;
     nsresult rv = NS_NewChannel(getter_AddRefs(channel),
                                 aURI,
                                 aLoaderPrincipal,
-                                nsILoadInfo::SEC_NORMAL,
-                                nsIContentPolicy::TYPE_OTHER,
+                                aSecurityFlags,
+                                aContentPolicyType,
                                 aLoadGroup);
     NS_ENSURE_SUCCESS(rv, rv);
 
     if (!aForceToXML) {
         channel->SetContentType(NS_LITERAL_CSTRING("text/xml"));
     }
 
     bool isChrome = false, isResource = false;
-    bool isSync = (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) &&
-                     isChrome) ||
-                    (NS_SUCCEEDED(aURI->SchemeIs("resource", &isResource)) &&
-                     isResource);
-
+    bool isSync =
+      (NS_SUCCEEDED(aURI->SchemeIs("chrome", &isChrome)) && isChrome) ||
+      (NS_SUCCEEDED(aURI->SchemeIs("resource", &isResource)) && isResource);
     RefPtr<nsSyncLoader> loader = new nsSyncLoader();
-    return loader->LoadDocument(channel, aLoaderPrincipal, isSync,
-                                aForceToXML, aReferrerPolicy, aResult);
-
+    return loader->LoadDocument(channel, isSync, aForceToXML,
+                                aReferrerPolicy, aResult);
 }
 
 /* static */
 nsresult
 nsSyncLoadService::PushSyncStreamToListener(nsIInputStream* aIn,
                                             nsIStreamListener* aListener,
                                             nsIChannel* aChannel)
 {
--- a/dom/base/nsSyncLoadService.h
+++ b/dom/base/nsSyncLoadService.h
@@ -24,27 +24,32 @@ class nsIChannel;
 
 class nsSyncLoadService
 {
 public:
     /**
      * Synchronously load the document from the specified URI.
      *
      * @param aURI URI to load the document from.
+     * @param aContentPolicyType contentPolicyType to be set on the channel
      * @param aLoaderPrincipal Principal of loading document. For security
-     *                         checks and referrer header. May be null if no
-     *                         security checks should be done.
+     *                         checks and referrer header.
+     * @param aSecurityFlags securityFlags to be set on the channel
      * @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,
+    static nsresult LoadDocument(nsIURI *aURI,
+                                 nsContentPolicyType aContentPolicyType,
+                                 nsIPrincipal *aLoaderPrincipal,
+                                 nsSecurityFlags aSecurityFlags,
+                                 nsILoadGroup *aLoadGroup,
+                                 bool aForceToXML,
                                  mozilla::net::ReferrerPolicy aReferrerPolicy,
                                  nsIDOMDocument** aResult);
 
     /**
      * Read input stream aIn in chunks and deliver synchronously to aListener.
      *
      * @param aIn The stream to be read.
      * @param aListener The listener that will receive
--- a/dom/security/nsContentSecurityManager.cpp
+++ b/dom/security/nsContentSecurityManager.cpp
@@ -95,17 +95,17 @@ DoSOPChecks(nsIURI* aURI, nsILoadInfo* a
                                         true, // report to console
                                         sameOriginDataInherits);
 }
 
 nsresult
 DoCORSChecks(nsIChannel* aChannel, nsILoadInfo* aLoadInfo,
              nsCOMPtr<nsIStreamListener>& aInAndOutListener)
 {
-  MOZ_ASSERT(aInAndOutListener, "can not perform CORS checks without a listener");
+  MOZ_RELEASE_ASSERT(aInAndOutListener, "can not perform CORS checks without a listener");
   nsIPrincipal* loadingPrincipal = aLoadInfo->LoadingPrincipal();
   RefPtr<nsCORSListenerProxy> corsListener =
     new nsCORSListenerProxy(aInAndOutListener,
                             loadingPrincipal,
                             aLoadInfo->GetRequireCorsWithCredentials());
   // XXX: @arg: DataURIHandling::Allow
   // lets use  DataURIHandling::Allow for now and then decide on callsite basis. see also:
   // http://mxr.mozilla.org/mozilla-central/source/dom/security/nsCORSListenerProxy.h#33
--- a/dom/xml/nsXMLPrettyPrinter.cpp
+++ b/dom/xml/nsXMLPrettyPrinter.cpp
@@ -97,17 +97,19 @@ nsXMLPrettyPrinter::PrettyPrint(nsIDocum
 
     // Load the XSLT
     nsCOMPtr<nsIURI> xslUri;
     rv = NS_NewURI(getter_AddRefs(xslUri),
                    NS_LITERAL_CSTRING("chrome://global/content/xml/XMLPrettyPrint.xsl"));
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIDOMDocument> xslDocument;
-    rv = nsSyncLoadService::LoadDocument(xslUri, nsContentUtils::GetSystemPrincipal(),
+    rv = nsSyncLoadService::LoadDocument(xslUri, nsIContentPolicy::TYPE_XSLT,
+                                         nsContentUtils::GetSystemPrincipal(),
+                                         nsILoadInfo::SEC_ALLOW_CROSS_ORIGIN_DATA_IS_NULL,
                                          nullptr, true, mozilla::net::RP_Default,
                                          getter_AddRefs(xslDocument));
     NS_ENSURE_SUCCESS(rv, rv);
 
     // Transform the document
     nsCOMPtr<nsIXSLTProcessor> transformer =
         do_CreateInstance("@mozilla.org/document-transformer;1?type=xslt", &rv);
     NS_ENSURE_SUCCESS(rv, rv);
--- a/dom/xslt/xml/txXMLParser.cpp
+++ b/dom/xslt/xml/txXMLParser.cpp
@@ -10,18 +10,20 @@
 #include "nsIDocument.h"
 #include "nsIDOMDocument.h"
 #include "nsSyncLoadService.h"
 #include "nsNetUtil.h"
 #include "nsIURI.h"
 #include "nsIPrincipal.h"
 
 nsresult
-txParseDocumentFromURI(const nsAString& aHref, const txXPathNode& aLoader,
-                       nsAString& aErrMsg, txXPathNode** aResult)
+txParseDocumentFromURI(const nsAString& aHref,
+                       const txXPathNode& aLoader,
+                       nsAString& aErrMsg,
+                       txXPathNode** aResult)
 {
     NS_ENSURE_ARG_POINTER(aResult);
     *aResult = nullptr;
     nsCOMPtr<nsIURI> documentURI;
     nsresult rv = NS_NewURI(getter_AddRefs(documentURI), aHref);
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsIDocument* loaderDocument = txXPathNativeNode::getDocument(aLoader);
@@ -31,17 +33,19 @@ txParseDocumentFromURI(const nsAString& 
     // 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,
+                                         nsIContentPolicy::TYPE_INTERNAL_XMLHTTPREQUEST,
                                          loaderDocument->NodePrincipal(),
+                                         nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
                                          loadGroup, true,
                                          loaderDocument->GetReferrerPolicy(),
                                          &theDocument);
 
     if (NS_FAILED(rv)) {
         aErrMsg.AppendLiteral("Document load of ");
         aErrMsg.Append(aHref);
         aErrMsg.AppendLiteral(" failed.");
--- a/dom/xslt/xslt/txMozillaStylesheetCompiler.cpp
+++ b/dom/xslt/xslt/txMozillaStylesheetCompiler.cpp
@@ -624,41 +624,31 @@ txSyncCompileObserver::loadURI(const nsA
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIPrincipal> referrerPrincipal;
     rv = nsContentUtils::GetSecurityManager()->
       GetSimpleCodebasePrincipal(referrerUri,
                                  getter_AddRefs(referrerPrincipal));
     NS_ENSURE_SUCCESS(rv, rv);
 
-    // Content Policy
-    int16_t shouldLoad = nsIContentPolicy::ACCEPT;
-    rv = NS_CheckContentLoadPolicy(nsIContentPolicy::TYPE_INTERNAL_STYLESHEET,
-                                   uri,
-                                   referrerPrincipal,
-                                   nullptr,
-                                   NS_LITERAL_CSTRING("application/xml"),
-                                   nullptr,
-                                   &shouldLoad);
-    NS_ENSURE_SUCCESS(rv, rv);
-    if (NS_CP_REJECTED(shouldLoad)) {
-        return NS_ERROR_DOM_BAD_URI;
-    }
-
     // This is probably called by js, a loadGroup for the channel doesn't
     // make sense.
     nsCOMPtr<nsINode> source;
     if (mProcessor) {
       source =
         do_QueryInterface(mProcessor->GetSourceContentModel());
     }
     nsAutoSyncOperation sync(source ? source->OwnerDoc() : nullptr);
     nsCOMPtr<nsIDOMDocument> document;
-    rv = nsSyncLoadService::LoadDocument(uri, referrerPrincipal, nullptr,
-                                         false, aReferrerPolicy,
+
+    rv = nsSyncLoadService::LoadDocument(uri, nsIContentPolicy::TYPE_XSLT,
+                                         referrerPrincipal,
+                                         nsILoadInfo::SEC_REQUIRE_CORS_DATA_INHERITS,
+                                         nullptr, false,
+                                         aReferrerPolicy,
                                          getter_AddRefs(document));
     NS_ENSURE_SUCCESS(rv, rv);
 
     nsCOMPtr<nsIDocument> doc = do_QueryInterface(document);
     rv = handleNode(doc, aCompiler);
     if (NS_FAILED(rv)) {
         nsAutoCString spec;
         uri->GetSpec(spec);