PM: Only enable instrumentation under the chrome embedder.

As of https://chromium-review.googlesource.com/c/1425042, content/
no longer provides forwarding for the ResourceCoordinator
instrumentation interfaces. This was a step in a larger refactoring,
but had the effect of orphaning the instrumentation in all but the
chrome/browser embedder. This in turn led to a memory regression in
the Android WebView.
This CL makes the initialization and the usage of the instrumentation
interfaces conditional on a runtime flag, which is only set by the
ChromeContentRendererClient.
This is still an intermediate state in the refactoring, as the
interfaces in question will be moved and simplified.

Bug: 926426
Change-Id: Ia640f544b536bf86d53e853de6178aeaf9c9b6f5
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1496303
Commit-Queue: Sigurður Ásgeirsson <siggi@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Stephen White <senorblanco@chromium.org>
Reviewed-by: Chris Hamilton <chrisha@chromium.org>
Cr-Commit-Position: refs/heads/master@{#638575}
diff --git a/chrome/renderer/chrome_content_renderer_client.cc b/chrome/renderer/chrome_content_renderer_client.cc
index d35c15a..d007c01 100644
--- a/chrome/renderer/chrome_content_renderer_client.cc
+++ b/chrome/renderer/chrome_content_renderer_client.cc
@@ -1520,6 +1520,10 @@
 
 void ChromeContentRendererClient::
     SetRuntimeFeaturesDefaultsBeforeBlinkInitialization() {
+  // The performance manager service interfaces are provided by the chrome
+  // embedder only.
+  blink::WebRuntimeFeatures::EnablePerformanceManagerInstrumentation(true);
+
 // Web Share is shipped on Android, experimental otherwise. It is enabled here,
 // in chrome/, to avoid it being made available in other clients of content/
 // that do not have a Web Share Mojo implementation.
diff --git a/third_party/blink/public/platform/web_runtime_features.h b/third_party/blink/public/platform/web_runtime_features.h
index d51c3c5..a6be471 100644
--- a/third_party/blink/public/platform/web_runtime_features.h
+++ b/third_party/blink/public/platform/web_runtime_features.h
@@ -148,6 +148,8 @@
   BLINK_PLATFORM_EXPORT static void EnablePaymentRequest(bool);
   BLINK_PLATFORM_EXPORT static void EnablePaymentRequestHasEnrolledInstrument(
       bool);
+  BLINK_PLATFORM_EXPORT static void EnablePerformanceManagerInstrumentation(
+      bool);
   BLINK_PLATFORM_EXPORT static void EnablePermissionsAPI(bool);
   BLINK_PLATFORM_EXPORT static void EnablePictureInPicture(bool);
   BLINK_PLATFORM_EXPORT static void EnablePictureInPictureAPI(bool);
diff --git a/third_party/blink/renderer/controller/bloated_renderer_detector.cc b/third_party/blink/renderer/controller/bloated_renderer_detector.cc
index 33792cd..58399c7 100644
--- a/third_party/blink/renderer/controller/bloated_renderer_detector.cc
+++ b/third_party/blink/renderer/controller/bloated_renderer_detector.cc
@@ -32,7 +32,8 @@
     }
   }
   last_detection_time_ = now;
-  RendererResourceCoordinator::Get().OnRendererIsBloated();
+  if (auto* renderer_resource_coordinator = RendererResourceCoordinator::Get())
+    renderer_resource_coordinator->OnRendererIsBloated();
   return NearV8HeapLimitHandling::kForwardedToBrowser;
 }
 
diff --git a/third_party/blink/renderer/core/frame/local_frame.cc b/third_party/blink/renderer/core/frame/local_frame.cc
index 04c8ce4..c0aecdf 100644
--- a/third_party/blink/renderer/core/frame/local_frame.cc
+++ b/third_party/blink/renderer/core/frame/local_frame.cc
@@ -1350,7 +1350,7 @@
     auto* local_frame_client = Client();
     if (!local_frame_client)
       return nullptr;
-    frame_resource_coordinator_ = FrameResourceCoordinator::Create(
+    frame_resource_coordinator_ = FrameResourceCoordinator::MaybeCreate(
         local_frame_client->GetInterfaceProvider());
   }
   return frame_resource_coordinator_.get();
