Files
palemoon27/dom/base/nsImageLoadingContent.cpp
T
roytam1 d457251529 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1120715 - Part 6: Remove the dom.requestcache.enabled pref; r=bkelly (800c996a96)
- Bug 1143222 - Put the DOM Cache tests in sequential mode again until we fix the rest of the intermittent failures; a=RyanVM (2ebdd659a1)
- Bug 1255636 - Give a better error message when the Request constructor fails because of a cross-origin referrer URL; r=bkelly (d81a21c0bb)
- Bug 1265056 - don't needlessly construct nsAutoCString temporaries in dom/; r=baku (3be49ca3fc)
- Bug 1243849 - Restore support for accessing the Cache API from app:// URLs and also for storing requests/responses with app:// URLs within it; r=bkelly (eb56fa564c)
- Bug 1263235, part 1 - Move PBrowser::AsyncMessage's data argument last. r=smaug (6852b87c22)
- Bug 1263235, part 2 - Make PContent::AsyncMessage and PContentBridge::AsyncMessage's data argument last. r=smaug (9e8cd94461)
- Bug 1263028 - send HTTP data to the content process in smaller chunks, r=michal (c0da548157)
- Bug 1263235, part 3 - Move PHttpChannel::OnTransportAndData's data argument last. r=mayhemer (e1bf4f430f)
- Bug 1263235, part 4 - Make PBrowserStream::Write's data argument last. r=jimm (8bcec4d541)
- Bug 1260876 - Remove process switch code for signed package code (added by Bug 1186290). r=valentin. (cef270b44c)
- Bug 1234575 - Empty fragment is ignored in URI of location header r=mcmanus (db68f102d8)
- Bug 1262506 - Unused variable in a runnable in BackgroundParentImpl, r=ehsan (9288f0a111)
- bug 1239166 - platform work to support Microsoft Family Safety functionality r=froydnj,mgoodwin,mhowell,rbarnes,vladan (adc357f3b3)
- Bug 842818 - Make Crypto::GetRandomValues() work off the main thread r=baku,keeler,mt (533f8942c4)
- Bug 1247089 - Add a mode to ReportToConsoleNonLocalized that ignores the calling location. r=bkelly (4be23e0869)
- Bug 1258883 - Add a way to replace the entire Push service in tests. r=wchen (06a5f27016)
- Bug 1243856 - Remove alarms from the Push H2 backend. r=dragana (60d146dc73)
- Bug 1246066 - Clear PushService timeout tasks on uninitialization. r=itcambridge (461276a972)
- Bug 1214338 - Implement Android GCM-based PushService protocol. r=rnewman r=kitcambridge (f2bb78994a)
- Bug 1257821 - Support the new aesgcm content encoding scheme. r=mt (1da653c14a)
- Bug 1243856 - Remove alarms from the Push WebSocket backend. r=dragana (43f74c4999)
- Bug 1258145 - Remove waitForPromise from the xpcshell tests. r=wchen (cdd1aff2f6)
- Bug 1253831 - Don't check actual intervals in the Push backoff test. r=wchen (859fa0bba3)
- Bug 1246341 - Include status codes in "ack" and "unregister" requests. r=dragana (a62d0daf9b)
- Bug 1246341 - Add a test for push event error reporting. r=dragana (013bc814e4)
- Bug 1247089 - Log Web Push decryption errors. r=bkelly (ffc093dc2f)
- Bug 1258221 - patch 2 - Port FileSystem API and DeviceStorage API to PBackground, r=smaug (c1c0e08bc0)
- Bug 1258221 - patch 3 - Rename FileSystemTaskBase to FileSystemTaskChildBase, r=smaug (867a0e65fd)
- Bug 1251032 - Don't return layersId or textureFactoryIdentifier as outparams in RenderFrameParent constructor. r=kanru (198ddff7fc)
- Bug 1251032 - Send RenderFrame info down to child in CreateWindow message. r=kanru (87e9001088)
- Bug 1251032 - Make it possible to assign a frameloader to RenderFrameParent after construction. r=kanru (96483d1282)
- Bug 1254865 - Send disableglobalhistory state down to TabChild after construction asynchronously. r=smaug (3949285b62)
- Bug 1238707 Release the window immediately in TabParent::Destroy() to avoid leaks. r=smaug (fc612485d7)
- Bug 1256589 part.1 Move the implementation of StopPropagation() from dom::Event to WidgetEvent r=smaug (181721b64c)
- Bug 1256589 part.2 Move the implementation of StopImmediatePropagation() from dom::Event to WidgetEvent r=smaug (554a0dc5b5)
- Bug 1203059 part.1 nsXBLWincowKeyHandler mark WidgetEvent::mFlags if it's reserved by chrome before the event is dispatched into the content r=smaug (9162dd68cb)
- Bug 1203059 part.2 When an event is reserved by chrome, it should be fired only on chrome r=smaug (35f082ca5f)
- Bug 1203059 part.3 Installing and removing keyboard event listeners of nsXBLWindowKeyHandler should be done by the class itself r=smaug (1e06c2d0bc)
- Bug 1203059 part.4 Update test_keycodes.xul for the new behavior r=smaug (3014d21c75)
- Bug 1256589 part.3 Move the implementation of StopCrossProcessForwarding() from dom::Event to WidgetEvent r=smaug (96db915b51)
- Bug 1257180 - patch 1 - Directory clonable to workers, r=smaug (5634acb08d)
- Bug 1257180 - patch 2 - Directory can be sent via postMessage(), r=smaug (82ada39ae3)
- Bug 1263311: Part 3 - s/nsCancelableRunnable/CancelableRunnable/g. r=froydnj (79d0a6c81f)
- Bug 1253198: add WebRtcIce prefix to all ICE unit tests. r=bwc (9c18f5fd56)
- Bug 1244926: added TCP socket filter to only allow outgoing STUN. r=jesup (75debfdc84)
- Bug 1257405 - Increase auth secret length to 16 octets. r=mt (b1e000c331)
- Bug 1257401 - Remove the worker descriptor for PushSubscription. r=khuey (5791fb69ef)
- Bug 1257401 - Remove the worker descriptor for PushManager. r=khuey (e2c75903a9)
- Bug 1257821 - Remove the authenticated aesgcm128 content coding scheme. r=mt,marco (64a2917910)
- Bug 1256488 - Add a Base64 URL-decoder for C++ and chrome JS callers. r=mt,baku (0bbb250298)
- Bug 1247685 - Validate and store app server keys in the Push service. r=mt (c3c026ccb1)
- Bug 1252650 - Support loading PushService immediately on Android; r=kitcambridge (d59a37fec4)
- Bug 1258595 - Shut down the Push service if errors occur at startup. r=wchen (eef1805652)
- Bug 1258595 - Wait for the Push service to shut down between tests. r=wchen (c30cf92ce6)
- Bug 1262618 - Fix an unchained promise and a couple of non-promise returns in the push service. r=wchen (8eadab5706)
- Bug 1263747 - Log error messages when stringifying errors. r=bgrins (edffd0074e)
- Bug 1265705 - Silence startup JavaScript strict warning in resource://gre/modules/PushService.jsm. r=kitcambridge (e7e210fb61)
- Bug 1264062 - Don't bother checking which accelerated layer types are available if they're all disabled by pref r=milan (0d3208ad59)
- Bug 1263346. Remove wrong Ivy Bridge device id. r=Bas (7e39e7f370)
- fix misspatch (a67a111b2c)
- Bug 1251334 - Create a disposable pref to force-disable e10s in an emergency. r=jimm (ef892d4474)
- Bug 1254774 - error: member access into incomplete type 'nsIUUIDGenerator' after bug 1237847. r=aklotz (c1f334609f)
- Bug 1257242 - Split the ::BrowserTabsRemoteAutostart() function into two parts, to allow for the blocking policies to be checked independently from the prefs checks. r=jimm (1babda578f)
- Bug 1260190 - Disable e10s for accessibility users on OS X. r=jimm (219e5b1f19)
- Bug 1237769 - Disable e10s on Windows XP if layers acceleration is requested r=milan (60f2434e9f)
- Bug 1232911 - [3.2] Block VPX support in ADM on unsupported devices. r=snorp (6924aa073a)
- Bug 1263249 - Bubble up unique failureId in GetFeatureStatus. r=mconley,milan (cd56eeab3c)
- Bug 1219296 - Split fields not needed for repaints out from FrameMetrics. r=kats (9003ca634a)
- Bug 1219296 - Factor out scroll snap information into a form that's usable by the compositor. r=kats (23d3e619a1)
- Bug 1219296 - Make ScrollMetadata::sNullMetadata a StaticAutoPtr so that ScrollMetadata can admit nsTArray members. r=kats (1729ff7d93)
- Bug 1257641 - Replace the mUpdateScrollOffset bool with an enum, needed in the next patch. r=botond (f9d546f8e8)
- Bug 1257641 - Use empty transactions to carry scroll offset updates to APZ that don't require a repaint. r=mattwoodrow,mstange,botond (ba4a8a8c29)
- Bug 1246290 - Add a bit to FrameMetrics to indicate if APZ-scrolling should be disabled on that APZC. r=botond (af2067137f)
- Bug 1256589 part.4 Move the implementation of PreventDefault() and add PreventDefaultBeforeDispatch() from dom::Event to WidgetEvent r=smaug (e7828f2d8f)
- Bug 1256589 part.5 Add DefaultPrevented() and DefaultPreventedByContent() to WidgetEvent r=smaug (e65cdd9127)
- Bug 1249915 - Fix missing MOZ_COUNT_CTOR and some misc cleanup. r=karlt (d2f26cf971)
- Bug 1154183 part.1 Move shortcut/access key candidate list creators from nsContentUtils to WidgetKeyboardEvent r=smaug (40b0b11a5a)
- Bug 1154183 part.2 eKeyDown event should have charCode value of following keypress event r=smaug (28c1443ba3)
- Bug 1154183 part.3 Clean up some variable names in nsXBLWindowKeyHandler::WalkHandlersAndExecute() r=smaug (81e25023d8)
- Bug 1154183 part.4 Implement nsXBLWindowKeyHandler::GetElementForHandler() r=smaug (b5605d5c83)
- Bug 1154183 part.5 Make nsXBLWindowKeyHandler::GetElementForHandler() use early return style r=smaug (017467204f)
- Bug 1154183 part.6 Add nsXBLWindowKeyHandler::IsExecuteableElement() r=smaug (1fda349113)
- Bug 1154183 part.7 Don't dispatch preceding keydown events of reserved keypress events on content in the default event group r=smaug (15b9e8c9d2)
- Bug 1256589 part.6 Move the implementation of IsTrusted() from dom::Event to WidgetEvent r=smaug (ec79520fd3)
- Bug 1253044. Fix fall through of observer topics when other conditions aren't met in PresShell::Observe. r=dholbert (dcc36884aa)
- Bug 1157546 - Replace the image visibility API with a more general API that tracks visibility for any kind of frame. r=mstange (d6ea061614)
- Bug 1219296 - Factor out the algorithm that computes a scroll snap destination into a reusable form. r=kats (296cbe9e49)
- Bug 1254275 - Inspect the event queue to find out whether momentum events are following. r=kats (b2bb8a26b9)
- Bug 1219296 - Scroll snap directly in APZ instead of going through the main thread. r=kats (0a30b550f9)
- Bug 1219296 - Fix an include-what-you-use error. r=kats (4a128ae98e)
- Bug 1260588 - C++ APZ should only allow handoff to ancestor APZC r=botond (9856ab5160)
- Bug 1257269 - Panning up in a scrollable element should not hide the toolbar r=kats,jchen (1036ffc9e3)
- Bug 1219296 - Ship scroll snap information to the compositor. r=kats (0e920f02a1)
- Bug 1219296 - Move the layout.css.scroll-snap.proximity-threshold pref to gfxPrefs, so it can be queried on the compositor thread. r=kats (2e3e1ec16e)
- Bug 1219296 - Remove StartSmoothScroll()'s argument, which is no longer used. r=kats (12efcd9c79)
- Bug 1219296 - Light refactoring to how a smooth scroll is launched inside APZC. r=kats (ba6a9ed9a2)
- Bug 1219296 - Followup to fix stale code comments. r=me and DONTBUILD (fec5f65988)
- Bug 1257641 - Remove now-unused code for the lightweight scroll offset update message. r=botond (d449e45d6d)
- Bug 1236680 Part 1: Add new WinUtils function to Resolve moved Users folder. r=jimm (51d12f856c)
- Bug 1236680 Part 2: Resolve GMP path for moved Users folder. r=cpearce (e568217b78)
- Bug 1236680 Part 3: Add #ifs to include to fix bustage. r=me (40c38680ea)
- Bug 1240315: Add startup crash report annotation for AppInit_DLLs; r=jimm (aa2040baae)
- Bug 1253446 - patch 2 - Return the proper scaling factor when querying the primary screen on Windows. r=emk (9765e4f7ca)
- Bug 1251624 - patch 1 - The desktop to device scaling in WinUtils::MonitorFromRect should not depend on custom CSS pixel scaling (devPixelsPerPx setting). r=emk (788b4ad5db)
- Bug 1251624 - patch 2 - Check for scaling override (devPixelsPerPx setting) in nsScreenWin::GetDefaultCSSScaleFactor, for proper window positioning when a custom scale factor is used. r=emk (2843a3fe70)
- Bug 1222149 - delete unused fields from AsyncEncodeAndWriteIcon; r=roc clang-cl says these are unused, so let's delete them. (62cf7f8f47)
- Bug 1204809 - Notify (don't hang) third party windows when adding shortcut icon. r=jmathies (7e4058a0f8)
- Bug 1253566 - Deal with char16_t/wchar_t mismatch. r=aklotz (1c6cf160c7)
- Bug 1211941 - Let nsICacheStorage.openTruncate impl return an HTTP cache entry write handle, r=michal (6a5796fb93)
- Bug 1050613 - Make sure force-valid for HTTP cache entries is removed when entries are removed, r=michal (9efb91eefc)
- Bug 1248389 - Cache index causing CPU loops, r=honzab (175b5b27f2)
- Bug 1066970 - Show 'calculating' during HTTP cache cleaning process in preferences window, r=michal (db722000d8)
- Bug 1248958 - CacheIndex mRWBuf ownership too fragile, read-after-free, r=honzab (66ee3d1d0d)
- Bug 1248003 - Purge from HTTP cache memory pool only in reasonable intervals, r=michal (1cd6cb5983)
- Bug 1068674 - Don't turn off e10s if hardware acceleration is disabled. r=jimm (dfbef44278)
2024-04-18 11:14:32 +08:00

1557 lines
45 KiB
C++

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* 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/. */
/*
* 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).
*/
#include "nsImageLoadingContent.h"
#include "nsAutoPtr.h"
#include "nsError.h"
#include "nsIContent.h"
#include "nsIDocument.h"
#include "nsIScriptGlobalObject.h"
#include "nsIDOMWindow.h"
#include "nsServiceManagerUtils.h"
#include "nsContentPolicyUtils.h"
#include "nsIURI.h"
#include "nsILoadGroup.h"
#include "imgIContainer.h"
#include "imgLoader.h"
#include "imgRequestProxy.h"
#include "nsThreadUtils.h"
#include "nsNetUtil.h"
#include "nsImageFrame.h"
#include "nsIPresShell.h"
#include "nsIChannel.h"
#include "nsIStreamListener.h"
#include "nsIFrame.h"
#include "nsIDOMNode.h"
#include "nsContentUtils.h"
#include "nsLayoutUtils.h"
#include "nsIContentPolicy.h"
#include "nsSVGEffects.h"
#include "gfxPrefs.h"
#include "mozAutoDocUpdate.h"
#include "mozilla/AsyncEventDispatcher.h"
#include "mozilla/EventStates.h"
#include "mozilla/dom/Element.h"
#include "mozilla/dom/ScriptSettings.h"
#include "mozilla/Preferences.h"
#ifdef LoadImage
// Undefine LoadImage to prevent naming conflict with Windows.
#undef LoadImage
#endif
using namespace mozilla;
#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;
}
nsAutoCString spec;
uri->GetSpec(spec);
printf("spec='%s'\n", spec.get());
}
#endif /* DEBUG_chb */
nsImageLoadingContent::nsImageLoadingContent()
: mCurrentRequestFlags(0),
mPendingRequestFlags(0),
mObserverList(nullptr),
mImageBlockingStatus(nsIContentPolicy::ACCEPT),
mLoadingEnabled(true),
mIsImageStateForced(false),
mLoading(false),
// mBroken starts out true, since an image without a URI is broken....
mBroken(true),
mUserDisabled(false),
mSuppressed(false),
mNewRequestsWillNeedAnimationReset(false),
mStateChangerDepth(0),
mCurrentRequestRegistered(false),
mPendingRequestRegistered(false),
mFrameCreateCalled(false)
{
if (!nsContentUtils::GetImgLoaderForChannel(nullptr, nullptr)) {
mLoadingEnabled = false;
}
bool isInconsistent;
mMostRecentRequestChange = TimeStamp::ProcessCreation(isInconsistent);
}
void
nsImageLoadingContent::DestroyImageLoadingContent()
{
// Cancel our requests so they won't hold stale refs to us
// NB: Don't ask to discard the images here.
ClearCurrentRequest(NS_BINDING_ABORTED);
ClearPendingRequest(NS_BINDING_ABORTED);
}
nsImageLoadingContent::~nsImageLoadingContent()
{
NS_ASSERTION(!mCurrentRequest && !mPendingRequest,
"DestroyImageLoadingContent not called");
NS_ASSERTION(!mObserverList.mObserver && !mObserverList.mNext,
"Observers still registered?");
}
/*
* imgINotificationObserver impl
*/
NS_IMETHODIMP
nsImageLoadingContent::Notify(imgIRequest* aRequest,
int32_t aType,
const nsIntRect* aData)
{
if (aType == imgINotificationObserver::IS_ANIMATED) {
return OnImageIsAnimated(aRequest);
}
if (aType == imgINotificationObserver::UNLOCKED_DRAW) {
OnUnlockedDraw();
return NS_OK;
}
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
// We should definitely have a request here
MOZ_ASSERT(aRequest, "no request?");
NS_PRECONDITION(aRequest == mCurrentRequest || aRequest == mPendingRequest,
"Unknown request");
}
{
// Calling Notify on observers can modify the list of observers so make
// a local copy.
AutoTArray<nsCOMPtr<imgINotificationObserver>, 2> observers;
for (ImageObserver* observer = &mObserverList, *next; observer;
observer = next) {
next = observer->mNext;
if (observer->mObserver) {
observers.AppendElement(observer->mObserver);
}
}
nsAutoScriptBlocker scriptBlocker;
for (auto& observer : observers) {
observer->Notify(aRequest, aType, aData);
}
}
if (aType == imgINotificationObserver::SIZE_AVAILABLE) {
// Have to check for state changes here, since we might have been in
// the LOADING state before.
UpdateImageState(true);
}
if (aType == imgINotificationObserver::LOAD_COMPLETE) {
uint32_t reqStatus;
aRequest->GetImageStatus(&reqStatus);
/* triage STATUS_ERROR */
if (reqStatus & imgIRequest::STATUS_ERROR) {
nsresult errorCode = NS_OK;
aRequest->GetImageErrorCode(&errorCode);
/* Handle image not loading error because source was a tracking URL.
* We make a note of this image node by including it in a dedicated
* array of blocked tracking nodes under its parent document.
*/
if (errorCode == NS_ERROR_TRACKING_URI) {
nsCOMPtr<nsIContent> thisNode
= do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
nsIDocument *doc = GetOurOwnerDoc();
doc->AddBlockedTrackingNode(thisNode);
}
}
nsresult status =
reqStatus & imgIRequest::STATUS_ERROR ? NS_ERROR_FAILURE : NS_OK;
return OnLoadComplete(aRequest, status);
}
if (aType == imgINotificationObserver::DECODE_COMPLETE) {
nsCOMPtr<imgIContainer> container;
aRequest->GetImage(getter_AddRefs(container));
if (container) {
container->PropagateUseCounters(GetOurOwnerDoc());
}
UpdateImageState(true);
}
return NS_OK;
}
nsresult
nsImageLoadingContent::OnLoadComplete(imgIRequest* aRequest, nsresult aStatus)
{
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;
// Our state may change. Watch it.
AutoStateChanger changer(this, true);
// If the pending request is loaded, switch to it.
if (aRequest == mPendingRequest) {
MakePendingRequestCurrent();
}
MOZ_ASSERT(aRequest == mCurrentRequest,
"One way or another, we should be current by now");
// Fire the appropriate DOM event.
if (NS_SUCCEEDED(aStatus)) {
FireEvent(NS_LITERAL_STRING("load"));
} else {
FireEvent(NS_LITERAL_STRING("error"));
}
nsCOMPtr<nsINode> thisNode = do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
nsSVGEffects::InvalidateDirectRenderingObservers(thisNode->AsElement());
return NS_OK;
}
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;
}
void
nsImageLoadingContent::OnUnlockedDraw()
{
// 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.
if (!ImageIsAnimated(mCurrentRequest) && !ImageIsAnimated(mPendingRequest)) {
return;
}
nsIFrame* frame = GetOurPrimaryFrame();
if (!frame) {
return;
}
if (frame->GetVisibility() == Visibility::APPROXIMATELY_VISIBLE) {
// This frame is already marked visible; there's nothing to do.
return;
}
nsPresContext* presContext = frame->PresContext();
if (!presContext) {
return;
}
nsIPresShell* presShell = presContext->PresShell();
if (!presShell) {
return;
}
presShell->EnsureFrameInApproximatelyVisibleList(frame);
}
nsresult
nsImageLoadingContent::OnImageIsAnimated(imgIRequest *aRequest)
{
bool* requestFlag = GetRegisteredFlagForRequest(aRequest);
if (requestFlag) {
nsLayoutUtils::RegisterImageRequest(GetFramePresContext(),
aRequest, requestFlag);
}
return NS_OK;
}
/*
* nsIImageLoadingContent impl
*/
NS_IMETHODIMP
nsImageLoadingContent::GetLoadingEnabled(bool *aLoadingEnabled)
{
*aLoadingEnabled = mLoadingEnabled;
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::SetLoadingEnabled(bool aLoadingEnabled)
{
if (nsContentUtils::GetImgLoaderForChannel(nullptr, nullptr)) {
mLoadingEnabled = aLoadingEnabled;
}
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::GetImageBlockingStatus(int16_t* aStatus)
{
NS_PRECONDITION(aStatus, "Null out param");
*aStatus = ImageBlockingStatus();
return NS_OK;
}
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);
}
}
NS_IMETHODIMP
nsImageLoadingContent::AddObserver(imgINotificationObserver* aObserver)
{
NS_ENSURE_ARG_POINTER(aObserver);
if (!mObserverList.mObserver) {
// Don't touch the linking of the list!
mObserverList.mObserver = aObserver;
ReplayImageStatus(mCurrentRequest, aObserver);
ReplayImageStatus(mPendingRequest, aObserver);
return NS_OK;
}
// otherwise we have to create a new entry
ImageObserver* observer = &mObserverList;
while (observer->mNext) {
observer = observer->mNext;
}
observer->mNext = new ImageObserver(aObserver);
ReplayImageStatus(mCurrentRequest, aObserver);
ReplayImageStatus(mPendingRequest, aObserver);
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::RemoveObserver(imgINotificationObserver* aObserver)
{
NS_ENSURE_ARG_POINTER(aObserver);
if (mObserverList.mObserver == aObserver) {
mObserverList.mObserver = nullptr;
// Don't touch the linking of the list!
return NS_OK;
}
// 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;
oldObserver->mNext = nullptr; // so we don't destroy them all
delete oldObserver;
}
#ifdef DEBUG
else {
NS_WARNING("Asked to remove nonexistent observer");
}
#endif
return NS_OK;
}
already_AddRefed<imgIRequest>
nsImageLoadingContent::GetRequest(int32_t aRequestType,
ErrorResult& aError)
{
nsCOMPtr<imgIRequest> request;
switch(aRequestType) {
case CURRENT_REQUEST:
request = mCurrentRequest;
break;
case PENDING_REQUEST:
request = mPendingRequest;
break;
default:
NS_ERROR("Unknown request type");
aError.Throw(NS_ERROR_UNEXPECTED);
}
return request.forget();
}
NS_IMETHODIMP
nsImageLoadingContent::GetRequest(int32_t aRequestType,
imgIRequest** aRequest)
{
NS_ENSURE_ARG_POINTER(aRequest);
ErrorResult result;
*aRequest = GetRequest(aRequestType, result).take();
return result.StealNSResult();
}
NS_IMETHODIMP_(bool)
nsImageLoadingContent::CurrentRequestHasSize()
{
return HaveSize(mCurrentRequest);
}
NS_IMETHODIMP_(void)
nsImageLoadingContent::FrameCreated(nsIFrame* aFrame)
{
NS_ASSERTION(aFrame, "aFrame is null");
mFrameCreateCalled = true;
TrackImage(mCurrentRequest);
TrackImage(mPendingRequest);
// We need to make sure that our image request is registered, if it should
// be registered.
nsPresContext* presContext = aFrame->PresContext();
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");
mFrameCreateCalled = false;
// We need to make sure that our image request is deregistered.
nsPresContext* presContext = GetFramePresContext();
if (mCurrentRequest) {
nsLayoutUtils::DeregisterImageRequest(presContext,
mCurrentRequest,
&mCurrentRequestRegistered);
}
if (mPendingRequest) {
nsLayoutUtils::DeregisterImageRequest(presContext,
mPendingRequest,
&mPendingRequestRegistered);
}
UntrackImage(mCurrentRequest);
UntrackImage(mPendingRequest);
nsIPresShell* presShell = presContext ? presContext->GetPresShell() : nullptr;
if (presShell) {
presShell->RemoveFrameFromApproximatelyVisibleList(aFrame);
}
}
/* static */
nsContentPolicyType
nsImageLoadingContent::PolicyTypeForLoad(ImageLoadType aImageLoadType)
{
if (aImageLoadType == eImageLoadType_Imageset) {
return nsIContentPolicy::TYPE_IMAGESET;
}
MOZ_ASSERT(aImageLoadType == eImageLoadType_Normal,
"Unknown ImageLoadType type in PolicyTypeForLoad");
return nsIContentPolicy::TYPE_INTERNAL_IMAGE;
}
int32_t
nsImageLoadingContent::GetRequestType(imgIRequest* aRequest,
ErrorResult& aError)
{
if (aRequest == mCurrentRequest) {
return CURRENT_REQUEST;
}
if (aRequest == mPendingRequest) {
return PENDING_REQUEST;
}
NS_ERROR("Unknown request");
aError.Throw(NS_ERROR_UNEXPECTED);
return UNKNOWN_REQUEST;
}
NS_IMETHODIMP
nsImageLoadingContent::GetRequestType(imgIRequest* aRequest,
int32_t* aRequestType)
{
NS_PRECONDITION(aRequestType, "Null out param");
ErrorResult result;
*aRequestType = GetRequestType(aRequest, result);
return result.StealNSResult();
}
already_AddRefed<nsIURI>
nsImageLoadingContent::GetCurrentURI(ErrorResult& aError)
{
nsCOMPtr<nsIURI> uri;
if (mCurrentRequest) {
mCurrentRequest->GetURI(getter_AddRefs(uri));
} else if (mCurrentURI) {
nsresult rv = NS_EnsureSafeToReturn(mCurrentURI, getter_AddRefs(uri));
if (NS_FAILED(rv)) {
aError.Throw(rv);
}
}
return uri.forget();
}
NS_IMETHODIMP
nsImageLoadingContent::GetCurrentURI(nsIURI** aURI)
{
NS_ENSURE_ARG_POINTER(aURI);
ErrorResult result;
*aURI = GetCurrentURI(result).take();
return result.StealNSResult();
}
already_AddRefed<nsIStreamListener>
nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel,
ErrorResult& aError)
{
imgLoader* loader =
nsContentUtils::GetImgLoaderForChannel(aChannel, GetOurOwnerDoc());
if (!loader) {
aError.Throw(NS_ERROR_NULL_POINTER);
return nullptr;
}
nsCOMPtr<nsIDocument> doc = GetOurOwnerDoc();
if (!doc) {
// Don't bother
return nullptr;
}
// 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?
// Our state might change. Watch it.
AutoStateChanger changer(this, true);
// Do the load.
nsCOMPtr<nsIStreamListener> listener;
RefPtr<imgRequestProxy>& req = PrepareNextRequest(eImageLoadType_Normal);
nsresult rv = loader->
LoadImageWithChannel(aChannel, this, doc,
getter_AddRefs(listener),
getter_AddRefs(req));
if (NS_SUCCEEDED(rv)) {
TrackImage(req);
ResetAnimationIfNeeded();
} else {
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));
FireEvent(NS_LITERAL_STRING("error"));
aError.Throw(rv);
}
return listener.forget();
}
NS_IMETHODIMP
nsImageLoadingContent::LoadImageWithChannel(nsIChannel* aChannel,
nsIStreamListener** aListener)
{
NS_ENSURE_ARG_POINTER(aListener);
ErrorResult result;
*aListener = LoadImageWithChannel(aChannel, result).take();
return result.StealNSResult();
}
void
nsImageLoadingContent::ForceReload(const mozilla::dom::Optional<bool>& aNotify,
mozilla::ErrorResult& aError)
{
nsCOMPtr<nsIURI> currentURI;
GetCurrentURI(getter_AddRefs(currentURI));
if (!currentURI) {
aError.Throw(NS_ERROR_NOT_AVAILABLE);
return;
}
// defaults to true
bool notify = !aNotify.WasPassed() || aNotify.Value();
// 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;
nsresult rv = LoadImage(currentURI, true, notify, loadType, nullptr,
nsIRequest::VALIDATE_ALWAYS);
if (NS_FAILED(rv)) {
aError.Throw(rv);
}
}
NS_IMETHODIMP
nsImageLoadingContent::ForceReload(bool aNotify /* = true */,
uint8_t aArgc)
{
mozilla::dom::Optional<bool> notify;
if (aArgc >= 1) {
notify.Construct() = aNotify;
}
ErrorResult result;
ForceReload(notify, result);
return result.StealNSResult();
}
NS_IMETHODIMP
nsImageLoadingContent::BlockOnload(imgIRequest* aRequest)
{
if (aRequest == mCurrentRequest) {
NS_ASSERTION(!(mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD),
"Double BlockOnload!?");
mCurrentRequestFlags |= REQUEST_BLOCKS_ONLOAD;
} else if (aRequest == mPendingRequest) {
NS_ASSERTION(!(mPendingRequestFlags & REQUEST_BLOCKS_ONLOAD),
"Double BlockOnload!?");
mPendingRequestFlags |= REQUEST_BLOCKS_ONLOAD;
} else {
return NS_OK;
}
nsIDocument* doc = GetOurCurrentDoc();
if (doc) {
doc->BlockOnload();
}
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::UnblockOnload(imgIRequest* aRequest)
{
if (aRequest == mCurrentRequest) {
NS_ASSERTION(mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD,
"Double UnblockOnload!?");
mCurrentRequestFlags &= ~REQUEST_BLOCKS_ONLOAD;
} else if (aRequest == mPendingRequest) {
NS_ASSERTION(mPendingRequestFlags & REQUEST_BLOCKS_ONLOAD,
"Double UnblockOnload!?");
mPendingRequestFlags &= ~REQUEST_BLOCKS_ONLOAD;
} else {
return NS_OK;
}
nsIDocument* doc = GetOurCurrentDoc();
if (doc) {
doc->UnblockOnload(false);
}
return NS_OK;
}
/*
* Non-interface methods
*/
nsresult
nsImageLoadingContent::LoadImage(const nsAString& aNewURI,
bool aForce,
bool aNotify,
ImageLoadType aImageLoadType)
{
// First, get a document (needed for security checks and the like)
nsIDocument* doc = GetOurOwnerDoc();
if (!doc) {
// No reason to bother, I think...
return NS_OK;
}
nsCOMPtr<nsIURI> imageURI;
nsresult rv = StringToURI(aNewURI, doc, getter_AddRefs(imageURI));
NS_ENSURE_SUCCESS(rv, rv);
// XXXbiesi fire onerror if that failed?
bool equal;
if (aNewURI.IsEmpty() &&
doc->GetDocumentURI() &&
NS_SUCCEEDED(doc->GetDocumentURI()->EqualsExceptRef(imageURI, &equal)) &&
equal) {
// Loading an embedded img from the same URI as the document URI will not work
// as a resource cannot recursively embed itself. Attempting to do so generally
// results in having to pre-emptively close down an in-flight HTTP transaction
// and then incurring the significant cost of establishing a new TCP channel.
// This is generally triggered from <img src="">
// In light of that, just skip loading it..
// Do make sure to drop our existing image, if any
CancelImageRequests(aNotify);
return NS_OK;
}
NS_TryToSetImmutable(imageURI);
return LoadImage(imageURI, aForce, aNotify, aImageLoadType, doc);
}
nsresult
nsImageLoadingContent::LoadImage(nsIURI* aNewURI,
bool aForce,
bool aNotify,
ImageLoadType aImageLoadType,
nsIDocument* aDocument,
nsLoadFlags aLoadFlags)
{
if (!mLoadingEnabled) {
// XXX Why fire an error here? seems like the callers to SetLoadingEnabled
// don't want/need it.
FireEvent(NS_LITERAL_STRING("error"));
return NS_OK;
}
NS_ASSERTION(!aDocument || aDocument == GetOurOwnerDoc(),
"Bogus document passed in");
// First, get a document (needed for security checks and the like)
if (!aDocument) {
aDocument = GetOurOwnerDoc();
if (!aDocument) {
// No reason to bother, I think...
return NS_OK;
}
}
// 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.
if (!aForce && NS_CP_ACCEPTED(mImageBlockingStatus)) {
nsCOMPtr<nsIURI> currentURI;
GetCurrentURI(getter_AddRefs(currentURI));
bool equal;
if (currentURI &&
NS_SUCCEEDED(currentURI->Equals(aNewURI, &equal)) &&
equal) {
// Nothing to do here.
return NS_OK;
}
}
// From this point on, our image state could change. Watch it.
AutoStateChanger changer(this, aNotify);
// 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.
#ifdef DEBUG
nsIContent* thisContent = AsContent();
MOZ_ASSERT(thisContent &&
thisContent->NodePrincipal() == aDocument->NodePrincipal(),
"Principal mismatch?");
#endif
// Are we blocked?
int16_t cpDecision = nsIContentPolicy::REJECT_REQUEST;
nsContentPolicyType policyType = PolicyTypeForLoad(aImageLoadType);
nsContentUtils::CanLoadImage(aNewURI,
static_cast<nsIImageLoadingContent*>(this),
aDocument,
aDocument->NodePrincipal(),
&cpDecision,
policyType);
if (!NS_CP_ACCEPTED(cpDecision)) {
FireEvent(NS_LITERAL_STRING("error"));
SetBlockedRequest(aNewURI, cpDecision);
return NS_OK;
}
nsLoadFlags loadFlags = aLoadFlags;
int32_t corsmode = GetCORSMode();
if (corsmode == CORS_ANONYMOUS) {
loadFlags |= imgILoader::LOAD_CORS_ANONYMOUS;
} else if (corsmode == CORS_USE_CREDENTIALS) {
loadFlags |= imgILoader::LOAD_CORS_USE_CREDENTIALS;
}
// get document wide referrer policy
// if referrer attributes are enabled in preferences, load img referrer 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;
}
// Not blocked. Do the load.
RefPtr<imgRequestProxy>& req = PrepareNextRequest(aImageLoadType);
nsIContent* content = AsContent();
nsresult rv = nsContentUtils::LoadImage(aNewURI, aDocument,
aDocument->NodePrincipal(),
aDocument->GetDocumentURI(),
referrerPolicy,
this, loadFlags,
content->LocalName(),
getter_AddRefs(req),
policyType);
// 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);
if (NS_SUCCEEDED(rv)) {
TrackImage(req);
ResetAnimationIfNeeded();
// 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) {
uint32_t pendingLoadStatus;
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);
}
}
}
} else {
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)
mCurrentURI = aNewURI;
FireEvent(NS_LITERAL_STRING("error"));
return NS_OK;
}
return NS_OK;
}
nsresult
nsImageLoadingContent::ForceImageState(bool aForce,
EventStates::InternalType aState)
{
mIsImageStateForced = aForce;
mForcedImageState = EventStates(aState);
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::GetNaturalWidth(uint32_t* aNaturalWidth)
{
NS_ENSURE_ARG_POINTER(aNaturalWidth);
nsCOMPtr<imgIContainer> image;
if (mCurrentRequest) {
mCurrentRequest->GetImage(getter_AddRefs(image));
}
int32_t width;
if (image && NS_SUCCEEDED(image->GetWidth(&width))) {
*aNaturalWidth = width;
} else {
*aNaturalWidth = 0;
}
return NS_OK;
}
NS_IMETHODIMP
nsImageLoadingContent::GetNaturalHeight(uint32_t* aNaturalHeight)
{
NS_ENSURE_ARG_POINTER(aNaturalHeight);
nsCOMPtr<imgIContainer> image;
if (mCurrentRequest) {
mCurrentRequest->GetImage(getter_AddRefs(image));
}
int32_t height;
if (image && NS_SUCCEEDED(image->GetHeight(&height))) {
*aNaturalHeight = height;
} else {
*aNaturalHeight = 0;
}
return NS_OK;
}
EventStates
nsImageLoadingContent::ImageState() const
{
if (mIsImageStateForced) {
return mForcedImageState;
}
EventStates states;
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;
}
void
nsImageLoadingContent::UpdateImageState(bool aNotify)
{
if (mStateChangerDepth > 0) {
// 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
// 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.
return;
}
nsIContent* thisContent = AsContent();
mLoading = mBroken = mUserDisabled = mSuppressed = false;
// 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) {
mSuppressed = true;
} else if (mImageBlockingStatus == nsIContentPolicy::REJECT_TYPE) {
mUserDisabled = true;
} else if (!mCurrentRequest) {
// No current request means error, since we weren't disabled or suppressed
mBroken = true;
} else {
uint32_t currentLoadStatus;
nsresult rv = mCurrentRequest->GetImageStatus(&currentLoadStatus);
if (NS_FAILED(rv) || (currentLoadStatus & imgIRequest::STATUS_ERROR)) {
mBroken = true;
} else if (!(currentLoadStatus & imgIRequest::STATUS_SIZE_AVAILABLE)) {
mLoading = true;
}
}
NS_ASSERTION(thisContent->IsElement(), "Not an element?");
thisContent->AsElement()->UpdateState(aNotify);
}
void
nsImageLoadingContent::CancelImageRequests(bool aNotify)
{
AutoStateChanger changer(this, aNotify);
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
}
nsresult
nsImageLoadingContent::UseAsPrimaryRequest(imgRequestProxy* aRequest,
bool aNotify,
ImageLoadType aImageLoadType)
{
// Our state will change. Watch it.
AutoStateChanger changer(this, aNotify);
// Get rid if our existing images
ClearPendingRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
ClearCurrentRequest(NS_BINDING_ABORTED, Some(OnNonvisible::DISCARD_IMAGES));
// Clone the request we were given.
RefPtr<imgRequestProxy>& req = PrepareNextRequest(aImageLoadType);
nsresult rv = aRequest->Clone(this, getter_AddRefs(req));
if (NS_SUCCEEDED(rv)) {
TrackImage(req);
} else {
MOZ_ASSERT(!req, "Shouldn't have non-null request here");
return rv;
}
return NS_OK;
}
nsIDocument*
nsImageLoadingContent::GetOurOwnerDoc()
{
return AsContent()->OwnerDoc();
}
nsIDocument*
nsImageLoadingContent::GetOurCurrentDoc()
{
return AsContent()->GetComposedDoc();
}
nsIFrame*
nsImageLoadingContent::GetOurPrimaryFrame()
{
return AsContent()->GetPrimaryFrame();
}
nsPresContext* nsImageLoadingContent::GetFramePresContext()
{
nsIFrame* frame = GetOurPrimaryFrame();
if (!frame) {
return nullptr;
}
return frame->PresContext();
}
nsresult
nsImageLoadingContent::StringToURI(const nsAString& aSpec,
nsIDocument* aDocument,
nsIURI** aURI)
{
NS_PRECONDITION(aDocument, "Must have a document");
NS_PRECONDITION(aURI, "Null out param");
// (1) Get the base URI
nsIContent* thisContent = AsContent();
nsCOMPtr<nsIURI> baseURL = thisContent->GetBaseURI();
// (2) Get the charset
const nsAFlatCString &charset = aDocument->GetDocumentCharacterSet();
// (3) Construct the silly thing
return NS_NewURI(aURI,
aSpec,
charset.IsEmpty() ? nullptr : charset.get(),
baseURL,
nsContentUtils::GetIOService());
}
nsresult
nsImageLoadingContent::FireEvent(const nsAString& aEventType)
{
if (nsContentUtils::DocumentInactiveForImageLoads(GetOurOwnerDoc())) {
// Don't bother to fire any events, especially error events.
return NS_OK;
}
// 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.
nsCOMPtr<nsINode> thisNode = do_QueryInterface(static_cast<nsIImageLoadingContent*>(this));
RefPtr<AsyncEventDispatcher> loadBlockingAsyncDispatcher =
new LoadBlockingAsyncEventDispatcher(thisNode, aEventType, false, false);
loadBlockingAsyncDispatcher->PostDOMEvent();
return NS_OK;
}
RefPtr<imgRequestProxy>&
nsImageLoadingContent::PrepareNextRequest(ImageLoadType aImageLoadType)
{
nsImageFrame* frame = do_QueryFrame(GetOurPrimaryFrame());
if (frame) {
// 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.
frame->SetForceSyncDecoding(now - mMostRecentRequestChange < threshold);
mMostRecentRequestChange = now;
}
// If we don't have a usable current request, get rid of any half-baked
// request that might be sitting there and make this one current.
if (!HaveSize(mCurrentRequest))
return PrepareCurrentRequest(aImageLoadType);
// Otherwise, make it pending.
return PreparePendingRequest(aImageLoadType);
}
void
nsImageLoadingContent::SetBlockedRequest(nsIURI* aURI, int16_t aContentDecision)
{
// Sanity
MOZ_ASSERT(!NS_CP_ACCEPTED(aContentDecision), "Blocked but not?");
// We do some slightly illogical stuff here to maintain consistency with
// old behavior that people probably depend on. Even in the case where the
// new image is blocked, the old one should really be canceled with the
// reason "image source changed". However, apparently there's some abuse
// over in nsImageFrame where the displaying of the "broken" icon for the
// next image depends on the cancel reason of the previous image. ugh.
// XXX(seth): So shouldn't we fix nsImageFrame?!
ClearPendingRequest(NS_ERROR_IMAGE_BLOCKED,
Some(OnNonvisible::DISCARD_IMAGES));
// For the blocked case, we only want to cancel the existing current request
// if size is not available. bz says the web depends on this behavior.
if (!HaveSize(mCurrentRequest)) {
mImageBlockingStatus = aContentDecision;
uint32_t keepFlags = mCurrentRequestFlags & REQUEST_IS_IMAGESET;
ClearCurrentRequest(NS_ERROR_IMAGE_BLOCKED,
Some(OnNonvisible::DISCARD_IMAGES));
// We still want to remember what URI we were and if it was an imageset,
// despite not having an actual request. These are both cleared as part of
// ClearCurrentRequest() before a new request is started.
mCurrentURI = aURI;
mCurrentRequestFlags = keepFlags;
}
}
RefPtr<imgRequestProxy>&
nsImageLoadingContent::PrepareCurrentRequest(ImageLoadType aImageLoadType)
{
// 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.
ClearCurrentRequest(NS_ERROR_IMAGE_SRC_CHANGED,
Some(OnNonvisible::DISCARD_IMAGES));
if (mNewRequestsWillNeedAnimationReset) {
mCurrentRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET;
}
if (aImageLoadType == eImageLoadType_Imageset) {
mCurrentRequestFlags |= REQUEST_IS_IMAGESET;
}
// Return a reference.
return mCurrentRequest;
}
RefPtr<imgRequestProxy>&
nsImageLoadingContent::PreparePendingRequest(ImageLoadType aImageLoadType)
{
// Get rid of anything that was there previously.
ClearPendingRequest(NS_ERROR_IMAGE_SRC_CHANGED,
Some(OnNonvisible::DISCARD_IMAGES));
if (mNewRequestsWillNeedAnimationReset) {
mPendingRequestFlags |= REQUEST_NEEDS_ANIMATION_RESET;
}
if (aImageLoadType == eImageLoadType_Imageset) {
mPendingRequestFlags |= REQUEST_IS_IMAGESET;
}
// Return a reference.
return mPendingRequest;
}
namespace {
class ImageRequestAutoLock
{
public:
explicit ImageRequestAutoLock(imgIRequest* aRequest)
: mRequest(aRequest)
{
if (mRequest) {
mRequest->LockImage();
}
}
~ImageRequestAutoLock()
{
if (mRequest) {
mRequest->UnlockImage();
}
}
private:
nsCOMPtr<imgIRequest> mRequest;
};
} // namespace
void
nsImageLoadingContent::MakePendingRequestCurrent()
{
MOZ_ASSERT(mPendingRequest);
// 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);
ImageLoadType loadType = \
(mPendingRequestFlags & REQUEST_IS_IMAGESET) ? eImageLoadType_Imageset
: eImageLoadType_Normal;
PrepareCurrentRequest(loadType) = mPendingRequest;
mPendingRequest = nullptr;
mCurrentRequestFlags = mPendingRequestFlags;
mPendingRequestFlags = 0;
ResetAnimationIfNeeded();
}
void
nsImageLoadingContent::ClearCurrentRequest(nsresult aReason,
const Maybe<OnNonvisible>& aNonvisibleAction)
{
if (!mCurrentRequest) {
// Even if we didn't have a current request, we might have been keeping
// a URI and flags as a placeholder for a failed load. Clear that now.
mCurrentURI = nullptr;
mCurrentRequestFlags = 0;
return;
}
MOZ_ASSERT(!mCurrentURI,
"Shouldn't have both mCurrentRequest and mCurrentURI!");
// Deregister this image from the refresh driver so it no longer receives
// notifications.
nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mCurrentRequest,
&mCurrentRequestRegistered);
// Clean up the request.
UntrackImage(mCurrentRequest, aNonvisibleAction);
mCurrentRequest->CancelAndForgetObserver(aReason);
mCurrentRequest = nullptr;
mCurrentRequestFlags = 0;
}
void
nsImageLoadingContent::ClearPendingRequest(nsresult aReason,
const Maybe<OnNonvisible>& aNonvisibleAction)
{
if (!mPendingRequest)
return;
// Deregister this image from the refresh driver so it no longer receives
// notifications.
nsLayoutUtils::DeregisterImageRequest(GetFramePresContext(), mPendingRequest,
&mPendingRequestRegistered);
UntrackImage(mPendingRequest, aNonvisibleAction);
mPendingRequest->CancelAndForgetObserver(aReason);
mPendingRequest = nullptr;
mPendingRequestFlags = 0;
}
bool*
nsImageLoadingContent::GetRegisteredFlagForRequest(imgIRequest* aRequest)
{
if (aRequest == mCurrentRequest) {
return &mCurrentRequestRegistered;
} else if (aRequest == mPendingRequest) {
return &mPendingRequestRegistered;
} else {
return nullptr;
}
}
void
nsImageLoadingContent::ResetAnimationIfNeeded()
{
if (mCurrentRequest &&
(mCurrentRequestFlags & REQUEST_NEEDS_ANIMATION_RESET)) {
nsCOMPtr<imgIContainer> container;
mCurrentRequest->GetImage(getter_AddRefs(container));
if (container)
container->ResetAnimation();
mCurrentRequestFlags &= ~REQUEST_NEEDS_ANIMATION_RESET;
}
}
bool
nsImageLoadingContent::HaveSize(imgIRequest *aImage)
{
// Handle the null case
if (!aImage)
return false;
// Query the image
uint32_t status;
nsresult rv = aImage->GetImageStatus(&status);
return (NS_SUCCEEDED(rv) && (status & imgIRequest::STATUS_SIZE_AVAILABLE));
}
void
nsImageLoadingContent::BindToTree(nsIDocument* aDocument, nsIContent* aParent,
nsIContent* aBindingParent,
bool aCompileEventHandlers)
{
// We may be entering the document, so if our image should be tracked,
// track it.
if (!aDocument)
return;
TrackImage(mCurrentRequest);
TrackImage(mPendingRequest);
if (mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD)
aDocument->BlockOnload();
}
void
nsImageLoadingContent::UnbindFromTree(bool aDeep, bool aNullParent)
{
// We may be leaving the document, so if our image is tracked, untrack it.
nsCOMPtr<nsIDocument> doc = GetOurCurrentDoc();
if (!doc)
return;
UntrackImage(mCurrentRequest);
UntrackImage(mPendingRequest);
if (mCurrentRequestFlags & REQUEST_BLOCKS_ONLOAD)
doc->UnblockOnload(false);
}
void
nsImageLoadingContent::OnVisibilityChange(Visibility aNewVisibility,
const Maybe<OnNonvisible>& aNonvisibleAction)
{
switch (aNewVisibility) {
case Visibility::APPROXIMATELY_VISIBLE:
TrackImage(mCurrentRequest);
TrackImage(mPendingRequest);
break;
case Visibility::APPROXIMATELY_NONVISIBLE:
UntrackImage(mCurrentRequest, aNonvisibleAction);
UntrackImage(mPendingRequest, aNonvisibleAction);
break;
case Visibility::UNTRACKED:
MOZ_ASSERT_UNREACHABLE("Shouldn't notify for untracked visibility");
break;
}
}
void
nsImageLoadingContent::TrackImage(imgIRequest* aImage)
{
if (!aImage)
return;
MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest,
"Why haven't we heard of this request?");
nsIDocument* doc = GetOurCurrentDoc();
if (!doc) {
return;
}
// We only want to track this request if we're visible. Ordinarily we check
// the visible count, but that requires a frame; in cases where
// GetOurPrimaryFrame() cannot obtain a frame (e.g. <feImage>), we assume
// we're visible if FrameCreated() was called.
nsIFrame* frame = GetOurPrimaryFrame();
if ((frame && frame->GetVisibility() == Visibility::APPROXIMATELY_NONVISIBLE) ||
(!frame && !mFrameCreateCalled)) {
return;
}
if (aImage == mCurrentRequest && !(mCurrentRequestFlags & REQUEST_IS_TRACKED)) {
mCurrentRequestFlags |= REQUEST_IS_TRACKED;
doc->AddImage(mCurrentRequest);
}
if (aImage == mPendingRequest && !(mPendingRequestFlags & REQUEST_IS_TRACKED)) {
mPendingRequestFlags |= REQUEST_IS_TRACKED;
doc->AddImage(mPendingRequest);
}
}
void
nsImageLoadingContent::UntrackImage(imgIRequest* aImage,
const Maybe<OnNonvisible>& aNonvisibleAction
/* = Nothing() */)
{
if (!aImage)
return;
MOZ_ASSERT(aImage == mCurrentRequest || aImage == mPendingRequest,
"Why haven't we heard of this request?");
// 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.
nsIDocument* doc = GetOurCurrentDoc();
if (aImage == mCurrentRequest) {
if (doc && (mCurrentRequestFlags & REQUEST_IS_TRACKED)) {
mCurrentRequestFlags &= ~REQUEST_IS_TRACKED;
doc->RemoveImage(mCurrentRequest,
aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)
? nsIDocument::REQUEST_DISCARD
: 0);
} else if (aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)) {
// 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)) {
mPendingRequestFlags &= ~REQUEST_IS_TRACKED;
doc->RemoveImage(mPendingRequest,
aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)
? nsIDocument::REQUEST_DISCARD
: 0);
} else if (aNonvisibleAction == Some(OnNonvisible::DISCARD_IMAGES)) {
// If we're not in the document we may still need to be discarded.
aImage->RequestDiscard();
}
}
}
void
nsImageLoadingContent::CreateStaticImageClone(nsImageLoadingContent* aDest) const
{
aDest->mCurrentRequest = nsContentUtils::GetStaticRequest(mCurrentRequest);
aDest->TrackImage(aDest->mCurrentRequest);
aDest->mForcedImageState = mForcedImageState;
aDest->mImageBlockingStatus = mImageBlockingStatus;
aDest->mLoadingEnabled = mLoadingEnabled;
aDest->mStateChangerDepth = mStateChangerDepth;
aDest->mIsImageStateForced = mIsImageStateForced;
aDest->mLoading = mLoading;
aDest->mBroken = mBroken;
aDest->mUserDisabled = mUserDisabled;
aDest->mSuppressed = mSuppressed;
}
CORSMode
nsImageLoadingContent::GetCORSMode()
{
return CORS_NONE;
}
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);
}
// 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;
};