File: DOMIntersectionObserver.cpp

package info (click to toggle)
firefox 143.0.3-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,617,328 kB
  • sloc: cpp: 7,478,492; javascript: 6,417,157; ansic: 3,720,058; python: 1,396,372; xml: 627,523; asm: 438,677; java: 186,156; sh: 63,477; makefile: 19,171; objc: 13,059; perl: 12,983; yacc: 4,583; cs: 3,846; pascal: 3,405; lex: 1,720; ruby: 1,003; exp: 762; php: 436; lisp: 258; awk: 247; sql: 66; sed: 53; csh: 10
file content (973 lines) | stat: -rw-r--r-- 38,347 bytes parent folder | download
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
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
/* -*- 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/. */

#include "DOMIntersectionObserver.h"

#include "Units.h"
#include "mozilla/PresShell.h"
#include "mozilla/ScrollContainerFrame.h"
#include "mozilla/ServoBindings.h"
#include "mozilla/StaticPrefs_dom.h"
#include "mozilla/StaticPrefs_layout.h"
#include "mozilla/dom/BrowserChild.h"
#include "mozilla/dom/BrowsingContext.h"
#include "mozilla/dom/DocumentInlines.h"
#include "mozilla/dom/HTMLIFrameElement.h"
#include "mozilla/dom/HTMLImageElement.h"
#include "nsCSSPropertyID.h"
#include "nsContainerFrame.h"
#include "nsContentUtils.h"
#include "nsIFrame.h"
#include "nsLayoutUtils.h"
#include "nsRefreshDriver.h"

namespace mozilla::dom {

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMIntersectionObserverEntry)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMIntersectionObserverEntry)
NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMIntersectionObserverEntry)

NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(DOMIntersectionObserverEntry, mOwner,
                                      mRootBounds, mBoundingClientRect,
                                      mIntersectionRect, mTarget)

NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(DOMIntersectionObserver)
  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  NS_INTERFACE_MAP_ENTRY(nsISupports)
  NS_INTERFACE_MAP_ENTRY(DOMIntersectionObserver)
NS_INTERFACE_MAP_END

NS_IMPL_CYCLE_COLLECTING_ADDREF(DOMIntersectionObserver)
NS_IMPL_CYCLE_COLLECTING_RELEASE(DOMIntersectionObserver)

NS_IMPL_CYCLE_COLLECTION_CLASS(DOMIntersectionObserver)

NS_IMPL_CYCLE_COLLECTION_TRACE_BEGIN(DOMIntersectionObserver)
  NS_IMPL_CYCLE_COLLECTION_TRACE_PRESERVED_WRAPPER
NS_IMPL_CYCLE_COLLECTION_TRACE_END

NS_IMPL_CYCLE_COLLECTION_UNLINK_BEGIN(DOMIntersectionObserver)
  NS_IMPL_CYCLE_COLLECTION_UNLINK_PRESERVED_WRAPPER
  tmp->Disconnect();
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mOwner)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mDocument)
  if (tmp->mCallback.is<RefPtr<dom::IntersectionCallback>>()) {
    ImplCycleCollectionUnlink(
        tmp->mCallback.as<RefPtr<dom::IntersectionCallback>>());
  }
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mRoot)
  NS_IMPL_CYCLE_COLLECTION_UNLINK(mQueuedEntries)
NS_IMPL_CYCLE_COLLECTION_UNLINK_END

NS_IMPL_CYCLE_COLLECTION_TRAVERSE_BEGIN(DOMIntersectionObserver)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mOwner)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mDocument)
  if (tmp->mCallback.is<RefPtr<dom::IntersectionCallback>>()) {
    ImplCycleCollectionTraverse(
        cb, tmp->mCallback.as<RefPtr<dom::IntersectionCallback>>(), "mCallback",
        0);
  }
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mRoot)
  NS_IMPL_CYCLE_COLLECTION_TRAVERSE(mQueuedEntries)
NS_IMPL_CYCLE_COLLECTION_TRAVERSE_END

DOMIntersectionObserver::DOMIntersectionObserver(
    already_AddRefed<nsPIDOMWindowInner>&& aOwner,
    dom::IntersectionCallback& aCb)
    : mOwner(aOwner),
      mDocument(mOwner->GetExtantDoc()),
      mCallback(RefPtr<dom::IntersectionCallback>(&aCb)) {}

already_AddRefed<DOMIntersectionObserver> DOMIntersectionObserver::Constructor(
    const GlobalObject& aGlobal, dom::IntersectionCallback& aCb,
    ErrorResult& aRv) {
  return Constructor(aGlobal, aCb, IntersectionObserverInit(), aRv);
}

