Create a process-global MetadataRecorder for the stack sampling profiler

This paves the way for Chromium code to manipulate the metadata
recorder and help provide the stack sampling profiler with context
about what's happening when a sample is taken.

Bug: 913570
Change-Id: I96d7bcc2f4872467852042495c0fdf1ad5d8c4cc
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1572370
Commit-Queue: Charlie Andrews <charliea@chromium.org>
Commit-Queue: Mike Wittman <wittman@chromium.org>
Reviewed-by: Mike Wittman <wittman@chromium.org>
Auto-Submit: Charlie Andrews <charliea@chromium.org>
Cr-Commit-Position: refs/heads/master@{#652161}
diff --git a/chrome/common/thread_profiler.cc b/chrome/common/thread_profiler.cc
index db16b3b..ca9b35b 100644
--- a/chrome/common/thread_profiler.cc
+++ b/chrome/common/thread_profiler.cc
@@ -234,7 +234,9 @@
       std::make_unique<CallStackProfileBuilder>(
           CallStackProfileParams(GetProcess(), thread,
                                  CallStackProfileParams::PROCESS_STARTUP),
-          work_id_recorder_.get()));
+          work_id_recorder_.get(),
+          &metrics::CallStackProfileBuilder::
+              GetStackSamplingProfilerMetadataRecorder()));
 
   startup_profiler_->Start();
 
@@ -280,7 +282,9 @@
       std::make_unique<CallStackProfileBuilder>(
           CallStackProfileParams(GetProcess(), thread_,
                                  CallStackProfileParams::PERIODIC_COLLECTION),
-          work_id_recorder_.get(), nullptr,
+          work_id_recorder_.get(),
+          &metrics::CallStackProfileBuilder::
+              GetStackSamplingProfilerMetadataRecorder(),
           base::BindOnce(&ThreadProfiler::OnPeriodicCollectionCompleted,
                          owning_thread_task_runner_,
                          weak_factory_.GetWeakPtr())));
diff --git a/components/metrics/call_stack_profile_builder.cc b/components/metrics/call_stack_profile_builder.cc
index 3505f11..2adbe53 100644
--- a/components/metrics/call_stack_profile_builder.cc
+++ b/components/metrics/call_stack_profile_builder.cc
@@ -200,6 +200,13 @@
       std::move(browser_interface));
 }
 
+// static
+MetadataRecorder&
+CallStackProfileBuilder::GetStackSamplingProfilerMetadataRecorder() {
+  static base::NoDestructor<MetadataRecorder> instance;
+  return *instance;
+}
+
 void CallStackProfileBuilder::PassProfilesToMetricsProvider(
     SampledProfile sampled_profile) {
   if (sampled_profile.process() == BROWSER_PROCESS) {
diff --git a/components/metrics/call_stack_profile_builder.h b/components/metrics/call_stack_profile_builder.h
index 877944c..23341fc 100644
--- a/components/metrics/call_stack_profile_builder.h
+++ b/components/metrics/call_stack_profile_builder.h
@@ -81,6 +81,10 @@
   static void SetParentProfileCollectorForChildProcess(
       metrics::mojom::CallStackProfileCollectorPtr browser_interface);
 
+  // Returns the process-global metadata recorder instance used for tracking
+  // sampling profiler metadata.
+  static MetadataRecorder& GetStackSamplingProfilerMetadataRecorder();
+
  protected:
   // Test seam.
   virtual void PassProfilesToMetricsProvider(SampledProfile sampled_profile);