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
This commit is contained in:
Matt Woodrow 2019-12-16 21:40:06 +00:00
parent 9beb54445d
commit d1851fa263
12 changed files with 85 additions and 0 deletions

View File

@ -1008,6 +1008,10 @@ PdfStreamConverter.prototype = {
this.listener = aListener;
},
getConvertedType(aFromType) {
return "text/html";
},
// nsIStreamListener::onDataAvailable
onDataAvailable(aRequest, aInputStream, aOffset, aCount) {
if (!this.dataListener) {

View File

@ -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.

View File

@ -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,

View File

@ -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) {

View File

@ -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,

View File

@ -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));

View File

@ -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;

View File

@ -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) {

View File

@ -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...

View File

@ -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++

View File

@ -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);
/**
* <b>SYNCHRONOUS VERSION</b>
* Converts a stream of one type, to a stream of another type.

View File

@ -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<nsIStreamConverter> 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,