diff --git a/third_party/blink/renderer/platform/exported/platform.cc b/third_party/blink/renderer/platform/exported/platform.cc
index 7b1643a7..aafd9121 100644
--- a/third_party/blink/renderer/platform/exported/platform.cc
+++ b/third_party/blink/renderer/platform/exported/platform.cc
@@ -220,7 +220,7 @@
       ParkableStringManagerDumpProvider::Instance(), "ParkableStrings",
       base::ThreadTaskRunnerHandle::Get());
 
-  RendererResourceCoordinator::Initialize();
+  RendererResourceCoordinator::MaybeInitialize();
 }
 
 void Platform::SetCurrentPlatformForTesting(Platform* platform) {
diff --git a/third_party/blink/renderer/platform/exported/web_runtime_features.cc b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
index 5818469..f580fb8 100644
--- a/third_party/blink/renderer/platform/exported/web_runtime_features.cc
+++ b/third_party/blink/renderer/platform/exported/web_runtime_features.cc
@@ -354,6 +354,10 @@
   RuntimeEnabledFeatures::SetPaymentRequestHasEnrolledInstrumentEnabled(enable);
 }
 
+void WebRuntimeFeatures::EnablePerformanceManagerInstrumentation(bool enable) {
+  RuntimeEnabledFeatures::SetPerformanceManagerInstrumentationEnabled(enable);
+}
+
 void WebRuntimeFeatures::EnablePermissionsAPI(bool enable) {
   RuntimeEnabledFeatures::SetPermissionsEnabled(enable);
 }
diff --git a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/DEPS b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/DEPS
index 0d49842..07468f5 100644
--- a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/DEPS
+++ b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/DEPS
@@ -1,3 +1,4 @@
 include_rules = [
     "+services/resource_coordinator/public",
-]
\ No newline at end of file
+    "+third_party/blink/renderer/platform/runtime_enabled_features.h",
+]
diff --git a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.cc b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.cc
index 1b5b1d3..7a7fe50 100644
--- a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.cc
+++ b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.cc
@@ -4,8 +4,11 @@
 
 #include "third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.h"
 
+#include <memory>
+
 #include "base/memory/ptr_util.h"
 #include "services/service_manager/public/cpp/interface_provider.h"
+#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
 
 namespace blink {
 
@@ -17,8 +20,11 @@
 }  // namespace
 
 // static
