Create and pass shared-histogram-allocator from browser to renderer.

After the renderer is started, a shared-memory segment
is created and sent to it; all renderer histograms
created after that point will be stored there.

The SubprocessMetricsProvider extracts that shared
segment on the browser side and, when metrics are being
uploaded, iterates through all the histograms stored
there.

When the renderer exits, the SMP will continue to hold
a handle to the memory segment.  Once all the histograms
are uploaded (during the next cycle), it will finally
release the memory back to the operating system.

BUG=546019

Committed: https://crrev.com/df016a7ce60decb9174ab2201bf4864fc7cefa82
Cr-Commit-Position: refs/heads/master@{#385873}

Review URL: https://codereview.chromium.org/1671933002

Cr-Commit-Position: refs/heads/master@{#386055}


CrOS-Libchrome-Original-Commit: 5370ba54d267e69cc055c75738306644a93454cf
diff --git a/base/metrics/persistent_histogram_allocator.cc b/base/metrics/persistent_histogram_allocator.cc
index 864753b..fe2fdef 100644
--- a/base/metrics/persistent_histogram_allocator.cc
+++ b/base/metrics/persistent_histogram_allocator.cc
@@ -480,15 +480,16 @@
     size_t size,
     uint64_t id,
     StringPiece name) {
-  if (!memory->memory() && !memory->Map(size))
+  if ((!memory->memory() && !memory->Map(size)) ||
+      !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*memory)) {
     NOTREACHED();
-
-  if (memory->memory()) {
-    DCHECK_LE(memory->mapped_size(), size);
-    Set(WrapUnique(new GlobalHistogramAllocator(
-        WrapUnique(new SharedPersistentMemoryAllocator(
-            std::move(memory), 0, StringPiece(), /*readonly=*/false)))));
+    return;
   }
+
+  DCHECK_LE(memory->mapped_size(), size);
+  Set(WrapUnique(new GlobalHistogramAllocator(
+      WrapUnique(new SharedPersistentMemoryAllocator(
+          std::move(memory), 0, StringPiece(), /*readonly=*/false)))));
 }
 
 // static
@@ -497,7 +498,8 @@
     size_t size) {
   std::unique_ptr<SharedMemory> shm(
       new SharedMemory(handle, /*readonly=*/false));
-  if (!shm->Map(size)) {
+  if (!shm->Map(size) ||
+      !SharedPersistentMemoryAllocator::IsSharedMemoryAcceptable(*shm)) {
     NOTREACHED();
     return;
   }
diff --git a/base/metrics/persistent_histogram_allocator.h b/base/metrics/persistent_histogram_allocator.h
index 12ac685..b2038c4 100644
--- a/base/metrics/persistent_histogram_allocator.h
+++ b/base/metrics/persistent_histogram_allocator.h
@@ -116,13 +116,6 @@
   // Histogram containing creation results. Visible for testing.
   static HistogramBase* GetCreateHistogramResultHistogram();
 
-  // This access to the persistent allocator is only for testing; it extracts
-  // the current allocator completely. This allows easy creation of histograms
-  // within persistent memory segments which can then be extracted and used
-  // in other ways.
-  static std::unique_ptr<PersistentHistogramAllocator>
-  ReleaseGlobalAllocatorForTesting();
-
  protected:
   // The structure used to hold histogram data in persistent memory. It is
   // defined and used entirely within the .cc file.