mac: Make Mach port scopers better ScopedGenerics

Previously, Pass() did not work correctly for ScopedMachReceiveRight,
ScopedMachSendRight, or ScopedMachPortSet. These were defined as
subclasses of ScopedGeneric<> with appropriate traits types. They did
not have the full range of constructors made available by
ScopedGeneric<>, and their Pass() methods referred to their
ScopedGeneric<> superclass rather than their proper class types.

This changes these scopers to work as ScopedGeneric<> intends, with a
"using" or "typedef" declaration, so that names such as
ScopedMachReceiveRight actually refer to the same type as the underlying
ScopedGeneric<>. This allows Pass() and all other ScopedGeneric<>
functionality to work as intended.

Unfortunately, ScopedGeneric<> doesn't provide a type conversion
operator to the underlying wrapped type, so many use sites need to be
transformed to use the get() accessor. Many existing use sites already
used this accessor.

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

Cr-Commit-Position: refs/heads/master@{#355112}
diff --git a/base/mac/scoped_mach_port.h b/base/mac/scoped_mach_port.h
index beb62b0..67fed6b 100644
--- a/base/mac/scoped_mach_port.h
+++ b/base/mac/scoped_mach_port.h
@@ -45,40 +45,21 @@
 // reference counted, and this takes ownership of the right on construction
 // and then removes a reference to the right on destruction. If the reference
 // is the last one on the right, the right is deallocated.
-class BASE_EXPORT ScopedMachSendRight :
-    public base::ScopedGeneric<mach_port_t, internal::SendRightTraits> {
- public:
-  explicit ScopedMachSendRight(mach_port_t port = traits_type::InvalidValue())
-      : ScopedGeneric(port) {}
-
-  operator mach_port_t() const { return get(); }
-};
+using ScopedMachSendRight =
+    ScopedGeneric<mach_port_t, internal::SendRightTraits>;
 
 // A scoper for handling a Mach port's receive right. There is only one
 // receive right per port. This takes ownership of the receive right on
 // construction and then destroys the right on destruction, turning all
 // outstanding send rights into dead names.
-class BASE_EXPORT ScopedMachReceiveRight :
-    public base::ScopedGeneric<mach_port_t, internal::ReceiveRightTraits> {
- public:
-  explicit ScopedMachReceiveRight(
-      mach_port_t port = traits_type::InvalidValue()) : ScopedGeneric(port) {}
-
-  operator mach_port_t() const { return get(); }
-};
+using ScopedMachReceiveRight =
+    ScopedGeneric<mach_port_t, internal::ReceiveRightTraits>;
 
 // A scoper for handling a Mach port set. A port set can have only one
 // reference. This takes ownership of that single reference on construction and
 // destroys the port set on destruction. Destroying a port set does not destroy
 // the receive rights that are members of the port set.
-class BASE_EXPORT ScopedMachPortSet :
-    public ScopedGeneric<mach_port_t, internal::PortSetTraits> {
- public:
-  explicit ScopedMachPortSet(mach_port_t port = traits_type::InvalidValue())
-      : ScopedGeneric(port) {}
-
-  operator mach_port_t() const { return get(); }
-};
+using ScopedMachPortSet = ScopedGeneric<mach_port_t, internal::PortSetTraits>;
 
 }  // namespace mac
 }  // namespace base
diff --git a/base/memory/shared_memory_mac_unittest.cc b/base/memory/shared_memory_mac_unittest.cc
index d5845b3c..e4c5e8b19 100644
--- a/base/memory/shared_memory_mac_unittest.cc
+++ b/base/memory/shared_memory_mac_unittest.cc
@@ -126,7 +126,7 @@
   mach_port_t client_port = MakeReceivingPort();
 
   // Send the port that this process is listening on to the server.
-  SendMachPort(server_port, client_port, MACH_MSG_TYPE_MAKE_SEND);
+  SendMachPort(server_port.get(), client_port, MACH_MSG_TYPE_MAKE_SEND);
   return client_port;
 }
 
@@ -161,7 +161,7 @@
     service_name_ = CreateRandomServiceName();
     server_port_.reset(BecomeMachServer(service_name_.c_str()));
     child_process_ = SpawnChild(name);
-    client_port_.reset(ReceiveMachPort(server_port_));
+    client_port_.reset(ReceiveMachPort(server_port_.get()));
   }
 
   static const int s_memory_size = 99999;
