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"
|
2010-05-14 13:47:59 -07:00
|
|
|
#include "imgStatusTracker.h"
|
|
|
|
|
|
|
|
#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
|
|
|
|
2012-01-06 08:02:27 -08:00
|
|
|
using namespace mozilla::image;
|
2013-12-12 13:17:35 -08:00
|
|
|
using mozilla::WeakPtr;
|
2010-08-13 21:09:49 -07:00
|
|
|
|
2013-09-28 11:28:44 -07:00
|
|
|
imgStatusTrackerInit::imgStatusTrackerInit(mozilla::image::Image* aImage,
|
|
|
|
imgStatusTracker* aTracker)
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(aImage);
|
|
|
|
|
|
|
|
if (aTracker) {
|
|
|
|
mTracker = aTracker;
|
|
|
|
mTracker->SetImage(aImage);
|
|
|
|
} else {
|
|
|
|
mTracker = new imgStatusTracker(aImage);
|
|
|
|
}
|
|
|
|
aImage->SetStatusTracker(mTracker);
|
|
|
|
MOZ_ASSERT(mTracker);
|
|
|
|
}
|
|
|
|
|
|
|
|
imgStatusTrackerInit::~imgStatusTrackerInit()
|
|
|
|
{
|
|
|
|
mTracker->ResetImage();
|
|
|
|
}
|
|
|
|
|
2010-08-23 15:44:07 -07:00
|
|
|
void
|
|
|
|
imgStatusTracker::SetImage(Image* aImage)
|
|
|
|
{
|
|
|
|
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
|
|
|
|
imgStatusTracker::ResetImage()
|
|
|
|
{
|
|
|
|
NS_ABORT_IF_FALSE(mImage, "Resetting image when it's already null!");
|
|
|
|
mImage = nullptr;
|
|
|
|
}
|
|
|
|
|
2014-11-06 17:33:59 -08:00
|
|
|
void imgStatusTracker::SetIsMultipart()
|
|
|
|
{
|
|
|
|
mState |= FLAG_IS_MULTIPART;
|
2014-11-14 20:06:19 -08:00
|
|
|
|
|
|
|
// If we haven't already blocked onload, make sure we never do.
|
|
|
|
if (!(mState & FLAG_ONLOAD_BLOCKED)) {
|
|
|
|
mState |= FLAG_ONLOAD_BLOCKED | FLAG_ONLOAD_UNBLOCKED;
|
|
|
|
}
|
2014-11-06 17:33:59 -08:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2010-05-14 13:47:59 -07:00
|
|
|
imgStatusTracker::IsLoading() const
|
|
|
|
{
|
|
|
|
// 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-06 17:33:57 -08:00
|
|
|
return !(mState & FLAG_REQUEST_STOPPED);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t
|
2010-05-14 13:47:59 -07:00
|
|
|
imgStatusTracker::GetImageStatus() const
|
|
|
|
{
|
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.
|
|
|
|
if (mState & FLAG_HAS_SIZE) {
|
|
|
|
status |= imgIRequest::STATUS_SIZE_AVAILABLE;
|
|
|
|
}
|
|
|
|
if (mState & FLAG_DECODE_STARTED) {
|
|
|
|
status |= imgIRequest::STATUS_DECODE_STARTED;
|
|
|
|
}
|
|
|
|
if (mState & FLAG_DECODE_STOPPED) {
|
|
|
|
status |= imgIRequest::STATUS_DECODE_COMPLETE;
|
|
|
|
}
|
|
|
|
if (mState & FLAG_FRAME_STOPPED) {
|
|
|
|
status |= imgIRequest::STATUS_FRAME_COMPLETE;
|
|
|
|
}
|
|
|
|
if (mState & FLAG_REQUEST_STOPPED) {
|
|
|
|
status |= imgIRequest::STATUS_LOAD_COMPLETE;
|
|
|
|
}
|
|
|
|
if (mState & FLAG_HAS_ERROR) {
|
|
|
|
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.
|
|
|
|
class imgRequestNotifyRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-09-28 11:28:44 -07:00
|
|
|
imgRequestNotifyRunnable(imgStatusTracker* 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(aRequestProxy, "aRequestProxy should not be null");
|
|
|
|
MOZ_ASSERT(aTracker, "aTracker 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
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
mTracker->mRequestRunnable = 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:
|
2010-08-12 08:59:28 -07:00
|
|
|
friend class imgStatusTracker;
|
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
nsRefPtr<imgStatusTracker> mTracker;
|
|
|
|
nsTArray< nsRefPtr<imgRequestProxy> > mProxies;
|
2010-07-28 14:52:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
void
|
2012-12-19 13:28:54 -08:00
|
|
|
imgStatusTracker::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);
|
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "imgStatusTracker::Notify async", "uri", spec.get());
|
|
|
|
} else {
|
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "imgStatusTracker::Notify async", "uri", "<unknown>");
|
|
|
|
}
|
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.
|
|
|
|
imgRequestNotifyRunnable* runnable = static_cast<imgRequestNotifyRunnable*>(mRequestRunnable.get());
|
2012-12-19 13:28:54 -08:00
|
|
|
if (runnable) {
|
2010-08-12 08:59:28 -07:00
|
|
|
runnable->AddProxy(proxy);
|
|
|
|
} else {
|
2012-12-19 13:28:54 -08:00
|
|
|
mRequestRunnable = new imgRequestNotifyRunnable(this, proxy);
|
2010-08-12 08:59:28 -07:00
|
|
|
NS_DispatchToCurrentThread(mRequestRunnable);
|
|
|
|
}
|
2010-07-28 14:52:14 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
// A helper class to allow us to call SyncNotify asynchronously for a given,
|
|
|
|
// fixed, state.
|
|
|
|
class imgStatusNotifyRunnable : public nsRunnable
|
|
|
|
{
|
|
|
|
public:
|
2013-09-28 11:28:44 -07:00
|
|
|
imgStatusNotifyRunnable(imgStatusTracker* statusTracker,
|
2010-07-28 14:52:14 -07:00
|
|
|
imgRequestProxy* requestproxy)
|
2013-09-28 11:28:44 -07:00
|
|
|
: mStatusTracker(statusTracker), mProxy(requestproxy)
|
2013-09-28 11:28:43 -07:00
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread(), "Should be created on the main thread");
|
|
|
|
MOZ_ASSERT(requestproxy, "requestproxy cannot be null");
|
2013-09-28 11:28:44 -07:00
|
|
|
MOZ_ASSERT(statusTracker, "status should not be null");
|
|
|
|
mImage = statusTracker->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
|
|
|
|
2013-09-28 11:28:44 -07:00
|
|
|
mStatusTracker->SyncNotify(mProxy);
|
2010-07-28 14:52:14 -07:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
2013-09-28 11:28:44 -07:00
|
|
|
nsRefPtr<imgStatusTracker> mStatusTracker;
|
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
|
|
|
nsRefPtr<imgRequestProxy> mProxy;
|
|
|
|
};
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2010-07-28 14:52:14 -07:00
|
|
|
imgStatusTracker::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);
|
2012-10-29 16:32:10 -07:00
|
|
|
LOG_FUNC_WITH_PARAM(GetImgLog(), "imgStatusTracker::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
|
|
|
|
2013-03-29 13:14:19 -07:00
|
|
|
// We don't keep track of
|
2013-09-28 11:28:44 -07:00
|
|
|
nsCOMPtr<nsIRunnable> ev = new imgStatusNotifyRunnable(this, proxy);
|
2010-07-28 14:52:14 -07:00
|
|
|
NS_DispatchToCurrentThread(ev);
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
#define NOTIFY_IMAGE_OBSERVERS(func) \
|
|
|
|
do { \
|
2014-11-10 12:37:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(aProxies); \
|
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()) { \
|
2013-01-18 13:47:18 -08:00
|
|
|
proxy->func; \
|
|
|
|
} \
|
|
|
|
} \
|
|
|
|
} while (false);
|
|
|
|
|
2013-01-18 13:47:17 -08:00
|
|
|
/* static */ void
|
2014-11-10 12:37:35 -08:00
|
|
|
imgStatusTracker::SyncNotifyState(ProxyArray& aProxies,
|
|
|
|
bool aHasImage, uint32_t aState,
|
|
|
|
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
|
|
|
// OnStartRequest
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_REQUEST_STARTED)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStartRequest());
|
2010-05-14 13:47:59 -07:00
|
|
|
|
|
|
|
// OnStartContainer
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_HAS_SIZE)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStartContainer());
|
2012-10-11 18:58:24 -07:00
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
// OnStartDecode
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_DECODE_STARTED)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStartDecode());
|
2012-12-19 13:28:54 -08:00
|
|
|
|
2012-08-13 15:58:53 -07:00
|
|
|
// BlockOnload
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_ONLOAD_BLOCKED)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(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())
|
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnFrameUpdate(&aDirtyRect));
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_FRAME_STOPPED)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStopFrame());
|
2011-11-09 13:39:15 -08:00
|
|
|
|
|
|
|
// OnImageIsAnimated
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_IS_ANIMATED)
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(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-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_ONLOAD_UNBLOCKED) {
|
2014-11-06 17:33:57 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(UnblockOnload());
|
|
|
|
}
|
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_DECODE_STOPPED) {
|
|
|
|
MOZ_ASSERT(aHasImage, "Stopped decoding without ever having an image?");
|
2013-01-18 13:47:18 -08:00
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStopDecode());
|
2010-08-23 15:44:07 -07:00
|
|
|
}
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aState & FLAG_REQUEST_STOPPED) {
|
|
|
|
NOTIFY_IMAGE_OBSERVERS(OnStopRequest(aState & FLAG_MULTIPART_STOPPED));
|
2013-01-18 13:47:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-08-28 15:39:04 -07:00
|
|
|
ImageStatusDiff
|
2014-11-14 20:06:19 -08:00
|
|
|
imgStatusTracker::Difference(const ImageStatusDiff& aOther) const
|
2013-01-18 13:47:17 -08:00
|
|
|
{
|
2013-08-28 15:39:04 -07:00
|
|
|
ImageStatusDiff diff;
|
2014-11-14 20:06:19 -08:00
|
|
|
diff.diffState = ~mState & aOther.diffState;
|
|
|
|
|
|
|
|
// Don't unblock onload if we're not blocked.
|
|
|
|
if (!((mState | diff.diffState) & FLAG_ONLOAD_BLOCKED)) {
|
|
|
|
diff.diffState &= ~FLAG_ONLOAD_UNBLOCKED;
|
|
|
|
}
|
2013-08-28 15:39:04 -07:00
|
|
|
|
2013-08-28 15:39:05 -07:00
|
|
|
return diff;
|
|
|
|
}
|
|
|
|
|
2013-08-28 15:39:04 -07:00
|
|
|
void
|
|
|
|
imgStatusTracker::ApplyDifference(const ImageStatusDiff& aDiff)
|
|
|
|
{
|
|
|
|
LOG_SCOPE(GetImgLog(), "imgStatusTracker::ApplyDifference");
|
2014-11-06 17:34:01 -08:00
|
|
|
mState |= aDiff.diffState;
|
2013-03-01 15:17:24 -08:00
|
|
|
}
|
2013-01-18 13:47:17 -08:00
|
|
|
|
2013-03-01 15:17:24 -08:00
|
|
|
void
|
2014-11-10 12:37:35 -08:00
|
|
|
imgStatusTracker::SyncNotifyDifference(const ImageStatusDiff& aDiff,
|
|
|
|
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
|
|
|
LOG_SCOPE(GetImgLog(), "imgStatusTracker::SyncNotifyDifference");
|
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
SyncNotifyState(mConsumers, !!mImage, aDiff.diffState, aInvalidRect);
|
2013-03-01 15:17:24 -08:00
|
|
|
|
2014-11-10 12:37:35 -08:00
|
|
|
if (aDiff.diffState & FLAG_HAS_ERROR) {
|
2013-01-18 13:47:17 -08:00
|
|
|
FireFailureNotification();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:17 -08:00
|
|
|
void
|
|
|
|
imgStatusTracker::SyncNotify(imgRequestProxy* proxy)
|
|
|
|
{
|
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);
|
|
|
|
LOG_SCOPE_WITH_PARAM(GetImgLog(), "imgStatusTracker::SyncNotify", "uri", spec.get());
|
|
|
|
#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-06 17:33:59 -08:00
|
|
|
SyncNotifyState(array, !!mImage, mState, r);
|
2013-01-18 13:47:18 -08:00
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2012-10-11 09:35:43 -07:00
|
|
|
imgStatusTracker::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);
|
|
|
|
|
2012-10-12 09:11:23 -07:00
|
|
|
// In certain cases the request might not have started yet.
|
|
|
|
// We still need to fulfill the contract.
|
2014-11-06 17:33:57 -08:00
|
|
|
if (!(mState & FLAG_REQUEST_STARTED)) {
|
2012-10-12 09:11:22 -07:00
|
|
|
aProxy->OnStartRequest();
|
|
|
|
}
|
|
|
|
|
2014-11-06 17:33:57 -08:00
|
|
|
if (mState & FLAG_ONLOAD_BLOCKED && !(mState & FLAG_ONLOAD_UNBLOCKED)) {
|
2012-10-09 09:47:14 -07:00
|
|
|
aProxy->UnblockOnload();
|
|
|
|
}
|
|
|
|
|
2014-11-06 17:33:57 -08:00
|
|
|
if (!(mState & FLAG_REQUEST_STOPPED)) {
|
2011-10-17 07:59:28 -07:00
|
|
|
aProxy->OnStopRequest(true);
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:20 -07:00
|
|
|
void
|
|
|
|
imgStatusTracker::AddConsumer(imgRequestProxy* aConsumer)
|
|
|
|
{
|
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
|
|
|
|
imgStatusTracker::RemoveConsumer(imgRequestProxy* aConsumer, nsresult aStatus)
|
|
|
|
{
|
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.
|
|
|
|
imgRequestNotifyRunnable* runnable = static_cast<imgRequestNotifyRunnable*>(mRequestRunnable.get());
|
|
|
|
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
|
|
|
|
imgStatusTracker::FirstConsumerIs(imgRequestProxy* aConsumer)
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2014-11-14 20:06:19 -08:00
|
|
|
imgStatusTracker::RecordError()
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2014-11-06 17:34:00 -08:00
|
|
|
mState |= FLAG_HAS_ERROR;
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2012-12-19 13:28:54 -08:00
|
|
|
void
|
|
|
|
imgStatusTracker::SendStartDecode(imgRequestProxy* aProxy)
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-12-19 13:28:54 -08:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
|
|
|
aProxy->OnStartDecode();
|
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2012-10-12 09:11:23 -07:00
|
|
|
imgStatusTracker::SendStartContainer(imgRequestProxy* aProxy)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-08-12 08:59:37 -07:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
2012-10-12 09:11:23 -07:00
|
|
|
aProxy->OnStartContainer();
|
2012-10-11 18:58:24 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2012-10-12 09:11:23 -07:00
|
|
|
imgStatusTracker::SendStopFrame(imgRequestProxy* aProxy)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-07-28 14:52:14 -07:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
2012-10-12 09:11:23 -07:00
|
|
|
aProxy->OnStopFrame();
|
2012-10-11 18:58:24 -07:00
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2012-10-12 09:11:23 -07:00
|
|
|
imgStatusTracker::SendStopDecode(imgRequestProxy* aProxy,
|
|
|
|
nsresult aStatus)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-07-28 14:52:14 -07:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
2012-10-12 09:11:22 -07:00
|
|
|
aProxy->OnStopDecode();
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2013-02-24 16:59:22 -08:00
|
|
|
void
|
2013-01-18 13:47:17 -08:00
|
|
|
imgStatusTracker::SendDiscard(imgRequestProxy* aProxy)
|
2013-02-24 16:59:22 -08:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-01-18 13:47:17 -08:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
|
|
|
aProxy->OnDiscard();
|
2013-02-24 16:59:22 -08:00
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:17 -08:00
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2013-01-18 13:47:17 -08:00
|
|
|
imgStatusTracker::SendImageIsAnimated(imgRequestProxy* aProxy)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-07-28 14:52:14 -07:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
2013-01-18 13:47:17 -08:00
|
|
|
aProxy->OnImageIsAnimated();
|
2010-05-14 13:47:59 -07:00
|
|
|
}
|
|
|
|
|
2013-02-24 16:59:22 -08:00
|
|
|
void
|
|
|
|
imgStatusTracker::SendUnlockedDraw(imgRequestProxy* aProxy)
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-24 16:59:22 -08:00
|
|
|
if (!aProxy->NotificationsDeferred())
|
|
|
|
aProxy->OnUnlockedDraw();
|
|
|
|
}
|
|
|
|
|
2013-01-18 13:47:18 -08:00
|
|
|
void
|
|
|
|
imgStatusTracker::OnUnlockedDraw()
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
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) {
|
|
|
|
SendUnlockedDraw(proxy);
|
|
|
|
}
|
2013-01-18 13:47:18 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
/* non-virtual sort-of-nsIRequestObserver methods */
|
|
|
|
void
|
2014-11-14 20:06:19 -08:00
|
|
|
imgStatusTracker::SendStartRequest(imgRequestProxy* aProxy)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2014-11-14 20:06:19 -08:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
if (!aProxy->NotificationsDeferred())
|
|
|
|
aProxy->OnStartRequest();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
imgStatusTracker::OnStartRequest()
|
|
|
|
{
|
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
// We're starting a new load, so clear any status and state bits indicating
|
2014-11-06 17:34:01 -08:00
|
|
|
// load/decode.
|
|
|
|
// XXX(seth): Are these really the only flags we want to clear?
|
2014-11-06 17:33:57 -08:00
|
|
|
mState &= ~FLAG_REQUEST_STARTED;
|
|
|
|
mState &= ~FLAG_DECODE_STARTED;
|
|
|
|
mState &= ~FLAG_DECODE_STOPPED;
|
|
|
|
mState &= ~FLAG_REQUEST_STOPPED;
|
|
|
|
mState &= ~FLAG_ONLOAD_BLOCKED;
|
2014-11-06 17:33:57 -08:00
|
|
|
mState &= ~FLAG_ONLOAD_UNBLOCKED;
|
2014-11-06 17:33:57 -08:00
|
|
|
mState &= ~FLAG_IS_ANIMATED;
|
|
|
|
|
|
|
|
mState |= FLAG_REQUEST_STARTED;
|
2010-05-14 13:47:59 -07:00
|
|
|
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
2012-10-12 09:11:21 -07:00
|
|
|
while (iter.HasMore()) {
|
2013-12-12 13:17:35 -08:00
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get();
|
|
|
|
if (proxy) {
|
|
|
|
SendStartRequest(proxy);
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-05-14 13:47:59 -07:00
|
|
|
void
|
2012-10-12 09:11:23 -07:00
|
|
|
imgStatusTracker::SendStopRequest(imgRequestProxy* aProxy,
|
|
|
|
bool aLastPart,
|
|
|
|
nsresult aStatus)
|
2010-05-14 13:47:59 -07:00
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2010-07-28 14:52:14 -07:00
|
|
|
if (!aProxy->NotificationsDeferred()) {
|
|
|
|
aProxy->OnStopRequest(aLastPart);
|
|
|
|
}
|
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
|
|
|
|
imgStatusTracker::OnDiscard()
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2013-02-07 14:22:38 -08:00
|
|
|
|
|
|
|
/* notify the kids */
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
2013-02-07 14:22:38 -08:00
|
|
|
while (iter.HasMore()) {
|
2013-12-12 13:17:35 -08:00
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get();
|
|
|
|
if (proxy) {
|
|
|
|
SendDiscard(proxy);
|
|
|
|
}
|
2013-02-07 14:22:38 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-10-12 09:11:21 -07:00
|
|
|
void
|
|
|
|
imgStatusTracker::OnDataAvailable()
|
|
|
|
{
|
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(
|
|
|
|
NS_NewRunnableMethod(this, &imgStatusTracker::OnDataAvailable));
|
|
|
|
return;
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
// Notify any imgRequestProxys that are observing us that we have an Image.
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
2012-10-12 09:11:21 -07:00
|
|
|
while (iter.HasMore()) {
|
2013-12-12 13:17:35 -08:00
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get();
|
|
|
|
if (proxy) {
|
|
|
|
proxy->SetHasImage();
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-08-13 15:58:53 -07:00
|
|
|
void
|
|
|
|
imgStatusTracker::SendBlockOnload(imgRequestProxy* aProxy)
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-08-13 15:58:53 -07:00
|
|
|
if (!aProxy->NotificationsDeferred()) {
|
|
|
|
aProxy->BlockOnload();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
imgStatusTracker::SendUnblockOnload(imgRequestProxy* aProxy)
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
MOZ_ASSERT(NS_IsMainThread());
|
2012-08-13 15:58:53 -07:00
|
|
|
if (!aProxy->NotificationsDeferred()) {
|
|
|
|
aProxy->UnblockOnload();
|
|
|
|
}
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
|
|
|
|
void
|
|
|
|
imgStatusTracker::MaybeUnblockOnload()
|
|
|
|
{
|
2013-09-28 11:28:43 -07:00
|
|
|
if (!NS_IsMainThread()) {
|
|
|
|
NS_DispatchToMainThread(
|
|
|
|
NS_NewRunnableMethod(this, &imgStatusTracker::MaybeUnblockOnload));
|
|
|
|
return;
|
|
|
|
}
|
2014-11-06 17:33:57 -08:00
|
|
|
if (!(mState & FLAG_ONLOAD_BLOCKED) || (mState & FLAG_ONLOAD_UNBLOCKED)) {
|
2012-10-12 09:11:21 -07:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2014-11-14 20:06:19 -08:00
|
|
|
mState |= FLAG_ONLOAD_UNBLOCKED;
|
2012-10-12 09:11:21 -07:00
|
|
|
|
2013-12-12 13:17:35 -08:00
|
|
|
ProxyArray::ForwardIterator iter(mConsumers);
|
2012-10-12 09:11:21 -07:00
|
|
|
while (iter.HasMore()) {
|
2013-12-12 13:17:35 -08:00
|
|
|
nsRefPtr<imgRequestProxy> proxy = iter.GetNext().get();
|
|
|
|
if (proxy) {
|
|
|
|
SendUnblockOnload(proxy);
|
|
|
|
}
|
2012-10-12 09:11:21 -07:00
|
|
|
}
|
|
|
|
}
|
2012-11-17 05:33:20 -08:00
|
|
|
|
|
|
|
void
|
2012-12-19 13:28:54 -08:00
|
|
|
imgStatusTracker::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
|
|
|
}
|