SharedMemoryTracker will stop tracking memory if TakeHandle() was called

When TakeHandle() is called, the SharedMemory instance resets itself
and transfers ownership to the caller. In this case stop tracking the
instance since Unmap() will be noop since memory will be nullptr.
On Mac TakeHandle() just duplicates the handle and doesn't reset the
current instance. So, still tracking this memory is ok.
Note: This will change how private memory footprint is calculated since
we no longer track some shared memory instances.

BUG=804399

Change-Id: I28ed88bfee2033cdf07595d314505413f067e49b
Reviewed-on: https://chromium-review.googlesource.com/884999
Commit-Queue: Siddhartha S <ssid@chromium.org>
Reviewed-by: Daniel Cheng <dcheng@chromium.org>
Reviewed-by: Hajime Hoshi <hajimehoshi@chromium.org>
Reviewed-by: Erik Chen <erikchen@chromium.org>
Cr-Original-Commit-Position: refs/heads/master@{#533570}(cherry picked from commit a0996a237aaec59e2973eeed7f58e55f13169bc3)
Reviewed-on: https://chromium-review.googlesource.com/929721
Reviewed-by: Siddhartha S <ssid@chromium.org>
Cr-Commit-Position: refs/branch-heads/3325@{#541}
Cr-Branched-From: bc084a8b5afa3744a74927344e304c02ae54189f-refs/heads/master@{#530369}
diff --git a/base/memory/shared_memory_fuchsia.cc b/base/memory/shared_memory_fuchsia.cc
index 15211d9..a76f4f1 100644
--- a/base/memory/shared_memory_fuchsia.cc
+++ b/base/memory/shared_memory_fuchsia.cc
@@ -139,6 +139,12 @@
   SharedMemoryHandle handle(shm_);
   handle.SetOwnershipPassesToIPC(true);
   shm_ = SharedMemoryHandle();
+  // TODO(ssid): Find some way to track the shared memory in this case
+  // https://crbug/804399.
+  if (memory_) {
+    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
+    mapped_id_ = UnguessableToken();
+  }
   memory_ = nullptr;
   mapped_size_ = 0;
   return handle;
diff --git a/base/memory/shared_memory_nacl.cc b/base/memory/shared_memory_nacl.cc
index 442c036..6ac0b3d 100644
--- a/base/memory/shared_memory_nacl.cc
+++ b/base/memory/shared_memory_nacl.cc
@@ -118,6 +118,12 @@
   SharedMemoryHandle handle_copy = shm_;
   handle_copy.SetOwnershipPassesToIPC(true);
   shm_ = SharedMemoryHandle();
+  // TODO(ssid): Find some way to track the shared memory in this case
+  // https://crbug/804399.
+  if (memory_) {
+    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
+    mapped_id_ = UnguessableToken();
+  }
   memory_ = nullptr;
   mapped_size_ = 0;
   return handle_copy;
diff --git a/base/memory/shared_memory_posix.cc b/base/memory/shared_memory_posix.cc
index f9d71f4..d9e03a0 100644
--- a/base/memory/shared_memory_posix.cc
+++ b/base/memory/shared_memory_posix.cc
@@ -336,6 +336,12 @@
 SharedMemoryHandle SharedMemory::TakeHandle() {
   SharedMemoryHandle handle_copy = shm_;
   handle_copy.SetOwnershipPassesToIPC(true);
+  // TODO(ssid): Find some way to track the shared memory in this case
+  // https://crbug/804399.
+  if (memory_) {
+    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
+    mapped_id_ = UnguessableToken();
+  }
   shm_ = SharedMemoryHandle();
   memory_ = nullptr;
   mapped_size_ = 0;
diff --git a/base/memory/shared_memory_win.cc b/base/memory/shared_memory_win.cc
index 5540004..2130218 100644
--- a/base/memory/shared_memory_win.cc
+++ b/base/memory/shared_memory_win.cc
@@ -374,6 +374,12 @@
   SharedMemoryHandle handle(shm_);
   handle.SetOwnershipPassesToIPC(true);
   shm_ = SharedMemoryHandle();
+  // TODO(ssid): Find some way to track the shared memory in this case
+  // https://crbug/804399.
+  if (memory_) {
+    SharedMemoryTracker::GetInstance()->DecrementMemoryUsage(*this);
+    mapped_id_ = UnguessableToken();
+  }
   memory_ = nullptr;
   mapped_size_ = 0;
   return handle;