Reland "SequenceManager to perform periodic housekeeping (sweep canceled tasks)"

This reverts commit 23cd26b84ec8ea9d5d6ea54a8e903c5bade93346.

The fix (https://chromium-review.googlesource.com/c/chromium/src/+/1495541) has landed so it should be safe to reland.

Original change's description:
> Revert "SequenceManager to perform periodic housekeeping (sweep canceled tasks)"
> 
> This reverts commit 606087eb105f4586e9c1af653f8005960cc664dd.
> 
> This is crashing and the speculative fix was ineffective.
> 
> Original change's description:
> > SequenceManager to perform periodic housekeeping (sweep canceled tasks)
> >
> > Long delay canceled tasks can really pile up if we don't activly remove
> > them. We already do that in the renderer but we need to bring this
> > technology to other SequenceMangers.
> >
> > Bug: 863341
> > Change-Id: Ie16674043028090f94c184c02acbef6efcf3471b
> > Reviewed-on: https://chromium-review.googlesource.com/c/1483076
> > Commit-Queue: Alex Clarke <alexclarke@chromium.org>
> > Reviewed-by: Sami Kyöstilä <skyostil@chromium.org>
> > Reviewed-by: Alexander Timin <altimin@chromium.org>
> > Reviewed-by: Alex Clarke <alexclarke@chromium.org>
> > Cr-Commit-Position: refs/heads/master@{#635497}
> 
> TBR=skyostil@chromium.org,alexclarke@chromium.org,altimin@chromium.org,carlscab@google.com
> 
> # Not skipping CQ checks because original CL landed > 1 day ago.
> 
> Bug: 863341, 936734
> Change-Id: Ie948d9519dee14326137ad04aa88e53207db6eed
> Reviewed-on: https://chromium-review.googlesource.com/c/1495976
> Reviewed-by: Alex Clarke <alexclarke@chromium.org>
> Commit-Queue: Alex Clarke <alexclarke@chromium.org>
> Cr-Commit-Position: refs/heads/master@{#636765}

TBR=skyostil@chromium.org,alexclarke@chromium.org,altimin@chromium.org,carlscab@google.com

Change-Id: I63ec7c7620171c4ee499d78f47dad4bc6cb1218a
No-Presubmit: true
No-Tree-Checks: true
No-Try: true
Bug: 863341, 936734
Reviewed-on: https://chromium-review.googlesource.com/c/1496856
Reviewed-by: Alex Clarke <alexclarke@chromium.org>
Commit-Queue: Alex Clarke <alexclarke@chromium.org>
Cr-Commit-Position: refs/heads/master@{#637115}
diff --git a/base/task/sequence_manager/lazy_now.h b/base/task/sequence_manager/lazy_now.h
index d9ace8b..e62db65 100644
--- a/base/task/sequence_manager/lazy_now.h
+++ b/base/task/sequence_manager/lazy_now.h
@@ -28,6 +28,8 @@
   // Result will not be updated on any subsesequent calls.
   TimeTicks Now();
 
+  bool has_value() const { return !!now_; }
+
  private:
   const TickClock* tick_clock_;  // Not owned.
   Optional<TimeTicks> now_;
diff --git a/base/task/sequence_manager/sequence_manager_impl.cc b/base/task/sequence_manager/sequence_manager_impl.cc
index 69dc1d5..ea7c599 100644
--- a/base/task/sequence_manager/sequence_manager_impl.cc
+++ b/base/task/sequence_manager/sequence_manager_impl.cc
@@ -62,8 +62,8 @@
 
 namespace {
 
-constexpr base::TimeDelta kLongTaskTraceEventThreshold =
-    base::TimeDelta::FromMilliseconds(50);
+constexpr TimeDelta kLongTaskTraceEventThreshold =
+    TimeDelta::FromMilliseconds(50);
 // Proportion of tasks which will record thread time for metrics.
 const double kTaskSamplingRateForRecordingCPUTime = 0.01;
 // Proprortion of SequenceManagers which will record thread time for each task,
@@ -80,6 +80,8 @@
   if (time_domain_now->find(time_domain) == time_domain_now->end())
     time_domain_now->insert(std::make_pair(time_domain, time_domain->Now()));
   queue->ReclaimMemory(time_domain_now->at(time_domain));
+  queue->delayed_work_queue()->RemoveAllCanceledTasksFromFront();
+  queue->immediate_work_queue()->RemoveAllCanceledTasksFromFront();
 }
 
 SequenceManager::MetricRecordingSettings InitializeMetricRecordingSettings(
@@ -112,6 +114,9 @@
       TRACE_DISABLED_BY_DEFAULT("sequence_manager"), "SequenceManager", this);
   main_thread_only().selector.SetTaskQueueSelectorObserver(this);
 
+  main_thread_only().next_time_to_reclaim_memory =
+      settings.clock->NowTicks() + kReclaimMemoryInterval;
+
   RegisterTimeDomain(main_thread_only().real_time_domain.get());
 
   controller_->SetSequencedTaskSource(this);
@@ -385,6 +390,13 @@
   LazyNow lazy_now(controller_->GetClock());
   WakeUpReadyDelayedQueues(&lazy_now);
 
+  // If we sampled now, check if it's time to reclaim memory next time we go
+  // idle.
+  if (lazy_now.has_value() &&
+      lazy_now.Now() >= main_thread_only().next_time_to_reclaim_memory) {
+    main_thread_only().memory_reclaim_scheduled = true;
+  }
+
   while (true) {
     internal::WorkQueue* work_queue =
         main_thread_only().selector.SelectWorkQueueToService();
@@ -486,6 +498,8 @@
       have_work_to_do = true;
     }
   }
+  if (!have_work_to_do)
+    MaybeReclaimMemory();
   return have_work_to_do;
 }
 
@@ -708,6 +722,19 @@
     ScheduleWork();
 }
 
+void SequenceManagerImpl::MaybeReclaimMemory() {
+  if (!main_thread_only().memory_reclaim_scheduled)
+    return;
+
+  TRACE_EVENT0("sequence_manager", "SequenceManagerImpl::MaybeReclaimMemory");
+  ReclaimMemory();
+
+  // To avoid performance regressions we only want to do this every so often.
+  main_thread_only().next_time_to_reclaim_memory =
+      NowTicks() + kReclaimMemoryInterval;
+  main_thread_only().memory_reclaim_scheduled = false;
+}
+
 void SequenceManagerImpl::ReclaimMemory() {
   std::map<TimeDomain*, TimeTicks> time_domain_now;
   for (auto* const queue : main_thread_only().active_queues)
@@ -906,6 +933,8 @@
   return main_thread_only().task_execution_stack.rbegin()->task_queue;
 }
 
+constexpr TimeDelta SequenceManagerImpl::kReclaimMemoryInterval;
+
 }  // namespace internal
 }  // namespace sequence_manager
 }  // namespace base
