Move code that sets up the environment to run a task to TaskTracker::PerformRunTask().

Move code that sets up the environment to run a task (task runner
handles, thread restrictions) from TaskTracker::RunTask() to
TaskTracker::PerformRunTask(). That prevents code in the overrides
of TaskTracker::PerformRunTask() to run under the thread restrictions
set by TaskTracker::RunTask().

BUG=553459

Review-Url: https://codereview.chromium.org/2899443002
Cr-Original-Commit-Position: refs/heads/master@{#473338}
Cr-Mirrored-From: https://chromium.googlesource.com/chromium/src
Cr-Mirrored-Commit: ea326b6451c3b0b6176c65bf517e66a26fc3a543
diff --git a/task_scheduler/task_tracker.cc b/task_scheduler/task_tracker.cc
index ba12beb..55230aa 100644
--- a/task_scheduler/task_tracker.cc
+++ b/task_scheduler/task_tracker.cc
@@ -246,57 +246,7 @@
   const bool is_delayed = !task->delayed_run_time.is_null();
 
   if (can_run_task) {
-    RecordTaskLatencyHistogram(task.get());
-
-    const bool previous_singleton_allowed =
-        ThreadRestrictions::SetSingletonAllowed(
-            task->traits.shutdown_behavior() !=
-            TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN);
-    const bool previous_io_allowed =
-        ThreadRestrictions::SetIOAllowed(task->traits.may_block());
-    const bool previous_wait_allowed = ThreadRestrictions::SetWaitAllowed(
-        task->traits.with_base_sync_primitives());
-
-    {
-      ScopedSetSequenceTokenForCurrentThread
-          scoped_set_sequence_token_for_current_thread(sequence_token);
-      ScopedSetTaskPriorityForCurrentThread
-          scoped_set_task_priority_for_current_thread(task->traits.priority());
-
-      // Set up TaskRunnerHandle as expected for the scope of the task.
-      std::unique_ptr<SequencedTaskRunnerHandle> sequenced_task_runner_handle;
-      std::unique_ptr<ThreadTaskRunnerHandle> single_thread_task_runner_handle;
-      DCHECK(!task->sequenced_task_runner_ref ||
-             !task->single_thread_task_runner_ref);
-      if (task->sequenced_task_runner_ref) {
-        sequenced_task_runner_handle.reset(
-            new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref));
-      } else if (task->single_thread_task_runner_ref) {
-        single_thread_task_runner_handle.reset(
-            new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref));
-      }
-
-      TRACE_TASK_EXECUTION(kRunFunctionName, *task);
-
-      const char* const execution_mode =
-          task->single_thread_task_runner_ref
-              ? kSingleThreadExecutionMode
-              : (task->sequenced_task_runner_ref ? kSequencedExecutionMode
-                                                 : kParallelExecutionMode);
-      // TODO(gab): In a better world this would be tacked on as an extra arg
-      // to the trace event generated above. This is not possible however until
-      // http://crbug.com/652692 is resolved.
-      TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info",
-                   MakeUnique<TaskTracingInfo>(task->traits, execution_mode,
-                                               sequence_token));
-
-      PerformRunTask(std::move(task));
-    }
-
-    ThreadRestrictions::SetWaitAllowed(previous_wait_allowed);
-    ThreadRestrictions::SetIOAllowed(previous_io_allowed);
-    ThreadRestrictions::SetSingletonAllowed(previous_singleton_allowed);
-
+    PerformRunTask(std::move(task), sequence_token);
     AfterRunTask(shutdown_behavior);
   }
 
@@ -327,8 +277,58 @@
   state_->StartShutdown();
 }
 
