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 
7 #ifndef nsDOMWindowUtils_h_
8 #define nsDOMWindowUtils_h_
9 
10 #include "nsWeakReference.h"
11 
12 #include "nsIDOMWindowUtils.h"
13 #include "mozilla/Attributes.h"
14 #include "mozilla/BasicEvents.h"
15 #include "mozilla/Result.h"
16 
17 class nsGlobalWindowOuter;
18 class nsIDocShell;
19 class nsIWidget;
20 class nsPresContext;
21 class nsView;
22 struct nsPoint;
23 
24 namespace mozilla {
25 class PresShell;
26 namespace dom {
27 class Document;
28 class Element;
29 }  // namespace dom
30 namespace layers {
31 class LayerTransactionChild;
32 class WebRenderBridgeChild;
33 }  // namespace layers
34 }  // namespace mozilla
35 
36 class nsTranslationNodeList final : public nsITranslationNodeList {
37  public:
nsTranslationNodeList()38   nsTranslationNodeList() {
39     mNodes.SetCapacity(1000);
40     mNodeIsRoot.SetCapacity(1000);
41     mLength = 0;
42   }
43 
44   NS_DECL_ISUPPORTS
45   NS_DECL_NSITRANSLATIONNODELIST
46 
AppendElement(nsINode * aElement,bool aIsRoot)47   void AppendElement(nsINode* aElement, bool aIsRoot) {
48     mNodes.AppendElement(aElement);
49     mNodeIsRoot.AppendElement(aIsRoot);
50     mLength++;
51   }
52 
53  private:
54   ~nsTranslationNodeList() = default;
55 
56   nsTArray<nsCOMPtr<nsINode> > mNodes;
57   nsTArray<bool> mNodeIsRoot;
58   uint32_t mLength;
59 };
60 
61 class nsDOMWindowUtils final : public nsIDOMWindowUtils,
62                                public nsSupportsWeakReference {
63   using TextEventDispatcher = mozilla::widget::TextEventDispatcher;
64 
65  public:
66   explicit nsDOMWindowUtils(nsGlobalWindowOuter* aWindow);
67   NS_DECL_ISUPPORTS
68   NS_DECL_NSIDOMWINDOWUTILS
69 
70  protected:
71   ~nsDOMWindowUtils();
72 
73   nsWeakPtr mWindow;
74 
75   // If aOffset is non-null, it gets filled in with the offset of the root
76   // frame of our window to the nearest widget in the app units of our window.
77   // Add this offset to any event offset we're given to make it relative to the
78   // widget returned by GetWidget.
79   nsIWidget* GetWidget(nsPoint* aOffset = nullptr);
80   nsIWidget* GetWidgetForElement(mozilla::dom::Element* aElement);
81 
82   nsIDocShell* GetDocShell();
83   mozilla::PresShell* GetPresShell();
84   nsPresContext* GetPresContext();
85   mozilla::dom::Document* GetDocument();
86   mozilla::layers::WebRenderBridgeChild* GetWebRenderBridge();
87   mozilla::layers::CompositorBridgeChild* GetCompositorBridge();
88 
89   // Until callers are annotated.
90   MOZ_CAN_RUN_SCRIPT
91   NS_IMETHOD SendMouseEventCommon(
92       const nsAString& aType, float aX, float aY, int32_t aButton,
93       int32_t aClickCount, int32_t aModifiers, bool aIgnoreRootScrollFrame,
94       float aPressure, unsigned short aInputSourceArg, uint32_t aIdentifier,
95       bool aToWindow, bool* aPreventDefault, bool aIsDOMEventSynthesized,
96       bool aIsWidgetEventSynthesized, int32_t aButtons);
97 
98   MOZ_CAN_RUN_SCRIPT
99   nsresult SendTouchEventCommon(
100       const nsAString& aType, const nsTArray<uint32_t>& aIdentifiers,
101       const nsTArray<int32_t>& aXs, const nsTArray<int32_t>& aYs,
102       const nsTArray<uint32_t>& aRxs, const nsTArray<uint32_t>& aRys,
103       const nsTArray<float>& aRotationAngles, const nsTArray<float>& aForces,
104       int32_t aModifiers, bool aIgnoreRootScrollFrame, bool aToWindow,
105       bool* aPreventDefault);
106 
107   void ReportErrorMessageForWindow(const nsAString& aErrorMessage,
108                                    const char* aClassification,
109                                    bool aFromChrome);
110 
111  private:
112   mozilla::Result<mozilla::ScreenRect, nsresult> ConvertToScreenRect(
113       float aX, float aY, float aWidth, float aHeight);
114 };
115 
116 #endif
117