diagnostics: Add Mojo interface for passing UI messages

* Introduce a mojom file for the bidirectional Mojo API between
  diagnosticsd and the browser.
* Add methods for bidirectional relay of messages that
  originate/target from/to diagnostics_processor and the
  diagnostics UI extension.

BUG=chromium:869377
TEST=verify the diagnosticsd package builds

Change-Id: I5d6a60d9d3e674eae98781dca79115a174de4687
Reviewed-on: https://chromium-review.googlesource.com/1177714
Commit-Ready: Maksim Ivanov <emaxx@chromium.org>
Tested-by: Maksim Ivanov <emaxx@chromium.org>
Reviewed-by: Pavol Marko <pmarko@chromium.org>
diff --git a/diagnostics/README.md b/diagnostics/README.md
index 3191445..f30829f 100644
--- a/diagnostics/README.md
+++ b/diagnostics/README.md
@@ -14,3 +14,9 @@
 This daemon will process the telemetry information provided by the
 `diagnosticsd` daemon. Exposes an API that allows to obtain the output
 of the telemetry processing.
+
+## APIs between diagnosticsd and browser
+
+The bidirectional API between `diagnosticsd` and the browser is based on
+Mojo. The bootstrapping of the Mojo connection is performed via D-Bus -
+specifically, by the browser calling the BootstrapMojoConnection method.
diff --git a/diagnostics/diagnostics.gyp b/diagnostics/diagnostics.gyp
index 8bb4fa9..f2d14d9 100644
--- a/diagnostics/diagnostics.gyp
+++ b/diagnostics/diagnostics.gyp
@@ -3,6 +3,7 @@
     {
       'target_name': 'diagnosticsd',
       'type': 'executable',
+      'includes': ['mojom_generator.gypi'],
       'variables': {
         'deps': [
           'libbrillo-<(libbase_ver)',
@@ -10,6 +11,7 @@
       },
       'sources': [
         'diagnosticsd/main.cc',
+        'mojo/diagnosticsd.mojom',
       ],
     },
   ],
diff --git a/diagnostics/mojo/diagnosticsd.mojom b/diagnostics/mojo/diagnosticsd.mojom
new file mode 100644
index 0000000..9fd6e60
--- /dev/null
+++ b/diagnostics/mojo/diagnosticsd.mojom
@@ -0,0 +1,35 @@
+// Copyright 2018 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+// API exposed by the diagnosticsd daemon. Normally the consumer of this API is
+// the browser.
+
+// NOTE: This mojom should be kept in sync with the copy in Chromium's repo.
+
+module chromeos.diagnostics.mojom;
+
+// Interface exposed by the diagnosticsd daemon.
+interface DiagnosticsdService {
+  // Performs initialization, with passing a client handle that allows for
+  // bidirectional communication. This should be called before calling any other
+  // methods on the interface.
+  Init@0(DiagnosticsdServiceClient client_ptr) => ();
+
+  // Sends a message, originating from the diagnostics UI extension (hosted by
+  // the browser), to the diagnostics_processor daemon. The message contents are
+  // serialized JSON. Delivery of the message is not guaranteed (for example, if
+  // the diagnostics_processor daemon isn't running at the moment).
+  SendUiMessageToDiagnosticsProcessor@1(handle<shared_buffer> json_message)
+      => ();
+};
+
+// Interface exposed by the consumer of DiagnosticsdService.
+interface DiagnosticsdServiceClient {
+  // Sends a message, originating from the diagnostics_processor daemon, to the
+  // diagnostics UI extension (hosted by the browser). The message contents are
+  // serialized JSON. Delivery of the message is not guaranteed (for example, if
+  // the diagnostics UI extension isn't running at the moment).
+  SendDiagnosticsProcessorMessageToUi@0(handle<shared_buffer> json_message)
+      => ();
+};
diff --git a/diagnostics/mojom_generator.gypi b/diagnostics/mojom_generator.gypi
new file mode 100644
index 0000000..49a0261
--- /dev/null
+++ b/diagnostics/mojom_generator.gypi
@@ -0,0 +1,68 @@
+# Place this .gypi in the 'includes' of a library or executable target to
+# process any *.mojom sources into generated .cc and .h files.
+# TODO(emaxx): Think about moving this into a common place for reuse.
+{
+  'variables': {
+    'deps': [
+      'libchrome-<(libbase_ver)',
+      'libmojo-<(libbase_ver)',
+    ],
+    'mojo_binding_generator': '<(sysroot)/usr/src/libmojo-<(libbase_ver)/mojo/mojom_bindings_generator.py',
+    'mojo_output_dir': '<(SHARED_INTERMEDIATE_DIR)/include',
+    'mojo_template_dir': '<(SHARED_INTERMEDIATE_DIR)/templates',
+  },
+  'actions': [
+    {
+      'action_name': 'create_templates_dir',
+      'inputs': [
+      ],
+      'outputs': [
+        '<(mojo_template_dir)',
+      ],
+      'message': 'Creating mojo C++ templates dir <(mojo_template_dir)',
+      'action': [
+        'mkdir', '-p', '<(mojo_template_dir)',
+      ],
+    },
+    {
+      'action_name': 'generate_mojom_templates',
+      'inputs': [
+        '<(mojo_binding_generator)',
+        '<(mojo_template_dir)',
+      ],
+      'outputs': [
+        '<(mojo_template_dir)/cpp_templates.zip',
+      ],
+      'message': 'Generating mojo templates in <(mojo_template_dir)',
+      'action': [
+        'python', '<(mojo_binding_generator)', '--use_bundled_pylibs',
+        'precompile', '-o', '<(mojo_template_dir)',
+      ],
+    },
+  ],
+  'rules': [
+    {
+      'rule_name': 'generate_mojom_bindings',
+      'extension': 'mojom',
+      'inputs': [
+        '<(mojo_binding_generator)',
+        '<(mojo_template_dir)/cpp_templates.zip',
+      ],
+      'outputs': [
+        '<(mojo_output_dir)/<(RULE_INPUT_PATH)-internal.h',
+        '<(mojo_output_dir)/<(RULE_INPUT_PATH).cc',
+        '<(mojo_output_dir)/<(RULE_INPUT_PATH).h',
+      ],
+      'message': 'Generating mojo C++ bindings for <(RULE_INPUT_PATH)',
+      'action': [
+        'python', '<(mojo_binding_generator)', '--use_bundled_pylibs',
+        'generate', '<(RULE_INPUT_PATH)',
+        '--bytecode_path', '<(mojo_template_dir)',
+        '-I', '.',
+        '--output_dir', '<(mojo_output_dir)',
+        '--generators', 'c++',
+      ],
+      'process_outputs_as_sources': 1,
+    },
+  ],
+}