2010-05-15 00:47:59 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04: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-15 00:47:59 +04:00
|
|
|
|
2014-08-23 00:12:38 +04:00
|
|
|
#include "ImageLogging.h"
|
2014-11-15 07:10:47 +03:00
|
|
|
#include "ProgressTracker.h"
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
#include "imgINotificationObserver.h"
|
|
|
|
#include "imgIRequest.h"
|
2010-08-14 08:09:48 +04:00
|
|
|
#include "Image.h"
|
2013-09-28 22:28:42 +04:00
|
|
|
#include "nsNetUtil.h"
|
2012-10-12 20:11:21 +04:00
|
|
|
#include "nsIObserverService.h"
|
2012-10-12 05:34:22 +04:00
|
|
|
|
2012-10-12 20:11:20 +04:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-10-12 20:11:21 +04:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-12 20:11:20 +04:00
|
|
|
|
2013-12-13 01:17:35 +04:00
|
|
|
using mozilla::WeakPtr;
|
2010-08-14 08:09:49 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
2016-12-04 01:07:10 +03:00
|
|
|
static void CheckProgressConsistency(Progress aOldProgress,
|
|
|
|
Progress aNewProgress, bool aIsMultipart) {
|
2014-11-15 07:10:48 +03:00
|
|
|
// Check preconditions for every progress bit.
|
|
|
|
|
2016-12-04 01:07:10 +03:00
|
|
|
// Error's do not get propagated from the tracker for each image part to the
|
|
|
|
// tracker for the multipart image because we don't want one bad part to
|
|
|
|
// prevent the remaining parts from showing. So we need to consider whether
|
|
|
|
// this is a tracker for a multipart image for these assertions to work.
|
|
|
|
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-18 01:29:56 +03:00
|
|
|
// No preconditions.
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_DECODE_COMPLETE) {
|
|
|
|
MOZ_ASSERT(aNewProgress & FLAG_SIZE_AVAILABLE);
|
2016-12-04 01:07:10 +03:00
|
|
|
MOZ_ASSERT(aIsMultipart ||
|
|
|
|
aNewProgress & (FLAG_FRAME_COMPLETE | FLAG_HAS_ERROR));
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_FRAME_COMPLETE) {
|
|
|
|
MOZ_ASSERT(aNewProgress & FLAG_SIZE_AVAILABLE);
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_LOAD_COMPLETE) {
|
2016-12-04 01:07:10 +03:00
|
|
|
MOZ_ASSERT(aIsMultipart ||
|
|
|
|
aNewProgress & (FLAG_SIZE_AVAILABLE | FLAG_HAS_ERROR));
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_IS_ANIMATED) {
|
|
|
|
// No preconditions; like FLAG_HAS_TRANSPARENCY, we should normally never
|
|
|
|
// discover this *after* FLAG_SIZE_AVAILABLE, but unfortunately some corrupt
|
|
|
|
// GIFs may fool us.
|
2014-11-17 22:16:45 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_HAS_TRANSPARENCY) {
|
2016-03-22 00:15:20 +03:00
|
|
|
// XXX We'd like to assert that transparency is only set during metadata
|
|
|
|
// decode but we don't have any way to assert that until bug 1254892 is
|
|
|
|
// fixed.
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_LAST_PART_COMPLETE) {
|
|
|
|
MOZ_ASSERT(aNewProgress & FLAG_LOAD_COMPLETE);
|
2014-11-15 07:10:48 +03:00
|
|
|
}
|
2016-02-25 22:12:58 +03:00
|
|
|
if (aNewProgress & FLAG_HAS_ERROR) {
|
2014-11-15 07:10:48 +03:00
|
|
|
// No preconditions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
ProgressTracker::ProgressTracker()
|
|
|
|
: mMutex("ProgressTracker::mMutex"),
|
|
|
|
mImage(nullptr),
|
2020-04-07 18:17:07 +03:00
|
|
|
mEventTarget(WrapNotNull(
|
|
|
|
nsCOMPtr<nsIEventTarget>(GetMainThreadSerialEventTarget()))),
|
2017-07-19 21:15:11 +03:00
|
|
|
mObserversWithTargets(0),
|
|
|
|
mObservers(new ObserverTable),
|
|
|
|
mProgress(NoProgress),
|
|
|
|
mIsMultipart(false) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::SetImage(Image* aImage) {
|
2017-07-19 21:15:11 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aImage, "Setting null image");
|
|
|
|
MOZ_ASSERT(!mImage, "Setting image when we already have one");
|
2010-08-24 02:44:07 +04:00
|
|
|
mImage = aImage;
|
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::ResetImage() {
|
2017-07-19 21:15:11 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(mImage, "Resetting image when it's already null!");
|
2013-09-28 22:28:44 +04:00
|
|
|
mImage = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
uint32_t ProgressTracker::GetImageStatus() const {
|
2014-11-07 04:34:00 +03:00
|
|
|
uint32_t status = imgIRequest::STATUS_NONE;
|
|
|
|
|
|
|
|
// Translate our current state to a set of imgIRequest::STATE_* flags.
|
2014-11-18 01:29:56 +03:00
|
|
|
if (mProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-07 04:34:00 +03:00
|
|
|
status |= imgIRequest::STATUS_SIZE_AVAILABLE;
|
|
|
|
}
|
2014-11-18 01:29:56 +03:00
|
|
|
if (mProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-07 04:34:00 +03:00
|
|
|
status |= imgIRequest::STATUS_DECODE_COMPLETE;
|
|
|
|
}
|
2014-11-18 01:29:56 +03:00
|
|
|
if (mProgress & FLAG_FRAME_COMPLETE) {
|
2014-11-07 04:34:00 +03:00
|
|
|
status |= imgIRequest::STATUS_FRAME_COMPLETE;
|
|
|
|
}
|
2014-11-18 01:29:56 +03:00
|
|
|
if (mProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-07 04:34:00 +03:00
|
|
|
status |= imgIRequest::STATUS_LOAD_COMPLETE;
|
|
|
|
}
|
2014-11-25 10:42:43 +03:00
|
|
|
if (mProgress & FLAG_IS_ANIMATED) {
|
|
|
|
status |= imgIRequest::STATUS_IS_ANIMATED;
|
|
|
|
}
|
|
|
|
if (mProgress & FLAG_HAS_TRANSPARENCY) {
|
|
|
|
status |= imgIRequest::STATUS_HAS_TRANSPARENCY;
|
|
|
|
}
|
2014-11-15 07:10:47 +03:00
|
|
|
if (mProgress & FLAG_HAS_ERROR) {
|
2014-11-07 04:34:00 +03:00
|
|
|
status |= imgIRequest::STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
|
|
|
|
2010-07-29 01:52:14 +04:00
|
|
|
// A helper class to allow us to call SyncNotify asynchronously.
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncNotifyRunnable : public Runnable {
|
2010-07-29 01:52:14 +04:00
|
|
|
public:
|
2014-11-15 07:10:47 +03:00
|
|
|
AsyncNotifyRunnable(ProgressTracker* aTracker, IProgressObserver* aObserver)
|
2017-02-11 09:11:48 +03:00
|
|
|
: Runnable("ProgressTracker::AsyncNotifyRunnable"), mTracker(aTracker) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
|
|
|
MOZ_ASSERT(aTracker, "aTracker should not be null");
|
2015-01-07 12:35:20 +03:00
|
|
|
MOZ_ASSERT(aObserver, "aObserver should not be null");
|
|
|
|
mObservers.AppendElement(aObserver);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
|
|
|
MOZ_ASSERT(mTracker, "mTracker should not be null");
|
2015-01-07 12:35:20 +03:00
|
|
|
for (uint32_t i = 0; i < mObservers.Length(); ++i) {
|
2018-02-07 15:27:27 +03:00
|
|
|
mObservers[i]->ClearPendingNotify();
|
2015-01-07 12:35:20 +03:00
|
|
|
mTracker->SyncNotify(mObservers[i]);
|
2010-08-12 19:59:28 +04:00
|
|
|
}
|
2010-07-29 01:52:14 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
mTracker->mRunnable = nullptr;
|
2010-07-29 01:52:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void AddObserver(IProgressObserver* aObserver) {
|
|
|
|
mObservers.AppendElement(aObserver);
|
2010-08-12 19:59:28 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void RemoveObserver(IProgressObserver* aObserver) {
|
|
|
|
mObservers.RemoveElement(aObserver);
|
2013-01-19 01:47:18 +04:00
|
|
|
}
|
|
|
|
|
2010-07-29 01:52:14 +04:00
|
|
|
private:
|
2014-11-15 07:10:47 +03:00
|
|
|
friend class ProgressTracker;
|
2010-08-12 19:59:28 +04:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> mTracker;
|
|
|
|
nsTArray<RefPtr<IProgressObserver>> mObservers;
|
2010-07-29 01:52:14 +04:00
|
|
|
};
|
|
|
|
|
2019-05-18 02:09:38 +03:00
|
|
|
ProgressTracker::MediumHighRunnable::MediumHighRunnable(
|
|
|
|
already_AddRefed<AsyncNotifyRunnable>&& aEvent)
|
|
|
|
: PrioritizableRunnable(std::move(aEvent),
|
|
|
|
nsIRunnablePriority::PRIORITY_MEDIUMHIGH) {}
|
|
|
|
|
|
|
|
void ProgressTracker::MediumHighRunnable::AddObserver(
|
|
|
|
IProgressObserver* aObserver) {
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get())->AddObserver(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ProgressTracker::MediumHighRunnable::RemoveObserver(
|
|
|
|
IProgressObserver* aObserver) {
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get())->RemoveObserver(aObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* static */
|
|
|
|
already_AddRefed<ProgressTracker::MediumHighRunnable>
|
|
|
|
ProgressTracker::MediumHighRunnable::Create(
|
|
|
|
already_AddRefed<AsyncNotifyRunnable>&& aEvent) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
RefPtr<ProgressTracker::MediumHighRunnable> event(
|
|
|
|
new ProgressTracker::MediumHighRunnable(std::move(aEvent)));
|
|
|
|
return event.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void ProgressTracker::Notify(IProgressObserver* aObserver) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
if (aObserver->NotificationsDeferred()) {
|
|
|
|
// There is a pending notification, or the observer isn't ready yet.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "ProgressTracker::Notify async", "uri", image);
|
2012-12-20 01:28:54 +04:00
|
|
|
}
|
2010-07-29 01:52:34 +04:00
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
aObserver->MarkPendingNotify();
|
2010-07-29 01:52:14 +04:00
|
|
|
|
2015-01-07 12:35:20 +03: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.
|
2019-05-18 02:09:38 +03:00
|
|
|
if (mRunnable) {
|
|
|
|
mRunnable->AddObserver(aObserver);
|
2010-08-12 19:59:28 +04:00
|
|
|
} else {
|
2019-05-18 02:09:38 +03:00
|
|
|
RefPtr<AsyncNotifyRunnable> ev = new AsyncNotifyRunnable(this, aObserver);
|
|
|
|
mRunnable = ProgressTracker::MediumHighRunnable::Create(ev.forget());
|
2017-07-19 21:15:11 +03:00
|
|
|
mEventTarget->Dispatch(mRunnable, NS_DISPATCH_NORMAL);
|
2010-08-12 19:59:28 +04:00
|
|
|
}
|
2010-07-29 01:52:14 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// A helper class to allow us to call SyncNotify asynchronously for a given,
|
|
|
|
// fixed, state.
|
2016-04-26 03:23:21 +03:00
|
|
|
class AsyncNotifyCurrentStateRunnable : public Runnable {
|
2010-07-29 01:52:14 +04:00
|
|
|
public:
|
2014-11-15 07:10:47 +03:00
|
|
|
AsyncNotifyCurrentStateRunnable(ProgressTracker* aProgressTracker,
|
2015-01-07 12:35:20 +03:00
|
|
|
IProgressObserver* aObserver)
|
2017-06-12 22:34:10 +03:00
|
|
|
: Runnable("image::AsyncNotifyCurrentStateRunnable"),
|
|
|
|
mProgressTracker(aProgressTracker),
|
2015-01-07 12:35:20 +03:00
|
|
|
mObserver(aObserver) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
2014-11-15 07:10:47 +03:00
|
|
|
MOZ_ASSERT(mProgressTracker, "mProgressTracker should not be null");
|
2015-01-07 12:35:20 +03:00
|
|
|
MOZ_ASSERT(mObserver, "mObserver should not be null");
|
2014-11-15 07:10:47 +03:00
|
|
|
mImage = mProgressTracker->GetImage();
|
2013-09-28 22:28:43 +04:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2016-08-08 05:18:10 +03:00
|
|
|
NS_IMETHOD Run() override {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
2018-02-07 15:27:27 +03:00
|
|
|
mObserver->ClearPendingNotify();
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
mProgressTracker->SyncNotify(mObserver);
|
2010-07-29 01:52:14 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-29 01:52:14 +04:00
|
|
|
private:
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<ProgressTracker> mProgressTracker;
|
|
|
|
RefPtr<IProgressObserver> mObserver;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2010-07-29 01:52:14 +04: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 08:24:48 +03:00
|
|
|
RefPtr<Image> mImage;
|
2010-07-29 01:52:14 +04:00
|
|
|
};
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void ProgressTracker::NotifyCurrentState(IProgressObserver* aObserver) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
if (aObserver->NotificationsDeferred()) {
|
|
|
|
// There is a pending notification, or the observer isn't ready yet.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-11-16 20:21:00 +03:00
|
|
|
if (MOZ_LOG_TEST(gImgLog, LogLevel::Debug)) {
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_FUNC_WITH_PARAM(gImgLog, "ProgressTracker::NotifyCurrentState", "uri",
|
|
|
|
image);
|
2015-01-07 12:35:20 +03:00
|
|
|
}
|
2010-07-29 01:52:34 +04:00
|
|
|
|
2018-02-07 15:27:27 +03:00
|
|
|
aObserver->MarkPendingNotify();
|
2010-07-29 01:52:14 +04:00
|
|
|
|
2015-03-31 20:56:00 +03:00
|
|
|
nsCOMPtr<nsIRunnable> ev =
|
|
|
|
new AsyncNotifyCurrentStateRunnable(this, aObserver);
|
2017-07-19 21:15:11 +03:00
|
|
|
mEventTarget->Dispatch(ev.forget(), NS_DISPATCH_NORMAL);
|
2010-07-29 01:52:14 +04:00
|
|
|
}
|
|
|
|
|
2015-08-26 02:26:43 +03: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 08:24:48 +03:00
|
|
|
RefPtr<IProgressObserver> observer = iter.Data().get();
|
2015-08-26 02:26:43 +03: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) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
typedef imgINotificationObserver I;
|
2015-08-26 02:26:43 +03:00
|
|
|
ImageObserverNotifier<T> notify(aObservers);
|
2015-01-07 12:35:20 +03:00
|
|
|
|
2014-12-06 02:58:00 +03:00
|
|
|
if (aProgress & FLAG_SIZE_AVAILABLE) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::SIZE_AVAILABLE); });
|
2014-12-06 02:58:00 +03:00
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2014-11-10 23:37:35 +03:00
|
|
|
if (aHasImage) {
|
2013-01-19 01:47:17 +04:00
|
|
|
// OnFrameUpdate
|
2012-12-18 02:05:18 +04: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-06 02:58:00 +03:00
|
|
|
if (!aDirtyRect.IsEmpty()) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([&](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(I::FRAME_UPDATE, &aDirtyRect);
|
|
|
|
});
|
2014-12-06 02:58:00 +03:00
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2014-12-06 02:58:00 +03:00
|
|
|
if (aProgress & FLAG_FRAME_COMPLETE) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::FRAME_COMPLETE); });
|
2014-12-06 02:58:00 +03:00
|
|
|
}
|
2011-11-10 01:39:15 +04:00
|
|
|
|
2014-12-06 02:58:00 +03:00
|
|
|
if (aProgress & FLAG_HAS_TRANSPARENCY) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify(
|
|
|
|
[](IProgressObserver* aObs) { aObs->Notify(I::HAS_TRANSPARENCY); });
|
2014-12-06 02:58:00 +03:00
|
|
|
}
|
2014-11-17 22:16:45 +03:00
|
|
|
|
2014-12-06 02:58:00 +03:00
|
|
|
if (aProgress & FLAG_IS_ANIMATED) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::IS_ANIMATED); });
|
2014-12-06 02:58:00 +03:00
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
|
|
|
|
2014-11-18 01:29:56 +03:00
|
|
|
if (aProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-10 23:37:35 +03:00
|
|
|
MOZ_ASSERT(aHasImage, "Stopped decoding without ever having an image?");
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([](IProgressObserver* aObs) { aObs->Notify(I::DECODE_COMPLETE); });
|
2010-08-24 02:44:07 +04:00
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2014-11-18 01:29:56 +03:00
|
|
|
if (aProgress & FLAG_LOAD_COMPLETE) {
|
2015-08-26 02:26:43 +03:00
|
|
|
notify([=](IProgressObserver* aObs) {
|
|
|
|
aObs->OnLoadComplete(aProgress & FLAG_LAST_PART_COMPLETE);
|
|
|
|
});
|
2013-01-19 01:47:17 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::SyncNotifyProgress(Progress aProgress,
|
2014-12-06 02:58:00 +03:00
|
|
|
const nsIntRect& aInvalidRect
|
|
|
|
/* = nsIntRect() */) {
|
2015-01-07 12:35:20 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Use mObservers on main thread only");
|
2013-03-02 03:17:24 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
Progress progress = Difference(aProgress);
|
2016-12-04 01:07:10 +03:00
|
|
|
CheckProgressConsistency(mProgress, mProgress | progress, mIsMultipart);
|
2016-02-25 22:12:58 +03:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
// Apply the changes.
|
2014-11-15 07:10:47 +03:00
|
|
|
mProgress |= progress;
|
2014-11-15 07:10:47 +03:00
|
|
|
|
|
|
|
// Send notifications.
|
2015-08-26 02:26:43 +03:00
|
|
|
mObservers.Read([&](const ObserverTable* aTable) {
|
|
|
|
SyncNotifyInternal(aTable, HasImage(), progress, aInvalidRect);
|
|
|
|
});
|
2013-03-02 03:17:24 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
if (progress & FLAG_HAS_ERROR) {
|
2013-01-19 01:47:17 +04:00
|
|
|
FireFailureNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void ProgressTracker::SyncNotify(IProgressObserver* aObserver) {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2018-06-06 03:42:57 +03:00
|
|
|
LOG_SCOPE_WITH_PARAM(gImgLog, "ProgressTracker::SyncNotify", "uri", image);
|
2013-01-19 01:47:17 +04:00
|
|
|
|
2015-01-07 12:40:23 +03:00
|
|
|
nsIntRect rect;
|
2015-05-01 04:13:14 +03:00
|
|
|
if (image) {
|
2017-12-21 00:46:28 +03:00
|
|
|
int32_t width, height;
|
|
|
|
if (NS_FAILED(image->GetWidth(&width)) ||
|
|
|
|
NS_FAILED(image->GetHeight(&height))) {
|
2015-01-07 12:40:23 +03:00
|
|
|
// Either the image has no intrinsic size, or it has an error.
|
2015-04-21 18:04:57 +03:00
|
|
|
rect = GetMaxSizedIntRect();
|
2017-12-21 00:46:28 +03:00
|
|
|
} else {
|
|
|
|
rect.SizeTo(width, height);
|
2015-01-07 12:40:23 +03:00
|
|
|
}
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
2013-01-19 01:47:17 +04:00
|
|
|
|
2015-08-26 02:26:43 +03:00
|
|
|
SyncNotifyInternal(aObserver, !!image, mProgress, rect);
|
2013-01-19 01:47:18 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:37:20 +03:00
|
|
|
void ProgressTracker::EmulateRequestFinished(IProgressObserver* aObserver) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
2015-01-07 12:35:20 +03:00
|
|
|
"SyncNotifyState and mObservers are not threadsafe");
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IProgressObserver> kungFuDeathGrip(aObserver);
|
2010-05-15 00:47:59 +04:00
|
|
|
|
2014-11-18 01:29:56 +03:00
|
|
|
if (!(mProgress & FLAG_LOAD_COMPLETE)) {
|
2015-01-07 12:35:20 +03:00
|
|
|
aObserver->OnLoadComplete(true);
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
already_AddRefed<nsIEventTarget> ProgressTracker::GetEventTarget() const {
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
nsCOMPtr<nsIEventTarget> target = mEventTarget;
|
|
|
|
return target.forget();
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
void ProgressTracker::AddObserver(IProgressObserver* aObserver) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IProgressObserver> observer = aObserver;
|
2015-08-26 02:26:43 +03:00
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target = observer->GetEventTarget();
|
|
|
|
if (target) {
|
|
|
|
if (mObserversWithTargets == 0) {
|
|
|
|
// On the first observer with a target (i.e. listener), always accept its
|
|
|
|
// event target; this may be for a specific DocGroup, or it may be the
|
|
|
|
// unlabelled main thread target.
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
mEventTarget = WrapNotNull(target);
|
|
|
|
} else if (mEventTarget.get() != target.get()) {
|
|
|
|
// If a subsequent observer comes in with a different target, we need to
|
|
|
|
// switch to use the unlabelled main thread target, if we haven't already.
|
|
|
|
MutexAutoLock lock(mMutex);
|
|
|
|
nsCOMPtr<nsIEventTarget> mainTarget(do_GetMainThread());
|
|
|
|
mEventTarget = WrapNotNull(mainTarget);
|
|
|
|
}
|
|
|
|
++mObserversWithTargets;
|
|
|
|
}
|
|
|
|
|
2015-08-26 02:26:43 +03:00
|
|
|
mObservers.Write([=](ObserverTable* aTable) {
|
2015-09-01 17:58:37 +03:00
|
|
|
MOZ_ASSERT(!aTable->Get(observer, nullptr),
|
2015-08-26 02:26:43 +03:00
|
|
|
"Adding duplicate entry for image observer");
|
|
|
|
|
2015-09-01 17:58:37 +03:00
|
|
|
WeakPtr<IProgressObserver> weakPtr = observer.get();
|
|
|
|
aTable->Put(observer, weakPtr);
|
2015-08-26 02:26:43 +03:00
|
|
|
});
|
2017-07-19 21:15:11 +03:00
|
|
|
|
|
|
|
MOZ_ASSERT(mObserversWithTargets <= ObserverCount());
|
2012-10-12 20:11:20 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:37:20 +03:00
|
|
|
bool ProgressTracker::RemoveObserver(IProgressObserver* aObserver) {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<IProgressObserver> observer = aObserver;
|
2012-10-12 20:11:20 +04:00
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
// Remove the observer from the list.
|
2017-07-19 21:15:11 +03:00
|
|
|
bool removed = mObservers.Write(
|
|
|
|
[observer](ObserverTable* aTable) { return aTable->Remove(observer); });
|
2015-01-07 12:35:20 +03:00
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
// Sometimes once an image is decoded, and all of its observers removed, a new
|
|
|
|
// document may request the same image. Thus we need to clear our event target
|
|
|
|
// state when the last observer is removed, so that we select the most
|
|
|
|
// appropriate event target when a new observer is added. Since the event
|
|
|
|
// target may have changed (e.g. due to the scheduler group going away before
|
|
|
|
// we were removed), so we should be cautious comparing this target against
|
|
|
|
// anything at this stage.
|
|
|
|
if (removed) {
|
|
|
|
nsCOMPtr<nsIEventTarget> target = observer->GetEventTarget();
|
|
|
|
if (target) {
|
|
|
|
MOZ_ASSERT(mObserversWithTargets > 0);
|
|
|
|
--mObserversWithTargets;
|
|
|
|
|
2020-04-07 18:17:07 +03:00
|
|
|
// If we've shutdown the main thread there's no need to update
|
|
|
|
// event targets.
|
|
|
|
if ((mObserversWithTargets == 0) && !gXPCOMThreadsShutDown) {
|
2017-07-19 21:15:11 +03:00
|
|
|
MutexAutoLock lock(mMutex);
|
2020-04-07 18:17:07 +03:00
|
|
|
nsCOMPtr<nsIEventTarget> target(do_GetMainThread());
|
2017-07-19 21:15:11 +03:00
|
|
|
mEventTarget = WrapNotNull(target);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mObserversWithTargets <= ObserverCount());
|
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
// Observers can get confused if they don't get all the proper teardown
|
2012-10-12 20:11:20 +04:00
|
|
|
// notifications. Part ways on good terms.
|
2015-01-07 12:35:20 +03:00
|
|
|
if (removed && !aObserver->NotificationsDeferred()) {
|
2015-01-07 12:37:20 +03:00
|
|
|
EmulateRequestFinished(aObserver);
|
2013-01-19 01:47:18 +04:00
|
|
|
}
|
|
|
|
|
2015-01-07 12:35:20 +03:00
|
|
|
// Make sure we don't give callbacks to an observer that isn't interested in
|
2013-01-19 01:47:18 +04:00
|
|
|
// them any more.
|
2019-05-18 02:09:38 +03:00
|
|
|
if (aObserver->NotificationsDeferred() && mRunnable) {
|
|
|
|
mRunnable->RemoveObserver(aObserver);
|
2018-02-07 15:27:27 +03:00
|
|
|
aObserver->ClearPendingNotify();
|
2013-01-19 01:47:18 +04:00
|
|
|
}
|
|
|
|
|
2012-10-12 20:11:20 +04:00
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2015-08-26 02:26:43 +03:00
|
|
|
uint32_t ProgressTracker::ObserverCount() const {
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
return mObservers.Read(
|
|
|
|
[](const ObserverTable* aTable) { return aTable->Count(); });
|
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::OnUnlockedDraw() {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-08-26 02:26:43 +03:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*> notify(aTable);
|
|
|
|
notify([](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(imgINotificationObserver::UNLOCKED_DRAW);
|
|
|
|
});
|
|
|
|
});
|
2014-11-15 07:06:19 +03:00
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::ResetForNewRequest() {
|
2014-11-15 07:06:19 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-01-07 12:37:20 +03:00
|
|
|
mProgress = NoProgress;
|
2010-05-15 00:47:59 +04:00
|
|
|
}
|
2012-08-14 02:58:53 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::OnDiscard() {
|
2013-09-28 22:28:43 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-08-26 02:26:43 +03:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*> notify(aTable);
|
|
|
|
notify([](IProgressObserver* aObs) {
|
|
|
|
aObs->Notify(imgINotificationObserver::DISCARD);
|
|
|
|
});
|
|
|
|
});
|
2013-02-08 02:22:38 +04:00
|
|
|
}
|
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::OnImageAvailable() {
|
2015-03-24 05:37:45 +03:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2015-02-17 00:15:04 +03:00
|
|
|
// Notify any imgRequestProxys that are observing us that we have an Image.
|
2015-08-26 02:26:43 +03:00
|
|
|
mObservers.Read([](const ObserverTable* aTable) {
|
|
|
|
ImageObserverNotifier<const ObserverTable*> notify(
|
|
|
|
aTable, /* aIgnoreDeferral = */ true);
|
|
|
|
notify([](IProgressObserver* aObs) { aObs->SetHasImage(); });
|
|
|
|
});
|
2012-08-14 02:58:53 +04:00
|
|
|
}
|
2012-10-12 20:11:21 +04:00
|
|
|
|
2014-11-15 07:10:47 +03:00
|
|
|
void ProgressTracker::FireFailureNotification() {
|
2013-01-19 01:47:18 +04:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-20 01:28:54 +04:00
|
|
|
// Some kind of problem has happened with image decoding.
|
|
|
|
// Report the URI to net:failed-to-process-uri-conent observers.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<Image> image = GetImage();
|
2015-05-01 04:13:14 +03:00
|
|
|
if (image) {
|
2013-09-28 22:28:42 +04:00
|
|
|
// Should be on main thread, so ok to create a new nsIURI.
|
2018-06-06 03:42:57 +03:00
|
|
|
nsCOMPtr<nsIURI> uri = image->GetURI();
|
2013-02-02 05:06:34 +04:00
|
|
|
if (uri) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->NotifyObservers(uri, "net:failed-to-process-uri-content", nullptr);
|
|
|
|
}
|
2012-12-20 01:28:54 +04:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 17:33:20 +04:00
|
|
|
}
|
2014-11-15 07:10:47 +03:00
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|