ImageDocument.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_ImageDocument_h
  6. #define mozilla_dom_ImageDocument_h
  7. #include "mozilla/Attributes.h"
  8. #include "imgINotificationObserver.h"
  9. #include "MediaDocument.h"
  10. #include "nsIDOMEventListener.h"
  11. #include "nsIImageDocument.h"
  12. namespace mozilla {
  13. namespace dom {
  14. class ImageDocument final : public MediaDocument,
  15. public nsIImageDocument,
  16. public imgINotificationObserver,
  17. public nsIDOMEventListener
  18. {
  19. public:
  20. ImageDocument();
  21. NS_DECL_ISUPPORTS_INHERITED
  22. virtual nsresult Init() override;
  23. virtual nsresult StartDocumentLoad(const char* aCommand,
  24. nsIChannel* aChannel,
  25. nsILoadGroup* aLoadGroup,
  26. nsISupports* aContainer,
  27. nsIStreamListener** aDocListener,
  28. bool aReset = true,
  29. nsIContentSink* aSink = nullptr) override;
  30. virtual void SetScriptGlobalObject(nsIScriptGlobalObject* aScriptGlobalObject) override;
  31. virtual void Destroy() override;
  32. virtual void OnPageShow(bool aPersisted,
  33. EventTarget* aDispatchStartTarget) override;
  34. NS_DECL_NSIIMAGEDOCUMENT
  35. NS_DECL_IMGINOTIFICATIONOBSERVER
  36. // nsIDOMEventListener
  37. NS_IMETHOD HandleEvent(nsIDOMEvent* aEvent) override;
  38. NS_DECL_CYCLE_COLLECTION_CLASS_INHERITED(ImageDocument, MediaDocument)
  39. friend class ImageListener;
  40. void DefaultCheckOverflowing() { CheckOverflowing(mResizeImageByDefault); }
  41. // WebIDL API
  42. virtual JSObject* WrapNode(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  43. override;
  44. bool ImageIsOverflowing() const
  45. {
  46. return mImageIsOverflowingHorizontally || mImageIsOverflowingVertically;
  47. }
  48. bool ImageIsResized() const
  49. {
  50. return mImageIsResized;
  51. }
  52. already_AddRefed<imgIRequest> GetImageRequest(ErrorResult& aRv);
  53. void ShrinkToFit();
  54. void RestoreImage();
  55. void RestoreImageTo(int32_t aX, int32_t aY)
  56. {
  57. ScrollImageTo(aX, aY, true);
  58. }
  59. void ToggleImageSize();
  60. protected:
  61. virtual ~ImageDocument();
  62. virtual nsresult CreateSyntheticDocument() override;
  63. nsresult CheckOverflowing(bool changeState);
  64. void UpdateTitleAndCharset();
  65. void ScrollImageTo(int32_t aX, int32_t aY, bool restoreImage);
  66. float GetRatio() {
  67. return std::min(mVisibleWidth / mImageWidth,
  68. mVisibleHeight / mImageHeight);
  69. }
  70. void ResetZoomLevel();
  71. float GetZoomLevel();
  72. void UpdateSizeFromLayout();
  73. enum eModeClasses {
  74. eNone,
  75. eShrinkToFit,
  76. eOverflowingVertical, // And maybe horizontal too.
  77. eOverflowingHorizontalOnly
  78. };
  79. void SetModeClass(eModeClasses mode);
  80. nsresult OnSizeAvailable(imgIRequest* aRequest, imgIContainer* aImage);
  81. nsresult OnLoadComplete(imgIRequest* aRequest, nsresult aStatus);
  82. void OnHasTransparency();
  83. nsCOMPtr<Element> mImageContent;
  84. float mVisibleWidth;
  85. float mVisibleHeight;
  86. int32_t mImageWidth;
  87. int32_t mImageHeight;
  88. // Holds the custom background color for stand-alone images
  89. nsAutoString mBackgroundColor;
  90. bool mResizeImageByDefault;
  91. bool mClickResizingEnabled;
  92. bool mImageIsOverflowingHorizontally;
  93. bool mImageIsOverflowingVertically;
  94. // mImageIsResized is true if the image is currently resized
  95. bool mImageIsResized;
  96. // mShouldResize is true if the image should be resized when it doesn't fit
  97. // mImageIsResized cannot be true when this is false, but mImageIsResized
  98. // can be false when this is true
  99. bool mShouldResize;
  100. bool mFirstResize;
  101. // mObservingImageLoader is true while the observer is set.
  102. bool mObservingImageLoader;
  103. float mOriginalZoomLevel;
  104. };
  105. } // namespace dom
  106. } // namespace mozilla
  107. #endif /* mozilla_dom_ImageDocument_h */