Bug 1063197 - Storing LoadInfo on all channels created through NS_NewInputStreamChannel - API changes (r=mcmanus)
authorChristoph Kerschbaumer <mozilla@christophkerschbaumer.com>
Thu, 16 Oct 2014 11:14:05 -0700
changeset 210803 7e905781c7152f82c6e1c6c52e12133278d1720f
parent 210802 63241287b1741a9fa2913b41740b8e5321f8f007
child 210804 a87d8dff37cabaa195c6aebfdabb96fdbd88af64
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersmcmanus
bugs1063197
milestone36.0a1
Bug 1063197 - Storing LoadInfo on all channels created through NS_NewInputStreamChannel - API changes (r=mcmanus)
netwerk/base/public/nsNetUtil.h
--- a/netwerk/base/public/nsNetUtil.h
+++ b/netwerk/base/public/nsNetUtil.h
@@ -592,111 +592,155 @@ NS_GetRealPort(nsIURI* aURI)
     rv = aURI->GetScheme(scheme);
     if (NS_FAILED(rv))
         return -1;
 
     return NS_GetDefaultPort(scheme.get());
 }
 
 inline nsresult
-NS_NewInputStreamChannel(nsIChannel      **result,
-                         nsIURI           *uri,
-                         nsIInputStream   *stream,
-                         const nsACString &contentType,
-                         const nsACString *contentCharset)
+NS_NewInputStreamChannelInternal(nsIChannel**        outChannel,
+                                 nsIURI*             aUri,
+                                 nsIInputStream*     aStream,
+                                 const nsACString&   aContentType,
+                                 const nsACString&   aContentCharset,
+                                 nsINode*            aRequestingNode,
+                                 nsIPrincipal*       aRequestingPrincipal,
+                                 nsSecurityFlags     aSecurityFlags,
+                                 nsContentPolicyType aContentPolicyType)
 {
-    nsresult rv;
-    nsCOMPtr<nsIInputStreamChannel> isc =
-        do_CreateInstance(NS_INPUTSTREAMCHANNEL_CONTRACTID, &rv);
-    if (NS_FAILED(rv))
-        return rv;
-    rv = isc->SetURI(uri);
-    nsresult tmp = isc->SetContentStream(stream);
-    if (NS_FAILED(tmp)) {
-        rv = tmp;
-    }
-    if (NS_FAILED(rv))
-        return rv;
-    nsCOMPtr<nsIChannel> chan = do_QueryInterface(isc, &rv);
-    if (NS_FAILED(rv))
-        return rv;
-    if (!contentType.IsEmpty())
-        rv = chan->SetContentType(contentType);
-    if (contentCharset && !contentCharset->IsEmpty()) {
-        tmp = chan->SetContentCharset(*contentCharset);
-        if (NS_FAILED(tmp)) {
-            rv = tmp;
-        }
-    }
-    if (NS_SUCCEEDED(rv)) {
-        *result = nullptr;
-        chan.swap(*result);
-    }
-    return rv;
+  nsresult rv;
+  nsCOMPtr<nsIInputStreamChannel> isc =
+    do_CreateInstance(NS_INPUTSTREAMCHANNEL_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = isc->SetURI(aUri);
+  NS_ENSURE_SUCCESS(rv, rv);
+  rv = isc->SetContentStream(aStream);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  nsCOMPtr<nsIChannel> channel = do_QueryInterface(isc, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (!aContentType.IsEmpty()) {
+    rv = channel->SetContentType(aContentType);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+  if (!aContentCharset.IsEmpty()) {
+    rv = channel->SetContentCharset(aContentCharset);
+    NS_ENSURE_SUCCESS(rv, rv);
+  }
+
+  nsCOMPtr<nsILoadInfo> loadInfo =
+    new mozilla::LoadInfo(aRequestingPrincipal,
+                          aRequestingNode,
+                          aSecurityFlags,
+                          aContentPolicyType);
+  if (!loadInfo) {
+    return NS_ERROR_UNEXPECTED;
+  }
+  channel->SetLoadInfo(loadInfo);
+
+  // If we're sandboxed, make sure to clear any owner the channel
+  // might already have.
+  if (loadInfo->GetLoadingSandboxed()) {
+    channel->SetOwner(nullptr);
+  }
+
+  channel.forget(outChannel);
+  return NS_OK;
 }
 
-inline nsresult
-NS_NewInputStreamChannel(nsIChannel      **result,
-                         nsIURI           *uri,
-                         nsIInputStream   *stream,
-                         const nsACString &contentType    = EmptyCString())
+inline nsresult /* NS_NewInputStreamChannelPrincipal */
+NS_NewInputStreamChannel(nsIChannel**        outChannel,
+                         nsIURI*             aUri,
+                         nsIInputStream*     aStream,
+                         nsIPrincipal*       aRequestingPrincipal,
+                         nsSecurityFlags     aSecurityFlags,
+                         nsContentPolicyType aContentPolicyType,
+                         const nsACString&   aContentType    = EmptyCString(),
+                         const nsACString&   aContentCharset = EmptyCString())
 {
-    return NS_NewInputStreamChannel(result, uri, stream, contentType, nullptr);
+  return NS_NewInputStreamChannelInternal(outChannel,
+                                          aUri,
+                                          aStream,
+                                          aContentType,
+                                          aContentCharset,
+                                          nullptr, // aRequestingNode
+                                          aRequestingPrincipal,
+                                          aSecurityFlags,
+                                          aContentPolicyType);
 }
 
 inline nsresult
-NS_NewInputStreamChannel(nsIChannel      **result,
-                         nsIURI           *uri,
-                         nsIInputStream   *stream,
-                         const nsACString &contentType,
-                         const nsACString &contentCharset)
+NS_NewInputStreamChannelInternal(nsIChannel**        outChannel,
+                                 nsIURI*             aUri,
+                                 const nsAString&    aData,
+                                 const nsACString&   aContentType,
+                                 nsINode*            aRequestingNode,
+                                 nsIPrincipal*       aRequestingPrincipal,
+                                 nsSecurityFlags     aSecurityFlags,
+                                 nsContentPolicyType aContentPolicyType,
+                                 bool                aIsSrcdocChannel = false)
 {
-    return NS_NewInputStreamChannel(result, uri, stream, contentType,
-                                    &contentCharset);
+  nsresult rv;
+  nsCOMPtr<nsIStringInputStream> stream;
+  stream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
+  NS_ENSURE_SUCCESS(rv, rv);
+
+#ifdef MOZILLA_INTERNAL_API
+    uint32_t len;
+    char* utf8Bytes = ToNewUTF8String(aData, &len);
+    rv = stream->AdoptData(utf8Bytes, len);
+#else
+    char* utf8Bytes = ToNewUTF8String(aData);
+    rv = stream->AdoptData(utf8Bytes, strlen(utf8Bytes));
+#endif
+
+  nsCOMPtr<nsIChannel> channel;
+  rv = NS_NewInputStreamChannelInternal(getter_AddRefs(channel),
+                                        aUri,
+                                        stream,
+                                        aContentType,
+                                        NS_LITERAL_CSTRING("UTF-8"),
+                                        aRequestingNode,
+                                        aRequestingPrincipal,
+                                        aSecurityFlags,
+                                        aContentPolicyType);
+
+  NS_ENSURE_SUCCESS(rv, rv);
+
+  if (aIsSrcdocChannel) {
+    nsCOMPtr<nsIInputStreamChannel> inStrmChan = do_QueryInterface(channel);
+    NS_ENSURE_TRUE(inStrmChan, NS_ERROR_FAILURE);
+    inStrmChan->SetSrcdocData(aData);
+  }
+  channel.forget(outChannel);
+  return NS_OK;
 }
 
 inline nsresult
-NS_NewInputStreamChannel(nsIChannel      **result,
-                         nsIURI           *uri,
-                         const nsAString  &data,
-                         const nsACString &contentType,
-                         bool              isSrcdocChannel = false)
+NS_NewInputStreamChannel(nsIChannel**        outChannel,
+                         nsIURI*             aUri,
+                         const nsAString&    aData,
+                         const nsACString&   aContentType,
+                         nsIPrincipal*       aRequestingPrincipal,
+                         nsSecurityFlags     aSecurityFlags,
+                         nsContentPolicyType aContentPolicyType,
+                         bool                aIsSrcdocChannel = false)
 {
-
-    nsresult rv;
-
-    nsCOMPtr<nsIStringInputStream> stream;
-    stream = do_CreateInstance(NS_STRINGINPUTSTREAM_CONTRACTID, &rv);
-    NS_ENSURE_SUCCESS(rv, rv);
-
-#ifdef MOZILLA_INTERNAL_API
-    uint32_t len;
-    char* utf8Bytes = ToNewUTF8String(data, &len);
-    rv = stream->AdoptData(utf8Bytes, len);
-#else
-    char* utf8Bytes = ToNewUTF8String(data);
-    rv = stream->AdoptData(utf8Bytes, strlen(utf8Bytes));
-#endif
-
-    nsCOMPtr<nsIChannel> chan;
-
-    rv = NS_NewInputStreamChannel(getter_AddRefs(chan), uri, stream,
-                                  contentType, NS_LITERAL_CSTRING("UTF-8"));
-    NS_ENSURE_SUCCESS(rv, rv);
-
-    if (isSrcdocChannel) {
-        nsCOMPtr<nsIInputStreamChannel> inStrmChan = do_QueryInterface(chan);
-        NS_ENSURE_TRUE(inStrmChan, NS_ERROR_FAILURE);
-        inStrmChan->SetSrcdocData(data);
-    }
-
-    *result = nullptr;
-    chan.swap(*result);
-
-    return NS_OK;
+  return NS_NewInputStreamChannelInternal(outChannel,
+                                          aUri,
+                                          aData,
+                                          aContentType,
+                                          nullptr, // aRequestingNode
+                                          aRequestingPrincipal,
+                                          aSecurityFlags,
+                                          aContentPolicyType,
+                                          aIsSrcdocChannel);
 }
 
 inline nsresult
 NS_NewInputStreamPump(nsIInputStreamPump **result,
                       nsIInputStream      *stream,
                       int64_t              streamPos = int64_t(-1),
                       int64_t              streamLen = int64_t(-1),
                       uint32_t             segsize = 0,