mirror of
https://gitlab.winehq.org/wine/wine-gecko.git
synced 2024-09-13 09:24:08 -07:00
Bug 1209184: Part 2 - [webext] Localize CSS files served from moz-extension: channels. r=jdm
This commit is contained in:
parent
4f7155879e
commit
a8da996011
@ -8,6 +8,16 @@
|
||||
|
||||
#include "nsIAddonPolicyService.h"
|
||||
#include "nsServiceManagerUtils.h"
|
||||
#include "nsIURL.h"
|
||||
#include "nsIChannel.h"
|
||||
#include "nsIStreamListener.h"
|
||||
#include "nsIRequestObserver.h"
|
||||
#include "nsIInputStreamChannel.h"
|
||||
#include "nsIInputStream.h"
|
||||
#include "nsIOutputStream.h"
|
||||
#include "nsIStreamConverterService.h"
|
||||
#include "nsIPipe.h"
|
||||
#include "nsNetUtil.h"
|
||||
|
||||
namespace mozilla {
|
||||
|
||||
@ -33,4 +43,107 @@ ExtensionProtocolHandler::GetFlagsForURI(nsIURI* aURI, uint32_t* aFlags)
|
||||
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)
|
||||
|
||||
nsresult
|
||||
ExtensionProtocolHandler::SubstituteChannel(nsIURI* aURI,
|
||||
nsILoadInfo* aLoadInfo,
|
||||
nsIChannel** result)
|
||||
{
|
||||
nsresult rv;
|
||||
nsCOMPtr<nsIURL> url = do_QueryInterface(aURI, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsAutoCString ext;
|
||||
rv = url->GetFileExtension(ext);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
if (!ext.LowerCaseEqualsLiteral("css")) {
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
// Filter CSS files to replace locale message tokens with localized strings.
|
||||
|
||||
nsCOMPtr<nsIStreamConverterService> convService =
|
||||
do_GetService(NS_STREAMCONVERTERSERVICE_CONTRACTID, &rv);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
const char* kFromType = "application/vnd.mozilla.webext.unlocalized";
|
||||
const char* kToType = "text/css";
|
||||
|
||||
nsCOMPtr<nsIInputStream> inputStream;
|
||||
if (aLoadInfo && aLoadInfo->GetSecurityMode()) {
|
||||
// Certain security checks require an async channel.
|
||||
|
||||
nsCOMPtr<nsIOutputStream> outputStream;
|
||||
rv = NS_NewPipe(getter_AddRefs(inputStream), getter_AddRefs(outputStream),
|
||||
0, UINT32_MAX, true, false);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIStreamListener> listener;
|
||||
nsCOMPtr<nsIRequestObserver> observer = new PipeCloser(outputStream);
|
||||
rv = NS_NewSimpleStreamListener(getter_AddRefs(listener), outputStream, observer);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
nsCOMPtr<nsIStreamListener> converter;
|
||||
rv = convService->AsyncConvertData(kFromType, kToType, listener,
|
||||
aURI, getter_AddRefs(converter));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = (*result)->AsyncOpen2(converter);
|
||||
} else {
|
||||
// Stylesheet loads for extension content scripts require a sync channel,
|
||||
// but fortunately do not invoke security checks.
|
||||
|
||||
nsCOMPtr<nsIInputStream> sourceStream;
|
||||
rv = (*result)->Open(getter_AddRefs(sourceStream));
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
rv = convService->Convert(sourceStream, kFromType, kToType,
|
||||
aURI, getter_AddRefs(inputStream));
|
||||
}
|
||||
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);
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
} // namespace mozilla
|
||||
|
@ -39,6 +39,8 @@ protected:
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
virtual nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result) override;
|
||||
};
|
||||
|
||||
} // namespace mozilla
|
||||
|
@ -253,7 +253,10 @@ SubstitutingProtocolHandler::NewChannel2(nsIURI* uri,
|
||||
nsLoadFlags loadFlags = 0;
|
||||
(*result)->GetLoadFlags(&loadFlags);
|
||||
(*result)->SetLoadFlags(loadFlags & ~nsIChannel::LOAD_REPLACE);
|
||||
return (*result)->SetOriginalURI(uri);
|
||||
rv = (*result)->SetOriginalURI(uri);
|
||||
NS_ENSURE_SUCCESS(rv, rv);
|
||||
|
||||
return SubstituteChannel(uri, aLoadInfo, result);
|
||||
}
|
||||
|
||||
nsresult
|
||||
|
@ -59,6 +59,13 @@ protected:
|
||||
return false;
|
||||
}
|
||||
|
||||
// Override this in the subclass to check for special case when opening
|
||||
// channels.
|
||||
virtual nsresult SubstituteChannel(nsIURI* uri, nsILoadInfo* aLoadInfo, nsIChannel** result)
|
||||
{
|
||||
return NS_OK;
|
||||
}
|
||||
|
||||
nsIIOService* IOService() { return mIOService; }
|
||||
|
||||
private:
|
||||
|
Loading…
Reference in New Issue
Block a user