diff --git a/base/task/sequence_manager/sequence_manager_impl.h b/base/task/sequence_manager/sequence_manager_impl.h
index 90db1ee..f780a67 100644
--- a/base/task/sequence_manager/sequence_manager_impl.h
+++ b/base/task/sequence_manager/sequence_manager_impl.h
@@ -188,6 +188,10 @@
 
   WeakPtr<SequenceManagerImpl> GetWeakPtr();
 
+  // How frequently to perform housekeeping tasks (sweeping canceled tasks etc).
+  static constexpr TimeDelta kReclaimMemoryInterval =
+      TimeDelta::FromSeconds(30);
+
  protected:
   // Create a task queue manager where |controller| controls the thread
   // on which the tasks are eventually run.
@@ -251,6 +255,12 @@
     std::set<TimeDomain*> time_domains;
     std::unique_ptr<internal::RealTimeDomain> real_time_domain;
 
+    // If true MaybeReclaimMemory will attempt to reclaim memory.
+    bool memory_reclaim_scheduled = false;
+
+    // Used to ensure we don't perform expensive housekeeping too frequently.
+    TimeTicks next_time_to_reclaim_memory;
+
     // List of task queues managed by this SequenceManager.
     // - active_queues contains queues that are still running tasks.
     //   Most often they are owned by relevant TaskQueues, but
