Merge "Use expect instead of expectCallback"
diff --git a/Cronet/Android.bp b/Cronet/Android.bp
index ec7158c..e699656 100644
--- a/Cronet/Android.bp
+++ b/Cronet/Android.bp
@@ -20,26 +20,28 @@
     name: "cronet_impl_native_java",
     jars: ["prebuilt/cronet_impl_native_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 java_import {
     name: "cronet_impl_common_java",
     jars: ["prebuilt/cronet_impl_common_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 java_import {
     name: "cronet_impl_platform_java",
     jars: ["prebuilt/cronet_impl_platform_java.jar"],
     visibility: ["//visibility:private"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 cc_prebuilt_library_shared {
     name: "libcronet.80.0.3986.0",
-    // STOPSHIP: Confirm if it is safe
     shared_libs: [
         "libandroid",
         "libc",
@@ -66,7 +68,8 @@
     strip: {
         none: true,
     },
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
+    min_sdk_version: "29",
 }
 
 genrule {
@@ -79,15 +82,11 @@
 }
 
 java_sdk_library {
-    name: "org.chromium.net.cronet",
+    name: "framework-cronet",
+    defaults: ["framework-module-defaults"],
     srcs: [
         ":cronet_api-src",
     ],
-    // Only public non-hide methods for now.
-    // TODO: Confirm target APIs with Cronet team.
-    api_packages: [
-        "org.chromium.net",
-    ],
     libs: [
         "androidx.annotation_annotation",
     ],
@@ -96,18 +95,9 @@
         "cronet_impl_native_java",
         "cronet_impl_platform_java",
     ],
-    // Add dependencies on files used by droiddoc_options.
-    droiddoc_option_files: [":cronet-api-current.txt"],
-    apex_available: ["com.android.cronet"],
+    apex_available: ["com.android.tethering"],
     unsafe_ignore_missing_latest_api: true,
     dist_group: "android",
-}
-
-// Make the current.txt available for use by the cts/tests/signature tests.
-// ========================================================================
-filegroup {
-    name: "cronet-api-current.txt",
-    srcs: [
-        "api/current.txt",
-    ],
+    // cronet is used as a shared library.
+    shared_library: true,
 }
diff --git a/Cronet/apex/Android.bp b/Cronet/apex/Android.bp
index 2c5b481..64362da 100644
--- a/Cronet/apex/Android.bp
+++ b/Cronet/apex/Android.bp
@@ -31,37 +31,18 @@
 apex_defaults {
     name: "CronetApexDefaultsEnabled",
     jni_libs: ["libcronet.80.0.3986.0"],
-    java_libs: ["org.chromium.net.cronet"],
+    java_libs: ["framework-cronet"],
 }
 
 apex_defaults {
     name: "CronetApexDefaultsDisabled",
 }
 
-apex_defaults {
-    name: "com.android.cronet-defaults",
-    compile_multilib: "both",
-    jni_libs: ["libcronet.80.0.3986.0"],
-    java_libs: ["org.chromium.net.cronet"],
-    key: "com.android.cronet.key",
-    certificate: ":com.android.cronet.certificate",
-    updatable: false,
-    generate_hashtree: false,
-
-    // Use a custom AndroidManifest.xml used for API targeting.
-    androidManifest: ":cronet-manifest.xml",
-}
-
-filegroup {
-    name: "cronet-manifest.xml",
-    srcs: [
-        "AndroidManifest.xml",
-    ],
-}
-
+// TODO: Remove cronet apex after com.android.cronet is removed from PRODUCT_PACKAGES
 apex {
     name: "com.android.cronet",
-    defaults: ["com.android.cronet-defaults"],
+    key: "com.android.cronet.key",
+    updatable: false,
     manifest: "manifest.json",
 }
 
@@ -70,8 +51,3 @@
     public_key: "com.android.cronet.avbpubkey",
     private_key: "com.android.cronet.pem",
 }
-
-android_app_certificate {
-     name: "com.android.cronet.certificate",
-     certificate: "com.android.cronet",
-}
diff --git a/Cronet/apex/testing/Android.bp b/Cronet/apex/testing/Android.bp
deleted file mode 100644
index a6859ff..0000000
--- a/Cronet/apex/testing/Android.bp
+++ /dev/null
@@ -1,29 +0,0 @@
-// Copyright 2019 The Android Open Source Project
-//
-// Licensed under the Apache License, Version 2.0 (the "License");
-// you may not use this file except in compliance with the License.
-// You may obtain a copy of the License at
-//
-//      http://www.apache.org/licenses/LICENSE-2.0
-//
-// Unless required by applicable law or agreed to in writing, software
-// distributed under the License is distributed on an "AS IS" BASIS,
-// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-// See the License for the specific language governing permissions and
-// limitations under the License.
-
-package {
-    default_applicable_licenses: ["Android-Apache-2.0"],
-}
-
-apex_test {
-    name: "test_com.android.cronet",
-    visibility: [
-        "//system/apex/tests",
-    ],
-    defaults: ["com.android.cronet-defaults"],
-    manifest: "test_apex_manifest.json",
-    file_contexts: ":com.android.cronet-file_contexts",
-    // Test APEX, should never be installed
-    installable: false,
-}
diff --git a/Cronet/apex/testing/test_apex_manifest.json b/Cronet/apex/testing/test_apex_manifest.json
deleted file mode 100644
index 15aaead..0000000
--- a/Cronet/apex/testing/test_apex_manifest.json
+++ /dev/null
@@ -1,4 +0,0 @@
-{
-    "name": "com.android.cronet",
-    "version": 300000000
-}
\ No newline at end of file
diff --git a/Cronet/api/module-lib-current.txt b/Cronet/api/module-lib-current.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Cronet/api/module-lib-current.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Cronet/api/module-lib-removed.txt b/Cronet/api/module-lib-removed.txt
new file mode 100644
index 0000000..d802177
--- /dev/null
+++ b/Cronet/api/module-lib-removed.txt
@@ -0,0 +1 @@
+// Signature format: 2.0
diff --git a/Cronet/tests/apitest/Android.bp b/Cronet/tests/apitest/Android.bp
index 9f1c007..e71c707 100644
--- a/Cronet/tests/apitest/Android.bp
+++ b/Cronet/tests/apitest/Android.bp
@@ -31,13 +31,13 @@
         "android.test.base",
         "android.test.mock",
         "androidx.annotation_annotation",
-        "org.chromium.net.cronet.stubs",
+        "framework-cronet",
     ],
 }
 
 android_test {
     name: "CronetApiTest",
-    platform_apis: true,
+    sdk_version: "test_current",
     test_suites: ["device-tests"],
     certificate: "platform",
     static_libs: [
diff --git a/Cronet/tests/apitest/AndroidManifest.xml b/Cronet/tests/apitest/AndroidManifest.xml
index 052ac8f..db0f0b3 100644
--- a/Cronet/tests/apitest/AndroidManifest.xml
+++ b/Cronet/tests/apitest/AndroidManifest.xml
@@ -25,7 +25,7 @@
 
     <application>
         <uses-library android:name="android.test.runner" />
-        <uses-library android:name="org.chromium.net.cronet" />
+        <uses-library android:name="framework-cronet" />
     </application>
 
     <instrumentation
diff --git a/Tethering/apex/Android.bp b/Tethering/apex/Android.bp
index 8cf46ef..c590850 100644
--- a/Tethering/apex/Android.bp
+++ b/Tethering/apex/Android.bp
@@ -54,6 +54,7 @@
     name: "com.android.tethering",
     defaults: [
         "ConnectivityApexDefaults",
+        "CronetApexDefaults",
         "r-launched-apex-module",
     ],
     compile_multilib: "both",
diff --git a/framework/src/android/net/ConnectivityManager.java b/framework/src/android/net/ConnectivityManager.java
index 547b4ba..b5c3c64 100644
--- a/framework/src/android/net/ConnectivityManager.java
+++ b/framework/src/android/net/ConnectivityManager.java
@@ -4080,7 +4080,7 @@
         }
     }
 
