Files
palemoon27/image/MultipartImage.cpp
T
roytam1 e7535dc94b import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1187401 (Part 1) - Simplify the condition that determines whether we set RasterImage::mHasBeenDecoded. r=tn (69be36e7ef)
- Bug 1187401 (Part 2) - Eliminate the nsresult return value from RasterImage::SetMetadata, since it's not used anymore. r=tn (dc521c4b9f)
- Bug 1187401 (Part 3) - For consistency, call DoError if SetMetadata sees a negative size. r=tn (d9ca8ec91b)
- Bug 1207183 - micro-optimize removing work items from DecodePool's queues; r=seth (6bd2717e3a)
- No bug - Fix out-of-date comment in Decoder.cpp. r=me (54fdbbd444)
- Bug 1181324 - Eliminate the duplicate mRefCnt member in MultipartImage. r=seth (c4512a443e)
- Bug 1180105 - Do not leak the SourceSurface returned from imgIContainer::GetFrame in BlockUntilDecodedAndFinishObserving; r=seth (1cadbffc53)
- Bug 1181909 - Fix potential null dereference in NextPartObserver. r=tn (cfd8ad0119)
- Bug 108603 - Remove NS_IMPL_QUERY_INTERFACE_INHERITED0. r=mccr8, r=froydnj (4bfa6771fc)
- Bug 1159502 - Don't block onload for multipart images. r=tn (8b50eadf39)
- Bug 1200413 - Part 1: Re-write RefCountedInsideLambdaChecker to use captures instead of checking for DeclRef instances, r=ehsan (80ef99efe2)
- Bug 1200413 - Part 2: Make lambdas in ProgressTracker.cpp capture strong references, r=seth (9e4d96dffa)
- Bug 1194557 - Ensure that if the image was locked before RecoverFromLossOfFrames() was called, it's still locked afterwards. r=tn (ea4dc6ea9f)
- code style (ad3773ba42)
- Bug 1167590 - Mark imgRequestProxy::mListener as MOZ_UNSAFE_REF. r=seth (946ffaed8a)
- Bug 1148397 - Fix data race on imgRequest::mHadInsecureRedirect. r=tanvi (e73d0664f3)
- No bug - Tweak formatting of logging statement in imgRequest. r=me (cab2bcb014)
- Bug 1180126 - Read content disposition regardless of content type in imgRequest::PrepareForNewPart. r=tn (2934597743)
- Bug 1139225 - Followup - Remove duplicate multiPartChannel variable. (7f7f555a0b)
- Bug 1141398 - Do not always revalidate image cache entries for file URIs. r=tn (31d73cb508)
- Bug 1183563 - Fix incorrect mixed content warning after internal redirects. r=tanvi, r=seth (12a6c8a15f)
- Bug 1150127 - Stop leaking windows via imgCacheValidator. r=baku (a7809c5fa7)
- bits of Bug 1102048 (Part 20, imgLoader) (b2098c8a5a)
- (No bug) - Correct blatantly lying comment in imgLoader.cpp. r=me DONTBUILD (64c42a5b09)
- Bug 1160592 - Report image source size again in about:memory. r=dholbert (4e04cf3c3e)
- Add an assertion for the first argument of NewImageChannel, no bug (4c8f087a8f)
- Bug 1127534 - Remove assertion before creating a channel (r=sicking) (988692dc91)
- Bug 1175371 - Make VectorImage wait to deliver LOAD_COMPLETE until its size is available. r=dholbert (3c81e0daff)
- Bug 1181323 - Move nsSVGRenderingObserver's isupports/refcounting decl to subclasses, since one subclass (nsSVGFilterReference) already has its own redundant copy of the decl. r=dholbert (6171171c2c)
- Bug 1161722 - If we're shutting down, don't warn about untracked unlocked surfaces. r=dholbert (f7e18ce481)
- Bug 1170877 - Track how many times the SurfaceCache has overflowed and report it in about:memory. r=dholbert (884176cb1d)
- Bug 1161743 - Upgrade 'WARNING: Not expiration-tracking an unlocked surface' to an assertion. r=dholbert (9900169e7b)
- Bug 1167557 - Crash when a null surface is passed to SurfaceCache::Insert. r=dholbert (b3c4cf60aa)
- remove bypass cache not fonud either in FF nor TF (3ed4056a27)
- Missing bit Bug 1102048 (Part 25, header guards) - Make image/src files comply (cb8ed2428f)
- No bug - Remove obsolete comment in SourceBuffer.h. r=me (6e9c233448)
- coding style (94b7269690)
- Bug 1157065 - GFX: 2D: Add Loongson3 MMI helpers. r=jrmuizel (ebce946c91)
- reverto to FF52 and TFF settings (e147a8c7b5)
- Bug 1134599 - Fix rpi build target. r=jrmuizel, r=shuang (b9722f860c)
- Bug 1129147 - Part 1. Take CanvasPath into a separate file, to avoid circular dependency. r=roc (859bcad807)
- Bug 1129147 - Part 2. Path option to addHitRegion. r=ehsan r=gw280 (b2ab08a8a1)
- Bug 1206076: Use a specialized PersistentBufferProvider for Canvas2D when using a SkiaGL DrawTarget. r=jrmuizel (859589caf8)
- Bug 1188752 - Addendum: Make PersistentBufferProviderBasic constructor explicit. r=bustage on a CLOSED TREE (a27a4dc974)
- style (72a65dcb26)
- Bug 1198574 - Remove unnecessary argument for PersistentBufferProvider. r=bas (dca718bba8)
- Bug 1163124 - The initial value of the canvas filter property should be "none". r=roc (59df6a01d8)
2022-05-20 09:45:08 +08:00