@@ -319,6 +329,10 @@
   std::unique_ptr<internal::TaskQueueImpl> CreateTaskQueueImpl(
       const TaskQueue::Spec& spec) override;
 
+  // Periodically reclaims memory by sweeping away canceled tasks and shrinking
+  // buffers.
+  void MaybeReclaimMemory();
+
   // Deletes queues marked for deletion and empty queues marked for shutdown.
   void CleanUpQueues();
 
diff --git a/base/task/sequence_manager/sequence_manager_impl_unittest.cc b/base/task/sequence_manager/sequence_manager_impl_unittest.cc
index 2e64024..6975f0c 100644
--- a/base/task/sequence_manager/sequence_manager_impl_unittest.cc
+++ b/base/task/sequence_manager/sequence_manager_impl_unittest.cc
@@ -11,6 +11,7 @@
 #include "base/auto_reset.h"
 #include "base/bind.h"
 #include "base/callback.h"
+#include "base/callback_helpers.h"
 #include "base/location.h"
 #include "base/memory/ref_counted_memory.h"
 #include "base/message_loop/message_loop.h"
@@ -128,6 +129,8 @@
     return now_callback_.Run();
   }
 
+  void Reset() { now_call_count_.store(0); }
+
   int now_call_count() const { return now_call_count_; }
 
  private:
@@ -146,9 +149,15 @@
             nullptr,
             ThreadTaskRunnerHandle::Get(),
             mock_tick_clock(),
-            SequenceManager::Settings{.randomised_sampling_enabled = false})) {
+            SequenceManager::Settings{.randomised_sampling_enabled = false,
+                                      .clock = mock_tick_clock()})) {
     // A null clock triggers some assertions.
     AdvanceMockTickClock(TimeDelta::FromMilliseconds(1));
+
+    // The SequenceManager constructor calls Now() once for setting up
+    // housekeeping.
+    EXPECT_EQ(1, GetNowTicksCallCount());
+    call_counting_clock_.Reset();
   }
 
   void AdvanceMockTickClock(TimeDelta delta) override {
@@ -211,8 +220,14 @@
     sequence_manager_ = SequenceManagerForTest::Create(
         std::make_unique<ThreadControllerWithMessagePumpImpl>(
             std::move(pump), mock_tick_clock()),
-        SequenceManager::Settings{.randomised_sampling_enabled = false});
+        SequenceManager::Settings{.randomised_sampling_enabled = false,
+                                  .clock = mock_tick_clock()});
     sequence_manager_->SetDefaultTaskRunner(MakeRefCounted<NullTaskRunner>());
+
+    // The SequenceManager constructor calls Now() once for setting up
+    // housekeeping.
+    EXPECT_EQ(1, GetNowTicksCallCount());
+    call_counting_clock_.Reset();
   }
 
   void AdvanceMockTickClock(TimeDelta delta) override {
@@ -287,7 +302,14 @@
     sequence_manager_ = SequenceManagerForTest::Create(
         message_loop_->GetMessageLoopBase(), ThreadTaskRunnerHandle::Get(),
         mock_tick_clock(),
-        SequenceManager::Settings{.randomised_sampling_enabled = false});
+        SequenceManager::Settings{.randomised_sampling_enabled = false,
+                                  .clock = mock_tick_clock()});
+
+    // The SequenceManager constructor calls Now() once for setting up
+    // housekeeping. The MessageLoop also contains a SequenceManager so two
+    // calls are expected.
+    EXPECT_EQ(2, GetNowTicksCallCount());
+    call_counting_clock_.Reset();
   }
 
   void AdvanceMockTickClock(TimeDelta delta) override {
@@ -4471,6 +4493,77 @@
             "555555555555555555555555555555555555555555555555555555555555");
 }
 
