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