@@ -193,7 +193,8 @@
   memset(shared_memory.memory(), 'a', s_memory_size);
 
   // Send the underlying memory object to the client process.
-  SendMachPort(client_port_, shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND);
+  SendMachPort(
+      client_port_.get(), shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND);
   int rv = -1;
   ASSERT_TRUE(child_process_.WaitForExitWithTimeout(
       TestTimeouts::action_timeout(), &rv));
@@ -203,7 +204,7 @@
 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryClient) {
   mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp());
   // The next mach port should be for a memory object.
-  mach_port_t memory_object = ReceiveMachPort(client_port);
+  mach_port_t memory_object = ReceiveMachPort(client_port.get());
   SharedMemoryHandle shm(memory_object,
                          SharedMemoryMacMultiProcessTest::s_memory_size,
                          GetCurrentProcId());
@@ -232,7 +233,8 @@
   memset(start + 2 * page_size, 'c', page_size);
 
   // Send the underlying memory object to the client process.
-  SendMachPort(client_port_, shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND);
+  SendMachPort(
+      client_port_.get(), shm.GetMemoryObject(), MACH_MSG_TYPE_COPY_SEND);
   int rv = -1;
   ASSERT_TRUE(child_process_.WaitForExitWithTimeout(
       TestTimeouts::action_timeout(), &rv));