+class CancelableTaskWithDestructionObserver {
+ public:
+  CancelableTaskWithDestructionObserver() : weak_factory_(this) {}
+
+  void Task(std::unique_ptr<ScopedClosureRunner> destruction_observer) {
+    destruction_observer_ = std::move(destruction_observer);
+  }
+
+  std::unique_ptr<ScopedClosureRunner> destruction_observer_;
+  WeakPtrFactory<CancelableTaskWithDestructionObserver> weak_factory_;
+};
+
+TEST_P(SequenceManagerTest, PeriodicHousekeeping) {
+  auto queue = CreateTaskQueue();
+
+  // Post a task that will trigger housekeeping.
+  queue->task_runner()->PostDelayedTask(
+      FROM_HERE, BindOnce(&NopTask),
+      SequenceManagerImpl::kReclaimMemoryInterval);
+
+  // Posts some tasks set to run long in the future and then cancel some of
+  // them.
+  bool task1_deleted = false;
+  bool task2_deleted = false;
+  bool task3_deleted = false;
+  CancelableTaskWithDestructionObserver task1;
+  CancelableTaskWithDestructionObserver task2;
+  CancelableTaskWithDestructionObserver task3;
+
+  queue->task_runner()->PostDelayedTask(
+      FROM_HERE,
+      BindOnce(&CancelableTaskWithDestructionObserver::Task,
+               task1.weak_factory_.GetWeakPtr(),
+               std::make_unique<ScopedClosureRunner>(
+                   BindLambdaForTesting([&]() { task1_deleted = true; }))),
+      TimeDelta::FromHours(1));
+
+  queue->task_runner()->PostDelayedTask(
+      FROM_HERE,
+      BindOnce(&CancelableTaskWithDestructionObserver::Task,
+               task2.weak_factory_.GetWeakPtr(),
+               std::make_unique<ScopedClosureRunner>(
+                   BindLambdaForTesting([&]() { task2_deleted = true; }))),
+      TimeDelta::FromHours(2));
+
+  queue->task_runner()->PostDelayedTask(
+      FROM_HERE,
+      BindOnce(&CancelableTaskWithDestructionObserver::Task,
+               task3.weak_factory_.GetWeakPtr(),
+               std::make_unique<ScopedClosureRunner>(
+                   BindLambdaForTesting([&]() { task3_deleted = true; }))),
+      TimeDelta::FromHours(3));
+
+  task2.weak_factory_.InvalidateWeakPtrs();
+  task3.weak_factory_.InvalidateWeakPtrs();
+
+  EXPECT_FALSE(task1_deleted);
+  EXPECT_FALSE(task2_deleted);
+  EXPECT_FALSE(task3_deleted);
+
+  // This should trigger housekeeping which will sweep away the canceled tasks.
+  FastForwardBy(SequenceManagerImpl::kReclaimMemoryInterval);
+
+  EXPECT_FALSE(task1_deleted);
+  EXPECT_TRUE(task2_deleted);
+  EXPECT_TRUE(task3_deleted);
+
+  // Tidy up.
+  FastForwardUntilNoTasksRemain();
+}
+
 }  // namespace sequence_manager_impl_unittest
 }  // namespace internal
 }  // namespace sequence_manager
diff --git a/base/task/sequence_manager/time_domain.cc b/base/task/sequence_manager/time_domain.cc
index 69d35c2..1d81608 100644
--- a/base/task/sequence_manager/time_domain.cc
+++ b/base/task/sequence_manager/time_domain.cc
@@ -160,5 +160,9 @@
   return pending_high_res_wake_up_count_;
 }
 
+bool TimeDomain::Empty() const {
+  return delayed_wake_up_queue_.empty();
+}
+
 }  // namespace sequence_manager
 }  // namespace base
diff --git a/base/task/sequence_manager/time_domain.h b/base/task/sequence_manager/time_domain.h
index a09d22c9..04cab72 100644
--- a/base/task/sequence_manager/time_domain.h
+++ b/base/task/sequence_manager/time_domain.h
@@ -59,6 +59,9 @@
   void AsValueInto(trace_event::TracedValue* state) const;
   bool HasPendingHighResolutionTasks() const;
 
+  // Returns true if there are no pending delayed tasks.
+  bool Empty() const;
+
   // This is the signal that virtual time should step forward. If
   // RunLoop::QuitWhenIdle has been called then |quit_when_idle_requested| will
   // be true. Returns true if time advanced and there is now a task to run.
