Bug 1665252 - remove allowpaymentrequest attribute from HTMLIFrameElement r=dom-worke...
[gecko.git] / dom / base / DOMRect.h
blob362f83ac9fb1a03819668aaa1bd68b97260d7a3f
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/. */
7 #ifndef MOZILLA_DOMRECT_H_
8 #define MOZILLA_DOMRECT_H_
10 #include "js/StructuredClone.h"
11 #include "nsTArray.h"
12 #include "nsCOMPtr.h"
13 #include "nsWrapperCache.h"
14 #include "nsCycleCollectionParticipant.h"
15 #include "mozilla/Attributes.h"
16 #include "mozilla/dom/BindingDeclarations.h"
17 #include "mozilla/ErrorResult.h"
18 #include "mozilla/FloatingPoint.h"
20 struct nsRect;
21 class nsIGlobalObject;
23 namespace mozilla {
24 namespace dom {
26 struct DOMRectInit;
28 class DOMRectReadOnly : public nsISupports, public nsWrapperCache {
29 protected:
30 virtual ~DOMRectReadOnly() = default;
32 public:
33 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
34 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly)
36 explicit DOMRectReadOnly(nsISupports* aParent, double aX = 0, double aY = 0,
37 double aWidth = 0, double aHeight = 0)
38 : mParent(aParent), mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight) {}
40 nsISupports* GetParentObject() const {
41 MOZ_ASSERT(mParent);
42 return mParent;
45 virtual JSObject* WrapObject(JSContext* aCx,
46 JS::Handle<JSObject*> aGivenProto) override;
48 static already_AddRefed<DOMRectReadOnly> FromRect(const GlobalObject& aGlobal,
49 const DOMRectInit& aInit);
51 static already_AddRefed<DOMRectReadOnly> Constructor(
52 const GlobalObject& aGlobal, double aX, double aY, double aWidth,
53 double aHeight);
55 double X() const { return mX; }
56 double Y() const { return mY; }
57 double Width() const { return mWidth; }
58 double Height() const { return mHeight; }
60 double Left() const {
61 double x = X(), w = Width();
62 return NaNSafeMin(x, x + w);
64 double Top() const {
65 double y = Y(), h = Height();
66 return NaNSafeMin(y, y + h);
68 double Right() const {
69 double x = X(), w = Width();
70 return NaNSafeMax(x, x + w);
72 double Bottom() const {
73 double y = Y(), h = Height();
74 return NaNSafeMax(y, y + h);
77 bool WriteStructuredClone(JSContext* aCx,
78 JSStructuredCloneWriter* aWriter) const;
80 static already_AddRefed<DOMRectReadOnly> ReadStructuredClone(
81 JSContext* aCx, nsIGlobalObject* aGlobal,
82 JSStructuredCloneReader* aReader);
84 protected:
85 // Shared implementation of ReadStructuredClone for DOMRect and
86 // DOMRectReadOnly.
87 bool ReadStructuredClone(JSStructuredCloneReader* aReader);
89 nsCOMPtr<nsISupports> mParent;
90 double mX, mY, mWidth, mHeight;
93 class DOMRect final : public DOMRectReadOnly {
94 public:
95 explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0,
96 double aWidth = 0, double aHeight = 0)
97 : DOMRectReadOnly(aParent, aX, aY, aWidth, aHeight) {}
99 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMRect, DOMRectReadOnly)
101 static already_AddRefed<DOMRect> FromRect(const GlobalObject& aGlobal,
102 const DOMRectInit& aInit);
104 static already_AddRefed<DOMRect> Constructor(const GlobalObject& aGlobal,
105 double aX, double aY,
106 double aWidth, double aHeight);
108 virtual JSObject* WrapObject(JSContext* aCx,
109 JS::Handle<JSObject*> aGivenProto) override;
111 static already_AddRefed<DOMRect> ReadStructuredClone(
112 JSContext* aCx, nsIGlobalObject* aGlobal,
113 JSStructuredCloneReader* aReader);
114 using DOMRectReadOnly::ReadStructuredClone;
116 void SetRect(float aX, float aY, float aWidth, float aHeight) {
117 mX = aX;
118 mY = aY;
119 mWidth = aWidth;
120 mHeight = aHeight;
122 void SetLayoutRect(const nsRect& aLayoutRect);
124 void SetX(double aX) { mX = aX; }
125 void SetY(double aY) { mY = aY; }
126 void SetWidth(double aWidth) { mWidth = aWidth; }
127 void SetHeight(double aHeight) { mHeight = aHeight; }
129 static DOMRect* FromSupports(nsISupports* aSupports) {
130 return static_cast<DOMRect*>(aSupports);
133 private:
134 ~DOMRect() = default;
137 class DOMRectList final : public nsISupports, public nsWrapperCache {
138 ~DOMRectList() = default;
140 public:
141 explicit DOMRectList(nsISupports* aParent) : mParent(aParent) {}
143 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
144 NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList)
146 virtual JSObject* WrapObject(JSContext* cx,
147 JS::Handle<JSObject*> aGivenProto) override;
149 nsISupports* GetParentObject() { return mParent; }
151 void Append(DOMRect* aElement) { mArray.AppendElement(aElement); }
153 uint32_t Length() { return mArray.Length(); }
154 DOMRect* Item(uint32_t aIndex) { return mArray.SafeElementAt(aIndex); }
155 DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound) {
156 aFound = aIndex < mArray.Length();
157 if (!aFound) {
158 return nullptr;
160 return mArray[aIndex];
163 protected:
164 nsTArray<RefPtr<DOMRect> > mArray;
165 nsCOMPtr<nsISupports> mParent;
168 } // namespace dom
169 } // namespace mozilla
171 #endif /*MOZILLA_DOMRECT_H_*/