@@ -242,7 +244,7 @@
 MULTIPROCESS_TEST_MAIN(MachBasedSharedMemoryWithOffsetClient) {
   mac::ScopedMachReceiveRight client_port(CommonChildProcessSetUp());
   // The next mach port should be for a memory object.
-  mach_port_t memory_object = ReceiveMachPort(client_port);
+  mach_port_t memory_object = ReceiveMachPort(client_port.get());
   SharedMemoryHandle shm(memory_object,
                          SharedMemoryMacMultiProcessTest::s_memory_size,
                          GetCurrentProcId());
diff --git a/base/process/process_metrics_mac.cc b/base/process/process_metrics_mac.cc
index a2ecd8e..82acb06 100644
--- a/base/process/process_metrics_mac.cc
+++ b/base/process/process_metrics_mac.cc
@@ -352,7 +352,7 @@
   base::mac::ScopedMachSendRight host(mach_host_self());
   mach_msg_type_number_t count = HOST_VM_INFO_COUNT;
   vm_statistics_data_t data;
-  kern_return_t kr = host_statistics(host, HOST_VM_INFO,
+  kern_return_t kr = host_statistics(host.get(), HOST_VM_INFO,
                                      reinterpret_cast<host_info_t>(&data),
                                      &count);
   if (kr != KERN_SUCCESS) {
@@ -368,7 +368,7 @@
   struct host_basic_info hostinfo;
   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
   base::mac::ScopedMachSendRight host(mach_host_self());
-  int result = host_info(host, HOST_BASIC_INFO,
+  int result = host_info(host.get(), HOST_BASIC_INFO,
                          reinterpret_cast<host_info_t>(&hostinfo), &count);
   if (result != KERN_SUCCESS)
     return false;
diff --git a/base/sys_info_ios.mm b/base/sys_info_ios.mm
index 0e24a2a4..5aa31809 100644
--- a/base/sys_info_ios.mm
+++ b/base/sys_info_ios.mm
@@ -66,7 +66,7 @@
   struct host_basic_info hostinfo;
   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
   base::mac::ScopedMachSendRight host(mach_host_self());
-  int result = host_info(host,
+  int result = host_info(host.get(),
                          HOST_BASIC_INFO,
                          reinterpret_cast<host_info_t>(&hostinfo),
                          &count);
diff --git a/base/sys_info_mac.cc b/base/sys_info_mac.cc
index 18df6248..025c768 100644
--- a/base/sys_info_mac.cc
+++ b/base/sys_info_mac.cc
@@ -46,7 +46,7 @@
   struct host_basic_info hostinfo;
   mach_msg_type_number_t count = HOST_BASIC_INFO_COUNT;
   base::mac::ScopedMachSendRight host(mach_host_self());
-  int result = host_info(host,
+  int result = host_info(host.get(),
                          HOST_BASIC_INFO,
                          reinterpret_cast<host_info_t>(&hostinfo),
                          &count);
diff --git a/base/time/time_mac.cc b/base/time/time_mac.cc
index 1dbbc337..8983ab2 100644
--- a/base/time/time_mac.cc
+++ b/base/time/time_mac.cc
@@ -83,7 +83,7 @@
   }
 
   kern_return_t kr = thread_info(
-      thread,
+      thread.get(),
       THREAD_BASIC_INFO,
       reinterpret_cast<thread_info_t>(&thread_info_data),
       &thread_info_count);
diff --git a/content/browser/browser_io_surface_manager_mac.cc b/content/browser/browser_io_surface_manager_mac.cc
index 898fa4b2..3d42d00 100644
--- a/content/browser/browser_io_surface_manager_mac.cc
+++ b/content/browser/browser_io_surface_manager_mac.cc
@@ -192,7 +192,7 @@
 
   kern_return_t kr =
       mach_msg(&request.msg.header, MACH_RCV_MSG, 0, sizeof(request),
-               server_port_, MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
+               server_port_.get(), MACH_MSG_TIMEOUT_NONE, MACH_PORT_NULL);
   if (kr != KERN_SUCCESS) {
     MACH_LOG(ERROR, kr) << "mach_msg";
     return;
diff --git a/content/browser/mach_broker_mac.mm b/content/browser/mach_broker_mac.mm
index d42bbeed..4cf3bb6c 100644
--- a/content/browser/mach_broker_mac.mm
+++ b/content/browser/mach_broker_mac.mm
@@ -200,7 +200,7 @@
                               options,
                               0,
                               sizeof(msg),
-                              server_port_,
+                              server_port_.get(),
                               MACH_MSG_TIMEOUT_NONE,
                               MACH_PORT_NULL);
   if (kr != KERN_SUCCESS) {
diff --git a/content/child/child_io_surface_manager_mac.cc b/content/child/child_io_surface_manager_mac.cc
index 883defe9..063fe4c 100644
--- a/content/child/child_io_surface_manager_mac.cc
+++ b/content/child/child_io_surface_manager_mac.cc
@@ -45,12 +45,12 @@
   data.request.header.msgh_bits =
       MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE) |
       MACH_MSGH_BITS_COMPLEX;
-  data.request.header.msgh_remote_port = service_port_;
+  data.request.header.msgh_remote_port = service_port_.get();
   data.request.header.msgh_local_port = reply_port;
   data.request.header.msgh_size = sizeof(data.request);
   data.request.header.msgh_id = IOSurfaceManagerHostMsg_RegisterIOSurface::ID;
   data.request.body.msgh_descriptor_count = 1;
-  data.request.io_surface_port.name = scoped_io_surface_right;
+  data.request.io_surface_port.name = scoped_io_surface_right.get();
   data.request.io_surface_port.disposition = MACH_MSG_TYPE_COPY_SEND;
   data.request.io_surface_port.type = MACH_MSG_PORT_DESCRIPTOR;
   data.request.io_surface_id = io_surface_id.id;
@@ -75,7 +75,7 @@
 
   IOSurfaceManagerHostMsg_UnregisterIOSurface request = {{0}};
   request.header.msgh_bits = MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, 0);
-  request.header.msgh_remote_port = service_port_;
+  request.header.msgh_remote_port = service_port_.get();
   request.header.msgh_local_port = MACH_PORT_NULL;
   request.header.msgh_size = sizeof(request);
   request.header.msgh_id = IOSurfaceManagerHostMsg_UnregisterIOSurface::ID;
@@ -114,7 +114,7 @@
   } data = {{{0}}};
   data.request.header.msgh_bits =
       MACH_MSGH_BITS(MACH_MSG_TYPE_COPY_SEND, MACH_MSG_TYPE_MAKE_SEND_ONCE);
-  data.request.header.msgh_remote_port = service_port_;
+  data.request.header.msgh_remote_port = service_port_.get();
   data.request.header.msgh_local_port = reply_port;
   data.request.header.msgh_size = sizeof(data.request);
   data.request.header.msgh_id = IOSurfaceManagerHostMsg_AcquireIOSurface::ID;
@@ -137,7 +137,7 @@
   base::mac::ScopedMachSendRight scoped_io_surface_right(
       data.reply.msg.io_surface_port.name);
 
-  return IOSurfaceLookupFromMachPort(scoped_io_surface_right);
+  return IOSurfaceLookupFromMachPort(scoped_io_surface_right.get());
 }
 
 ChildIOSurfaceManager::ChildIOSurfaceManager() {}
diff --git a/ipc/attachment_broker_mac_unittest.cc b/ipc/attachment_broker_mac_unittest.cc
index a801b10..8f2aad0b 100644
--- a/ipc/attachment_broker_mac_unittest.cc
+++ b/ipc/attachment_broker_mac_unittest.cc
@@ -383,8 +383,9 @@
     ASSERT_TRUE(ConnectChannel());
     ASSERT_TRUE(StartClient());
 
-    client_port_.reset(IPC::ReceiveMachPort(server_port_).release());
-    IPC::SendMachPort(client_port_, mach_task_self(), MACH_MSG_TYPE_COPY_SEND);
+    client_port_.reset(IPC::ReceiveMachPort(server_port_.get()).release());
+    IPC::SendMachPort(
+        client_port_.get(), mach_task_self(), MACH_MSG_TYPE_COPY_SEND);
     active_names_at_start_ = IPC::GetActiveNameCount();
     get_proxy_listener()->set_listener(&result_listener_);
   }
@@ -470,14 +471,15 @@
   base::mac::ScopedMachReceiveRight client_port(IPC::MakeReceivingPort());
 
   // Send the port that this process is listening on to the server.
-  IPC::SendMachPort(server_port, client_port, MACH_MSG_TYPE_MAKE_SEND);
+  IPC::SendMachPort(
+      server_port.get(), client_port.get(), MACH_MSG_TYPE_MAKE_SEND);
 
   // Receive the task port of the server process.
   base::mac::ScopedMachSendRight server_task_port(
-      IPC::ReceiveMachPort(client_port));
+      IPC::ReceiveMachPort(client_port.get()));
 
   scoped_ptr<ChildProcessGlobals> globals(new ChildProcessGlobals);