diff --git a/base/task/sequence_manager/time_domain_unittest.cc b/base/task/sequence_manager/time_domain_unittest.cc
index 53b56ac..e51be10 100644
--- a/base/task/sequence_manager/time_domain_unittest.cc
+++ b/base/task/sequence_manager/time_domain_unittest.cc
@@ -99,12 +99,14 @@
 TEST_F(TimeDomainTest, ScheduleWakeUpForQueue) {
   TimeDelta delay = TimeDelta::FromMilliseconds(10);
   TimeTicks delayed_runtime = time_domain_->Now() + delay;
+  EXPECT_TRUE(time_domain_->Empty());
   EXPECT_CALL(*time_domain_.get(), SetNextDelayedDoWork(_, delayed_runtime));
   TimeTicks now = time_domain_->Now();
   LazyNow lazy_now(now);
   task_queue_->SetDelayedWakeUpForTesting(
       internal::DelayedWakeUp{now + delay, 0});
 
+  EXPECT_FALSE(time_domain_->Empty());
   EXPECT_EQ(delayed_runtime, time_domain_->NextScheduledRunTime());
 
   EXPECT_EQ(task_queue_.get(), time_domain_->NextScheduledTaskQueue());
@@ -195,6 +197,7 @@
   std::unique_ptr<TaskQueueImplForTest> task_queue2 =
       std::make_unique<TaskQueueImplForTest>(nullptr, time_domain_.get(),
                                              TaskQueue::Spec("test"));
+  EXPECT_TRUE(time_domain_->Empty());
 
   TimeTicks now = time_domain_->Now();
   LazyNow lazy_now(now);
@@ -203,6 +206,7 @@
   task_queue_->SetDelayedWakeUpForTesting(internal::DelayedWakeUp{wake_up1, 0});
   TimeTicks wake_up2 = now + TimeDelta::FromMilliseconds(100);
   task_queue2->SetDelayedWakeUpForTesting(internal::DelayedWakeUp{wake_up2, 0});
+  EXPECT_FALSE(time_domain_->Empty());
 
   EXPECT_EQ(task_queue_.get(), time_domain_->NextScheduledTaskQueue());
 
@@ -216,6 +220,7 @@
   task_queue_->UnregisterTaskQueue();
   task_queue_ = nullptr;
 
+  EXPECT_FALSE(time_domain_->Empty());
   testing::Mock::VerifyAndClearExpectations(time_domain_.get());
 
   EXPECT_CALL(*time_domain_.get(), SetNextDelayedDoWork(_, TimeTicks::Max()))
@@ -226,6 +231,7 @@
 
   task_queue2->UnregisterTaskQueue();
   task_queue2 = nullptr;
+  EXPECT_TRUE(time_domain_->Empty());
 }
 
 TEST_F(TimeDomainTest, WakeUpReadyDelayedQueues) {
diff --git a/base/task/sequence_manager/work_queue.cc b/base/task/sequence_manager/work_queue.cc
index 7a7bea6..3cf0cb4 100644
--- a/base/task/sequence_manager/work_queue.cc
+++ b/base/task/sequence_manager/work_queue.cc
@@ -154,7 +154,8 @@
 }
 
 bool WorkQueue::RemoveAllCanceledTasksFromFront() {
-  DCHECK(work_queue_sets_);
+  if (!work_queue_sets_)
+    return false;
   bool task_removed = false;
   while (!tasks_.empty() &&
          (!tasks_.front().task || tasks_.front().task.IsCancelled())) {
diff --git a/third_party/blink/renderer/platform/scheduler/BUILD.gn b/third_party/blink/renderer/platform/scheduler/BUILD.gn
index 9dc0d1d..d7d6dc0a 100644
--- a/third_party/blink/renderer/platform/scheduler/BUILD.gn
+++ b/third_party/blink/renderer/platform/scheduler/BUILD.gn
@@ -17,8 +17,6 @@
     "common/frame_or_worker_scheduler.cc",
     "common/idle_helper.cc",
     "common/idle_helper.h",
-    "common/idle_memory_reclaimer.cc",
-    "common/idle_memory_reclaimer.h",
     "common/metrics_helper.cc",
     "common/metrics_helper.h",
     "common/pollable_thread_safe_flag.cc",
@@ -185,7 +183,6 @@
   sources = [
     "common/cooperative_scheduling_manager_unittest.cc",
     "common/idle_helper_unittest.cc",
-    "common/idle_memory_reclaimer_unittest.cc",
     "common/metrics_helper_unittest.cc",
     "common/post_cancellable_task_unittest.cc",
     "common/scheduler_helper_unittest.cc",
diff --git a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.cc b/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.cc
deleted file mode 100644
index 850cc2c..0000000
--- a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.cc
+++ /dev/null
@@ -1,39 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h"
-
-#include "base/bind.h"
-
-namespace blink {
-namespace scheduler {
-
-namespace {
-constexpr const int kReclaimMemoryIntervalSeconds = 30;
-}
-
-IdleMemoryReclaimer::IdleMemoryReclaimer(
-    SchedulerHelper* scheduler_helper,
-    scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner)
-    : scheduler_helper_(scheduler_helper),
-      idle_task_runner_(idle_task_runner),
-      weak_factory_(this) {
-  PostIdleTask();
-}
-
-void IdleMemoryReclaimer::PostIdleTask() {
-  idle_task_runner_->PostDelayedIdleTask(
-      FROM_HERE, base::TimeDelta::FromSeconds(kReclaimMemoryIntervalSeconds),
-      base::BindOnce(&IdleMemoryReclaimer::IdleTask,
-                     weak_factory_.GetWeakPtr()));
-}
-
-void IdleMemoryReclaimer::IdleTask(base::TimeTicks deadline) {
-  TRACE_EVENT0("renderer.scheduler", "IdleMemoryReclaimer::IdleTask");
-  scheduler_helper_->ReclaimMemory();
-  PostIdleTask();
-}
-
-}  // namespace scheduler
-}  // namespace blink
diff --git a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h b/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h
deleted file mode 100644
index 6ca1db0..0000000
--- a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h
+++ /dev/null
@@ -1,38 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_IDLE_MEMORY_RECLAIMER_H_
-#define THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_IDLE_MEMORY_RECLAIMER_H_
-
-#include "base/macros.h"
-#include "base/memory/weak_ptr.h"
-#include "third_party/blink/renderer/platform/scheduler/common/scheduler_helper.h"
-#include "third_party/blink/renderer/platform/scheduler/common/single_thread_idle_task_runner.h"
-
-namespace blink {
-namespace scheduler {
-
-// This class periodically sweeps away canceled delayed tasks and considers
-// resizing to fit all internal queues, which helps reduce memory consumption.
-class PLATFORM_EXPORT IdleMemoryReclaimer {
- public:
-  IdleMemoryReclaimer(
-      SchedulerHelper* scheduler_helper,
-      scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner);
-
- private:
-  void PostIdleTask();
-  void IdleTask(base::TimeTicks deadline);
-
-  SchedulerHelper* scheduler_helper_;  // NOT OWNED
-  scoped_refptr<SingleThreadIdleTaskRunner> idle_task_runner_;
-  base::WeakPtrFactory<IdleMemoryReclaimer> weak_factory_;
-
-  DISALLOW_COPY_AND_ASSIGN(IdleMemoryReclaimer);
-};
-
-}  // namespace scheduler
-}  // namespace blink
-
-#endif  // THIRD_PARTY_BLINK_RENDERER_PLATFORM_SCHEDULER_COMMON_IDLE_MEMORY_RECLAIMER_H_
diff --git a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer_unittest.cc b/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer_unittest.cc
deleted file mode 100644
index 59bd4a3..0000000
--- a/third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer_unittest.cc
+++ /dev/null
@@ -1,132 +0,0 @@
-// Copyright 2017 The Chromium Authors. All rights reserved.
-// Use of this source code is governed by a BSD-style license that can be
-// found in the LICENSE file.
-
-#include "third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h"
-
-#include "base/bind.h"
-#include "base/task/sequence_manager/lazy_now.h"
-#include "base/task/sequence_manager/task_queue.h"
-#include "base/task/sequence_manager/test/sequence_manager_for_test.h"
-#include "base/test/scoped_task_environment.h"
-#include "testing/gtest/include/gtest/gtest.h"
-#include "third_party/blink/renderer/platform/scheduler/common/idle_helper.h"
-#include "third_party/blink/renderer/platform/scheduler/common/scheduler_helper.h"
-#include "third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_helper.h"
-
-namespace blink {
-namespace scheduler {
-
-class TestClass {
- public:
-  TestClass() : weak_factory_(this) {}
-
-  void NopTask() {}
-
-  base::WeakPtrFactory<TestClass> weak_factory_;
-};
-
-class IdleMemoryReclaimerTest : public testing::Test,
-                                public IdleHelper::Delegate {
- public:
-  IdleMemoryReclaimerTest()
-      : task_environment_(
-            base::test::ScopedTaskEnvironment::MainThreadType::MOCK_TIME,
-            base::test::ScopedTaskEnvironment::ExecutionMode::QUEUED),
-        scheduler_helper_(new MainThreadSchedulerHelper(
-            base::sequence_manager::SequenceManagerForTest::Create(
-                nullptr,
-                task_environment_.GetMainThreadTaskRunner(),
-                task_environment_.GetMockTickClock()),
-            nullptr)),
-        idle_helper_(
-            new IdleHelper(scheduler_helper_.get(),
-                           this,
-                           "test",
-                           base::TimeDelta::FromSeconds(30),
-                           scheduler_helper_->NewTaskQueue(
-                               MainThreadTaskQueue::QueueCreationParams(
-                                   MainThreadTaskQueue::QueueType::kTest)))),
-        idle_canceled_delayed_taks_sweeper_(
-            new IdleMemoryReclaimer(scheduler_helper_.get(),
-                                    idle_helper_->IdleTaskRunner())),
-        default_task_queue_(scheduler_helper_->DefaultMainThreadTaskQueue()) {
-    // Null clock might trigger some assertions.
-    task_environment_.FastForwardBy(base::TimeDelta::FromMilliseconds(5));
-  }
-
-  ~IdleMemoryReclaimerTest() override = default;
-
-  void TearDown() override {
-    // Check that all tests stop posting tasks.
-    task_environment_.FastForwardUntilNoTasksRemain();
-  }
-
-  // IdleHelper::Delegate implementation:
-  bool CanEnterLongIdlePeriod(
-      base::TimeTicks now,
-      base::TimeDelta* next_long_idle_period_delay_out) override {
-    return true;
-  }
-  void IsNotQuiescent() override {}
-  void OnIdlePeriodStarted() override {}
-  void OnIdlePeriodEnded() override {}
-  void OnPendingTasksChanged(bool has_tasks) override {}
-
- protected:
-  base::test::ScopedTaskEnvironment task_environment_;
-
-  std::unique_ptr<MainThreadSchedulerHelper> scheduler_helper_;
-  std::unique_ptr<IdleHelper> idle_helper_;
-  std::unique_ptr<IdleMemoryReclaimer> idle_canceled_delayed_taks_sweeper_;
-  scoped_refptr<base::sequence_manager::TaskQueue> default_task_queue_;
-
-  DISALLOW_COPY_AND_ASSIGN(IdleMemoryReclaimerTest);
-};
-
-TEST_F(IdleMemoryReclaimerTest, TestSweep) {
-  TestClass class1;
-  TestClass class2;
-
-  // Post one task we won't cancel.
-  default_task_queue_->task_runner()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&TestClass::NopTask, class1.weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromSeconds(100));
-
-  // And a bunch we will.
-  default_task_queue_->task_runner()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&TestClass::NopTask, class2.weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromSeconds(101));
-
-  default_task_queue_->task_runner()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&TestClass::NopTask, class2.weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromSeconds(102));
-
-  default_task_queue_->task_runner()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&TestClass::NopTask, class2.weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromSeconds(103));
-
-  default_task_queue_->task_runner()->PostDelayedTask(
-      FROM_HERE,
-      base::BindOnce(&TestClass::NopTask, class2.weak_factory_.GetWeakPtr()),
-      base::TimeDelta::FromSeconds(104));
-
-  // Cancel the last four tasks.
-  class2.weak_factory_.InvalidateWeakPtrs();
-
-  // Give the IdleMemoryReclaimer a chance to run but don't let
-  // the first non canceled delayed task run.  This is important because the
-  // canceled tasks would get removed by TaskQueueImpl::WakeUpForDelayedWork.
-  task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(40));
-  idle_helper_->EnableLongIdlePeriod();
-  task_environment_.FastForwardBy(base::TimeDelta::FromSeconds(40));
-
-  EXPECT_EQ(1u, default_task_queue_->GetNumberOfPendingTasks());
-}
-
-}  // namespace scheduler
-}  // namespace blink
diff --git a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.cc b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.cc
index 76c065d..530569b 100644
--- a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.cc
+++ b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.cc
@@ -182,7 +182,6 @@
                            MainThreadTaskQueue::QueueType::kIdle)
                            .SetFixedPriority(
                                TaskQueue::QueuePriority::kBestEffortPriority))),
