ImageDocument.h (4664B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 #ifndef mozilla_dom_ImageDocument_h 7 #define mozilla_dom_ImageDocument_h 8 9 #include "imgINotificationObserver.h" 10 #include "mozilla/Attributes.h" 11 #include "mozilla/dom/MediaDocument.h" 12 #include "nsIDOMEventListener.h" 13 14 namespace mozilla { 15 enum class StyleImageRendering : uint8_t; 16 struct IntrinsicSize; 17 } // namespace mozilla 18 19 namespace mozilla::dom { 20 class HTMLImageElement; 21 22 class ImageDocument final : public MediaDocument, 23 public imgINotificationObserver, 24 public nsIDOMEventListener { 25 public: 26 ImageDocument(); 27 28 NS_DECL_ISUPPORTS_INHERITED 29 30 enum MediaDocumentKind MediaDocumentKind() const override { 31 return MediaDocumentKind::Image; 32 } 33 34 nsresult Init(nsIPrincipal* aPrincipal, 35 nsIPrincipal* aPartitionedPrincipal) override; 36 37 nsresult StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, 38 nsILoadGroup* aLoadGroup, nsISupports* aContainer, 39 nsIStreamListener** aDocListener, 40 bool aReset = true) override; 41 42 void SetScriptGlobalObject(nsIScriptGlobalObject*) override; 43 void Destroy() override; 44 void OnPageShow(bool aPersisted, EventTarget* aDispatchStartTarget, 45 bool aOnlySystemGroup = false) override; 46 47 NS_DECL_IMGINOTIFICATIONOBSERVER 48 49 // nsIDOMEventListener 50 NS_DECL_NSIDOMEVENTLISTENER 51 52 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ImageDocument, MediaDocument) 53 54 friend class ImageListener; 55 56 void DefaultCheckOverflowing(); 57 58 // WebIDL API 59 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 60 61 bool ImageIsOverflowing() const { 62 return ImageIsOverflowingHorizontally() || ImageIsOverflowingVertically(); 63 } 64 65 bool ImageIsOverflowingVertically() const { 66 return mImageHeight > mVisibleHeight; 67 } 68 69 bool ImageIsOverflowingHorizontally() const { 70 return mImageWidth > mVisibleWidth; 71 } 72 73 bool ImageIsResized() const { return mImageIsResized; } 74 // ShrinkToFit is called from xpidl methods and we don't have a good 75 // way to mark those MOZ_CAN_RUN_SCRIPT yet. 76 MOZ_CAN_RUN_SCRIPT_BOUNDARY void ShrinkToFit(); 77 void RestoreImage(); 78 79 void NotifyPossibleTitleChange(bool aBoundTitleElement) override; 80 81 void UpdateRemoteStyle(StyleImageRendering aImageRendering); 82 83 protected: 84 virtual ~ImageDocument(); 85 86 nsresult CreateSyntheticDocument() override; 87 88 nsresult CheckOverflowing(bool changeState); 89 90 void UpdateTitleAndCharset(); 91 92 void ScrollImageTo(int32_t aX, int32_t aY); 93 94 float GetRatio() const { 95 return std::min(mVisibleWidth / mImageWidth, mVisibleHeight / mImageHeight); 96 } 97 98 bool IsSiteSpecific(); 99 100 void ResetZoomLevel(); 101 float GetZoomLevel(); 102 void CheckFullZoom(); 103 float GetResolution(); 104 105 void UpdateSizeFromLayout(); 106 107 enum eModeClasses { 108 eNone, 109 eShrinkToFit, 110 eOverflowingVertical, // And maybe horizontal too. 111 eOverflowingHorizontalOnly, 112 eIsInObjectOrEmbed 113 }; 114 void SetModeClass(eModeClasses mode); 115 116 void OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage); 117 void OnLoadComplete(imgIRequest* aRequest, nsresult aStatus); 118 void OnHasTransparency(); 119 120 void MaybeSendResultToEmbedder(nsresult aResult); 121 122 RefPtr<HTMLImageElement> mImageContent; 123 124 float mVisibleWidth; 125 float mVisibleHeight; 126 int32_t mImageWidth; 127 int32_t mImageHeight; 128 129 // mImageIsResized is true if the image is currently resized 130 bool mImageIsResized; 131 // mShouldResize is true if the image should be resized when it doesn't fit 132 // mImageIsResized cannot be true when this is false, but mImageIsResized 133 // can be false when this is true 134 bool mShouldResize; 135 bool mFirstResize; 136 // mObservingImageLoader is true while the observer is set. 137 bool mObservingImageLoader; 138 bool mTitleUpdateInProgress; 139 bool mHasCustomTitle; 140 141 // True iff embedder is either <object> or <embed>. 142 bool mIsInObjectOrEmbed; 143 144 float mOriginalZoomLevel; 145 float mOriginalResolution; 146 }; 147 148 inline ImageDocument* Document::AsImageDocument() { 149 MOZ_ASSERT(IsImageDocument()); 150 return static_cast<ImageDocument*>(this); 151 } 152 153 inline const ImageDocument* Document::AsImageDocument() const { 154 MOZ_ASSERT(IsImageDocument()); 155 return static_cast<const ImageDocument*>(this); 156 } 157 158 } // namespace mozilla::dom 159 160 #endif /* mozilla_dom_ImageDocument_h */