Bug 1351163: Part 3 - Don't open moz-extension: base channel on NewChannel2(). r=bz

MozReview-Commit-ID: F8H75VM33nz

--HG--
extra : rebase_source : 1c5c61ecd5f4e3c27907feb29f9c07ecaba1bcc6
This commit is contained in:
Kris Maglione 2017-04-07 22:35:57 -07:00
Родитель 26b71c675c
Коммит 8cb2eed52d
1 изменённых файлов: 39 добавлений и 67 удалений

Просмотреть файл

@ -11,14 +11,12 @@
#include "nsIURL.h" #include "nsIURL.h"
#include "nsIChannel.h" #include "nsIChannel.h"
#include "nsIStreamListener.h" #include "nsIStreamListener.h"
#include "nsIRequestObserver.h"
#include "nsIInputStreamChannel.h"
#include "nsIInputStream.h" #include "nsIInputStream.h"
#include "nsIOutputStream.h" #include "nsIOutputStream.h"
#include "nsIStreamConverterService.h" #include "nsIStreamConverterService.h"
#include "nsIPipe.h"
#include "nsNetUtil.h" #include "nsNetUtil.h"
#include "LoadInfo.h" #include "LoadInfo.h"
#include "SimpleChannel.h"
namespace mozilla { namespace mozilla {
namespace net { namespace net {
@ -45,39 +43,6 @@ ExtensionProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aFlags)
return NS_OK; return NS_OK;
} }
class PipeCloser : public nsIRequestObserver
{
public:
NS_DECL_ISUPPORTS
explicit PipeCloser(nsIOutputStream* aOutputStream) :
mOutputStream(aOutputStream)
{
}
NS_IMETHOD OnStartRequest(nsIRequest*, nsISupports*) override
{
return NS_OK;
}
NS_IMETHOD OnStopRequest(nsIRequest*, nsISupports*, nsresult aStatusCode) override
{
NS_ENSURE_TRUE(mOutputStream, NS_ERROR_UNEXPECTED);
nsresult rv = mOutputStream->Close();
mOutputStream = nullptr;
return rv;
}
protected:
virtual ~PipeCloser() {}
private:
nsCOMPtr<nsIOutputStream> mOutputStream;
};
NS_IMPL_ISUPPORTS(PipeCloser, nsIRequestObserver)
bool bool
ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost, ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
const nsACString& aPath, const nsACString& aPath,
@ -111,6 +76,17 @@ ExtensionProtocolHandler::ResolveSpecialCases(const nsACString& aHost,
return false; return false;
} }
static inline Result<Ok, nsresult>
WrapNSResult(nsresult aRv)
{
if (NS_FAILED(aRv)) {
return Err(aRv);
}
return Ok();
}
#define NS_TRY(expr) MOZ_TRY(WrapNSResult(expr))
nsresult nsresult
ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI, ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI,
nsILoadInfo* aLoadInfo, nsILoadInfo* aLoadInfo,
@ -130,50 +106,46 @@ ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI,
// Filter CSS files to replace locale message tokens with localized strings. // Filter CSS files to replace locale message tokens with localized strings.
nsCOMPtr<nsIStreamConverterService> convService = bool haveLoadInfo = aLoadInfo;
do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv); nsCOMPtr<nsIChannel> channel = NS_NewSimpleChannel(
NS_ENSURE_SUCCESS(rv, rv); aURI, aLoadInfo, *result,
[haveLoadInfo] (nsIStreamListener* listener, nsIChannel* channel, nsIChannel* origChannel) -> RequestOrReason {
nsresult rv;
nsCOMPtr<nsIStreamConverterService> convService =
do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv);
NS_TRY(rv);
const char* kFromType = "application/vnd.mozilla.webext.unlocalized"; nsCOMPtr<nsIURI> uri;
const char* kToType = "text/css"; NS_TRY(channel->GetURI(getter_AddRefs(uri)));
nsCOMPtr<nsIInputStream> inputStream; const char* kFromType = "application/vnd.mozilla.webext.unlocalized";
nsCOMPtr<nsIOutputStream> outputStream; const char* kToType = "text/css";
rv = NS_NewPipe(getter_AddRefs(inputStream), getter_AddRefs(outputStream),
0, UINT32_MAX, true, false);
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIStreamListener> listener; nsCOMPtr<nsIStreamListener> converter;
nsCOMPtr<nsIRequestObserver> observer = new PipeCloser(outputStream); NS_TRY(convService->AsyncConvertData(kFromType, kToType, listener,
rv = NS_NewSimpleStreamListener(getter_AddRefs(listener), outputStream, observer); uri, getter_AddRefs(converter)));
NS_ENSURE_SUCCESS(rv, rv); if (haveLoadInfo) {
NS_TRY(origChannel->AsyncOpen2(converter));
} else {
NS_TRY(origChannel->AsyncOpen(converter, nullptr));
}
nsCOMPtr<nsIStreamListener> converter; return RequestOrReason(origChannel);
rv = convService->AsyncConvertData(kFromType, kToType, listener, });
aURI, getter_AddRefs(converter)); NS_ENSURE_TRUE(channel, NS_ERROR_OUT_OF_MEMORY);
NS_ENSURE_SUCCESS(rv, rv);
if (aLoadInfo) { if (aLoadInfo) {
nsCOMPtr<nsILoadInfo> loadInfo = nsCOMPtr<nsILoadInfo> loadInfo =
static_cast<LoadInfo*>(aLoadInfo)->CloneForNewRequest(); static_cast<LoadInfo*>(aLoadInfo)->CloneForNewRequest();
(*result)->SetLoadInfo(loadInfo); (*result)->SetLoadInfo(loadInfo);
rv = (*result)->AsyncOpen2(converter);
} else {
rv = (*result)->AsyncOpen(converter, nullptr);
} }
NS_ENSURE_SUCCESS(rv, rv);
nsCOMPtr<nsIChannel> channel;
rv = NS_NewInputStreamChannelInternal(getter_AddRefs(channel), aURI, inputStream,
NS_LITERAL_CSTRING("text/css"),
NS_LITERAL_CSTRING("utf-8"),
aLoadInfo);
NS_ENSURE_SUCCESS(rv, rv);
channel.swap(*result); channel.swap(*result);
return NS_OK; return NS_OK;
} }
#undef NS_TRY
} // namespace net } // namespace net
} // namespace mozilla } // namespace mozilla