Bug 1908539 restrict MacOS platform audio processing to Nightly r=webrtc-reviewers...
[gecko.git] / dom / base / AbstractRange.h
blob38bd7c59bc6cf9b0f59f0651a64638326edce548
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_dom_AbstractRange_h
8 #define mozilla_dom_AbstractRange_h
10 #include <cstdint>
11 #include <ostream>
12 #include "ErrorList.h"
13 #include "js/RootingAPI.h"
14 #include "mozilla/Assertions.h"
15 #include "mozilla/Maybe.h"
16 #include "mozilla/RangeBoundary.h"
17 #include "mozilla/RefPtr.h"
18 #include "mozilla/WeakPtr.h"
19 #include "nsCycleCollectionParticipant.h"
20 #include "nsISupports.h"
21 #include "nsWrapperCache.h"
23 class JSObject;
24 class nsIContent;
25 class nsINode;
26 class nsRange;
27 struct JSContext;
29 namespace mozilla::dom {
30 class Document;
31 class Selection;
32 class StaticRange;
34 enum class AllowRangeCrossShadowBoundary : bool { No, Yes };
36 class AbstractRange : public nsISupports,
37 public nsWrapperCache,
38 // For linking together selection-associated ranges.
39 public mozilla::LinkedListElement<AbstractRange> {
40 using AllowRangeCrossShadowBoundary =
41 mozilla::dom::AllowRangeCrossShadowBoundary;
43 protected:
44 explicit AbstractRange(nsINode* aNode, bool aIsDynamicRange);
45 virtual ~AbstractRange();
47 public:
48 AbstractRange() = delete;
49 explicit AbstractRange(const AbstractRange& aOther) = delete;
51 /**
52 * Called when the process is shutting down.
54 static void Shutdown();
56 NS_DECL_CYCLE_COLLECTING_ISUPPORTS
57 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(AbstractRange)
59 /**
60 * All of the MayCrossShadowBoundary* methods are used to get the boundary
61 * endpoints that cross shadow boundaries. They would return
62 * the same value as the non-MayCrossShadowBoundary* methods if the range
63 * boundaries don't cross shadow boundaries.
65 const RangeBoundary& StartRef() const { return mStart; }
66 const RangeBoundary& MayCrossShadowBoundaryStartRef() const;
68 const RangeBoundary& EndRef() const { return mEnd; }
69 const RangeBoundary& MayCrossShadowBoundaryEndRef() const;
71 nsIContent* GetChildAtStartOffset() const {
72 return mStart.GetChildAtOffset();
74 nsIContent* GetMayCrossShadowBoundaryChildAtStartOffset() const;
76 nsIContent* GetChildAtEndOffset() const { return mEnd.GetChildAtOffset(); }
77 nsIContent* GetMayCrossShadowBoundaryChildAtEndOffset() const;
79 bool IsPositioned() const { return mIsPositioned; }
80 /**
81 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
83 nsINode* GetClosestCommonInclusiveAncestor(
84 AllowRangeCrossShadowBoundary aAllowCrossShadowBoundary =
85 AllowRangeCrossShadowBoundary::No) const;
87 // WebIDL
89 // If Range is created from JS, it's initialized with Document.createRange()
90 // and it collaps the range to start of the Document. Therefore, the
91 // following WebIDL methods are called only when `mIsPositioned` is true.
92 // So, it does not make sense to take `ErrorResult` as their parameter
93 // since its destruction cost may appear in profile. If you create range
94 // object from C++ and needs to check whether it's positioned, should call
95 // `IsPositioned()` directly.
97 nsINode* GetStartContainer() const { return mStart.Container(); }
98 nsINode* GetMayCrossShadowBoundaryStartContainer() const;
100 nsINode* GetEndContainer() const { return mEnd.Container(); }
101 nsINode* GetMayCrossShadowBoundaryEndContainer() const;
103 bool MayCrossShadowBoundary() const;
105 Document* GetComposedDocOfContainers() const {
106 return mStart.Container() ? mStart.Container()->GetComposedDoc() : nullptr;
109 // FYI: Returns 0 if it's not positioned.
110 uint32_t StartOffset() const {
111 return static_cast<uint32_t>(
112 *mStart.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets));
114 uint32_t MayCrossShadowBoundaryStartOffset() const;
116 // FYI: Returns 0 if it's not positioned.
117 uint32_t EndOffset() const {
118 return static_cast<uint32_t>(
119 *mEnd.Offset(RangeBoundary::OffsetFilter::kValidOrInvalidOffsets));
121 uint32_t MayCrossShadowBoundaryEndOffset() const;
123 bool Collapsed() const {
124 return !mIsPositioned || (mStart.Container() == mEnd.Container() &&
125 StartOffset() == EndOffset());
128 bool AreNormalRangeAndCrossShadowBoundaryRangeCollapsed() const;
130 nsINode* GetParentObject() const;
131 virtual JSObject* WrapObject(JSContext* aCx,
132 JS::Handle<JSObject*> aGivenProto) override;
134 bool HasEqualBoundaries(const AbstractRange& aOther) const {
135 return (mStart == aOther.mStart) && (mEnd == aOther.mEnd);
137 bool IsDynamicRange() const { return mIsDynamicRange; }
138 bool IsStaticRange() const { return !mIsDynamicRange; }
139 inline nsRange* AsDynamicRange();
140 inline const nsRange* AsDynamicRange() const;
141 inline StaticRange* AsStaticRange();
142 inline const StaticRange* AsStaticRange() const;
145 * Return true if this range is part of a Selection object
146 * and isn't detached.
148 bool IsInAnySelection() const { return !mSelections.IsEmpty(); }
150 MOZ_CAN_RUN_SCRIPT void RegisterSelection(
151 mozilla::dom::Selection& aSelection);
153 void UnregisterSelection(const mozilla::dom::Selection& aSelection);
156 * Returns a list of all Selections the range is associated with.
158 const nsTArray<WeakPtr<Selection>>& GetSelections() const;
161 * Return true if this range is in |aSelection|.
163 bool IsInSelection(const mozilla::dom::Selection& aSelection) const;
166 * Return true if aRoot is a UA shadow root.
168 static bool IsRootUAWidget(const nsINode* aRoot);
170 protected:
171 template <typename SPT, typename SRT, typename EPT, typename ERT,
172 typename RangeType>
173 static nsresult SetStartAndEndInternal(
174 const RangeBoundaryBase<SPT, SRT>& aStartBoundary,
175 const RangeBoundaryBase<EPT, ERT>& aEndBoundary, RangeType* aRange);
177 template <class RangeType>
178 static bool MaybeCacheToReuse(RangeType& aInstance);
180 void Init(nsINode* aNode);
182 friend std::ostream& operator<<(std::ostream& aStream,
183 const AbstractRange& aRange) {
184 if (aRange.Collapsed()) {
185 aStream << "{ mStart=mEnd=" << aRange.mStart;
186 } else {
187 aStream << "{ mStart=" << aRange.mStart << ", mEnd=" << aRange.mEnd;
189 return aStream << ", mIsGenerated="
190 << (aRange.mIsGenerated ? "true" : "false")
191 << ", mCalledByJS="
192 << (aRange.mIsPositioned ? "true" : "false")
193 << ", mIsDynamicRange="
194 << (aRange.mIsDynamicRange ? "true" : "false") << " }";
198 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
200 void RegisterClosestCommonInclusiveAncestor(nsINode* aNode);
202 * https://dom.spec.whatwg.org/#concept-tree-inclusive-ancestor
204 void UnregisterClosestCommonInclusiveAncestor(nsINode* aNode,
205 bool aIsUnlinking);
207 void UpdateCommonAncestorIfNecessary();
209 static void MarkDescendants(const nsINode& aNode);
210 static void UnmarkDescendants(const nsINode& aNode);
212 private:
213 void ClearForReuse();
215 protected:
216 RefPtr<Document> mOwner;
217 RangeBoundary mStart;
218 RangeBoundary mEnd;
220 // A Range can be part of multiple |Selection|s. This is a very rare use case.
221 AutoTArray<WeakPtr<Selection>, 1> mSelections;
222 // mRegisteredClosestCommonInclusiveAncestor is only non-null when the range
223 // IsInAnySelection().
224 nsCOMPtr<nsINode> mRegisteredClosestCommonInclusiveAncestor;
226 // `true` if `mStart` and `mEnd` are set for StaticRange or set and valid
227 // for nsRange.
228 bool mIsPositioned;
230 // Used by nsRange, but this should have this for minimizing the size.
231 bool mIsGenerated;
232 // Used by nsRange, but this should have this for minimizing the size.
233 bool mCalledByJS;
235 // true if this is an `nsRange` object.
236 const bool mIsDynamicRange;
238 static bool sHasShutDown;
241 } // namespace mozilla::dom
243 #endif // #ifndef mozilla_dom_AbstractRange_h