Bug 1157343 - Protect ProgressTracker::mImage with a mutex. r=tn

This commit is contained in:
Seth Fowler 2015-04-23 15:37:08 -07:00
parent cfa72e2d4f
commit b8b6ac7d84
2 changed files with 28 additions and 16 deletions

View File

@ -87,6 +87,7 @@ CheckProgressConsistency(Progress aProgress)
void
ProgressTracker::SetImage(Image* aImage)
{
MutexAutoLock lock(mImageMutex);
MOZ_ASSERT(aImage, "Setting null image");
MOZ_ASSERT(!mImage, "Setting image when we already have one");
mImage = aImage;
@ -95,6 +96,7 @@ ProgressTracker::SetImage(Image* aImage)
void
ProgressTracker::ResetImage()
{
MutexAutoLock lock(mImageMutex);
MOZ_ASSERT(mImage, "Resetting image when it's already null!");
mImage = nullptr;
}
@ -183,8 +185,9 @@ ProgressTracker::Notify(IProgressObserver* aObserver)
MOZ_ASSERT(NS_IsMainThread());
#ifdef PR_LOGGING
if (mImage && mImage->GetURI()) {
nsRefPtr<ImageURL> uri(mImage->GetURI());
nsRefPtr<Image> image = GetImage();
if (image && image->GetURI()) {
nsRefPtr<ImageURL> uri(image->GetURI());
nsAutoCString spec;
uri->GetSpec(spec);
LOG_FUNC_WITH_PARAM(GetImgLog(),
@ -251,9 +254,10 @@ ProgressTracker::NotifyCurrentState(IProgressObserver* aObserver)
MOZ_ASSERT(NS_IsMainThread());
#ifdef PR_LOGGING
nsRefPtr<Image> image = GetImage();
nsAutoCString spec;
if (mImage && mImage->GetURI()) {
mImage->GetURI()->GetSpec(spec);
if (image && image->GetURI()) {
image->GetURI()->GetSpec(spec);
}
LOG_FUNC_WITH_PARAM(GetImgLog(),
"ProgressTracker::NotifyCurrentState", "uri", spec.get());
@ -367,7 +371,7 @@ ProgressTracker::SyncNotifyProgress(Progress aProgress,
CheckProgressConsistency(mProgress);
// Send notifications.
SyncNotifyInternal(mObservers, !!mImage, progress, aInvalidRect);
SyncNotifyInternal(mObservers, HasImage(), progress, aInvalidRect);
if (progress & FLAG_HAS_ERROR) {
FireFailureNotification();
@ -379,19 +383,21 @@ ProgressTracker::SyncNotify(IProgressObserver* aObserver)
{
MOZ_ASSERT(NS_IsMainThread());
nsRefPtr<Image> image = GetImage();
#ifdef PR_LOGGING
nsAutoCString spec;
if (mImage && mImage->GetURI()) {
mImage->GetURI()->GetSpec(spec);
if (image && image->GetURI()) {
image->GetURI()->GetSpec(spec);
}
LOG_SCOPE_WITH_PARAM(GetImgLog(),
"ProgressTracker::SyncNotify", "uri", spec.get());
#endif
nsIntRect rect;
if (mImage) {
if (NS_FAILED(mImage->GetWidth(&rect.width)) ||
NS_FAILED(mImage->GetHeight(&rect.height))) {
if (image) {
if (NS_FAILED(image->GetWidth(&rect.width)) ||
NS_FAILED(image->GetHeight(&rect.height))) {
// Either the image has no intrinsic size, or it has an error.
rect = GetMaxSizedIntRect();
}
@ -399,7 +405,7 @@ ProgressTracker::SyncNotify(IProgressObserver* aObserver)
ObserverArray array;
array.AppendElement(aObserver);
SyncNotifyInternal(array, !!mImage, mProgress, rect);
SyncNotifyInternal(array, !!image, mProgress, rect);
}
void
@ -511,11 +517,12 @@ ProgressTracker::FireFailureNotification()
// Some kind of problem has happened with image decoding.
// Report the URI to net:failed-to-process-uri-conent observers.
if (mImage) {
nsRefPtr<Image> image = GetImage();
if (image) {
// Should be on main thread, so ok to create a new nsIURI.
nsCOMPtr<nsIURI> uri;
{
nsRefPtr<ImageURL> threadsafeUriData = mImage->GetURI();
nsRefPtr<ImageURL> threadsafeUriData = image->GetURI();
uri = threadsafeUriData ? threadsafeUriData->ToIURI() : nullptr;
}
if (uri) {

View File

@ -7,6 +7,7 @@
#ifndef mozilla_image_src_ProgressTracker_h
#define mozilla_image_src_ProgressTracker_h
#include "mozilla/Mutex.h"
#include "mozilla/RefPtr.h"
#include "mozilla/WeakPtr.h"
#include "nsCOMPtr.h"
@ -76,13 +77,15 @@ public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(ProgressTracker)
ProgressTracker()
: mImage(nullptr)
: mImageMutex("ProgressTracker::mImage")
, mImage(nullptr)
, mProgress(NoProgress)
{ }
bool HasImage() const { return mImage; }
bool HasImage() const { MutexAutoLock lock(mImageMutex); return mImage; }
already_AddRefed<Image> GetImage() const
{
MutexAutoLock lock(mImageMutex);
nsRefPtr<Image> image = mImage;
return image.forget();
}
@ -190,7 +193,9 @@ private:
nsCOMPtr<nsIRunnable> mRunnable;
// This weak ref should be set null when the image goes out of scope.
// mImage is a weak ref; it should be set to null when the image goes out of
// scope. mImageMutex protects mImage.
mutable Mutex mImageMutex;
Image* mImage;
// List of observers attached to the image. Each observer represents a