michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0: // vim: ft=cpp tw=78 sw=2 et ts=2
michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0: * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0:
michael@0: /*
michael@0: * A base class which implements nsIImageLoadingContent and can be
michael@0: * subclassed by various content nodes that want to provide image
michael@0: * loading functionality (eg , , etc).
michael@0: */
michael@0:
michael@0: #ifndef nsImageLoadingContent_h__
michael@0: #define nsImageLoadingContent_h__
michael@0:
michael@0: #include "imgINotificationObserver.h"
michael@0: #include "imgIOnloadBlocker.h"
michael@0: #include "mozilla/CORSMode.h"
michael@0: #include "mozilla/EventStates.h"
michael@0: #include "nsCOMPtr.h"
michael@0: #include "nsIImageLoadingContent.h"
michael@0: #include "nsIRequest.h"
michael@0: #include "mozilla/ErrorResult.h"
michael@0: #include "nsAutoPtr.h"
michael@0:
michael@0: class nsIURI;
michael@0: class nsIDocument;
michael@0: class imgILoader;
michael@0: class nsIIOService;
michael@0: class nsPresContext;
michael@0: class nsIContent;
michael@0: class imgRequestProxy;
michael@0:
michael@0: #ifdef LoadImage
michael@0: // Undefine LoadImage to prevent naming conflict with Windows.
michael@0: #undef LoadImage
michael@0: #endif
michael@0:
michael@0: class nsImageLoadingContent : public nsIImageLoadingContent,
michael@0: public imgIOnloadBlocker
michael@0: {
michael@0: /* METHODS */
michael@0: public:
michael@0: nsImageLoadingContent();
michael@0: virtual ~nsImageLoadingContent();
michael@0:
michael@0: NS_DECL_IMGINOTIFICATIONOBSERVER
michael@0: NS_DECL_NSIIMAGELOADINGCONTENT
michael@0: NS_DECL_IMGIONLOADBLOCKER
michael@0:
michael@0: // Web IDL binding methods.
michael@0: // Note that the XPCOM SetLoadingEnabled, AddObserver, RemoveObserver,
michael@0: // ForceImageState methods are OK for Web IDL bindings to use as well,
michael@0: // since none of them throw when called via the Web IDL bindings.
michael@0:
michael@0: bool LoadingEnabled() const { return mLoadingEnabled; }
michael@0: int16_t ImageBlockingStatus() const
michael@0: {
michael@0: return mImageBlockingStatus;
michael@0: }
michael@0: already_AddRefed
michael@0: GetRequest(int32_t aRequestType, mozilla::ErrorResult& aError);
michael@0: int32_t
michael@0: GetRequestType(imgIRequest* aRequest, mozilla::ErrorResult& aError);
michael@0: already_AddRefed GetCurrentURI(mozilla::ErrorResult& aError);
michael@0: already_AddRefed
michael@0: LoadImageWithChannel(nsIChannel* aChannel, mozilla::ErrorResult& aError);
michael@0: void ForceReload(mozilla::ErrorResult& aError);
michael@0:
michael@0:
michael@0:
michael@0: protected:
michael@0: /**
michael@0: * LoadImage is called by subclasses when the appropriate
michael@0: * attributes (eg 'src' for tags) change. The string passed
michael@0: * in is the new uri string; this consolidates the code for getting
michael@0: * the charset, constructing URI objects, and any other incidentals
michael@0: * into this superclass.
michael@0: *
michael@0: * @param aNewURI the URI spec to be loaded (may be a relative URI)
michael@0: * @param aForce If true, make sure to load the URI. If false, only
michael@0: * load if the URI is different from the currently loaded URI.
michael@0: * @param aNotify If true, nsIDocumentObserver state change notifications
michael@0: * will be sent as needed.
michael@0: */
michael@0: nsresult LoadImage(const nsAString& aNewURI, bool aForce,
michael@0: bool aNotify);
michael@0:
michael@0: /**
michael@0: * ImageState is called by subclasses that are computing their content state.
michael@0: * The return value will have the NS_EVENT_STATE_BROKEN,
michael@0: * NS_EVENT_STATE_USERDISABLED, and NS_EVENT_STATE_SUPPRESSED bits set as
michael@0: * needed. Note that this state assumes that this node is "trying" to be an
michael@0: * image (so for example complete lack of attempt to load an image will lead
michael@0: * to NS_EVENT_STATE_BROKEN being set). Subclasses that are not "trying" to
michael@0: * be an image (eg an HTML of type other than "image") should just
michael@0: * not call this method when computing their intrinsic state.
michael@0: */
michael@0: mozilla::EventStates ImageState() const;
michael@0:
michael@0: /**
michael@0: * LoadImage is called by subclasses when the appropriate
michael@0: * attributes (eg 'src' for tags) change. If callers have an
michael@0: * URI object already available, they should use this method.
michael@0: *
michael@0: * @param aNewURI the URI to be loaded
michael@0: * @param aForce If true, make sure to load the URI. If false, only
michael@0: * load if the URI is different from the currently loaded URI.
michael@0: * @param aNotify If true, nsIDocumentObserver state change notifications
michael@0: * will be sent as needed.
michael@0: * @param aDocument Optional parameter giving the document this node is in.
michael@0: * This is purely a performance optimization.
michael@0: * @param aLoadFlags Optional parameter specifying load flags to use for
michael@0: * the image load
michael@0: */
michael@0: nsresult LoadImage(nsIURI* aNewURI, bool aForce, bool aNotify,
michael@0: nsIDocument* aDocument = nullptr,
michael@0: nsLoadFlags aLoadFlags = nsIRequest::LOAD_NORMAL);
michael@0:
michael@0: /**
michael@0: * helpers to get the document for this content (from the nodeinfo
michael@0: * and such). Not named GetOwnerDoc/GetCurrentDoc to prevent ambiguous
michael@0: * method names in subclasses
michael@0: *
michael@0: * @return the document we belong to
michael@0: */
michael@0: nsIDocument* GetOurOwnerDoc();
michael@0: nsIDocument* GetOurCurrentDoc();
michael@0:
michael@0: /**
michael@0: * Helper function to get the frame associated with this content. Not named
michael@0: * GetPrimaryFrame to prevent ambiguous method names in subclasses.
michael@0: *
michael@0: * @return The frame which we belong to, or nullptr if it doesn't exist.
michael@0: */
michael@0: nsIFrame* GetOurPrimaryFrame();
michael@0:
michael@0: /**
michael@0: * Helper function to get the PresContext associated with this content's
michael@0: * frame. Not named GetPresContext to prevent ambiguous method names in
michael@0: * subclasses.
michael@0: *
michael@0: * @return The nsPresContext associated with our frame, or nullptr if either
michael@0: * the frame doesn't exist, or the frame's prescontext doesn't exist.
michael@0: */
michael@0: nsPresContext* GetFramePresContext();
michael@0:
michael@0: /**
michael@0: * CancelImageRequests is called by subclasses when they want to
michael@0: * cancel all image requests (for example when the subclass is
michael@0: * somehow not an image anymore).
michael@0: */
michael@0: void CancelImageRequests(bool aNotify);
michael@0:
michael@0: /**
michael@0: * UseAsPrimaryRequest is called by subclasses when they have an existing
michael@0: * imgRequestProxy that they want this nsImageLoadingContent to use. This may
michael@0: * effectively be called instead of LoadImage or LoadImageWithChannel.
michael@0: * If aNotify is true, this method will notify on state changes.
michael@0: */
michael@0: nsresult UseAsPrimaryRequest(imgRequestProxy* aRequest, bool aNotify);
michael@0:
michael@0: /**
michael@0: * Derived classes of nsImageLoadingContent MUST call
michael@0: * DestroyImageLoadingContent from their destructor, or earlier. It
michael@0: * does things that cannot be done in ~nsImageLoadingContent because
michael@0: * they rely on being able to QueryInterface to other derived classes,
michael@0: * which cannot happen once the derived class destructor has started
michael@0: * calling the base class destructors.
michael@0: */
michael@0: void DestroyImageLoadingContent();
michael@0:
michael@0: void ClearBrokenState() { mBroken = false; }
michael@0:
michael@0: // Sets blocking state only if the desired state is different from the
michael@0: // current one. See the comment for mBlockingOnload for more information.
michael@0: void SetBlockingOnload(bool aBlocking);
michael@0:
michael@0: /**
michael@0: * Returns the CORS mode that will be used for all future image loads. The
michael@0: * default implementation returns CORS_NONE unconditionally.
michael@0: */
michael@0: virtual mozilla::CORSMode GetCORSMode();
michael@0:
michael@0: // Subclasses are *required* to call BindToTree/UnbindFromTree.
michael@0: void BindToTree(nsIDocument* aDocument, nsIContent* aParent,
michael@0: nsIContent* aBindingParent, bool aCompileEventHandlers);
michael@0: void UnbindFromTree(bool aDeep, bool aNullParent);
michael@0:
michael@0: nsresult OnStopRequest(imgIRequest* aRequest, nsresult aStatus);
michael@0: void OnUnlockedDraw();
michael@0: nsresult OnImageIsAnimated(imgIRequest *aRequest);
michael@0:
michael@0: private:
michael@0: /**
michael@0: * Struct used to manage the image observers.
michael@0: */
michael@0: struct ImageObserver {
michael@0: ImageObserver(imgINotificationObserver* aObserver);
michael@0: ~ImageObserver();
michael@0:
michael@0: nsCOMPtr mObserver;
michael@0: ImageObserver* mNext;
michael@0: };
michael@0:
michael@0: /**
michael@0: * Struct to report state changes
michael@0: */
michael@0: struct AutoStateChanger {
michael@0: AutoStateChanger(nsImageLoadingContent* aImageContent,
michael@0: bool aNotify) :
michael@0: mImageContent(aImageContent),
michael@0: mNotify(aNotify)
michael@0: {
michael@0: mImageContent->mStateChangerDepth++;
michael@0: }
michael@0: ~AutoStateChanger()
michael@0: {
michael@0: mImageContent->mStateChangerDepth--;
michael@0: mImageContent->UpdateImageState(mNotify);
michael@0: }
michael@0:
michael@0: nsImageLoadingContent* mImageContent;
michael@0: bool mNotify;
michael@0: };
michael@0:
michael@0: friend struct AutoStateChanger;
michael@0:
michael@0: /**
michael@0: * UpdateImageState recomputes the current state of this image loading
michael@0: * content and updates what ImageState() returns accordingly. It will also
michael@0: * fire a ContentStatesChanged() notification as needed if aNotify is true.
michael@0: */
michael@0: void UpdateImageState(bool aNotify);
michael@0:
michael@0: /**
michael@0: * Method to fire an event once we know what's going on with the image load.
michael@0: *
michael@0: * @param aEventType "load" or "error" depending on how things went
michael@0: */
michael@0: nsresult FireEvent(const nsAString& aEventType);
michael@0:
michael@0: protected:
michael@0: /**
michael@0: * Method to create an nsIURI object from the given string (will
michael@0: * handle getting the right charset, base, etc). You MUST pass in a
michael@0: * non-null document to this function.
michael@0: *
michael@0: * @param aSpec the string spec (from an HTML attribute, eg)
michael@0: * @param aDocument the document we belong to
michael@0: * @return the URI we want to be loading
michael@0: */
michael@0: nsresult StringToURI(const nsAString& aSpec, nsIDocument* aDocument,
michael@0: nsIURI** aURI);
michael@0:
michael@0: void CreateStaticImageClone(nsImageLoadingContent* aDest) const;
michael@0:
michael@0: /**
michael@0: * Prepare and returns a reference to the "next request". If there's already
michael@0: * a _usable_ current request (one with SIZE_AVAILABLE), this request is
michael@0: * "pending" until it becomes usable. Otherwise, this becomes the current
michael@0: * request.
michael@0: */
michael@0: nsRefPtr& PrepareNextRequest();
michael@0:
michael@0: /**
michael@0: * Called when we would normally call PrepareNextRequest(), but the request was
michael@0: * blocked.
michael@0: */
michael@0: void SetBlockedRequest(nsIURI* aURI, int16_t aContentDecision);
michael@0:
michael@0: /**
michael@0: * Returns a COMPtr reference to the current/pending image requests, cleaning
michael@0: * up and canceling anything that was there before. Note that if you just want
michael@0: * to get rid of one of the requests, you should call
michael@0: * Clear*Request(NS_BINDING_ABORTED) instead, since it passes a more appropriate
michael@0: * aReason than Prepare*Request() does (NS_ERROR_IMAGE_SRC_CHANGED).
michael@0: */
michael@0: nsRefPtr& PrepareCurrentRequest();
michael@0: nsRefPtr& PreparePendingRequest();
michael@0:
michael@0: /**
michael@0: * Switch our pending request to be our current request.
michael@0: * mPendingRequest must be non-null!
michael@0: */
michael@0: void MakePendingRequestCurrent();
michael@0:
michael@0: /**
michael@0: * Cancels and nulls-out the "current" and "pending" requests if they exist.
michael@0: */
michael@0: void ClearCurrentRequest(nsresult aReason, uint32_t aFlags);
michael@0: void ClearPendingRequest(nsresult aReason, uint32_t aFlags);
michael@0:
michael@0: /**
michael@0: * Retrieve a pointer to the 'registered with the refresh driver' flag for
michael@0: * which a particular image request corresponds.
michael@0: *
michael@0: * @returns A pointer to the boolean flag for a given image request, or
michael@0: * |nullptr| if the request is not either |mPendingRequest| or
michael@0: * |mCurrentRequest|.
michael@0: */
michael@0: bool* GetRegisteredFlagForRequest(imgIRequest* aRequest);
michael@0:
michael@0: /**
michael@0: * Reset animation of the current request if |mNewRequestsWillNeedAnimationReset|
michael@0: * was true when the request was prepared.
michael@0: */
michael@0: void ResetAnimationIfNeeded();
michael@0:
michael@0: /**
michael@0: * Static helper method to tell us if we have the size of a request. The
michael@0: * image may be null.
michael@0: */
michael@0: static bool HaveSize(imgIRequest *aImage);
michael@0:
michael@0: /**
michael@0: * Adds/Removes a given imgIRequest from our document's tracker.
michael@0: *
michael@0: * No-op if aImage is null.
michael@0: *
michael@0: * REQUEST_DISCARD passed to UntrackImage means we request the discard of the
michael@0: * decoded data of the image.
michael@0: */
michael@0: void TrackImage(imgIRequest* aImage);
michael@0: enum {
michael@0: REQUEST_DISCARD = 0x1
michael@0: };
michael@0: void UntrackImage(imgIRequest* aImage, uint32_t aFlags = 0);
michael@0:
michael@0: /* MEMBERS */
michael@0: nsRefPtr mCurrentRequest;
michael@0: nsRefPtr mPendingRequest;
michael@0: uint32_t mCurrentRequestFlags;
michael@0: uint32_t mPendingRequestFlags;
michael@0:
michael@0: enum {
michael@0: // Set if the request needs ResetAnimation called on it.
michael@0: REQUEST_NEEDS_ANIMATION_RESET = 0x00000001U,
michael@0: // Set if the request is blocking onload.
michael@0: REQUEST_BLOCKS_ONLOAD = 0x00000002U,
michael@0: // Set if the request is currently tracked with the document.
michael@0: REQUEST_IS_TRACKED = 0x00000004U
michael@0: };
michael@0:
michael@0: // If the image was blocked or if there was an error loading, it's nice to
michael@0: // still keep track of what the URI was despite not having an imgIRequest.
michael@0: // We only maintain this in those situations (in the common case, this is
michael@0: // always null).
michael@0: nsCOMPtr mCurrentURI;
michael@0:
michael@0: private:
michael@0: /**
michael@0: * Typically we will have only one observer (our frame in the screen
michael@0: * prescontext), so we want to only make space for one and to
michael@0: * heap-allocate anything past that (saves memory and malloc churn
michael@0: * in the common case). The storage is a linked list, we just
michael@0: * happen to actually hold the first observer instead of a pointer
michael@0: * to it.
michael@0: */
michael@0: ImageObserver mObserverList;
michael@0:
michael@0: /**
michael@0: * When mIsImageStateForced is true, this holds the ImageState that we'll
michael@0: * return in ImageState().
michael@0: */
michael@0: mozilla::EventStates mForcedImageState;
michael@0:
michael@0: int16_t mImageBlockingStatus;
michael@0: bool mLoadingEnabled : 1;
michael@0:
michael@0: /**
michael@0: * When true, we return mForcedImageState from ImageState().
michael@0: */
michael@0: bool mIsImageStateForced : 1;
michael@0:
michael@0: /**
michael@0: * The state we had the last time we checked whether we needed to notify the
michael@0: * document of a state change. These are maintained by UpdateImageState.
michael@0: */
michael@0: bool mLoading : 1;
michael@0: bool mBroken : 1;
michael@0: bool mUserDisabled : 1;
michael@0: bool mSuppressed : 1;
michael@0: bool mFireEventsOnDecode : 1;
michael@0:
michael@0: protected:
michael@0: /**
michael@0: * A hack to get animations to reset, see bug 594771. On requests
michael@0: * that originate from setting .src, we mark them for needing their animation
michael@0: * reset when they are ready. mNewRequestsWillNeedAnimationReset is set to
michael@0: * true while preparing such requests (as a hack around needing to change an
michael@0: * interface), and the other two booleans store which of the current
michael@0: * and pending requests are of the sort that need their animation restarted.
michael@0: */
michael@0: bool mNewRequestsWillNeedAnimationReset : 1;
michael@0:
michael@0: private:
michael@0: /* The number of nested AutoStateChangers currently tracking our state. */
michael@0: uint8_t mStateChangerDepth;
michael@0:
michael@0: // Flags to indicate whether each of the current and pending requests are
michael@0: // registered with the refresh driver.
michael@0: bool mCurrentRequestRegistered;
michael@0: bool mPendingRequestRegistered;
michael@0:
michael@0: // True when FrameCreate has been called but FrameDestroy has not.
michael@0: bool mFrameCreateCalled;
michael@0:
michael@0: uint32_t mVisibleCount;
michael@0: };
michael@0:
michael@0: #endif // nsImageLoadingContent_h__