EDK: Convert most uses of PlatformHandleVector to std::vector<ScopedPlatformHandle>.

(And ScopedPlatformHandleVectorPtr to
std::unique_ptr<std::vector<ScopedPlatformHandle>>.)

There are a few more uses, but this change is already too big.

R=vardhan@google.com

Review URL: https://codereview.chromium.org/1478503003 .
diff --git a/mojo/edk/embedder/platform_channel_pair_unittest.cc b/mojo/edk/embedder/platform_channel_pair_unittest.cc
index 20b9a7a..a84072a 100644
--- a/mojo/edk/embedder/platform_channel_pair_unittest.cc
+++ b/mojo/edk/embedder/platform_channel_pair_unittest.cc
@@ -118,7 +118,7 @@
     WaitReadable(client_handle.get());
 
     char buf[10000] = {};
-    std::deque<PlatformHandle> received_handles;
+    std::deque<ScopedPlatformHandle> received_handles;
     ssize_t result = PlatformChannelRecvmsg(client_handle.get(), buf,
                                             sizeof(buf), &received_handles);
     EXPECT_EQ(static_cast<ssize_t>(send_string.size()), result);
@@ -168,7 +168,7 @@
     WaitReadable(client_handle.get());
 
     char buf[10000] = {};
-    std::deque<PlatformHandle> received_handles;
+    std::deque<ScopedPlatformHandle> received_handles;
     // We assume that the |recvmsg()| actually reads all the data.
     EXPECT_EQ(static_cast<ssize_t>(sizeof(kHello)),
               PlatformChannelRecvmsg(client_handle.get(), buf, sizeof(buf),
@@ -178,7 +178,7 @@
 
     for (size_t j = 0; !received_handles.empty(); j++) {
       util::ScopedFILE fp(test::FILEFromPlatformHandle(
-          ScopedPlatformHandle(received_handles.front()), "rb"));
+          std::move(received_handles.front()), "rb"));
       received_handles.pop_front();
       ASSERT_TRUE(fp);
       rewind(fp.get());
@@ -223,8 +223,8 @@
   WaitReadable(client_handle.get());
 
   // Start with an invalid handle in the deque.
-  std::deque<PlatformHandle> received_handles;
-  received_handles.push_back(PlatformHandle());
+  std::deque<ScopedPlatformHandle> received_handles;
+  received_handles.push_back(ScopedPlatformHandle());
 
   char buf[100] = {};
   // We assume that the |recvmsg()| actually reads all the data.
@@ -237,9 +237,8 @@
   EXPECT_TRUE(received_handles[1].is_valid());
 
   {
-    util::ScopedFILE fp(test::FILEFromPlatformHandle(
-        ScopedPlatformHandle(received_handles[1]), "rb"));
-    received_handles[1] = PlatformHandle();
+    util::ScopedFILE fp(
+        test::FILEFromPlatformHandle(std::move(received_handles[1]), "rb"));
     ASSERT_TRUE(fp);
     rewind(fp.get());
     char read_buf[100];
diff --git a/mojo/edk/embedder/platform_channel_utils.cc b/mojo/edk/embedder/platform_channel_utils.cc
index 71c1159..85af2e4 100644
--- a/mojo/edk/embedder/platform_channel_utils.cc
+++ b/mojo/edk/embedder/platform_channel_utils.cc
@@ -139,10 +139,11 @@
   return true;
 }
 
-ssize_t PlatformChannelRecvmsg(PlatformHandle h,
-                               void* buf,
-                               size_t num_bytes,
-                               std::deque<PlatformHandle>* platform_handles) {
+ssize_t PlatformChannelRecvmsg(
+    PlatformHandle h,
+    void* buf,
+    size_t num_bytes,
+    std::deque<ScopedPlatformHandle>* platform_handles) {
   DCHECK(buf);
   DCHECK_GT(num_bytes, 0u);
   DCHECK(platform_handles);
@@ -173,7 +174,8 @@
       size_t num_fds = payload_length / sizeof(int);
       const int* fds = reinterpret_cast<int*>(CMSG_DATA(cmsg));
       for (size_t i = 0; i < num_fds; i++) {
-        platform_handles->push_back(PlatformHandle(fds[i]));
+        platform_handles->push_back(
+            ScopedPlatformHandle(PlatformHandle(fds[i])));
         DCHECK(platform_handles->back().is_valid());
       }
     }
diff --git a/mojo/edk/embedder/platform_channel_utils.h b/mojo/edk/embedder/platform_channel_utils.h
index 8458b06..94e2f5c 100644
--- a/mojo/edk/embedder/platform_channel_utils.h
+++ b/mojo/edk/embedder/platform_channel_utils.h
@@ -11,6 +11,7 @@
 #include <deque>
 
 #include "mojo/edk/embedder/platform_handle.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 
 struct iovec;  // Declared in <sys/uio.h>.
 
@@ -61,12 +62,13 @@
                                 size_t num_handles);
 
 // Wrapper around |recvmsg()|, which will extract any attached file descriptors
-// (in the control message) to |PlatformHandle|s (and append them to
+// (in the control message) to |ScopedPlatformHandle|s (and append them to
 // |platform_handles|). (This also handles |EINTR|.)
-ssize_t PlatformChannelRecvmsg(PlatformHandle h,
-                               void* buf,
-                               size_t num_bytes,
-                               std::deque<PlatformHandle>* platform_handles);
+ssize_t PlatformChannelRecvmsg(
+    PlatformHandle h,
+    void* buf,
+    size_t num_bytes,
+    std::deque<ScopedPlatformHandle>* platform_handles);
 
 }  // namespace embedder
 }  // namespace mojo
diff --git a/mojo/edk/system/channel.cc b/mojo/edk/system/channel.cc
index b13d08c..2e1b96c 100644
--- a/mojo/edk/system/channel.cc
+++ b/mojo/edk/system/channel.cc
@@ -14,6 +14,7 @@
 #include "mojo/edk/system/endpoint_relayer.h"
 #include "mojo/edk/system/transport_data.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MakeRefCounted;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