-  globals->port_provider.InsertEntry(getppid(), server_task_port);
+  globals->port_provider.InsertEntry(getppid(), server_task_port.get());
   globals->broker.SetPortProvider(&globals->port_provider);
   globals->server_task_port.reset(server_task_port.release());
   return globals;
diff --git a/ipc/attachment_broker_privileged_mac.cc b/ipc/attachment_broker_privileged_mac.cc
index 29e7833..0eb65a0 100644
--- a/ipc/attachment_broker_privileged_mac.cc
+++ b/ipc/attachment_broker_privileged_mac.cc
@@ -231,7 +231,8 @@
             send_right_type);
 
   // This call takes ownership of |send_once_right|.
-  kr = SendMachPort(send_once_right, port_to_insert, MACH_MSG_TYPE_COPY_SEND);
+  kr = SendMachPort(
+      send_once_right, port_to_insert.get(), MACH_MSG_TYPE_COPY_SEND);
   if (kr != KERN_SUCCESS) {
     // TODO(erikchen): UMA metric.
     mach_port_deallocate(task_port, endpoint);
diff --git a/ipc/attachment_broker_privileged_mac_unittest.cc b/ipc/attachment_broker_privileged_mac_unittest.cc
index 6231ef8..c396ea3 100644
--- a/ipc/attachment_broker_privileged_mac_unittest.cc
+++ b/ipc/attachment_broker_privileged_mac_unittest.cc
@@ -89,10 +89,10 @@
   *original_name_count = GetActiveNameCount() - 1;
 
   // Send the port that this process is listening on to the server.
-  SendMachPort(server_port, client_port, MACH_MSG_TYPE_MAKE_SEND);
+  SendMachPort(server_port.get(), client_port.get(), MACH_MSG_TYPE_MAKE_SEND);
 
   // Send the task port for this process.
-  SendMachPort(server_port, mach_task_self(), MACH_MSG_TYPE_COPY_SEND);
+  SendMachPort(server_port.get(), mach_task_self(), MACH_MSG_TYPE_COPY_SEND);
   return client_port;
 }
 
@@ -138,8 +138,8 @@
     service_name_ = CreateRandomServiceName();
     server_port_.reset(BecomeMachServer(service_name_.c_str()).release());
     child_process_ = SpawnChild(name);
