aidl: support RPC in Java

In Java, when need to make a transaction on an RPC binder object, the
Parcel needs to be in a different format. This adds a build option to
enable RPC to work (by attaching the binder to a parcel, the correct
format can be used). In the future, we could enable this by default
on all AIDL interfaces which are built against a new enough API version
(b/175819535).

Bug: 175814583
Test: atest aidl_integration_test, manual
Change-Id: Ia5234e58d0f1731ddb7bb577c4e193a81779e6ec
diff --git a/generate_java_binder.cpp b/generate_java_binder.cpp
index 04887fc..7a738cd 100644
--- a/generate_java_binder.cpp
+++ b/generate_java_binder.cpp
@@ -615,8 +615,13 @@
   proxy->statements->Add(std::make_shared<VariableDeclaration>(
       _data, std::make_shared<MethodCall>("android.os.Parcel", "obtain")));
 
+  if (options.GenRpc()) {
+    proxy->statements->Add(
+        std::make_shared<LiteralStatement>("_data.markForBinder(asBinder());\n"));
+  }
+
   if (iface.IsSensitiveData()) {
-    proxy->statements->Add(std::make_shared<LiteralStatement>("_data.markSensitive();"));
+    proxy->statements->Add(std::make_shared<LiteralStatement>("_data.markSensitive();\n"));
   }
 
   std::shared_ptr<Variable> _reply = nullptr;
@@ -875,8 +880,11 @@
            << "android.os.RemoteException {\n"
            << "  if (mCachedVersion == -1) {\n"
            << "    android.os.Parcel data = android.os.Parcel.obtain();\n"
-           << "    android.os.Parcel reply = android.os.Parcel.obtain();\n"
-           << "    try {\n"
+           << "    android.os.Parcel reply = android.os.Parcel.obtain();\n";
+      if (options.GenRpc()) {
+        code << "    data.markForBinder(asBinder());\n";
+      }
+      code << "    try {\n"
            << "      data.writeInterfaceToken(DESCRIPTOR);\n"
            << "      boolean _status = mRemote.transact(Stub." << transactCodeName << ", "
            << "data, reply, 0);\n"
@@ -904,8 +912,11 @@
            << "android.os.RemoteException {\n"
            << "  if (\"-1\".equals(mCachedHash)) {\n"
            << "    android.os.Parcel data = android.os.Parcel.obtain();\n"
-           << "    android.os.Parcel reply = android.os.Parcel.obtain();\n"
-           << "    try {\n"
+           << "    android.os.Parcel reply = android.os.Parcel.obtain();\n";
+      if (options.GenRpc()) {
+        code << "    data.markForBinder(asBinder());\n";
+      }
+      code << "    try {\n"
            << "      data.writeInterfaceToken(DESCRIPTOR);\n"
            << "      boolean _status = mRemote.transact(Stub." << transactCodeName << ", "
            << "data, reply, 0);\n"