// https://w3c.github.io/IntersectionObserver/#initialize-new-intersection-observer
already_AddRefed<DOMIntersectionObserver> DOMIntersectionObserver::Constructor(
    const GlobalObject& aGlobal, dom::IntersectionCallback& aCb,
    const IntersectionObserverInit& aOptions, ErrorResult& aRv) {
  nsCOMPtr<nsPIDOMWindowInner> window =
      do_QueryInterface(aGlobal.GetAsSupports());
  if (!window) {
    aRv.Throw(NS_ERROR_FAILURE);
    return nullptr;
  }

  // 1. Let this be a new IntersectionObserver object
  // 2. Set this’s internal [[callback]] slot to callback.
  RefPtr<DOMIntersectionObserver> observer =
      new DOMIntersectionObserver(window.forget(), aCb);

  if (!aOptions.mRoot.IsNull()) {
    if (aOptions.mRoot.Value().IsElement()) {
      observer->mRoot = aOptions.mRoot.Value().GetAsElement();
    } else {
      MOZ_ASSERT(aOptions.mRoot.Value().IsDocument());
      observer->mRoot = aOptions.mRoot.Value().GetAsDocument();
    }
  }

  // 3. Attempt to parse a margin from options.rootMargin. If a list is
  // returned, set this’s internal [[rootMargin]] slot to that. Otherwise, throw
  // a SyntaxError exception.
  if (!observer->SetRootMargin(aOptions.mRootMargin)) {
    aRv.ThrowSyntaxError("rootMargin must be specified in pixels or percent.");
    return nullptr;
  }

  // 4. Attempt to parse a margin from options.scrollMargin. If a list is
  // returned, set this’s internal [[scrollMargin]] slot to that. Otherwise,
  // throw a SyntaxError exception.
  if (!observer->SetScrollMargin(aOptions.mScrollMargin)) {
    aRv.ThrowSyntaxError(
        "scrollMargin must be specified in pixels or percent.");
    return nullptr;
  }

  // 5. Let thresholds be a list equal to options.threshold.
  if (aOptions.mThreshold.IsDoubleSequence()) {
    const Sequence<double>& thresholds =
        aOptions.mThreshold.GetAsDoubleSequence();
    observer->mThresholds.SetCapacity(thresholds.Length());

    // 6. If any value in thresholds is less than 0.0 or greater than 1.0, throw
    // a RangeError exception.
    for (const auto& thresh : thresholds) {
      if (thresh < 0.0 || thresh > 1.0) {
        aRv.ThrowRangeError<dom::MSG_THRESHOLD_RANGE_ERROR>();
        return nullptr;
      }
      observer->mThresholds.AppendElement(thresh);
    }

    // 7. Sort thresholds in ascending order.
    observer->mThresholds.Sort();

    // 8. If thresholds is empty, append 0 to thresholds.
    if (observer->mThresholds.IsEmpty()) {
      observer->mThresholds.AppendElement(0.0);
    }
  } else {
    double thresh = aOptions.mThreshold.GetAsDouble();
    if (thresh < 0.0 || thresh > 1.0) {
      aRv.ThrowRangeError<dom::MSG_THRESHOLD_RANGE_ERROR>();
      return nullptr;
    }
    observer->mThresholds.AppendElement(thresh);
  }

  // 9. The thresholds attribute getter will return this sorted thresholds list.
  // (This is implicit given `observer->mThresholds`)

  // 10. Let delay be the value of options.delay.
  // TODO: https://bugzilla.mozilla.org/show_bug.cgi?id=1896900

  // 11. If options.trackVisibility is true and delay is less than 100, set
  // delay to 100.
  // TODO: https://bugzilla.mozilla.org/show_bug.cgi?id=1896900

  // 12. Set this’s internal [[delay]] slot to options.delay to delay.
  // TODO: https://bugzilla.mozilla.org/show_bug.cgi?id=1896900

  // 13. Set this’s internal [[trackVisibility]] slot to
  // options.trackVisibility.
  // TODO: https://bugzilla.mozilla.org/show_bug.cgi?id=1896900

  // 14. Return this.
  return observer.forget();
}

static void LazyLoadCallback(
    const Sequence<OwningNonNull<DOMIntersectionObserverEntry>>& aEntries) {
  for (const auto& entry : aEntries) {
    Element* target = entry->Target();
    if (entry->IsIntersecting()) {
      if (auto* image = HTMLImageElement::FromNode(target)) {
        image->StopLazyLoading();
      } else if (auto* iframe = HTMLIFrameElement::FromNode(target)) {
        iframe->StopLazyLoading();
      } else {
        MOZ_ASSERT_UNREACHABLE(
            "Only <img> and <iframe> should be observed by lazy load observer");
      }
    }
  }
}

static LengthPercentage PrefMargin(float aValue, bool aIsPercentage) {
  return aIsPercentage ? LengthPercentage::FromPercentage(aValue / 100.0f)
                       : LengthPercentage::FromPixels(aValue);
}