-    client_port_.reset(ReceiveMachPort(server_port_).release());
-    client_task_port_.reset(ReceiveMachPort(server_port_).release());
+    client_port_.reset(ReceiveMachPort(server_port_.get()).release());
+    client_task_port_.reset(ReceiveMachPort(server_port_.get()).release());
   }
 
   static const int s_memory_size = 99999;
@@ -179,11 +179,11 @@
   IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
                         MACH_PORT_RIGHT_SEND);
   mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
-      client_task_port_, base::mac::ScopedMachSendRight(
-                             shared_memory->handle().GetMemoryObject()));
+      client_task_port_.get(), base::mac::ScopedMachSendRight(
+                                   shared_memory->handle().GetMemoryObject()));
   EXPECT_NE(inserted_memory_object,
             static_cast<mach_port_name_t>(MACH_PORT_NULL));
-  SendUInt32(client_port_, inserted_memory_object);
+  SendUInt32(client_port_.get(), inserted_memory_object);
 
   // Check that no names have been leaked.
   shared_memory.reset();
@@ -199,8 +199,10 @@
   mach_msg_type_number_t original_name_count = 0;
   base::mac::ScopedMachReceiveRight client_port(
       CommonChildProcessSetUp(&original_name_count).release());
-  base::mac::ScopedMachReceiveRight inserted_port(ReceiveUInt32(client_port));
-  base::mac::ScopedMachSendRight memory_object(ReceiveMachPort(inserted_port));
+  base::mac::ScopedMachReceiveRight inserted_port(
+      ReceiveUInt32(client_port.get()));
+  base::mac::ScopedMachSendRight memory_object(
+      ReceiveMachPort(inserted_port.get()));
   inserted_port.reset();
 
   // The server should have inserted a right into this process.
@@ -241,11 +243,12 @@
     IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
                           MACH_PORT_RIGHT_SEND);
     mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
-        client_task_port_, base::mac::ScopedMachSendRight(
-                               shared_memory->handle().GetMemoryObject()));
+        client_task_port_.get(),
+        base::mac::ScopedMachSendRight(
+            shared_memory->handle().GetMemoryObject()));
     EXPECT_NE(inserted_memory_object,
               static_cast<mach_port_name_t>(MACH_PORT_NULL));
-    SendUInt32(client_port_, inserted_memory_object);
+    SendUInt32(client_port_.get(), inserted_memory_object);
   }
 
   // Check that no names have been leaked.
@@ -264,11 +267,14 @@
       CommonChildProcessSetUp(&original_name_count).release());
 
   // Receive two memory objects.
-  base::mac::ScopedMachReceiveRight inserted_port(ReceiveUInt32(client_port));
-  base::mac::ScopedMachReceiveRight inserted_port2(ReceiveUInt32(client_port));
-  base::mac::ScopedMachSendRight memory_object(ReceiveMachPort(inserted_port));
+  base::mac::ScopedMachReceiveRight inserted_port(
+      ReceiveUInt32(client_port.get()));
+  base::mac::ScopedMachReceiveRight inserted_port2(
+      ReceiveUInt32(client_port.get()));
+  base::mac::ScopedMachSendRight memory_object(
+      ReceiveMachPort(inserted_port.get()));
   base::mac::ScopedMachSendRight memory_object2(
-      ReceiveMachPort(inserted_port2));
+      ReceiveMachPort(inserted_port2.get()));
   inserted_port.reset();
   inserted_port2.reset();
 
@@ -329,11 +335,12 @@
     IncrementMachRefCount(shared_memory->handle().GetMemoryObject(),
                           MACH_PORT_RIGHT_SEND);
     mach_port_name_t inserted_memory_object = broker.CreateIntermediateMachPort(
-        client_task_port_, base::mac::ScopedMachSendRight(
-                               shared_memory->handle().GetMemoryObject()));
+        client_task_port_.get(),
+        base::mac::ScopedMachSendRight(
+            shared_memory->handle().GetMemoryObject()));
     EXPECT_NE(inserted_memory_object,
               static_cast<mach_port_name_t>(MACH_PORT_NULL));
-    SendUInt32(client_port_, inserted_memory_object);
+    SendUInt32(client_port_.get(), inserted_memory_object);
   }
 
   // Check that no names have been leaked.
