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 "mozilla/Attributes.h" 10 #include "imgINotificationObserver.h" 11 #include "MediaDocument.h" 12 #include "nsIDOMEventListener.h" 13 14 namespace mozilla { 15 namespace dom { 16 17 class HTMLImageElement; 18 19 class ImageDocument final : public MediaDocument, 20 public imgINotificationObserver, 21 public nsIDOMEventListener { 22 public: 23 ImageDocument(); 24 25 NS_DECL_ISUPPORTS_INHERITED 26 MediaDocumentKind()27 enum MediaDocumentKind MediaDocumentKind() const override { 28 return MediaDocumentKind::Image; 29 } 30 31 nsresult Init() override; 32 33 nsresult StartDocumentLoad(const char* aCommand, nsIChannel* aChannel, 34 nsILoadGroup* aLoadGroup, nsISupports* aContainer, 35 nsIStreamListener** aDocListener, 36 bool aReset = true, 37 nsIContentSink* aSink = nullptr) override; 38 39 void SetScriptGlobalObject(nsIScriptGlobalObject*) override; 40 void Destroy() override; 41 void OnPageShow(bool aPersisted, EventTarget* aDispatchStartTarget, 42 bool aOnlySystemGroup = false) override; 43 44 NS_DECL_IMGINOTIFICATIONOBSERVER 45 46 // nsIDOMEventListener 47 NS_DECL_NSIDOMEVENTLISTENER 48 49 NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ImageDocument, MediaDocument) 50 51 friend class ImageListener; 52 53 void DefaultCheckOverflowing(); 54 55 // WebIDL API 56 JSObject* WrapNode(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 57 ImageIsOverflowing()58 bool ImageIsOverflowing() const { 59 return ImageIsOverflowingHorizontally() || ImageIsOverflowingVertically(); 60 } 61 ImageIsOverflowingVertically()62 bool ImageIsOverflowingVertically() const { 63 return mImageHeight > mVisibleHeight; 64 } 65 ImageIsOverflowingHorizontally()66 bool ImageIsOverflowingHorizontally() const { 67 return mImageWidth > mVisibleWidth; 68 } 69 ImageIsResized()70 bool ImageIsResized() const { return mImageIsResized; } 71 // ShrinkToFit is called from xpidl methods and we don't have a good 72 // way to mark those MOZ_CAN_RUN_SCRIPT yet. 73 MOZ_CAN_RUN_SCRIPT_BOUNDARY void ShrinkToFit(); 74 void RestoreImage(); 75 76 void NotifyPossibleTitleChange(bool aBoundTitleElement) override; 77 78 protected: 79 virtual ~ImageDocument(); 80 81 nsresult CreateSyntheticDocument() override; 82 83 nsresult CheckOverflowing(bool changeState); 84 85 void UpdateTitleAndCharset(); 86 87 void ScrollImageTo(int32_t aX, int32_t aY); 88 GetRatio()89 float GetRatio() { 90 return std::min(mVisibleWidth / mImageWidth, mVisibleHeight / mImageHeight); 91 } 92 93 void ResetZoomLevel(); 94 float GetZoomLevel(); 95 float GetResolution(); 96 97 void UpdateSizeFromLayout(); 98 99 enum eModeClasses { 100 eNone, 101 eShrinkToFit, 102 eOverflowingVertical, // And maybe horizontal too. 103 eOverflowingHorizontalOnly 104 }; 105 void SetModeClass(eModeClasses mode); 106 107 void OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage); 108 void OnLoadComplete(imgIRequest* aRequest, nsresult aStatus); 109 void OnHasTransparency(); 110 111 RefPtr<HTMLImageElement> mImageContent; 112 113 float mVisibleWidth; 114 float mVisibleHeight; 115 int32_t mImageWidth; 116 int32_t mImageHeight; 117 118 // mImageIsResized is true if the image is currently resized 119 bool mImageIsResized; 120 // mShouldResize is true if the image should be resized when it doesn't fit 121 // mImageIsResized cannot be true when this is false, but mImageIsResized 122 // can be false when this is true 123 bool mShouldResize; 124 bool mFirstResize; 125 // mObservingImageLoader is true while the observer is set. 126 bool mObservingImageLoader; 127 bool mTitleUpdateInProgress; 128 bool mHasCustomTitle; 129 130 float mOriginalZoomLevel; 131 float mOriginalResolution; 132 }; 133 134 } // namespace dom 135 } // namespace mozilla 136 137 #endif /* mozilla_dom_ImageDocument_h */ 138