2010-05-14 20:47:59 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 11:12:37 +00:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2014-08-22 20:12:38 +00:00
|
|
|
#include "ImageLogging.h"
|
2014-11-15 04:10:47 +00:00
|
|
|
#include "ProgressTracker.h"
|
2010-05-14 20:47:59 +00:00
|
|
|
|
|
|
|
#include "imgIContainer.h"
|
2015-01-07 09:35:20 +00:00
|
|
|
#include "imgINotificationObserver.h"
|
|
|
|
#include "imgIRequest.h"
|
2010-08-14 04:09:48 +00:00
|
|
|
#include "Image.h"
|
2013-09-28 18:28:42 +00:00
|
|
|
#include "nsNetUtil.h"
|
2012-10-12 16:11:21 +00:00
|
|
|
#include "nsIObserverService.h"
|
2012-10-12 01:34:22 +00:00
|
|
|
|
2012-10-12 16:11:20 +00:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-10-12 16:11:21 +00:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-12 16:11:20 +00:00
|
|
|
|
2013-12-12 21:17:35 +00:00
|
|
|
using mozilla::WeakPtr;
|
2010-08-14 04:09:49 +00:00
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2014-11-15 04:10:48 +00:00
|
|
|
static void
|
|
|
|
CheckProgressConsistency(Progress aProgress)
|
|
|
|
{
|
|
|
|
// Check preconditions for every progress bit.
|
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-17 22:29:56 +00:00
|
|
|
// No preconditions.
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_DECODE_COMPLETE) {
|
2015-07-07 00:11:14 +00:00
|
|
|
// No preconditions.
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_FRAME_COMPLETE) {
|
2015-07-07 00:11:14 +00:00
|
|
|
// No preconditions.
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-17 22:29:56 +00:00
|
|
|
// No preconditions.
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_ONLOAD_BLOCKED) {
|
2015-06-30 09:37:58 +00:00
|
|
|
// No preconditions.
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_ONLOAD_UNBLOCKED) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_ONLOAD_BLOCKED);
|
2015-06-30 09:37:58 +00:00
|
|
|
MOZ_ASSERT(aProgress & (FLAG_SIZE_AVAILABLE | FLAG_HAS_ERROR));
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_IS_ANIMATED) {
|
2014-11-17 22:29:56 +00:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_SIZE_AVAILABLE);
|
2014-11-17 19:16:45 +00:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_HAS_TRANSPARENCY) {
|
2014-11-17 22:29:56 +00:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_SIZE_AVAILABLE);
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_LAST_PART_COMPLETE) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_LOAD_COMPLETE);
|
2014-11-15 04:10:48 +00:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_HAS_ERROR) {
|
|
|
|
// No preconditions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-23 22:44:07 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::SetImage(Image* aImage)
|
2010-08-23 22:44:07 +00:00
|
|
|
{
|
2015-05-01 01:13:14 +00:00
|
|
|
MutexAutoLock lock(mImageMutex);
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(aImage, "Setting null image");
|
|
|
|
MOZ_ASSERT(!mImage, "Setting image when we already have one");
|
2010-08-23 22:44:07 +00:00
|
|
|
mImage = aImage;
|
|
|
|
}
|
|
|
|
|
2013-09-28 18:28:44 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::ResetImage()
|
2013-09-28 18:28:44 +00:00
|
|
|
{
|
2015-05-01 01:13:14 +00:00
|
|
|
MutexAutoLock lock(mImageMutex);
|
2015-02-09 22:34:50 +00:00
|
|
|
MOZ_ASSERT(mImage, "Resetting image when it's already null!");
|
2013-09-28 18:28:44 +00:00
|
|
|
mImage = nullptr;
|
|
|
|
}
|
|
|
|
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::GetImageStatus() const
|
2010-05-14 20:47:59 +00:00
|
|
|
{
|
2014-11-07 01:34:00 +00:00
|
|
|
uint32_t status = imgIRequest::STATUS_NONE;
|
|
|
|
|
|
|
|
// Translate our current state to a set of imgIRequest::STATE_* flags.
|
2014-11-17 22:29:56 +00:00
|
|
|
if (mProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-07 01:34:00 +00:00
|
|
|
status |= imgIRequest::STATUS_SIZE_AVAILABLE;
|
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (mProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-07 01:34:00 +00:00
|
|
|
status |= imgIRequest::STATUS_DECODE_COMPLETE;
|
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (mProgress & FLAG_FRAME_COMPLETE) {
|
2014-11-07 01:34:00 +00:00
|
|
|
status |= imgIRequest::STATUS_FRAME_COMPLETE;
|
|
|
|
}
|
2014-11-17 22:29:56 +00:00
|
|
|
if (mProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-07 01:34:00 +00:00
|
|
|
status |= imgIRequest::STATUS_LOAD_COMPLETE;
|
|
|
|
}
|
2014-11-25 07:42:43 +00:00
|
|
|
if (mProgress & FLAG_IS_ANIMATED) {
|
|
|
|
status |= imgIRequest::STATUS_IS_ANIMATED;
|
|
|
|
}
|
|
|
|
if (mProgress & FLAG_HAS_TRANSPARENCY) {
|
|
|
|
status |= imgIRequest::STATUS_HAS_TRANSPARENCY;
|
|
|
|
}
|
2014-11-15 04:10:47 +00:00
|
|
|
if (mProgress & FLAG_HAS_ERROR) {
|
2014-11-07 01:34:00 +00:00
|
|
|
status |= imgIRequest::STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 21:52:14 +00:00
|
|
|
// A helper class to allow us to call SyncNotify asynchronously.
|
2014-11-15 04:10:47 +00:00
|
|
|
class AsyncNotifyRunnable : public nsRunnable
|
2010-07-28 21:52:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-11-15 04:10:47 +00:00
|
|
|
AsyncNotifyRunnable(ProgressTracker* aTracker,
|
2015-01-07 09:35:20 +00:00
|
|
|
IProgressObserver* aObserver)
|
2012-12-19 21:28:54 +00:00
|
|
|
: mTracker(aTracker)
|
2010-08-12 15:59:28 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
|
|
|
MOZ_ASSERT(aTracker, "aTracker should not be null");
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(aObserver, "aObserver should not be null");
|
|
|
|
mObservers.AppendElement(aObserver);
|
2010-08-12 15:59:28 +00:00
|
|
|
}
|
2010-07-28 21:52:14 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
|
|
|
MOZ_ASSERT(mTracker, "mTracker should not be null");
|
2015-01-07 09:35:20 +00:00
|
|
|
for (uint32_t i = 0; i < mObservers.Length(); ++i) {
|
|
|
|
mObservers[i]->SetNotificationsDeferred(false);
|
|
|
|
mTracker->SyncNotify(mObservers[i]);
|
2010-08-12 15:59:28 +00:00
|
|
|
}
|
2010-07-28 21:52:14 +00:00
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
mTracker->mRunnable = nullptr;
|
2010-07-28 21:52:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
void AddObserver(IProgressObserver* aObserver)
|
2010-08-12 15:59:28 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
mObservers.AppendElement(aObserver);
|
2010-08-12 15:59:28 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
void RemoveObserver(IProgressObserver* aObserver)
|
2013-01-18 21:47:18 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
mObservers.RemoveElement(aObserver);
|
2013-01-18 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2010-07-28 21:52:14 +00:00
|
|
|
private:
|
2014-11-15 04:10:47 +00:00
|
|
|
friend class ProgressTracker;
|
2010-08-12 15:59:28 +00:00
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> mTracker;
|
|
|
|
nsTArray<RefPtr<IProgressObserver>> mObservers;
|
2010-07-28 21:52:14 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2015-01-07 09:35:20 +00:00
|
|
|
ProgressTracker::Notify(IProgressObserver* aObserver)
|
2010-07-28 21:52:14 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-05-11 20:42:30 +00:00
|
|
|
if (image && image->GetURI()) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ImageURL> uri(image->GetURI());
|
2015-05-11 20:42:30 +00:00
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog,
|
2015-05-11 20:42:30 +00:00
|
|
|
"ProgressTracker::Notify async", "uri", spec.get());
|
|
|
|
} else {
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog,
|
2015-05-11 20:42:30 +00:00
|
|
|
"ProgressTracker::Notify async", "uri", "<unknown>");
|
|
|
|
}
|
2012-12-19 21:28:54 +00:00
|
|
|
}
|
2010-07-28 21:52:34 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
aObserver->SetNotificationsDeferred(true);
|
2010-07-28 21:52:14 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
// If we have an existing runnable that we can use, we just append this
|
|
|
|
// observer to its list of observers to be notified. This ensures we don't
|
|
|
|
// unnecessarily delay onload.
|
2014-11-15 04:10:47 +00:00
|
|
|
AsyncNotifyRunnable* runnable =
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get());
|
|
|
|
|
2012-12-19 21:28:54 +00:00
|
|
|
if (runnable) {
|
2015-01-07 09:35:20 +00:00
|
|
|
runnable->AddObserver(aObserver);
|
2010-08-12 15:59:28 +00:00
|
|
|
} else {
|
2015-01-07 09:35:20 +00:00
|
|
|
mRunnable = new AsyncNotifyRunnable(this, aObserver);
|
2014-11-15 04:10:47 +00:00
|
|
|
NS_DispatchToCurrentThread(mRunnable);
|
2010-08-12 15:59:28 +00:00
|
|
|
}
|
2010-07-28 21:52:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// A helper class to allow us to call SyncNotify asynchronously for a given,
|
|
|
|
// fixed, state.
|
2014-11-15 04:10:47 +00:00
|
|
|
class AsyncNotifyCurrentStateRunnable : public nsRunnable
|
2010-07-28 21:52:14 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-11-15 04:10:47 +00:00
|
|
|
AsyncNotifyCurrentStateRunnable(ProgressTracker* aProgressTracker,
|
2015-01-07 09:35:20 +00:00
|
|
|
IProgressObserver* aObserver)
|
2014-11-15 04:10:47 +00:00
|
|
|
: mProgressTracker(aProgressTracker)
|
2015-01-07 09:35:20 +00:00
|
|
|
, mObserver(aObserver)
|
2013-09-28 18:28:43 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
2014-11-15 04:10:47 +00:00
|
|
|
MOZ_ASSERT(mProgressTracker, "mProgressTracker should not be null");
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(mObserver, "mObserver should not be null");
|
2014-11-15 04:10:47 +00:00
|
|
|
mImage = mProgressTracker->GetImage();
|
2013-09-28 18:28:43 +00:00
|
|
|
}
|
2010-07-28 21:52:14 +00:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
2015-01-07 09:35:20 +00:00
|
|
|
mObserver->SetNotificationsDeferred(false);
|
2010-07-28 21:52:14 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
mProgressTracker->SyncNotify(mObserver);
|
2010-07-28 21:52:14 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ProgressTracker> mProgressTracker;
|
|
|
|
RefPtr<IProgressObserver> mObserver;
|
2014-11-15 04:10:47 +00:00
|
|
|
|
2010-07-28 21:52:14 +00:00
|
|
|
// We have to hold on to a reference to the tracker's image, just in case
|
|
|
|
// it goes away while we're in the event queue.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> mImage;
|
2010-07-28 21:52:14 +00:00
|
|
|
};
|
|
|
|
|
2010-05-14 20:47:59 +00:00
|
|
|
void
|
2015-01-07 09:35:20 +00:00
|
|
|
ProgressTracker::NotifyCurrentState(IProgressObserver* aObserver)
|
2010-05-14 20:47:59 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-11-16 17:21:00 +00:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-05-11 20:42:30 +00:00
|
|
|
nsAutoCString spec;
|
|
|
|
if (image && image->GetURI()) {
|
|
|
|
image->GetURI()->GetSpec(spec);
|
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog,
|
2015-05-11 20:42:30 +00:00
|
|
|
"ProgressTracker::NotifyCurrentState", "uri", spec.get());
|
2015-01-07 09:35:20 +00:00
|
|
|
}
|
2010-07-28 21:52:34 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
aObserver->SetNotificationsDeferred(true);
|
2010-07-28 21:52:14 +00:00
|
|
|
|
2015-03-31 17:56:00 +00:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new AsyncNotifyCurrentStateRunnable(this,
|
|
|
|
aObserver);
|
2010-07-28 21:52:14 +00:00
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:26:43 +00:00
|
|
|
/**
|
|
|
|
* ImageObserverNotifier is a helper type that abstracts over the difference
|
|
|
|
* between sending notifications to all of the observers in an ObserverTable,
|
|
|
|
* and sending them to a single observer. This allows the same notification code
|
|
|
|
* to be used for both cases.
|
|
|
|
*/
|
|
|
|
template <typename T> struct ImageObserverNotifier;
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MOZ_STACK_CLASS ImageObserverNotifier<const ObserverTable*>
|
|
|
|
{
|
|
|
|
explicit ImageObserverNotifier(const ObserverTable* aObservers,
|
|
|
|
bool aIgnoreDeferral = false)
|
|
|
|
: mObservers(aObservers)
|
|
|
|
, mIgnoreDeferral(aIgnoreDeferral)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template <typename Lambda>
|
|
|
|
void operator()(Lambda aFunc)
|
|
|
|
{
|
|
|
|
for (auto iter = mObservers->ConstIter(); !iter.Done(); iter.Next()) {
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<IProgressObserver> observer = iter.Data().get();
|
2015-08-25 23:26:43 +00:00
|
|
|
if (observer &&
|
|
|
|
(mIgnoreDeferral || !observer->NotificationsDeferred())) {
|
|
|
|
aFunc(observer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
const ObserverTable* mObservers;
|
|
|
|
const bool mIgnoreDeferral;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <>
|
|
|
|
struct MOZ_STACK_CLASS ImageObserverNotifier<IProgressObserver*>
|
|
|
|
{
|
|
|
|
explicit ImageObserverNotifier(IProgressObserver* aObserver)
|
|
|
|
: mObserver(aObserver)
|
|
|
|
{ }
|
|
|
|
|
|
|
|
template <typename Lambda>
|
|
|
|
void operator()(Lambda aFunc)
|
|
|
|
{
|
|
|
|
if (mObserver && !mObserver->NotificationsDeferred()) {
|
|
|
|
aFunc(mObserver);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
IProgressObserver* mObserver;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T> void
|
|
|
|
SyncNotifyInternal(const T& aObservers,
|
|
|
|
bool aHasImage,
|
|
|
|
Progress aProgress,
|
|
|
|
const nsIntRect& aDirtyRect)
|
2010-07-28 21:52:14 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
typedef imgINotificationObserver I;
|
2015-08-25 23:26:43 +00:00
|
|
|
ImageObserverNotifier<T> notify(aObservers);
|
2015-01-07 09:35:20 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
if (aProgress & FLAG_SIZE_AVAILABLE) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::SIZE_AVAILABLE); });
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2012-10-12 01:58:24 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
if (aProgress & FLAG_ONLOAD_BLOCKED) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->BlockOnload(); });
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2012-08-13 22:58:53 +00:00
|
|
|
|
2014-11-10 20:37:35 +00:00
|
|
|
if (aHasImage) {
|
2013-01-18 21:47:17 +00:00
|
|
|
// OnFrameUpdate
|
2012-12-17 22:05:18 +00:00
|
|
|
// If there's any content in this frame at all (always true for
|
|
|
|
// vector images, true for raster images that have decoded at
|
|
|
|
// least one frame) then send OnFrameUpdate.
|
2014-12-05 23:58:00 +00:00
|
|
|
if (!aDirtyRect.IsEmpty()) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([&](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(I::FRAME_UPDATE, &aDirtyRect);
|
|
|
|
});
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
if (aProgress & FLAG_FRAME_COMPLETE) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::FRAME_COMPLETE); });
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2011-11-09 21:39:15 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
if (aProgress & FLAG_HAS_TRANSPARENCY) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::HAS_TRANSPARENCY); });
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2014-11-17 19:16:45 +00:00
|
|
|
|
2014-12-05 23:58:00 +00:00
|
|
|
if (aProgress & FLAG_IS_ANIMATED) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::IS_ANIMATED); });
|
2014-12-05 23:58:00 +00:00
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
|
|
|
|
2014-11-07 01:33:57 +00:00
|
|
|
// Send UnblockOnload before OnStopDecode and OnStopRequest. This allows
|
|
|
|
// observers that can fire events when they receive those notifications to do
|
|
|
|
// so then, instead of being forced to wait for UnblockOnload.
|
2014-11-15 04:10:47 +00:00
|
|
|
if (aProgress & FLAG_ONLOAD_UNBLOCKED) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->UnblockOnload(); });
|
2014-11-07 01:33:57 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-10 20:37:35 +00:00
|
|
|
MOZ_ASSERT(aHasImage, "Stopped decoding without ever having an image?");
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::DECODE_COMPLETE); });
|
2010-08-23 22:44:07 +00:00
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
if (aProgress & FLAG_LOAD_COMPLETE) {
|
2015-08-25 23:26:43 +00:00
|
|
|
notify([=](IProgressObserver* aObs) {
|
|
|
|
aObs->OnLoadComplete(aProgress & FLAG_LAST_PART_COMPLETE);
|
|
|
|
});
|
2013-01-18 21:47:17 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 23:17:24 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::SyncNotifyProgress(Progress aProgress,
|
2014-12-05 23:58:00 +00:00
|
|
|
const nsIntRect& aInvalidRect
|
|
|
|
/* = nsIntRect() */)
|
2013-03-01 23:17:24 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Use mObservers on main thread only");
|
2013-03-01 23:17:24 +00:00
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
// Don't unblock onload if we're not blocked.
|
2014-11-15 04:10:47 +00:00
|
|
|
Progress progress = Difference(aProgress);
|
|
|
|
if (!((mProgress | progress) & FLAG_ONLOAD_BLOCKED)) {
|
|
|
|
progress &= ~FLAG_ONLOAD_UNBLOCKED;
|
2014-11-15 04:10:47 +00:00
|
|
|
}
|
|
|
|
|
2015-01-22 01:36:20 +00:00
|
|
|
// XXX(seth): Hack to work around the fact that some observers have bugs and
|
|
|
|
// need to get onload blocking notifications multiple times. We should fix
|
|
|
|
// those observers and remove this.
|
|
|
|
if ((aProgress & FLAG_DECODE_COMPLETE) &&
|
|
|
|
(mProgress & FLAG_ONLOAD_BLOCKED) &&
|
|
|
|
(mProgress & FLAG_ONLOAD_UNBLOCKED)) {
|
|
|
|
progress |= FLAG_ONLOAD_BLOCKED | FLAG_ONLOAD_UNBLOCKED;
|
|
|
|
}
|
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
// Apply the changes.
|
2014-11-15 04:10:47 +00:00
|
|
|
mProgress |= progress;
|
2014-11-15 04:10:47 +00:00
|
|
|
|
2014-11-15 04:10:48 +00:00
|
|
|
CheckProgressConsistency(mProgress);
|
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
// Send notifications.
|
2015-08-25 23:26:43 +00:00
|
|
|
mObservers.Read([&](const ObserverTable* aTable) {
|
|
|
|
SyncNotifyInternal(aTable, HasImage(), progress, aInvalidRect);
|
|
|
|
});
|
2013-03-01 23:17:24 +00:00
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
if (progress & FLAG_HAS_ERROR) {
|
2013-01-18 21:47:17 +00:00
|
|
|
FireFailureNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 21:47:17 +00:00
|
|
|
void
|
2015-01-07 09:35:20 +00:00
|
|
|
ProgressTracker::SyncNotify(IProgressObserver* aObserver)
|
2013-01-18 21:47:17 +00:00
|
|
|
{
|
2015-01-07 09:35:20 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-05-01 01:13:14 +00:00
|
|
|
|
2013-01-18 21:47:17 +00:00
|
|
|
nsAutoCString spec;
|
2015-05-01 01:13:14 +00:00
|
|
|
if (image && image->GetURI()) {
|
|
|
|
image->GetURI()->GetSpec(spec);
|
2015-01-07 09:35:20 +00:00
|
|
|
}
|
2015-11-16 17:21:00 +00:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog,
|
2014-12-05 23:58:00 +00:00
|
|
|
"ProgressTracker::SyncNotify", "uri", spec.get());
|
2013-01-18 21:47:17 +00:00
|
|
|
|
2015-01-07 09:40:23 +00:00
|
|
|
nsIntRect rect;
|
2015-05-01 01:13:14 +00:00
|
|
|
if (image) {
|
|
|
|
if (NS_FAILED(image->GetWidth(&rect.width)) ||
|
|
|
|
NS_FAILED(image->GetHeight(&rect.height))) {
|
2015-01-07 09:40:23 +00:00
|
|
|
// Either the image has no intrinsic size, or it has an error.
|
2015-04-21 15:04:57 +00:00
|
|
|
rect = GetMaxSizedIntRect();
|
2015-01-07 09:40:23 +00:00
|
|
|
}
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
2013-01-18 21:47:17 +00:00
|
|
|
|
2015-08-25 23:26:43 +00:00
|
|
|
SyncNotifyInternal(aObserver, !!image, mProgress, rect);
|
2013-01-18 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2010-05-14 20:47:59 +00:00
|
|
|
void
|
2015-01-07 09:37:20 +00:00
|
|
|
ProgressTracker::EmulateRequestFinished(IProgressObserver* aObserver)
|
2010-05-14 20:47:59 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
2015-01-07 09:35:20 +00:00
|
|
|
"SyncNotifyState and mObservers are not threadsafe");
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<IProgressObserver> kungFuDeathGrip(aObserver);
|
2010-05-14 20:47:59 +00:00
|
|
|
|
2014-11-15 04:10:47 +00:00
|
|
|
if (mProgress & FLAG_ONLOAD_BLOCKED && !(mProgress & FLAG_ONLOAD_UNBLOCKED)) {
|
2015-01-07 09:35:20 +00:00
|
|
|
aObserver->UnblockOnload();
|
2012-10-09 16:47:14 +00:00
|
|
|
}
|
|
|
|
|
2014-11-17 22:29:56 +00:00
|
|
|
if (!(mProgress & FLAG_LOAD_COMPLETE)) {
|
2015-01-07 09:35:20 +00:00
|
|
|
aObserver->OnLoadComplete(true);
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 16:11:20 +00:00
|
|
|
void
|
2015-01-07 09:35:20 +00:00
|
|
|
ProgressTracker::AddObserver(IProgressObserver* aObserver)
|
2012-10-12 16:11:20 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<IProgressObserver> observer = aObserver;
|
2015-08-25 23:26:43 +00:00
|
|
|
|
|
|
|
mObservers.Write([=](ObserverTable* aTable) {
|
2015-09-01 14:58:37 +00:00
|
|
|
MOZ_ASSERT(!aTable->Get(observer, nullptr),
|
2015-08-25 23:26:43 +00:00
|
|
|
"Adding duplicate entry for image observer");
|
|
|
|
|
2015-09-01 14:58:37 +00:00
|
|
|
WeakPtr<IProgressObserver> weakPtr = observer.get();
|
|
|
|
aTable->Put(observer, weakPtr);
|
2015-08-25 23:26:43 +00:00
|
|
|
});
|
2012-10-12 16:11:20 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
2015-01-07 09:37:20 +00:00
|
|
|
ProgressTracker::RemoveObserver(IProgressObserver* aObserver)
|
2012-10-12 16:11:20 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<IProgressObserver> observer = aObserver;
|
2012-10-12 16:11:20 +00:00
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
// Remove the observer from the list.
|
2015-08-25 23:26:43 +00:00
|
|
|
bool removed = mObservers.Write([=](ObserverTable* aTable) {
|
2015-09-01 14:58:37 +00:00
|
|
|
bool removed = aTable->Get(observer, nullptr);
|
|
|
|
aTable->Remove(observer);
|
2015-08-25 23:26:43 +00:00
|
|
|
return removed;
|
|
|
|
});
|
2015-01-07 09:35:20 +00:00
|
|
|
|
|
|
|
// Observers can get confused if they don't get all the proper teardown
|
2012-10-12 16:11:20 +00:00
|
|
|
// notifications. Part ways on good terms.
|
2015-01-07 09:35:20 +00:00
|
|
|
if (removed && !aObserver->NotificationsDeferred()) {
|
2015-01-07 09:37:20 +00:00
|
|
|
EmulateRequestFinished(aObserver);
|
2013-01-18 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
// Make sure we don't give callbacks to an observer that isn't interested in
|
2013-01-18 21:47:18 +00:00
|
|
|
// them any more.
|
2014-11-15 04:10:47 +00:00
|
|
|
AsyncNotifyRunnable* runnable =
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get());
|
|
|
|
|
2015-01-07 09:35:20 +00:00
|
|
|
if (aObserver->NotificationsDeferred() && runnable) {
|
|
|
|
runnable->RemoveObserver(aObserver);
|
|
|
|
aObserver->SetNotificationsDeferred(false);
|
2013-01-18 21:47:18 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 16:11:20 +00:00
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2015-08-25 23:26:43 +00:00
|
|
|
uint32_t
|
|
|
|
ProgressTracker::ObserverCount() const
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
return aTable->Count();
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-18 21:47:18 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::OnUnlockedDraw()
|
2013-01-18 21:47:18 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-08-25 23:26:43 +00:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*> notify(aTable);
|
|
|
|
notify([](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(imgINotificationObserver::UNLOCKED_DRAW);
|
|
|
|
});
|
|
|
|
});
|
2014-11-15 04:06:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::ResetForNewRequest()
|
2014-11-15 04:06:19 +00:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-07 09:37:20 +00:00
|
|
|
mProgress = NoProgress;
|
2014-11-15 04:10:48 +00:00
|
|
|
CheckProgressConsistency(mProgress);
|
2010-05-14 20:47:59 +00:00
|
|
|
}
|
2012-08-13 22:58:53 +00:00
|
|
|
|
2013-02-07 22:22:38 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::OnDiscard()
|
2013-02-07 22:22:38 +00:00
|
|
|
{
|
2013-09-28 18:28:43 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-08-25 23:26:43 +00:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*> notify(aTable);
|
|
|
|
notify([](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(imgINotificationObserver::DISCARD);
|
|
|
|
});
|
|
|
|
});
|
2013-02-07 22:22:38 +00:00
|
|
|
}
|
|
|
|
|
2012-10-12 16:11:21 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::OnImageAvailable()
|
2012-10-12 16:11:21 +00:00
|
|
|
{
|
2015-03-24 02:37:45 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-16 21:15:04 +00:00
|
|
|
// Notify any imgRequestProxys that are observing us that we have an Image.
|
2015-08-25 23:26:43 +00:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*>
|
|
|
|
notify(aTable, /* aIgnoreDeferral = */ true);
|
|
|
|
notify([](IProgressObserver* aObs) {
|
|
|
|
aObs->SetHasImage();
|
|
|
|
});
|
|
|
|
});
|
2012-08-13 22:58:53 +00:00
|
|
|
}
|
2012-10-12 16:11:21 +00:00
|
|
|
|
2012-11-17 13:33:20 +00:00
|
|
|
void
|
2014-11-15 04:10:47 +00:00
|
|
|
ProgressTracker::FireFailureNotification()
|
2012-11-17 13:33:20 +00:00
|
|
|
{
|
2013-01-18 21:47:18 +00:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-19 21:28:54 +00:00
|
|
|
// Some kind of problem has happened with image decoding.
|
|
|
|
// Report the URI to net:failed-to-process-uri-conent observers.
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-05-01 01:13:14 +00:00
|
|
|
if (image) {
|
2013-09-28 18:28:42 +00:00
|
|
|
// Should be on main thread, so ok to create a new nsIURI.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
{
|
2015-10-18 05:24:48 +00:00
|
|
|
RefPtr<ImageURL> threadsafeUriData = image->GetURI();
|
2013-09-28 18:28:42 +00:00
|
|
|
uri = threadsafeUriData ? threadsafeUriData->ToIURI() : nullptr;
|
|
|
|
}
|
2013-02-02 01:06:34 +00:00
|
|
|
if (uri) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->NotifyObservers(uri, "net:failed-to-process-uri-content", nullptr);
|
|
|
|
}
|
2012-12-19 21:28:54 +00:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 13:33:20 +00:00
|
|
|
}
|
2014-11-15 04:10:47 +00:00
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|