@@ -295,7 +296,7 @@
 
 void Channel::OnReadMessage(
     const MessageInTransit::View& message_view,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles) {
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) {
 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
   DCHECK(thread_checker_.IsCreationThreadCurrent());
 #endif  // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
@@ -352,7 +353,7 @@
 
 void Channel::OnReadMessageForEndpoint(
     const MessageInTransit::View& message_view,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles) {
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) {
 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
   DCHECK(thread_checker_.IsCreationThreadCurrent());
 #endif  // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
@@ -415,7 +416,7 @@
 
 void Channel::OnReadMessageForChannel(
     const MessageInTransit::View& message_view,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles) {
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) {
 #if !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
   DCHECK(thread_checker_.IsCreationThreadCurrent());
 #endif  // !defined(NDEBUG) || defined(DCHECK_ALWAYS_ON)
diff --git a/mojo/edk/system/channel.h b/mojo/edk/system/channel.h
index 1780205..166e385 100644
--- a/mojo/edk/system/channel.h
+++ b/mojo/edk/system/channel.h
@@ -191,16 +191,19 @@
   // |RawChannel::Delegate| implementation (only called on the creation thread):
   void OnReadMessage(
       const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override;
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles) override;
   void OnError(Error error) override;
 
   // Helpers for |OnReadMessage| (only called on the creation thread):
   void OnReadMessageForEndpoint(
       const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles);
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles);
   void OnReadMessageForChannel(
       const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles);
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles);
 
   // Handles "attach and run endpoint" messages.
   bool OnAttachAndRunEndpoint(ChannelEndpointId local_id,
diff --git a/mojo/edk/system/data_pipe.cc b/mojo/edk/system/data_pipe.cc
index 109f1ac..3efd6db 100644
--- a/mojo/edk/system/data_pipe.cc
+++ b/mojo/edk/system/data_pipe.cc
@@ -25,6 +25,7 @@
 #include "mojo/edk/system/remote_producer_data_pipe_impl.h"
 #include "mojo/edk/util/make_unique.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -416,7 +417,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   MutexLocker locker(&mutex_);
   DCHECK(has_local_producer_no_lock());
   // Warning: After |ProducerEndSerialize()|, quite probably |impl_| has
@@ -625,7 +626,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   MutexLocker locker(&mutex_);
   DCHECK(has_local_consumer_no_lock());
   // Warning: After |ConsumerEndSerialize()|, quite probably |impl_| has
diff --git a/mojo/edk/system/data_pipe.h b/mojo/edk/system/data_pipe.h
index 0d77668..866ad9d 100644
--- a/mojo/edk/system/data_pipe.h
+++ b/mojo/edk/system/data_pipe.h
@@ -8,8 +8,9 @@
 #include <stdint.h>
 
 #include <memory>
+#include <vector>
 
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/channel_endpoint_client.h"
 #include "mojo/edk/system/handle_signals_state.h"
 #include "mojo/edk/system/memory.h"
@@ -119,10 +120,11 @@
   void ProducerStartSerialize(Channel* channel,
                               size_t* max_size,
                               size_t* max_platform_handles);
-  bool ProducerEndSerialize(Channel* channel,
-                            void* destination,
-                            size_t* actual_size,
-                            embedder::PlatformHandleVector* platform_handles);
+  bool ProducerEndSerialize(
+      Channel* channel,
+      void* destination,
+      size_t* actual_size,
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles);
   bool ProducerIsBusy() const;
 
   // These are called by the consumer dispatcher to implement its methods of
@@ -151,10 +153,11 @@
   void ConsumerStartSerialize(Channel* channel,
                               size_t* max_size,
                               size_t* max_platform_handles);
-  bool ConsumerEndSerialize(Channel* channel,
-                            void* destination,
-                            size_t* actual_size,
-                            embedder::PlatformHandleVector* platform_handles);
+  bool ConsumerEndSerialize(
+      Channel* channel,
+      void* destination,
+      size_t* actual_size,
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles);
   bool ConsumerIsBusy() const;
 
   // The following are only to be used by |DataPipeImpl| (and its subclasses):
diff --git a/mojo/edk/system/data_pipe_consumer_dispatcher.cc b/mojo/edk/system/data_pipe_consumer_dispatcher.cc
index 490b917..b0101e3 100644
--- a/mojo/edk/system/data_pipe_consumer_dispatcher.cc
+++ b/mojo/edk/system/data_pipe_consumer_dispatcher.cc
@@ -10,6 +10,7 @@
 #include "mojo/edk/system/data_pipe.h"
 #include "mojo/edk/system/memory.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -161,7 +162,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
 
   bool rv = data_pipe_->ConsumerEndSerialize(channel, destination, actual_size,
diff --git a/mojo/edk/system/data_pipe_consumer_dispatcher.h b/mojo/edk/system/data_pipe_consumer_dispatcher.h
index 9f2389d..f650c7b 100644
--- a/mojo/edk/system/data_pipe_consumer_dispatcher.h
+++ b/mojo/edk/system/data_pipe_consumer_dispatcher.h
@@ -69,7 +69,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override
       MOJO_NOT_THREAD_SAFE;
   bool IsBusyNoLock() const override;
 
diff --git a/mojo/edk/system/data_pipe_impl.h b/mojo/edk/system/data_pipe_impl.h
index e25b697..4596a62 100644
--- a/mojo/edk/system/data_pipe_impl.h
+++ b/mojo/edk/system/data_pipe_impl.h
@@ -8,8 +8,9 @@
 #include <stdint.h>
 
 #include <utility>
+#include <vector>
 
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/data_pipe.h"
 #include "mojo/edk/system/handle_signals_state.h"
 #include "mojo/edk/system/memory.h"
@@ -75,7 +76,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) = 0;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) = 0;
 
   virtual void ConsumerClose() = 0;
   // |num_bytes.Get()| will be a nonzero multiple of |element_num_bytes()|.
@@ -102,7 +103,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) = 0;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) = 0;
 
   virtual bool OnReadMessage(unsigned port, MessageInTransit* message) = 0;
   virtual void OnDetachFromChannel(unsigned port) = 0;
diff --git a/mojo/edk/system/data_pipe_producer_dispatcher.cc b/mojo/edk/system/data_pipe_producer_dispatcher.cc
index 814fa59..c08bd88 100644
--- a/mojo/edk/system/data_pipe_producer_dispatcher.cc
+++ b/mojo/edk/system/data_pipe_producer_dispatcher.cc
@@ -10,6 +10,7 @@
 #include "mojo/edk/system/data_pipe.h"
 #include "mojo/edk/system/memory.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -137,7 +138,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
 
   bool rv = data_pipe_->ProducerEndSerialize(channel, destination, actual_size,
diff --git a/mojo/edk/system/data_pipe_producer_dispatcher.h b/mojo/edk/system/data_pipe_producer_dispatcher.h
index 9251ce1..6118449 100644
--- a/mojo/edk/system/data_pipe_producer_dispatcher.h
+++ b/mojo/edk/system/data_pipe_producer_dispatcher.h
@@ -69,7 +69,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override
       MOJO_NOT_THREAD_SAFE;
   bool IsBusyNoLock() const override;
 
diff --git a/mojo/edk/system/dispatcher.cc b/mojo/edk/system/dispatcher.cc
index ea88870..9433ccc 100644
--- a/mojo/edk/system/dispatcher.cc
+++ b/mojo/edk/system/dispatcher.cc
@@ -12,6 +12,7 @@
 #include "mojo/edk/system/platform_handle_dispatcher.h"
 #include "mojo/edk/system/shared_buffer_dispatcher.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -63,7 +64,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   DCHECK(dispatcher);
   return dispatcher->EndSerializeAndClose(channel, destination, actual_size,
                                           platform_handles);
@@ -75,7 +76,7 @@
     int32_t type,
     const void* source,
     size_t size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   switch (static_cast<Dispatcher::Type>(type)) {
     case Type::UNKNOWN:
       DVLOG(2) << "Deserializing invalid handle";
@@ -412,7 +413,7 @@
     Channel* /*channel*/,
     void* /*destination*/,
     size_t* /*actual_size*/,
-    embedder::PlatformHandleVector* /*platform_handles*/) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
   DCHECK(is_closed_);
   // By default, serializing isn't supported, so just close.
@@ -461,7 +462,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   DCHECK(channel);
   DCHECK(actual_size);
   AssertHasOneRef();  // Only one ref => no need to take the lock.
diff --git a/mojo/edk/system/dispatcher.h b/mojo/edk/system/dispatcher.h
index e425bb3..b9bb895 100644
--- a/mojo/edk/system/dispatcher.h
+++ b/mojo/edk/system/dispatcher.h
@@ -12,7 +12,7 @@
 #include <ostream>
 #include <vector>
 
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/handle_signals_state.h"
 #include "mojo/edk/system/memory.h"
 #include "mojo/edk/util/mutex.h"
@@ -200,7 +200,7 @@
         Channel* channel,
         void* destination,
         size_t* actual_size,
-        embedder::PlatformHandleVector* platform_handles);
+        std::vector<embedder::ScopedPlatformHandle>* platform_handles);
 
     // Deserialization API.
     // Note: This "clears" (i.e., reset to the invalid handle) any platform