-    private class CallbackHandler extends Handler {
+    private static class CallbackHandler extends Handler {
         private static final String TAG = "ConnectivityManager.CallbackHandler";
         private static final boolean DBG = false;
 
@@ -4095,7 +4095,10 @@
         @Override
         public void handleMessage(Message message) {
             if (message.what == EXPIRE_LEGACY_REQUEST) {
-                expireRequest((NetworkCapabilities) message.obj, message.arg1);
+                // the sInstance can't be null because to send this message a ConnectivityManager
+                // instance must have been created prior to creating the thread on which this
+                // Handler is running.
+                sInstance.expireRequest((NetworkCapabilities) message.obj, message.arg1);
                 return;
             }
 
diff --git a/service/src/com/android/server/connectivity/ClatCoordinator.java b/service/src/com/android/server/connectivity/ClatCoordinator.java
index 42b3827..5d04632 100644
--- a/service/src/com/android/server/connectivity/ClatCoordinator.java
+++ b/service/src/com/android/server/connectivity/ClatCoordinator.java
@@ -899,7 +899,7 @@
     }
 
     /**
-     * Dump the cordinator information.
+     * Dump the coordinator information.
      *
      * @param pw print writer.
      */
@@ -907,7 +907,7 @@
         // TODO: move map dump to a global place to avoid duplicate dump while there are two or
         // more IPv6 only networks.
         if (isStarted()) {
-            pw.println("CLAT tracker: " + mClatdTracker.toString());
+            pw.println("CLAT tracker: " + mClatdTracker);
             pw.println("Forwarding rules:");
             pw.increaseIndent();
             dumpBpfIngress(pw);
diff --git a/tools/gn2bp/Android.bp.swp b/tools/gn2bp/Android.bp.swp
index 72674bd..ad075f3 100644
--- a/tools/gn2bp/Android.bp.swp
+++ b/tools/gn2bp/Android.bp.swp
@@ -2055,20 +2055,28 @@
 // GN: //build/android:build_config_gen
 genrule {
     name: "cronet_aml_build_android_build_config_gen",
-    cmd: "$(location build/android/gyp/gcc_preprocess.py) --include-dirs " +
-         "[\"../../\", \"gen\"] " +
-         "--output " +
-         "gen/build/android/build_config_gen.srcjar " +
-         "--define " +
-         "_ENABLE_ASSERTS " +
-         "../../build/android/java/templates/BuildConfig.template",
+    tools: [
+        "soong_zip",
+    ],
+    cmd: "echo " +
+         "\"package org.chromium.build;\n " +
+         "public class BuildConfig {\n " +
+         "public static boolean IS_MULTIDEX_ENABLED ;\n " +
+         "public static boolean ENABLE_ASSERTS = true;\n " +
+         "public static boolean IS_UBSAN ;\n " +
+         "public static boolean IS_CHROME_BRANDED ;\n " +
+         "public static int R_STRING_PRODUCT_VERSION ;\n " +
+         "public static int MIN_SDK_VERSION = 1;\n " +
+         "public static boolean BUNDLES_SUPPORTED ;\n " +
+         "public static boolean IS_INCREMENTAL_INSTALL ;\n " +
+         "public static boolean ISOLATED_SPLITS_ENABLED ;\n " +
+         "public static boolean IS_FOR_TEST ;\n " +
+         "}\n\" " +
+         "> $(genDir)/BuildConfig.java && " +
+         "$(location soong_zip) -o $(out) -srcjar -f $(genDir)/BuildConfig.java",
     out: [
         "build/android/build_config_gen.srcjar",
     ],
-    tool_files: [
-        "build/android/gyp/gcc_preprocess.py",
-        "build/android/java/templates/BuildConfig.template",
-    ],
 }
 
 // GN: //build/android:build_java__build_config_crbug_908819
@@ -2858,7 +2866,7 @@
 genrule {
     name: "cronet_aml_build_android_native_libraries_gen",
     cmd: "$(location build/android/gyp/write_native_libraries_java.py) --output " +
-         "gen/build/android/native_libraries_gen.srcjar " +
+         "$(out) " +
          "--cpu-family " +
          "CPU_FAMILY_X86",
     out: [
@@ -10084,11 +10092,11 @@
 // GN: //tools/grit:grit_sources
 genrule {
     name: "cronet_aml_tools_grit_grit_sources",
-    cmd: "$(location tools/grit/stamp_grit_sources.py) ../../tools/grit " +
-         "obj/tools/grit/grit_sources.script.stamp " +
-         "obj/tools/grit/grit_sources.d",
+    cmd: "python $(location tools/grit/stamp_grit_sources.py) `dirname $(location tools/grit/grit.py)` " +
+         "$(out) " +
+         "$(genDir)/grit_sources.d",
     out: [
-        "//out/test/obj/tools/grit/grit_sources.script.stamp",
+        "out/test/obj/tools/grit/grit_sources.script.stamp",
     ],
     tool_files: [
         "tools/grit/grit.py",
diff --git a/tools/gn2bp/gen_android_bp b/tools/gn2bp/gen_android_bp
index 97eeb4d..f0eb1d8 100755
--- a/tools/gn2bp/gen_android_bp
+++ b/tools/gn2bp/gen_android_bp
@@ -667,6 +667,37 @@
 
   blueprint.add_module(module)
 
+# HACK: Need to support build_cofig_gen flexibly instead of hardcoding
+# build_config_gen generates srcjar by executing gcc via gcc_preprocess.py but gcc is not
+# available in genrule sandbox. Also gcc path is not configurable.
+# Under the //net:net, gcc_preprocess.py is only used for build_config_gen.
+# So, for now, hardcoding BuildConfig.java and generates srcjar by soong_zip.
+def override_build_config_gen(module):
+  module.tool_files.clear()
+  module.tools.add("soong_zip")
+  cmd = [
+    "echo",
+    "\\\"package org.chromium.build;\\n",
+    "public class BuildConfig {\\n",
+    "public static boolean IS_MULTIDEX_ENABLED ;\\n",
+    "public static boolean ENABLE_ASSERTS = true;\\n",
+    "public static boolean IS_UBSAN ;\\n",
+    "public static boolean IS_CHROME_BRANDED ;\\n",
+    "public static int R_STRING_PRODUCT_VERSION ;\\n",
+    "public static int MIN_SDK_VERSION = 1;\\n",
+    "public static boolean BUNDLES_SUPPORTED ;\\n",
+    "public static boolean IS_INCREMENTAL_INSTALL ;\\n",
+    "public static boolean ISOLATED_SPLITS_ENABLED ;\\n",
+    "public static boolean IS_FOR_TEST ;\\n",
+    "}\\n\\\"",
+    "> $(genDir)/BuildConfig.java &&",
+    "$(location soong_zip) -o $(out) -srcjar -f $(genDir)/BuildConfig.java"
+  ]
+  NEWLINE = ' " +\n         "'
+  module.cmd = NEWLINE.join(cmd)
+  return module
+
+
 def create_action_module(blueprint, target):
   bp_module_name = label_to_module_name(target.name)
   module = Module('genrule', bp_module_name, target.name)
@@ -777,6 +808,20 @@
         # wrap filename in \"$(location filename)\"
         args = ['$(location %s)' % arg for arg in args]
         target.args[i + 1] = '[%s]' % ', '.join(args)
+  elif target.script == "//build/android/gyp/write_native_libraries_java.py":
+    for i, val in enumerate(target.args):
+      if val == '--output':
+        target.args[i + 1] = '$(out)'
+  elif target.script == "//tools/grit/stamp_grit_sources.py":
+    target.outputs = [re.sub('^\/\/', '', out) for out in target.outputs]
+    # Directory that contains grit scripts
+    target.args[0] = '`dirname $(location tools/grit/grit.py)`'
+    # Path to the stamp file
+    target.args[1] = '$(out)'
+    # Script tries to create args[2] file but this is not in the output.
+    # Specifying file under $(genDir) so that parent directory exists.
+    # If this file is used by other module, we may need to add this file to the outputs.
+    target.args[2] = '$(genDir)/' + target.args[2].split('/')[-1]
 
   script = gn_utils.label_to_path(target.script)
   module.tool_files.add(script)
@@ -822,6 +867,13 @@
     module.srcs.remove(script)
 
   module.out.update(target.outputs)
+
+  if target.name == "//build/android:build_config_gen":
+    module = override_build_config_gen(module)
+  elif target.script == "//tools/grit/stamp_grit_sources.py":
+    # stamp_grit_sources.py is not executable
+    module.cmd = "python " + module.cmd
+
   blueprint.add_module(module)
   return module