-void TaskTracker::PerformRunTask(std::unique_ptr<Task> task) {
-  debug::TaskAnnotator().RunTask(kQueueFunctionName, task.get());
+void TaskTracker::PerformRunTask(std::unique_ptr<Task> task,
+                                 const SequenceToken& sequence_token) {
+  RecordTaskLatencyHistogram(task.get());
+
+  const bool previous_singleton_allowed =
+      ThreadRestrictions::SetSingletonAllowed(
+          task->traits.shutdown_behavior() !=
+          TaskShutdownBehavior::CONTINUE_ON_SHUTDOWN);
+  const bool previous_io_allowed =
+      ThreadRestrictions::SetIOAllowed(task->traits.may_block());
+  const bool previous_wait_allowed = ThreadRestrictions::SetWaitAllowed(
+      task->traits.with_base_sync_primitives());
+
+  {
+    ScopedSetSequenceTokenForCurrentThread
+        scoped_set_sequence_token_for_current_thread(sequence_token);
+    ScopedSetTaskPriorityForCurrentThread
+        scoped_set_task_priority_for_current_thread(task->traits.priority());
+
+    // Set up TaskRunnerHandle as expected for the scope of the task.
+    std::unique_ptr<SequencedTaskRunnerHandle> sequenced_task_runner_handle;
+    std::unique_ptr<ThreadTaskRunnerHandle> single_thread_task_runner_handle;
+    DCHECK(!task->sequenced_task_runner_ref ||
+           !task->single_thread_task_runner_ref);
+    if (task->sequenced_task_runner_ref) {
+      sequenced_task_runner_handle.reset(
+          new SequencedTaskRunnerHandle(task->sequenced_task_runner_ref));
+    } else if (task->single_thread_task_runner_ref) {
+      single_thread_task_runner_handle.reset(
+          new ThreadTaskRunnerHandle(task->single_thread_task_runner_ref));
+    }
+
+    TRACE_TASK_EXECUTION(kRunFunctionName, *task);
+
+    const char* const execution_mode =
+        task->single_thread_task_runner_ref
+            ? kSingleThreadExecutionMode
+            : (task->sequenced_task_runner_ref ? kSequencedExecutionMode
+                                               : kParallelExecutionMode);
+    // TODO(gab): In a better world this would be tacked on as an extra arg
+    // to the trace event generated above. This is not possible however until
+    // http://crbug.com/652692 is resolved.
+    TRACE_EVENT1("task_scheduler", "TaskTracker::RunTask", "task_info",
+                 MakeUnique<TaskTracingInfo>(task->traits, execution_mode,
+                                             sequence_token));
+
+    debug::TaskAnnotator().RunTask(kQueueFunctionName, task.get());
+  }
+
+  ThreadRestrictions::SetWaitAllowed(previous_wait_allowed);
+  ThreadRestrictions::SetIOAllowed(previous_io_allowed);
+  ThreadRestrictions::SetSingletonAllowed(previous_singleton_allowed);
 }
 
 void TaskTracker::PerformShutdown() {
diff --git a/task_scheduler/task_tracker.h b/task_scheduler/task_tracker.h
index 24b63e3..1a1977d 100644
--- a/task_scheduler/task_tracker.h
+++ b/task_scheduler/task_tracker.h
@@ -77,9 +77,11 @@
   void SetHasShutdownStartedForTesting();
 
  protected:
-  // Runs |task|. An override is expected to call its parent's implementation
-  // but is free to perform extra work before and after doing so.
-  virtual void PerformRunTask(std::unique_ptr<Task> task);
+  // Runs |task|. |sequence_token| is the token identifying the sequence from
+  // which |task| was extracted. An override is expected to call its parent's
+  // implementation but is free to perform extra work before and after doing so.
+  virtual void PerformRunTask(std::unique_ptr<Task> task,
+                              const SequenceToken& sequence_token);
 
 #if DCHECK_IS_ON()
   // Returns true if this context should be exempt from blocking shutdown
diff --git a/task_scheduler/task_tracker_posix.cc b/task_scheduler/task_tracker_posix.cc
index 4249680..b1c1b92 100644
--- a/task_scheduler/task_tracker_posix.cc
+++ b/task_scheduler/task_tracker_posix.cc
@@ -14,11 +14,12 @@
 TaskTrackerPosix::TaskTrackerPosix() = default;
 TaskTrackerPosix::~TaskTrackerPosix() = default;
 
-void TaskTrackerPosix::PerformRunTask(std::unique_ptr<Task> task) {
+void TaskTrackerPosix::PerformRunTask(std::unique_ptr<Task> task,
+                                      const SequenceToken& sequence_token) {
   DCHECK(watch_file_descriptor_message_loop_);
   FileDescriptorWatcher file_descriptor_watcher(
       watch_file_descriptor_message_loop_);
-  TaskTracker::PerformRunTask(std::move(task));
+  TaskTracker::PerformRunTask(std::move(task), sequence_token);
 }
 
 #if DCHECK_IS_ON()
diff --git a/task_scheduler/task_tracker_posix.h b/task_scheduler/task_tracker_posix.h
index 0408953..a98b464 100644
--- a/task_scheduler/task_tracker_posix.h
+++ b/task_scheduler/task_tracker_posix.h
@@ -52,7 +52,8 @@
 
  private:
   // TaskTracker:
-  void PerformRunTask(std::unique_ptr<Task> task) override;
+  void PerformRunTask(std::unique_ptr<Task> task,
+                      const SequenceToken& sequence_token) override;
 
 #if DCHECK_IS_ON()
   bool IsPostingBlockShutdownTaskAfterShutdownAllowed() override;