IntersectionObserverMargin DOMIntersectionObserver::LazyLoadingRootMargin() {
  IntersectionObserverMargin margin;
#define SET_MARGIN(side_, side_lower_)                                 \
  margin.Get(eSide##side_) = PrefMargin(                               \
      StaticPrefs::dom_image_lazy_loading_root_margin_##side_lower_(), \
      StaticPrefs::                                                    \
          dom_image_lazy_loading_root_margin_##side_lower_##_percentage());
  SET_MARGIN(Top, top);
  SET_MARGIN(Right, right);
  SET_MARGIN(Bottom, bottom);
  SET_MARGIN(Left, left);
#undef SET_MARGIN
  return margin;
}

DOMIntersectionObserver::DOMIntersectionObserver(Document& aDocument,
                                                 NativeCallback aCallback)
    : mOwner(aDocument.GetInnerWindow()),
      mDocument(&aDocument),
      mCallback(aCallback) {}

already_AddRefed<DOMIntersectionObserver>
DOMIntersectionObserver::CreateLazyLoadObserver(Document& aDocument) {
  RefPtr<DOMIntersectionObserver> observer =
      new DOMIntersectionObserver(aDocument, LazyLoadCallback);
  observer->mThresholds.AppendElement(0.0f);
  observer->mRootMargin = LazyLoadingRootMargin();
  return observer.forget();
}

bool DOMIntersectionObserver::SetRootMargin(const nsACString& aString) {
  return Servo_IntersectionObserverMargin_Parse(&aString, &mRootMargin);
}

bool DOMIntersectionObserver::SetScrollMargin(const nsACString& aString) {
  return Servo_IntersectionObserverMargin_Parse(&aString, &mScrollMargin);
}

nsISupports* DOMIntersectionObserver::GetParentObject() const { return mOwner; }

void DOMIntersectionObserver::GetRootMargin(nsACString& aRetVal) {
  Servo_IntersectionObserverMargin_ToString(&mRootMargin, &aRetVal);
}

void DOMIntersectionObserver::GetScrollMargin(nsACString& aRetVal) {
  Servo_IntersectionObserverMargin_ToString(&mScrollMargin, &aRetVal);
}

void DOMIntersectionObserver::GetThresholds(nsTArray<double>& aRetVal) {
  aRetVal = mThresholds.Clone();
}

// https://w3c.github.io/IntersectionObserver/#observe-target-element
void DOMIntersectionObserver::Observe(Element& aTarget) {
  // 1. If target is in observer’s internal [[ObservationTargets]] slot, return.
  bool wasPresent =
      mObservationTargetMap.WithEntryHandle(&aTarget, [](auto handle) {
        if (handle.HasEntry()) {
          return true;
        }
        handle.Insert(Uninitialized);
        return false;
      });
  if (wasPresent) {
    return;
  }

  // 2. Let intersectionObserverRegistration be an
  // IntersectionObserverRegistration record with an observer property set to
  // observer, a previousThresholdIndex property set to -1, a
  // previousIsIntersecting property set to false, and a previousIsVisible
  // property set to false.
  // 3. Append intersectionObserverRegistration to target’s internal
  // [[RegisteredIntersectionObservers]] slot.
  // 4. Add target to observer’s internal [[ObservationTargets]] slot.
  aTarget.BindObject(this, [](nsISupports* aObserver, nsINode* aTarget) {
    static_cast<DOMIntersectionObserver*>(aObserver)->UnlinkTarget(
        *aTarget->AsElement());
  });
  mObservationTargets.AppendElement(&aTarget);

  MOZ_ASSERT(mObservationTargets.Length() == mObservationTargetMap.Count());

  Connect();
  if (mDocument) {
    if (nsPresContext* pc = mDocument->GetPresContext()) {
      pc->RefreshDriver()->EnsureIntersectionObservationsUpdateHappens();
    }
  }
}

// https://w3c.github.io/IntersectionObserver/#unobserve-target-element
void DOMIntersectionObserver::Unobserve(Element& aTarget) {
  // 1. Remove the IntersectionObserverRegistration record whose observer
  // property is equal to this from target’s internal
  // [[RegisteredIntersectionObservers]] slot, if present.
  if (!mObservationTargetMap.Remove(&aTarget)) {
    return;
  }

  // 2. Remove target from this’s internal [[ObservationTargets]] slot, if
  // present
  mObservationTargets.RemoveElement(&aTarget);

  aTarget.UnbindObject(this);

  MOZ_ASSERT(mObservationTargets.Length() == mObservationTargetMap.Count());

  if (mObservationTargets.IsEmpty()) {
    Disconnect();
  }
}

// Inner step of
// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-disconnect
void DOMIntersectionObserver::UnlinkTarget(Element& aTarget) {
  // 1. Remove the IntersectionObserverRegistration record whose observer
  // property is equal to this from target’s internal
  // [[RegisteredIntersectionObservers]] slot.
  // 2. Remove target from this’s internal [[ObservationTargets]] slot.
  mObservationTargets.RemoveElement(&aTarget);
  mObservationTargetMap.Remove(&aTarget);

  if (mObservationTargets.IsEmpty()) {
    Disconnect();
  }
}

void DOMIntersectionObserver::Connect() {
  if (mConnected) {
    return;
  }

  mConnected = true;
  if (mDocument) {
    mDocument->AddIntersectionObserver(*this);
  }
}

// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-disconnect
void DOMIntersectionObserver::Disconnect() {
  if (!mConnected) {
    return;
  }

  mConnected = false;
  // 1. For each target in this’s internal [[ObservationTargets]] slot:
  for (Element* target : mObservationTargets) {
    // 2. Remove the IntersectionObserverRegistration record whose observer
    // property is equal to this from target’s internal
    // [[RegisteredIntersectionObservers]] slot.
    // 3. Remove target from this’s internal [[ObservationTargets]] slot.
    target->UnbindObject(this);
  }

  mObservationTargets.Clear();
  mObservationTargetMap.Clear();
  if (mDocument) {
    mDocument->RemoveIntersectionObserver(*this);
  }
}

// https://w3c.github.io/IntersectionObserver/#dom-intersectionobserver-takerecords
void DOMIntersectionObserver::TakeRecords(
    nsTArray<RefPtr<DOMIntersectionObserverEntry>>& aRetVal) {
  aRetVal = std::move(mQueuedEntries);
}

enum class BrowsingContextOrigin { Similar, Different };

// NOTE(emilio): Checking docgroup as per discussion in:
// https://github.com/w3c/IntersectionObserver/issues/161
static BrowsingContextOrigin SimilarOrigin(const Element& aTarget,
                                           const nsINode* aRoot) {
  if (!aRoot) {
    return BrowsingContextOrigin::Different;
  }
  return aTarget.OwnerDoc()->GetDocGroup() == aRoot->OwnerDoc()->GetDocGroup()
             ? BrowsingContextOrigin::Similar
             : BrowsingContextOrigin::Different;
}

// NOTE: This returns nullptr if |aDocument| is in another process from the top
// level content document.
static const Document* GetTopLevelContentDocumentInThisProcess(
    const Document& aDocument) {
  auto* wc = aDocument.GetTopLevelWindowContext();
  return wc ? wc->GetExtantDoc() : nullptr;
}

static nsMargin ResolveMargin(const IntersectionObserverMargin& aMargin,
                              const nsSize& aPercentBasis) {
  nsMargin margin;
  for (const auto side : mozilla::AllPhysicalSides()) {
    nscoord basis = side == eSideTop || side == eSideBottom
                        ? aPercentBasis.Height()
                        : aPercentBasis.Width();
    margin.Side(side) = aMargin.Get(side).Resolve(
        basis, static_cast<nscoord (*)(float)>(NSToCoordRoundWithClamp));
  }
  return margin;
}

// https://w3c.github.io/IntersectionObserver/#compute-the-intersection
//
// TODO(emilio): Proof of this being equivalent to the spec welcome, seems
// reasonably close.
//
// Also, it's unclear to me why the spec talks about browsing context while
// discarding observations of targets of different documents.
//
// Both aRootBounds and the return value are relative to
// nsLayoutUtils::GetContainingBlockForClientRect(aRoot).
//
// In case of out-of-process document, aRemoteDocumentVisibleRect is a rectangle
// in the out-of-process document's coordinate system.
static Maybe<nsRect> ComputeTheIntersection(
    nsIFrame* aTarget, const nsRect& aTargetRectRelativeToTarget,
    nsIFrame* aRoot, const nsRect& aRootBounds,
    const IntersectionObserverMargin& aScrollMargin,
    const Maybe<nsRect>& aRemoteDocumentVisibleRect,
    DOMIntersectionObserver::IsForProximityToViewport
        aIsForProximityToViewport) {
  nsIFrame* target = aTarget;
  // 1. Let intersectionRect be the result of running the
  // getBoundingClientRect() algorithm on the target.
  //
  // `intersectionRect` is kept relative to `target` during the loop.
  auto inflowRect = aTargetRectRelativeToTarget;
  Maybe<nsRect> intersectionRect = Some(inflowRect);

  // 2. Let container be the containing block of the target.
  // (We go through the parent chain and only look at scroll frames)
  //
  // FIXME(emilio): Spec uses containing blocks, we use scroll frames, but we
  // only apply overflow-clipping, not clip-path, so it's ~fine. We do need to
  // apply clip-path.
  //
  // 3. While container is not the intersection root:
  nsIFrame* containerFrame =
      nsLayoutUtils::GetCrossDocParentFrameInProcess(target);
  while (containerFrame && containerFrame != aRoot) {
    // FIXME(emilio): What about other scroll frames that inherit from
    // ScrollContainerFrame but have a different type, like nsListControlFrame?
    // This looks bogus in that case, but different bug.
    if (ScrollContainerFrame* scrollContainerFrame =
            do_QueryFrame(containerFrame)) {
      if (containerFrame->GetParent() == aRoot && !aRoot->GetParent()) {
        // This is subtle: if we're computing the intersection against the
        // viewport (the root frame), and this is its scroll frame, we really
        // want to skip this intersection (because we want to account for the
        // root margin, which is already in aRootBounds).
        break;
      }
      nsRect subFrameRect =
          scrollContainerFrame->GetScrollPortRectAccountingForDynamicToolbar();

      // 3.1 If container is the document of a nested browsing context, update
      // intersectionRect by clipping to the viewport of the document, and
      // update container to be the browsing context container of container.
      // XXX: Handled below by aRemoteDocumentVisibleRect & walking
      // CrossDocParentFrame.

      // 3.2 Map intersectionRect to the coordinate space of container.
      nsRect intersectionRectRelativeToContainer =
          nsLayoutUtils::TransformFrameRectToAncestor(
              target, intersectionRect.value(), containerFrame);

      // 3.3 If container is a scroll container, apply the
      // IntersectionObserver’s [[scrollMargin]] to the container’s clip rect as
      // described in apply scroll margin to a scrollport.
      subFrameRect.Inflate(ResolveMargin(aScrollMargin, subFrameRect.Size()));

      intersectionRect =
          intersectionRectRelativeToContainer.EdgeInclusiveIntersection(
              subFrameRect);
      if (!intersectionRect) {
        return Nothing();
      }
      target = containerFrame;
    } else {
      const auto& disp = *containerFrame->StyleDisplay();
      auto clipAxes = containerFrame->ShouldApplyOverflowClipping(&disp);
      // 3.4 If container has a content clip or a css clip-path property, update
      // intersectionRect by applying container’s clip.

      // 3.4 TODO: Apply clip-path.
      if (!clipAxes.isEmpty()) {
        // 3.2 Map intersectionRect to the coordinate space of container.
        const nsRect intersectionRectRelativeToContainer =
            nsLayoutUtils::TransformFrameRectToAncestor(
                target, intersectionRect.value(), containerFrame);
        const nsRect clipRect = OverflowAreas::GetOverflowClipRect(
            intersectionRectRelativeToContainer,
            containerFrame->GetRectRelativeToSelf(), clipAxes,
            containerFrame->OverflowClipMargin(clipAxes));
        intersectionRect =
            intersectionRectRelativeToContainer.EdgeInclusiveIntersection(
                clipRect);
        if (!intersectionRect) {
          return Nothing();
        }
        target = containerFrame;
      }
    }
    // 3.5 If container is the root element of a browsing context, update
    // container to be the browsing context’s document; otherwise, update
    // container to be the containing block of container.
    containerFrame =
        nsLayoutUtils::GetCrossDocParentFrameInProcess(containerFrame);
  }
  MOZ_ASSERT(intersectionRect);

  // 4. Map intersectionRect to the coordinate space of the intersection root.
  nsRect intersectionRectRelativeToRoot =
      nsLayoutUtils::TransformFrameRectToAncestor(
          target, intersectionRect.value(),
          nsLayoutUtils::GetContainingBlockForClientRect(aRoot));

  // 5.Update intersectionRect by intersecting it with the root intersection
  // rectangle.
  intersectionRect =
      intersectionRectRelativeToRoot.EdgeInclusiveIntersection(aRootBounds);
  if (intersectionRect.isNothing()) {
    return Nothing();
  }
  // 6. Map intersectionRect to the coordinate space of the viewport of the
  // Document containing the target.
  //
  // FIXME(emilio): I think this may not be correct if the root is explicit
  // and in the same document, since then the rectangle may not be relative to
  // the viewport already (but it's in the same document).
  nsRect rect = intersectionRect.value();
  if (aTarget->PresContext() != aRoot->PresContext()) {
    if (nsIFrame* rootScrollContainerFrame =
            aTarget->PresShell()->GetRootScrollContainerFrame()) {
      nsLayoutUtils::TransformRect(aRoot, rootScrollContainerFrame, rect);
    }
  }

  // In out-of-process iframes we need to take an intersection with the remote
  // document visible rect which was already clipped by ancestor document's
  // viewports.
  if (aRemoteDocumentVisibleRect) {
    MOZ_ASSERT(aRoot->PresContext()->IsRootContentDocumentInProcess() &&
               !aRoot->PresContext()->IsRootContentDocumentCrossProcess());

    intersectionRect =
        rect.EdgeInclusiveIntersection(*aRemoteDocumentVisibleRect);
    if (intersectionRect.isNothing()) {
      return Nothing();
    }
    rect = intersectionRect.value();
  }

  // 7. Return intersectionRect.
  return Some(rect);
}

struct OopIframeMetrics {
  nsIFrame* mInProcessRootFrame = nullptr;
  nsRect mInProcessRootRect;
  nsRect mRemoteDocumentVisibleRect;
};

static Maybe<OopIframeMetrics> GetOopIframeMetrics(
    const Document& aDocument, const Document* aRootDocument) {
  const Document* rootDoc =
      nsContentUtils::GetInProcessSubtreeRootDocument(&aDocument);
  MOZ_ASSERT(rootDoc);

  if (rootDoc->IsTopLevelContentDocument()) {
    return Nothing();
  }

  if (aRootDocument &&
      rootDoc ==
          nsContentUtils::GetInProcessSubtreeRootDocument(aRootDocument)) {
    // aRootDoc, if non-null, is either the implicit root
    // (top-level-content-document) or a same-origin document passed explicitly.
    //
    // In the former case, we should've returned above if there are no iframes
    // in between. This condition handles the explicit, same-origin root
    // document, when both are embedded in an OOP iframe.
    return Nothing();
  }

  PresShell* rootPresShell = rootDoc->GetPresShell();
  if (!rootPresShell || rootPresShell->IsDestroying()) {
    return Some(OopIframeMetrics{});
  }

  nsIFrame* inProcessRootFrame = rootPresShell->GetRootFrame();
  if (!inProcessRootFrame) {
    return Some(OopIframeMetrics{});
  }

  BrowserChild* browserChild = BrowserChild::GetFrom(rootDoc->GetDocShell());
  if (!browserChild) {
    return Some(OopIframeMetrics{});
  }

  if (MOZ_UNLIKELY(NS_WARN_IF(browserChild->IsTopLevel()))) {
    // FIXME(bug 1772083): This can be hit with popups, e.g. in
    // html/browsers/the-window-object/open-close/no_window_open_when_term_nesting_level_nonzero.window.html
    // temporarily while opening a new popup (on the about:blank doc).
    // MOZ_ASSERT_UNREACHABLE("Top level BrowserChild but non-top level doc?");
    return Nothing();
  }

  nsRect inProcessRootRect;
  if (ScrollContainerFrame* rootScrollContainerFrame =
          rootPresShell->GetRootScrollContainerFrame()) {
    inProcessRootRect = rootScrollContainerFrame
                            ->GetScrollPortRectAccountingForDynamicToolbar();
  }

  Maybe<LayoutDeviceRect> remoteDocumentVisibleRect =
      browserChild->GetTopLevelViewportVisibleRectInSelfCoords();
  if (!remoteDocumentVisibleRect) {
    return Some(OopIframeMetrics{});
  }

  return Some(OopIframeMetrics{
      inProcessRootFrame,
      inProcessRootRect,
      LayoutDeviceRect::ToAppUnits(
          *remoteDocumentVisibleRect,
          rootPresShell->GetPresContext()->AppUnitsPerDevPixel()),
  });
}

// https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo
// step 2.1
IntersectionInput DOMIntersectionObserver::ComputeInput(
    const Document& aDocument, const nsINode* aRoot,
    const IntersectionObserverMargin* aRootMargin,
    const IntersectionObserverMargin* aScrollMargin) {
  // 1 - Let rootBounds be observer's root intersection rectangle.
  //  ... but since the intersection rectangle depends on the target, we defer
  //      the inflation until later.
  // NOTE: |rootRect| and |rootFrame| will be root in the same process. In
  // out-of-process iframes, they are NOT root ones of the top level content
  // document.
  nsRect rootRect;
  nsIFrame* rootFrame = nullptr;
  const nsINode* root = aRoot;
  const bool isImplicitRoot = !aRoot;
  Maybe<nsRect> remoteDocumentVisibleRect;
  if (aRoot && aRoot->IsElement()) {
    if ((rootFrame = aRoot->AsElement()->GetPrimaryFrame())) {
      nsRect rootRectRelativeToRootFrame;
      if (ScrollContainerFrame* scrollContainerFrame =
              do_QueryFrame(rootFrame)) {
        // rootRectRelativeToRootFrame should be the content rect of rootFrame,
        // not including the scrollbars.
        rootRectRelativeToRootFrame =
            scrollContainerFrame
                ->GetScrollPortRectAccountingForDynamicToolbar();
      } else {
        // rootRectRelativeToRootFrame should be the border rect of rootFrame.
        rootRectRelativeToRootFrame = rootFrame->GetRectRelativeToSelf();
      }
      nsIFrame* containingBlock =
          nsLayoutUtils::GetContainingBlockForClientRect(rootFrame);
      rootRect = nsLayoutUtils::TransformFrameRectToAncestor(
          rootFrame, rootRectRelativeToRootFrame, containingBlock);
    }
  } else {
    MOZ_ASSERT(!aRoot || aRoot->IsDocument());
    const Document* rootDocument =
        aRoot ? aRoot->AsDocument()
              : GetTopLevelContentDocumentInThisProcess(aDocument);
    root = rootDocument;

    if (rootDocument) {
      // We're in the same process as the root document, though note that there
      // could be an out-of-process iframe in between us and the root. Grab the
      // root frame and the root rect.
      //
      // Note that the root rect is always good (we assume no DPI changes in
      // between the two documents, and we don't need to convert coordinates).
      //
      // The root frame however we may need to tweak in the block below, if
      // there's any OOP iframe in between `rootDocument` and `aDocument`, to
      // handle the OOP iframe positions.
      if (PresShell* presShell = rootDocument->GetPresShell()) {
        rootFrame = presShell->GetRootFrame();
        // We use the root scroll container frame's scroll port to account the
        // scrollbars in rootRect, if needed.
        if (ScrollContainerFrame* rootScrollContainerFrame =
                presShell->GetRootScrollContainerFrame()) {
          rootRect = rootScrollContainerFrame
                         ->GetScrollPortRectAccountingForDynamicToolbar();
        } else if (rootFrame) {
          rootRect = rootFrame->GetRectRelativeToSelf();
        }
      }
    }

    if (Maybe<OopIframeMetrics> metrics =
            GetOopIframeMetrics(aDocument, rootDocument)) {
      rootFrame = metrics->mInProcessRootFrame;
      if (!rootDocument) {
        rootRect = metrics->mInProcessRootRect;
      }
      remoteDocumentVisibleRect = Some(metrics->mRemoteDocumentVisibleRect);
    }
  }

  nsMargin rootMargin;  // This root margin is NOT applied in `implicit root`
                        // case, e.g. in out-of-process iframes.
  if (aRootMargin) {
    rootMargin = ResolveMargin(*aRootMargin, rootRect.Size());
  }

  return {isImplicitRoot,
          root,
          rootFrame,
          rootRect,
          rootMargin,
          aScrollMargin ? *aScrollMargin : IntersectionObserverMargin(),
          remoteDocumentVisibleRect};
}

// https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo
// (steps 2.1 - 2.5)
IntersectionOutput DOMIntersectionObserver::Intersect(
    const IntersectionInput& aInput, const Element& aTarget, BoxToUse aBoxToUse,
    IsForProximityToViewport aIsForProximityToViewport) {
  const bool isSimilarOrigin = SimilarOrigin(aTarget, aInput.mRootNode) ==
                               BrowsingContextOrigin::Similar;
  nsIFrame* targetFrame = aTarget.GetPrimaryFrame();
  if (!targetFrame || !aInput.mRootFrame) {
    return {isSimilarOrigin};
  }

  // "From the perspective of an IntersectionObserver, the skipped contents
  // of an element are never intersecting the intersection root. This is
  // true even if both the root and the target elements are in the skipped
  // contents."
  // https://drafts.csswg.org/css-contain/#cv-notes
  //
  // Skip the intersection if the element is hidden, unless this is the
  // specifically to determine the proximity to the viewport for
  // `content-visibility: auto` elements.
  if (aIsForProximityToViewport == IsForProximityToViewport::No &&
      targetFrame->IsHiddenByContentVisibilityOnAnyAncestor()) {
    return {isSimilarOrigin};
  }

  // 2.2. If the intersection root is not the implicit root, and target is
  // not in the same Document as the intersection root, skip to step 11.
  if (!aInput.mIsImplicitRoot &&
      aInput.mRootNode->OwnerDoc() != aTarget.OwnerDoc()) {
    return {isSimilarOrigin};
  }

  // 2.3. If the intersection root is an element and target is not a descendant
  // of the intersection root in the containing block chain, skip to step 11.
  //
  // NOTE(emilio): We also do this if target is the implicit root, pending
  // clarification in
  // https://github.com/w3c/IntersectionObserver/issues/456.
  if (aInput.mRootFrame == targetFrame ||
      !nsLayoutUtils::IsAncestorFrameCrossDocInProcess(aInput.mRootFrame,
                                                       targetFrame)) {
    return {isSimilarOrigin};
  }

  nsRect rootBounds = aInput.mRootRect;
  if (isSimilarOrigin) {
    rootBounds.Inflate(aInput.mRootMargin);

    // Implicit roots should apply the scrollMargin as well:
    if (aInput.mIsImplicitRoot) {
      rootBounds.Inflate(
          ResolveMargin(aInput.mScrollMargin, aInput.mRootRect.Size()));
    }
  }

  // 2.4. Set targetRect to the DOMRectReadOnly obtained by running the
  // getBoundingClientRect() algorithm on target. We compute the box relative to
  // self first, then transform.
  nsLayoutUtils::GetAllInFlowRectsFlags flags{
      nsLayoutUtils::GetAllInFlowRectsFlag::AccountForTransforms};
  if (aBoxToUse == BoxToUse::Content) {
    flags += nsLayoutUtils::GetAllInFlowRectsFlag::UseContentBox;
  }
  nsRect targetRectRelativeToTarget =
      nsLayoutUtils::GetAllInFlowRectsUnion(targetFrame, targetFrame, flags);

  if (aBoxToUse == BoxToUse::OverflowClip) {
    const auto& disp = *targetFrame->StyleDisplay();
    auto clipAxes = targetFrame->ShouldApplyOverflowClipping(&disp);
    if (!clipAxes.isEmpty()) {
      targetRectRelativeToTarget = OverflowAreas::GetOverflowClipRect(
          targetRectRelativeToTarget, targetRectRelativeToTarget, clipAxes,
          targetFrame->OverflowClipMargin(clipAxes));
    }
  }

  auto targetRect = nsLayoutUtils::TransformFrameRectToAncestor(
      targetFrame, targetRectRelativeToTarget,
      nsLayoutUtils::GetContainingBlockForClientRect(targetFrame));

  // For content-visibility, we need to observe the overflow clip edge,
  // https://drafts.csswg.org/css-contain-2/#close-to-the-viewport
  MOZ_ASSERT_IF(aIsForProximityToViewport == IsForProximityToViewport::Yes,
                aBoxToUse == BoxToUse::OverflowClip);

  // 2.5. Let intersectionRect be the result of running the compute the
  // intersection algorithm on target and observer’s intersection root.
  Maybe<nsRect> intersectionRect = ComputeTheIntersection(
      targetFrame, targetRectRelativeToTarget, aInput.mRootFrame, rootBounds,
      aInput.mScrollMargin, aInput.mRemoteDocumentVisibleRect,
      aIsForProximityToViewport);

  return {isSimilarOrigin, rootBounds, targetRect, intersectionRect};
}

IntersectionOutput DOMIntersectionObserver::Intersect(
    const IntersectionInput& aInput, const nsRect& aTargetRect) {
  nsRect rootBounds = aInput.mRootRect;
  rootBounds.Inflate(aInput.mRootMargin);
  auto intersectionRect =
      aInput.mRootRect.EdgeInclusiveIntersection(aTargetRect);
  if (intersectionRect && aInput.mRemoteDocumentVisibleRect) {
    intersectionRect = intersectionRect->EdgeInclusiveIntersection(
        *aInput.mRemoteDocumentVisibleRect);
  }
  return {true, rootBounds, aTargetRect, intersectionRect};
}

// https://w3c.github.io/IntersectionObserver/#update-intersection-observations-algo
// (step 2)
void DOMIntersectionObserver::Update(Document& aDocument,
                                     DOMHighResTimeStamp time) {
  auto input = ComputeInput(aDocument, mRoot, &mRootMargin, &mScrollMargin);

  // 2. For each target in observer’s internal [[ObservationTargets]] slot,
  // processed in the same order that observe() was called on each target:
  for (Element* target : mObservationTargets) {
    // 2.1 - 2.4.
    IntersectionOutput output = Intersect(input, *target);

    // 2.5. Let targetArea be targetRect’s area.
    int64_t targetArea = (int64_t)output.mTargetRect.Width() *
                         (int64_t)output.mTargetRect.Height();

    // 2.6. Let intersectionArea be intersectionRect’s area.
    int64_t intersectionArea =
        !output.mIntersectionRect
            ? 0
            : (int64_t)output.mIntersectionRect->Width() *
                  (int64_t)output.mIntersectionRect->Height();

    // 2.7. Let isIntersecting be true if targetRect and rootBounds intersect or
    // are edge-adjacent, even if the intersection has zero area (because
    // rootBounds or targetRect have zero area); otherwise, let isIntersecting
    // be false.
    const bool isIntersecting = output.Intersects();

    // 2.8. If targetArea is non-zero, let intersectionRatio be intersectionArea
    // divided by targetArea. Otherwise, let intersectionRatio be 1 if
    // isIntersecting is true, or 0 if isIntersecting is false.
    double intersectionRatio;
    if (targetArea > 0.0) {
      intersectionRatio =
          std::min((double)intersectionArea / (double)targetArea, 1.0);
    } else {
      intersectionRatio = isIntersecting ? 1.0 : 0.0;
    }

    // 2.9 Let thresholdIndex be the index of the first entry in
    // observer.thresholds whose value is greater than intersectionRatio, or the
    // length of observer.thresholds if intersectionRatio is greater than or
    // equal to the last entry in observer.thresholds.
    int32_t thresholdIndex = -1;

    // If not intersecting, we can just shortcut, as we know that the thresholds
    // are always between 0 and 1.
    if (isIntersecting) {
      thresholdIndex = mThresholds.IndexOfFirstElementGt(intersectionRatio);
      if (thresholdIndex == 0) {
        // Per the spec, we should leave threshold at 0 and distinguish between
        // "less than all thresholds and intersecting" and "not intersecting"
        // (queuing observer entries as both cases come to pass). However,
        // neither Chrome nor the WPT tests expect this behavior, so treat these
        // two cases as one.
        //
        // See https://github.com/w3c/IntersectionObserver/issues/432 about
        // this.
        thresholdIndex = -1;
      }
    }

    // Steps 2.10 - 2.15.
    bool updated = false;
    if (auto entry = mObservationTargetMap.Lookup(target)) {
      updated = entry.Data() != thresholdIndex;
      entry.Data() = thresholdIndex;
    } else {
      MOZ_ASSERT_UNREACHABLE("Target not properly registered?");
    }

    if (updated) {
      // See https://github.com/w3c/IntersectionObserver/issues/432 about
      // why we use thresholdIndex > 0 rather than isIntersecting for the
      // entry's isIntersecting value.
      QueueIntersectionObserverEntry(
          target, time,
          output.mIsSimilarOrigin ? Some(output.mRootBounds) : Nothing(),
          output.mTargetRect, output.mIntersectionRect, thresholdIndex > 0,
          intersectionRatio);
    }
  }
}

void DOMIntersectionObserver::QueueIntersectionObserverEntry(
    Element* aTarget, DOMHighResTimeStamp time, const Maybe<nsRect>& aRootRect,
    const nsRect& aTargetRect, const Maybe<nsRect>& aIntersectionRect,
    bool aIsIntersecting, double aIntersectionRatio) {
  RefPtr<DOMRect> rootBounds;
  if (aRootRect.isSome()) {
    rootBounds = new DOMRect(mOwner);
    rootBounds->SetLayoutRect(aRootRect.value());
  }
  RefPtr<DOMRect> boundingClientRect = new DOMRect(mOwner);
  boundingClientRect->SetLayoutRect(aTargetRect);
  RefPtr<DOMRect> intersectionRect = new DOMRect(mOwner);
  if (aIntersectionRect.isSome()) {
    intersectionRect->SetLayoutRect(aIntersectionRect.value());
  }
  RefPtr<DOMIntersectionObserverEntry> entry = new DOMIntersectionObserverEntry(
      mOwner, time, rootBounds.forget(), boundingClientRect.forget(),
      intersectionRect.forget(), aIsIntersecting, aTarget, aIntersectionRatio);
  mQueuedEntries.AppendElement(entry.forget());
}

void DOMIntersectionObserver::Notify() {
  if (!mQueuedEntries.Length()) {
    return;
  }
  Sequence<OwningNonNull<DOMIntersectionObserverEntry>> entries;
  if (entries.SetCapacity(mQueuedEntries.Length(), mozilla::fallible)) {
    for (size_t i = 0; i < mQueuedEntries.Length(); ++i) {
      RefPtr<DOMIntersectionObserverEntry> next = mQueuedEntries[i];
      *entries.AppendElement(mozilla::fallible) = next;
    }
  }
  mQueuedEntries.Clear();

  if (mCallback.is<RefPtr<dom::IntersectionCallback>>()) {
    RefPtr<dom::IntersectionCallback> callback(
        mCallback.as<RefPtr<dom::IntersectionCallback>>());
    callback->Call(this, entries, *this);
  } else {
    mCallback.as<NativeCallback>()(entries);
  }
}

}  // namespace mozilla::dom