@@ -210,7 +210,7 @@
         int32_t type,
         const void* source,
         size_t size,
-        embedder::PlatformHandleVector* platform_handles);
+        std::vector<embedder::ScopedPlatformHandle>* platform_handles);
   };
 
  protected:
@@ -304,7 +304,8 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) MOJO_NOT_THREAD_SAFE;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles)
+      MOJO_NOT_THREAD_SAFE;
 
   // This should be overridden to return true if/when there's an ongoing
   // operation (e.g., two-phase read/writes on data pipes) that should prevent a
@@ -371,8 +372,8 @@
   bool EndSerializeAndClose(Channel* channel,
                             void* destination,
                             size_t* actual_size,
-                            embedder::PlatformHandleVector* platform_handles)
-      MOJO_NOT_THREAD_SAFE;
+                            std::vector<embedder::ScopedPlatformHandle>*
+                                platform_handles) MOJO_NOT_THREAD_SAFE;
 
   // This protects the following members as well as any state added by
   // subclasses.
diff --git a/mojo/edk/system/local_data_pipe_impl.cc b/mojo/edk/system/local_data_pipe_impl.cc
index d4330ae..565d4af 100644
--- a/mojo/edk/system/local_data_pipe_impl.cc
+++ b/mojo/edk/system/local_data_pipe_impl.cc
@@ -25,6 +25,7 @@
 #include "mojo/edk/system/remote_producer_data_pipe_impl.h"
 #include "mojo/edk/util/make_unique.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::RefPtr;
 
 namespace mojo {
@@ -164,7 +165,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   SerializedDataPipeProducerDispatcher* s =
       static_cast<SerializedDataPipeProducerDispatcher*>(destination);
   s->validated_options = validated_options();
@@ -336,7 +337,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   SerializedDataPipeConsumerDispatcher* s =
       static_cast<SerializedDataPipeConsumerDispatcher*>(destination);
   s->validated_options = validated_options();
diff --git a/mojo/edk/system/local_data_pipe_impl.h b/mojo/edk/system/local_data_pipe_impl.h
index aa14590..b4f513e 100644
--- a/mojo/edk/system/local_data_pipe_impl.h
+++ b/mojo/edk/system/local_data_pipe_impl.h
@@ -43,7 +43,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   void ConsumerClose() override;
   MojoResult ConsumerReadData(UserPointer<void> elements,
                               UserPointer<uint32_t> num_bytes,
@@ -66,7 +66,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   bool OnReadMessage(unsigned port, MessageInTransit* message) override;
   void OnDetachFromChannel(unsigned port) override;
 
diff --git a/mojo/edk/system/master_connection_manager.cc b/mojo/edk/system/master_connection_manager.cc
index 4e78897..19f2d5d 100644
--- a/mojo/edk/system/master_connection_manager.cc
+++ b/mojo/edk/system/master_connection_manager.cc
@@ -7,6 +7,7 @@
 #include <memory>
 #include <unordered_map>
 #include <utility>
+#include <vector>
 
 #include "base/bind.h"
 #include "base/bind_helpers.h"
@@ -17,6 +18,7 @@
 #include "mojo/edk/embedder/platform_channel_pair.h"
 #include "mojo/edk/embedder/platform_handle.h"
 #include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/connection_manager_messages.h"
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/system/raw_channel.h"
@@ -25,8 +27,10 @@
 #include "mojo/edk/util/waitable_event.h"
 #include "mojo/public/cpp/system/macros.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::platform::TaskRunner;
 using mojo::util::AutoResetWaitableEvent;
+using mojo::util::MakeUnique;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -84,9 +88,9 @@
 
  private:
   // |RawChannel::Delegate| methods:
-  void OnReadMessage(
-      const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override;
+  void OnReadMessage(const MessageInTransit::View& message_view,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                         platform_handles) override;
   void OnError(Error error) override;
 
   // Handles an error that's fatal to this object. Note that this probably
@@ -129,7 +133,7 @@
 
 void MasterConnectionManager::Helper::OnReadMessage(
     const MessageInTransit::View& message_view,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles) {
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) {
   if (message_view.type() != MessageInTransit::Type::CONNECTION_MANAGER) {
     LOG(ERROR) << "Invalid message type " << message_view.type();
     FatalError();  // WARNING: This destroys us.
@@ -199,9 +203,8 @@
     DCHECK_EQ(message_view.subtype(),
               MessageInTransit::Subtype::CONNECTION_MANAGER_CONNECT);
     DCHECK(platform_handle.is_valid());
-    embedder::ScopedPlatformHandleVectorPtr platform_handles(
-        new embedder::PlatformHandleVector());
-    platform_handles->push_back(platform_handle.release());
+    auto platform_handles = MakeUnique<std::vector<ScopedPlatformHandle>>();
+    platform_handles->push_back(std::move(platform_handle));
     response->SetTransportData(util::MakeUnique<TransportData>(
         std::move(platform_handles),
         raw_channel_->GetSerializedPlatformHandleSize()));
@@ -317,6 +320,7 @@
   }
 
  private:
+  // TODO(vtl): Make |second| |ScopedPlatformHandle|s.
   std::unordered_map<ProcessIdentifier, embedder::PlatformHandle>
       process_connections_;  // "Owns" any valid platform handles.
 
diff --git a/mojo/edk/system/message_pipe.cc b/mojo/edk/system/message_pipe.cc
index 5256bd3..6287426 100644
--- a/mojo/edk/system/message_pipe.cc
+++ b/mojo/edk/system/message_pipe.cc
@@ -19,6 +19,7 @@
 #include "mojo/edk/system/proxy_message_pipe_endpoint.h"
 #include "mojo/edk/util/make_unique.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MakeRefCounted;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
@@ -228,7 +229,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* /*platform_handles*/) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   DCHECK(port == 0 || port == 1);
 
   MutexLocker locker(&mutex_);
diff --git a/mojo/edk/system/message_pipe.h b/mojo/edk/system/message_pipe.h
index 99b1200..4e82e03 100644
--- a/mojo/edk/system/message_pipe.h
+++ b/mojo/edk/system/message_pipe.h
@@ -11,7 +11,7 @@
 #include <memory>
 #include <vector>
 
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/channel_endpoint_client.h"
 #include "mojo/edk/system/dispatcher.h"
 #include "mojo/edk/system/handle_signals_state.h"
@@ -109,11 +109,12 @@
                       Channel* channel,
                       size_t* max_size,
                       size_t* max_platform_handles);
-  bool EndSerialize(unsigned port,
-                    Channel* channel,
-                    void* destination,
-                    size_t* actual_size,
-                    embedder::PlatformHandleVector* platform_handles);
+  bool EndSerialize(
+      unsigned port,
+      Channel* channel,
+      void* destination,
+      size_t* actual_size,
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles);
 
   // |ChannelEndpointClient| methods:
   bool OnReadMessage(unsigned port, MessageInTransit* message) override;
diff --git a/mojo/edk/system/message_pipe_dispatcher.cc b/mojo/edk/system/message_pipe_dispatcher.cc
index 704fd13..cc400d5 100644
--- a/mojo/edk/system/message_pipe_dispatcher.cc
+++ b/mojo/edk/system/message_pipe_dispatcher.cc
@@ -14,6 +14,7 @@
 #include "mojo/edk/system/options_validation.h"
 #include "mojo/edk/system/proxy_message_pipe_endpoint.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::RefPtr;
 
 namespace mojo {
@@ -204,7 +205,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
 
   bool rv = message_pipe_->EndSerialize(port_, channel, destination,
diff --git a/mojo/edk/system/message_pipe_dispatcher.h b/mojo/edk/system/message_pipe_dispatcher.h
index da64649..a26fdf0 100644
--- a/mojo/edk/system/message_pipe_dispatcher.h
+++ b/mojo/edk/system/message_pipe_dispatcher.h
@@ -106,7 +106,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override
       MOJO_NOT_THREAD_SAFE;
 
   // This will be null if closed.
diff --git a/mojo/edk/system/platform_handle_dispatcher.cc b/mojo/edk/system/platform_handle_dispatcher.cc
index 91c64fd..7cb7110 100644
--- a/mojo/edk/system/platform_handle_dispatcher.cc
+++ b/mojo/edk/system/platform_handle_dispatcher.cc
@@ -5,9 +5,11 @@
 #include "mojo/edk/system/platform_handle_dispatcher.h"
 
 #include <algorithm>
+#include <utility>
 
 #include "base/logging.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
 
@@ -24,7 +26,7 @@
 
 }  // namespace
 
-embedder::ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() {
+ScopedPlatformHandle PlatformHandleDispatcher::PassPlatformHandle() {
   MutexLocker locker(&mutex());
   return platform_handle_.Pass();
 }
@@ -38,7 +40,7 @@
     Channel* channel,
     const void* source,
     size_t size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   if (size != sizeof(SerializedPlatformHandleDispatcher)) {
     LOG(ERROR) << "Invalid serialized platform handle dispatcher (bad size)";
     return nullptr;
@@ -49,7 +51,7 @@
   size_t platform_handle_index = serialization->platform_handle_index;
 
   // Starts off invalid, which is what we want.
-  embedder::PlatformHandle platform_handle;
+  ScopedPlatformHandle platform_handle;
 
   if (platform_handle_index != kInvalidPlatformHandleIndex) {
     if (!platform_handles ||
@@ -64,13 +66,12 @@
     std::swap(platform_handle, (*platform_handles)[platform_handle_index]);
   }
 
-  return Create(embedder::ScopedPlatformHandle(platform_handle));
+  return Create(std::move(platform_handle));
 }
 
 PlatformHandleDispatcher::PlatformHandleDispatcher(
-    embedder::ScopedPlatformHandle platform_handle)
-    : platform_handle_(platform_handle.Pass()) {
-}
+    ScopedPlatformHandle platform_handle)
+    : platform_handle_(platform_handle.Pass()) {}
 
 PlatformHandleDispatcher::~PlatformHandleDispatcher() {
 }
@@ -99,14 +100,14 @@
     Channel* /*channel*/,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
 
   SerializedPlatformHandleDispatcher* serialization =
       static_cast<SerializedPlatformHandleDispatcher*>(destination);
   if (platform_handle_.is_valid()) {
     serialization->platform_handle_index = platform_handles->size();
-    platform_handles->push_back(platform_handle_.release());
+    platform_handles->push_back(std::move(platform_handle_));
   } else {
     serialization->platform_handle_index = kInvalidPlatformHandleIndex;
   }
diff --git a/mojo/edk/system/platform_handle_dispatcher.h b/mojo/edk/system/platform_handle_dispatcher.h
index d9b809f..16ce340 100644
--- a/mojo/edk/system/platform_handle_dispatcher.h
+++ b/mojo/edk/system/platform_handle_dispatcher.h
@@ -34,7 +34,7 @@
       Channel* channel,
       const void* source,
       size_t size,
-      embedder::PlatformHandleVector* platform_handles);
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles);
 
  private:
   explicit PlatformHandleDispatcher(
@@ -53,7 +53,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override
       MOJO_NOT_THREAD_SAFE;
 
   embedder::ScopedPlatformHandle platform_handle_ MOJO_GUARDED_BY(mutex());
diff --git a/mojo/edk/system/raw_channel.cc b/mojo/edk/system/raw_channel.cc
index 16f924d..3b83a67 100644
--- a/mojo/edk/system/raw_channel.cc
+++ b/mojo/edk/system/raw_channel.cc
@@ -16,6 +16,7 @@
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/system/transport_data.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::MutexLocker;
 
 namespace mojo {
@@ -58,7 +59,7 @@
   if (!transport_data)
     return false;
 
-  const embedder::PlatformHandleVector* all_platform_handles =
+  const std::vector<ScopedPlatformHandle>* all_platform_handles =
       transport_data->platform_handles();
   if (!all_platform_handles) {
     DCHECK_EQ(platform_handles_offset_, 0u);
@@ -80,11 +81,12 @@
 
   MessageInTransit* message = message_queue_.PeekMessage();
   TransportData* transport_data = message->transport_data();
-  embedder::PlatformHandleVector* all_platform_handles =
+  std::vector<ScopedPlatformHandle>* all_platform_handles =
       transport_data->platform_handles();
   *num_platform_handles =
       all_platform_handles->size() - platform_handles_offset_;
-  *platform_handles = &(*all_platform_handles)[platform_handles_offset_];
+  *platform_handles = reinterpret_cast<embedder::PlatformHandle*>(
+      &(*all_platform_handles)[platform_handles_offset_]);
 
   if (serialized_platform_handle_size_ > 0) {
     size_t serialization_data_offset =
@@ -326,7 +328,7 @@
           return;  // |this| may have been destroyed in |CallOnError()|.
         }
       } else {
-        embedder::ScopedPlatformHandleVectorPtr platform_handles;
+        std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles;
         if (message_view.transport_data_buffer()) {
           size_t num_platform_handles;
           const void* platform_handle_table;
diff --git a/mojo/edk/system/raw_channel.h b/mojo/edk/system/raw_channel.h
index beb0194..f085120 100644
--- a/mojo/edk/system/raw_channel.h
+++ b/mojo/edk/system/raw_channel.h
@@ -9,7 +9,6 @@
 #include <vector>
 
 #include "base/memory/weak_ptr.h"
-#include "mojo/edk/embedder/platform_handle_vector.h"
 #include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/system/message_in_transit_queue.h"
@@ -67,7 +66,8 @@
     // |Shutdown()| and then (if desired) destroy it.
     virtual void OnReadMessage(
         const MessageInTransit::View& message_view,
-        embedder::ScopedPlatformHandleVectorPtr platform_handles) = 0;
+        std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+            platform_handles) = 0;
 
     // Called when there's a (fatal) error. This may call the |RawChannel|'s
     // |Shutdown()| and then (if desired) destroy it.
@@ -261,9 +261,10 @@
   // called when |num_platform_handles| is nonzero. Returns null if the
   // |num_platform_handles| handles are not available. Only called on the I/O
   // thread.
-  virtual embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
-      size_t num_platform_handles,
-      const void* platform_handle_table) MOJO_LOCKS_EXCLUDED(write_mutex_) = 0;
+  virtual std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+  GetReadPlatformHandles(size_t num_platform_handles,
+                         const void* platform_handle_table)
+      MOJO_LOCKS_EXCLUDED(write_mutex_) = 0;
 
   // Writes contents in |write_buffer_no_lock()|.
   // This class guarantees that:
diff --git a/mojo/edk/system/raw_channel_posix.cc b/mojo/edk/system/raw_channel_posix.cc
index 096d0d7..cec566b 100644
--- a/mojo/edk/system/raw_channel_posix.cc
+++ b/mojo/edk/system/raw_channel_posix.cc
@@ -10,8 +10,10 @@
 
 #include <algorithm>
 #include <deque>
+#include <iterator>
 #include <memory>
 #include <utility>
+#include <vector>
 
 #include "base/bind.h"
 #include "base/location.h"
@@ -20,11 +22,13 @@
 #include "base/message_loop/message_loop.h"
 #include "mojo/edk/embedder/platform_channel_utils.h"
 #include "mojo/edk/embedder/platform_handle.h"
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/transport_data.h"
 #include "mojo/edk/util/make_unique.h"
 #include "mojo/public/cpp/system/macros.h"
 
+using mojo::embedder::ScopedPlatformHandle;
+using mojo::util::MakeUnique;
 using mojo::util::MutexLocker;
 
 namespace mojo {
@@ -52,7 +56,7 @@
       const MessageInTransit::View& message_view) override;
   IOResult Read(size_t* bytes_read) override;
   IOResult ScheduleRead() override;
-  embedder::ScopedPlatformHandleVectorPtr GetReadPlatformHandles(
+  std::unique_ptr<std::vector<ScopedPlatformHandle>> GetReadPlatformHandles(
       size_t num_platform_handles,
       const void* platform_handle_table) override;
   IOResult WriteNoLock(size_t* platform_handles_written,
@@ -80,7 +84,7 @@
 
   bool pending_read_;
 
-  std::deque<embedder::PlatformHandle> read_platform_handles_;
+  std::deque<embedder::ScopedPlatformHandle> read_platform_handles_;
 
   bool pending_write_ MOJO_GUARDED_BY(write_mutex());
 
@@ -112,8 +116,6 @@
   // These must have been shut down/destroyed on the I/O thread.
   DCHECK(!read_watcher_);
   DCHECK(!write_watcher_);
-
-  embedder::CloseAllPlatformHandles(&read_platform_handles_);
 }
 
 size_t RawChannelPosix::GetSerializedPlatformHandleSize() const {
@@ -124,7 +126,7 @@
 void RawChannelPosix::EnqueueMessageNoLock(
     std::unique_ptr<MessageInTransit> message) {
   if (message->transport_data()) {
-    embedder::PlatformHandleVector* const platform_handles =
+    std::vector<ScopedPlatformHandle>* const platform_handles =
         message->transport_data()->platform_handles();
     if (platform_handles &&
         platform_handles->size() > embedder::kPlatformChannelMaxNumHandles) {
@@ -139,11 +141,13 @@
             MessageInTransit::Type::RAW_CHANNEL,
             MessageInTransit::Subtype::RAW_CHANNEL_POSIX_EXTRA_PLATFORM_HANDLES,
             0, nullptr));
-        embedder::ScopedPlatformHandleVectorPtr fds(
-            new embedder::PlatformHandleVector(
-                platform_handles->begin() + i,
-                platform_handles->begin() + i +
-                    embedder::kPlatformChannelMaxNumHandles));
+        using IteratorType = std::vector<ScopedPlatformHandle>::iterator;
+        std::unique_ptr<std::vector<ScopedPlatformHandle>> fds(
+            MakeUnique<std::vector<ScopedPlatformHandle>>(
+                std::move_iterator<IteratorType>(platform_handles->begin() + i),
+                std::move_iterator<IteratorType>(
+                    platform_handles->begin() + i +
+                    embedder::kPlatformChannelMaxNumHandles)));
         fd_message->SetTransportData(util::MakeUnique<TransportData>(
             std::move(fds), GetSerializedPlatformHandleSize()));
         RawChannel::EnqueueMessageNoLock(std::move(fd_message));
@@ -193,21 +197,21 @@
   return IO_PENDING;
 }
 
-embedder::ScopedPlatformHandleVectorPtr RawChannelPosix::GetReadPlatformHandles(
-    size_t num_platform_handles,
-    const void* /*platform_handle_table*/) {
+std::unique_ptr<std::vector<ScopedPlatformHandle>>
+RawChannelPosix::GetReadPlatformHandles(size_t num_platform_handles,
+                                        const void* /*platform_handle_table*/) {
   DCHECK_GT(num_platform_handles, 0u);
 
   if (read_platform_handles_.size() < num_platform_handles) {
-    embedder::CloseAllPlatformHandles(&read_platform_handles_);
     read_platform_handles_.clear();
-    return embedder::ScopedPlatformHandleVectorPtr();
+    return nullptr;
   }
 
-  embedder::ScopedPlatformHandleVectorPtr rv(
-      new embedder::PlatformHandleVector(num_platform_handles));
-  rv->assign(read_platform_handles_.begin(),
-             read_platform_handles_.begin() + num_platform_handles);
+  auto rv = MakeUnique<std::vector<ScopedPlatformHandle>>(num_platform_handles);
+  using IteratorType = std::deque<ScopedPlatformHandle>::iterator;
+  rv->assign(std::move_iterator<IteratorType>(read_platform_handles_.begin()),
+             std::move_iterator<IteratorType>(read_platform_handles_.begin() +
+                                              num_platform_handles));
   read_platform_handles_.erase(
       read_platform_handles_.begin(),
       read_platform_handles_.begin() + num_platform_handles);
@@ -422,7 +426,6 @@
         (TransportData::GetMaxPlatformHandles() +
          embedder::kPlatformChannelMaxNumHandles)) {
       LOG(ERROR) << "Received too many platform handles";
-      embedder::CloseAllPlatformHandles(&read_platform_handles_);
       read_platform_handles_.clear();
       return IO_FAILED_UNKNOWN;
     }
diff --git a/mojo/edk/system/raw_channel_unittest.cc b/mojo/edk/system/raw_channel_unittest.cc
index 202c152..a5278cb 100644
--- a/mojo/edk/system/raw_channel_unittest.cc
+++ b/mojo/edk/system/raw_channel_unittest.cc
@@ -31,7 +31,9 @@
 #include "mojo/public/cpp/system/macros.h"
 #include "testing/gtest/include/gtest/gtest.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::AutoResetWaitableEvent;
+using mojo::util::MakeUnique;
 using mojo::util::Mutex;
 using mojo::util::MutexLocker;
 
@@ -95,7 +97,7 @@
  protected:
   test::TestIOThread* io_thread() { return &io_thread_; }
 
-  embedder::ScopedPlatformHandle handles[2];
+  ScopedPlatformHandle handles[2];
 
  private:
   test::TestIOThread io_thread_;
@@ -111,9 +113,9 @@
   ~WriteOnlyRawChannelDelegate() override {}
 
   // |RawChannel::Delegate| implementation:
-  void OnReadMessage(
-      const MessageInTransit::View& /*message_view*/,
-      embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override {
+  void OnReadMessage(const MessageInTransit::View& /*message_view*/,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                     /*platform_handles*/) override {
     CHECK(false);  // Should not get called.
   }
   void OnError(Error error) override {
@@ -224,9 +226,9 @@
   ~ReadCheckerRawChannelDelegate() override {}
 
   // |RawChannel::Delegate| implementation (called on the I/O thread):
-  void OnReadMessage(
-      const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override {
+  void OnReadMessage(const MessageInTransit::View& message_view,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                         platform_handles) override {
     EXPECT_FALSE(platform_handles);
 
     size_t position;
@@ -339,9 +341,9 @@
   ~ReadCountdownRawChannelDelegate() override {}
 
   // |RawChannel::Delegate| implementation (called on the I/O thread):
-  void OnReadMessage(
-      const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override {
+  void OnReadMessage(const MessageInTransit::View& message_view,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                         platform_handles) override {
     EXPECT_FALSE(platform_handles);
 
     EXPECT_LT(count_, expected_count_);
@@ -553,9 +555,9 @@
   ~ShutdownOnReadMessageRawChannelDelegate() override {}
 
   // |RawChannel::Delegate| implementation (called on the I/O thread):
-  void OnReadMessage(
-      const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override {
+  void OnReadMessage(const MessageInTransit::View& message_view,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                         platform_handles) override {
     EXPECT_FALSE(platform_handles);
     EXPECT_FALSE(did_shutdown_);
     EXPECT_TRUE(
@@ -629,7 +631,8 @@
   // |RawChannel::Delegate| implementation (called on the I/O thread):
   void OnReadMessage(
       const MessageInTransit::View& /*message_view*/,
-      embedder::ScopedPlatformHandleVectorPtr /*platform_handles*/) override {
+      std::unique_ptr<std::vector<ScopedPlatformHandle>> /*platform_handles*/)
+      override {
     CHECK(false);  // Should not get called.
   }
   void OnError(Error error) override {
@@ -728,9 +731,9 @@
   ~ReadPlatformHandlesCheckerRawChannelDelegate() override {}
 
   // |RawChannel::Delegate| implementation (called on the I/O thread):
-  void OnReadMessage(
-      const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override {
+  void OnReadMessage(const MessageInTransit::View& message_view,
+                     std::unique_ptr<std::vector<ScopedPlatformHandle>>
+                         platform_handles) override {
     const char kHello[] = "hello";
 
     EXPECT_EQ(sizeof(kHello), message_view.num_bytes());
@@ -738,9 +741,9 @@
 
     ASSERT_TRUE(platform_handles);
     ASSERT_EQ(2u, platform_handles->size());
-    embedder::ScopedPlatformHandle h1(platform_handles->at(0));
+    ScopedPlatformHandle h1(std::move(platform_handles->at(0)));
     EXPECT_TRUE(h1.is_valid());
-    embedder::ScopedPlatformHandle h2(platform_handles->at(1));
+    ScopedPlatformHandle h2(std::move(platform_handles->at(1)));
     EXPECT_TRUE(h2.is_valid());
     platform_handles->clear();
 
@@ -798,12 +801,11 @@
 
   {
     const char kHello[] = "hello";
-    embedder::ScopedPlatformHandleVectorPtr platform_handles(
-        new embedder::PlatformHandleVector());
+    auto platform_handles = MakeUnique<std::vector<ScopedPlatformHandle>>();
     platform_handles->push_back(
-        mojo::test::PlatformHandleFromFILE(std::move(fp1)).release());
+        mojo::test::PlatformHandleFromFILE(std::move(fp1)));
     platform_handles->push_back(
-        mojo::test::PlatformHandleFromFILE(std::move(fp2)).release());
+        mojo::test::PlatformHandleFromFILE(std::move(fp2)));
 
     std::unique_ptr<MessageInTransit> message(
         new MessageInTransit(MessageInTransit::Type::ENDPOINT_CLIENT,
diff --git a/mojo/edk/system/remote_consumer_data_pipe_impl.cc b/mojo/edk/system/remote_consumer_data_pipe_impl.cc
index b54b0fa..a3bd83d 100644
--- a/mojo/edk/system/remote_consumer_data_pipe_impl.cc
+++ b/mojo/edk/system/remote_consumer_data_pipe_impl.cc
@@ -18,6 +18,7 @@
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/system/remote_data_pipe_ack.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::RefPtr;
 
 namespace mojo {
@@ -278,7 +279,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   SerializedDataPipeProducerDispatcher* s =
       static_cast<SerializedDataPipeProducerDispatcher*>(destination);
   s->validated_options = validated_options();
@@ -365,7 +366,7 @@
     Channel* /*channel*/,
     void* /*destination*/,
     size_t* /*actual_size*/,
-    embedder::PlatformHandleVector* /*platform_handles*/) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   NOTREACHED();
   return false;
 }
diff --git a/mojo/edk/system/remote_consumer_data_pipe_impl.h b/mojo/edk/system/remote_consumer_data_pipe_impl.h
index 20a3f04..4c8e8e8 100644
--- a/mojo/edk/system/remote_consumer_data_pipe_impl.h
+++ b/mojo/edk/system/remote_consumer_data_pipe_impl.h
@@ -62,7 +62,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   void ConsumerClose() override;
   MojoResult ConsumerReadData(UserPointer<void> elements,
                               UserPointer<uint32_t> num_bytes,
@@ -85,7 +85,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   bool OnReadMessage(unsigned port, MessageInTransit* message) override;
   void OnDetachFromChannel(unsigned port) override;
 
diff --git a/mojo/edk/system/remote_producer_data_pipe_impl.cc b/mojo/edk/system/remote_producer_data_pipe_impl.cc
index 772277b..c473499 100644
--- a/mojo/edk/system/remote_producer_data_pipe_impl.cc
+++ b/mojo/edk/system/remote_producer_data_pipe_impl.cc
@@ -20,6 +20,7 @@
 #include "mojo/edk/system/remote_consumer_data_pipe_impl.h"
 #include "mojo/edk/system/remote_data_pipe_ack.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::RefPtr;
 
 namespace mojo {
@@ -162,7 +163,7 @@
     Channel* /*channel*/,
     void* /*destination*/,
     size_t* /*actual_size*/,
-    embedder::PlatformHandleVector* /*platform_handles*/) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   NOTREACHED();
   return false;
 }
@@ -305,7 +306,7 @@
     Channel* channel,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* /*platform_handles*/) {
   SerializedDataPipeConsumerDispatcher* s =
       static_cast<SerializedDataPipeConsumerDispatcher*>(destination);
   s->validated_options = validated_options();
diff --git a/mojo/edk/system/remote_producer_data_pipe_impl.h b/mojo/edk/system/remote_producer_data_pipe_impl.h
index 1210b73..0073814 100644
--- a/mojo/edk/system/remote_producer_data_pipe_impl.h
+++ b/mojo/edk/system/remote_producer_data_pipe_impl.h
@@ -63,7 +63,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   void ConsumerClose() override;
   MojoResult ConsumerReadData(UserPointer<void> elements,
                               UserPointer<uint32_t> num_bytes,
@@ -86,7 +86,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override;
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override;
   bool OnReadMessage(unsigned port, MessageInTransit* message) override;
   void OnDetachFromChannel(unsigned port) override;
 
diff --git a/mojo/edk/system/shared_buffer_dispatcher.cc b/mojo/edk/system/shared_buffer_dispatcher.cc
index 8b86d0a..128708e 100644
--- a/mojo/edk/system/shared_buffer_dispatcher.cc
+++ b/mojo/edk/system/shared_buffer_dispatcher.cc
@@ -15,6 +15,7 @@
 #include "mojo/edk/system/options_validation.h"
 #include "mojo/public/c/system/macros.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::util::RefPtr;
 
 namespace mojo {
@@ -98,7 +99,7 @@
     Channel* channel,
     const void* source,
     size_t size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   DCHECK(channel);
 
   if (size != sizeof(SerializedSharedBufferDispatcher)) {
@@ -140,7 +141,7 @@
   }
 
   // Starts off invalid, which is what we want.
-  embedder::PlatformHandle platform_handle;
+  ScopedPlatformHandle platform_handle;
   // We take ownership of the handle, so we have to invalidate the one in
   // |platform_handles|.
   std::swap(platform_handle, (*platform_handles)[platform_handle_index]);
@@ -149,7 +150,7 @@
   // closed even if creation fails.
   auto shared_buffer =
       channel->platform_support()->CreateSharedBufferFromHandle(
-          num_bytes, embedder::ScopedPlatformHandle(platform_handle));
+          num_bytes, std::move(platform_handle));
   if (!shared_buffer) {
     LOG(ERROR)
         << "Invalid serialized shared buffer dispatcher (invalid num_bytes?)";
@@ -267,7 +268,7 @@
     Channel* /*channel*/,
     void* destination,
     size_t* actual_size,
-    embedder::PlatformHandleVector* platform_handles) {
+    std::vector<ScopedPlatformHandle>* platform_handles) {
   AssertHasOneRef();  // Only one ref => no need to take the lock.
   DCHECK(shared_buffer_);
 
@@ -276,7 +277,7 @@
   // If there's only one reference to |shared_buffer_|, then it's ours (and no
   // one else can make any more references to it), so we can just take its
   // handle.
-  embedder::ScopedPlatformHandle platform_handle(
+  ScopedPlatformHandle platform_handle(
       shared_buffer_->HasOneRef() ? shared_buffer_->PassPlatformHandle()
                                   : shared_buffer_->DuplicatePlatformHandle());
   if (!platform_handle.is_valid()) {
@@ -286,7 +287,7 @@
 
   serialization->num_bytes = shared_buffer_->GetNumBytes();
   serialization->platform_handle_index = platform_handles->size();
-  platform_handles->push_back(platform_handle.release());
+  platform_handles->push_back(std::move(platform_handle));
   *actual_size = sizeof(SerializedSharedBufferDispatcher);
 
   shared_buffer_ = nullptr;
diff --git a/mojo/edk/system/shared_buffer_dispatcher.h b/mojo/edk/system/shared_buffer_dispatcher.h
index 9f153cf..bcb8279 100644
--- a/mojo/edk/system/shared_buffer_dispatcher.h
+++ b/mojo/edk/system/shared_buffer_dispatcher.h
@@ -59,7 +59,7 @@
       Channel* channel,
       const void* source,
       size_t size,
-      embedder::PlatformHandleVector* platform_handles);
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles);
 
  private:
   static util::RefPtr<SharedBufferDispatcher> CreateInternal(
@@ -100,7 +100,7 @@
       Channel* channel,
       void* destination,
       size_t* actual_size,
-      embedder::PlatformHandleVector* platform_handles) override
+      std::vector<embedder::ScopedPlatformHandle>* platform_handles) override
       MOJO_NOT_THREAD_SAFE;
 
   util::RefPtr<embedder::PlatformSharedBuffer> shared_buffer_
diff --git a/mojo/edk/system/slave_connection_manager.cc b/mojo/edk/system/slave_connection_manager.cc
index 0018a60..c0504e7 100644
--- a/mojo/edk/system/slave_connection_manager.cc
+++ b/mojo/edk/system/slave_connection_manager.cc
@@ -15,6 +15,7 @@
 #include "mojo/edk/system/message_in_transit.h"
 #include "mojo/edk/util/make_unique.h"
 
+using mojo::embedder::ScopedPlatformHandle;
 using mojo::platform::TaskRunner;
 using mojo::util::MutexLocker;
 using mojo::util::RefPtr;
@@ -49,7 +50,7 @@
 void SlaveConnectionManager::Init(
     RefPtr<TaskRunner>&& delegate_thread_task_runner,
     embedder::SlaveProcessDelegate* slave_process_delegate,
-    embedder::ScopedPlatformHandle platform_handle) {
+    ScopedPlatformHandle platform_handle) {
   DCHECK(delegate_thread_task_runner);
   DCHECK(slave_process_delegate);
   DCHECK(platform_handle.is_valid());
@@ -116,7 +117,7 @@
     const ConnectionIdentifier& connection_id,
     ProcessIdentifier* peer_process_identifier,
     bool* is_first,
-    embedder::ScopedPlatformHandle* platform_handle) {
+    ScopedPlatformHandle* platform_handle) {
   AssertNotOnPrivateThread();
   DCHECK(peer_process_identifier);
   DCHECK(is_first);
@@ -135,7 +136,7 @@
 }
 
 void SlaveConnectionManager::InitOnPrivateThread(
-    embedder::ScopedPlatformHandle platform_handle) {
+    ScopedPlatformHandle platform_handle) {
   AssertOnPrivateThread();
 
   raw_channel_ = RawChannel::Create(platform_handle.Pass());
@@ -208,7 +209,7 @@
     Result* result,
     ProcessIdentifier* peer_process_identifier,
     bool* is_first,
-    embedder::ScopedPlatformHandle* platform_handle) {
+    ScopedPlatformHandle* platform_handle) {
   DCHECK(result);
   AssertOnPrivateThread();
   // This should only posted (from another thread, to |private_thread_|) with
@@ -236,7 +237,7 @@
 
 void SlaveConnectionManager::OnReadMessage(
     const MessageInTransit::View& message_view,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles) {
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles) {
   AssertOnPrivateThread();
 
   // Set |*ack_result_| to failure by default.
@@ -292,8 +293,7 @@
             CONNECTION_MANAGER_ACK_SUCCESS_CONNECT_NEW_CONNECTION:
           CHECK_EQ(num_platform_handles, 1u);
           *ack_result_ = Result::SUCCESS_CONNECT_NEW_CONNECTION;
-          ack_platform_handle_->reset(platform_handles->at(0));
-          platform_handles->at(0) = embedder::PlatformHandle();
+          *ack_platform_handle_ = std::move(platform_handles->at(0));
           break;
         case MessageInTransit::Subtype::
             CONNECTION_MANAGER_ACK_SUCCESS_CONNECT_REUSE_CONNECTION:
diff --git a/mojo/edk/system/slave_connection_manager.h b/mojo/edk/system/slave_connection_manager.h
index 2453f56..951f1e1 100644
--- a/mojo/edk/system/slave_connection_manager.h
+++ b/mojo/edk/system/slave_connection_manager.h
@@ -80,7 +80,8 @@
   // |RawChannel::Delegate| methods (only called on |private_thread_|):
   void OnReadMessage(
       const MessageInTransit::View& message_view,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles) override;
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles) override;
   void OnError(Error error) override;
 
   // Asserts that the current thread is *not* |private_thread_| (no-op if
diff --git a/mojo/edk/system/transport_data.cc b/mojo/edk/system/transport_data.cc
index 805d24c..1f7d8ea 100644
--- a/mojo/edk/system/transport_data.cc
+++ b/mojo/edk/system/transport_data.cc
@@ -13,6 +13,8 @@
 #include "mojo/edk/system/configuration.h"
 #include "mojo/edk/system/message_in_transit.h"
 
+using mojo::embedder::ScopedPlatformHandle;
+
 namespace mojo {
 namespace system {
 
@@ -124,7 +126,7 @@
 
   if (estimated_num_platform_handles > 0) {
     DCHECK(!platform_handles_);
-    platform_handles_.reset(new embedder::PlatformHandleVector());
+    platform_handles_.reset(new std::vector<ScopedPlatformHandle>());
   }
 
   Header* header = reinterpret_cast<Header*>(buffer_.get());
@@ -194,7 +196,7 @@
 }
 
 TransportData::TransportData(
-    embedder::ScopedPlatformHandleVectorPtr platform_handles,
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles,
     size_t serialized_platform_handle_size)
     : buffer_size_(), platform_handles_(std::move(platform_handles)) {
   buffer_size_ = MessageInTransit::RoundUpMessageAlignment(
@@ -311,7 +313,7 @@
 std::unique_ptr<DispatcherVector> TransportData::DeserializeDispatchers(
     const void* buffer,
     size_t buffer_size,
-    embedder::ScopedPlatformHandleVectorPtr platform_handles,
+    std::unique_ptr<std::vector<ScopedPlatformHandle>> platform_handles,
     Channel* channel) {
   DCHECK(buffer);
   DCHECK_GT(buffer_size, 0u);
diff --git a/mojo/edk/system/transport_data.h b/mojo/edk/system/transport_data.h
index fb4ba6b..60a0821 100644
--- a/mojo/edk/system/transport_data.h
+++ b/mojo/edk/system/transport_data.h
@@ -11,8 +11,7 @@
 #include <vector>
 
 #include "base/memory/aligned_memory.h"
-#include "mojo/edk/embedder/platform_handle.h"
-#include "mojo/edk/embedder/platform_handle_vector.h"
+#include "mojo/edk/embedder/scoped_platform_handle.h"
 #include "mojo/edk/system/dispatcher.h"
 #include "mojo/public/cpp/system/macros.h"
 
@@ -95,7 +94,8 @@
   // |Dispatcher|s. (|Header| will be present, and zero except for
   // |num_platform_handles|, and |platform_handle_table_offset| if necessary.)
   explicit TransportData(
-      embedder::ScopedPlatformHandleVectorPtr platform_handles,
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles,
       size_t serialized_platform_handle_size);
 
   ~TransportData();
@@ -110,10 +110,10 @@
 
   // Gets attached platform-specific handles; this may return null if there are
   // none. Note that the caller may mutate the set of platform-specific handles.
-  const embedder::PlatformHandleVector* platform_handles() const {
+  const std::vector<embedder::ScopedPlatformHandle>* platform_handles() const {
     return platform_handles_.get();
   }
-  embedder::PlatformHandleVector* platform_handles() {
+  std::vector<embedder::ScopedPlatformHandle>* platform_handles() {
     return platform_handles_.get();
   }
 
@@ -142,7 +142,8 @@
   static std::unique_ptr<DispatcherVector> DeserializeDispatchers(
       const void* buffer,
       size_t buffer_size,
-      embedder::ScopedPlatformHandleVectorPtr platform_handles,
+      std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+          platform_handles,
       Channel* channel);
 
  private:
@@ -179,7 +180,8 @@
   // transport). The vector (if any) owns the handles that it contains (and is
   // responsible for closing them).
   // TODO(vtl): With C++11, change it to a vector of |ScopedPlatformHandle|s.
-  embedder::ScopedPlatformHandleVectorPtr platform_handles_;
+  std::unique_ptr<std::vector<embedder::ScopedPlatformHandle>>
+      platform_handles_;
 
   MOJO_DISALLOW_COPY_AND_ASSIGN(TransportData);
 };