2015-05-03 22:32:37 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2006-03-31 12:00:42 +04:00
|
|
|
/*
|
|
|
|
* A base class which implements nsIImageLoadingContent and can be
|
|
|
|
* subclassed by various content nodes that want to provide image
|
|
|
|
* loading functionality (eg <img>, <object>, etc).
|
|
|
|
*/
|
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
#include "nsImageLoadingContent.h"
|
2012-07-27 18:03:27 +04:00
|
|
|
#include "nsError.h"
|
2003-03-19 04:08:22 +03:00
|
|
|
#include "nsIContent.h"
|
2019-01-02 16:05:23 +03:00
|
|
|
#include "mozilla/dom/Document.h"
|
2003-03-19 04:08:22 +03:00
|
|
|
#include "nsIScriptGlobalObject.h"
|
|
|
|
#include "nsIDOMWindow.h"
|
2004-11-25 01:48:45 +03:00
|
|
|
#include "nsServiceManagerUtils.h"
|
2018-07-04 20:26:09 +03:00
|
|
|
#include "nsContentList.h"
|
2003-03-19 04:08:22 +03:00
|
|
|
#include "nsContentPolicyUtils.h"
|
|
|
|
#include "nsIURI.h"
|
|
|
|
#include "nsILoadGroup.h"
|
|
|
|
#include "imgIContainer.h"
|
2012-11-16 22:24:58 +04:00
|
|
|
#include "imgLoader.h"
|
2012-10-12 16:43:01 +04:00
|
|
|
#include "imgRequestProxy.h"
|
2006-05-10 21:30:15 +04:00
|
|
|
#include "nsThreadUtils.h"
|
2003-03-19 04:08:22 +03:00
|
|
|
#include "nsNetUtil.h"
|
2012-03-20 02:22:02 +04:00
|
|
|
#include "nsImageFrame.h"
|
2017-05-23 22:11:13 +03:00
|
|
|
#include "nsSVGImageFrame.h"
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2003-04-02 07:27:20 +04:00
|
|
|
#include "nsIChannel.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
|
2003-04-16 05:53:48 +04:00
|
|
|
#include "nsIFrame.h"
|
|
|
|
|
2004-03-09 06:57:51 +03:00
|
|
|
#include "nsContentUtils.h"
|
2011-11-10 01:39:16 +04:00
|
|
|
#include "nsLayoutUtils.h"
|
2005-03-02 07:05:12 +03:00
|
|
|
#include "nsIContentPolicy.h"
|
2017-08-30 16:14:46 +03:00
|
|
|
#include "SVGObserverUtils.h"
|
2004-03-09 06:57:51 +03:00
|
|
|
|
2015-07-13 21:25:13 +03:00
|
|
|
#include "gfxPrefs.h"
|
|
|
|
|
2008-04-11 21:29:06 +04:00
|
|
|
#include "mozAutoDocUpdate.h"
|
2014-03-17 10:56:54 +04:00
|
|
|
#include "mozilla/AsyncEventDispatcher.h"
|
2017-03-16 11:09:50 +03:00
|
|
|
#include "mozilla/AutoRestore.h"
|
2017-04-25 04:17:38 +03:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2014-04-03 08:18:36 +04:00
|
|
|
#include "mozilla/EventStates.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2016-10-13 10:04:15 +03:00
|
|
|
#include "mozilla/dom/ImageTracker.h"
|
2013-12-12 05:51:58 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2019-01-30 15:43:34 +03:00
|
|
|
#include "mozilla/net/UrlClassifierFeatureFactory.h"
|
2015-06-06 01:25:24 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
2019-04-03 15:40:26 +03:00
|
|
|
#include "mozilla/PresShell.h"
|
2008-04-11 21:29:06 +04:00
|
|
|
|
2014-03-12 05:11:37 +04:00
|
|
|
#ifdef LoadImage
|
2013-12-02 14:26:11 +04:00
|
|
|
// Undefine LoadImage to prevent naming conflict with Windows.
|
|
|
|
# undef LoadImage
|
|
|
|
#endif
|
|
|
|
|
2012-03-10 20:13:51 +04:00
|
|
|
using namespace mozilla;
|
2016-10-13 10:04:15 +03:00
|
|
|
using namespace mozilla::dom;
|
2012-03-10 20:13:51 +04:00
|
|
|
|
2004-06-05 21:57:00 +04:00
|
|
|
#ifdef DEBUG_chb
|
|
|
|
static void PrintReqURL(imgIRequest* req) {
|
|
|
|
if (!req) {
|
|
|
|
printf("(null req)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
req->GetURI(getter_AddRefs(uri));
|
|
|
|
if (!uri) {
|
|
|
|
printf("(null uri)\n");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2012-09-02 06:35:17 +04:00
|
|
|
nsAutoCString spec;
|
2004-06-05 21:57:00 +04:00
|
|
|
uri->GetSpec(spec);
|
|
|
|
printf("spec='%s'\n", spec.get());
|
|
|
|
}
|
|
|
|
#endif /* DEBUG_chb */
|
|
|
|
|
2018-08-08 14:56:01 +03:00
|
|
|
const nsAttrValue::EnumTable nsImageLoadingContent::kDecodingTable[] = {
|
|
|
|
{"auto", nsImageLoadingContent::ImageDecodingType::Auto},
|
|
|
|
{"async", nsImageLoadingContent::ImageDecodingType::Async},
|
|
|
|
{"sync", nsImageLoadingContent::ImageDecodingType::Sync},
|
|
|
|
{nullptr, 0}};
|
|
|
|
|
|
|
|
const nsAttrValue::EnumTable* nsImageLoadingContent::kDecodingTableDefault =
|
|
|
|
&nsImageLoadingContent::kDecodingTable[0];
|
2004-05-13 22:34:18 +04:00
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
nsImageLoadingContent::nsImageLoadingContent()
|
2012-08-14 02:11:50 +04:00
|
|
|
: mCurrentRequestFlags(0),
|
|
|
|
mPendingRequestFlags(0),
|
|
|
|
mObserverList(nullptr),
|
2018-11-08 20:45:50 +03:00
|
|
|
mOutstandingDecodePromises(0),
|
|
|
|
mRequestGeneration(0),
|
2005-03-02 07:05:12 +03:00
|
|
|
mImageBlockingStatus(nsIContentPolicy::ACCEPT),
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoadingEnabled(true),
|
|
|
|
mIsImageStateForced(false),
|
|
|
|
mLoading(false),
|
2005-09-18 22:05:40 +04:00
|
|
|
// mBroken starts out true, since an image without a URI is broken....
|
2011-10-17 18:59:28 +04:00
|
|
|
mBroken(true),
|
|
|
|
mUserDisabled(false),
|
|
|
|
mSuppressed(false),
|
|
|
|
mNewRequestsWillNeedAnimationReset(false),
|
2017-04-25 04:17:38 +03:00
|
|
|
mUseUrgentStartForChannel(false),
|
2011-11-10 01:39:16 +04:00
|
|
|
mStateChangerDepth(0),
|
|
|
|
mCurrentRequestRegistered(false),
|
2017-03-16 11:09:50 +03:00
|
|
|
mPendingRequestRegistered(false),
|
2018-08-08 14:56:01 +03:00
|
|
|
mIsStartingImageLoad(false),
|
|
|
|
mSyncDecodingHint(false) {
|
2015-02-13 05:44:32 +03:00
|
|
|
if (!nsContentUtils::GetImgLoaderForChannel(nullptr, nullptr)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoadingEnabled = false;
|
2005-09-21 23:14:30 +04:00
|
|
|
}
|
2015-07-08 00:00:08 +03:00
|
|
|
|
2017-04-26 07:55:54 +03:00
|
|
|
mMostRecentRequestChange = TimeStamp::ProcessCreation();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2006-06-01 22:35:21 +04:00
|
|
|
void nsImageLoadingContent::DestroyImageLoadingContent() {
|
2003-03-19 04:08:22 +03:00
|
|
|
// Cancel our requests so they won't hold stale refs to us
|
2013-04-09 20:50:35 +04:00
|
|
|
// NB: Don't ask to discard the images here.
|
2018-11-08 20:45:50 +03:00
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INVALID_REQUEST);
|
2016-03-26 00:49:43 +03:00
|
|
|
ClearCurrentRequest(NS_BINDING_ABORTED);
|
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED);
|
2006-06-01 22:35:21 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsImageLoadingContent::~nsImageLoadingContent() {
|
2018-11-08 20:45:50 +03:00
|
|
|
MOZ_ASSERT(!mCurrentRequest && !mPendingRequest,
|
|
|
|
"DestroyImageLoadingContent not called");
|
|
|
|
MOZ_ASSERT(!mObserverList.mObserver && !mObserverList.mNext,
|
|
|
|
"Observers still registered?");
|
|
|
|
MOZ_ASSERT(mScriptedObservers.IsEmpty(),
|
|
|
|
"Scripted observers still registered?");
|
|
|
|
MOZ_ASSERT(mOutstandingDecodePromises == 0,
|
|
|
|
"Decode promises still unfulfilled?");
|
|
|
|
MOZ_ASSERT(mDecodePromises.IsEmpty(), "Decode promises still unfulfilled?");
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-10-12 20:11:22 +04:00
|
|
|
* imgINotificationObserver impl
|
2003-03-19 04:08:22 +03:00
|
|
|
*/
|
|
|
|
NS_IMETHODIMP
|
2012-10-12 20:11:22 +04:00
|
|
|
nsImageLoadingContent::Notify(imgIRequest* aRequest, int32_t aType,
|
|
|
|
const nsIntRect* aData) {
|
2018-05-16 21:51:17 +03:00
|
|
|
MOZ_ASSERT(aRequest, "no request?");
|
|
|
|
MOZ_ASSERT(aRequest == mCurrentRequest || aRequest == mPendingRequest,
|
|
|
|
"Forgot to cancel a previous request?");
|
|
|
|
|
2012-10-12 20:11:22 +04:00
|
|
|
if (aType == imgINotificationObserver::IS_ANIMATED) {
|
|
|
|
return OnImageIsAnimated(aRequest);
|
|
|
|
}
|
2011-02-01 07:50:01 +03:00
|
|
|
|
2013-02-25 04:59:22 +04:00
|
|
|
if (aType == imgINotificationObserver::UNLOCKED_DRAW) {
|
|
|
|
OnUnlockedDraw();
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2013-01-18 21:49:59 +04:00
|
|
|
{
|
2017-06-29 01:45:55 +03:00
|
|
|
// Calling Notify on observers can modify the list of observers so make
|
|
|
|
// a local copy.
|
|
|
|
AutoTArray<nsCOMPtr<imgINotificationObserver>, 2> observers;
|
2013-01-18 21:49:59 +04:00
|
|
|
for (ImageObserver *observer = &mObserverList, *next; observer;
|
|
|
|
observer = next) {
|
|
|
|
next = observer->mNext;
|
|
|
|
if (observer->mObserver) {
|
2017-06-29 01:45:55 +03:00
|
|
|
observers.AppendElement(observer->mObserver);
|
2013-01-18 21:49:59 +04:00
|
|
|
}
|
|
|
|
}
|
2017-06-29 01:45:55 +03:00
|
|
|
|
|
|
|
nsAutoScriptBlocker scriptBlocker;
|
|
|
|
|
|
|
|
for (auto& observer : observers) {
|
|
|
|
observer->Notify(aRequest, aType, aData);
|
|
|
|
}
|
2013-01-18 21:49:59 +04:00
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
// Have to check for state changes here, since we might have been in
|
|
|
|
// the LOADING state before.
|
|
|
|
UpdateImageState(true);
|
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2012-10-12 20:11:23 +04:00
|
|
|
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
|
2012-10-12 20:11:22 +04:00
|
|
|
uint32_t reqStatus;
|
|
|
|
aRequest->GetImageStatus(&reqStatus);
|
2014-06-25 04:37:03 +04:00
|
|
|
/* triage STATUS_ERROR */
|
|
|
|
if (reqStatus & imgIRequest::STATUS_ERROR) {
|
|
|
|
nsresult errorCode = NS_OK;
|
|
|
|
aRequest->GetImageErrorCode(&errorCode);
|
|
|
|
|
2019-01-30 15:43:34 +03:00
|
|
|
/* Handle image not loading error because source was a tracking URL (or
|
|
|
|
* fingerprinting, cryptomining, etc).
|
2014-08-08 00:35:03 +04:00
|
|
|
* We make a note of this image node by including it in a dedicated
|
|
|
|
* array of blocked tracking nodes under its parent document.
|
2014-06-25 04:37:03 +04:00
|
|
|
*/
|
2019-01-30 15:43:34 +03:00
|
|
|
if (net::UrlClassifierFeatureFactory::IsClassifierBlockingErrorCode(
|
|
|
|
errorCode)) {
|
2014-06-25 04:37:03 +04:00
|
|
|
nsCOMPtr<nsIContent> thisNode =
|
|
|
|
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetOurOwnerDoc();
|
2019-01-30 15:43:34 +03:00
|
|
|
doc->AddBlockedNodeByClassifier(thisNode);
|
2014-06-25 04:37:03 +04:00
|
|
|
}
|
|
|
|
}
|
2012-10-12 20:11:22 +04:00
|
|
|
nsresult status =
|
|
|
|
reqStatus & imgIRequest::STATUS_ERROR ? NS_ERROR_FAILURE : NS_OK;
|
2014-11-18 01:29:56 +03:00
|
|
|
return OnLoadComplete(aRequest, status);
|
2012-10-12 20:11:22 +04:00
|
|
|
}
|
2012-10-12 05:58:24 +04:00
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
if (aType == imgINotificationObserver::FRAME_COMPLETE &&
|
|
|
|
mCurrentRequest == aRequest) {
|
|
|
|
MaybeResolveDecodePromises();
|
|
|
|
}
|
|
|
|
|
2014-08-28 22:43:21 +04:00
|
|
|
if (aType == imgINotificationObserver::DECODE_COMPLETE) {
|
2015-06-03 20:42:07 +03:00
|
|
|
nsCOMPtr<imgIContainer> container;
|
|
|
|
aRequest->GetImage(getter_AddRefs(container));
|
|
|
|
if (container) {
|
|
|
|
container->PropagateUseCounters(GetOurOwnerDoc());
|
|
|
|
}
|
|
|
|
|
2013-02-15 20:48:17 +04:00
|
|
|
UpdateImageState(true);
|
|
|
|
}
|
|
|
|
|
2012-10-12 05:58:24 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-11-18 01:29:56 +03:00
|
|
|
nsresult nsImageLoadingContent::OnLoadComplete(imgIRequest* aRequest,
|
|
|
|
nsresult aStatus) {
|
2012-10-12 20:11:22 +04:00
|
|
|
uint32_t oldStatus;
|
|
|
|
aRequest->GetImageStatus(&oldStatus);
|
|
|
|
|
|
|
|
// XXXjdm This occurs when we have a pending request created, then another
|
|
|
|
// pending request replaces it before the first one is finished.
|
|
|
|
// This begs the question of what the correct behaviour is; we used
|
|
|
|
// to not have to care because we ran this code in OnStopDecode which
|
|
|
|
// wasn't called when the first request was cancelled. For now, I choose
|
|
|
|
// to punt when the given request doesn't appear to have terminated in
|
|
|
|
// an expected state.
|
|
|
|
if (!(oldStatus &
|
|
|
|
(imgIRequest::STATUS_ERROR | imgIRequest::STATUS_LOAD_COMPLETE)))
|
|
|
|
return NS_OK;
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Our state may change. Watch it.
|
2011-10-17 18:59:28 +04:00
|
|
|
AutoStateChanger changer(this, true);
|
2009-09-13 02:44:18 +04:00
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// If the pending request is loaded, switch to it.
|
|
|
|
if (aRequest == mPendingRequest) {
|
2012-03-20 02:22:02 +04:00
|
|
|
MakePendingRequestCurrent();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(aRequest == mCurrentRequest,
|
|
|
|
"One way or another, we should be current by now");
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2015-06-20 01:55:12 +03:00
|
|
|
// Fire the appropriate DOM event.
|
|
|
|
if (NS_SUCCEEDED(aStatus)) {
|
|
|
|
FireEvent(NS_LITERAL_STRING("load"));
|
2016-08-16 07:01:12 +03:00
|
|
|
|
|
|
|
// Do not fire loadend event for multipart/x-mixed-replace image streams.
|
|
|
|
bool isMultipart;
|
|
|
|
if (NS_FAILED(aRequest->GetMultipart(&isMultipart)) || !isMultipart) {
|
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
} else {
|
2015-06-20 01:55:12 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
2016-08-16 07:01:12 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2012-08-14 02:58:53 +04:00
|
|
|
nsCOMPtr<nsINode> thisNode =
|
|
|
|
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
|
2017-08-30 17:58:31 +03:00
|
|
|
SVGObserverUtils::InvalidateDirectRenderingObservers(thisNode->AsElement());
|
2018-11-08 20:45:50 +03:00
|
|
|
MaybeResolveDecodePromises();
|
2010-08-13 17:36:13 +04:00
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2015-06-03 04:29:50 +03:00
|
|
|
static bool ImageIsAnimated(imgIRequest* aRequest) {
|
|
|
|
if (!aRequest) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (NS_SUCCEEDED(aRequest->GetImage(getter_AddRefs(image)))) {
|
|
|
|
bool isAnimated = false;
|
|
|
|
nsresult rv = image->GetAnimated(&isAnimated);
|
|
|
|
if (NS_SUCCEEDED(rv) && isAnimated) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-02-25 04:59:22 +04:00
|
|
|
void nsImageLoadingContent::OnUnlockedDraw() {
|
2016-03-26 00:49:43 +03:00
|
|
|
// It's OK for non-animated images to wait until the next frame visibility
|
2015-06-03 04:29:50 +03:00
|
|
|
// update to become locked. (And that's preferable, since in the case of
|
|
|
|
// scrolling it keeps memory usage minimal.) For animated images, though, we
|
|
|
|
// want to mark them visible right away so we can call
|
|
|
|
// IncrementAnimationConsumers() on them and they'll start animating.
|
|
|
|
if (!ImageIsAnimated(mCurrentRequest) && !ImageIsAnimated(mPendingRequest)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2016-03-26 00:49:43 +03:00
|
|
|
nsIFrame* frame = GetOurPrimaryFrame();
|
|
|
|
if (!frame) {
|
2013-02-25 04:59:22 +04:00
|
|
|
return;
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:19:08 +03:00
|
|
|
if (frame->GetVisibility() == Visibility::APPROXIMATELY_VISIBLE) {
|
|
|
|
// This frame is already marked visible; there's nothing to do.
|
|
|
|
return;
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* presContext = frame->PresContext();
|
|
|
|
if (!presContext) {
|
|
|
|
return;
|
|
|
|
}
|
2013-02-25 04:59:22 +04:00
|
|
|
|
2019-04-03 15:40:26 +03:00
|
|
|
PresShell* presShell = presContext->GetPresShell();
|
2016-03-26 00:49:43 +03:00
|
|
|
if (!presShell) {
|
2013-02-25 04:59:22 +04:00
|
|
|
return;
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
2013-02-25 04:59:22 +04:00
|
|
|
|
2016-09-12 08:19:08 +03:00
|
|
|
presShell->EnsureFrameInApproximatelyVisibleList(frame);
|
2013-02-25 04:59:22 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:15 +04:00
|
|
|
nsresult nsImageLoadingContent::OnImageIsAnimated(imgIRequest* aRequest) {
|
2011-11-10 01:39:16 +04:00
|
|
|
bool* requestFlag = GetRegisteredFlagForRequest(aRequest);
|
|
|
|
if (requestFlag) {
|
|
|
|
nsLayoutUtils::RegisterImageRequest(GetFramePresContext(), aRequest,
|
|
|
|
requestFlag);
|
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:15 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
/*
|
2003-04-02 07:27:20 +04:00
|
|
|
* nsIImageLoadingContent impl
|
2003-03-19 04:08:22 +03:00
|
|
|
*/
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsImageLoadingContent::SetLoadingEnabled(bool aLoadingEnabled) {
|
2015-02-13 05:44:32 +03:00
|
|
|
if (nsContentUtils::GetImgLoaderForChannel(nullptr, nullptr)) {
|
2003-05-02 23:21:00 +04:00
|
|
|
mLoadingEnabled = aLoadingEnabled;
|
2005-09-21 23:14:30 +04:00
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
already_AddRefed<Promise> nsImageLoadingContent::QueueDecodeAsync(
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
Document* doc = GetOurOwnerDoc();
|
|
|
|
RefPtr<Promise> promise = Promise::Create(doc->GetScopeObject(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
class QueueDecodeTask final : public Runnable {
|
|
|
|
public:
|
|
|
|
QueueDecodeTask(nsImageLoadingContent* aOwner, Promise* aPromise,
|
|
|
|
uint32_t aRequestGeneration)
|
|
|
|
: Runnable("nsImageLoadingContent::QueueDecodeTask"),
|
|
|
|
mOwner(aOwner),
|
|
|
|
mPromise(aPromise),
|
|
|
|
mRequestGeneration(aRequestGeneration) {}
|
|
|
|
|
|
|
|
NS_IMETHOD Run() override {
|
|
|
|
mOwner->DecodeAsync(std::move(mPromise), mRequestGeneration);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<nsImageLoadingContent> mOwner;
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
uint32_t mRequestGeneration;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (++mOutstandingDecodePromises == 1) {
|
|
|
|
MOZ_ASSERT(mDecodePromises.IsEmpty());
|
|
|
|
doc->RegisterActivityObserver(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
auto task = MakeRefPtr<QueueDecodeTask>(this, promise, mRequestGeneration);
|
|
|
|
nsContentUtils::RunInStableState(task.forget());
|
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::DecodeAsync(RefPtr<Promise>&& aPromise,
|
|
|
|
uint32_t aRequestGeneration) {
|
|
|
|
MOZ_ASSERT(nsContentUtils::IsInStableOrMetaStableState());
|
|
|
|
MOZ_ASSERT(aPromise);
|
|
|
|
MOZ_ASSERT(mOutstandingDecodePromises > mDecodePromises.Length());
|
|
|
|
|
|
|
|
// The request may have gotten updated since the decode call was issued.
|
|
|
|
if (aRequestGeneration != mRequestGeneration) {
|
|
|
|
aPromise->MaybeReject(NS_ERROR_DOM_IMAGE_INVALID_REQUEST);
|
|
|
|
// We never got placed in mDecodePromises, so we must ensure we decrement
|
|
|
|
// the counter explicitly.
|
|
|
|
--mOutstandingDecodePromises;
|
|
|
|
MaybeDeregisterActivityObserver();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool wasEmpty = mDecodePromises.IsEmpty();
|
|
|
|
mDecodePromises.AppendElement(std::move(aPromise));
|
|
|
|
if (wasEmpty) {
|
|
|
|
MaybeResolveDecodePromises();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::MaybeResolveDecodePromises() {
|
|
|
|
if (mDecodePromises.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!mCurrentRequest) {
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INVALID_REQUEST);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only can resolve if our document is the active document. If not we are
|
|
|
|
// supposed to reject the promise, even if it was fulfilled successfully.
|
|
|
|
if (!GetOurOwnerDoc()->IsCurrentActiveDocument()) {
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INACTIVE_DOCUMENT);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If any error occurred while decoding, we need to reject first.
|
|
|
|
uint32_t status = imgIRequest::STATUS_NONE;
|
|
|
|
mCurrentRequest->GetImageStatus(&status);
|
|
|
|
if (status & imgIRequest::STATUS_ERROR) {
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_BROKEN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We need the size to bother with requesting a decode, as we are either
|
|
|
|
// blocked on validation or metadata decoding.
|
|
|
|
if (!(status & imgIRequest::STATUS_SIZE_AVAILABLE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check the surface cache status and/or request decoding begin. We do this
|
|
|
|
// before LOAD_COMPLETE because we want to start as soon as possible.
|
|
|
|
uint32_t flags = imgIContainer::FLAG_HIGH_QUALITY_SCALING |
|
|
|
|
imgIContainer::FLAG_AVOID_REDECODE_FOR_SIZE;
|
|
|
|
if (!mCurrentRequest->RequestDecodeWithResult(flags)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// We can only fulfill the promises once we have all the data.
|
|
|
|
if (!(status & imgIRequest::STATUS_LOAD_COMPLETE)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& promise : mDecodePromises) {
|
|
|
|
promise->MaybeResolveWithUndefined();
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mOutstandingDecodePromises >= mDecodePromises.Length());
|
|
|
|
mOutstandingDecodePromises -= mDecodePromises.Length();
|
|
|
|
mDecodePromises.Clear();
|
|
|
|
MaybeDeregisterActivityObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::RejectDecodePromises(nsresult aStatus) {
|
|
|
|
if (mDecodePromises.IsEmpty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (auto& promise : mDecodePromises) {
|
|
|
|
promise->MaybeReject(aStatus);
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(mOutstandingDecodePromises >= mDecodePromises.Length());
|
|
|
|
mOutstandingDecodePromises -= mDecodePromises.Length();
|
|
|
|
mDecodePromises.Clear();
|
|
|
|
MaybeDeregisterActivityObserver();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::MaybeAgeRequestGeneration(nsIURI* aNewURI) {
|
|
|
|
MOZ_ASSERT(mCurrentRequest);
|
|
|
|
|
|
|
|
// If the current request is about to change, we need to verify if the new
|
|
|
|
// URI matches the existing current request's URI. If it doesn't, we need to
|
|
|
|
// reject any outstanding promises due to the current request mutating as per
|
|
|
|
// step 2.2 of the decode API requirements.
|
|
|
|
//
|
|
|
|
// https://html.spec.whatwg.org/multipage/embedded-content.html#dom-img-decode
|
|
|
|
if (aNewURI) {
|
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
mCurrentRequest->GetURI(getter_AddRefs(currentURI));
|
|
|
|
|
|
|
|
bool equal = false;
|
|
|
|
if (NS_SUCCEEDED(aNewURI->Equals(currentURI, &equal)) && equal) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
++mRequestGeneration;
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INVALID_REQUEST);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::MaybeDeregisterActivityObserver() {
|
|
|
|
if (mOutstandingDecodePromises == 0) {
|
|
|
|
MOZ_ASSERT(mDecodePromises.IsEmpty());
|
|
|
|
GetOurOwnerDoc()->UnregisterActivityObserver(this);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-08 14:56:01 +03:00
|
|
|
void nsImageLoadingContent::SetSyncDecodingHint(bool aHint) {
|
|
|
|
if (mSyncDecodingHint == aHint) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSyncDecodingHint = aHint;
|
|
|
|
MaybeForceSyncDecoding(/* aPrepareNextRequest */ false);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::MaybeForceSyncDecoding(
|
|
|
|
bool aPrepareNextRequest, nsIFrame* aFrame /* = nullptr */) {
|
|
|
|
nsIFrame* frame = aFrame ? aFrame : GetOurPrimaryFrame();
|
|
|
|
nsImageFrame* imageFrame = do_QueryFrame(frame);
|
|
|
|
nsSVGImageFrame* svgImageFrame = do_QueryFrame(frame);
|
|
|
|
if (!imageFrame && !svgImageFrame) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool forceSync = mSyncDecodingHint;
|
|
|
|
if (!forceSync && aPrepareNextRequest) {
|
|
|
|
// Detect JavaScript-based animations created by changing the |src|
|
|
|
|
// attribute on a timer.
|
|
|
|
TimeStamp now = TimeStamp::Now();
|
|
|
|
TimeDuration threshold = TimeDuration::FromMilliseconds(
|
|
|
|
gfxPrefs::ImageInferSrcAnimationThresholdMS());
|
|
|
|
|
|
|
|
// If the length of time between request changes is less than the threshold,
|
|
|
|
// then force sync decoding to eliminate flicker from the animation.
|
|
|
|
forceSync = (now - mMostRecentRequestChange < threshold);
|
|
|
|
mMostRecentRequestChange = now;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (imageFrame) {
|
|
|
|
imageFrame->SetForceSyncDecoding(forceSync);
|
|
|
|
} else {
|
|
|
|
svgImageFrame->SetForceSyncDecoding(forceSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-20 04:20:17 +03:00
|
|
|
int16_t nsImageLoadingContent::GetImageBlockingStatus() {
|
|
|
|
return ImageBlockingStatus();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
2003-07-24 21:30:52 +04:00
|
|
|
|
2014-11-25 10:42:43 +03:00
|
|
|
static void ReplayImageStatus(imgIRequest* aRequest,
|
|
|
|
imgINotificationObserver* aObserver) {
|
|
|
|
if (!aRequest) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t status = 0;
|
|
|
|
nsresult rv = aRequest->GetImageStatus(&status);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (status & imgIRequest::STATUS_SIZE_AVAILABLE) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::SIZE_AVAILABLE,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
if (status & imgIRequest::STATUS_FRAME_COMPLETE) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::FRAME_COMPLETE,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
if (status & imgIRequest::STATUS_HAS_TRANSPARENCY) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::HAS_TRANSPARENCY,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
if (status & imgIRequest::STATUS_IS_ANIMATED) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::IS_ANIMATED, nullptr);
|
|
|
|
}
|
|
|
|
if (status & imgIRequest::STATUS_DECODE_COMPLETE) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::DECODE_COMPLETE,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
if (status & imgIRequest::STATUS_LOAD_COMPLETE) {
|
|
|
|
aObserver->Notify(aRequest, imgINotificationObserver::LOAD_COMPLETE,
|
|
|
|
nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
void nsImageLoadingContent::AddNativeObserver(
|
|
|
|
imgINotificationObserver* aObserver) {
|
2018-01-19 22:19:39 +03:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
|
|
|
return;
|
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
|
|
|
|
if (!mObserverList.mObserver) {
|
|
|
|
// Don't touch the linking of the list!
|
2014-11-25 10:42:43 +03:00
|
|
|
mObserverList.mObserver = aObserver;
|
|
|
|
|
|
|
|
ReplayImageStatus(mCurrentRequest, aObserver);
|
|
|
|
ReplayImageStatus(mPendingRequest, aObserver);
|
|
|
|
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise we have to create a new entry
|
|
|
|
|
|
|
|
ImageObserver* observer = &mObserverList;
|
|
|
|
while (observer->mNext) {
|
|
|
|
observer = observer->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
observer->mNext = new ImageObserver(aObserver);
|
2014-11-25 10:42:43 +03:00
|
|
|
ReplayImageStatus(mCurrentRequest, aObserver);
|
|
|
|
ReplayImageStatus(mPendingRequest, aObserver);
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
void nsImageLoadingContent::RemoveNativeObserver(
|
|
|
|
imgINotificationObserver* aObserver) {
|
2018-01-19 22:19:39 +03:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
|
|
|
return;
|
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
|
|
|
|
if (mObserverList.mObserver == aObserver) {
|
2012-07-30 18:20:58 +04:00
|
|
|
mObserverList.mObserver = nullptr;
|
2003-03-19 04:08:22 +03:00
|
|
|
// Don't touch the linking of the list!
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise have to find it and splice it out
|
|
|
|
ImageObserver* observer = &mObserverList;
|
|
|
|
while (observer->mNext && observer->mNext->mObserver != aObserver) {
|
|
|
|
observer = observer->mNext;
|
|
|
|
}
|
|
|
|
|
|
|
|
// At this point, we are pointing to the list element whose mNext is
|
|
|
|
// the right observer (assuming of course that mNext is not null)
|
|
|
|
if (observer->mNext) {
|
|
|
|
// splice it out
|
|
|
|
ImageObserver* oldObserver = observer->mNext;
|
|
|
|
observer->mNext = oldObserver->mNext;
|
2012-07-30 18:20:58 +04:00
|
|
|
oldObserver->mNext = nullptr; // so we don't destroy them all
|
2003-03-19 04:08:22 +03:00
|
|
|
delete oldObserver;
|
|
|
|
}
|
|
|
|
#ifdef DEBUG
|
|
|
|
else {
|
2010-05-23 23:26:15 +04:00
|
|
|
NS_WARNING("Asked to remove nonexistent observer");
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
void nsImageLoadingContent::AddObserver(imgINotificationObserver* aObserver) {
|
2018-01-19 22:19:39 +03:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-19 21:15:11 +03:00
|
|
|
|
|
|
|
RefPtr<imgRequestProxy> currentReq;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
// Scripted observers may not belong to the same document as us, so when we
|
|
|
|
// create the imgRequestProxy, we shouldn't use any. This allows the request
|
|
|
|
// to dispatch notifications from the correct scheduler group.
|
2018-07-18 18:17:22 +03:00
|
|
|
nsresult rv =
|
|
|
|
mCurrentRequest->Clone(aObserver, nullptr, getter_AddRefs(currentReq));
|
2017-07-19 21:15:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2017-07-19 21:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<imgRequestProxy> pendingReq;
|
|
|
|
if (mPendingRequest) {
|
|
|
|
// See above for why we don't use the loading document.
|
2018-07-18 18:17:22 +03:00
|
|
|
nsresult rv =
|
|
|
|
mPendingRequest->Clone(aObserver, nullptr, getter_AddRefs(pendingReq));
|
2017-07-19 21:15:11 +03:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
mCurrentRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2017-07-19 21:15:11 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
mScriptedObservers.AppendElement(new ScriptedImageObserver(
|
2018-05-30 22:15:35 +03:00
|
|
|
aObserver, std::move(currentReq), std::move(pendingReq)));
|
2017-07-19 21:15:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::RemoveObserver(
|
|
|
|
imgINotificationObserver* aObserver) {
|
2018-01-19 22:19:39 +03:00
|
|
|
if (NS_WARN_IF(!aObserver)) {
|
|
|
|
return;
|
|
|
|
}
|
2017-07-19 21:15:11 +03:00
|
|
|
|
|
|
|
if (NS_WARN_IF(mScriptedObservers.IsEmpty())) {
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2017-07-19 21:15:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<ScriptedImageObserver> observer;
|
|
|
|
auto i = mScriptedObservers.Length();
|
|
|
|
do {
|
|
|
|
--i;
|
|
|
|
if (mScriptedObservers[i]->mObserver == aObserver) {
|
2018-05-30 22:15:35 +03:00
|
|
|
observer = std::move(mScriptedObservers[i]);
|
2017-07-19 21:15:11 +03:00
|
|
|
mScriptedObservers.RemoveElementAt(i);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} while (i > 0);
|
|
|
|
|
|
|
|
if (NS_WARN_IF(!observer)) {
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2017-07-19 21:15:11 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// If the cancel causes a mutation, it will be harmless, because we have
|
|
|
|
// already removed the observer from the list.
|
|
|
|
observer->CancelRequests();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::ClearScriptedRequests(int32_t aRequestType,
|
|
|
|
nsresult aReason) {
|
|
|
|
if (MOZ_LIKELY(mScriptedObservers.IsEmpty())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers);
|
|
|
|
auto i = observers.Length();
|
|
|
|
do {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
RefPtr<imgRequestProxy> req;
|
|
|
|
switch (aRequestType) {
|
|
|
|
case CURRENT_REQUEST:
|
2018-05-30 22:15:35 +03:00
|
|
|
req = std::move(observers[i]->mCurrentRequest);
|
2017-07-19 21:15:11 +03:00
|
|
|
break;
|
|
|
|
case PENDING_REQUEST:
|
2018-05-30 22:15:35 +03:00
|
|
|
req = std::move(observers[i]->mPendingRequest);
|
2017-07-19 21:15:11 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown request type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (req) {
|
|
|
|
req->CancelAndForgetObserver(aReason);
|
|
|
|
}
|
|
|
|
} while (i > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::CloneScriptedRequests(imgRequestProxy* aRequest) {
|
|
|
|
MOZ_ASSERT(aRequest);
|
|
|
|
|
|
|
|
if (MOZ_LIKELY(mScriptedObservers.IsEmpty())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool current;
|
|
|
|
if (aRequest == mCurrentRequest) {
|
|
|
|
current = true;
|
|
|
|
} else if (aRequest == mPendingRequest) {
|
|
|
|
current = false;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Unknown request type");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers);
|
|
|
|
auto i = observers.Length();
|
|
|
|
do {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
ScriptedImageObserver* observer = observers[i];
|
|
|
|
RefPtr<imgRequestProxy>& req =
|
|
|
|
current ? observer->mCurrentRequest : observer->mPendingRequest;
|
|
|
|
if (NS_WARN_IF(req)) {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Should have cancelled original request");
|
|
|
|
req->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
|
|
|
req = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsresult rv =
|
|
|
|
aRequest->Clone(observer->mObserver, nullptr, getter_AddRefs(req));
|
|
|
|
Unused << NS_WARN_IF(NS_FAILED(rv));
|
|
|
|
} while (i > 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::MakePendingScriptedRequestsCurrent() {
|
|
|
|
if (MOZ_LIKELY(mScriptedObservers.IsEmpty())) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers);
|
|
|
|
auto i = observers.Length();
|
|
|
|
do {
|
|
|
|
--i;
|
|
|
|
|
|
|
|
ScriptedImageObserver* observer = observers[i];
|
|
|
|
if (observer->mCurrentRequest) {
|
|
|
|
observer->mCurrentRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
|
|
|
}
|
2018-05-30 22:15:35 +03:00
|
|
|
observer->mCurrentRequest = std::move(observer->mPendingRequest);
|
2017-07-19 21:15:11 +03:00
|
|
|
} while (i > 0);
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
already_AddRefed<imgIRequest> nsImageLoadingContent::GetRequest(
|
2013-01-02 23:30:02 +04:00
|
|
|
int32_t aRequestType, ErrorResult& aError) {
|
|
|
|
nsCOMPtr<imgIRequest> request;
|
2003-03-19 04:08:22 +03:00
|
|
|
switch (aRequestType) {
|
|
|
|
case CURRENT_REQUEST:
|
2013-01-02 23:30:02 +04:00
|
|
|
request = mCurrentRequest;
|
2003-03-19 04:08:22 +03:00
|
|
|
break;
|
|
|
|
case PENDING_REQUEST:
|
2013-01-02 23:30:02 +04:00
|
|
|
request = mPendingRequest;
|
2003-03-19 04:08:22 +03:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
NS_ERROR("Unknown request type");
|
2013-01-02 23:30:02 +04:00
|
|
|
aError.Throw(NS_ERROR_UNEXPECTED);
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
2013-01-02 23:30:02 +04:00
|
|
|
|
|
|
|
return request.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsImageLoadingContent::GetRequest(int32_t aRequestType,
|
|
|
|
imgIRequest** aRequest) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aRequest);
|
|
|
|
|
|
|
|
ErrorResult result;
|
2014-03-15 23:00:15 +04:00
|
|
|
*aRequest = GetRequest(aRequestType, result).take();
|
2013-01-02 23:30:02 +04:00
|
|
|
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsImageLoadingContent::FrameCreated(nsIFrame* aFrame) {
|
|
|
|
NS_ASSERTION(aFrame, "aFrame is null");
|
|
|
|
|
2018-08-08 14:56:01 +03:00
|
|
|
MaybeForceSyncDecoding(/* aPrepareNextRequest */ false, aFrame);
|
2017-03-22 08:32:48 +03:00
|
|
|
TrackImage(mCurrentRequest, aFrame);
|
|
|
|
TrackImage(mPendingRequest, aFrame);
|
2013-02-25 04:59:21 +04:00
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
// We need to make sure that our image request is registered, if it should
|
|
|
|
// be registered.
|
2013-09-15 04:05:05 +04:00
|
|
|
nsPresContext* presContext = aFrame->PresContext();
|
2011-11-10 01:39:16 +04:00
|
|
|
if (mCurrentRequest) {
|
|
|
|
nsLayoutUtils::RegisterImageRequestIfAnimated(presContext, mCurrentRequest,
|
|
|
|
&mCurrentRequestRegistered);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPendingRequest) {
|
|
|
|
nsLayoutUtils::RegisterImageRequestIfAnimated(presContext, mPendingRequest,
|
|
|
|
&mPendingRequestRegistered);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP_(void)
|
|
|
|
nsImageLoadingContent::FrameDestroyed(nsIFrame* aFrame) {
|
|
|
|
NS_ASSERTION(aFrame, "aFrame is null");
|
|
|
|
|
|
|
|
// We need to make sure that our image request is deregistered.
|
2014-04-15 09:24:01 +04:00
|
|
|
nsPresContext* presContext = GetFramePresContext();
|
2011-11-10 01:39:16 +04:00
|
|
|
if (mCurrentRequest) {
|
2014-04-15 09:24:01 +04:00
|
|
|
nsLayoutUtils::DeregisterImageRequest(presContext, mCurrentRequest,
|
2011-11-10 01:39:16 +04:00
|
|
|
&mCurrentRequestRegistered);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mPendingRequest) {
|
2014-04-15 09:24:01 +04:00
|
|
|
nsLayoutUtils::DeregisterImageRequest(presContext, mPendingRequest,
|
2011-11-10 01:39:16 +04:00
|
|
|
&mPendingRequestRegistered);
|
|
|
|
}
|
2013-02-14 00:18:08 +04:00
|
|
|
|
2013-02-25 04:59:21 +04:00
|
|
|
UntrackImage(mCurrentRequest);
|
|
|
|
UntrackImage(mPendingRequest);
|
2013-02-25 04:59:23 +04:00
|
|
|
|
2019-04-03 15:40:26 +03:00
|
|
|
PresShell* presShell = presContext ? presContext->GetPresShell() : nullptr;
|
2014-04-15 09:24:01 +04:00
|
|
|
if (presShell) {
|
2016-09-12 08:19:08 +03:00
|
|
|
presShell->RemoveFrameFromApproximatelyVisibleList(aFrame);
|
2013-02-25 04:59:23 +04:00
|
|
|
}
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
/* static */
|
|
|
|
nsContentPolicyType nsImageLoadingContent::PolicyTypeForLoad(
|
|
|
|
ImageLoadType aImageLoadType) {
|
|
|
|
if (aImageLoadType == eImageLoadType_Imageset) {
|
|
|
|
return nsIContentPolicy::TYPE_IMAGESET;
|
|
|
|
}
|
|
|
|
|
|
|
|
MOZ_ASSERT(aImageLoadType == eImageLoadType_Normal,
|
|
|
|
"Unknown ImageLoadType type in PolicyTypeForLoad");
|
2015-09-21 00:55:59 +03:00
|
|
|
return nsIContentPolicy::TYPE_INTERNAL_IMAGE;
|
2014-08-20 01:49:38 +04:00
|
|
|
}
|
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
int32_t nsImageLoadingContent::GetRequestType(imgIRequest* aRequest,
|
2013-01-02 23:30:02 +04:00
|
|
|
ErrorResult& aError) {
|
2003-03-19 04:08:22 +03:00
|
|
|
if (aRequest == mCurrentRequest) {
|
2013-01-02 23:30:02 +04:00
|
|
|
return CURRENT_REQUEST;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
if (aRequest == mPendingRequest) {
|
2013-01-02 23:30:02 +04:00
|
|
|
return PENDING_REQUEST;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
NS_ERROR("Unknown request");
|
2013-01-02 23:30:02 +04:00
|
|
|
aError.Throw(NS_ERROR_UNEXPECTED);
|
|
|
|
return UNKNOWN_REQUEST;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2004-06-05 21:57:00 +04:00
|
|
|
NS_IMETHODIMP
|
2013-01-02 23:30:02 +04:00
|
|
|
nsImageLoadingContent::GetRequestType(imgIRequest* aRequest,
|
|
|
|
int32_t* aRequestType) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aRequestType, "Null out param");
|
2013-01-02 23:30:02 +04:00
|
|
|
|
|
|
|
ErrorResult result;
|
|
|
|
*aRequestType = GetRequestType(aRequest, result);
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-01-02 23:30:02 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
already_AddRefed<nsIURI> nsImageLoadingContent::GetCurrentURI(
|
|
|
|
ErrorResult& aError) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
2005-09-21 23:14:30 +04:00
|
|
|
if (mCurrentRequest) {
|
2013-01-02 23:30:02 +04:00
|
|
|
mCurrentRequest->GetURI(getter_AddRefs(uri));
|
2018-05-09 19:21:24 +03:00
|
|
|
} else {
|
|
|
|
uri = mCurrentURI;
|
2005-09-21 23:14:30 +04:00
|
|
|
}
|
2004-06-05 21:57:00 +04:00
|
|
|
|
2013-01-02 23:30:02 +04:00
|
|
|
return uri.forget();
|
2004-06-05 21:57:00 +04:00
|
|
|
}
|
|
|
|
|
2003-04-02 07:27:20 +04:00
|
|
|
NS_IMETHODIMP
|
2013-01-02 23:30:02 +04:00
|
|
|
nsImageLoadingContent::GetCurrentURI(nsIURI** aURI) {
|
|
|
|
NS_ENSURE_ARG_POINTER(aURI);
|
2011-02-01 07:50:01 +03:00
|
|
|
|
2013-01-02 23:30:02 +04:00
|
|
|
ErrorResult result;
|
2014-03-15 23:00:15 +04:00
|
|
|
*aURI = GetCurrentURI(result).take();
|
2015-04-27 16:18:51 +03:00
|
|
|
return result.StealNSResult();
|
2013-01-02 23:30:02 +04:00
|
|
|
}
|
|
|
|
|
2017-11-14 13:20:03 +03:00
|
|
|
already_AddRefed<nsIURI> nsImageLoadingContent::GetCurrentRequestFinalURI() {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->GetFinalURI(getter_AddRefs(uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
return uri.forget();
|
|
|
|
}
|
|
|
|
|
2016-09-22 18:58:35 +03:00
|
|
|
NS_IMETHODIMP
|
2013-01-02 23:30:02 +04:00
|
|
|
nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel,
|
2016-09-22 18:58:35 +03:00
|
|
|
nsIStreamListener** aListener) {
|
2015-02-13 05:44:32 +03:00
|
|
|
imgLoader* loader =
|
|
|
|
nsContentUtils::GetImgLoaderForChannel(aChannel, GetOurOwnerDoc());
|
|
|
|
if (!loader) {
|
2016-09-22 18:58:35 +03:00
|
|
|
return NS_ERROR_NULL_POINTER;
|
2005-09-21 23:14:30 +04:00
|
|
|
}
|
2003-05-02 23:21:00 +04:00
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = GetOurOwnerDoc();
|
2003-07-29 01:09:56 +04:00
|
|
|
if (!doc) {
|
|
|
|
// Don't bother
|
2016-09-22 18:58:35 +03:00
|
|
|
*aListener = nullptr;
|
|
|
|
return NS_OK;
|
2003-07-29 01:09:56 +04:00
|
|
|
}
|
2005-08-19 18:55:07 +04:00
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// XXX what should we do with content policies here, if anything?
|
|
|
|
// Shouldn't that be done before the start of the load?
|
|
|
|
// XXX what about shouldProcess?
|
2003-04-02 07:27:20 +04:00
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
// If we have a current request without a size, we know we will replace it
|
|
|
|
// with the PrepareNextRequest below. If the new current request is for a
|
|
|
|
// different URI, then we need to reject any outstanding promises.
|
|
|
|
if (mCurrentRequest && !HaveSize(mCurrentRequest)) {
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
aChannel->GetOriginalURI(getter_AddRefs(uri));
|
|
|
|
MaybeAgeRequestGeneration(uri);
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Our state might change. Watch it.
|
2011-10-17 18:59:28 +04:00
|
|
|
AutoStateChanger changer(this, true);
|
2003-04-02 07:27:20 +04:00
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Do the load.
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequestProxy>& req = PrepareNextRequest(eImageLoadType_Normal);
|
2018-12-26 05:32:55 +03:00
|
|
|
nsresult rv = loader->LoadImageWithChannel(aChannel, this, ToSupports(doc),
|
|
|
|
aListener, getter_AddRefs(req));
|
2010-08-04 04:40:29 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-07-19 21:15:11 +03:00
|
|
|
CloneScriptedRequests(req);
|
2010-08-04 04:40:29 +04:00
|
|
|
TrackImage(req);
|
2012-06-14 05:12:37 +04:00
|
|
|
ResetAnimationIfNeeded();
|
2016-09-22 18:58:35 +03:00
|
|
|
return NS_OK;
|
2010-07-31 21:54:59 +04:00
|
|
|
}
|
2003-04-02 07:27:20 +04:00
|
|
|
|
2016-09-22 18:58:35 +03:00
|
|
|
MOZ_ASSERT(!req, "Shouldn't have non-null request here");
|
|
|
|
// If we don't have a current URI, we might as well store this URI so people
|
|
|
|
// know what we tried (and failed) to load.
|
|
|
|
if (!mCurrentRequest) aChannel->GetURI(getter_AddRefs(mCurrentURI));
|
2013-01-02 23:30:02 +04:00
|
|
|
|
2016-09-22 18:58:35 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
|
|
|
return rv;
|
2013-01-02 23:30:02 +04:00
|
|
|
}
|
2011-02-01 07:50:01 +03:00
|
|
|
|
2018-01-19 22:19:39 +03:00
|
|
|
void nsImageLoadingContent::ForceReload(bool aNotify, ErrorResult& aError) {
|
2006-09-08 02:51:57 +04:00
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
GetCurrentURI(getter_AddRefs(currentURI));
|
|
|
|
if (!currentURI) {
|
2013-01-02 23:30:02 +04:00
|
|
|
aError.Throw(NS_ERROR_NOT_AVAILABLE);
|
|
|
|
return;
|
2006-09-08 02:51:57 +04:00
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
// We keep this flag around along with the old URI even for failed requests
|
|
|
|
// without a live request object
|
|
|
|
ImageLoadType loadType = (mCurrentRequestFlags & REQUEST_IS_IMAGESET)
|
|
|
|
? eImageLoadType_Imageset
|
|
|
|
: eImageLoadType_Normal;
|
2018-01-19 22:19:39 +03:00
|
|
|
nsresult rv = LoadImage(currentURI, true, aNotify, loadType, true, nullptr,
|
2014-08-20 01:49:38 +04:00
|
|
|
nsIRequest::VALIDATE_ALWAYS);
|
2013-01-02 23:30:02 +04:00
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
aError.Throw(rv);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
/*
|
|
|
|
* Non-interface methods
|
|
|
|
*/
|
2005-09-18 22:05:40 +04:00
|
|
|
|
2006-01-01 14:25:43 +03:00
|
|
|
nsresult nsImageLoadingContent::LoadImage(const nsAString& aNewURI, bool aForce,
|
2014-08-20 01:49:38 +04:00
|
|
|
bool aNotify,
|
2017-10-03 06:28:32 +03:00
|
|
|
ImageLoadType aImageLoadType,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal) {
|
2003-07-03 06:45:34 +04:00
|
|
|
// First, get a document (needed for security checks and the like)
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetOurOwnerDoc();
|
2003-04-15 05:09:09 +04:00
|
|
|
if (!doc) {
|
|
|
|
// No reason to bother, I think...
|
2003-07-29 01:09:56 +04:00
|
|
|
return NS_OK;
|
2003-04-15 05:09:09 +04:00
|
|
|
}
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
// Pending load/error events need to be canceled in some situations. This
|
|
|
|
// is not documented in the spec, but can cause site compat problems if not
|
|
|
|
// done. See bug 1309461 and https://github.com/whatwg/html/issues/1872.
|
|
|
|
CancelPendingEvent();
|
|
|
|
|
2016-09-08 11:27:03 +03:00
|
|
|
if (aNewURI.IsEmpty()) {
|
|
|
|
// Cancel image requests and then fire only error event per spec.
|
|
|
|
CancelImageRequests(aNotify);
|
2016-10-06 07:30:35 +03:00
|
|
|
// Mark error event as cancelable only for src="" case, since only this
|
|
|
|
// error causes site compat problem (bug 1308069) for now.
|
|
|
|
FireEvent(NS_LITERAL_STRING("error"), true);
|
2016-09-08 11:27:03 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-05 05:06:27 +03:00
|
|
|
// Fire loadstart event
|
|
|
|
FireEvent(NS_LITERAL_STRING("loadstart"));
|
|
|
|
|
|
|
|
// Parse the URI string to get image URI
|
2003-03-19 04:08:22 +03:00
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
2005-09-21 23:14:30 +04:00
|
|
|
nsresult rv = StringToURI(aNewURI, doc, getter_AddRefs(imageURI));
|
2017-01-09 06:50:55 +03:00
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
// XXXbiesi fire onerror if that failed?
|
2005-09-21 23:14:30 +04:00
|
|
|
|
2017-10-03 06:28:32 +03:00
|
|
|
return LoadImage(imageURI, aForce, aNotify, aImageLoadType, false, doc,
|
|
|
|
nsIRequest::LOAD_NORMAL, aTriggeringPrincipal);
|
2005-09-21 23:14:30 +04:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
nsresult nsImageLoadingContent::LoadImage(nsIURI* aNewURI, bool aForce,
|
|
|
|
bool aNotify,
|
|
|
|
ImageLoadType aImageLoadType,
|
|
|
|
bool aLoadStart, Document* aDocument,
|
|
|
|
nsLoadFlags aLoadFlags,
|
|
|
|
nsIPrincipal* aTriggeringPrincipal) {
|
2017-03-16 11:09:50 +03:00
|
|
|
MOZ_ASSERT(!mIsStartingImageLoad, "some evil code is reentering LoadImage.");
|
|
|
|
if (mIsStartingImageLoad) {
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
// Pending load/error events need to be canceled in some situations. This
|
|
|
|
// is not documented in the spec, but can cause site compat problems if not
|
|
|
|
// done. See bug 1309461 and https://github.com/whatwg/html/issues/1872.
|
|
|
|
CancelPendingEvent();
|
|
|
|
|
2016-10-05 05:06:27 +03:00
|
|
|
// Fire loadstart event if required
|
|
|
|
if (aLoadStart) {
|
|
|
|
FireEvent(NS_LITERAL_STRING("loadstart"));
|
|
|
|
}
|
|
|
|
|
2005-09-21 23:14:30 +04:00
|
|
|
if (!mLoadingEnabled) {
|
2009-11-20 07:39:11 +03:00
|
|
|
// XXX Why fire an error here? seems like the callers to SetLoadingEnabled
|
|
|
|
// don't want/need it.
|
2006-06-01 22:35:21 +04:00
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
2016-08-16 07:01:12 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
2005-09-21 23:14:30 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
NS_ASSERTION(!aDocument || aDocument == GetOurOwnerDoc(),
|
2005-09-21 23:14:30 +04:00
|
|
|
"Bogus document passed in");
|
|
|
|
// First, get a document (needed for security checks and the like)
|
|
|
|
if (!aDocument) {
|
2012-08-14 02:11:50 +04:00
|
|
|
aDocument = GetOurOwnerDoc();
|
2005-09-21 23:14:30 +04:00
|
|
|
if (!aDocument) {
|
|
|
|
// No reason to bother, I think...
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-16 11:09:50 +03:00
|
|
|
AutoRestore<bool> guard(mIsStartingImageLoad);
|
|
|
|
mIsStartingImageLoad = true;
|
|
|
|
|
2017-01-19 06:48:00 +03:00
|
|
|
// Data documents, or documents from DOMParser shouldn't perform image
|
|
|
|
// loading.
|
|
|
|
if (aDocument->IsLoadedAsData()) {
|
2018-04-04 21:40:01 +03:00
|
|
|
// This is the only codepath on which we can reach SetBlockedRequest while
|
|
|
|
// our pending request exists. Just clear it out here if we do have one.
|
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
|
|
|
|
|
2017-01-19 06:48:00 +03:00
|
|
|
SetBlockedRequest(nsIContentPolicy::REJECT_REQUEST);
|
2017-03-16 11:09:50 +03:00
|
|
|
|
2017-01-19 06:48:00 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// URI equality check.
|
|
|
|
//
|
|
|
|
// We skip the equality check if our current image was blocked, since in that
|
|
|
|
// case we really do want to try loading again.
|
2005-04-11 20:37:27 +04:00
|
|
|
if (!aForce && NS_CP_ACCEPTED(mImageBlockingStatus)) {
|
2005-04-06 06:55:02 +04:00
|
|
|
nsCOMPtr<nsIURI> currentURI;
|
|
|
|
GetCurrentURI(getter_AddRefs(currentURI));
|
2011-09-29 10:19:26 +04:00
|
|
|
bool equal;
|
2005-09-21 23:14:30 +04:00
|
|
|
if (currentURI && NS_SUCCEEDED(currentURI->Equals(aNewURI, &equal)) &&
|
2005-04-06 06:55:02 +04:00
|
|
|
equal) {
|
|
|
|
// Nothing to do here.
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
// If we have a current request without a size, we know we will replace it
|
|
|
|
// with the PrepareNextRequest below. If the new current request is for a
|
|
|
|
// different URI, then we need to reject any outstanding promises.
|
|
|
|
if (mCurrentRequest && !HaveSize(mCurrentRequest)) {
|
|
|
|
MaybeAgeRequestGeneration(aNewURI);
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// From this point on, our image state could change. Watch it.
|
2005-09-18 22:05:40 +04:00
|
|
|
AutoStateChanger changer(this, aNotify);
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Sanity check.
|
|
|
|
//
|
|
|
|
// We use the principal of aDocument to avoid having to QI |this| an extra
|
|
|
|
// time. It should always be the same as the principal of this node.
|
2007-07-03 22:45:39 +04:00
|
|
|
#ifdef DEBUG
|
2017-03-17 00:43:34 +03:00
|
|
|
nsIContent* thisContent = AsContent();
|
|
|
|
MOZ_ASSERT(thisContent->NodePrincipal() == aDocument->NodePrincipal(),
|
2015-02-10 01:34:50 +03:00
|
|
|
"Principal mismatch?");
|
2007-07-03 22:45:39 +04:00
|
|
|
#endif
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2018-01-31 19:10:01 +03:00
|
|
|
nsLoadFlags loadFlags =
|
|
|
|
aLoadFlags | nsContentUtils::CORSModeToLoadImageFlags(GetCORSMode());
|
2011-07-14 22:47:35 +04:00
|
|
|
|
2015-06-06 01:25:24 +03:00
|
|
|
// get document wide referrer policy
|
|
|
|
// if referrer attributes are enabled in preferences, load img referrer
|
2015-07-14 04:04:42 +03:00
|
|
|
// attribute if the image does not provide a referrer attribute, ignore this
|
|
|
|
net::ReferrerPolicy referrerPolicy = aDocument->GetReferrerPolicy();
|
|
|
|
net::ReferrerPolicy imgReferrerPolicy = GetImageReferrerPolicy();
|
|
|
|
if (imgReferrerPolicy != net::RP_Unset) {
|
|
|
|
referrerPolicy = imgReferrerPolicy;
|
2015-06-06 01:25:24 +03:00
|
|
|
}
|
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequestProxy>& req = PrepareNextRequest(aImageLoadType);
|
2013-10-16 05:35:44 +04:00
|
|
|
nsCOMPtr<nsIContent> content =
|
|
|
|
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
|
2017-09-15 12:25:55 +03:00
|
|
|
|
2017-10-03 06:38:30 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
|
2017-10-11 12:07:35 +03:00
|
|
|
bool result = nsContentUtils::QueryTriggeringPrincipal(
|
|
|
|
content, aTriggeringPrincipal, getter_AddRefs(triggeringPrincipal));
|
2017-09-15 12:25:55 +03:00
|
|
|
|
2017-10-11 12:07:35 +03:00
|
|
|
// If result is true, which means this node has specified
|
2017-09-15 12:25:55 +03:00
|
|
|
// 'triggeringprincipal' attribute on it, so we use favicon as the policy
|
|
|
|
// type.
|
|
|
|
nsContentPolicyType policyType =
|
|
|
|
result ? nsIContentPolicy::TYPE_INTERNAL_IMAGE_FAVICON
|
|
|
|
: PolicyTypeForLoad(aImageLoadType);
|
|
|
|
|
2016-04-11 11:58:03 +03:00
|
|
|
nsCOMPtr<nsINode> thisNode =
|
|
|
|
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
|
|
|
|
nsresult rv = nsContentUtils::LoadImage(
|
2017-10-03 06:38:30 +03:00
|
|
|
aNewURI, thisNode, aDocument, triggeringPrincipal, 0,
|
2019-01-16 22:26:35 +03:00
|
|
|
aDocument->GetDocumentURIAsReferrer(), referrerPolicy, this, loadFlags,
|
2015-07-14 04:04:42 +03:00
|
|
|
content->LocalName(), getter_AddRefs(req), policyType,
|
2017-04-25 04:17:38 +03:00
|
|
|
mUseUrgentStartForChannel);
|
|
|
|
|
|
|
|
// Reset the flag to avoid loading from XPCOM or somewhere again else without
|
|
|
|
// initiated by user interaction.
|
|
|
|
mUseUrgentStartForChannel = false;
|
2013-10-16 05:35:44 +04:00
|
|
|
|
2014-11-24 19:57:49 +03:00
|
|
|
// Tell the document to forget about the image preload, if any, for
|
|
|
|
// this URI, now that we might have another imgRequestProxy for it.
|
|
|
|
// That way if we get canceled later the image load won't continue.
|
|
|
|
aDocument->ForgetImagePreload(aNewURI);
|
|
|
|
|
2010-08-04 04:40:29 +04:00
|
|
|
if (NS_SUCCEEDED(rv)) {
|
2017-07-19 21:15:11 +03:00
|
|
|
CloneScriptedRequests(req);
|
2010-08-04 04:40:29 +04:00
|
|
|
TrackImage(req);
|
2012-06-14 05:12:37 +04:00
|
|
|
ResetAnimationIfNeeded();
|
2012-03-20 02:22:02 +04:00
|
|
|
|
|
|
|
// Handle cases when we just ended up with a pending request but it's
|
|
|
|
// already done. In that situation we have to synchronously switch that
|
|
|
|
// request to being the current request, because websites depend on that
|
|
|
|
// behavior.
|
|
|
|
if (req == mPendingRequest) {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t pendingLoadStatus;
|
2012-03-20 02:22:02 +04:00
|
|
|
rv = req->GetImageStatus(&pendingLoadStatus);
|
|
|
|
if (NS_SUCCEEDED(rv) &&
|
|
|
|
(pendingLoadStatus & imgIRequest::STATUS_LOAD_COMPLETE)) {
|
|
|
|
MakePendingRequestCurrent();
|
|
|
|
MOZ_ASSERT(mCurrentRequest,
|
|
|
|
"How could we not have a current request here?");
|
|
|
|
|
|
|
|
nsImageFrame* f = do_QueryFrame(GetOurPrimaryFrame());
|
|
|
|
if (f) {
|
|
|
|
f->NotifyNewCurrentRequest(mCurrentRequest, NS_OK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2010-08-04 04:40:29 +04:00
|
|
|
} else {
|
2013-02-14 00:18:08 +04:00
|
|
|
MOZ_ASSERT(!req, "Shouldn't have non-null request here");
|
2010-07-31 21:54:59 +04:00
|
|
|
// If we don't have a current URI, we might as well store this URI so people
|
|
|
|
// know what we tried (and failed) to load.
|
|
|
|
if (!mCurrentRequest) mCurrentURI = aNewURI;
|
2016-08-16 07:01:12 +03:00
|
|
|
|
2005-08-18 03:01:09 +04:00
|
|
|
FireEvent(NS_LITERAL_STRING("error"));
|
2016-08-16 07:01:12 +03:00
|
|
|
FireEvent(NS_LITERAL_STRING("loadend"));
|
2005-08-18 03:01:09 +04:00
|
|
|
}
|
|
|
|
|
2005-09-18 22:05:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
void nsImageLoadingContent::ForceImageState(bool aForce,
|
|
|
|
EventStates::InternalType aState) {
|
2009-06-26 11:25:17 +04:00
|
|
|
mIsImageStateForced = aForce;
|
2014-04-03 08:18:36 +04:00
|
|
|
mForcedImageState = EventStates(aState);
|
2009-06-26 11:25:17 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 04:20:17 +03:00
|
|
|
uint32_t nsImageLoadingContent::NaturalWidth() {
|
2014-05-03 04:05:19 +04:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->GetImage(getter_AddRefs(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t width;
|
|
|
|
if (image && NS_SUCCEEDED(image->GetWidth(&width))) {
|
2018-11-20 04:20:17 +03:00
|
|
|
return width;
|
2014-05-03 04:05:19 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 04:20:17 +03:00
|
|
|
return 0;
|
2014-05-03 04:05:19 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 04:20:17 +03:00
|
|
|
uint32_t nsImageLoadingContent::NaturalHeight() {
|
2014-05-03 04:05:19 +04:00
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->GetImage(getter_AddRefs(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
int32_t height;
|
|
|
|
if (image && NS_SUCCEEDED(image->GetHeight(&height))) {
|
2018-11-20 04:20:17 +03:00
|
|
|
return height;
|
2014-05-03 04:05:19 +04:00
|
|
|
}
|
|
|
|
|
2018-11-20 04:20:17 +03:00
|
|
|
return 0;
|
2014-05-03 04:05:19 +04:00
|
|
|
}
|
|
|
|
|
2005-09-18 22:05:40 +04:00
|
|
|
EventStates nsImageLoadingContent::ImageState() const {
|
2010-10-20 15:26:32 +04:00
|
|
|
if (mIsImageStateForced) {
|
|
|
|
return mForcedImageState;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
EventStates states;
|
2010-10-20 15:26:32 +04:00
|
|
|
|
|
|
|
if (mBroken) {
|
|
|
|
states |= NS_EVENT_STATE_BROKEN;
|
|
|
|
}
|
|
|
|
if (mUserDisabled) {
|
|
|
|
states |= NS_EVENT_STATE_USERDISABLED;
|
|
|
|
}
|
|
|
|
if (mSuppressed) {
|
|
|
|
states |= NS_EVENT_STATE_SUPPRESSED;
|
|
|
|
}
|
|
|
|
if (mLoading) {
|
|
|
|
states |= NS_EVENT_STATE_LOADING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return states;
|
2005-09-18 22:05:40 +04:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsImageLoadingContent::UpdateImageState(bool aNotify) {
|
2010-07-31 21:54:59 +04:00
|
|
|
if (mStateChangerDepth > 0) {
|
2014-11-14 20:59:00 +03:00
|
|
|
// Ignore this call; we'll update our state when the outermost state changer
|
|
|
|
// is destroyed. Need this to work around the fact that some ImageLib
|
2010-07-31 21:54:59 +04:00
|
|
|
// stuff is actually sync and hence we can get OnStopDecode called while
|
|
|
|
// we're still under LoadImage, and OnStopDecode doesn't know anything about
|
|
|
|
// aNotify.
|
|
|
|
// XXX - This machinery should be removed after bug 521604.
|
2005-09-19 01:06:19 +04:00
|
|
|
return;
|
|
|
|
}
|
2014-08-20 01:49:38 +04:00
|
|
|
|
2017-03-17 00:43:34 +03:00
|
|
|
nsIContent* thisContent = AsContent();
|
2003-04-16 05:53:48 +04:00
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoading = mBroken = mUserDisabled = mSuppressed = false;
|
2014-08-20 01:49:38 +04:00
|
|
|
|
2005-09-18 22:05:40 +04:00
|
|
|
// If we were blocked by server-based content policy, we claim to be
|
|
|
|
// suppressed. If we were blocked by type-based content policy, we claim to
|
|
|
|
// be user-disabled. Otherwise, claim to be broken.
|
|
|
|
if (mImageBlockingStatus == nsIContentPolicy::REJECT_SERVER) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mSuppressed = true;
|
2005-09-18 22:05:40 +04:00
|
|
|
} else if (mImageBlockingStatus == nsIContentPolicy::REJECT_TYPE) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mUserDisabled = true;
|
2005-09-18 22:05:40 +04:00
|
|
|
} else if (!mCurrentRequest) {
|
|
|
|
// No current request means error, since we weren't disabled or suppressed
|
2011-10-17 18:59:28 +04:00
|
|
|
mBroken = true;
|
2018-11-08 20:45:50 +03:00
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_BROKEN);
|
2005-09-18 22:05:40 +04:00
|
|
|
} else {
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t currentLoadStatus;
|
2005-09-18 22:05:40 +04:00
|
|
|
nsresult rv = mCurrentRequest->GetImageStatus(¤tLoadStatus);
|
|
|
|
if (NS_FAILED(rv) || (currentLoadStatus & imgIRequest::STATUS_ERROR)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mBroken = true;
|
2018-11-08 20:45:50 +03:00
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_BROKEN);
|
2005-10-17 04:50:39 +04:00
|
|
|
} else if (!(currentLoadStatus & imgIRequest::STATUS_SIZE_AVAILABLE)) {
|
2011-10-17 18:59:28 +04:00
|
|
|
mLoading = true;
|
2005-09-18 22:05:40 +04:00
|
|
|
}
|
2003-04-16 05:53:48 +04:00
|
|
|
}
|
|
|
|
|
2011-06-01 05:46:57 +04:00
|
|
|
NS_ASSERTION(thisContent->IsElement(), "Not an element?");
|
|
|
|
thisContent->AsElement()->UpdateState(aNotify);
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
void nsImageLoadingContent::CancelImageRequests(bool aNotify) {
|
2018-11-08 20:45:50 +03:00
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INVALID_REQUEST);
|
2008-12-12 22:41:11 +03:00
|
|
|
AutoStateChanger changer(this, aNotify);
|
2016-03-26 00:49:43 +03:00
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
|
|
|
|
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* nsImageLoadingContent::GetOurOwnerDoc() {
|
2017-03-17 00:43:34 +03:00
|
|
|
return AsContent()->OwnerDoc();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* nsImageLoadingContent::GetOurCurrentDoc() {
|
2017-03-17 00:43:34 +03:00
|
|
|
return AsContent()->GetComposedDoc();
|
2012-08-14 02:11:50 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
nsIFrame* nsImageLoadingContent::GetOurPrimaryFrame() {
|
2017-03-17 00:43:34 +03:00
|
|
|
return AsContent()->GetPrimaryFrame();
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
nsPresContext* nsImageLoadingContent::GetFramePresContext() {
|
|
|
|
nsIFrame* frame = GetOurPrimaryFrame();
|
|
|
|
if (!frame) {
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
return frame->PresContext();
|
|
|
|
}
|
|
|
|
|
2005-04-06 06:55:02 +04:00
|
|
|
nsresult nsImageLoadingContent::StringToURI(const nsAString& aSpec,
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* aDocument,
|
2003-03-19 04:08:22 +03:00
|
|
|
nsIURI** aURI) {
|
2018-04-28 22:50:58 +03:00
|
|
|
MOZ_ASSERT(aDocument, "Must have a document");
|
|
|
|
MOZ_ASSERT(aURI, "Null out param");
|
2003-03-19 04:08:22 +03:00
|
|
|
|
|
|
|
// (1) Get the base URI
|
2017-03-17 00:43:34 +03:00
|
|
|
nsIContent* thisContent = AsContent();
|
2004-01-10 02:54:21 +03:00
|
|
|
nsCOMPtr<nsIURI> baseURL = thisContent->GetBaseURI();
|
2003-03-19 04:08:22 +03:00
|
|
|
|
|
|
|
// (2) Get the charset
|
2017-06-18 14:37:50 +03:00
|
|
|
auto encoding = aDocument->GetDocumentCharacterSet();
|
2003-03-19 04:08:22 +03:00
|
|
|
|
|
|
|
// (3) Construct the silly thing
|
|
|
|
return NS_NewURI(aURI, aSpec, encoding, baseURL,
|
2005-09-11 14:08:43 +04:00
|
|
|
nsContentUtils::GetIOService());
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
nsresult nsImageLoadingContent::FireEvent(const nsAString& aEventType,
|
|
|
|
bool aIsCancelable) {
|
2015-02-13 05:44:32 +03:00
|
|
|
if (nsContentUtils::DocumentInactiveForImageLoads(GetOurOwnerDoc())) {
|
|
|
|
// Don't bother to fire any events, especially error events.
|
2018-11-08 20:45:50 +03:00
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INACTIVE_DOCUMENT);
|
2015-02-13 05:44:32 +03:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2003-03-19 04:08:22 +03:00
|
|
|
// We have to fire the event asynchronously so that we won't go into infinite
|
|
|
|
// loops in cases when onLoad handlers reset the src and the new src is in
|
|
|
|
// cache.
|
2003-05-10 00:42:49 +04:00
|
|
|
|
2012-08-14 02:58:53 +04:00
|
|
|
nsCOMPtr<nsINode> thisNode =
|
|
|
|
do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
|
2003-03-19 04:08:22 +03:00
|
|
|
|
2015-10-18 08:24:48 +03:00
|
|
|
RefPtr<AsyncEventDispatcher> loadBlockingAsyncDispatcher =
|
2018-06-25 19:23:50 +03:00
|
|
|
new LoadBlockingAsyncEventDispatcher(thisNode, aEventType, CanBubble::eNo,
|
|
|
|
ChromeOnlyDispatch::eNo);
|
2014-03-17 10:56:54 +04:00
|
|
|
loadBlockingAsyncDispatcher->PostDOMEvent();
|
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
if (aIsCancelable) {
|
|
|
|
mPendingEvent = loadBlockingAsyncDispatcher;
|
|
|
|
}
|
|
|
|
|
2010-02-25 05:45:43 +03:00
|
|
|
return NS_OK;
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
2006-06-01 22:35:21 +04:00
|
|
|
|
2016-10-06 07:30:35 +03:00
|
|
|
void nsImageLoadingContent::AsyncEventRunning(AsyncEventDispatcher* aEvent) {
|
|
|
|
if (mPendingEvent == aEvent) {
|
|
|
|
mPendingEvent = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::CancelPendingEvent() {
|
|
|
|
if (mPendingEvent) {
|
|
|
|
mPendingEvent->Cancel();
|
|
|
|
mPendingEvent = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
RefPtr<imgRequestProxy>& nsImageLoadingContent::PrepareNextRequest(
|
|
|
|
ImageLoadType aImageLoadType) {
|
2018-08-08 14:56:01 +03:00
|
|
|
MaybeForceSyncDecoding(/* aPrepareNextRequest */ true);
|
2015-07-08 00:00:08 +03:00
|
|
|
|
2017-02-03 06:49:07 +03:00
|
|
|
// We only want to cancel the existing current request if size is not
|
|
|
|
// available. bz says the web depends on this behavior.
|
|
|
|
// Otherwise, we get rid of any half-baked request that might be sitting there
|
|
|
|
// and make this one current.
|
|
|
|
return HaveSize(mCurrentRequest) ? PreparePendingRequest(aImageLoadType)
|
|
|
|
: PrepareCurrentRequest(aImageLoadType);
|
2010-07-31 21:54:59 +04:00
|
|
|
}
|
|
|
|
|
2017-02-03 06:49:07 +03:00
|
|
|
void nsImageLoadingContent::SetBlockedRequest(int16_t aContentDecision) {
|
2017-03-16 11:09:50 +03:00
|
|
|
// If this is not calling from LoadImage, for example, from ServiceWorker,
|
|
|
|
// bail out.
|
|
|
|
if (!mIsStartingImageLoad) {
|
2018-01-19 22:19:39 +03:00
|
|
|
return;
|
2017-03-16 11:09:50 +03:00
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Sanity
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!NS_CP_ACCEPTED(aContentDecision), "Blocked but not?");
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2017-02-03 06:49:07 +03:00
|
|
|
// We should never have a pending request after we got blocked.
|
|
|
|
MOZ_ASSERT(!mPendingRequest, "mPendingRequest should be null.");
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2017-02-03 06:49:07 +03:00
|
|
|
if (HaveSize(mCurrentRequest)) {
|
|
|
|
// PreparePendingRequest set mPendingRequestFlags, now since we've decided
|
|
|
|
// to block it, we reset it back to 0.
|
|
|
|
mPendingRequestFlags = 0;
|
|
|
|
} else {
|
2010-07-31 21:54:59 +04:00
|
|
|
mImageBlockingStatus = aContentDecision;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
RefPtr<imgRequestProxy>& nsImageLoadingContent::PrepareCurrentRequest(
|
|
|
|
ImageLoadType aImageLoadType) {
|
2010-07-31 21:54:59 +04:00
|
|
|
// Blocked images go through SetBlockedRequest, which is a separate path. For
|
|
|
|
// everything else, we're unblocked.
|
|
|
|
mImageBlockingStatus = nsIContentPolicy::ACCEPT;
|
|
|
|
|
|
|
|
// Get rid of anything that was there previously.
|
2017-02-03 06:49:07 +03:00
|
|
|
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
if (mNewRequestsWillNeedAnimationReset) {
|
|
|
|
mCurrentRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET;
|
|
|
|
}
|
2011-01-26 21:52:48 +03:00
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
if (aImageLoadType == eImageLoadType_Imageset) {
|
|
|
|
mCurrentRequestFlags |= REQUEST_IS_IMAGESET;
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Return a reference.
|
|
|
|
return mCurrentRequest;
|
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
RefPtr<imgRequestProxy>& nsImageLoadingContent::PreparePendingRequest(
|
|
|
|
ImageLoadType aImageLoadType) {
|
2010-07-31 21:54:59 +04:00
|
|
|
// Get rid of anything that was there previously.
|
2017-02-03 06:49:07 +03:00
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
if (mNewRequestsWillNeedAnimationReset) {
|
|
|
|
mPendingRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET;
|
|
|
|
}
|
2011-01-26 21:52:48 +03:00
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
if (aImageLoadType == eImageLoadType_Imageset) {
|
|
|
|
mPendingRequestFlags |= REQUEST_IS_IMAGESET;
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Return a reference.
|
|
|
|
return mPendingRequest;
|
|
|
|
}
|
|
|
|
|
2012-06-20 21:43:06 +04:00
|
|
|
namespace {
|
|
|
|
|
|
|
|
class ImageRequestAutoLock {
|
|
|
|
public:
|
2014-09-02 04:49:25 +04:00
|
|
|
explicit ImageRequestAutoLock(imgIRequest* aRequest) : mRequest(aRequest) {
|
2012-06-20 21:43:06 +04:00
|
|
|
if (mRequest) {
|
|
|
|
mRequest->LockImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ImageRequestAutoLock() {
|
|
|
|
if (mRequest) {
|
|
|
|
mRequest->UnlockImage();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
nsCOMPtr<imgIRequest> mRequest;
|
|
|
|
};
|
|
|
|
|
2015-07-13 18:25:42 +03:00
|
|
|
} // namespace
|
2012-06-20 21:43:06 +04:00
|
|
|
|
2012-03-20 02:22:02 +04:00
|
|
|
void nsImageLoadingContent::MakePendingRequestCurrent() {
|
|
|
|
MOZ_ASSERT(mPendingRequest);
|
2012-06-20 21:43:06 +04:00
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
// If we have a pending request, we know that there is an existing current
|
|
|
|
// request with size information. If the pending request is for a different
|
|
|
|
// URI, then we need to reject any outstanding promises.
|
|
|
|
nsCOMPtr<nsIURI> uri;
|
|
|
|
mPendingRequest->GetURI(getter_AddRefs(uri));
|
|
|
|
MaybeAgeRequestGeneration(uri);
|
|
|
|
|
2012-06-20 21:43:06 +04:00
|
|
|
// Lock mCurrentRequest for the duration of this method. We do this because
|
|
|
|
// PrepareCurrentRequest() might unlock mCurrentRequest. If mCurrentRequest
|
|
|
|
// and mPendingRequest are both requests for the same image, unlocking
|
|
|
|
// mCurrentRequest before we lock mPendingRequest can cause the lock count
|
|
|
|
// to go to 0 and the image to be discarded!
|
|
|
|
ImageRequestAutoLock autoLock(mCurrentRequest);
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
ImageLoadType loadType = (mPendingRequestFlags & REQUEST_IS_IMAGESET)
|
|
|
|
? eImageLoadType_Imageset
|
|
|
|
: eImageLoadType_Normal;
|
|
|
|
|
|
|
|
PrepareCurrentRequest(loadType) = mPendingRequest;
|
2017-07-19 21:15:11 +03:00
|
|
|
MakePendingScriptedRequestsCurrent();
|
2012-07-30 18:20:58 +04:00
|
|
|
mPendingRequest = nullptr;
|
2012-08-14 02:11:50 +04:00
|
|
|
mCurrentRequestFlags = mPendingRequestFlags;
|
|
|
|
mPendingRequestFlags = 0;
|
2017-04-01 08:35:32 +03:00
|
|
|
mCurrentRequestRegistered = mPendingRequestRegistered;
|
|
|
|
mPendingRequestRegistered = false;
|
2012-06-14 05:12:37 +04:00
|
|
|
ResetAnimationIfNeeded();
|
2012-03-20 02:22:02 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 20:50:35 +04:00
|
|
|
void nsImageLoadingContent::ClearCurrentRequest(
|
2016-03-26 00:49:43 +03:00
|
|
|
nsresult aReason, const Maybe<OnNonvisible>& aNonvisibleAction) {
|
2010-07-31 21:54:59 +04:00
|
|
|
if (!mCurrentRequest) {
|
|
|
|
// Even if we didn't have a current request, we might have been keeping
|
2014-08-20 01:49:38 +04:00
|
|
|
// a URI and flags as a placeholder for a failed load. Clear that now.
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurrentURI = nullptr;
|
2014-08-20 01:49:38 +04:00
|
|
|
mCurrentRequestFlags = 0;
|
2010-07-31 21:54:59 +04:00
|
|
|
return;
|
|
|
|
}
|
2015-02-10 01:34:50 +03:00
|
|
|
MOZ_ASSERT(!mCurrentURI,
|
|
|
|
"Shouldn't have both mCurrentRequest and mCurrentURI!");
|
2010-07-31 21:54:59 +04:00
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
// Deregister this image from the refresh driver so it no longer receives
|
|
|
|
// notifications.
|
|
|
|
nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mCurrentRequest,
|
|
|
|
&mCurrentRequestRegistered);
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// Clean up the request.
|
2015-02-06 13:51:12 +03:00
|
|
|
UntrackImage(mCurrentRequest, aNonvisibleAction);
|
2017-07-19 21:15:11 +03:00
|
|
|
ClearScriptedRequests(CURRENT_REQUEST, aReason);
|
2010-07-31 21:54:59 +04:00
|
|
|
mCurrentRequest->CancelAndForgetObserver(aReason);
|
2012-07-30 18:20:58 +04:00
|
|
|
mCurrentRequest = nullptr;
|
2012-08-14 02:11:50 +04:00
|
|
|
mCurrentRequestFlags = 0;
|
2010-07-31 21:54:59 +04:00
|
|
|
}
|
|
|
|
|
2013-04-09 20:50:35 +04:00
|
|
|
void nsImageLoadingContent::ClearPendingRequest(
|
2016-03-26 00:49:43 +03:00
|
|
|
nsresult aReason, const Maybe<OnNonvisible>& aNonvisibleAction) {
|
2010-07-31 21:54:59 +04:00
|
|
|
if (!mPendingRequest) return;
|
2011-02-01 07:50:01 +03:00
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
// Deregister this image from the refresh driver so it no longer receives
|
|
|
|
// notifications.
|
|
|
|
nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mPendingRequest,
|
|
|
|
&mPendingRequestRegistered);
|
|
|
|
|
2015-02-06 13:51:12 +03:00
|
|
|
UntrackImage(mPendingRequest, aNonvisibleAction);
|
2017-07-19 21:15:11 +03:00
|
|
|
ClearScriptedRequests(PENDING_REQUEST, aReason);
|
2010-07-31 21:54:59 +04:00
|
|
|
mPendingRequest->CancelAndForgetObserver(aReason);
|
2012-07-30 18:20:58 +04:00
|
|
|
mPendingRequest = nullptr;
|
2012-08-14 02:11:50 +04:00
|
|
|
mPendingRequestFlags = 0;
|
2010-07-31 21:54:59 +04:00
|
|
|
}
|
|
|
|
|
2011-11-10 01:39:16 +04:00
|
|
|
bool* nsImageLoadingContent::GetRegisteredFlagForRequest(
|
|
|
|
imgIRequest* aRequest) {
|
|
|
|
if (aRequest == mCurrentRequest) {
|
|
|
|
return &mCurrentRequestRegistered;
|
2017-02-09 17:44:02 +03:00
|
|
|
}
|
|
|
|
if (aRequest == mPendingRequest) {
|
2011-11-10 01:39:16 +04:00
|
|
|
return &mPendingRequestRegistered;
|
|
|
|
}
|
2017-02-09 17:44:02 +03:00
|
|
|
return nullptr;
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
|
|
|
|
2012-06-14 05:12:37 +04:00
|
|
|
void nsImageLoadingContent::ResetAnimationIfNeeded() {
|
2012-08-14 02:11:50 +04:00
|
|
|
if (mCurrentRequest &&
|
|
|
|
(mCurrentRequestFlags & REQUEST_NEEDS_ANIMATION_RESET)) {
|
2012-06-14 05:12:37 +04:00
|
|
|
nsCOMPtr<imgIContainer> container;
|
|
|
|
mCurrentRequest->GetImage(getter_AddRefs(container));
|
|
|
|
if (container) container->ResetAnimation();
|
2012-08-14 02:11:50 +04:00
|
|
|
mCurrentRequestFlags &= ~REQUEST_NEEDS_ANIMATION_RESET;
|
2012-06-14 05:12:37 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
bool nsImageLoadingContent::HaveSize(imgIRequest* aImage) {
|
|
|
|
// Handle the null case
|
|
|
|
if (!aImage) return false;
|
|
|
|
|
|
|
|
// Query the image
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t status;
|
2010-07-31 21:54:59 +04:00
|
|
|
nsresult rv = aImage->GetImageStatus(&status);
|
|
|
|
return (NS_SUCCEEDED(rv) && (status & imgIRequest::STATUS_SIZE_AVAILABLE));
|
|
|
|
}
|
|
|
|
|
2018-11-08 20:45:50 +03:00
|
|
|
void nsImageLoadingContent::NotifyOwnerDocumentActivityChanged() {
|
|
|
|
if (!GetOurOwnerDoc()->IsCurrentActiveDocument()) {
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_INACTIVE_DOCUMENT);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
void nsImageLoadingContent::BindToTree(Document* aDocument, nsIContent* aParent,
|
2018-07-31 21:18:38 +03:00
|
|
|
nsIContent* aBindingParent) {
|
2019-01-09 18:59:10 +03:00
|
|
|
// We may be getting connected, if so our image should be tracked,
|
|
|
|
if (GetOurCurrentDoc()) {
|
|
|
|
TrackImage(mCurrentRequest);
|
|
|
|
TrackImage(mPendingRequest);
|
|
|
|
}
|
2012-08-14 02:11:50 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::UnbindFromTree(bool aDeep, bool aNullParent) {
|
|
|
|
// We may be leaving the document, so if our image is tracked, untrack it.
|
2019-01-02 16:05:23 +03:00
|
|
|
nsCOMPtr<Document> doc = GetOurCurrentDoc();
|
2012-08-14 02:11:50 +04:00
|
|
|
if (!doc) return;
|
|
|
|
|
2013-02-25 04:59:21 +04:00
|
|
|
UntrackImage(mCurrentRequest);
|
|
|
|
UntrackImage(mPendingRequest);
|
2012-08-14 02:11:50 +04:00
|
|
|
}
|
|
|
|
|
2016-09-12 08:19:07 +03:00
|
|
|
void nsImageLoadingContent::OnVisibilityChange(
|
2016-03-26 00:49:43 +03:00
|
|
|
Visibility aNewVisibility, const Maybe<OnNonvisible>& aNonvisibleAction) {
|
|
|
|
switch (aNewVisibility) {
|
2016-09-12 08:19:08 +03:00
|
|
|
case Visibility::APPROXIMATELY_VISIBLE:
|
2016-09-12 08:19:07 +03:00
|
|
|
TrackImage(mCurrentRequest);
|
|
|
|
TrackImage(mPendingRequest);
|
2016-03-26 00:49:43 +03:00
|
|
|
break;
|
|
|
|
|
2016-09-12 08:19:08 +03:00
|
|
|
case Visibility::APPROXIMATELY_NONVISIBLE:
|
2016-03-26 00:49:43 +03:00
|
|
|
UntrackImage(mCurrentRequest, aNonvisibleAction);
|
|
|
|
UntrackImage(mPendingRequest, aNonvisibleAction);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case Visibility::UNTRACKED:
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Shouldn't notify for untracked visibility");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-22 08:32:48 +03:00
|
|
|
void nsImageLoadingContent::TrackImage(imgIRequest* aImage,
|
|
|
|
nsIFrame* aFrame /*= nullptr */) {
|
2010-09-01 05:26:41 +04:00
|
|
|
if (!aImage) return;
|
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest,
|
|
|
|
"Why haven't we heard of this request?");
|
|
|
|
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetOurCurrentDoc();
|
2016-03-26 00:49:43 +03:00
|
|
|
if (!doc) {
|
|
|
|
return;
|
|
|
|
}
|
2015-07-20 05:30:35 +03:00
|
|
|
|
2017-03-22 08:32:48 +03:00
|
|
|
if (!aFrame) {
|
|
|
|
aFrame = GetOurPrimaryFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This line is deceptively simple. It hides a lot of subtlety. Before we
|
|
|
|
* create an nsImageFrame we call nsImageFrame::ShouldCreateImageFrameFor
|
|
|
|
* to determine if we should create an nsImageFrame or create a frame based
|
|
|
|
* on the display of the element (ie inline, block, etc). Inline, block, etc
|
|
|
|
* frames don't register for visibility tracking so they will return UNTRACKED
|
|
|
|
* from GetVisibility(). So this line is choosing to mark such images as
|
|
|
|
* visible. Once the image loads we will get an nsImageFrame and the proper
|
|
|
|
* visibility. This is a pitfall of tracking the visibility on the frames
|
|
|
|
* instead of the content node.
|
|
|
|
*/
|
|
|
|
if (!aFrame ||
|
|
|
|
aFrame->GetVisibility() == Visibility::APPROXIMATELY_NONVISIBLE) {
|
2016-03-26 00:49:43 +03:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (aImage == mCurrentRequest &&
|
|
|
|
!(mCurrentRequestFlags & REQUEST_IS_TRACKED)) {
|
|
|
|
mCurrentRequestFlags |= REQUEST_IS_TRACKED;
|
2016-10-13 10:04:19 +03:00
|
|
|
doc->ImageTracker()->Add(mCurrentRequest);
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
if (aImage == mPendingRequest &&
|
|
|
|
!(mPendingRequestFlags & REQUEST_IS_TRACKED)) {
|
|
|
|
mPendingRequestFlags |= REQUEST_IS_TRACKED;
|
2016-10-13 10:04:19 +03:00
|
|
|
doc->ImageTracker()->Add(mPendingRequest);
|
2013-02-14 00:18:08 +04:00
|
|
|
}
|
2010-08-04 04:40:29 +04:00
|
|
|
}
|
|
|
|
|
2015-02-06 13:51:12 +03:00
|
|
|
void nsImageLoadingContent::UntrackImage(
|
2016-03-26 00:49:43 +03:00
|
|
|
imgIRequest* aImage, const Maybe<OnNonvisible>& aNonvisibleAction
|
|
|
|
/* = Nothing() */) {
|
2010-09-01 05:26:41 +04:00
|
|
|
if (!aImage) return;
|
|
|
|
|
2012-08-14 02:11:50 +04:00
|
|
|
MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest,
|
|
|
|
"Why haven't we heard of this request?");
|
|
|
|
|
2013-04-09 20:50:35 +04:00
|
|
|
// We may not be in the document. If we outlived our document that's fine,
|
|
|
|
// because the document empties out the tracker and unlocks all locked images
|
|
|
|
// on destruction. But if we were never in the document we may need to force
|
|
|
|
// discarding the image here, since this is the only chance we have.
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetOurCurrentDoc();
|
2013-04-09 20:50:35 +04:00
|
|
|
if (aImage == mCurrentRequest) {
|
|
|
|
if (doc && (mCurrentRequestFlags & REQUEST_IS_TRACKED)) {
|
2013-02-14 00:18:08 +04:00
|
|
|
mCurrentRequestFlags &= ~REQUEST_IS_TRACKED;
|
2016-10-13 10:04:19 +03:00
|
|
|
doc->ImageTracker()->Remove(
|
2016-10-13 10:04:15 +03:00
|
|
|
mCurrentRequest,
|
|
|
|
aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)
|
|
|
|
? ImageTracker::REQUEST_DISCARD
|
|
|
|
: 0);
|
2016-03-26 00:49:43 +03:00
|
|
|
} else if (aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)) {
|
2013-04-09 20:50:35 +04:00
|
|
|
// If we're not in the document we may still need to be discarded.
|
|
|
|
aImage->RequestDiscard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (aImage == mPendingRequest) {
|
|
|
|
if (doc && (mPendingRequestFlags & REQUEST_IS_TRACKED)) {
|
2013-02-14 00:18:08 +04:00
|
|
|
mPendingRequestFlags &= ~REQUEST_IS_TRACKED;
|
2016-10-13 10:04:19 +03:00
|
|
|
doc->ImageTracker()->Remove(
|
2016-10-13 10:04:15 +03:00
|
|
|
mPendingRequest,
|
|
|
|
aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)
|
|
|
|
? ImageTracker::REQUEST_DISCARD
|
|
|
|
: 0);
|
2016-03-26 00:49:43 +03:00
|
|
|
} else if (aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)) {
|
2013-04-09 20:50:35 +04:00
|
|
|
// If we're not in the document we may still need to be discarded.
|
|
|
|
aImage->RequestDiscard();
|
2013-02-14 00:18:08 +04:00
|
|
|
}
|
|
|
|
}
|
2010-08-04 04:40:29 +04:00
|
|
|
}
|
|
|
|
|
2009-12-11 07:02:13 +03:00
|
|
|
void nsImageLoadingContent::CreateStaticImageClone(
|
|
|
|
nsImageLoadingContent* aDest) const {
|
2017-07-19 21:15:11 +03:00
|
|
|
aDest->ClearScriptedRequests(CURRENT_REQUEST, NS_BINDING_ABORTED);
|
2017-07-19 21:15:11 +03:00
|
|
|
aDest->mCurrentRequest = nsContentUtils::GetStaticRequest(
|
|
|
|
aDest->GetOurOwnerDoc(), mCurrentRequest);
|
2017-07-19 21:15:11 +03:00
|
|
|
if (aDest->mCurrentRequest) {
|
|
|
|
aDest->CloneScriptedRequests(aDest->mCurrentRequest);
|
|
|
|
}
|
2010-09-01 05:26:41 +04:00
|
|
|
aDest->TrackImage(aDest->mCurrentRequest);
|
2009-12-11 07:02:13 +03:00
|
|
|
aDest->mForcedImageState = mForcedImageState;
|
|
|
|
aDest->mImageBlockingStatus = mImageBlockingStatus;
|
|
|
|
aDest->mLoadingEnabled = mLoadingEnabled;
|
2010-07-31 21:54:59 +04:00
|
|
|
aDest->mStateChangerDepth = mStateChangerDepth;
|
2009-12-11 07:02:13 +03:00
|
|
|
aDest->mIsImageStateForced = mIsImageStateForced;
|
|
|
|
aDest->mLoading = mLoading;
|
|
|
|
aDest->mBroken = mBroken;
|
|
|
|
aDest->mUserDisabled = mUserDisabled;
|
|
|
|
aDest->mSuppressed = mSuppressed;
|
|
|
|
}
|
|
|
|
|
2011-07-14 22:47:35 +04:00
|
|
|
CORSMode nsImageLoadingContent::GetCORSMode() { return CORS_NONE; }
|
2013-02-01 15:00:41 +04:00
|
|
|
|
|
|
|
nsImageLoadingContent::ImageObserver::ImageObserver(
|
|
|
|
imgINotificationObserver* aObserver)
|
|
|
|
: mObserver(aObserver), mNext(nullptr) {
|
|
|
|
MOZ_COUNT_CTOR(ImageObserver);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsImageLoadingContent::ImageObserver::~ImageObserver() {
|
|
|
|
MOZ_COUNT_DTOR(ImageObserver);
|
|
|
|
NS_CONTENT_DELETE_LIST_MEMBER(ImageObserver, this, mNext);
|
|
|
|
}
|
2015-06-06 01:25:24 +03:00
|
|
|
|
2017-07-19 21:15:11 +03:00
|
|
|
nsImageLoadingContent::ScriptedImageObserver::ScriptedImageObserver(
|
|
|
|
imgINotificationObserver* aObserver,
|
|
|
|
RefPtr<imgRequestProxy>&& aCurrentRequest,
|
|
|
|
RefPtr<imgRequestProxy>&& aPendingRequest)
|
|
|
|
: mObserver(aObserver),
|
|
|
|
mCurrentRequest(aCurrentRequest),
|
|
|
|
mPendingRequest(aPendingRequest) {}
|
|
|
|
|
|
|
|
nsImageLoadingContent::ScriptedImageObserver::~ScriptedImageObserver() {
|
|
|
|
// We should have cancelled any requests before getting released.
|
|
|
|
DebugOnly<bool> cancel = CancelRequests();
|
|
|
|
MOZ_ASSERT(!cancel, "Still have requests in ~ScriptedImageObserver!");
|
|
|
|
}
|
|
|
|
|
|
|
|
bool nsImageLoadingContent::ScriptedImageObserver::CancelRequests() {
|
|
|
|
bool cancelled = false;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
|
|
|
mCurrentRequest = nullptr;
|
|
|
|
cancelled = true;
|
|
|
|
}
|
|
|
|
if (mPendingRequest) {
|
|
|
|
mPendingRequest->CancelAndForgetObserver(NS_BINDING_ABORTED);
|
|
|
|
mPendingRequest = nullptr;
|
|
|
|
cancelled = true;
|
|
|
|
}
|
|
|
|
return cancelled;
|
|
|
|
}
|
|
|
|
|
2015-06-06 01:25:24 +03:00
|
|
|
// Only HTMLInputElement.h overrides this for <img> tags
|
|
|
|
// all other subclasses use this one, i.e. ignore referrer attributes
|
|
|
|
mozilla::net::ReferrerPolicy nsImageLoadingContent::GetImageReferrerPolicy() {
|
|
|
|
return mozilla::net::RP_Unset;
|
2018-04-28 22:50:58 +03:00
|
|
|
}
|
2018-07-04 20:26:09 +03:00
|
|
|
|
|
|
|
Element* nsImageLoadingContent::FindImageMap() {
|
|
|
|
nsIContent* thisContent = AsContent();
|
|
|
|
Element* thisElement = thisContent->AsElement();
|
|
|
|
|
|
|
|
nsAutoString useMap;
|
|
|
|
thisElement->GetAttr(kNameSpaceID_None, nsGkAtoms::usemap, useMap);
|
|
|
|
if (useMap.IsEmpty()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAString::const_iterator start, end;
|
|
|
|
useMap.BeginReading(start);
|
|
|
|
useMap.EndReading(end);
|
|
|
|
|
|
|
|
int32_t hash = useMap.FindChar('#');
|
|
|
|
if (hash < 0) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
// useMap contains a '#', set start to point right after the '#'
|
|
|
|
start.advance(hash + 1);
|
|
|
|
|
|
|
|
if (start == end) {
|
|
|
|
return nullptr; // useMap == "#"
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<nsContentList> imageMapList;
|
|
|
|
if (thisElement->IsInUncomposedDoc()) {
|
|
|
|
// Optimize the common case and use document level image map.
|
|
|
|
imageMapList = thisElement->OwnerDoc()->ImageMapList();
|
|
|
|
} else {
|
|
|
|
// Per HTML spec image map should be searched in the element's scope,
|
|
|
|
// so using SubtreeRoot() here.
|
|
|
|
// Because this is a temporary list, we don't need to make it live.
|
|
|
|
imageMapList =
|
|
|
|
new nsContentList(thisElement->SubtreeRoot(), kNameSpaceID_XHTML,
|
|
|
|
nsGkAtoms::map, nsGkAtoms::map, true, /* deep */
|
|
|
|
false /* live */);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsAutoString mapName(Substring(start, end));
|
|
|
|
|
|
|
|
uint32_t i, n = imageMapList->Length(true);
|
|
|
|
for (i = 0; i < n; ++i) {
|
|
|
|
nsIContent* map = imageMapList->Item(i);
|
|
|
|
if (map->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::id, mapName,
|
|
|
|
eCaseMatters) ||
|
|
|
|
map->AsElement()->AttrValueIs(kNameSpaceID_None, nsGkAtoms::name,
|
|
|
|
mapName, eCaseMatters)) {
|
|
|
|
return map->AsElement();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|