Mojo Bindings: Support handles in native structs

Changes [Native] struct serialization to use an IPC::Message instead of
a base::Pickle, allowing ParamTraits for [Native]-mapped types to be
parameterized over IPC::Message once again. In order to support this,
IPC::Message and related attachment support code has been moved into a
separate leaf target in //ipc, avoiding circular dependencies with Mojo
bindings.

Also changes the wire representation of native structs to allow for
typed Mojo handle attachments, and wires up native struct
serialization to automatically convert between IPC::MessageAttachments
and these typed mojom handles.

The net result here is that [Native] mojom structs can be mapped to
native types whose ParamTraits use message attachments.

BUG=762025

Change-Id: Ib058eff2f32e0e7abfff9619da9f142113ad28ed
Reviewed-on: https://chromium-review.googlesource.com/650219
Commit-Queue: Ken Rockot <[email protected]>
Reviewed-by: John Abd-El-Malek <[email protected]>
Reviewed-by: Daniel Cheng <[email protected]>
Reviewed-by: Yuzhu Shen <[email protected]>
Cr-Commit-Position: refs/heads/master@{#501866}
diff --git a/ipc/BUILD.gn b/ipc/BUILD.gn
index 5753bd5..a6390702 100644
--- a/ipc/BUILD.gn
+++ b/ipc/BUILD.gn
@@ -23,10 +23,6 @@
 component("ipc") {
   sources = [
     "export_template.h",
-    "handle_attachment_win.cc",
-    "handle_attachment_win.h",
-    "handle_win.cc",
-    "handle_win.h",
     "ipc_channel.cc",
     "ipc_channel.h",
     "ipc_channel_common.cc",
@@ -45,12 +41,6 @@
     "ipc_listener.h",
     "ipc_logging.cc",
     "ipc_logging.h",
-    "ipc_message.cc",
-    "ipc_message.h",
-    "ipc_message_attachment.cc",
-    "ipc_message_attachment.h",
-    "ipc_message_attachment_set.cc",
-    "ipc_message_attachment_set.h",
     "ipc_message_macros.h",
     "ipc_message_pipe_reader.cc",
     "ipc_message_pipe_reader.h",
@@ -61,27 +51,13 @@
     "ipc_message_utils.h",
     "ipc_mojo_bootstrap.cc",
     "ipc_mojo_bootstrap.h",
-    "ipc_mojo_handle_attachment.cc",
-    "ipc_mojo_handle_attachment.h",
-    "ipc_mojo_message_helper.cc",
-    "ipc_mojo_message_helper.h",
     "ipc_mojo_param_traits.cc",
     "ipc_mojo_param_traits.h",
-    "ipc_platform_file.cc",
-    "ipc_platform_file.h",
-    "ipc_platform_file_attachment_posix.cc",
-    "ipc_platform_file_attachment_posix.h",
     "ipc_sender.h",
     "ipc_sync_channel.cc",
     "ipc_sync_channel.h",
-    "ipc_sync_message.cc",
-    "ipc_sync_message.h",
     "ipc_sync_message_filter.cc",
     "ipc_sync_message_filter.h",
-    "mach_port_attachment_mac.cc",
-    "mach_port_attachment_mac.h",
-    "mach_port_mac.cc",
-    "mach_port_mac.h",
     "message_filter.cc",
     "message_filter.h",
     "message_filter_router.cc",
@@ -105,20 +81,13 @@
     ]
   }
 
-  if (is_fuchsia) {
-    sources += [
-      "handle_attachment_fuchsia.cc",
-      "handle_attachment_fuchsia.h",
-      "handle_fuchsia.cc",
-      "handle_fuchsia.h",
-    ]
-  }
-
   defines = [ "IPC_IMPLEMENTATION" ]
 
   public_deps = [
     ":ipc_features",
+    ":message_support",
     ":mojom",
+    ":native_handle_type_converters",
     ":param_traits",
     "//mojo/public/cpp/bindings",
     "//mojo/public/cpp/system",
@@ -134,17 +103,78 @@
     "//base",
   ]
 
-  if (is_win || is_mac) {
-    # On Windows HandleAttachmentWin needs to generate random IDs.
-    # On Mac MachPortAttachmentMac needs to generate random IDs.
-    deps += [ "//crypto" ]
-  }
-
   if (enable_ipc_fuzzer) {
     public_configs = [ "//tools/ipc_fuzzer:ipc_fuzzer_config" ]
   }
 }
 
+component("message_support") {
+  sources = [
+    "handle_attachment_win.cc",
+    "handle_attachment_win.h",
+    "handle_win.cc",
+    "handle_win.h",
+    "ipc_message.cc",
+    "ipc_message.h",
+    "ipc_message_attachment.cc",
+    "ipc_message_attachment.h",
+    "ipc_message_attachment_set.cc",
+    "ipc_message_attachment_set.h",
+    "ipc_message_support_export.h",
+    "ipc_mojo_handle_attachment.cc",
+    "ipc_mojo_handle_attachment.h",
+    "ipc_mojo_message_helper.cc",
+    "ipc_mojo_message_helper.h",
+    "ipc_platform_file.cc",
+    "ipc_platform_file.h",
+    "ipc_platform_file_attachment_posix.cc",
+    "ipc_platform_file_attachment_posix.h",
+    "ipc_sync_message.cc",
+    "ipc_sync_message.h",
+    "mach_port_attachment_mac.cc",
+    "mach_port_attachment_mac.h",
+    "mach_port_mac.cc",
+    "mach_port_mac.h",
+  ]
+
+  if (is_fuchsia) {
+    sources += [
+      "handle_attachment_fuchsia.cc",
+      "handle_attachment_fuchsia.h",
+      "handle_fuchsia.cc",
+      "handle_fuchsia.h",
+    ]
+  }
+
+  defines = [ "IPC_MESSAGE_SUPPORT_IMPL" ]
+
+  public_deps = [
+    ":ipc_features",
+    ":param_traits",
+    "//base",
+    "//mojo/public/cpp/system",
+  ]
+
+  if (is_win || is_mac) {
+    # On Windows HandleAttachmentWin needs to generate random IDs.
+    # On Mac MachPortAttachmentMac needs to generate random IDs.
+    deps = [
+      "//crypto",
+    ]
+  }
+}
+
+source_set("native_handle_type_converters") {
+  sources = [
+    "native_handle_type_converters.cc",
+    "native_handle_type_converters.h",
+  ]
+  public_deps = [
+    ":message_support",
+    "//mojo/public/interfaces/bindings:bindings_shared__generator",
+  ]
+}
+
 mojom_component("mojom") {
   output_prefix = "ipc_mojom"
   macro_prefix = "IPC_MOJOM"
@@ -154,6 +184,16 @@
   public_deps = [
     "//mojo/common:read_only_buffer",
   ]
+
+  # Don't generate a variant sources since we depend on generated internal
+  # bindings types and we don't generate or build variants of those.
+  disable_variants = true
+}
+
+mojom("mojom_constants") {
+  sources = [
+    "constants.mojom",
+  ]
 }
 
 mojom("test_interfaces") {