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"
|
|
|
|
#include "nsIScriptGlobalObject.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 "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"
|
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"
|
2004-03-09 06:57:51 +03:00
|
|
|
|
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"
|
2019-07-22 20:35:20 +03:00
|
|
|
#include "mozilla/CycleCollectedJSContext.h"
|
2017-04-25 04:17:38 +03:00
|
|
|
#include "mozilla/EventStateManager.h"
|
2020-06-25 23:42:49 +03:00
|
|
|
#include "mozilla/Preferences.h"
|
|
|
|
#include "mozilla/PresShell.h"
|
|
|
|
#include "mozilla/StaticPrefs_image.h"
|
|
|
|
#include "mozilla/SVGImageFrame.h"
|
|
|
|
#include "mozilla/SVGObserverUtils.h"
|
|
|
|
#include "mozilla/dom/BindContext.h"
|
|
|
|
#include "mozilla/dom/Document.h"
|
2011-07-20 23:18:54 +04:00
|
|
|
#include "mozilla/dom/Element.h"
|
2022-03-15 21:14:08 +03:00
|
|
|
#include "mozilla/dom/PContent.h" // For TextRecognitionResult
|
2020-02-13 00:29:32 +03:00
|
|
|
#include "mozilla/dom/HTMLImageElement.h"
|
2022-06-03 19:01:33 +03:00
|
|
|
#include "mozilla/dom/ImageTextBinding.h"
|
2016-10-13 10:04:15 +03:00
|
|
|
#include "mozilla/dom/ImageTracker.h"
|
2023-03-22 17:54:43 +03:00
|
|
|
#include "mozilla/dom/ReferrerInfo.h"
|
2013-12-12 05:51:58 +04:00
|
|
|
#include "mozilla/dom/ScriptSettings.h"
|
2022-08-10 22:38:46 +03:00
|
|
|
#include "mozilla/intl/LocaleService.h"
|
|
|
|
#include "mozilla/intl/Locale.h"
|
2019-01-30 15:43:34 +03:00
|
|
|
#include "mozilla/net/UrlClassifierFeatureFactory.h"
|
2022-03-15 21:14:08 +03:00
|
|
|
#include "mozilla/widget/TextRecognition.h"
|
2008-04-11 21:29:06 +04:00
|
|
|
|
2020-11-23 19:21:38 +03:00
|
|
|
#include "Orientation.h"
|
|
|
|
|
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()
|
2022-02-03 20:31:41 +03:00
|
|
|
: mObserverList(nullptr),
|
2018-11-08 20:45:50 +03:00
|
|
|
mOutstandingDecodePromises(0),
|
|
|
|
mRequestGeneration(0),
|
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),
|
|
|
|
mNewRequestsWillNeedAnimationReset(false),
|
2017-04-25 04:17:38 +03:00
|
|
|
mUseUrgentStartForChannel(false),
|
2020-02-15 11:45:27 +03:00
|
|
|
mLazyLoading(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
|
|
|
}
|
|
|
|
|
2020-07-21 15:08:14 +03:00
|
|
|
void nsImageLoadingContent::Destroy() {
|
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() {
|
2020-07-21 15:08:14 +03:00
|
|
|
MOZ_ASSERT(!mCurrentRequest && !mPendingRequest, "Destroy not called");
|
2018-11-08 20:45:50 +03:00
|
|
|
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
|
|
|
*/
|
2020-03-12 07:25:34 +03:00
|
|
|
void 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) {
|
2022-02-03 20:31:41 +03:00
|
|
|
return OnUnlockedDraw();
|
2013-02-25 04:59:22 +04:00
|
|
|
}
|
|
|
|
|
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)) {
|
2019-01-02 16:05:23 +03:00
|
|
|
Document* doc = GetOurOwnerDoc();
|
2022-03-15 21:14:08 +03:00
|
|
|
doc->AddBlockedNodeByClassifier(AsContent());
|
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
|
|
|
|
2019-07-29 16:05:14 +03:00
|
|
|
if ((aType == imgINotificationObserver::FRAME_COMPLETE ||
|
|
|
|
aType == imgINotificationObserver::FRAME_UPDATE) &&
|
2018-11-08 20:45:50 +03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-03-12 07:25:34 +03:00
|
|
|
void 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 &
|
2020-03-12 07:25:34 +03:00
|
|
|
(imgIRequest::STATUS_ERROR | imgIRequest::STATUS_LOAD_COMPLETE))) {
|
|
|
|
return;
|
|
|
|
}
|
2012-10-12 20:11:22 +04:00
|
|
|
|
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(u"load"_ns);
|
2003-03-19 04:08:22 +03:00
|
|
|
} else {
|
2015-06-20 01:55:12 +03:00
|
|
|
FireEvent(u"error"_ns);
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:14:08 +03:00
|
|
|
SVGObserverUtils::InvalidateDirectRenderingObservers(
|
|
|
|
AsContent()->AsElement());
|
2018-11-08 20:45:50 +03:00
|
|
|
MaybeResolveDecodePromises();
|
2003-03-19 04:08:22 +03:00
|
|
|
}
|
|
|
|
|
2013-02-25 04:59:22 +04:00
|
|
|
void nsImageLoadingContent::OnUnlockedDraw() {
|
2022-02-03 20:31:42 +03:00
|
|
|
// This notification is only sent for animated images. It's OK for
|
|
|
|
// non-animated images to wait until the next frame visibility 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.
|
2015-06-03 04:29:50 +03:00
|
|
|
|
2022-08-03 21:23:43 +03:00
|
|
|
nsIFrame* frame = GetOurPrimaryImageFrame();
|
2016-03-26 00:49:43 +03:00
|
|
|
if (!frame) {
|
2013-02-25 04:59:22 +04:00
|
|
|
return;
|
2016-03-26 00:49:43 +03:00
|
|
|
}
|
|
|
|
|
2019-04-26 02:03:04 +03:00
|
|
|
if (frame->GetVisibility() == Visibility::ApproximatelyVisible) {
|
2016-09-12 08:19:08 +03:00
|
|
|
// 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
|
|
|
}
|
|
|
|
|
2020-03-12 07:25:34 +03:00
|
|
|
void nsImageLoadingContent::OnImageIsAnimated(imgIRequest* aRequest) {
|
2022-02-03 20:31:41 +03:00
|
|
|
bool* requestFlag = nullptr;
|
|
|
|
if (aRequest == mCurrentRequest) {
|
|
|
|
requestFlag = &mCurrentRequestRegistered;
|
|
|
|
} else if (aRequest == mPendingRequest) {
|
|
|
|
requestFlag = &mPendingRequestRegistered;
|
|
|
|
} else {
|
|
|
|
MOZ_ASSERT_UNREACHABLE("Which image is this?");
|
|
|
|
return;
|
2011-11-10 01:39:16 +04:00
|
|
|
}
|
2022-02-03 20:31:41 +03:00
|
|
|
nsLayoutUtils::RegisterImageRequest(GetFramePresContext(), aRequest,
|
|
|
|
requestFlag);
|
2011-11-10 01:39:15 +04:00
|
|
|
}
|
|
|
|
|
2022-08-03 21:23:43 +03:00
|
|
|
static bool IsOurImageFrame(nsIFrame* aFrame) {
|
|
|
|
if (nsImageFrame* f = do_QueryFrame(aFrame)) {
|
2023-02-16 15:10:08 +03:00
|
|
|
return f->IsForImageLoadingContent();
|
2022-08-03 21:23:43 +03:00
|
|
|
}
|
|
|
|
return aFrame->IsSVGImageFrame() || aFrame->IsSVGFEImageFrame();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsIFrame* nsImageLoadingContent::GetOurPrimaryImageFrame() {
|
|
|
|
nsIFrame* frame = AsContent()->GetPrimaryFrame();
|
|
|
|
if (!frame || !IsOurImageFrame(frame)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return frame;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-08-17 13:10:26 +03:00
|
|
|
nsresult nsImageLoadingContent::GetSyncDecodingHint(bool* aHint) {
|
|
|
|
*aHint = mSyncDecodingHint;
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-22 20:35:20 +03:00
|
|
|
class QueueDecodeTask final : public MicroTaskRunnable {
|
2018-11-08 20:45:50 +03:00
|
|
|
public:
|
|
|
|
QueueDecodeTask(nsImageLoadingContent* aOwner, Promise* aPromise,
|
|
|
|
uint32_t aRequestGeneration)
|
2019-07-22 20:35:20 +03:00
|
|
|
: MicroTaskRunnable(),
|
2018-11-08 20:45:50 +03:00
|
|
|
mOwner(aOwner),
|
|
|
|
mPromise(aPromise),
|
|
|
|
mRequestGeneration(aRequestGeneration) {}
|
|
|
|
|
2019-07-22 20:35:20 +03:00
|
|
|
virtual void Run(AutoSlowOperation& aAso) override {
|
2018-11-08 20:45:50 +03:00
|
|
|
mOwner->DecodeAsync(std::move(mPromise), mRequestGeneration);
|
2019-07-22 20:35:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual bool Suppressed() override {
|
|
|
|
nsIGlobalObject* global = mOwner->GetOurOwnerDoc()->GetScopeObject();
|
|
|
|
return global && global->IsInSyncOperation();
|
2018-11-08 20:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
RefPtr<nsImageLoadingContent> mOwner;
|
|
|
|
RefPtr<Promise> mPromise;
|
|
|
|
uint32_t mRequestGeneration;
|
|
|
|
};
|
|
|
|
|
|
|
|
if (++mOutstandingDecodePromises == 1) {
|
|
|
|
MOZ_ASSERT(mDecodePromises.IsEmpty());
|
2019-04-18 20:31:10 +03:00
|
|
|
doc->RegisterActivityObserver(AsContent()->AsElement());
|
2018-11-08 20:45:50 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
auto task = MakeRefPtr<QueueDecodeTask>(this, promise, mRequestGeneration);
|
2019-07-22 20:35:20 +03:00
|
|
|
CycleCollectedJSContext::Get()->DispatchToMicroTask(task.forget());
|
2018-11-08 20:45:50 +03:00
|
|
|
return promise.forget();
|
|
|
|
}
|
|
|
|
|
|
|
|
void nsImageLoadingContent::DecodeAsync(RefPtr<Promise>&& aPromise,
|
|
|
|
uint32_t aRequestGeneration) {
|
|
|
|
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;
|
2020-04-21 00:04:45 +03:00
|
|
|
imgIContainer::DecodeResult decodeResult =
|
|
|
|
mCurrentRequest->RequestDecodeWithResult(flags);
|
|
|
|
if (decodeResult == imgIContainer::DECODE_REQUESTED) {
|
2018-11-08 20:45:50 +03:00
|
|
|
return;
|
|
|
|
}
|
2020-04-21 00:04:45 +03:00
|
|
|
if (decodeResult == imgIContainer::DECODE_REQUEST_FAILED) {
|
|
|
|
RejectDecodePromises(NS_ERROR_DOM_IMAGE_BROKEN);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
MOZ_ASSERT(decodeResult == imgIContainer::DECODE_SURFACE_AVAILABLE);
|
2018-11-08 20:45:50 +03:00
|
|
|
|
|
|
|
// 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());
|
2019-04-18 20:31:10 +03:00
|
|
|
GetOurOwnerDoc()->UnregisterActivityObserver(AsContent()->AsElement());
|
2018-11-08 20:45:50 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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 */) {
|
2022-08-17 13:10:26 +03:00
|
|
|
// GetOurPrimaryImageFrame() might not return the frame during frame init.
|
|
|
|
nsIFrame* frame = aFrame ? aFrame : GetOurPrimaryImageFrame();
|
2022-08-03 21:23:43 +03:00
|
|
|
if (!frame) {
|
2018-08-08 14:56:01 +03:00
|
|
|
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(
|
2019-06-26 06:24:21 +03:00
|
|
|
StaticPrefs::image_infer_src_animation_threshold_ms());
|
2018-08-08 14:56:01 +03:00
|
|
|
|
|
|
|
// 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;
|
|
|
|
}
|
|
|
|
|
2022-08-03 21:23:43 +03:00
|
|
|
if (nsImageFrame* imageFrame = do_QueryFrame(frame)) {
|
2018-08-08 14:56:01 +03:00
|
|
|
imageFrame->SetForceSyncDecoding(forceSync);
|
2022-08-03 21:23:43 +03:00
|
|
|
} else if (SVGImageFrame* svgImageFrame = do_QueryFrame(frame)) {
|
2018-08-08 14:56:01 +03:00
|
|
|
svgImageFrame->SetForceSyncDecoding(forceSync);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:08:02 +03:00
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers.Clone());
|
2017-07-19 21:15:11 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:08:02 +03:00
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers.Clone());
|
2017-07-19 21:15:11 +03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-05-05 13:08:02 +03:00
|
|
|
nsTArray<RefPtr<ScriptedImageObserver>> observers(mScriptedObservers.Clone());
|
2017-07-19 21:15:11 +03:00
|
|
|
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) {
|
2022-08-03 21:23:43 +03:00
|
|
|
MOZ_ASSERT(aFrame, "aFrame is null");
|
|
|
|
MOZ_ASSERT(IsOurImageFrame(aFrame));
|
2011-11-10 01:39:16 +04:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2020-09-15 14:59:28 +03:00
|
|
|
already_AddRefed<nsIURI> nsImageLoadingContent::GetCurrentURI() {
|
2013-01-02 23:30:02 +04:00
|
|
|
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);
|
2020-09-15 14:59:28 +03:00
|
|
|
*aURI = GetCurrentURI().take();
|
|
|
|
return NS_OK;
|
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);
|
2020-08-02 18:29:15 +03:00
|
|
|
nsresult rv = loader->LoadImageWithChannel(aChannel, this, 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(u"error"_ns);
|
|
|
|
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;
|
2021-05-07 16:56:33 +03:00
|
|
|
nsresult rv = LoadImage(currentURI, true, aNotify, loadType,
|
|
|
|
nsIRequest::VALIDATE_ALWAYS | LoadFlags());
|
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-05 05:06:27 +03:00
|
|
|
// Parse the URI string to get image URI
|
2003-03-19 04:08:22 +03:00
|
|
|
nsCOMPtr<nsIURI> imageURI;
|
2021-05-07 16:56:33 +03:00
|
|
|
if (!aNewURI.IsEmpty()) {
|
|
|
|
Unused << StringToURI(aNewURI, doc, getter_AddRefs(imageURI));
|
|
|
|
}
|
2005-09-21 23:14:30 +04:00
|
|
|
|
2021-05-07 16:56:33 +03:00
|
|
|
return LoadImage(imageURI, aForce, aNotify, aImageLoadType, LoadFlags(), doc,
|
|
|
|
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,
|
|
|
|
nsLoadFlags aLoadFlags,
|
2021-05-07 16:56:33 +03:00
|
|
|
Document* aDocument,
|
2019-01-02 16:05:23 +03:00
|
|
|
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();
|
|
|
|
|
2021-05-07 16:56:33 +03:00
|
|
|
if (!aNewURI) {
|
|
|
|
// Cancel image requests and then fire only error event per spec.
|
|
|
|
CancelImageRequests(aNotify);
|
|
|
|
if (aImageLoadType == eImageLoadType_Normal) {
|
|
|
|
// Mark error event as cancelable only for src="" case, since only this
|
|
|
|
// error causes site compat problem (bug 1308069) for now.
|
|
|
|
FireEvent(u"error"_ns, true);
|
|
|
|
}
|
|
|
|
return NS_OK;
|
2021-05-07 16:46:48 +03:00
|
|
|
}
|
2021-05-07 14:44:16 +03:00
|
|
|
|
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(u"error"_ns);
|
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.
|
2020-07-22 23:29:00 +03:00
|
|
|
//
|
|
|
|
// FIXME(emilio): Shouldn't this check be part of
|
|
|
|
// Document::ShouldLoadImages()? Or alternatively check ShouldLoadImages here
|
|
|
|
// instead? (It seems we only check ShouldLoadImages in HTMLImageElement,
|
|
|
|
// which seems wrong...)
|
|
|
|
if (aDocument->IsLoadedAsData() && !aDocument->IsStaticDocument()) {
|
2020-09-15 14:59:28 +03:00
|
|
|
// Clear our pending request if we do have one.
|
2019-04-26 02:03:04 +03:00
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DiscardImages));
|
2018-04-04 21:40:01 +03:00
|
|
|
|
2017-01-19 06:48:00 +03:00
|
|
|
FireEvent(u"error"_ns);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2010-07-31 21:54:59 +04:00
|
|
|
// URI equality check.
|
|
|
|
//
|
2020-09-15 14:59:28 +03:00
|
|
|
// We skip the equality check if we don't have a current image, since in that
|
2010-07-31 21:54:59 +04:00
|
|
|
// case we really do want to try loading again.
|
2020-09-15 14:59:28 +03:00
|
|
|
if (!aForce && mCurrentRequest) {
|
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.
|
2020-05-04 21:50:20 +03:00
|
|
|
Element* element = AsContent()->AsElement();
|
|
|
|
MOZ_ASSERT(element->NodePrincipal() == aDocument->NodePrincipal(),
|
2015-02-10 01:34:50 +03:00
|
|
|
"Principal mismatch?");
|
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-10-18 08:24:48 +03:00
|
|
|
RefPtr<imgRequestProxy>& req = PrepareNextRequest(aImageLoadType);
|
2017-10-03 06:38:30 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> triggeringPrincipal;
|
2017-10-11 12:07:35 +03:00
|
|
|
bool result = nsContentUtils::QueryTriggeringPrincipal(
|
2020-05-04 21:50:20 +03:00
|
|
|
element, 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);
|
|
|
|
|
2020-05-04 21:50:20 +03:00
|
|
|
auto referrerInfo = MakeRefPtr<ReferrerInfo>(*element);
|
2016-04-11 11:58:03 +03:00
|
|
|
nsresult rv = nsContentUtils::LoadImage(
|
2020-05-04 21:50:20 +03:00
|
|
|
aNewURI, element, aDocument, triggeringPrincipal, 0, referrerInfo, this,
|
|
|
|
loadFlags, element->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)) {
|
2021-09-28 15:04:42 +03:00
|
|
|
// Based on performance testing unsuppressing painting soon after the page
|
|
|
|
// has gotten an image may improve visual metrics.
|
|
|
|
if (Document* doc = element->GetComposedDoc()) {
|
|
|
|
if (PresShell* shell = doc->GetPresShell()) {
|
|
|
|
shell->TryUnsuppressPaintingSoon();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2022-10-29 00:56:05 +03:00
|
|
|
// Handle cases when we just ended up with a 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.
|
|
|
|
{
|
|
|
|
uint32_t loadStatus;
|
|
|
|
if (NS_SUCCEEDED(req->GetImageStatus(&loadStatus)) &&
|
|
|
|
(loadStatus & imgIRequest::STATUS_LOAD_COMPLETE)) {
|
|
|
|
if (req == mPendingRequest) {
|
|
|
|
MakePendingRequestCurrent();
|
|
|
|
}
|
2012-03-20 02:22:02 +04:00
|
|
|
MOZ_ASSERT(mCurrentRequest,
|
|
|
|
"How could we not have a current request here?");
|
|
|
|
|
2022-08-03 21:23:43 +03:00
|
|
|
if (nsImageFrame* f = do_QueryFrame(GetOurPrimaryImageFrame())) {
|
2012-03-20 02:22:02 +04:00
|
|
|
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.
|
2020-09-10 01:58:29 +03:00
|
|
|
if (!mCurrentRequest) {
|
|
|
|
mCurrentURI = aNewURI;
|
|
|
|
}
|
2016-08-16 07:01:12 +03:00
|
|
|
|
2005-08-18 03:01:09 +04:00
|
|
|
FireEvent(u"error"_ns);
|
|
|
|
}
|
|
|
|
|
2005-09-18 22:05:40 +04:00
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-03 08:18:36 +04:00
|
|
|
void nsImageLoadingContent::ForceImageState(bool aForce,
|
2022-06-08 02:09:52 +03:00
|
|
|
ElementState::InternalType aState) {
|
2009-06-26 11:25:17 +04:00
|
|
|
mIsImageStateForced = aForce;
|
2022-06-08 02:09:52 +03:00
|
|
|
mForcedImageState = ElementState(aState);
|
2009-06-26 11:25:17 +04:00
|
|
|
}
|
|
|
|
|
2022-03-15 21:14:08 +03:00
|
|
|
already_AddRefed<Promise> nsImageLoadingContent::RecognizeCurrentImageText(
|
|
|
|
ErrorResult& aRv) {
|
|
|
|
using widget::TextRecognition;
|
|
|
|
|
|
|
|
if (!mCurrentRequest) {
|
|
|
|
aRv.ThrowInvalidStateError("No current request");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
mCurrentRequest->GetImage(getter_AddRefs(image));
|
|
|
|
if (!image) {
|
|
|
|
aRv.ThrowInvalidStateError("No image");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
RefPtr<Promise> domPromise =
|
|
|
|
Promise::Create(GetOurOwnerDoc()->GetScopeObject(), aRv);
|
|
|
|
if (aRv.Failed()) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2022-08-10 22:38:46 +03:00
|
|
|
// The list of ISO 639-1 language tags to pass to the text recognition API.
|
|
|
|
AutoTArray<nsCString, 4> languages;
|
|
|
|
{
|
|
|
|
// The document's locale should be the top language to use. Parse the BCP 47
|
|
|
|
// locale and extract the ISO 639-1 language tag. e.g. "en-US" -> "en".
|
|
|
|
nsAutoCString elementLanguage;
|
|
|
|
nsAtom* imgLanguage = AsContent()->GetLang();
|
|
|
|
intl::Locale locale;
|
|
|
|
if (imgLanguage) {
|
|
|
|
imgLanguage->ToUTF8String(elementLanguage);
|
|
|
|
auto result = intl::LocaleParser::TryParse(elementLanguage, locale);
|
|
|
|
if (result.isOk()) {
|
|
|
|
languages.AppendElement(locale.Language().Span());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 20:19:31 +03:00
|
|
|
|
2022-08-10 22:38:46 +03:00
|
|
|
{
|
|
|
|
// The app locales should also be included after the document's locales.
|
|
|
|
// Extract the language tag like above.
|
|
|
|
nsTArray<nsCString> appLocales;
|
|
|
|
intl::LocaleService::GetInstance()->GetAppLocalesAsBCP47(appLocales);
|
|
|
|
|
|
|
|
for (const auto& localeString : appLocales) {
|
|
|
|
intl::Locale locale;
|
|
|
|
auto result = intl::LocaleParser::TryParse(localeString, locale);
|
|
|
|
if (result.isErr()) {
|
|
|
|
NS_WARNING("Could not parse an app locale string, ignoring it.");
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
languages.AppendElement(locale.Language().Span());
|
|
|
|
}
|
|
|
|
}
|
2022-08-10 20:19:31 +03:00
|
|
|
|
2022-08-10 22:38:46 +03:00
|
|
|
TextRecognition::FindText(*image, languages)
|
|
|
|
->Then(
|
|
|
|
GetCurrentSerialEventTarget(), __func__,
|
|
|
|
[weak = RefPtr{do_GetWeakReference(this)},
|
|
|
|
request = RefPtr{mCurrentRequest}, domPromise](
|
|
|
|
TextRecognition::NativePromise::ResolveOrRejectValue&& aValue) {
|
|
|
|
if (aValue.IsReject()) {
|
|
|
|
domPromise->MaybeRejectWithNotSupportedError(
|
|
|
|
aValue.RejectValue());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
RefPtr<nsIImageLoadingContent> iilc = do_QueryReferent(weak.get());
|
|
|
|
if (!iilc) {
|
|
|
|
domPromise->MaybeRejectWithInvalidStateError(
|
|
|
|
"Element was dead when we got the results");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto* ilc = static_cast<nsImageLoadingContent*>(iilc.get());
|
|
|
|
if (ilc->mCurrentRequest != request) {
|
|
|
|
domPromise->MaybeRejectWithInvalidStateError(
|
|
|
|
"Request not current");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
auto& textRecognitionResult = aValue.ResolveValue();
|
|
|
|
Element* el = ilc->AsContent()->AsElement();
|
|
|
|
|
|
|
|
// When enabled, this feature will place the recognized text as
|
|
|
|
// spans inside of the shadow dom of the img element. These are then
|
|
|
|
// positioned so that the user can select the text.
|
|
|
|
if (Preferences::GetBool("dom.text-recognition.shadow-dom-enabled",
|
|
|
|
false)) {
|
|
|
|
el->AttachAndSetUAShadowRoot(Element::NotifyUAWidgetSetup::Yes);
|
|
|
|
TextRecognition::FillShadow(*el->GetShadowRoot(),
|
|
|
|
textRecognitionResult);
|
|
|
|
el->NotifyUAWidgetSetupOrChange();
|
|
|
|
}
|
|
|
|
|
|
|
|
nsTArray<ImageText> imageTexts(
|
|
|
|
textRecognitionResult.quads().Length());
|
|
|
|
nsIGlobalObject* global = el->OwnerDoc()->GetOwnerGlobal();
|
|
|
|
|
|
|
|
for (const auto& quad : textRecognitionResult.quads()) {
|
|
|
|
NotNull<ImageText*> imageText = imageTexts.AppendElement();
|
|
|
|
|
|
|
|
// Note: These points are not actually CSSPixels, but a DOMQuad is
|
|
|
|
// a conveniently similar structure that can store these values.
|
|
|
|
CSSPoint points[4];
|
|
|
|
points[0] = CSSPoint(quad.points()[0].x, quad.points()[0].y);
|
|
|
|
points[1] = CSSPoint(quad.points()[1].x, quad.points()[1].y);
|
|
|
|
points[2] = CSSPoint(quad.points()[2].x, quad.points()[2].y);
|
|
|
|
points[3] = CSSPoint(quad.points()[3].x, quad.points()[3].y);
|
|
|
|
|
|
|
|
imageText->mQuad = new DOMQuad(global, points);
|
|
|
|
imageText->mConfidence = quad.confidence();
|
|
|
|
imageText->mString = quad.string();
|
|
|
|
}
|
|
|
|
domPromise->MaybeResolve(std::move(imageTexts));
|
|
|
|
});
|
2022-03-15 21:14:08 +03:00
|
|
|
return domPromise.forget();
|
|
|
|
}
|
|
|
|
|
2020-09-15 12:04:59 +03:00
|
|
|
CSSIntSize nsImageLoadingContent::GetWidthHeightForImage() {
|
|
|
|
Element* element = AsContent()->AsElement();
|
|
|
|
if (nsIFrame* frame = element->GetPrimaryFrame(FlushType::Layout)) {
|
|
|
|
return CSSIntSize::FromAppUnitsRounded(frame->GetContentRect().Size());
|
|
|
|
}
|
|
|
|
const nsAttrValue* value;
|
|
|
|
nsCOMPtr<imgIContainer> image;
|
|
|
|
if (mCurrentRequest) {
|
|
|
|
mCurrentRequest->GetImage(getter_AddRefs(image));
|
|
|
|
}
|
|
|
|
|
|
|
|
CSSIntSize size;
|
|
|
|
if ((value = element->GetParsedAttr(nsGkAtoms::width)) &&
|
|
|
|
value->Type() == nsAttrValue::eInteger) {
|
|
|
|
size.width = value->GetIntegerValue();
|
|
|
|
} else if (image) {
|
|
|
|
image->GetWidth(&size.width);
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((value = element->GetParsedAttr(nsGkAtoms::height)) &&
|
|
|
|
value->Type() == nsAttrValue::eInteger) {
|
|
|
|
size.height = value->GetIntegerValue();
|
|
|
|
} else if (image) {
|
|
|
|
image->GetHeight(&size.height);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_ASSERTION(size.width >= 0, "negative width");
|
|
|
|
NS_ASSERTION(size.height >= 0, "negative height");
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2022-06-08 02:09:52 +03:00
|
|
|
ElementState nsImageLoadingContent::ImageState() const {
|
2010-10-20 15:26:32 +04:00
|
|
|
if (mIsImageStateForced) {
|
|
|
|
return mForcedImageState;
|
|
|
|
}
|
|
|
|
|
2022-06-08 02:09:52 +03:00
|
|
|
ElementState states;
|
2010-10-20 15:26:32 +04:00
|
|
|
|
|
|
|
if (mBroken) {
|
2022-06-08 02:09:52 +03:00
|
|
|
states |= ElementState::BROKEN;
|
2010-10-20 15:26:32 +04:00
|
|
|
}
|
|
|
|
if (mLoading) {
|
2022-06-08 02:09:52 +03:00
|
|
|
states |= ElementState::LOADING;
|
2010-10-20 15:26:32 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
2020-09-13 14:33:13 +03:00
|
|
|
mLoading = mBroken = false;
|
2014-08-20 01:49:38 +04:00
|
|
|
|
2020-09-13 14:33:13 +03:00
|
|
|
// If we were blocked, we're broken, so are we if we don't have an image
|
|
|
|
// request at all or the image has errored.
|
2020-09-15 14:59:28 +03:00
|
|
|
if (!mCurrentRequest) {
|
2020-02-15 11:45:27 +03:00
|
|
|
if (!mLazyLoading) {
|
|
|
|
// In case of non-lazy loading, no current request means error, since we
|
|
|
|
// weren't disabled or suppressed
|
|
|
|
mBroken = true;
|
|
|
|
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
|
|
|
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);
|
2019-04-26 02:03:04 +03:00
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DiscardImages));
|
|
|
|
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DiscardImages));
|
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
|
|
|
nsPresContext* nsImageLoadingContent::GetFramePresContext() {
|
2022-08-03 21:23:43 +03:00
|
|
|
nsIFrame* frame = GetOurPrimaryImageFrame();
|
2011-11-10 01:39:16 +04:00
|
|
|
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();
|
2019-07-15 21:29:05 +03:00
|
|
|
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
|
2019-07-15 16:39:51 +03:00
|
|
|
return NS_NewURI(aURI, aSpec, encoding, baseURL);
|
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
|
|
|
|
2022-03-15 21:14:08 +03:00
|
|
|
nsCOMPtr<nsINode> thisNode = AsContent();
|
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
|
|
|
}
|
|
|
|
|
2014-08-20 01:49:38 +04:00
|
|
|
RefPtr<imgRequestProxy>& nsImageLoadingContent::PrepareCurrentRequest(
|
|
|
|
ImageLoadType aImageLoadType) {
|
2010-07-31 21:54:59 +04:00
|
|
|
// Get rid of anything that was there previously.
|
2019-04-26 02:03:04 +03:00
|
|
|
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DiscardImages));
|
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.
|
2019-04-26 02:03:04 +03:00
|
|
|
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DiscardImages));
|
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
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Bug 1555216 - Change the signature of BindToTree to be (BindContext&, nsINode& aParentNode). r=bzbarsky
BindContext was going to have way more information at first, but then I realized
that most of the things I wanted to know were basically a flag away using the
parent node.
Still I think it's worth it, now experimenting with BindToTree will only mean
adding a field to a struct that's included from a couple cpp files, instead of a
massive pain.
I also think this is clearer, and doing this highlights quite a few
inconsistencies in our code which I've left untouched, but commented with
FIXMEs.
Steps are:
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsresult BindToTree(Document\* aDocument, nsIContent\* aParent,#nsresult BindToTree(BindContext\&, nsINode\& aParent)#g' $file; done
$ for file in $(rg 'nsresult BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's# nsIContent\* aBindingParent) override#override#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(Document\* aDocument, nsIContent\* aParent,#::BindToTree(BindContext\& aContext, nsINode\& aParent)#g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#nsIContent\* aBindingParent)##g' $file; done
$ for file in $(rg '::BindToTree\(' | cut -d : -f 1 | sort | uniq); do sed -i 's#::BindToTree(aDocument, aParent, aBindingParent)#::BindToTree(aContext, aParent)#g' $file; done
$ ./mach clang-format
Then manual fixups.
Depends on D32948
Differential Revision: https://phabricator.services.mozilla.com/D32949
2019-05-29 07:27:04 +03:00
|
|
|
void nsImageLoadingContent::BindToTree(BindContext& aContext,
|
|
|
|
nsINode& aParent) {
|
2019-01-09 18:59:10 +03:00
|
|
|
// We may be getting connected, if so our image should be tracked,
|
2019-06-01 17:40:33 +03:00
|
|
|
if (aContext.InComposedDoc()) {
|
2019-01-09 18:59:10 +03:00
|
|
|
TrackImage(mCurrentRequest);
|
|
|
|
TrackImage(mPendingRequest);
|
|
|
|
}
|
2012-08-14 02:11:50 +04:00
|
|
|
}
|
|
|
|
|
2019-05-29 01:47:08 +03:00
|
|
|
void nsImageLoadingContent::UnbindFromTree(bool aNullParent) {
|
2012-08-14 02:11:50 +04:00
|
|
|
// 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) {
|
2019-04-26 02:03:04 +03:00
|
|
|
case Visibility::ApproximatelyVisible:
|
2016-09-12 08:19:07 +03:00
|
|
|
TrackImage(mCurrentRequest);
|
|
|
|
TrackImage(mPendingRequest);
|
2016-03-26 00:49:43 +03:00
|
|
|
break;
|
|
|
|
|
2019-04-26 02:03:04 +03:00
|
|
|
case Visibility::ApproximatelyNonVisible:
|
2016-03-26 00:49:43 +03:00
|
|
|
UntrackImage(mCurrentRequest, aNonvisibleAction);
|
|
|
|
UntrackImage(mPendingRequest, aNonvisibleAction);
|
|
|
|
break;
|
|
|
|
|
2019-04-26 02:03:04 +03:00
|
|
|
case Visibility::Untracked:
|
2016-03-26 00:49:43 +03:00
|
|
|
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) {
|
2022-08-03 21:23:43 +03:00
|
|
|
aFrame = GetOurPrimaryImageFrame();
|
2017-03-22 08:32:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 ||
|
2019-04-26 02:03:04 +03:00
|
|
|
aFrame->GetVisibility() == Visibility::ApproximatelyNonVisible) {
|
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,
|
2019-04-26 02:03:04 +03:00
|
|
|
aNonvisibleAction == Some(OnNonvisible::DiscardImages)
|
2016-10-13 10:04:15 +03:00
|
|
|
? ImageTracker::REQUEST_DISCARD
|
|
|
|
: 0);
|
2019-04-26 02:03:04 +03:00
|
|
|
} else if (aNonvisibleAction == Some(OnNonvisible::DiscardImages)) {
|
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,
|
2019-04-26 02:03:04 +03:00
|
|
|
aNonvisibleAction == Some(OnNonvisible::DiscardImages)
|
2016-10-13 10:04:15 +03:00
|
|
|
? ImageTracker::REQUEST_DISCARD
|
|
|
|
: 0);
|
2019-04-26 02:03:04 +03:00
|
|
|
} else if (aNonvisibleAction == Some(OnNonvisible::DiscardImages)) {
|
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
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
}
|
2020-02-13 00:29:32 +03:00
|
|
|
|
|
|
|
nsLoadFlags nsImageLoadingContent::LoadFlags() {
|
2020-03-11 00:39:40 +03:00
|
|
|
auto* image = HTMLImageElement::FromNode(AsContent());
|
|
|
|
if (image && image->OwnerDoc()->IsScriptEnabled() &&
|
2020-07-22 23:29:08 +03:00
|
|
|
!image->OwnerDoc()->IsStaticDocument() &&
|
2020-03-11 00:39:40 +03:00
|
|
|
image->LoadingState() == HTMLImageElement::Loading::Lazy) {
|
|
|
|
// Note that LOAD_BACKGROUND is not about priority of the load, but about
|
|
|
|
// whether it blocks the load event (by bypassing the loadgroup).
|
2020-02-13 00:29:32 +03:00
|
|
|
return nsIRequest::LOAD_BACKGROUND;
|
|
|
|
}
|
|
|
|
return nsIRequest::LOAD_NORMAL;
|
|
|
|
}
|