1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef DOMIntersectionObserver_h
#define DOMIntersectionObserver_h
#include "mozilla/Attributes.h"
#include "mozilla/ServoStyleConsts.h"
#include "mozilla/Variant.h"
#include "mozilla/dom/IntersectionObserverBinding.h"
#include "nsDOMNavigationTiming.h"
#include "nsTArray.h"
#include "nsTHashSet.h"
namespace mozilla::dom {
class DOMIntersectionObserver;
class DOMIntersectionObserverEntry final : public nsISupports,
public nsWrapperCache {
~DOMIntersectionObserverEntry() = default;
public:
DOMIntersectionObserverEntry(nsISupports* aOwner, DOMHighResTimeStamp aTime,
RefPtr<DOMRect> aRootBounds,
RefPtr<DOMRect> aBoundingClientRect,
RefPtr<DOMRect> aIntersectionRect,
bool aIsIntersecting, Element* aTarget,
double aIntersectionRatio)
: mOwner(aOwner),
mTime(aTime),
mRootBounds(std::move(aRootBounds)),
mBoundingClientRect(std::move(aBoundingClientRect)),
mIntersectionRect(std::move(aIntersectionRect)),
mIsIntersecting(aIsIntersecting),
mTarget(aTarget),
mIntersectionRatio(aIntersectionRatio) {}
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(DOMIntersectionObserverEntry)
nsISupports* GetParentObject() const { return mOwner; }
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override {
return IntersectionObserverEntry_Binding::Wrap(aCx, this, aGivenProto);
}
DOMHighResTimeStamp Time() const { return mTime; }
DOMRect* GetRootBounds() { return mRootBounds; }
DOMRect* BoundingClientRect() { return mBoundingClientRect; }
DOMRect* IntersectionRect() { return mIntersectionRect; }
bool IsIntersecting() const { return mIsIntersecting; }
double IntersectionRatio() const { return mIntersectionRatio; }
Element* Target() { return mTarget; }
protected:
nsCOMPtr<nsISupports> mOwner;
DOMHighResTimeStamp mTime;
RefPtr<DOMRect> mRootBounds;
RefPtr<DOMRect> mBoundingClientRect;
RefPtr<DOMRect> mIntersectionRect;
bool mIsIntersecting;
RefPtr<Element> mTarget;
double mIntersectionRatio;
};
#define NS_DOM_INTERSECTION_OBSERVER_IID \
{0x8570a575, 0xe303, 0x4d18, {0xb6, 0xb1, 0x4d, 0x2b, 0x49, 0xd8, 0xef, 0x94}}
using IntersectionObserverMargin = StyleRect<LengthPercentage>;
// An input suitable to compute intersections with multiple targets.
struct IntersectionInput {
// Whether the root is implicit (null, originally).
const bool mIsImplicitRoot = false;
// The computed root node. For the implicit root, this will be the in-process
// root document we can compute coordinates against (along with the remote
// document visible rect if appropriate).
const nsINode* mRootNode = nullptr;
nsIFrame* mRootFrame = nullptr;
// The rect of mRootFrame in client coordinates.
nsRect mRootRect;
// The root margin computed against the root rect.
nsMargin mRootMargin;
// The scroll margin computed against the root rect.
IntersectionObserverMargin mScrollMargin;
// If this is in an OOP iframe, the visible rect of the OOP frame.
Maybe<nsRect> mRemoteDocumentVisibleRect;
};
struct IntersectionOutput {
const bool mIsSimilarOrigin;
const nsRect mRootBounds;
const nsRect mTargetRect;
const Maybe<nsRect> mIntersectionRect;
bool Intersects() const { return mIntersectionRect.isSome(); }
};
class DOMIntersectionObserver final : public nsISupports,
public nsWrapperCache {
virtual ~DOMIntersectionObserver() { Disconnect(); }
using NativeCallback = void (*)(
const Sequence<OwningNonNull<DOMIntersectionObserverEntry>>& aEntries);
DOMIntersectionObserver(Document&, NativeCallback);
public:
DOMIntersectionObserver(already_AddRefed<nsPIDOMWindowInner>&& aOwner,
dom::IntersectionCallback& aCb);
NS_DECL_CYCLE_COLLECTING_ISUPPORTS
NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(DOMIntersectionObserver)
NS_INLINE_DECL_STATIC_IID(NS_DOM_INTERSECTION_OBSERVER_IID)
static already_AddRefed<DOMIntersectionObserver> Constructor(
const GlobalObject&, dom::IntersectionCallback&, ErrorResult&);
static already_AddRefed<DOMIntersectionObserver> Constructor(
const GlobalObject&, dom::IntersectionCallback&,
const IntersectionObserverInit&, ErrorResult&);
JSObject* WrapObject(JSContext* aCx,
JS::Handle<JSObject*> aGivenProto) override {
return IntersectionObserver_Binding::Wrap(aCx, this, aGivenProto);
}
nsISupports* GetParentObject() const;
nsINode* GetRoot() const { return mRoot; }
void GetRootMargin(nsACString&);
bool SetRootMargin(const nsACString&);
void GetScrollMargin(nsACString&);
bool SetScrollMargin(const nsACString&);
void GetThresholds(nsTArray<double>& aRetVal);
void Observe(Element& aTarget);
void Unobserve(Element& aTarget);
void UnlinkTarget(Element& aTarget);
void Disconnect();
void TakeRecords(nsTArray<RefPtr<DOMIntersectionObserverEntry>>& aRetVal);
static StyleRect<LengthPercentage> LazyLoadingRootMargin();
static IntersectionInput ComputeInput(
const Document& aDocument, const nsINode* aRoot,
const StyleRect<LengthPercentage>* aRootMargin,
const StyleRect<LengthPercentage>* aScrollMargin);
enum class IsForProximityToViewport : bool { No, Yes };
enum class BoxToUse : uint8_t {
Content,
Border,
OverflowClip,
};
static IntersectionOutput Intersect(
const IntersectionInput&, const Element&, BoxToUse = BoxToUse::Border,
IsForProximityToViewport = IsForProximityToViewport::No);
// Intersects with a given rect, already relative to the root frame.
static IntersectionOutput Intersect(const IntersectionInput&, const nsRect&);
void Update(Document& aDocument, DOMHighResTimeStamp time);
MOZ_CAN_RUN_SCRIPT void Notify();
static already_AddRefed<DOMIntersectionObserver> CreateLazyLoadObserver(
Document&);
static Maybe<nsRect> EdgeInclusiveIntersection(const nsRect& aRect,
const nsRect& aOtherRect);
protected:
void Connect();
void QueueIntersectionObserverEntry(Element* aTarget,
DOMHighResTimeStamp time,
const Maybe<nsRect>& aRootRect,
const nsRect& aTargetRect,
const Maybe<nsRect>& aIntersectionRect,
bool aIsIntersecting,
double aIntersectionRatio);
nsCOMPtr<nsPIDOMWindowInner> mOwner;
RefPtr<Document> mDocument;
Variant<RefPtr<dom::IntersectionCallback>, NativeCallback> mCallback;
RefPtr<nsINode> mRoot;
StyleRect<LengthPercentage> mRootMargin;
StyleRect<LengthPercentage> mScrollMargin;
AutoTArray<double, 1> mThresholds;
// These hold raw pointers which are explicitly cleared by UnlinkTarget().
//
// We keep a set and an array because we need ordered access, but also
// constant time lookup.
nsTArray<Element*> mObservationTargets;
// Value can be:
// -2: Makes sure next calculated threshold always differs, leading to a
// notification task being scheduled.
// -1: Non-intersecting.
// >= 0: Intersecting, valid index of aObserver->mThresholds.
enum ObservationState : int32_t { Uninitialized = -2, NotIntersecting = -1 };
nsTHashMap<Element*, int32_t> mObservationTargetMap;
nsTArray<RefPtr<DOMIntersectionObserverEntry>> mQueuedEntries;
bool mConnected = false;
};
} // namespace mozilla::dom
#endif
|