335 lines
9.6 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* 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/. */
#include "MultipartImage.h"
#include "imgINotificationObserver.h"
namespace mozilla {
namespace image {
///////////////////////////////////////////////////////////////////////////////
// Helpers
///////////////////////////////////////////////////////////////////////////////
class NextPartObserver : public IProgressObserver
{
public:
MOZ_DECLARE_REFCOUNTED_TYPENAME(NextPartObserver)
NS_INLINE_DECL_REFCOUNTING(NextPartObserver, override)
explicit NextPartObserver(MultipartImage* aOwner)
: mOwner(aOwner)
{
MOZ_ASSERT(mOwner);
}
void BeginObserving(Image* aImage)
{
MOZ_ASSERT(aImage);
mImage = aImage;
nsRefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
tracker->AddObserver(this);
}
void BlockUntilDecodedAndFinishObserving()
{
// Use GetFrame() to block until our image finishes decoding.
nsRefPtr<SourceSurface> surface =
mImage->GetFrame(imgIContainer::FRAME_CURRENT,
imgIContainer::FLAG_SYNC_DECODE);
// GetFrame() should've sent synchronous notifications that would have
// caused us to call FinishObserving() (and null out mImage) already. If for
// some reason it didn't, we should do so here.
if (mImage) {
FinishObserving();
}
}
virtual void Notify(int32_t aType,
const nsIntRect* aRect = nullptr) override
{
if (!mImage) {
// We've already finished observing the last image we were given.
return;
}
if (aType == imgINotificationObserver::FRAME_COMPLETE) {
FinishObserving();
}
}
virtual void OnLoadComplete(bool aLastPart) override
{
if (!mImage) {
// We've already finished observing the last image we were given.
return;
}
// If there's already an error, we may never get a FRAME_COMPLETE
// notification, so go ahead and notify our owner right away.
nsRefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
if (tracker->GetProgress() & FLAG_HAS_ERROR) {
FinishObserving();
}
}
// Other notifications are ignored.
virtual void BlockOnload() override { }
virtual void UnblockOnload() override { }
virtual void SetHasImage() override { }
virtual bool NotificationsDeferred() const override { return false; }
virtual void SetNotificationsDeferred(bool) override { }
private:
virtual ~NextPartObserver() { }
void FinishObserving()
{
MOZ_ASSERT(mImage);
nsRefPtr<ProgressTracker> tracker = mImage->GetProgressTracker();
tracker->RemoveObserver(this);
mImage = nullptr;
mOwner->FinishTransition();
}
MultipartImage* mOwner;
nsRefPtr<Image> mImage;
};
///////////////////////////////////////////////////////////////////////////////
// Implementation
///////////////////////////////////////////////////////////////////////////////
MultipartImage::MultipartImage(Image* aFirstPart)
: ImageWrapper(aFirstPart)
, mDeferNotifications(false)
{
mNextPartObserver = new NextPartObserver(this);
}
void
MultipartImage::Init()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mTracker, "Should've called SetProgressTracker() by now");
// Start observing the first part.
nsRefPtr<ProgressTracker> firstPartTracker =
InnerImage()->GetProgressTracker();
firstPartTracker->AddObserver(this);
InnerImage()->RequestDecode();
InnerImage()->IncrementAnimationConsumers();
}
MultipartImage::~MultipartImage()
{
// Ask our ProgressTracker to drop its weak reference to us.
mTracker->ResetImage();
}
NS_IMPL_ISUPPORTS_INHERITED0(MultipartImage, ImageWrapper)
void
MultipartImage::BeginTransitionToPart(Image* aNextPart)
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(aNextPart);
if (mNextPart) {
// Let the decoder catch up so we don't drop frames.
mNextPartObserver->BlockUntilDecodedAndFinishObserving();
MOZ_ASSERT(!mNextPart);
}
mNextPart = aNextPart;
// Start observing the next part; we'll complete the transition when
// NextPartObserver calls FinishTransition.
mNextPartObserver->BeginObserving(mNextPart);
mNextPart->RequestDecode();
mNextPart->IncrementAnimationConsumers();
}
static Progress
FilterProgress(Progress aProgress)
{
// Filter out onload blocking notifications, since we don't want to block
// onload for multipart images.
return aProgress & ~(FLAG_ONLOAD_BLOCKED | FLAG_ONLOAD_UNBLOCKED);
}
void
MultipartImage::FinishTransition()
{
MOZ_ASSERT(NS_IsMainThread());
MOZ_ASSERT(mNextPart, "Should have a next part here");
nsRefPtr<ProgressTracker> newCurrentPartTracker =
mNextPart->GetProgressTracker();
if (newCurrentPartTracker->GetProgress() & FLAG_HAS_ERROR) {
// This frame has an error; drop it.
mNextPart = nullptr;
// We still need to notify, though.
mTracker->ResetForNewRequest();
nsRefPtr<ProgressTracker> currentPartTracker =
InnerImage()->GetProgressTracker();
mTracker
->SyncNotifyProgress(FilterProgress(currentPartTracker->GetProgress()));
return;
}
// Stop observing the current part.
{
nsRefPtr<ProgressTracker> currentPartTracker =
InnerImage()->GetProgressTracker();
currentPartTracker->RemoveObserver(this);
}
// Make the next part become the current part.
mTracker->ResetForNewRequest();
SetInnerImage(mNextPart);
mNextPart = nullptr;
newCurrentPartTracker->AddObserver(this);
// Finally, send all the notifications for the new current part and send a
// FRAME_UPDATE notification so that observers know to redraw.
mTracker
->SyncNotifyProgress(FilterProgress(newCurrentPartTracker->GetProgress()),
GetMaxSizedIntRect());
}
already_AddRefed<imgIContainer>
MultipartImage::Unwrap()
{
// Although we wrap another image, we don't allow callers to unwrap as. As far
// as external code is concerned, MultipartImage is atomic.
nsCOMPtr<imgIContainer> image = this;
return image.forget();
}
already_AddRefed<ProgressTracker>
MultipartImage::GetProgressTracker()
{
MOZ_ASSERT(mTracker);
nsRefPtr<ProgressTracker> tracker = mTracker;
return tracker.forget();
}
void
MultipartImage::SetProgressTracker(ProgressTracker* aTracker)
{
MOZ_ASSERT(aTracker);
MOZ_ASSERT(!mTracker);
mTracker = aTracker;
}
nsresult
MultipartImage::OnImageDataAvailable(nsIRequest* aRequest,
nsISupports* aContext,
nsIInputStream* aInStr,
uint64_t aSourceOffset,
uint32_t aCount)
{
// Note that this method is special in that we forward it to the next part if
// one exists, and *not* the current part.
// We may trigger notifications that will free mNextPart, so keep it alive.
nsRefPtr<Image> nextPart = mNextPart;
if (nextPart) {
nextPart->OnImageDataAvailable(aRequest, aContext, aInStr,
aSourceOffset, aCount);
} else {
InnerImage()->OnImageDataAvailable(aRequest, aContext, aInStr,
aSourceOffset, aCount);
}
return NS_OK;
}
nsresult
MultipartImage::OnImageDataComplete(nsIRequest* aRequest,
nsISupports* aContext,
nsresult aStatus,
bool aLastPart)
{
// Note that this method is special in that we forward it to the next part if
// one exists, and *not* the current part.
// We may trigger notifications that will free mNextPart, so keep it alive.
nsRefPtr<Image> nextPart = mNextPart;
if (nextPart) {
nextPart->OnImageDataComplete(aRequest, aContext, aStatus, aLastPart);
} else {
InnerImage()->OnImageDataComplete(aRequest, aContext, aStatus, aLastPart);
}
return NS_OK;
}
void
MultipartImage::Notify(int32_t aType, const nsIntRect* aRect /* = nullptr*/)
{
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
mTracker->SyncNotifyProgress(FLAG_SIZE_AVAILABLE);
} else if (aType == imgINotificationObserver::FRAME_UPDATE) {
mTracker->SyncNotifyProgress(NoProgress, *aRect);
} else if (aType == imgINotificationObserver::FRAME_COMPLETE) {
mTracker->SyncNotifyProgress(FLAG_FRAME_COMPLETE);
} else if (aType == imgINotificationObserver::LOAD_COMPLETE) {
mTracker->SyncNotifyProgress(FLAG_LOAD_COMPLETE);
} else if (aType == imgINotificationObserver::DECODE_COMPLETE) {
mTracker->SyncNotifyProgress(FLAG_DECODE_COMPLETE);
} else if (aType == imgINotificationObserver::DISCARD) {
mTracker->OnDiscard();
} else if (aType == imgINotificationObserver::UNLOCKED_DRAW) {
mTracker->OnUnlockedDraw();
} else if (aType == imgINotificationObserver::IS_ANIMATED) {
mTracker->SyncNotifyProgress(FLAG_IS_ANIMATED);
} else if (aType == imgINotificationObserver::HAS_TRANSPARENCY) {
mTracker->SyncNotifyProgress(FLAG_HAS_TRANSPARENCY);
} else {
NS_NOTREACHED("Notification list should be exhaustive");
}
}
void
MultipartImage::OnLoadComplete(bool aLastPart)
{
Progress progress = FLAG_LOAD_COMPLETE;
if (aLastPart) {
progress |= FLAG_LAST_PART_COMPLETE;
}
mTracker->SyncNotifyProgress(progress);
}
void
MultipartImage::SetHasImage()
{
mTracker->OnImageAvailable();
}
bool
MultipartImage::NotificationsDeferred() const
{
return mDeferNotifications;
}
void
MultipartImage::SetNotificationsDeferred(bool aDeferNotifications)
{
mDeferNotifications = aDeferNotifications;
}
} // namespace image
} // namespace mozilla