2010-05-14 13:47:59 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
2012-05-21 04:12:37 -07: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 13:47:59 -07:00
|
|
|
|
2014-08-22 13:12:38 -07:00
|
|
|
#include "ImageLogging.h"
|
2014-11-14 20:10:47 -08:00
|
|
|
#include "ProgressTracker.h"
|
2010-05-14 13:47:59 -07:00
|
|
|
|
|
|
|
#include "imgIContainer.h"
|
|
|
|
#include "imgRequestProxy.h"
|
2010-08-13 21:09:48 -07:00
|
|
|
#include "Image.h"
|
2013-09-28 11:28:42 -07:00
|
|
|
#include "nsNetUtil.h"
|
2012-10-12 09:11:21 -07:00
|
|
|
#include "nsIObserverService.h"
|
2012-10-11 18:34:22 -07:00
|
|
|
|
2012-10-12 09:11:20 -07:00
|
|
|
#include "mozilla/Assertions.h"
|
2012-10-12 09:11:21 -07:00
|
|
|
#include "mozilla/Services.h"
|
2012-10-12 09:11:20 -07:00
|
|
|
|
2013-12-12 13:17:35 -08:00
|
|
|
using mozilla::WeakPtr;
|
2010-08-13 21:09:49 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
namespace mozilla {
|
|
|
|
namespace image {
|
|
|
|
|
|
|
|
ProgressTrackerInit::ProgressTrackerInit(Image* aImage,
|
|
|
|
ProgressTracker* aTracker)
|
2013-09-28 11:28:44 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
if (aTracker) {
|
|
|
|
mTracker = aTracker;
|
|
|
|
mTracker->SetImage(aImage);
|
|
|
|
} else {
|
2014-11-14 20:10:47 -08:00
|
|
|
mTracker = new ProgressTracker(aImage);
|
2013-09-28 11:28:44 -07:00
|
|
|
}
|
2014-11-14 20:10:47 -08:00
|
|
|
aImage->SetProgressTracker(mTracker);
|
2013-09-28 11:28:44 -07:00
|
|
|
MOZ_ASSERT(mTracker);
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTrackerInit::~ProgressTrackerInit()
|
2013-09-28 11:28:44 -07:00
|
|
|
{
|
|
|
|
mTracker->ResetImage();
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:48 -08:00
|
|
|
static void
|
|
|
|
CheckProgressConsistency(Progress aProgress)
|
|
|
|
{
|
|
|
|
// Check preconditions for every progress bit.
|
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-17 14:29:56 -08:00
|
|
|
// No preconditions.
|
2014-11-14 20:10:48 -08:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_DECODE_STARTED) {
|
2014-11-17 14:29:56 -08:00
|
|
|
// No preconditions.
|
2014-11-14 20:10:48 -08:00
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-14 20:10:48 -08:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_DECODE_STARTED);
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_FRAME_COMPLETE) {
|
2014-11-14 20:10:48 -08:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_DECODE_STARTED);
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-17 14:29:56 -08:00
|
|
|
// No preconditions.
|
2014-11-14 20:10:48 -08:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_ONLOAD_BLOCKED) {
|
|
|
|
if (aProgress & FLAG_IS_MULTIPART) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_ONLOAD_UNBLOCKED);
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_DECODE_STARTED);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aProgress & FLAG_ONLOAD_UNBLOCKED) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_ONLOAD_BLOCKED);
|
2014-11-17 14:29:56 -08:00
|
|
|
MOZ_ASSERT(aProgress & (FLAG_FRAME_COMPLETE |
|
2014-11-14 20:10:48 -08:00
|
|
|
FLAG_IS_MULTIPART |
|
|
|
|
FLAG_HAS_ERROR));
|
|
|
|
}
|
|
|
|
if (aProgress & FLAG_IS_ANIMATED) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_DECODE_STARTED);
|
2014-11-17 14:29:56 -08:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_SIZE_AVAILABLE);
|
2014-11-17 11:16:45 -08:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_HAS_TRANSPARENCY) {
|
2014-11-17 14:29:56 -08:00
|
|
|
MOZ_ASSERT(aProgress & FLAG_SIZE_AVAILABLE);
|
2014-11-14 20:10:48 -08:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_IS_MULTIPART) {
|
|
|
|
// No preconditions.
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_LAST_PART_COMPLETE) {
|
|
|
|
MOZ_ASSERT(aProgress & FLAG_LOAD_COMPLETE);
|
2014-11-14 20:10:48 -08:00
|
|
|
}
|
|
|
|
if (aProgress & FLAG_HAS_ERROR) {
|
|
|
|
// No preconditions.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:44:07 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::SetImage(Image* aImage)
|
2010-08-23 15:44:07 -07:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(aImage, "Setting null image");
|
|
|
|
NS_ABORT_IF_FALSE(!mImage, "Setting image when we already have one");
|
|
|
|
mImage = aImage;
|
|
|
|
}
|
|
|
|
|
2013-09-28 11:28:44 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::ResetImage()
|
2013-09-28 11:28:44 -07:00
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mImage, "Resetting image when it's already null!");
|
|
|
|
mImage = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
void ProgressTracker::SetIsMultipart()
|
2014-11-06 17:33:59 -08:00
|
|
|
{
|
2014-11-14 20:10:48 -08:00
|
|
|
if (mProgress & FLAG_IS_MULTIPART) {
|
|
|
|
return;
|
2014-11-14 20:06:19 -08:00
|
|
|
}
|
2014-11-14 20:10:48 -08:00
|
|
|
|
|
|
|
MOZ_ASSERT(!(mProgress & FLAG_ONLOAD_BLOCKED),
|
|
|
|
"Blocked onload before we knew we were multipart?");
|
|
|
|
|
|
|
|
// Set the MULTIPART flag and ensure that we never block onload.
|
|
|
|
mProgress |= FLAG_IS_MULTIPART | FLAG_ONLOAD_BLOCKED | FLAG_ONLOAD_UNBLOCKED;
|
2014-11-14 20:10:48 -08:00
|
|
|
|
|
|
|
CheckProgressConsistency(mProgress);
|
2014-11-06 17:33:59 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::IsLoading() const
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
|
|
|
// Checking for whether OnStopRequest has fired allows us to say we're
|
|
|
|
// loading before OnStartRequest gets called, letting the request properly
|
|
|
|
// get removed from the cache in certain cases.
|
2014-11-17 14:29:56 -08:00
|
|
|
return !(mProgress & FLAG_LOAD_COMPLETE);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::GetImageStatus() const
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2014-11-06 17:34:00 -08:00
|
|
|
uint32_t status = imgIRequest::STATUS_NONE;
|
|
|
|
|
|
|
|
// Translate our current state to a set of imgIRequest::STATE_* flags.
|
2014-11-17 14:29:56 -08:00
|
|
|
if (mProgress & FLAG_SIZE_AVAILABLE) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_SIZE_AVAILABLE;
|
|
|
|
}
|
2014-11-14 20:10:47 -08:00
|
|
|
if (mProgress & FLAG_DECODE_STARTED) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_DECODE_STARTED;
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (mProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_DECODE_COMPLETE;
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (mProgress & FLAG_FRAME_COMPLETE) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_FRAME_COMPLETE;
|
|
|
|
}
|
2014-11-17 14:29:56 -08:00
|
|
|
if (mProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_LOAD_COMPLETE;
|
|
|
|
}
|
2014-11-24 23:42:43 -08:00
|
|
|
if (mProgress & FLAG_IS_ANIMATED) {
|
|
|
|
status |= imgIRequest::STATUS_IS_ANIMATED;
|
|
|
|
}
|
|
|
|
if (mProgress & FLAG_HAS_TRANSPARENCY) {
|
|
|
|
status |= imgIRequest::STATUS_HAS_TRANSPARENCY;
|
|
|
|
}
|
2014-11-14 20:10:47 -08:00
|
|
|
if (mProgress & FLAG_HAS_ERROR) {
|
2014-11-06 17:34:00 -08:00
|
|
|
status |= imgIRequest::STATUS_ERROR;
|
|
|
|
}
|
|
|
|
|
|
|
|
return status;
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2010-07-28 14:52:14 -07:00
|
|
|
// A helper class to allow us to call SyncNotify asynchronously.
|
2014-11-14 20:10:47 -08:00
|
|
|
class AsyncNotifyRunnable : public nsRunnable
|
2010-07-28 14:52:14 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-11-14 20:10:47 -08:00
|
|
|
AsyncNotifyRunnable(ProgressTracker* aTracker,
|
|
|
|
imgRequestProxy* aRequestProxy)
|
2012-12-19 13:28:54 -08:00
|
|
|
: mTracker(aTracker)
|
2010-08-12 08:59:28 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
|
|
|
MOZ_ASSERT(aTracker, "aTracker should not be null");
|
2014-11-14 20:10:47 -08:00
|
|
|
MOZ_ASSERT(aRequestProxy, "aRequestProxy should not be null");
|
2012-12-19 13:28:54 -08:00
|
|
|
mProxies.AppendElement(aRequestProxy);
|
2010-08-12 08:59:28 -07:00
|
|
|
}
|
2010-07-28 14:52:14 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
|
|
|
MOZ_ASSERT(mTracker, "mTracker should not be null");
|
2012-08-22 08:56:38 -07:00
|
|
|
for (uint32_t i = 0; i < mProxies.Length(); ++i) {
|
2011-10-17 07:59:28 -07:00
|
|
|
mProxies[i]->SetNotificationsDeferred(false);
|
2012-12-19 13:28:54 -08:00
|
|
|
mTracker->SyncNotify(mProxies[i]);
|
2010-08-12 08:59:28 -07:00
|
|
|
}
|
2010-07-28 14:52:14 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
mTracker->mRunnable = nullptr;
|
2010-07-28 14:52:14 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-08-12 08:59:28 -07:00
|
|
|
void AddProxy(imgRequestProxy* aRequestProxy)
|
|
|
|
{
|
|
|
|
mProxies.AppendElement(aRequestProxy);
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
void RemoveProxy(imgRequestProxy* aRequestProxy)
|
|
|
|
{
|
|
|
|
mProxies.RemoveElement(aRequestProxy);
|
|
|
|
}
|
|
|
|
|
2010-07-28 14:52:14 -07:00
|
|
|
private:
|
2014-11-14 20:10:47 -08:00
|
|
|
friend class ProgressTracker;
|
2010-08-12 08:59:28 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
nsRefPtr<ProgressTracker> mTracker;
|
|
|
|
nsTArray<nsRefPtr<imgRequestProxy>> mProxies;
|
2010-07-28 14:52:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::Notify(imgRequestProxy* proxy)
|
2010-07-28 14:52:14 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "imgRequestProxy is not threadsafe");
|
2010-07-28 14:52:34 -07:00
|
|
|
#ifdef PR_LOGGING
|
2013-09-28 11:28:44 -07:00
|
|
|
if (mImage && mImage->GetURI()) {
|
|
|
|
nsRefPtr<ImageURL> uri(mImage->GetURI());
|
2012-12-19 13:28:54 -08:00
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
2014-11-14 20:10:47 -08:00
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "ProgressTracker::Notify async", "uri", spec.get());
|
2012-12-19 13:28:54 -08:00
|
|
|
} else {
|
2014-11-14 20:10:47 -08:00
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "ProgressTracker::Notify async", "uri", "<unknown>");
|
2012-12-19 13:28:54 -08:00
|
|
|
}
|
2010-07-28 14:52:34 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
proxy->SetNotificationsDeferred(true);
|
2010-07-28 14:52:14 -07:00
|
|
|
|
2010-08-12 08:59:28 -07:00
|
|
|
// If we have an existing runnable that we can use, we just append this proxy
|
|
|
|
// to its list of proxies to be notified. This ensures we don't unnecessarily
|
|
|
|
// delay onload.
|
2014-11-14 20:10:47 -08:00
|
|
|
AsyncNotifyRunnable* runnable =
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get());
|
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
if (runnable) {
|
2010-08-12 08:59:28 -07:00
|
|
|
runnable->AddProxy(proxy);
|
|
|
|
} else {
|
2014-11-14 20:10:47 -08:00
|
|
|
mRunnable = new AsyncNotifyRunnable(this, proxy);
|
|
|
|
NS_DispatchToCurrentThread(mRunnable);
|
2010-08-12 08:59:28 -07:00
|
|
|
}
|
2010-07-28 14:52:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// A helper class to allow us to call SyncNotify asynchronously for a given,
|
|
|
|
// fixed, state.
|
2014-11-14 20:10:47 -08:00
|
|
|
class AsyncNotifyCurrentStateRunnable : public nsRunnable
|
2010-07-28 14:52:14 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-11-14 20:10:47 -08:00
|
|
|
AsyncNotifyCurrentStateRunnable(ProgressTracker* aProgressTracker,
|
|
|
|
imgRequestProxy* aProxy)
|
|
|
|
: mProgressTracker(aProgressTracker)
|
|
|
|
, mProxy(aProxy)
|
2013-09-28 11:28:43 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
2014-11-14 20:10:47 -08:00
|
|
|
MOZ_ASSERT(mProgressTracker, "mProgressTracker should not be null");
|
|
|
|
MOZ_ASSERT(mProxy, "mProxy should not be null");
|
|
|
|
mImage = mProgressTracker->GetImage();
|
2013-09-28 11:28:43 -07:00
|
|
|
}
|
2010-07-28 14:52:14 -07:00
|
|
|
|
|
|
|
NS_IMETHOD Run()
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be running on the main thread");
|
2011-10-17 07:59:28 -07:00
|
|
|
mProxy->SetNotificationsDeferred(false);
|
2010-07-28 14:52:14 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
mProgressTracker->SyncNotify(mProxy);
|
2010-07-28 14:52:14 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2014-11-14 20:10:47 -08:00
|
|
|
nsRefPtr<ProgressTracker> mProgressTracker;
|
|
|
|
nsRefPtr<imgRequestProxy> mProxy;
|
|
|
|
|
2010-07-28 14:52:14 -07: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.
|
2010-08-13 21:09:49 -07:00
|
|
|
nsRefPtr<Image> mImage;
|
2010-07-28 14:52:14 -07:00
|
|
|
};
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::NotifyCurrentState(imgRequestProxy* proxy)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "imgRequestProxy is not threadsafe");
|
2010-07-28 14:52:34 -07:00
|
|
|
#ifdef PR_LOGGING
|
2013-09-28 11:28:42 -07:00
|
|
|
nsRefPtr<ImageURL> uri;
|
2010-07-28 14:52:34 -07:00
|
|
|
proxy->GetURI(getter_AddRefs(uri));
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString spec;
|
2010-07-28 14:52:34 -07:00
|
|
|
uri->GetSpec(spec);
|
2014-11-14 20:10:47 -08:00
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "ProgressTracker::NotifyCurrentState", "uri", spec.get());
|
2010-07-28 14:52:34 -07:00
|
|
|
#endif
|
|
|
|
|
2011-10-17 07:59:28 -07:00
|
|
|
proxy->SetNotificationsDeferred(true);
|
2010-07-28 14:52:14 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new AsyncNotifyCurrentStateRunnable(this, proxy);
|
2010-07-28 14:52:14 -07:00
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
#define NOTIFY_IMAGE_OBSERVERS(PROXIES, FUNC) \
|
2013-01-18 13:47:18 -08:00
|
|
|
do { \
|
2014-11-14 20:10:47 -08:00
|
|
|
ProxyArray::ForwardIterator iter(PROXIES); \
|
2013-01-18 13:47:18 -08:00
|
|
|
while (iter.HasMore()) { \
|
2013-12-12 13:17:35 -08:00
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get(); \
|
|
|
|
if (proxy && !proxy->NotificationsDeferred()) { \
|
2014-11-14 20:10:47 -08:00
|
|
|
proxy->FUNC; \
|
2013-01-18 13:47:18 -08:00
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (false);
|
|
|
|
|
2013-01-18 13:47:17 -08:00
|
|
|
/* static */ void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::SyncNotifyInternal(ProxyArray& aProxies,
|
|
|
|
bool aHasImage,
|
|
|
|
Progress aProgress,
|
|
|
|
const nsIntRect& aDirtyRect)
|
2010-07-28 14:52:14 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_SIZE_AVAILABLE)
|
2014-11-17 14:29:56 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnSizeAvailable());
|
2012-10-11 18:58:24 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
if (aProgress & FLAG_DECODE_STARTED)
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnStartDecode());
|
2012-12-19 13:28:54 -08:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
if (aProgress & FLAG_ONLOAD_BLOCKED)
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, BlockOnload());
|
2012-08-13 15:58:53 -07:00
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aHasImage) {
|
2013-01-18 13:47:17 -08:00
|
|
|
// OnFrameUpdate
|
2012-12-17 14:05:18 -08: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-11-10 12:37:35 -08:00
|
|
|
if (!aDirtyRect.IsEmpty())
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnFrameUpdate(&aDirtyRect));
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_FRAME_COMPLETE)
|
2014-11-17 14:29:56 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnFrameComplete());
|
2011-11-09 13:39:15 -08:00
|
|
|
|
2014-11-17 11:16:45 -08:00
|
|
|
if (aProgress & FLAG_HAS_TRANSPARENCY)
|
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnImageHasTransparency());
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
if (aProgress & FLAG_IS_ANIMATED)
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnImageIsAnimated());
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2014-11-06 17:33:57 -08: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-14 20:10:47 -08:00
|
|
|
if (aProgress & FLAG_ONLOAD_UNBLOCKED) {
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, UnblockOnload());
|
2014-11-06 17:33:57 -08:00
|
|
|
}
|
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_DECODE_COMPLETE) {
|
2014-11-10 12:37:35 -08:00
|
|
|
MOZ_ASSERT(aHasImage, "Stopped decoding without ever having an image?");
|
2014-11-17 14:29:56 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies, OnDecodeComplete());
|
2010-08-23 15:44:07 -07:00
|
|
|
}
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (aProgress & FLAG_LOAD_COMPLETE) {
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(aProxies,
|
2014-11-17 14:29:56 -08:00
|
|
|
OnLoadComplete(aProgress & FLAG_LAST_PART_COMPLETE));
|
2013-01-18 13:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-01 15:17:24 -08:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::SyncNotifyProgress(Progress aProgress,
|
|
|
|
const nsIntRect& aInvalidRect /* = nsIntRect() */)
|
2013-03-01 15:17:24 -08:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Use mConsumers on main thread only");
|
2013-03-01 15:17:24 -08:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
// Don't unblock onload if we're not blocked.
|
2014-11-14 20:10:47 -08:00
|
|
|
Progress progress = Difference(aProgress);
|
|
|
|
if (!((mProgress | progress) & FLAG_ONLOAD_BLOCKED)) {
|
|
|
|
progress &= ~FLAG_ONLOAD_UNBLOCKED;
|
2014-11-14 20:10:47 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the changes.
|
2014-11-14 20:10:47 -08:00
|
|
|
mProgress |= progress;
|
2014-11-14 20:10:47 -08:00
|
|
|
|
2014-11-14 20:10:48 -08:00
|
|
|
CheckProgressConsistency(mProgress);
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
// Send notifications.
|
2014-11-14 20:10:47 -08:00
|
|
|
SyncNotifyInternal(mConsumers, !!mImage, progress, aInvalidRect);
|
2013-03-01 15:17:24 -08:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
if (progress & FLAG_HAS_ERROR) {
|
2013-01-18 13:47:17 -08:00
|
|
|
FireFailureNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:17 -08:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::SyncNotify(imgRequestProxy* proxy)
|
2013-01-18 13:47:17 -08:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "imgRequestProxy is not threadsafe");
|
2013-01-18 13:47:17 -08:00
|
|
|
#ifdef PR_LOGGING
|
2013-09-28 11:28:42 -07:00
|
|
|
nsRefPtr<ImageURL> uri;
|
2013-01-18 13:47:17 -08:00
|
|
|
proxy->GetURI(getter_AddRefs(uri));
|
|
|
|
nsAutoCString spec;
|
|
|
|
uri->GetSpec(spec);
|
2014-11-14 20:10:47 -08:00
|
|
|
LOG_SCOPE_WITH_PARAM(GetImgLog(), "ProgressTracker::SyncNotify", "uri", spec.get());
|
2013-01-18 13:47:17 -08:00
|
|
|
#endif
|
|
|
|
|
|
|
|
nsIntRect r;
|
|
|
|
if (mImage) {
|
|
|
|
// XXX - Should only send partial rects here, but that needs to
|
|
|
|
// wait until we fix up the observer interface
|
|
|
|
r = mImage->FrameRect(imgIContainer::FRAME_CURRENT);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
2013-01-18 13:47:17 -08:00
|
|
|
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray array;
|
2014-07-30 12:52:05 -07:00
|
|
|
array.AppendElement(proxy);
|
2014-11-14 20:10:47 -08:00
|
|
|
SyncNotifyInternal(array, !!mImage, mProgress, r);
|
2013-01-18 13:47:18 -08:00
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::EmulateRequestFinished(imgRequestProxy* aProxy,
|
|
|
|
nsresult aStatus)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread(),
|
|
|
|
"SyncNotifyState and mConsumers are not threadsafe");
|
2010-05-14 13:47:59 -07:00
|
|
|
nsCOMPtr<imgIRequest> kungFuDeathGrip(aProxy);
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
if (mProgress & FLAG_ONLOAD_BLOCKED && !(mProgress & FLAG_ONLOAD_UNBLOCKED)) {
|
2012-10-09 09:47:14 -07:00
|
|
|
aProxy->UnblockOnload();
|
|
|
|
}
|
|
|
|
|
2014-11-17 14:29:56 -08:00
|
|
|
if (!(mProgress & FLAG_LOAD_COMPLETE)) {
|
2014-11-17 14:29:56 -08:00
|
|
|
aProxy->OnLoadComplete(true);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:20 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::AddConsumer(imgRequestProxy* aConsumer)
|
2012-10-12 09:11:20 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-07-30 12:52:05 -07:00
|
|
|
mConsumers.AppendElementUnlessExists(aConsumer);
|
2012-10-12 09:11:20 -07:00
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:23 -07:00
|
|
|
// XXX - The last argument should go away.
|
2012-10-12 09:11:20 -07:00
|
|
|
bool
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::RemoveConsumer(imgRequestProxy* aConsumer, nsresult aStatus)
|
2012-10-12 09:11:20 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-10-12 09:11:20 -07:00
|
|
|
// Remove the proxy from the list.
|
|
|
|
bool removed = mConsumers.RemoveElement(aConsumer);
|
|
|
|
|
|
|
|
// Consumers can get confused if they don't get all the proper teardown
|
|
|
|
// notifications. Part ways on good terms.
|
2013-01-18 13:47:18 -08:00
|
|
|
if (removed && !aConsumer->NotificationsDeferred()) {
|
2012-10-12 09:11:20 -07:00
|
|
|
EmulateRequestFinished(aConsumer, aStatus);
|
2013-01-18 13:47:18 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we don't give callbacks to a consumer that isn't interested in
|
|
|
|
// them any more.
|
2014-11-14 20:10:47 -08:00
|
|
|
AsyncNotifyRunnable* runnable =
|
|
|
|
static_cast<AsyncNotifyRunnable*>(mRunnable.get());
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
if (aConsumer->NotificationsDeferred() && runnable) {
|
|
|
|
runnable->RemoveProxy(aConsumer);
|
|
|
|
aConsumer->SetNotificationsDeferred(false);
|
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:20 -07:00
|
|
|
return removed;
|
|
|
|
}
|
|
|
|
|
2013-12-12 13:17:35 -08:00
|
|
|
bool
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::FirstConsumerIs(imgRequestProxy* aConsumer)
|
2013-12-12 13:17:35 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Use mConsumers on main thread only");
|
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
|
|
|
while (iter.HasMore()) {
|
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get();
|
|
|
|
if (proxy) {
|
|
|
|
return proxy.get() == aConsumer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::OnUnlockedDraw()
|
2013-01-18 13:47:18 -08:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(mConsumers, OnUnlockedDraw());
|
2014-11-14 20:06:19 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::ResetForNewRequest()
|
2014-11-14 20:06:19 -08:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
// We're starting a new load (and if this is called more than once, this is a
|
|
|
|
// multipart request) so keep only the bits that carry over between loads.
|
2014-11-14 20:10:48 -08:00
|
|
|
mProgress &= FLAG_IS_MULTIPART | FLAG_HAS_ERROR |
|
|
|
|
FLAG_ONLOAD_BLOCKED | FLAG_ONLOAD_UNBLOCKED;
|
2014-11-14 20:10:48 -08:00
|
|
|
|
|
|
|
CheckProgressConsistency(mProgress);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
2012-08-13 15:58:53 -07:00
|
|
|
|
2013-02-07 14:22:38 -08:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::OnDiscard()
|
2013-02-07 14:22:38 -08:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(mConsumers, OnDiscard());
|
2013-02-07 14:22:38 -08:00
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:21 -07:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::OnImageAvailable()
|
2012-10-12 09:11:21 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
// Note: SetHasImage calls Image::Lock and Image::IncrementAnimationCounter
|
|
|
|
// so subsequent calls or dispatches which Unlock or Decrement~ should
|
|
|
|
// be issued after this to avoid race conditions.
|
|
|
|
NS_DispatchToMainThread(
|
2014-11-14 20:10:47 -08:00
|
|
|
NS_NewRunnableMethod(this, &ProgressTracker::OnImageAvailable));
|
2013-09-28 11:28:43 -07:00
|
|
|
return;
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
|
2014-11-14 20:10:47 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(mConsumers, SetHasImage());
|
2012-08-13 15:58:53 -07:00
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
|
2012-11-17 05:33:20 -08:00
|
|
|
void
|
2014-11-14 20:10:47 -08:00
|
|
|
ProgressTracker::FireFailureNotification()
|
2012-11-17 05:33:20 -08:00
|
|
|
{
|
2013-01-18 13:47:18 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
// Some kind of problem has happened with image decoding.
|
|
|
|
// Report the URI to net:failed-to-process-uri-conent observers.
|
2013-09-28 11:28:44 -07:00
|
|
|
if (mImage) {
|
2013-09-28 11:28:42 -07:00
|
|
|
// Should be on main thread, so ok to create a new nsIURI.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
{
|
2013-09-28 11:28:44 -07:00
|
|
|
nsRefPtr<ImageURL> threadsafeUriData = mImage->GetURI();
|
2013-09-28 11:28:42 -07:00
|
|
|
uri = threadsafeUriData ? threadsafeUriData->ToIURI() : nullptr;
|
|
|
|
}
|
2013-02-01 17:06:34 -08:00
|
|
|
if (uri) {
|
|
|
|
nsCOMPtr<nsIObserverService> os = mozilla::services::GetObserverService();
|
|
|
|
if (os) {
|
|
|
|
os->NotifyObservers(uri, "net:failed-to-process-uri-content", nullptr);
|
|
|
|
}
|
2012-12-19 13:28:54 -08:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 05:33:20 -08:00
|
|
|
}
|
2014-11-14 20:10:47 -08:00
|
|
|
|
|
|
|
} // namespace image
|
|
|
|
} // namespace mozilla
|