[IntersectionObserver] Fix tracking bookkeeping

R=chrishtr@chromium.org

Change-Id: Ibcab6aef237ce0d801efae344c784ce4e9d8f468
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1513077
Reviewed-by: Chris Harrelson <chrishtr@chromium.org>
Commit-Queue: Stefan Zager <szager@chromium.org>
Cr-Commit-Position: refs/heads/master@{#639717}
diff --git a/third_party/blink/renderer/core/intersection_observer/intersection_observation.cc b/third_party/blink/renderer/core/intersection_observer/intersection_observation.cc
index 68adefc..f83bf49 100644
--- a/third_party/blink/renderer/core/intersection_observer/intersection_observation.cc
+++ b/third_party/blink/renderer/core/intersection_observer/intersection_observation.cc
@@ -92,8 +92,14 @@
 
 void IntersectionObservation::Disconnect() {
   DCHECK(Observer());
-  if (target_)
+  if (target_) {
     Target()->EnsureIntersectionObserverData().RemoveObservation(*Observer());
+    if (target_->isConnected()) {
+      target_->GetDocument()
+          .EnsureIntersectionObserverController()
+          .RemoveTrackedTarget(*target_);
+    }
+  }
   entries_.clear();
   observer_.Clear();
 }
diff --git a/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.cc b/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.cc
index d64e134..df11be7 100644
--- a/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.cc
+++ b/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.cc
@@ -89,7 +89,6 @@
 }
 
 void IntersectionObserverController::RemoveTrackedTarget(Element& target) {
-  target.ComputeIntersectionObservations(false);
   tracked_observation_targets_.erase(&target);
 }
 
diff --git a/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h b/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h
index 7eb4650..9abfda8 100644
--- a/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h
+++ b/third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h
@@ -49,6 +49,9 @@
   const char* NameInHeapSnapshot() const override {
     return "IntersectionObserverController";
   }
+  unsigned GetTrackedTargetCountForTesting() const {
+    return tracked_observation_targets_.size();
+  }
 
  private:
   void PostTaskToDeliverObservations();
diff --git a/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc b/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc
index bb96b98..b4c099e 100644
--- a/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc
+++ b/third_party/blink/renderer/core/intersection_observer/intersection_observer_test.cc
@@ -7,6 +7,7 @@
 #include "third_party/blink/renderer/core/dom/element.h"
 #include "third_party/blink/renderer/core/exported/web_view_impl.h"
 #include "third_party/blink/renderer/core/frame/local_frame_view.h"
+#include "third_party/blink/renderer/core/intersection_observer/intersection_observer_controller.h"
 #include "third_party/blink/renderer/core/intersection_observer/intersection_observer_delegate.h"
 #include "third_party/blink/renderer/core/intersection_observer/intersection_observer_init.h"
 #include "third_party/blink/renderer/core/layout/layout_view.h"
@@ -339,6 +340,10 @@
   Element* target = GetDocument().getElementById("target");
   ASSERT_TRUE(target);
   observer->observe(target, exception_state);
+  EXPECT_EQ(GetDocument()
+                .EnsureIntersectionObserverController()
+                .GetTrackedTargetCountForTesting(),
+            1u);
 
   Compositor().BeginFrame();
   test::RunPendingTasks();
@@ -350,6 +355,10 @@
                                                           kProgrammaticScroll);
   Compositor().BeginFrame();
   observer->disconnect();
+  EXPECT_EQ(GetDocument()
+                .EnsureIntersectionObserverController()
+                .GetTrackedTargetCountForTesting(),
+            0u);
   test::RunPendingTasks();
   EXPECT_EQ(observer_delegate->CallCount(), 1);
 }