Bug 916128: Ensure that image encoding callbacks are released on the main thread. r=khuey

This commit is contained in:
Stephen Pohl 2013-10-08 13:27:56 -04:00
parent 1ea44119a7
commit 6e9d057083

View File

@ -24,6 +24,7 @@ public:
, mScriptContext(aScriptContext)
, mEncoderThread(aEncoderThread)
, mCallback(&aCallback)
, mFailed(false)
{}
virtual ~EncodingCompleteEvent() {}
@ -31,19 +32,28 @@ public:
{
MOZ_ASSERT(NS_IsMainThread());
nsRefPtr<nsDOMMemoryFile> blob =
new nsDOMMemoryFile(mImgData, mImgSize, mType);
mozilla::ErrorResult rv;
if (mScriptContext) {
JSContext* jsContext = mScriptContext->GetNativeContext();
if (jsContext) {
JS_updateMallocCounter(jsContext, mImgSize);
if (!mFailed) {
nsRefPtr<nsDOMMemoryFile> blob =
new nsDOMMemoryFile(mImgData, mImgSize, mType);
if (mScriptContext) {
JSContext* jsContext = mScriptContext->GetNativeContext();
if (jsContext) {
JS_updateMallocCounter(jsContext, mImgSize);
}
}
mCallback->Call(blob, rv);
}
mozilla::ErrorResult rv;
mCallback->Call(blob, rv);
NS_ENSURE_SUCCESS(rv.ErrorCode(), rv.ErrorCode());
// These members aren't thread-safe. We're making sure that they're being
// released on the main thread here. Otherwise, they could be getting
// released by EncodingRunnable's destructor on the encoding thread
// (bug 916128).
mScriptContext = nullptr;
mCallback = nullptr;
mEncoderThread->Shutdown();
return rv.ErrorCode();
@ -56,6 +66,11 @@ public:
mType = aType;
}
void SetFailed()
{
mFailed = true;
}
private:
uint64_t mImgSize;
nsAutoString mType;
@ -63,6 +78,7 @@ private:
nsCOMPtr<nsIScriptContext> mScriptContext;
nsCOMPtr<nsIThread> mEncoderThread;
nsRefPtr<FileCallback> mCallback;
bool mFailed;
};
NS_IMPL_ISUPPORTS1(EncodingCompleteEvent, nsIRunnable);
@ -91,7 +107,7 @@ public:
{}
virtual ~EncodingRunnable() {}
NS_IMETHOD Run()
nsresult ProcessImageData(uint64_t* aImgSize, void** aImgData)
{
nsCOMPtr<nsIInputStream> stream;
nsresult rv = ImageEncoder::ExtractDataInternal(mType,
@ -117,18 +133,33 @@ public:
}
NS_ENSURE_SUCCESS(rv, rv);
rv = stream->Available(aImgSize);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(*aImgSize <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
rv = NS_ReadInputStreamToBuffer(stream, aImgData, *aImgSize);
NS_ENSURE_SUCCESS(rv, rv);
return rv;
}
NS_IMETHOD Run()
{
uint64_t imgSize;
rv = stream->Available(&imgSize);
NS_ENSURE_SUCCESS(rv, rv);
NS_ENSURE_TRUE(imgSize <= UINT32_MAX, NS_ERROR_FILE_TOO_BIG);
void* imgData = nullptr;
rv = NS_ReadInputStreamToBuffer(stream, &imgData, imgSize);
NS_ENSURE_SUCCESS(rv, rv);
mEncodingCompleteEvent->SetMembers(imgData, imgSize, mType);
nsresult rv = ProcessImageData(&imgSize, &imgData);
if (NS_FAILED(rv)) {
mEncodingCompleteEvent->SetFailed();
} else {
mEncodingCompleteEvent->SetMembers(imgData, imgSize, mType);
}
rv = NS_DispatchToMainThread(mEncodingCompleteEvent, NS_DISPATCH_NORMAL);
NS_ENSURE_SUCCESS(rv, rv);
if (NS_FAILED(rv)) {
// Better to leak than to crash.
mEncodingCompleteEvent.forget();
return rv;
}
return rv;
}