-      idle_memory_reclaimer_(&helper_, idle_helper_.IdleTaskRunner()),
       render_widget_scheduler_signals_(this),
       control_task_queue_(helper_.ControlMainThreadTaskQueue()),
       compositor_task_queue_(
diff --git a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.h b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.h
index c05ef3e..e2c2a5cb 100644
--- a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.h
+++ b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_scheduler_impl.h
@@ -27,7 +27,6 @@
 #include "third_party/blink/public/platform/scheduler/web_thread_scheduler.h"
 #include "third_party/blink/renderer/platform/platform_export.h"
 #include "third_party/blink/renderer/platform/scheduler/common/idle_helper.h"
-#include "third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h"
 #include "third_party/blink/renderer/platform/scheduler/common/pollable_thread_safe_flag.h"
 #include "third_party/blink/renderer/platform/scheduler/common/thread_scheduler_impl.h"
 #include "third_party/blink/renderer/platform/scheduler/common/tracing_helper.h"
@@ -743,7 +742,6 @@
 
   MainThreadSchedulerHelper helper_;
   IdleHelper idle_helper_;
-  IdleMemoryReclaimer idle_memory_reclaimer_;
   std::unique_ptr<TaskQueueThrottler> task_queue_throttler_;
   RenderWidgetSignals render_widget_scheduler_signals_;
   MemoryPurgeManager memory_purge_manager_;
diff --git a/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.cc b/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.cc
index bd4a98f..20fda05 100644
--- a/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.cc
+++ b/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.cc
@@ -107,7 +107,6 @@
                    "WorkerSchedulerIdlePeriod",
                    base::TimeDelta::FromMilliseconds(300),
                    helper()->NewTaskQueue(TaskQueue::Spec("worker_idle_tq"))),
