DOMRect.h (5363B)
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 MOZILLA_DOMRECT_H_ 8 #define MOZILLA_DOMRECT_H_ 9 10 #include <algorithm> 11 #include <cstdint> 12 #include <new> 13 #include <utility> 14 15 #include "js/TypeDecls.h" 16 #include "mozilla/AlreadyAddRefed.h" 17 #include "mozilla/Assertions.h" 18 #include "mozilla/FloatingPoint.h" 19 #include "mozilla/RefPtr.h" 20 #include "nsCOMPtr.h" 21 #include "nsCycleCollectionParticipant.h" 22 #include "nsISupports.h" 23 #include "nsTArray.h" 24 #include "nsWrapperCache.h" 25 26 class JSObject; 27 class nsIGlobalObject; 28 struct JSContext; 29 struct JSStructuredCloneReader; 30 struct JSStructuredCloneWriter; 31 struct nsRect; 32 33 namespace mozilla::dom { 34 35 class GlobalObject; 36 struct DOMRectInit; 37 38 class DOMRectReadOnly : public nsISupports, public nsWrapperCache { 39 protected: 40 virtual ~DOMRectReadOnly() = default; 41 42 public: 43 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 44 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMRectReadOnly) 45 46 explicit DOMRectReadOnly(nsISupports* aParent, double aX = 0, double aY = 0, 47 double aWidth = 0, double aHeight = 0) 48 : mParent(aParent), mX(aX), mY(aY), mWidth(aWidth), mHeight(aHeight) {} 49 50 nsISupports* GetParentObject() const { 51 MOZ_ASSERT(mParent); 52 return mParent; 53 } 54 55 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 56 57 static already_AddRefed<DOMRectReadOnly> FromRect(const GlobalObject& aGlobal, 58 const DOMRectInit& aInit); 59 60 static already_AddRefed<DOMRectReadOnly> Constructor( 61 const GlobalObject& aGlobal, double aX, double aY, double aWidth, 62 double aHeight); 63 64 double X() const { return mX; } 65 double Y() const { return mY; } 66 double Width() const { return mWidth; } 67 double Height() const { return mHeight; } 68 69 double Left() const { 70 double x = X(), w = Width(); 71 return NaNSafeMin(x, x + w); 72 } 73 double Top() const { 74 double y = Y(), h = Height(); 75 return NaNSafeMin(y, y + h); 76 } 77 double Right() const { 78 double x = X(), w = Width(); 79 return NaNSafeMax(x, x + w); 80 } 81 double Bottom() const { 82 double y = Y(), h = Height(); 83 return NaNSafeMax(y, y + h); 84 } 85 86 bool WriteStructuredClone(JSContext* aCx, 87 JSStructuredCloneWriter* aWriter) const; 88 89 static already_AddRefed<DOMRectReadOnly> ReadStructuredClone( 90 JSContext* aCx, nsIGlobalObject* aGlobal, 91 JSStructuredCloneReader* aReader); 92 93 protected: 94 // Shared implementation of ReadStructuredClone for DOMRect and 95 // DOMRectReadOnly. 96 bool ReadStructuredClone(JSStructuredCloneReader* aReader); 97 98 nsCOMPtr<nsISupports> mParent; 99 double mX, mY, mWidth, mHeight; 100 }; 101 102 class DOMRect final : public DOMRectReadOnly { 103 public: 104 explicit DOMRect(nsISupports* aParent, double aX = 0, double aY = 0, 105 double aWidth = 0, double aHeight = 0) 106 : DOMRectReadOnly(aParent, aX, aY, aWidth, aHeight) {} 107 108 NS_INLINE_DECL_REFCOUNTING_INHERITED(DOMRect, DOMRectReadOnly) 109 110 static already_AddRefed<DOMRect> FromRect(const GlobalObject& aGlobal, 111 const DOMRectInit& aInit); 112 113 static already_AddRefed<DOMRect> Constructor(const GlobalObject& aGlobal, 114 double aX, double aY, 115 double aWidth, double aHeight); 116 117 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 118 119 static already_AddRefed<DOMRect> ReadStructuredClone( 120 JSContext* aCx, nsIGlobalObject* aGlobal, 121 JSStructuredCloneReader* aReader); 122 using DOMRectReadOnly::ReadStructuredClone; 123 124 void SetRect(float aX, float aY, float aWidth, float aHeight) { 125 mX = aX; 126 mY = aY; 127 mWidth = aWidth; 128 mHeight = aHeight; 129 } 130 void SetLayoutRect(const nsRect& aLayoutRect); 131 132 void SetX(double aX) { mX = aX; } 133 void SetY(double aY) { mY = aY; } 134 void SetWidth(double aWidth) { mWidth = aWidth; } 135 void SetHeight(double aHeight) { mHeight = aHeight; } 136 137 static DOMRect* FromSupports(nsISupports* aSupports) { 138 return static_cast<DOMRect*>(aSupports); 139 } 140 141 private: 142 ~DOMRect() = default; 143 }; 144 145 class DOMRectList final : public nsISupports, public nsWrapperCache { 146 ~DOMRectList() = default; 147 148 public: 149 explicit DOMRectList(nsISupports* aParent) : mParent(aParent) {} 150 151 NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL 152 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMRectList) 153 154 JSObject* WrapObject(JSContext*, JS::Handle<JSObject*> aGivenProto) override; 155 156 nsISupports* GetParentObject() { return mParent; } 157 158 void Append(RefPtr<DOMRect>&& aElement) { 159 mArray.AppendElement(std::move(aElement)); 160 } 161 162 uint32_t Length() { return mArray.Length(); } 163 DOMRect* Item(uint32_t aIndex) { return mArray.SafeElementAt(aIndex); } 164 DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound) { 165 aFound = aIndex < mArray.Length(); 166 if (!aFound) { 167 return nullptr; 168 } 169 return mArray[aIndex]; 170 } 171 172 protected: 173 nsTArray<RefPtr<DOMRect> > mArray; 174 nsCOMPtr<nsISupports> mParent; 175 }; 176 177 } // namespace mozilla::dom 178 179 #endif /*MOZILLA_DOMRECT_H_*/