metrics: Convert metrics allocators to the new shared memory API

This is a refactoring CL that switches all usages of obsolete
base::SharedMemory and base::SharedMemoryHandle classes in metrics allocators
to the new shared memory API.

No behavioral change is expected.

The browser process creates one shared memory allocator per child process. The
shared memory is writable both in the parent and the child processes. Thus, the
base::WritableSharedMemoryRegion class is used.

Bug: 920183
Change-Id: I31df2ca2eefb606b053e255cf06a91a0d3e1038c
Reviewed-on: https://chromium-review.googlesource.com/c/1477121
Commit-Queue: Alex Ilin <[email protected]>
Reviewed-by: Robert Sesek <[email protected]>
Reviewed-by: Brian White <[email protected]>
Reviewed-by: Alex Moshchuk <[email protected]>
Cr-Commit-Position: refs/heads/master@{#634158}
diff --git a/content/browser/browser_child_process_host_impl.cc b/content/browser/browser_child_process_host_impl.cc
index 23a010b..fdf81747 100644
--- a/content/browser/browser_child_process_host_impl.cc
+++ b/content/browser/browser_child_process_host_impl.cc
@@ -266,7 +266,7 @@
   return child_process_->GetProcess();
 }
 
-std::unique_ptr<base::SharedPersistentMemoryAllocator>
+std::unique_ptr<base::PersistentMemoryAllocator>
 BrowserChildProcessHostImpl::TakeMetricsAllocator() {
   return std::move(metrics_allocator_);
 }
@@ -568,25 +568,25 @@
   // Create the shared memory segment and attach an allocator to it.
   // Mapping the memory shouldn't fail but be safe if it does; everything
   // will continue to work but just as if persistence weren't available.
-  std::unique_ptr<base::SharedMemory> shm(new base::SharedMemory());
-  if (!shm->CreateAndMapAnonymous(memory_size))
+  base::WritableSharedMemoryRegion shm_region =
+      base::WritableSharedMemoryRegion::Create(memory_size);
+  base::WritableSharedMemoryMapping shm_mapping = shm_region.Map();
+  if (!shm_region.IsValid() || !shm_mapping.IsValid())
     return;
-  metrics_allocator_.reset(new base::SharedPersistentMemoryAllocator(
-      std::move(shm), static_cast<uint64_t>(data_.id), metrics_name,
-      /*readonly=*/false));
+  metrics_allocator_ =
+      std::make_unique<base::WritableSharedPersistentMemoryAllocator>(
+          std::move(shm_mapping), static_cast<uint64_t>(data_.id),
+          metrics_name);
+  metrics_shared_region_ = std::move(shm_region);
 }
 
 void BrowserChildProcessHostImpl::ShareMetricsAllocatorToProcess() {
   if (metrics_allocator_) {
     HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
-        GetHost(),
-        mojo::WrapSharedMemoryHandle(
-            metrics_allocator_->shared_memory()->handle().Duplicate(),
-            metrics_allocator_->shared_memory()->mapped_size(),
-            mojo::UnwrappedSharedMemoryHandleProtection::kReadWrite));
+        GetHost(), std::move(metrics_shared_region_));
   } else {
     HistogramController::GetInstance()->SetHistogramMemory<ChildProcessHost>(
-        GetHost(), mojo::ScopedSharedBufferHandle());
+        GetHost(), base::WritableSharedMemoryRegion());
   }
 }