-std::unique_ptr<FrameResourceCoordinator> FrameResourceCoordinator::Create(
+std::unique_ptr<FrameResourceCoordinator> FrameResourceCoordinator::MaybeCreate(
     service_manager::InterfaceProvider* interface_provider) {
+  if (!RuntimeEnabledFeatures::PerformanceManagerInstrumentationEnabled())
+    return nullptr;
+
   return base::WrapUnique(new FrameResourceCoordinator(interface_provider));
 }
 
diff --git a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.h b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.h
index 88cb69b..f70aa48 100644
--- a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.h
+++ b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/frame_resource_coordinator.h
@@ -5,6 +5,8 @@
 #ifndef THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_RESOURCE_COORDINATOR_FRAME_RESOURCE_COORDINATOR_H_
 #define THIRD_PARTY_BLINK_RENDERER_PLATFORM_INSTRUMENTATION_RESOURCE_COORDINATOR_FRAME_RESOURCE_COORDINATOR_H_
 
+#include <memory>
+
 #include "base/macros.h"
 #include "services/resource_coordinator/public/mojom/coordination_unit.mojom-blink.h"
 #include "third_party/blink/renderer/platform/platform_export.h"
@@ -20,7 +22,8 @@
   USING_FAST_MALLOC(FrameResourceCoordinator);
 
  public:
-  static std::unique_ptr<FrameResourceCoordinator> Create(
+  // Returns nullptr if instrumentation is not enabled.
+  static std::unique_ptr<FrameResourceCoordinator> MaybeCreate(
       service_manager::InterfaceProvider*);
   ~FrameResourceCoordinator();
 
diff --git a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.cc b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.cc
index 9a7de51..4629476 100644
--- a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.cc
+++ b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.cc
@@ -7,6 +7,7 @@
 #include "services/service_manager/public/cpp/connector.h"
 #include "third_party/blink/public/platform/platform.h"
 #include "third_party/blink/renderer/platform/heap/thread_state.h"
+#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
 
 namespace blink {
 
@@ -17,7 +18,10 @@
 }  // namespace
 
 // static
-void RendererResourceCoordinator::Initialize() {
+void RendererResourceCoordinator::MaybeInitialize() {
+  if (!RuntimeEnabledFeatures::PerformanceManagerInstrumentationEnabled())
+    return;
+
   blink::Platform* platform = Platform::Current();
   DCHECK(IsMainThread());
   DCHECK(platform);
@@ -33,9 +37,8 @@
 }
 
 // static
-RendererResourceCoordinator& RendererResourceCoordinator::Get() {
-  DCHECK(g_renderer_resource_coordinator);
-  return *g_renderer_resource_coordinator;
+RendererResourceCoordinator* RendererResourceCoordinator::Get() {
+  return g_renderer_resource_coordinator;
 }
 
 RendererResourceCoordinator::RendererResourceCoordinator(
diff --git a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.h b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.h
index 464c2aa..c7a79e6 100644
--- a/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.h
+++ b/third_party/blink/renderer/platform/instrumentation/resource_coordinator/renderer_resource_coordinator.h
@@ -20,8 +20,9 @@
   USING_FAST_MALLOC(RendererResourceCoordinator);
 
  public:
-  static void Initialize();
-  static RendererResourceCoordinator& Get();
+  // Only initializes if the instrumentation runtime feature is enabled.
+  static void MaybeInitialize();
+  static RendererResourceCoordinator* Get();
 
   // Used to switch the current renderer resource coordinator only for testing.
   static void SetCurrentRendererResourceCoordinatorForTesting(
diff --git a/third_party/blink/renderer/platform/runtime_enabled_features.json5 b/third_party/blink/renderer/platform/runtime_enabled_features.json5
index 16b4465..2bcf056 100644
--- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
+++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
@@ -1067,6 +1067,9 @@
       status: "experimental",
     },
     {
+      name: "PerformanceManagerInstrumentation",
+    },
+    {
       // Flag enabling the performance observers buffered flag. The buffered
       // flag indicates whether or not the buffered entries should be loaded
       // into the observer's buffer upon registration. See crbug.com/725567.
diff --git a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_metrics_helper.cc b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_metrics_helper.cc
index d1b33c3..8708ad5 100644
--- a/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_metrics_helper.cc
+++ b/third_party/blink/renderer/platform/scheduler/main_thread/main_thread_metrics_helper.cc
@@ -576,15 +576,18 @@
   static const int main_thread_task_load_low_threshold =
       ::resource_coordinator::GetMainThreadTaskLoadLowThreshold();
 
-  // Avoid sending duplicate IPCs when the state doesn't change.
-  if (load_percentage <= main_thread_task_load_low_threshold &&
-      main_thread_task_load_state_ != MainThreadTaskLoadState::kLow) {
-    RendererResourceCoordinator::Get().SetMainThreadTaskLoadIsLow(true);
-    main_thread_task_load_state_ = MainThreadTaskLoadState::kLow;
-  } else if (load_percentage > main_thread_task_load_low_threshold &&
-             main_thread_task_load_state_ != MainThreadTaskLoadState::kHigh) {
-    RendererResourceCoordinator::Get().SetMainThreadTaskLoadIsLow(false);
-    main_thread_task_load_state_ = MainThreadTaskLoadState::kHigh;
+  if (auto* renderer_resource_coordinator =
+          RendererResourceCoordinator::Get()) {
+    // Avoid sending duplicate IPCs when the state doesn't change.
+    if (load_percentage <= main_thread_task_load_low_threshold &&
+        main_thread_task_load_state_ != MainThreadTaskLoadState::kLow) {
+      renderer_resource_coordinator->SetMainThreadTaskLoadIsLow(true);
+      main_thread_task_load_state_ = MainThreadTaskLoadState::kLow;
+    } else if (load_percentage > main_thread_task_load_low_threshold &&
+               main_thread_task_load_state_ != MainThreadTaskLoadState::kHigh) {
+      renderer_resource_coordinator->SetMainThreadTaskLoadIsLow(false);
+      main_thread_task_load_state_ = MainThreadTaskLoadState::kHigh;
+    }
   }
 }
 
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 559bba3..f1fa25d 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
@@ -2537,8 +2537,11 @@
                  "estimated_queueing_time_for_window",
                  queueing_time.InMillisecondsF());
 
-  RendererResourceCoordinator::Get().SetExpectedTaskQueueingDuration(
-      queueing_time);
+  if (auto* renderer_resource_coordinator =
+          RendererResourceCoordinator::Get()) {
+    renderer_resource_coordinator->SetExpectedTaskQueueingDuration(
+        queueing_time);
+  }
 }
 
 void MainThreadSchedulerImpl::OnReportFineGrainedExpectedQueueingTime(