bindings: Update performance memory counters and expose legacy values

Legacy values are exposed via Origin Trial "Legacy Performance Memory
Counters", see details at
https://developers.chrome.com/origintrials/#/view_trial/2540030189836959745

Bug: 914304
Change-Id: If4bad6c4341ca7a05cf4f7c64c2850381f71b09a
Reviewed-on: https://chromium-review.googlesource.com/c/1373752
Reviewed-by: Jochen Eisinger <jochen@chromium.org>
Reviewed-by: Kentaro Hara <haraken@chromium.org>
Reviewed-by: Hitoshi Yoshida <peria@chromium.org>
Reviewed-by: Yuki Shiino <yukishiino@chromium.org>
Commit-Queue: Michael Lippautz <mlippautz@chromium.org>
Cr-Commit-Position: refs/heads/master@{#621664}
diff --git a/third_party/blink/renderer/core/timing/memory_info.cc b/third_party/blink/renderer/core/timing/memory_info.cc
index 3c83c44..ebb7951b 100644
--- a/third_party/blink/renderer/core/timing/memory_info.cc
+++ b/third_party/blink/renderer/core/timing/memory_info.cc
@@ -49,9 +49,15 @@
 static void GetHeapSize(HeapInfo& info) {
   v8::HeapStatistics heap_statistics;
   v8::Isolate::GetCurrent()->GetHeapStatistics(&heap_statistics);
-  info.used_js_heap_size = heap_statistics.used_heap_size();
-  info.total_js_heap_size = heap_statistics.total_physical_size();
+  info.used_js_heap_size =
+      heap_statistics.used_heap_size() + heap_statistics.external_memory();
+  info.total_js_heap_size =
+      heap_statistics.total_physical_size() + heap_statistics.external_memory();
   info.js_heap_size_limit = heap_statistics.heap_size_limit();
+  info.used_js_heap_size_without_external_memory =
+      heap_statistics.used_heap_size();
+  info.total_js_heap_size_without_external_memory =
+      heap_statistics.total_physical_size();
 }
 
 class HeapSizeCache {
@@ -96,6 +102,10 @@
     info_.used_js_heap_size = QuantizeMemorySize(info_.used_js_heap_size);
     info_.total_js_heap_size = QuantizeMemorySize(info_.total_js_heap_size);
     info_.js_heap_size_limit = QuantizeMemorySize(info_.js_heap_size_limit);
+    info_.used_js_heap_size_without_external_memory =
+        QuantizeMemorySize(info_.used_js_heap_size_without_external_memory);
+    info_.total_js_heap_size_without_external_memory =
+        QuantizeMemorySize(info_.total_js_heap_size_without_external_memory);
   }
 
   base::Optional<TimeTicks> last_update_time_;
diff --git a/third_party/blink/renderer/core/timing/memory_info.h b/third_party/blink/renderer/core/timing/memory_info.h
index ffd71db6..af93db2 100644
--- a/third_party/blink/renderer/core/timing/memory_info.h
+++ b/third_party/blink/renderer/core/timing/memory_info.h
@@ -40,12 +40,13 @@
 
 struct HeapInfo {
   DISALLOW_NEW();
-  HeapInfo()
-      : used_js_heap_size(0), total_js_heap_size(0), js_heap_size_limit(0) {}
 
-  size_t used_js_heap_size;
-  size_t total_js_heap_size;
-  size_t js_heap_size_limit;
+  size_t used_js_heap_size = 0;
+  size_t total_js_heap_size = 0;
+  size_t js_heap_size_limit = 0;
+  // Values for origin trial: "Legacy Performance Memory Counters".
+  size_t used_js_heap_size_without_external_memory = 0;
+  size_t total_js_heap_size_without_external_memory = 0;
 };
 
 class CORE_EXPORT MemoryInfo final : public ScriptWrappable {
@@ -66,6 +67,12 @@
   size_t totalJSHeapSize() const { return info_.total_js_heap_size; }
   size_t usedJSHeapSize() const { return info_.used_js_heap_size; }
   size_t jsHeapSizeLimit() const { return info_.js_heap_size_limit; }
+  size_t usedJSHeapSizeWithoutExternalMemory() const {
+    return info_.used_js_heap_size_without_external_memory;
+  }
+  size_t totalJSHeapSizeWithoutExternalMemory() const {
+    return info_.total_js_heap_size_without_external_memory;
+  }
 
  private:
   HeapInfo info_;
diff --git a/third_party/blink/renderer/core/timing/memory_info.idl b/third_party/blink/renderer/core/timing/memory_info.idl
index dbbf246..f3f531e 100644
--- a/third_party/blink/renderer/core/timing/memory_info.idl
+++ b/third_party/blink/renderer/core/timing/memory_info.idl
@@ -37,4 +37,6 @@
     [Measure] readonly attribute unsigned long long totalJSHeapSize;
     [Measure] readonly attribute unsigned long long usedJSHeapSize;
     [Measure] readonly attribute unsigned long long jsHeapSizeLimit;
+    [OriginTrialEnabled=LegacyPerformanceMemoryCounters] readonly attribute unsigned long long usedJSHeapSizeWithoutExternalMemory;
+    [OriginTrialEnabled=LegacyPerformanceMemoryCounters] readonly attribute unsigned long long totalJSHeapSizeWithoutExternalMemory;
 };
diff --git a/third_party/blink/renderer/platform/runtime_enabled_features.json5 b/third_party/blink/renderer/platform/runtime_enabled_features.json5
index 571725c..27b661a 100644
--- a/third_party/blink/renderer/platform/runtime_enabled_features.json5
+++ b/third_party/blink/renderer/platform/runtime_enabled_features.json5
@@ -710,6 +710,10 @@
       // This is enabled by features::kLazyInitializeMediaControls.
     },
     {
+      name: "LegacyPerformanceMemoryCounters",
+      origin_trial_feature_name: "LegacyPerformanceMemoryCounters",
+    },
+    {
       name: "LongTaskV2",
     },
     {
diff --git a/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-disabled.html b/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-disabled.html
new file mode 100644
index 0000000..a3524ef
--- /dev/null
+++ b/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-disabled.html
@@ -0,0 +1,15 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script>
+
+test(t => {
+  assert_true(
+      performance.memory.usedJSHeapSizeWithoutExternalMemory === undefined);
+  assert_true(
+      performance.memory.totalJSHeapSizeWithoutExternalMemory === undefined);
+}, "Legacy performance memory counters are disabled without Origin Trial.");
+
+</script>
diff --git a/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-enabled.html b/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-enabled.html
new file mode 100644
index 0000000..4d6f6a9
--- /dev/null
+++ b/third_party/blink/web_tests/http/tests/origin_trials/webexposed/legacy-performance-memory-counters-enabled.html
@@ -0,0 +1,32 @@
+<!DOCTYPE html>
+<meta charset="utf-8">
+<!-- Generate this token with the command:
+generate_token.py --expire-timestamp=2000000000 http://127.0.0.1:8000 LegacyPerformanceMemoryCounters
+-->
+
+<meta http-equiv="origin-trial" content="AkkvkocWTiiH0+VhdTlxue2ibIiQ0Gy2Dp4HL5CJEEDHqSovu8L6CCqmHRz7PDWm9EgLLj9xpGXbvxxDdaN8cwAAAABneyJvcmlnaW4iOiAiaHR0cDovLzEyNy4wLjAuMTo4MDAwIiwgImZlYXR1cmUiOiAiTGVnYWN5UGVyZm9ybWFuY2VNZW1vcnlDb3VudGVycyIsICJleHBpcnkiOiAyMDAwMDAwMDAwfQ==" />
+
+<script src="../../../../resources/testharness.js"></script>
+<script src="../../../../resources/testharnessreport.js"></script>
+<script>
+
+test(t => {
+  assert_idl_attribute(performance.memory,
+      "usedJSHeapSizeWithoutExternalMemory",
+      "used JS heap size without external memory");
+  assert_idl_attribute(performance.memory,
+      "totalJSHeapSizeWithoutExternalMemory",
+      "total JS heap size without external memory");
+  const snapshot = performance.memory;
+  assert_less_than_equal(snapshot.usedJSHeapSizeWithoutExternalMemory,
+      snapshot.usedJSHeapSize,
+      "used without external memory <= used overall");
+  assert_less_than_equal(snapshot.totalJSHeapSizeWithoutExternalMemory,
+      snapshot.totalJSHeapSize,
+      "total without external memory <= total overall");
+  assert_less_than_equal(snapshot.usedJSHeapSizeWithoutExternalMemory,
+      snapshot.totalJSHeapSizeWithoutExternalMemory,
+      "used without external memory <= total without external memory");
+}, "Test legacy performance memory counters are enabled via origin trial.");
+
+</script>