From 71fde070bb57bef7ffa814c692a553b10bded753 Mon Sep 17 00:00:00 2001 From: Christoph Kerschbaumer Date: Tue, 28 Jun 2016 19:03:56 +0200 Subject: [PATCH] Bug 1282554 - Provide helper function to check if channel can be openen using open2() and asyncOpen2() r=bholly --- .../webbrowserpersist/nsWebBrowserPersist.cpp | 18 ++---------- modules/libjar/zipwriter/nsZipWriter.cpp | 19 +++---------- netwerk/base/nsNetUtil.cpp | 28 ++++++++++++++----- netwerk/base/nsNetUtil.h | 16 +++++++++++ netwerk/protocol/http/PackagedAppService.cpp | 7 +---- .../viewsource/nsViewSourceChannel.cpp | 9 +----- .../protocol/websocket/WebSocketChannel.cpp | 8 +----- 7 files changed, 47 insertions(+), 58 deletions(-) diff --git a/embedding/components/webbrowserpersist/nsWebBrowserPersist.cpp b/embedding/components/webbrowserpersist/nsWebBrowserPersist.cpp index 6d66100a2fd0..5aff610f86aa 100644 --- a/embedding/components/webbrowserpersist/nsWebBrowserPersist.cpp +++ b/embedding/components/webbrowserpersist/nsWebBrowserPersist.cpp @@ -1471,16 +1471,10 @@ nsresult nsWebBrowserPersist::SaveChannelInternal( nsCOMPtr fc(do_QueryInterface(aChannel)); nsCOMPtr fu(do_QueryInterface(aFile)); - nsCOMPtr loadInfo = aChannel->GetLoadInfo(); if (fc && !fu) { nsCOMPtr fileInputStream, bufferedInputStream; - nsresult rv; - if (loadInfo && loadInfo->GetSecurityMode()) { - rv = aChannel->Open2(getter_AddRefs(fileInputStream)); - } - else { - rv = aChannel->Open(getter_AddRefs(fileInputStream)); - } + nsresult rv = NS_MaybeOpenChannelUsingOpen2(aChannel, + getter_AddRefs(fileInputStream)); NS_ENSURE_SUCCESS(rv, rv); rv = NS_NewBufferedInputStream(getter_AddRefs(bufferedInputStream), fileInputStream, BUFFERED_OUTPUT_SIZE); @@ -1491,13 +1485,7 @@ nsresult nsWebBrowserPersist::SaveChannelInternal( } // Read from the input channel - nsresult rv; - if (loadInfo && loadInfo->GetSecurityMode()) { - rv = aChannel->AsyncOpen2(this); - } - else { - rv = aChannel->AsyncOpen(this, nullptr); - } + nsresult rv = NS_MaybeOpenChannelUsingAsyncOpen2(aChannel, this); if (rv == NS_ERROR_NO_CONTENT) { // Assume this is a protocol such as mailto: which does not feed out diff --git a/modules/libjar/zipwriter/nsZipWriter.cpp b/modules/libjar/zipwriter/nsZipWriter.cpp index db2591d7164c..25231fac0c39 100644 --- a/modules/libjar/zipwriter/nsZipWriter.cpp +++ b/modules/libjar/zipwriter/nsZipWriter.cpp @@ -418,14 +418,9 @@ NS_IMETHODIMP nsZipWriter::AddEntryChannel(const nsACString & aZipEntry, return NS_ERROR_FILE_ALREADY_EXISTS; nsCOMPtr inputStream; - nsCOMPtr loadInfo = aChannel->GetLoadInfo(); - nsresult rv; - if (loadInfo && loadInfo->GetSecurityMode()) { - rv = aChannel->Open2(getter_AddRefs(inputStream)); - } - else { - rv = aChannel->Open(getter_AddRefs(inputStream)); - } + nsresult rv = NS_MaybeOpenChannelUsingOpen2(aChannel, + getter_AddRefs(inputStream)); + NS_ENSURE_SUCCESS(rv, rv); rv = AddEntryStream(aZipEntry, aModTime, aCompression, inputStream, @@ -1001,13 +996,7 @@ inline nsresult nsZipWriter::BeginProcessingAddition(nsZipQueueItem* aItem, NS_ENSURE_SUCCESS(rv, rv); } else { - nsCOMPtr loadInfo = aItem->mChannel->GetLoadInfo(); - if (loadInfo && loadInfo->GetSecurityMode()) { - rv = aItem->mChannel->AsyncOpen2(stream); - } - else { - rv = aItem->mChannel->AsyncOpen(stream, nullptr); - } + rv = NS_MaybeOpenChannelUsingAsyncOpen2(aItem->mChannel, stream); NS_ENSURE_SUCCESS(rv, rv); } diff --git a/netwerk/base/nsNetUtil.cpp b/netwerk/base/nsNetUtil.cpp index 79c8aa1062ce..f8492cef7c5b 100644 --- a/netwerk/base/nsNetUtil.cpp +++ b/netwerk/base/nsNetUtil.cpp @@ -751,13 +751,7 @@ NS_ImplementChannelOpen(nsIChannel *channel, getter_AddRefs(stream)); NS_ENSURE_SUCCESS(rv, rv); - nsCOMPtr loadInfo = channel->GetLoadInfo(); - if (loadInfo && loadInfo->GetEnforceSecurity()) { - rv = channel->AsyncOpen2(listener); - } - else { - rv = channel->AsyncOpen(listener, nullptr); - } + rv = NS_MaybeOpenChannelUsingAsyncOpen2(channel, listener); NS_ENSURE_SUCCESS(rv, rv); uint64_t n; @@ -2003,6 +1997,26 @@ nsresult NS_MakeRandomInvalidURLString(nsCString &result) #undef NS_FAKE_SCHEME #undef NS_FAKE_TLD +nsresult NS_MaybeOpenChannelUsingOpen2(nsIChannel* aChannel, + nsIInputStream **aStream) +{ + nsCOMPtr loadInfo = aChannel->GetLoadInfo(); + if (loadInfo && loadInfo->GetSecurityMode() != 0) { + return aChannel->Open2(aStream); + } + return aChannel->Open(aStream); +} + +nsresult NS_MaybeOpenChannelUsingAsyncOpen2(nsIChannel* aChannel, + nsIStreamListener *aListener) +{ + nsCOMPtr loadInfo = aChannel->GetLoadInfo(); + if (loadInfo && loadInfo->GetSecurityMode() != 0) { + return aChannel->AsyncOpen2(aListener); + } + return aChannel->AsyncOpen(aListener, nullptr); +} + nsresult NS_CheckIsJavaCompatibleURLString(nsCString &urlString, bool *result) { diff --git a/netwerk/base/nsNetUtil.h b/netwerk/base/nsNetUtil.h index 5cc5934bdffd..edd9038fda7e 100644 --- a/netwerk/base/nsNetUtil.h +++ b/netwerk/base/nsNetUtil.h @@ -909,6 +909,22 @@ nsresult NS_LinkRedirectChannels(uint32_t channelId, */ nsresult NS_MakeRandomInvalidURLString(nsCString &result); +/** + * Helper function which checks whether the channel can be + * openend using Open2() or has to fall back to opening + * the channel using Open(). + */ +nsresult NS_MaybeOpenChannelUsingOpen2(nsIChannel* aChannel, + nsIInputStream **aStream); + +/** + * Helper function which checks whether the channel can be + * openend using AsyncOpen2() or has to fall back to opening + * the channel using AsyncOpen(). + */ +nsresult NS_MaybeOpenChannelUsingAsyncOpen2(nsIChannel* aChannel, + nsIStreamListener *aListener); + /** * Helper function to determine whether urlString is Java-compatible -- * whether it can be passed to the Java URL(String) constructor without the diff --git a/netwerk/protocol/http/PackagedAppService.cpp b/netwerk/protocol/http/PackagedAppService.cpp index 48865390ad9e..075ef77d9b48 100644 --- a/netwerk/protocol/http/PackagedAppService.cpp +++ b/netwerk/protocol/http/PackagedAppService.cpp @@ -1160,12 +1160,7 @@ PackagedAppService::GetResource(nsIChannel *aChannel, if (loadContext) { channel->SetNotificationCallbacks(loadContext); } - - if (loadInfo && loadInfo->GetEnforceSecurity()) { - return channel->AsyncOpen2(listener); - } - - return channel->AsyncOpen(listener, nullptr); + return NS_MaybeOpenChannelUsingAsyncOpen2(channel, listener); } nsresult diff --git a/netwerk/protocol/viewsource/nsViewSourceChannel.cpp b/netwerk/protocol/viewsource/nsViewSourceChannel.cpp index e83dae280773..074c1efd3068 100644 --- a/netwerk/protocol/viewsource/nsViewSourceChannel.cpp +++ b/netwerk/protocol/viewsource/nsViewSourceChannel.cpp @@ -251,14 +251,7 @@ nsViewSourceChannel::Open(nsIInputStream **_retval) { NS_ENSURE_TRUE(mChannel, NS_ERROR_FAILURE); - nsresult rv = NS_OK; - nsCOMPtr loadInfo = mChannel->GetLoadInfo(); - if (loadInfo && loadInfo->GetEnforceSecurity()) { - mChannel->Open2(_retval); - } - else { - mChannel->Open(_retval); - } + nsresult rv = NS_MaybeOpenChannelUsingOpen2(mChannel, _retval); if (NS_SUCCEEDED(rv)) { mOpened = true; } diff --git a/netwerk/protocol/websocket/WebSocketChannel.cpp b/netwerk/protocol/websocket/WebSocketChannel.cpp index 24eab2ff337f..73d0b43aef89 100644 --- a/netwerk/protocol/websocket/WebSocketChannel.cpp +++ b/netwerk/protocol/websocket/WebSocketChannel.cpp @@ -1402,13 +1402,7 @@ WebSocketChannel::BeginOpenInternal() } #endif - nsCOMPtr loadInfo = localChannel->GetLoadInfo(); - if (loadInfo && loadInfo->GetSecurityMode()) { - rv = localChannel->AsyncOpen2(this); - } - else { - rv = localChannel->AsyncOpen(this, nullptr); - } + rv = NS_MaybeOpenChannelUsingAsyncOpen2(localChannel, this); if (NS_FAILED(rv)) { LOG(("WebSocketChannel::BeginOpenInternal: cannot async open\n"));