michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: // vim: ft=cpp tw=78 sw=2 et ts=2 michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: /* michael@0: * A base class which implements nsIImageLoadingContent and can be michael@0: * subclassed by various content nodes that want to provide image michael@0: * loading functionality (eg , , etc). michael@0: */ michael@0: michael@0: #include "nsImageLoadingContent.h" michael@0: #include "nsAutoPtr.h" michael@0: #include "nsError.h" michael@0: #include "nsIContent.h" michael@0: #include "nsIDocument.h" michael@0: #include "nsIScriptGlobalObject.h" michael@0: #include "nsIDOMWindow.h" michael@0: #include "nsServiceManagerUtils.h" michael@0: #include "nsContentPolicyUtils.h" michael@0: #include "nsIURI.h" michael@0: #include "nsILoadGroup.h" michael@0: #include "imgIContainer.h" michael@0: #include "imgLoader.h" michael@0: #include "imgRequestProxy.h" michael@0: #include "nsThreadUtils.h" michael@0: #include "nsNetUtil.h" michael@0: #include "nsImageFrame.h" michael@0: michael@0: #include "nsIPresShell.h" michael@0: michael@0: #include "nsIChannel.h" michael@0: #include "nsIStreamListener.h" michael@0: michael@0: #include "nsIFrame.h" michael@0: #include "nsIDOMNode.h" michael@0: michael@0: #include "nsContentUtils.h" michael@0: #include "nsLayoutUtils.h" michael@0: #include "nsIContentPolicy.h" michael@0: #include "nsSVGEffects.h" michael@0: michael@0: #include "mozAutoDocUpdate.h" michael@0: #include "mozilla/AsyncEventDispatcher.h" michael@0: #include "mozilla/EventStates.h" michael@0: #include "mozilla/dom/Element.h" michael@0: #include "mozilla/dom/ScriptSettings.h" michael@0: michael@0: #ifdef LoadImage michael@0: // Undefine LoadImage to prevent naming conflict with Windows. michael@0: #undef LoadImage michael@0: #endif michael@0: michael@0: using namespace mozilla; michael@0: michael@0: #ifdef DEBUG_chb michael@0: static void PrintReqURL(imgIRequest* req) { michael@0: if (!req) { michael@0: printf("(null req)\n"); michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr uri; michael@0: req->GetURI(getter_AddRefs(uri)); michael@0: if (!uri) { michael@0: printf("(null uri)\n"); michael@0: return; michael@0: } michael@0: michael@0: nsAutoCString spec; michael@0: uri->GetSpec(spec); michael@0: printf("spec='%s'\n", spec.get()); michael@0: } michael@0: #endif /* DEBUG_chb */ michael@0: michael@0: michael@0: nsImageLoadingContent::nsImageLoadingContent() michael@0: : mCurrentRequestFlags(0), michael@0: mPendingRequestFlags(0), michael@0: mObserverList(nullptr), michael@0: mImageBlockingStatus(nsIContentPolicy::ACCEPT), michael@0: mLoadingEnabled(true), michael@0: mIsImageStateForced(false), michael@0: mLoading(false), michael@0: // mBroken starts out true, since an image without a URI is broken.... michael@0: mBroken(true), michael@0: mUserDisabled(false), michael@0: mSuppressed(false), michael@0: mFireEventsOnDecode(false), michael@0: mNewRequestsWillNeedAnimationReset(false), michael@0: mStateChangerDepth(0), michael@0: mCurrentRequestRegistered(false), michael@0: mPendingRequestRegistered(false), michael@0: mFrameCreateCalled(false), michael@0: mVisibleCount(0) michael@0: { michael@0: if (!nsContentUtils::GetImgLoaderForChannel(nullptr)) { michael@0: mLoadingEnabled = false; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::DestroyImageLoadingContent() michael@0: { michael@0: // Cancel our requests so they won't hold stale refs to us michael@0: // NB: Don't ask to discard the images here. michael@0: ClearCurrentRequest(NS_BINDING_ABORTED, 0); michael@0: ClearPendingRequest(NS_BINDING_ABORTED, 0); michael@0: } michael@0: michael@0: nsImageLoadingContent::~nsImageLoadingContent() michael@0: { michael@0: NS_ASSERTION(!mCurrentRequest && !mPendingRequest, michael@0: "DestroyImageLoadingContent not called"); michael@0: NS_ASSERTION(!mObserverList.mObserver && !mObserverList.mNext, michael@0: "Observers still registered?"); michael@0: } michael@0: michael@0: /* michael@0: * imgINotificationObserver impl michael@0: */ michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::Notify(imgIRequest* aRequest, michael@0: int32_t aType, michael@0: const nsIntRect* aData) michael@0: { michael@0: if (aType == imgINotificationObserver::IS_ANIMATED) { michael@0: return OnImageIsAnimated(aRequest); michael@0: } michael@0: michael@0: if (aType == imgINotificationObserver::UNLOCKED_DRAW) { michael@0: OnUnlockedDraw(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: if (aType == imgINotificationObserver::LOAD_COMPLETE) { michael@0: // We should definitely have a request here michael@0: NS_ABORT_IF_FALSE(aRequest, "no request?"); michael@0: michael@0: NS_PRECONDITION(aRequest == mCurrentRequest || aRequest == mPendingRequest, michael@0: "Unknown request"); michael@0: } michael@0: michael@0: { michael@0: nsAutoScriptBlocker scriptBlocker; michael@0: michael@0: for (ImageObserver* observer = &mObserverList, *next; observer; michael@0: observer = next) { michael@0: next = observer->mNext; michael@0: if (observer->mObserver) { michael@0: observer->mObserver->Notify(aRequest, aType, aData); michael@0: } michael@0: } michael@0: } michael@0: michael@0: if (aType == imgINotificationObserver::SIZE_AVAILABLE) { michael@0: // Have to check for state changes here, since we might have been in michael@0: // the LOADING state before. michael@0: UpdateImageState(true); michael@0: } michael@0: michael@0: if (aType == imgINotificationObserver::LOAD_COMPLETE) { michael@0: uint32_t reqStatus; michael@0: aRequest->GetImageStatus(&reqStatus); michael@0: nsresult status = michael@0: reqStatus & imgIRequest::STATUS_ERROR ? NS_ERROR_FAILURE : NS_OK; michael@0: return OnStopRequest(aRequest, status); michael@0: } michael@0: michael@0: if (aType == imgINotificationObserver::DECODE_COMPLETE && mFireEventsOnDecode) { michael@0: mFireEventsOnDecode = false; michael@0: michael@0: uint32_t reqStatus; michael@0: aRequest->GetImageStatus(&reqStatus); michael@0: if (reqStatus & imgIRequest::STATUS_ERROR) { michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: } else { michael@0: FireEvent(NS_LITERAL_STRING("load")); michael@0: } michael@0: michael@0: UpdateImageState(true); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::OnStopRequest(imgIRequest* aRequest, michael@0: nsresult aStatus) michael@0: { michael@0: uint32_t oldStatus; michael@0: aRequest->GetImageStatus(&oldStatus); michael@0: michael@0: //XXXjdm This occurs when we have a pending request created, then another michael@0: // pending request replaces it before the first one is finished. michael@0: // This begs the question of what the correct behaviour is; we used michael@0: // to not have to care because we ran this code in OnStopDecode which michael@0: // wasn't called when the first request was cancelled. For now, I choose michael@0: // to punt when the given request doesn't appear to have terminated in michael@0: // an expected state. michael@0: if (!(oldStatus & (imgIRequest::STATUS_ERROR | imgIRequest::STATUS_LOAD_COMPLETE))) michael@0: return NS_OK; michael@0: michael@0: // Our state may change. Watch it. michael@0: AutoStateChanger changer(this, true); michael@0: michael@0: // If the pending request is loaded, switch to it. michael@0: if (aRequest == mPendingRequest) { michael@0: MakePendingRequestCurrent(); michael@0: } michael@0: NS_ABORT_IF_FALSE(aRequest == mCurrentRequest, michael@0: "One way or another, we should be current by now"); michael@0: michael@0: // We just loaded all the data we're going to get. If we're visible and michael@0: // haven't done an initial paint (*), we want to make sure the image starts michael@0: // decoding immediately, for two reasons: michael@0: // michael@0: // 1) This image is sitting idle but might need to be decoded as soon as we michael@0: // start painting, in which case we've wasted time. michael@0: // michael@0: // 2) We want to block onload until all visible images are decoded. We do this michael@0: // by blocking onload until all in-progress decodes get at least one frame michael@0: // decoded. However, if all the data comes in while painting is suppressed michael@0: // (ie, before the initial paint delay is finished), we fire onload without michael@0: // doing a paint first. This means that decode-on-draw images don't start michael@0: // decoding, so we can't wait for them to finish. See bug 512435. michael@0: // michael@0: // (*) IsPaintingSuppressed returns false if we haven't gotten the initial michael@0: // reflow yet, so we have to test !DidInitialize || IsPaintingSuppressed. michael@0: // It's possible for painting to be suppressed for reasons other than the michael@0: // initial paint delay (for example, being in the bfcache), but we probably michael@0: // aren't loading images in those situations. michael@0: michael@0: // XXXkhuey should this be GetOurCurrentDoc? Decoding if we're not in michael@0: // the document seems silly. michael@0: bool startedDecoding = false; michael@0: nsIDocument* doc = GetOurOwnerDoc(); michael@0: nsIPresShell* shell = doc ? doc->GetShell() : nullptr; michael@0: if (shell && shell->IsVisible() && michael@0: (!shell->DidInitialize() || shell->IsPaintingSuppressed())) { michael@0: michael@0: // If we've gotten a frame and that frame has called FrameCreate and that michael@0: // frame has been reflowed then we know that it checked it's own visibility michael@0: // so we can trust our visible count and we don't start decode if we are not michael@0: // visible. michael@0: nsIFrame* f = GetOurPrimaryFrame(); michael@0: if (!mFrameCreateCalled || !f || (f->GetStateBits() & NS_FRAME_FIRST_REFLOW) || michael@0: mVisibleCount > 0 || shell->AssumeAllImagesVisible()) { michael@0: if (NS_SUCCEEDED(mCurrentRequest->StartDecoding())) { michael@0: startedDecoding = true; michael@0: } michael@0: } michael@0: } michael@0: michael@0: // We want to give the decoder a chance to find errors. If we haven't found michael@0: // an error yet and we've started decoding, either from the above michael@0: // StartDecoding or from some other place, we must only fire these events michael@0: // after we finish decoding. michael@0: uint32_t reqStatus; michael@0: aRequest->GetImageStatus(&reqStatus); michael@0: if (NS_SUCCEEDED(aStatus) && !(reqStatus & imgIRequest::STATUS_ERROR) && michael@0: (reqStatus & imgIRequest::STATUS_DECODE_STARTED || michael@0: (startedDecoding && !(reqStatus & imgIRequest::STATUS_DECODE_COMPLETE)))) { michael@0: mFireEventsOnDecode = true; michael@0: } else { michael@0: // Fire the appropriate DOM event. michael@0: if (NS_SUCCEEDED(aStatus)) { michael@0: FireEvent(NS_LITERAL_STRING("load")); michael@0: } else { michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: } michael@0: } michael@0: michael@0: nsCOMPtr thisNode = do_QueryInterface(static_cast(this)); michael@0: nsSVGEffects::InvalidateDirectRenderingObservers(thisNode->AsElement()); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::OnUnlockedDraw() michael@0: { michael@0: if (mVisibleCount > 0) { michael@0: // We should already be marked as visible, there is nothing more we can do. michael@0: return; michael@0: } michael@0: michael@0: nsPresContext* presContext = GetFramePresContext(); michael@0: if (!presContext) michael@0: return; michael@0: michael@0: nsIPresShell* presShell = presContext->PresShell(); michael@0: if (!presShell) michael@0: return; michael@0: michael@0: presShell->EnsureImageInVisibleList(this); michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::OnImageIsAnimated(imgIRequest *aRequest) michael@0: { michael@0: bool* requestFlag = GetRegisteredFlagForRequest(aRequest); michael@0: if (requestFlag) { michael@0: nsLayoutUtils::RegisterImageRequest(GetFramePresContext(), michael@0: aRequest, requestFlag); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: /* michael@0: * nsIImageLoadingContent impl michael@0: */ michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::GetLoadingEnabled(bool *aLoadingEnabled) michael@0: { michael@0: *aLoadingEnabled = mLoadingEnabled; michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::SetLoadingEnabled(bool aLoadingEnabled) michael@0: { michael@0: if (nsContentUtils::GetImgLoaderForChannel(nullptr)) { michael@0: mLoadingEnabled = aLoadingEnabled; michael@0: } michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::GetImageBlockingStatus(int16_t* aStatus) michael@0: { michael@0: NS_PRECONDITION(aStatus, "Null out param"); michael@0: *aStatus = ImageBlockingStatus(); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::AddObserver(imgINotificationObserver* aObserver) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aObserver); michael@0: michael@0: if (!mObserverList.mObserver) { michael@0: mObserverList.mObserver = aObserver; michael@0: // Don't touch the linking of the list! michael@0: return NS_OK; michael@0: } michael@0: michael@0: // otherwise we have to create a new entry michael@0: michael@0: ImageObserver* observer = &mObserverList; michael@0: while (observer->mNext) { michael@0: observer = observer->mNext; michael@0: } michael@0: michael@0: observer->mNext = new ImageObserver(aObserver); michael@0: if (! observer->mNext) { michael@0: return NS_ERROR_OUT_OF_MEMORY; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::RemoveObserver(imgINotificationObserver* aObserver) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aObserver); michael@0: michael@0: if (mObserverList.mObserver == aObserver) { michael@0: mObserverList.mObserver = nullptr; michael@0: // Don't touch the linking of the list! michael@0: return NS_OK; michael@0: } michael@0: michael@0: // otherwise have to find it and splice it out michael@0: ImageObserver* observer = &mObserverList; michael@0: while (observer->mNext && observer->mNext->mObserver != aObserver) { michael@0: observer = observer->mNext; michael@0: } michael@0: michael@0: // At this point, we are pointing to the list element whose mNext is michael@0: // the right observer (assuming of course that mNext is not null) michael@0: if (observer->mNext) { michael@0: // splice it out michael@0: ImageObserver* oldObserver = observer->mNext; michael@0: observer->mNext = oldObserver->mNext; michael@0: oldObserver->mNext = nullptr; // so we don't destroy them all michael@0: delete oldObserver; michael@0: } michael@0: #ifdef DEBUG michael@0: else { michael@0: NS_WARNING("Asked to remove nonexistent observer"); michael@0: } michael@0: #endif michael@0: return NS_OK; michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsImageLoadingContent::GetRequest(int32_t aRequestType, michael@0: ErrorResult& aError) michael@0: { michael@0: nsCOMPtr request; michael@0: switch(aRequestType) { michael@0: case CURRENT_REQUEST: michael@0: request = mCurrentRequest; michael@0: break; michael@0: case PENDING_REQUEST: michael@0: request = mPendingRequest; michael@0: break; michael@0: default: michael@0: NS_ERROR("Unknown request type"); michael@0: aError.Throw(NS_ERROR_UNEXPECTED); michael@0: } michael@0: michael@0: return request.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::GetRequest(int32_t aRequestType, michael@0: imgIRequest** aRequest) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aRequest); michael@0: michael@0: ErrorResult result; michael@0: *aRequest = GetRequest(aRequestType, result).take(); michael@0: michael@0: return result.ErrorCode(); michael@0: } michael@0: michael@0: NS_IMETHODIMP_(void) michael@0: nsImageLoadingContent::FrameCreated(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION(aFrame, "aFrame is null"); michael@0: michael@0: mFrameCreateCalled = true; michael@0: michael@0: if (aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP)) { michael@0: // Assume all images in popups are visible. michael@0: IncrementVisibleCount(); michael@0: } michael@0: michael@0: TrackImage(mCurrentRequest); michael@0: TrackImage(mPendingRequest); michael@0: michael@0: // We need to make sure that our image request is registered, if it should michael@0: // be registered. michael@0: nsPresContext* presContext = aFrame->PresContext(); michael@0: if (mCurrentRequest) { michael@0: nsLayoutUtils::RegisterImageRequestIfAnimated(presContext, mCurrentRequest, michael@0: &mCurrentRequestRegistered); michael@0: } michael@0: michael@0: if (mPendingRequest) { michael@0: nsLayoutUtils::RegisterImageRequestIfAnimated(presContext, mPendingRequest, michael@0: &mPendingRequestRegistered); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP_(void) michael@0: nsImageLoadingContent::FrameDestroyed(nsIFrame* aFrame) michael@0: { michael@0: NS_ASSERTION(aFrame, "aFrame is null"); michael@0: michael@0: mFrameCreateCalled = false; michael@0: michael@0: // We need to make sure that our image request is deregistered. michael@0: nsPresContext* presContext = GetFramePresContext(); michael@0: if (mCurrentRequest) { michael@0: nsLayoutUtils::DeregisterImageRequest(presContext, michael@0: mCurrentRequest, michael@0: &mCurrentRequestRegistered); michael@0: } michael@0: michael@0: if (mPendingRequest) { michael@0: nsLayoutUtils::DeregisterImageRequest(presContext, michael@0: mPendingRequest, michael@0: &mPendingRequestRegistered); michael@0: } michael@0: michael@0: UntrackImage(mCurrentRequest); michael@0: UntrackImage(mPendingRequest); michael@0: michael@0: nsIPresShell* presShell = presContext ? presContext->GetPresShell() : nullptr; michael@0: if (presShell) { michael@0: presShell->RemoveImageFromVisibleList(this); michael@0: } michael@0: michael@0: if (aFrame->HasAnyStateBits(NS_FRAME_IN_POPUP)) { michael@0: // We assume all images in popups are visible, so this decrement balances michael@0: // out the increment in FrameCreated above. michael@0: DecrementVisibleCount(); michael@0: } michael@0: } michael@0: michael@0: int32_t michael@0: nsImageLoadingContent::GetRequestType(imgIRequest* aRequest, michael@0: ErrorResult& aError) michael@0: { michael@0: if (aRequest == mCurrentRequest) { michael@0: return CURRENT_REQUEST; michael@0: } michael@0: michael@0: if (aRequest == mPendingRequest) { michael@0: return PENDING_REQUEST; michael@0: } michael@0: michael@0: NS_ERROR("Unknown request"); michael@0: aError.Throw(NS_ERROR_UNEXPECTED); michael@0: return UNKNOWN_REQUEST; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::GetRequestType(imgIRequest* aRequest, michael@0: int32_t* aRequestType) michael@0: { michael@0: NS_PRECONDITION(aRequestType, "Null out param"); michael@0: michael@0: ErrorResult result; michael@0: *aRequestType = GetRequestType(aRequest, result); michael@0: return result.ErrorCode(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsImageLoadingContent::GetCurrentURI(ErrorResult& aError) michael@0: { michael@0: nsCOMPtr uri; michael@0: if (mCurrentRequest) { michael@0: mCurrentRequest->GetURI(getter_AddRefs(uri)); michael@0: } else if (mCurrentURI) { michael@0: nsresult rv = NS_EnsureSafeToReturn(mCurrentURI, getter_AddRefs(uri)); michael@0: if (NS_FAILED(rv)) { michael@0: aError.Throw(rv); michael@0: } michael@0: } michael@0: michael@0: return uri.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::GetCurrentURI(nsIURI** aURI) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aURI); michael@0: michael@0: ErrorResult result; michael@0: *aURI = GetCurrentURI(result).take(); michael@0: return result.ErrorCode(); michael@0: } michael@0: michael@0: already_AddRefed michael@0: nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel, michael@0: ErrorResult& aError) michael@0: { michael@0: if (!nsContentUtils::GetImgLoaderForChannel(aChannel)) { michael@0: aError.Throw(NS_ERROR_NULL_POINTER); michael@0: return nullptr; michael@0: } michael@0: michael@0: nsCOMPtr doc = GetOurOwnerDoc(); michael@0: if (!doc) { michael@0: // Don't bother michael@0: return nullptr; michael@0: } michael@0: michael@0: // XXX what should we do with content policies here, if anything? michael@0: // Shouldn't that be done before the start of the load? michael@0: // XXX what about shouldProcess? michael@0: michael@0: // Our state might change. Watch it. michael@0: AutoStateChanger changer(this, true); michael@0: michael@0: // Do the load. michael@0: nsCOMPtr listener; michael@0: nsRefPtr& req = PrepareNextRequest(); michael@0: nsresult rv = nsContentUtils::GetImgLoaderForChannel(aChannel)-> michael@0: LoadImageWithChannel(aChannel, this, doc, michael@0: getter_AddRefs(listener), michael@0: getter_AddRefs(req)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: TrackImage(req); michael@0: ResetAnimationIfNeeded(); michael@0: } else { michael@0: MOZ_ASSERT(!req, "Shouldn't have non-null request here"); michael@0: // If we don't have a current URI, we might as well store this URI so people michael@0: // know what we tried (and failed) to load. michael@0: if (!mCurrentRequest) michael@0: aChannel->GetURI(getter_AddRefs(mCurrentURI)); michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: aError.Throw(rv); michael@0: } michael@0: return listener.forget(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel, michael@0: nsIStreamListener** aListener) michael@0: { michael@0: NS_ENSURE_ARG_POINTER(aListener); michael@0: michael@0: ErrorResult result; michael@0: *aListener = LoadImageWithChannel(aChannel, result).take(); michael@0: return result.ErrorCode(); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::ForceReload(ErrorResult& aError) michael@0: { michael@0: nsCOMPtr currentURI; michael@0: GetCurrentURI(getter_AddRefs(currentURI)); michael@0: if (!currentURI) { michael@0: aError.Throw(NS_ERROR_NOT_AVAILABLE); michael@0: return; michael@0: } michael@0: michael@0: nsresult rv = LoadImage(currentURI, true, true, nullptr, nsIRequest::VALIDATE_ALWAYS); michael@0: if (NS_FAILED(rv)) { michael@0: aError.Throw(rv); michael@0: } michael@0: } michael@0: michael@0: NS_IMETHODIMP nsImageLoadingContent::ForceReload() michael@0: { michael@0: ErrorResult result; michael@0: ForceReload(result); michael@0: return result.ErrorCode(); michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::BlockOnload(imgIRequest* aRequest) michael@0: { michael@0: if (aRequest == mCurrentRequest) { michael@0: NS_ASSERTION(!(mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD), michael@0: "Double BlockOnload!?"); michael@0: mCurrentRequestFlags |= REQUEST_BLOCKS_ONLOAD; michael@0: } else if (aRequest == mPendingRequest) { michael@0: NS_ASSERTION(!(mPendingRequestFlags & REQUEST_BLOCKS_ONLOAD), michael@0: "Double BlockOnload!?"); michael@0: mPendingRequestFlags |= REQUEST_BLOCKS_ONLOAD; michael@0: } else { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIDocument* doc = GetOurCurrentDoc(); michael@0: if (doc) { michael@0: doc->BlockOnload(); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_IMETHODIMP michael@0: nsImageLoadingContent::UnblockOnload(imgIRequest* aRequest) michael@0: { michael@0: if (aRequest == mCurrentRequest) { michael@0: NS_ASSERTION(mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD, michael@0: "Double UnblockOnload!?"); michael@0: mCurrentRequestFlags &= ~REQUEST_BLOCKS_ONLOAD; michael@0: } else if (aRequest == mPendingRequest) { michael@0: NS_ASSERTION(mPendingRequestFlags & REQUEST_BLOCKS_ONLOAD, michael@0: "Double UnblockOnload!?"); michael@0: mPendingRequestFlags &= ~REQUEST_BLOCKS_ONLOAD; michael@0: } else { michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIDocument* doc = GetOurCurrentDoc(); michael@0: if (doc) { michael@0: doc->UnblockOnload(false); michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::IncrementVisibleCount() michael@0: { michael@0: mVisibleCount++; michael@0: if (mVisibleCount == 1) { michael@0: TrackImage(mCurrentRequest); michael@0: TrackImage(mPendingRequest); michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::DecrementVisibleCount() michael@0: { michael@0: NS_ASSERTION(mVisibleCount > 0, "visible count should be positive here"); michael@0: mVisibleCount--; michael@0: michael@0: if (mVisibleCount == 0) { michael@0: UntrackImage(mCurrentRequest); michael@0: UntrackImage(mPendingRequest); michael@0: } michael@0: } michael@0: michael@0: uint32_t michael@0: nsImageLoadingContent::GetVisibleCount() michael@0: { michael@0: return mVisibleCount; michael@0: } michael@0: michael@0: /* michael@0: * Non-interface methods michael@0: */ michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::LoadImage(const nsAString& aNewURI, michael@0: bool aForce, michael@0: bool aNotify) michael@0: { michael@0: // First, get a document (needed for security checks and the like) michael@0: nsIDocument* doc = GetOurOwnerDoc(); michael@0: if (!doc) { michael@0: // No reason to bother, I think... michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsCOMPtr imageURI; michael@0: nsresult rv = StringToURI(aNewURI, doc, getter_AddRefs(imageURI)); michael@0: NS_ENSURE_SUCCESS(rv, rv); michael@0: // XXXbiesi fire onerror if that failed? michael@0: michael@0: bool equal; michael@0: michael@0: if (aNewURI.IsEmpty() && michael@0: doc->GetDocumentURI() && michael@0: NS_SUCCEEDED(doc->GetDocumentURI()->EqualsExceptRef(imageURI, &equal)) && michael@0: equal) { michael@0: michael@0: // Loading an embedded img from the same URI as the document URI will not work michael@0: // as a resource cannot recursively embed itself. Attempting to do so generally michael@0: // results in having to pre-emptively close down an in-flight HTTP transaction michael@0: // and then incurring the significant cost of establishing a new TCP channel. michael@0: // This is generally triggered from michael@0: // In light of that, just skip loading it.. michael@0: // Do make sure to drop our existing image, if any michael@0: CancelImageRequests(aNotify); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_TryToSetImmutable(imageURI); michael@0: michael@0: return LoadImage(imageURI, aForce, aNotify, doc); michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::LoadImage(nsIURI* aNewURI, michael@0: bool aForce, michael@0: bool aNotify, michael@0: nsIDocument* aDocument, michael@0: nsLoadFlags aLoadFlags) michael@0: { michael@0: if (!mLoadingEnabled) { michael@0: // XXX Why fire an error here? seems like the callers to SetLoadingEnabled michael@0: // don't want/need it. michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: return NS_OK; michael@0: } michael@0: michael@0: NS_ASSERTION(!aDocument || aDocument == GetOurOwnerDoc(), michael@0: "Bogus document passed in"); michael@0: // First, get a document (needed for security checks and the like) michael@0: if (!aDocument) { michael@0: aDocument = GetOurOwnerDoc(); michael@0: if (!aDocument) { michael@0: // No reason to bother, I think... michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: // URI equality check. michael@0: // michael@0: // We skip the equality check if our current image was blocked, since in that michael@0: // case we really do want to try loading again. michael@0: if (!aForce && NS_CP_ACCEPTED(mImageBlockingStatus)) { michael@0: nsCOMPtr currentURI; michael@0: GetCurrentURI(getter_AddRefs(currentURI)); michael@0: bool equal; michael@0: if (currentURI && michael@0: NS_SUCCEEDED(currentURI->Equals(aNewURI, &equal)) && michael@0: equal) { michael@0: // Nothing to do here. michael@0: return NS_OK; michael@0: } michael@0: } michael@0: michael@0: // From this point on, our image state could change. Watch it. michael@0: AutoStateChanger changer(this, aNotify); michael@0: michael@0: // Sanity check. michael@0: // michael@0: // We use the principal of aDocument to avoid having to QI |this| an extra michael@0: // time. It should always be the same as the principal of this node. michael@0: #ifdef DEBUG michael@0: nsCOMPtr thisContent = do_QueryInterface(static_cast(this)); michael@0: NS_ABORT_IF_FALSE(thisContent && michael@0: thisContent->NodePrincipal() == aDocument->NodePrincipal(), michael@0: "Principal mismatch?"); michael@0: #endif michael@0: michael@0: // Are we blocked? michael@0: int16_t cpDecision = nsIContentPolicy::REJECT_REQUEST; michael@0: nsContentUtils::CanLoadImage(aNewURI, michael@0: static_cast(this), michael@0: aDocument, michael@0: aDocument->NodePrincipal(), michael@0: &cpDecision); michael@0: if (!NS_CP_ACCEPTED(cpDecision)) { michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: SetBlockedRequest(aNewURI, cpDecision); michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsLoadFlags loadFlags = aLoadFlags; michael@0: int32_t corsmode = GetCORSMode(); michael@0: if (corsmode == CORS_ANONYMOUS) { michael@0: loadFlags |= imgILoader::LOAD_CORS_ANONYMOUS; michael@0: } else if (corsmode == CORS_USE_CREDENTIALS) { michael@0: loadFlags |= imgILoader::LOAD_CORS_USE_CREDENTIALS; michael@0: } michael@0: michael@0: // Not blocked. Do the load. michael@0: nsRefPtr& req = PrepareNextRequest(); michael@0: nsCOMPtr content = michael@0: do_QueryInterface(static_cast(this)); michael@0: nsresult rv; michael@0: rv = nsContentUtils::LoadImage(aNewURI, aDocument, michael@0: aDocument->NodePrincipal(), michael@0: aDocument->GetDocumentURI(), michael@0: this, loadFlags, michael@0: content->LocalName(), michael@0: getter_AddRefs(req)); michael@0: michael@0: if (NS_SUCCEEDED(rv)) { michael@0: TrackImage(req); michael@0: ResetAnimationIfNeeded(); michael@0: michael@0: // Handle cases when we just ended up with a pending request but it's michael@0: // already done. In that situation we have to synchronously switch that michael@0: // request to being the current request, because websites depend on that michael@0: // behavior. michael@0: if (req == mPendingRequest) { michael@0: uint32_t pendingLoadStatus; michael@0: rv = req->GetImageStatus(&pendingLoadStatus); michael@0: if (NS_SUCCEEDED(rv) && michael@0: (pendingLoadStatus & imgIRequest::STATUS_LOAD_COMPLETE)) { michael@0: MakePendingRequestCurrent(); michael@0: MOZ_ASSERT(mCurrentRequest, michael@0: "How could we not have a current request here?"); michael@0: michael@0: nsImageFrame *f = do_QueryFrame(GetOurPrimaryFrame()); michael@0: if (f) { michael@0: f->NotifyNewCurrentRequest(mCurrentRequest, NS_OK); michael@0: } michael@0: } michael@0: } michael@0: } else { michael@0: MOZ_ASSERT(!req, "Shouldn't have non-null request here"); michael@0: // If we don't have a current URI, we might as well store this URI so people michael@0: // know what we tried (and failed) to load. michael@0: if (!mCurrentRequest) michael@0: mCurrentURI = aNewURI; michael@0: FireEvent(NS_LITERAL_STRING("error")); michael@0: return NS_OK; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::ForceImageState(bool aForce, michael@0: EventStates::InternalType aState) michael@0: { michael@0: mIsImageStateForced = aForce; michael@0: mForcedImageState = EventStates(aState); michael@0: return NS_OK; michael@0: } michael@0: michael@0: EventStates michael@0: nsImageLoadingContent::ImageState() const michael@0: { michael@0: if (mIsImageStateForced) { michael@0: return mForcedImageState; michael@0: } michael@0: michael@0: EventStates states; michael@0: michael@0: if (mBroken) { michael@0: states |= NS_EVENT_STATE_BROKEN; michael@0: } michael@0: if (mUserDisabled) { michael@0: states |= NS_EVENT_STATE_USERDISABLED; michael@0: } michael@0: if (mSuppressed) { michael@0: states |= NS_EVENT_STATE_SUPPRESSED; michael@0: } michael@0: if (mLoading) { michael@0: states |= NS_EVENT_STATE_LOADING; michael@0: } michael@0: michael@0: return states; michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::UpdateImageState(bool aNotify) michael@0: { michael@0: if (mStateChangerDepth > 0) { michael@0: // Ignore this call; we'll update our state when the outermost state michael@0: // changer is destroyed. Need this to work around the fact that some libpr0n michael@0: // stuff is actually sync and hence we can get OnStopDecode called while michael@0: // we're still under LoadImage, and OnStopDecode doesn't know anything about michael@0: // aNotify. michael@0: // XXX - This machinery should be removed after bug 521604. michael@0: return; michael@0: } michael@0: michael@0: nsCOMPtr thisContent = do_QueryInterface(static_cast(this)); michael@0: if (!thisContent) { michael@0: return; michael@0: } michael@0: michael@0: mLoading = mBroken = mUserDisabled = mSuppressed = false; michael@0: michael@0: // If we were blocked by server-based content policy, we claim to be michael@0: // suppressed. If we were blocked by type-based content policy, we claim to michael@0: // be user-disabled. Otherwise, claim to be broken. michael@0: if (mImageBlockingStatus == nsIContentPolicy::REJECT_SERVER) { michael@0: mSuppressed = true; michael@0: } else if (mImageBlockingStatus == nsIContentPolicy::REJECT_TYPE) { michael@0: mUserDisabled = true; michael@0: } else if (!mCurrentRequest) { michael@0: // No current request means error, since we weren't disabled or suppressed michael@0: mBroken = true; michael@0: } else { michael@0: uint32_t currentLoadStatus; michael@0: nsresult rv = mCurrentRequest->GetImageStatus(¤tLoadStatus); michael@0: if (NS_FAILED(rv) || (currentLoadStatus & imgIRequest::STATUS_ERROR)) { michael@0: mBroken = true; michael@0: } else if (!(currentLoadStatus & imgIRequest::STATUS_SIZE_AVAILABLE)) { michael@0: mLoading = true; michael@0: } michael@0: } michael@0: michael@0: NS_ASSERTION(thisContent->IsElement(), "Not an element?"); michael@0: thisContent->AsElement()->UpdateState(aNotify); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::CancelImageRequests(bool aNotify) michael@0: { michael@0: AutoStateChanger changer(this, aNotify); michael@0: ClearPendingRequest(NS_BINDING_ABORTED, REQUEST_DISCARD); michael@0: ClearCurrentRequest(NS_BINDING_ABORTED, REQUEST_DISCARD); michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::UseAsPrimaryRequest(imgRequestProxy* aRequest, michael@0: bool aNotify) michael@0: { michael@0: // Our state will change. Watch it. michael@0: AutoStateChanger changer(this, aNotify); michael@0: michael@0: // Get rid if our existing images michael@0: ClearPendingRequest(NS_BINDING_ABORTED, REQUEST_DISCARD); michael@0: ClearCurrentRequest(NS_BINDING_ABORTED, REQUEST_DISCARD); michael@0: michael@0: // Clone the request we were given. michael@0: nsRefPtr& req = PrepareNextRequest(); michael@0: nsresult rv = aRequest->Clone(this, getter_AddRefs(req)); michael@0: if (NS_SUCCEEDED(rv)) { michael@0: TrackImage(req); michael@0: } else { michael@0: MOZ_ASSERT(!req, "Shouldn't have non-null request here"); michael@0: return rv; michael@0: } michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsIDocument* michael@0: nsImageLoadingContent::GetOurOwnerDoc() michael@0: { michael@0: nsCOMPtr thisContent = michael@0: do_QueryInterface(static_cast(this)); michael@0: NS_ENSURE_TRUE(thisContent, nullptr); michael@0: michael@0: return thisContent->OwnerDoc(); michael@0: } michael@0: michael@0: nsIDocument* michael@0: nsImageLoadingContent::GetOurCurrentDoc() michael@0: { michael@0: nsCOMPtr thisContent = michael@0: do_QueryInterface(static_cast(this)); michael@0: NS_ENSURE_TRUE(thisContent, nullptr); michael@0: michael@0: return thisContent->GetCurrentDoc(); michael@0: } michael@0: michael@0: nsIFrame* michael@0: nsImageLoadingContent::GetOurPrimaryFrame() michael@0: { michael@0: nsCOMPtr thisContent = michael@0: do_QueryInterface(static_cast(this)); michael@0: return thisContent->GetPrimaryFrame(); michael@0: } michael@0: michael@0: nsPresContext* nsImageLoadingContent::GetFramePresContext() michael@0: { michael@0: nsIFrame* frame = GetOurPrimaryFrame(); michael@0: if (!frame) { michael@0: return nullptr; michael@0: } michael@0: michael@0: return frame->PresContext(); michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::StringToURI(const nsAString& aSpec, michael@0: nsIDocument* aDocument, michael@0: nsIURI** aURI) michael@0: { michael@0: NS_PRECONDITION(aDocument, "Must have a document"); michael@0: NS_PRECONDITION(aURI, "Null out param"); michael@0: michael@0: // (1) Get the base URI michael@0: nsCOMPtr thisContent = do_QueryInterface(static_cast(this)); michael@0: NS_ASSERTION(thisContent, "An image loading content must be an nsIContent"); michael@0: nsCOMPtr baseURL = thisContent->GetBaseURI(); michael@0: michael@0: // (2) Get the charset michael@0: const nsAFlatCString &charset = aDocument->GetDocumentCharacterSet(); michael@0: michael@0: // (3) Construct the silly thing michael@0: return NS_NewURI(aURI, michael@0: aSpec, michael@0: charset.IsEmpty() ? nullptr : charset.get(), michael@0: baseURL, michael@0: nsContentUtils::GetIOService()); michael@0: } michael@0: michael@0: nsresult michael@0: nsImageLoadingContent::FireEvent(const nsAString& aEventType) michael@0: { michael@0: // We have to fire the event asynchronously so that we won't go into infinite michael@0: // loops in cases when onLoad handlers reset the src and the new src is in michael@0: // cache. michael@0: michael@0: nsCOMPtr thisNode = do_QueryInterface(static_cast(this)); michael@0: michael@0: nsRefPtr loadBlockingAsyncDispatcher = michael@0: new LoadBlockingAsyncEventDispatcher(thisNode, aEventType, false, false); michael@0: loadBlockingAsyncDispatcher->PostDOMEvent(); michael@0: michael@0: return NS_OK; michael@0: } michael@0: michael@0: nsRefPtr& michael@0: nsImageLoadingContent::PrepareNextRequest() michael@0: { michael@0: // If we don't have a usable current request, get rid of any half-baked michael@0: // request that might be sitting there and make this one current. michael@0: if (!HaveSize(mCurrentRequest)) michael@0: return PrepareCurrentRequest(); michael@0: michael@0: // Otherwise, make it pending. michael@0: return PreparePendingRequest(); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::SetBlockedRequest(nsIURI* aURI, int16_t aContentDecision) michael@0: { michael@0: // Sanity michael@0: NS_ABORT_IF_FALSE(!NS_CP_ACCEPTED(aContentDecision), "Blocked but not?"); michael@0: michael@0: // We do some slightly illogical stuff here to maintain consistency with michael@0: // old behavior that people probably depend on. Even in the case where the michael@0: // new image is blocked, the old one should really be canceled with the michael@0: // reason "image source changed". However, apparently there's some abuse michael@0: // over in nsImageFrame where the displaying of the "broken" icon for the michael@0: // next image depends on the cancel reason of the previous image. ugh. michael@0: ClearPendingRequest(NS_ERROR_IMAGE_BLOCKED, REQUEST_DISCARD); michael@0: michael@0: // For the blocked case, we only want to cancel the existing current request michael@0: // if size is not available. bz says the web depends on this behavior. michael@0: if (!HaveSize(mCurrentRequest)) { michael@0: michael@0: mImageBlockingStatus = aContentDecision; michael@0: ClearCurrentRequest(NS_ERROR_IMAGE_BLOCKED, REQUEST_DISCARD); michael@0: michael@0: // We still want to remember what URI we were despite not having an actual michael@0: // request. michael@0: mCurrentURI = aURI; michael@0: } michael@0: } michael@0: michael@0: nsRefPtr& michael@0: nsImageLoadingContent::PrepareCurrentRequest() michael@0: { michael@0: // Blocked images go through SetBlockedRequest, which is a separate path. For michael@0: // everything else, we're unblocked. michael@0: mImageBlockingStatus = nsIContentPolicy::ACCEPT; michael@0: michael@0: // Get rid of anything that was there previously. michael@0: ClearCurrentRequest(NS_ERROR_IMAGE_SRC_CHANGED, REQUEST_DISCARD); michael@0: michael@0: if (mNewRequestsWillNeedAnimationReset) { michael@0: mCurrentRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET; michael@0: } michael@0: michael@0: // Return a reference. michael@0: return mCurrentRequest; michael@0: } michael@0: michael@0: nsRefPtr& michael@0: nsImageLoadingContent::PreparePendingRequest() michael@0: { michael@0: // Get rid of anything that was there previously. michael@0: ClearPendingRequest(NS_ERROR_IMAGE_SRC_CHANGED, REQUEST_DISCARD); michael@0: michael@0: if (mNewRequestsWillNeedAnimationReset) { michael@0: mPendingRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET; michael@0: } michael@0: michael@0: // Return a reference. michael@0: return mPendingRequest; michael@0: } michael@0: michael@0: namespace { michael@0: michael@0: class ImageRequestAutoLock michael@0: { michael@0: public: michael@0: ImageRequestAutoLock(imgIRequest* aRequest) michael@0: : mRequest(aRequest) michael@0: { michael@0: if (mRequest) { michael@0: mRequest->LockImage(); michael@0: } michael@0: } michael@0: michael@0: ~ImageRequestAutoLock() michael@0: { michael@0: if (mRequest) { michael@0: mRequest->UnlockImage(); michael@0: } michael@0: } michael@0: michael@0: private: michael@0: nsCOMPtr mRequest; michael@0: }; michael@0: michael@0: } // anonymous namespace michael@0: michael@0: void michael@0: nsImageLoadingContent::MakePendingRequestCurrent() michael@0: { michael@0: MOZ_ASSERT(mPendingRequest); michael@0: michael@0: // Lock mCurrentRequest for the duration of this method. We do this because michael@0: // PrepareCurrentRequest() might unlock mCurrentRequest. If mCurrentRequest michael@0: // and mPendingRequest are both requests for the same image, unlocking michael@0: // mCurrentRequest before we lock mPendingRequest can cause the lock count michael@0: // to go to 0 and the image to be discarded! michael@0: ImageRequestAutoLock autoLock(mCurrentRequest); michael@0: michael@0: PrepareCurrentRequest() = mPendingRequest; michael@0: mPendingRequest = nullptr; michael@0: mCurrentRequestFlags = mPendingRequestFlags; michael@0: mPendingRequestFlags = 0; michael@0: ResetAnimationIfNeeded(); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::ClearCurrentRequest(nsresult aReason, michael@0: uint32_t aFlags) michael@0: { michael@0: if (!mCurrentRequest) { michael@0: // Even if we didn't have a current request, we might have been keeping michael@0: // a URI as a placeholder for a failed load. Clear that now. michael@0: mCurrentURI = nullptr; michael@0: return; michael@0: } michael@0: NS_ABORT_IF_FALSE(!mCurrentURI, michael@0: "Shouldn't have both mCurrentRequest and mCurrentURI!"); michael@0: michael@0: // Deregister this image from the refresh driver so it no longer receives michael@0: // notifications. michael@0: nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mCurrentRequest, michael@0: &mCurrentRequestRegistered); michael@0: michael@0: // Clean up the request. michael@0: UntrackImage(mCurrentRequest, aFlags); michael@0: mCurrentRequest->CancelAndForgetObserver(aReason); michael@0: mCurrentRequest = nullptr; michael@0: mCurrentRequestFlags = 0; michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::ClearPendingRequest(nsresult aReason, michael@0: uint32_t aFlags) michael@0: { michael@0: if (!mPendingRequest) michael@0: return; michael@0: michael@0: // Deregister this image from the refresh driver so it no longer receives michael@0: // notifications. michael@0: nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mPendingRequest, michael@0: &mPendingRequestRegistered); michael@0: michael@0: UntrackImage(mPendingRequest, aFlags); michael@0: mPendingRequest->CancelAndForgetObserver(aReason); michael@0: mPendingRequest = nullptr; michael@0: mPendingRequestFlags = 0; michael@0: } michael@0: michael@0: bool* michael@0: nsImageLoadingContent::GetRegisteredFlagForRequest(imgIRequest* aRequest) michael@0: { michael@0: if (aRequest == mCurrentRequest) { michael@0: return &mCurrentRequestRegistered; michael@0: } else if (aRequest == mPendingRequest) { michael@0: return &mPendingRequestRegistered; michael@0: } else { michael@0: return nullptr; michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::ResetAnimationIfNeeded() michael@0: { michael@0: if (mCurrentRequest && michael@0: (mCurrentRequestFlags & REQUEST_NEEDS_ANIMATION_RESET)) { michael@0: nsCOMPtr container; michael@0: mCurrentRequest->GetImage(getter_AddRefs(container)); michael@0: if (container) michael@0: container->ResetAnimation(); michael@0: mCurrentRequestFlags &= ~REQUEST_NEEDS_ANIMATION_RESET; michael@0: } michael@0: } michael@0: michael@0: bool michael@0: nsImageLoadingContent::HaveSize(imgIRequest *aImage) michael@0: { michael@0: // Handle the null case michael@0: if (!aImage) michael@0: return false; michael@0: michael@0: // Query the image michael@0: uint32_t status; michael@0: nsresult rv = aImage->GetImageStatus(&status); michael@0: return (NS_SUCCEEDED(rv) && (status & imgIRequest::STATUS_SIZE_AVAILABLE)); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::BindToTree(nsIDocument* aDocument, nsIContent* aParent, michael@0: nsIContent* aBindingParent, michael@0: bool aCompileEventHandlers) michael@0: { michael@0: // We may be entering the document, so if our image should be tracked, michael@0: // track it. michael@0: if (!aDocument) michael@0: return; michael@0: michael@0: TrackImage(mCurrentRequest); michael@0: TrackImage(mPendingRequest); michael@0: michael@0: if (mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD) michael@0: aDocument->BlockOnload(); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::UnbindFromTree(bool aDeep, bool aNullParent) michael@0: { michael@0: // We may be leaving the document, so if our image is tracked, untrack it. michael@0: nsCOMPtr doc = GetOurCurrentDoc(); michael@0: if (!doc) michael@0: return; michael@0: michael@0: UntrackImage(mCurrentRequest); michael@0: UntrackImage(mPendingRequest); michael@0: michael@0: if (mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD) michael@0: doc->UnblockOnload(false); michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::TrackImage(imgIRequest* aImage) michael@0: { michael@0: if (!aImage) michael@0: return; michael@0: michael@0: MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest, michael@0: "Why haven't we heard of this request?"); michael@0: michael@0: nsIDocument* doc = GetOurCurrentDoc(); michael@0: if (doc && (mFrameCreateCalled || GetOurPrimaryFrame()) && michael@0: (mVisibleCount > 0)) { michael@0: if (aImage == mCurrentRequest && !(mCurrentRequestFlags & REQUEST_IS_TRACKED)) { michael@0: mCurrentRequestFlags |= REQUEST_IS_TRACKED; michael@0: doc->AddImage(mCurrentRequest); michael@0: } michael@0: if (aImage == mPendingRequest && !(mPendingRequestFlags & REQUEST_IS_TRACKED)) { michael@0: mPendingRequestFlags |= REQUEST_IS_TRACKED; michael@0: doc->AddImage(mPendingRequest); michael@0: } michael@0: } michael@0: } michael@0: michael@0: void michael@0: nsImageLoadingContent::UntrackImage(imgIRequest* aImage, uint32_t aFlags /* = 0 */) michael@0: { michael@0: if (!aImage) michael@0: return; michael@0: michael@0: MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest, michael@0: "Why haven't we heard of this request?"); michael@0: michael@0: // We may not be in the document. If we outlived our document that's fine, michael@0: // because the document empties out the tracker and unlocks all locked images michael@0: // on destruction. But if we were never in the document we may need to force michael@0: // discarding the image here, since this is the only chance we have. michael@0: nsIDocument* doc = GetOurCurrentDoc(); michael@0: if (aImage == mCurrentRequest) { michael@0: if (doc && (mCurrentRequestFlags & REQUEST_IS_TRACKED)) { michael@0: mCurrentRequestFlags &= ~REQUEST_IS_TRACKED; michael@0: doc->RemoveImage(mCurrentRequest, michael@0: (aFlags & REQUEST_DISCARD) ? nsIDocument::REQUEST_DISCARD : 0); michael@0: } michael@0: else if (aFlags & REQUEST_DISCARD) { michael@0: // If we're not in the document we may still need to be discarded. michael@0: aImage->RequestDiscard(); michael@0: } michael@0: } michael@0: if (aImage == mPendingRequest) { michael@0: if (doc && (mPendingRequestFlags & REQUEST_IS_TRACKED)) { michael@0: mPendingRequestFlags &= ~REQUEST_IS_TRACKED; michael@0: doc->RemoveImage(mPendingRequest, michael@0: (aFlags & REQUEST_DISCARD) ? nsIDocument::REQUEST_DISCARD : 0); michael@0: } michael@0: else if (aFlags & REQUEST_DISCARD) { michael@0: // If we're not in the document we may still need to be discarded. michael@0: aImage->RequestDiscard(); michael@0: } michael@0: } michael@0: } michael@0: michael@0: michael@0: void michael@0: nsImageLoadingContent::CreateStaticImageClone(nsImageLoadingContent* aDest) const michael@0: { michael@0: aDest->mCurrentRequest = nsContentUtils::GetStaticRequest(mCurrentRequest); michael@0: aDest->TrackImage(aDest->mCurrentRequest); michael@0: aDest->mForcedImageState = mForcedImageState; michael@0: aDest->mImageBlockingStatus = mImageBlockingStatus; michael@0: aDest->mLoadingEnabled = mLoadingEnabled; michael@0: aDest->mStateChangerDepth = mStateChangerDepth; michael@0: aDest->mIsImageStateForced = mIsImageStateForced; michael@0: aDest->mLoading = mLoading; michael@0: aDest->mBroken = mBroken; michael@0: aDest->mUserDisabled = mUserDisabled; michael@0: aDest->mSuppressed = mSuppressed; michael@0: } michael@0: michael@0: CORSMode michael@0: nsImageLoadingContent::GetCORSMode() michael@0: { michael@0: return CORS_NONE; michael@0: } michael@0: michael@0: nsImageLoadingContent::ImageObserver::ImageObserver(imgINotificationObserver* aObserver) michael@0: : mObserver(aObserver) michael@0: , mNext(nullptr) michael@0: { michael@0: MOZ_COUNT_CTOR(ImageObserver); michael@0: } michael@0: michael@0: nsImageLoadingContent::ImageObserver::~ImageObserver() michael@0: { michael@0: MOZ_COUNT_DTOR(ImageObserver); michael@0: NS_CONTENT_DELETE_LIST_MEMBER(ImageObserver, this, mNext); michael@0: }