Thu, 15 Jan 2015 21:03:48 +0100
Integrate friendly tips from Tor colleagues to make (or not) 4.5 alpha 3;
This includes removal of overloaded (but unused) methods, and addition of
a overlooked call to DataStruct::SetData(nsISupports, uint32_t, bool.)
michael@0 | 1 | /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ |
michael@0 | 2 | /* This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 3 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 4 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 5 | |
michael@0 | 6 | #ifndef MOZILLA_DOMRECT_H_ |
michael@0 | 7 | #define MOZILLA_DOMRECT_H_ |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIDOMClientRect.h" |
michael@0 | 10 | #include "nsIDOMClientRectList.h" |
michael@0 | 11 | #include "nsTArray.h" |
michael@0 | 12 | #include "nsCOMPtr.h" |
michael@0 | 13 | #include "nsAutoPtr.h" |
michael@0 | 14 | #include "nsWrapperCache.h" |
michael@0 | 15 | #include "nsCycleCollectionParticipant.h" |
michael@0 | 16 | #include "mozilla/Attributes.h" |
michael@0 | 17 | #include "mozilla/dom/BindingDeclarations.h" |
michael@0 | 18 | #include "mozilla/ErrorResult.h" |
michael@0 | 19 | #include <algorithm> |
michael@0 | 20 | |
michael@0 | 21 | struct nsRect; |
michael@0 | 22 | |
michael@0 | 23 | namespace mozilla { |
michael@0 | 24 | namespace dom { |
michael@0 | 25 | |
michael@0 | 26 | class DOMRectReadOnly : public nsISupports |
michael@0 | 27 | , public nsWrapperCache |
michael@0 | 28 | { |
michael@0 | 29 | public: |
michael@0 | 30 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 31 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectReadOnly) |
michael@0 | 32 | |
michael@0 | 33 | virtual ~DOMRectReadOnly() {} |
michael@0 | 34 | |
michael@0 | 35 | DOMRectReadOnly(nsISupports* aParent) |
michael@0 | 36 | : mParent(aParent) |
michael@0 | 37 | { |
michael@0 | 38 | SetIsDOMBinding(); |
michael@0 | 39 | } |
michael@0 | 40 | |
michael@0 | 41 | nsISupports* GetParentObject() const |
michael@0 | 42 | { |
michael@0 | 43 | MOZ_ASSERT(mParent); |
michael@0 | 44 | return mParent; |
michael@0 | 45 | } |
michael@0 | 46 | virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 47 | |
michael@0 | 48 | virtual double X() const = 0; |
michael@0 | 49 | virtual double Y() const = 0; |
michael@0 | 50 | virtual double Width() const = 0; |
michael@0 | 51 | virtual double Height() const = 0; |
michael@0 | 52 | |
michael@0 | 53 | double Left() const |
michael@0 | 54 | { |
michael@0 | 55 | double x = X(), w = Width(); |
michael@0 | 56 | return std::min(x, x + w); |
michael@0 | 57 | } |
michael@0 | 58 | double Top() const |
michael@0 | 59 | { |
michael@0 | 60 | double y = Y(), h = Height(); |
michael@0 | 61 | return std::min(y, y + h); |
michael@0 | 62 | } |
michael@0 | 63 | double Right() const |
michael@0 | 64 | { |
michael@0 | 65 | double x = X(), w = Width(); |
michael@0 | 66 | return std::max(x, x + w); |
michael@0 | 67 | } |
michael@0 | 68 | double Bottom() const |
michael@0 | 69 | { |
michael@0 | 70 | double y = Y(), h = Height(); |
michael@0 | 71 | return std::max(y, y + h); |
michael@0 | 72 | } |
michael@0 | 73 | |
michael@0 | 74 | protected: |
michael@0 | 75 | nsCOMPtr<nsISupports> mParent; |
michael@0 | 76 | }; |
michael@0 | 77 | |
michael@0 | 78 | class DOMRect MOZ_FINAL : public DOMRectReadOnly |
michael@0 | 79 | , public nsIDOMClientRect |
michael@0 | 80 | { |
michael@0 | 81 | public: |
michael@0 | 82 | DOMRect(nsISupports* aParent, double aX = 0, double aY = 0, |
michael@0 | 83 | double aWidth = 0, double aHeight = 0) |
michael@0 | 84 | : DOMRectReadOnly(aParent) |
michael@0 | 85 | , mX(aX) |
michael@0 | 86 | , mY(aY) |
michael@0 | 87 | , mWidth(aWidth) |
michael@0 | 88 | , mHeight(aHeight) |
michael@0 | 89 | { |
michael@0 | 90 | } |
michael@0 | 91 | |
michael@0 | 92 | NS_DECL_ISUPPORTS_INHERITED |
michael@0 | 93 | NS_DECL_NSIDOMCLIENTRECT |
michael@0 | 94 | |
michael@0 | 95 | static already_AddRefed<DOMRect> |
michael@0 | 96 | Constructor(const GlobalObject& aGlobal, ErrorResult& aRV); |
michael@0 | 97 | static already_AddRefed<DOMRect> |
michael@0 | 98 | Constructor(const GlobalObject& aGlobal, double aX, double aY, |
michael@0 | 99 | double aWidth, double aHeight, ErrorResult& aRV); |
michael@0 | 100 | |
michael@0 | 101 | virtual JSObject* WrapObject(JSContext* aCx) MOZ_OVERRIDE; |
michael@0 | 102 | |
michael@0 | 103 | void SetRect(float aX, float aY, float aWidth, float aHeight) { |
michael@0 | 104 | mX = aX; mY = aY; mWidth = aWidth; mHeight = aHeight; |
michael@0 | 105 | } |
michael@0 | 106 | void SetLayoutRect(const nsRect& aLayoutRect); |
michael@0 | 107 | |
michael@0 | 108 | virtual double X() const MOZ_OVERRIDE |
michael@0 | 109 | { |
michael@0 | 110 | return mX; |
michael@0 | 111 | } |
michael@0 | 112 | virtual double Y() const MOZ_OVERRIDE |
michael@0 | 113 | { |
michael@0 | 114 | return mY; |
michael@0 | 115 | } |
michael@0 | 116 | virtual double Width() const MOZ_OVERRIDE |
michael@0 | 117 | { |
michael@0 | 118 | return mWidth; |
michael@0 | 119 | } |
michael@0 | 120 | virtual double Height() const MOZ_OVERRIDE |
michael@0 | 121 | { |
michael@0 | 122 | return mHeight; |
michael@0 | 123 | } |
michael@0 | 124 | |
michael@0 | 125 | void SetX(double aX) |
michael@0 | 126 | { |
michael@0 | 127 | mX = aX; |
michael@0 | 128 | } |
michael@0 | 129 | void SetY(double aY) |
michael@0 | 130 | { |
michael@0 | 131 | mY = aY; |
michael@0 | 132 | } |
michael@0 | 133 | void SetWidth(double aWidth) |
michael@0 | 134 | { |
michael@0 | 135 | mWidth = aWidth; |
michael@0 | 136 | } |
michael@0 | 137 | void SetHeight(double aHeight) |
michael@0 | 138 | { |
michael@0 | 139 | mHeight = aHeight; |
michael@0 | 140 | } |
michael@0 | 141 | |
michael@0 | 142 | protected: |
michael@0 | 143 | double mX, mY, mWidth, mHeight; |
michael@0 | 144 | }; |
michael@0 | 145 | |
michael@0 | 146 | class DOMRectList MOZ_FINAL : public nsIDOMClientRectList, |
michael@0 | 147 | public nsWrapperCache |
michael@0 | 148 | { |
michael@0 | 149 | public: |
michael@0 | 150 | DOMRectList(nsISupports *aParent) : mParent(aParent) |
michael@0 | 151 | { |
michael@0 | 152 | SetIsDOMBinding(); |
michael@0 | 153 | } |
michael@0 | 154 | |
michael@0 | 155 | NS_DECL_CYCLE_COLLECTING_ISUPPORTS |
michael@0 | 156 | NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMRectList) |
michael@0 | 157 | |
michael@0 | 158 | NS_DECL_NSIDOMCLIENTRECTLIST |
michael@0 | 159 | |
michael@0 | 160 | virtual JSObject* WrapObject(JSContext *cx) MOZ_OVERRIDE; |
michael@0 | 161 | |
michael@0 | 162 | nsISupports* GetParentObject() |
michael@0 | 163 | { |
michael@0 | 164 | return mParent; |
michael@0 | 165 | } |
michael@0 | 166 | |
michael@0 | 167 | void Append(DOMRect* aElement) { mArray.AppendElement(aElement); } |
michael@0 | 168 | |
michael@0 | 169 | static DOMRectList* FromSupports(nsISupports* aSupports) |
michael@0 | 170 | { |
michael@0 | 171 | #ifdef DEBUG |
michael@0 | 172 | { |
michael@0 | 173 | nsCOMPtr<nsIDOMClientRectList> list_qi = do_QueryInterface(aSupports); |
michael@0 | 174 | |
michael@0 | 175 | // If this assertion fires the QI implementation for the object in |
michael@0 | 176 | // question doesn't use the nsIDOMClientRectList pointer as the nsISupports |
michael@0 | 177 | // pointer. That must be fixed, or we'll crash... |
michael@0 | 178 | NS_ASSERTION(list_qi == static_cast<nsIDOMClientRectList*>(aSupports), |
michael@0 | 179 | "Uh, fix QI!"); |
michael@0 | 180 | } |
michael@0 | 181 | #endif |
michael@0 | 182 | |
michael@0 | 183 | return static_cast<DOMRectList*>(aSupports); |
michael@0 | 184 | } |
michael@0 | 185 | |
michael@0 | 186 | uint32_t Length() |
michael@0 | 187 | { |
michael@0 | 188 | return mArray.Length(); |
michael@0 | 189 | } |
michael@0 | 190 | DOMRect* Item(uint32_t aIndex) |
michael@0 | 191 | { |
michael@0 | 192 | return mArray.SafeElementAt(aIndex); |
michael@0 | 193 | } |
michael@0 | 194 | DOMRect* IndexedGetter(uint32_t aIndex, bool& aFound) |
michael@0 | 195 | { |
michael@0 | 196 | aFound = aIndex < mArray.Length(); |
michael@0 | 197 | if (!aFound) { |
michael@0 | 198 | return nullptr; |
michael@0 | 199 | } |
michael@0 | 200 | return mArray[aIndex]; |
michael@0 | 201 | } |
michael@0 | 202 | |
michael@0 | 203 | protected: |
michael@0 | 204 | nsTArray<nsRefPtr<DOMRect> > mArray; |
michael@0 | 205 | nsCOMPtr<nsISupports> mParent; |
michael@0 | 206 | }; |
michael@0 | 207 | |
michael@0 | 208 | } |
michael@0 | 209 | } |
michael@0 | 210 | |
michael@0 | 211 | #endif /*MOZILLA_DOMRECT_H_*/ |