IPC: Add MojoHandleAttachment

This CL introduces MojoHandleAttachment, a MessageAttachment subclass.
With this and ChannelMojo, we are able to send Mojo MessagePipes
over IPC.

There is also MojoMessageHelper which provides some static member functions
that reads and writes MessagePipes from/to a Message. In coming changes,
we'll add ParamTraits for MessagePipe based on this.

[email protected], [email protected]
TEST=IPCChannelMojoTest.SendMessagePipe, IPCChannelMojoTest.SendPlatformHandleAndPipe
BUG=448190

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

Cr-Commit-Position: refs/heads/master@{#314930}
diff --git a/ipc/ipc_message_attachment_set.cc b/ipc/ipc_message_attachment_set.cc
index dd881c5..cb74a5a 100644
--- a/ipc/ipc_message_attachment_set.cc
+++ b/ipc/ipc_message_attachment_set.cc
@@ -45,6 +45,13 @@
   });
 }
 
+unsigned MessageAttachmentSet::num_mojo_handles() const {
+  return std::count_if(attachments_.begin(), attachments_.end(),
+                       [](scoped_refptr<MessageAttachment> i) {
+    return i->GetType() == MessageAttachment::TYPE_MOJO_HANDLE;
+  });
+}
+
 unsigned MessageAttachmentSet::size() const {
   return static_cast<unsigned>(attachments_.size());
 }
@@ -52,7 +59,7 @@
 bool MessageAttachmentSet::AddAttachment(
     scoped_refptr<MessageAttachment> attachment) {
 #if defined(OS_POSIX)
-  if (attachment->GetType() != MessageAttachment::TYPE_PLATFORM_FILE ||
+  if (attachment->GetType() == MessageAttachment::TYPE_PLATFORM_FILE &&
       num_descriptors() == kMaxDescriptorsPerMessage) {
     DLOG(WARNING) << "Cannot add file descriptor. MessageAttachmentSet full.";
     return false;
@@ -102,6 +109,11 @@
   return attachments_[index];
 }
 
+void MessageAttachmentSet::CommitAll() {
+  attachments_.clear();
+  consumed_descriptor_highwater_ = 0;
+}
+
 #if defined(OS_POSIX)
 
 void MessageAttachmentSet::PeekDescriptors(base::PlatformFile* buffer) const {
@@ -120,11 +132,6 @@
   return false;
 }
 
-void MessageAttachmentSet::CommitAll() {
-  attachments_.clear();
-  consumed_descriptor_highwater_ = 0;
-}
-
 void MessageAttachmentSet::ReleaseFDsToClose(
     std::vector<base::PlatformFile>* fds) {
   for (size_t i = 0; i < attachments_.size(); ++i) {