-      idle_memory_reclaimer_(helper(), idle_helper_.IdleTaskRunner()),
       load_tracker_(helper()->NowTicks(),
                     base::BindRepeating(&ReportWorkerTaskLoad),
                     kUnspecifiedWorkerThreadLoadTrackerReportingInterval),
diff --git a/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h b/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h
index 7d78657..a1af776 100644
--- a/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h
+++ b/third_party/blink/renderer/platform/scheduler/worker/worker_thread_scheduler.h
@@ -12,7 +12,6 @@
 #include "components/scheduling_metrics/task_duration_metric_reporter.h"
 #include "third_party/blink/public/platform/web_thread_type.h"
 #include "third_party/blink/renderer/platform/scheduler/common/idle_helper.h"
-#include "third_party/blink/renderer/platform/scheduler/common/idle_memory_reclaimer.h"
 #include "third_party/blink/renderer/platform/scheduler/common/thread_load_tracker.h"
 #include "third_party/blink/renderer/platform/scheduler/public/frame_scheduler.h"
 #include "third_party/blink/renderer/platform/scheduler/public/frame_status.h"
@@ -140,7 +139,6 @@
 
   const WebThreadType thread_type_;
   IdleHelper idle_helper_;
-  IdleMemoryReclaimer idle_memory_reclaimer_;
   ThreadLoadTracker load_tracker_;
   bool initialized_;
   base::TimeTicks thread_start_time_;