@@ -351,11 +358,14 @@
       CommonChildProcessSetUp(&original_name_count).release());
 
   // Receive two memory objects.
-  base::mac::ScopedMachReceiveRight inserted_port(ReceiveUInt32(client_port));
-  base::mac::ScopedMachReceiveRight inserted_port2(ReceiveUInt32(client_port));
-  base::mac::ScopedMachSendRight memory_object(ReceiveMachPort(inserted_port));
+  base::mac::ScopedMachReceiveRight inserted_port(
+      ReceiveUInt32(client_port.get()));
+  base::mac::ScopedMachReceiveRight inserted_port2(
+      ReceiveUInt32(client_port.get()));
+  base::mac::ScopedMachSendRight memory_object(
+      ReceiveMachPort(inserted_port.get()));
   base::mac::ScopedMachSendRight memory_object2(
-      ReceiveMachPort(inserted_port2));
+      ReceiveMachPort(inserted_port2.get()));
   inserted_port.reset();
   inserted_port2.reset();
 
diff --git a/ipc/attachment_broker_unprivileged_mac.cc b/ipc/attachment_broker_unprivileged_mac.cc
index 65d26f2..144028d 100644
--- a/ipc/attachment_broker_unprivileged_mac.cc
+++ b/ipc/attachment_broker_unprivileged_mac.cc
@@ -90,7 +90,8 @@
   }
 
   base::mac::ScopedMachReceiveRight message_port(wire_format.mach_port);
-  base::mac::ScopedMachSendRight memory_object(ReceiveMachPort(message_port));
+  base::mac::ScopedMachSendRight memory_object(
+      ReceiveMachPort(message_port.get()));
   IPC::internal::MachPortAttachmentMac::WireFormat translated_wire_format(
       memory_object.release(), wire_format.destination_process,
       wire_format.attachment_id);
diff --git a/sandbox/mac/bootstrap_sandbox.h b/sandbox/mac/bootstrap_sandbox.h
index fa5f859..6e823a4 100644
--- a/sandbox/mac/bootstrap_sandbox.h
+++ b/sandbox/mac/bootstrap_sandbox.h
@@ -83,7 +83,7 @@
   const BootstrapSandboxPolicy* PolicyForProcess(pid_t pid) const;
 
   std::string server_bootstrap_name() const { return server_bootstrap_name_; }
-  mach_port_t real_bootstrap_port() const { return real_bootstrap_port_; }
+  mach_port_t real_bootstrap_port() const { return real_bootstrap_port_.get(); }
 
  private:
   BootstrapSandbox();
diff --git a/sandbox/mac/launchd_interception_server.cc b/sandbox/mac/launchd_interception_server.cc
index f466e77..6ce61f6 100644
--- a/sandbox/mac/launchd_interception_server.cc
+++ b/sandbox/mac/launchd_interception_server.cc
@@ -44,12 +44,12 @@
     return false;
   }
   sandbox_port_.reset(port);
-  if ((kr = mach_port_insert_right(task, sandbox_port_, sandbox_port_,
-          MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)) {
+  if ((kr = mach_port_insert_right(task, sandbox_port_.get(),
+          sandbox_port_.get(), MACH_MSG_TYPE_MAKE_SEND) != KERN_SUCCESS)) {
     MACH_LOG(ERROR, kr) << "Failed to allocate dummy sandbox port send right.";
     return false;
   }
-  sandbox_send_port_.reset(sandbox_port_);
+  sandbox_send_port_.reset(sandbox_port_.get());
 
   if (base::mac::IsOSYosemiteOrLater()) {
     message_server_.reset(new XPCMessageServer(this, server_receive_right));
diff --git a/sandbox/mac/xpc_message_server.cc b/sandbox/mac/xpc_message_server.cc
index e161b5a..d811e5d5c 100644
--- a/sandbox/mac/xpc_message_server.cc
+++ b/sandbox/mac/xpc_message_server.cc
@@ -104,7 +104,7 @@
 
 void XPCMessageServer::ReceiveMessage() {
   IPCMessage request;
-  int rv = xpc_pipe_receive(server_port_, &request.xpc);
+  int rv = xpc_pipe_receive(server_port_.get(), &request.xpc);
   if (rv) {
     LOG(ERROR) << "Failed to xpc_pipe_receive(): " << rv;
     return;