From d1851fa263838bd8224abe4d91f631656327bc09 Mon Sep 17 00:00:00 2001 From: Matt Woodrow Date: Mon, 16 Dec 2019 21:40:06 +0000 Subject: [PATCH] Bug 1574372 - Add API to test stream converters to find out their output type. r=bzbarsky We don't want to run stream conversion in the parent (since a lot of them require access to the document), so this instead adds a way to find out what their output type will be. Differential Revision: https://phabricator.services.mozilla.com/D56134 --HG-- extra : moz-landing-system : lando --- .../pdfjs/content/PdfStreamConverter.jsm | 4 ++++ devtools/client/jsonview/converter-child.js | 3 +++ .../libjar/zipwriter/nsDeflateConverter.cpp | 6 ++++++ .../converters/mozTXTToHTMLConv.cpp | 6 ++++++ .../converters/nsFTPDirListingConv.cpp | 6 ++++++ .../converters/nsHTTPCompressConv.cpp | 6 ++++++ .../streamconv/converters/nsIndexedToHTML.cpp | 6 ++++++ .../converters/nsMultiMixedConv.cpp | 6 ++++++ .../converters/nsUnknownDecoder.cpp | 6 ++++++ netwerk/streamconv/nsIStreamConverter.idl | 9 +++++++++ .../streamconv/nsIStreamConverterService.idl | 8 ++++++++ .../streamconv/nsStreamConverterService.cpp | 19 +++++++++++++++++++ 12 files changed, 85 insertions(+) diff --git a/browser/extensions/pdfjs/content/PdfStreamConverter.jsm b/browser/extensions/pdfjs/content/PdfStreamConverter.jsm index 43a5d6d30313..7740a75c6938 100644 --- a/browser/extensions/pdfjs/content/PdfStreamConverter.jsm +++ b/browser/extensions/pdfjs/content/PdfStreamConverter.jsm @@ -1008,6 +1008,10 @@ PdfStreamConverter.prototype = { this.listener = aListener; }, + getConvertedType(aFromType) { + return "text/html"; + }, + // nsIStreamListener::onDataAvailable onDataAvailable(aRequest, aInputStream, aOffset, aCount) { if (!this.dataListener) { diff --git a/devtools/client/jsonview/converter-child.js b/devtools/client/jsonview/converter-child.js index 087085c01a32..525c9277af49 100644 --- a/devtools/client/jsonview/converter-child.js +++ b/devtools/client/jsonview/converter-child.js @@ -75,6 +75,9 @@ Converter.prototype = { asyncConvertData: function(fromType, toType, listener, ctx) { this.listener = listener; }, + getConvertedType: function(fromType) { + return "text/html"; + }, onDataAvailable: function(request, inputStream, offset, count) { // Decode and insert data. diff --git a/modules/libjar/zipwriter/nsDeflateConverter.cpp b/modules/libjar/zipwriter/nsDeflateConverter.cpp index 821bb95ef558..09ac1a4885bc 100644 --- a/modules/libjar/zipwriter/nsDeflateConverter.cpp +++ b/modules/libjar/zipwriter/nsDeflateConverter.cpp @@ -91,6 +91,12 @@ NS_IMETHODIMP nsDeflateConverter::AsyncConvertData(const char* aFromType, return rv; } +NS_IMETHODIMP +nsDeflateConverter::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + NS_IMETHODIMP nsDeflateConverter::OnDataAvailable(nsIRequest* aRequest, nsIInputStream* aInputStream, uint64_t aOffset, diff --git a/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp b/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp index 3c2776aedd62..3c09e15ea6dc 100644 --- a/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp +++ b/netwerk/streamconv/converters/mozTXTToHTMLConv.cpp @@ -1217,6 +1217,12 @@ mozTXTToHTMLConv::AsyncConvertData(const char* aFromType, const char* aToType, return NS_ERROR_NOT_IMPLEMENTED; } +NS_IMETHODIMP +mozTXTToHTMLConv::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + NS_IMETHODIMP mozTXTToHTMLConv::OnDataAvailable(nsIRequest* request, nsIInputStream* inStr, uint64_t sourceOffset, uint32_t count) { diff --git a/netwerk/streamconv/converters/nsFTPDirListingConv.cpp b/netwerk/streamconv/converters/nsFTPDirListingConv.cpp index b721016afd8f..c8cebd23d797 100644 --- a/netwerk/streamconv/converters/nsFTPDirListingConv.cpp +++ b/netwerk/streamconv/converters/nsFTPDirListingConv.cpp @@ -70,6 +70,12 @@ nsFTPDirListingConv::AsyncConvertData(const char* aFromType, return NS_OK; } +NS_IMETHODIMP +nsFTPDirListingConv::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + // nsIStreamListener implementation NS_IMETHODIMP nsFTPDirListingConv::OnDataAvailable(nsIRequest* request, nsIInputStream* inStr, diff --git a/netwerk/streamconv/converters/nsHTTPCompressConv.cpp b/netwerk/streamconv/converters/nsHTTPCompressConv.cpp index 6408a32eb0b8..2ad0b4a49060 100644 --- a/netwerk/streamconv/converters/nsHTTPCompressConv.cpp +++ b/netwerk/streamconv/converters/nsHTTPCompressConv.cpp @@ -115,6 +115,12 @@ nsHTTPCompressConv::AsyncConvertData(const char* aFromType, const char* aToType, return NS_OK; } +NS_IMETHODIMP +nsHTTPCompressConv::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + NS_IMETHODIMP nsHTTPCompressConv::OnStartRequest(nsIRequest* request) { LOG(("nsHttpCompresssConv %p onstart\n", this)); diff --git a/netwerk/streamconv/converters/nsIndexedToHTML.cpp b/netwerk/streamconv/converters/nsIndexedToHTML.cpp index 64d03c4a6804..cc2850edc7e4 100644 --- a/netwerk/streamconv/converters/nsIndexedToHTML.cpp +++ b/netwerk/streamconv/converters/nsIndexedToHTML.cpp @@ -90,6 +90,12 @@ nsIndexedToHTML::AsyncConvertData(const char* aFromType, const char* aToType, return Init(aListener); } +NS_IMETHODIMP +nsIndexedToHTML::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + NS_IMETHODIMP nsIndexedToHTML::OnStartRequest(nsIRequest* request) { nsCString buffer; diff --git a/netwerk/streamconv/converters/nsMultiMixedConv.cpp b/netwerk/streamconv/converters/nsMultiMixedConv.cpp index f1b6b1bca72c..88209b6d2dc5 100644 --- a/netwerk/streamconv/converters/nsMultiMixedConv.cpp +++ b/netwerk/streamconv/converters/nsMultiMixedConv.cpp @@ -406,6 +406,12 @@ nsMultiMixedConv::AsyncConvertData(const char* aFromType, const char* aToType, return NS_OK; } +NS_IMETHODIMP +nsMultiMixedConv::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + // nsIRequestObserver implementation NS_IMETHODIMP nsMultiMixedConv::OnStartRequest(nsIRequest* request) { diff --git a/netwerk/streamconv/converters/nsUnknownDecoder.cpp b/netwerk/streamconv/converters/nsUnknownDecoder.cpp index 686fcbcea055..76eb1fd889e8 100644 --- a/netwerk/streamconv/converters/nsUnknownDecoder.cpp +++ b/netwerk/streamconv/converters/nsUnknownDecoder.cpp @@ -143,6 +143,12 @@ nsUnknownDecoder::AsyncConvertData(const char* aFromType, const char* aToType, return (aListener) ? NS_OK : NS_ERROR_FAILURE; } +NS_IMETHODIMP +nsUnknownDecoder::GetConvertedType(const nsACString& aFromType, + nsACString& aToType) { + return NS_ERROR_NOT_IMPLEMENTED; +} + // ---- // // nsIStreamListener methods... diff --git a/netwerk/streamconv/nsIStreamConverter.idl b/netwerk/streamconv/nsIStreamConverter.idl index 22e6cbcfa8f2..a0f5dc5caa1d 100644 --- a/netwerk/streamconv/nsIStreamConverter.idl +++ b/netwerk/streamconv/nsIStreamConverter.idl @@ -93,6 +93,15 @@ interface nsIStreamConverter : nsIStreamListener { in string aToType, in nsIStreamListener aListener, in nsISupports aCtxt); + + /** + * Returns the content type that the stream listener passed to asyncConvertData will + * see on the channel if the conversion is being done from aFromType to * /*. + * + * @throws if the converter does not support conversion to * /* or if it doesn't know + * the type in advance. + */ + ACString getConvertedType(in ACString aFromType); }; %{C++ diff --git a/netwerk/streamconv/nsIStreamConverterService.idl b/netwerk/streamconv/nsIStreamConverterService.idl index 73d117942520..d4072808c687 100644 --- a/netwerk/streamconv/nsIStreamConverterService.idl +++ b/netwerk/streamconv/nsIStreamConverterService.idl @@ -34,6 +34,14 @@ interface nsIStreamConverterService : nsISupports { */ boolean canConvert(in string aFromType, in string aToType); + /** + * Returns the content type that will be returned from a converter + * created with aFromType and * /*. + * Can fail if no converters support this conversion, or if the + * output type isn't known in advance. + */ + ACString convertedType(in ACString aFromType); + /** * SYNCHRONOUS VERSION * Converts a stream of one type, to a stream of another type. diff --git a/netwerk/streamconv/nsStreamConverterService.cpp b/netwerk/streamconv/nsStreamConverterService.cpp index ae558295be40..a6a57ed2f168 100644 --- a/netwerk/streamconv/nsStreamConverterService.cpp +++ b/netwerk/streamconv/nsStreamConverterService.cpp @@ -352,6 +352,25 @@ nsStreamConverterService::CanConvert(const char* aFromType, const char* aToType, return NS_OK; } +NS_IMETHODIMP +nsStreamConverterService::ConvertedType(const nsACString& aFromType, + nsACString& aOutToType) { + // first determine whether we can even handle this conversion + // build a CONTRACTID + nsAutoCString contractID; + contractID.AssignLiteral(NS_ISTREAMCONVERTER_KEY "?from="); + contractID.Append(aFromType); + contractID.AppendLiteral("&to=*/*"); + const char* cContractID = contractID.get(); + + nsresult rv; + nsCOMPtr converter(do_CreateInstance(cContractID, &rv)); + if (NS_SUCCEEDED(rv)) { + return converter->GetConvertedType(aFromType, aOutToType); + } + return rv; +} + NS_IMETHODIMP nsStreamConverterService::Convert(nsIInputStream* aFromStream, const char* aFromType, const char* aToType,