TextTrackRegion.h (3602B)
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim:set ts=2 sw=2 et tw=78: */ 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_dom_TextTrackRegion_h 8 #define mozilla_dom_TextTrackRegion_h 9 10 #include "mozilla/ErrorResult.h" 11 #include "mozilla/Preferences.h" 12 #include "mozilla/dom/TextTrack.h" 13 #include "mozilla/dom/VTTRegionBinding.h" 14 #include "nsCycleCollectionParticipant.h" 15 #include "nsString.h" 16 #include "nsWrapperCache.h" 17 18 namespace mozilla::dom { 19 20 class GlobalObject; 21 class TextTrack; 22 23 class TextTrackRegion final : public nsISupports, public nsWrapperCache { 24 public: 25 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 26 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(TextTrackRegion) 27 28 JSObject* WrapObject(JSContext* aCx, 29 JS::Handle<JSObject*> aGivenProto) override; 30 31 nsISupports* GetParentObject() const { return mParent; } 32 33 explicit TextTrackRegion(nsISupports* aGlobal); 34 35 /** WebIDL Methods. */ 36 37 static already_AddRefed<TextTrackRegion> Constructor( 38 const GlobalObject& aGlobal, ErrorResult& aRv); 39 40 double Lines() const { return mLines; } 41 42 void SetLines(double aLines, ErrorResult& aRv) { 43 if (aLines < 0) { 44 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); 45 } else { 46 mLines = aLines; 47 } 48 } 49 50 double Width() const { return mWidth; } 51 52 void SetWidth(double aWidth, ErrorResult& aRv) { 53 if (!InvalidValue(aWidth, aRv)) { 54 mWidth = aWidth; 55 } 56 } 57 58 double RegionAnchorX() const { return mRegionAnchorX; } 59 60 void SetRegionAnchorX(double aVal, ErrorResult& aRv) { 61 if (!InvalidValue(aVal, aRv)) { 62 mRegionAnchorX = aVal; 63 } 64 } 65 66 double RegionAnchorY() const { return mRegionAnchorY; } 67 68 void SetRegionAnchorY(double aVal, ErrorResult& aRv) { 69 if (!InvalidValue(aVal, aRv)) { 70 mRegionAnchorY = aVal; 71 } 72 } 73 74 double ViewportAnchorX() const { return mViewportAnchorX; } 75 76 void SetViewportAnchorX(double aVal, ErrorResult& aRv) { 77 if (!InvalidValue(aVal, aRv)) { 78 mViewportAnchorX = aVal; 79 } 80 } 81 82 double ViewportAnchorY() const { return mViewportAnchorY; } 83 84 void SetViewportAnchorY(double aVal, ErrorResult& aRv) { 85 if (!InvalidValue(aVal, aRv)) { 86 mViewportAnchorY = aVal; 87 } 88 } 89 90 ScrollSetting Scroll() const { return mScroll; } 91 92 void SetScroll(const ScrollSetting& aScroll) { 93 if (aScroll == ScrollSetting::_empty || aScroll == ScrollSetting::Up) { 94 mScroll = aScroll; 95 } 96 } 97 98 void GetId(nsAString& aId) const { aId = mId; } 99 100 void SetId(const nsAString& aId) { mId = aId; } 101 102 /** end WebIDL Methods. */ 103 104 // Helper to aid copying of a given TextTrackRegion's width, lines, 105 // anchor, viewport and scroll values. 106 void CopyValues(TextTrackRegion& aRegion); 107 108 // -----helpers------- 109 const nsAString& Id() const { return mId; } 110 111 private: 112 ~TextTrackRegion() = default; 113 114 nsCOMPtr<nsISupports> mParent; 115 nsString mId; 116 double mWidth; 117 long mLines; 118 double mRegionAnchorX; 119 double mRegionAnchorY; 120 double mViewportAnchorX; 121 double mViewportAnchorY; 122 ScrollSetting mScroll; 123 124 // Helper to ensure new value is in the range: 0.0% - 100.0%; throws 125 // an IndexSizeError otherwise. 126 inline bool InvalidValue(double aValue, ErrorResult& aRv) { 127 if (aValue < 0.0 || aValue > 100.0) { 128 aRv.Throw(NS_ERROR_DOM_INDEX_SIZE_ERR); 129 return true; 130 } 131 132 return false; 133 } 134 }; 135 136 } // namespace mozilla::dom 137 138 #endif // mozilla_dom_TextTrackRegion_h