Auto-generate Java that provide information about modules at runtime

The first piece of information is the native libraries a module depends
on. We will use this info in following CLs to auto-load the libraries
on first module access.

The Java is generated in the chrome_feature_module template via a new
module_desc_java template. Module_desc_java lives in
//components/module_installer since this component is using the Java and
has expectations about its format.

Modules that don't use module descriptors (e.g. they are packaged into
base such as tab_management in this CL) have to manually create a
module_desc_java to be able to use Module.java's loading goodies.

APKs won't automatically have module_desc_javas. In order to make the
Module.java API work in APKs (e.g. test APKs) we use stub descriptors.
This should be fine as APKs already load all native libraries at
startup.

See go/native-dfm-load-v2 for more context and details.

Also in this CL, moving native lib loading before impl initialization
so that the module is fully set up by the time module code is handed
control.

Bug: 870055
Change-Id: I8b6112bfc57b7d49698702b4fc12d2874b55fa12
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1873911
Commit-Queue: Tibor Goldschwendt <[email protected]>
Commit-Queue: Christopher Grant <[email protected]>
Auto-Submit: Tibor Goldschwendt <[email protected]>
Reviewed-by: Christopher Grant <[email protected]>
Reviewed-by: Andrew Grieve <[email protected]>
Cr-Commit-Position: refs/heads/master@{#709165}
diff --git a/components/module_installer/android/module_desc_java.gni b/components/module_installer/android/module_desc_java.gni
new file mode 100644
index 0000000..293a032e
--- /dev/null
+++ b/components/module_installer/android/module_desc_java.gni
@@ -0,0 +1,67 @@
+# Copyright 2019 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.
+
+import("//build/config/android/rules.gni")
+import("//build/config/python.gni")
+
+# Writes an implementation of
+# |org.chromium.components.module_installer.builder.ModuleDescriptor| for a
+# particular module to Java. The module loader backend expects such an
+# implementation for each module to automate module setup on first access.
+# Instantiations of this template can be depended on like |android_library|
+# targets.
+#
+# Supports the following variables:
+#   module_name: Name of the module.
+#   shared_libraries: (Optional) List of shared_library targets the module
+#     requires at runtime. Will consider all transitively depended on
+#     shared_libraries.
+template("module_desc_java") {
+  _target_name = target_name
+
+  _libraries = "${target_gen_dir}/${_target_name}.libraries"
+  _rebased_libraries = rebase_path(_libraries, root_out_dir)
+  generated_file("${_target_name}__libraries") {
+    if (defined(invoker.shared_libraries)) {
+      deps = invoker.shared_libraries
+    }
+    outputs = [
+      _libraries,
+    ]
+    data_keys = [ "shared_libraries" ]
+    walk_keys = [ "shared_libraries_barrier" ]
+    rebase = root_build_dir
+    output_conversion = "json"
+  }
+
+  _srcjar = "$target_gen_dir/${_target_name}__srcjar.srcjar"
+  action_with_pydeps("${_target_name}__srcjar") {
+    script = "//components/module_installer/android/module_desc_java.py"
+    deps = [
+      ":${_target_name}__libraries",
+    ]
+    inputs = [
+      _libraries,
+    ]
+    outputs = [
+      _srcjar,
+    ]
+    args = [
+      "--module",
+      invoker.module_name,
+      "--libraries",
+      "@FileArg($_rebased_libraries)",
+      "--output",
+      rebase_path(_srcjar, root_out_dir),
+    ]
+  }
+
+  android_library(_target_name) {
+    deps = [
+      "//base:base_java",
+      "//components/module_installer/android:module_installer_java",
+    ]
+    srcjar_deps = [ ":${_target_name}__srcjar" ]
+  }
+}