Move data class out of auto-generated code.
These don't change between auto-generated instances.
This also removes the 'associate' function in favor of the
'fromBinder' function which now always does the correct action based
on the binder in question.
Bug: 127336441
Test: atest android.binder.cts
Change-Id: I0329b5c4fa46c3ec08c5d43fbd584b3a6f7e45cc
diff --git a/generate_ndk.cpp b/generate_ndk.cpp
index 8d2927f..fa90a58 100644
--- a/generate_ndk.cpp
+++ b/generate_ndk.cpp
@@ -27,7 +27,7 @@
namespace aidl {
namespace ndk {
-static constexpr const char* kClazz = "clazz";
+static constexpr const char* kClazz = "_g_aidl_clazz";
static constexpr const char* kDescriptor = "descriptor";
static constexpr const char* kVersion = "version";
static constexpr const char* kCacheVariable = "_aidl_cached_value";
@@ -231,9 +231,6 @@
LeaveNdkNamespace(out, defined_type);
}
-static std::string DataClassFor(const AidlInterface& defined_type) {
- return "AidlClassData_" + ClassName(defined_type, ClassNames::INTERFACE);
-}
static std::string MethodId(const AidlMethod& m) {
return "(FIRST_CALL_TRANSACTION + " + std::to_string(m.GetId()) + " /*" + m.GetName() + "*/)";
}
@@ -393,34 +390,9 @@
const AidlInterface& defined_type, const Options& /*options*/) {
const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
const std::string bn_clazz = ClassName(defined_type, ClassNames::SERVER);
- const std::string data_clazz = DataClassFor(defined_type);
- const std::string on_create = data_clazz + "_onCreate";
- const std::string on_destroy = data_clazz + "_onDestory";
- const std::string on_transact = data_clazz + "_onTransact";
- const std::string on_dump = data_clazz + "_onDump";
- out << "struct " << data_clazz << " {\n";
- out.Indent();
- out << "static AIBinder_Class* " << kClazz << ";\n";
- out << "std::shared_ptr<" << bn_clazz << "> instance;\n";
- out.Dedent();
- out << "};\n\n";
-
- out << "static void* " << on_create << "(void* args) {\n";
- out.Indent();
- out << data_clazz << "* data = new " << data_clazz << "{static_cast<" << bn_clazz
- << "*>(args)->ref<" << bn_clazz << ">()};\n";
- out << "return static_cast<void*>(data);\n";
- out.Dedent();
- out << "};\n\n";
-
- out << "static void " << on_destroy << "(void* userData) {\n";
- out.Indent();
- out << "delete static_cast<" << data_clazz << "*>(userData);\n";
- out.Dedent();
- out << "};\n\n";
-
- out << "static binder_status_t " << on_transact
+ out << "static binder_status_t "
+ << "_aidl_onTransact"
<< "(AIBinder* _aidl_binder, transaction_code_t _aidl_code, const AParcel* _aidl_in, "
"AParcel* _aidl_out) {\n";
out.Indent();
@@ -428,8 +400,10 @@
out << "(void)_aidl_out;\n";
out << "binder_status_t _aidl_ret_status = STATUS_UNKNOWN_TRANSACTION;\n";
if (!defined_type.GetMethods().empty()) {
- out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = static_cast<" << data_clazz
- << "*>(AIBinder_getUserData(_aidl_binder))->instance;\n";
+ // we know this cast is valid because this method is only called by the ICInterface
+ // AIBinder_Class object which is associated with this class.
+ out << "std::shared_ptr<" << bn_clazz << "> _aidl_impl = std::static_pointer_cast<" << bn_clazz
+ << ">(::ndk::ICInterface::asInterface(_aidl_binder));\n";
out << "switch (_aidl_code) {\n";
out.Indent();
for (const auto& method : defined_type.GetMethods()) {
@@ -445,33 +419,12 @@
out.Dedent();
out << "};\n\n";
- out << "static binder_status_t " << on_dump
- << "(AIBinder* binder, int fd, const char** args, uint32_t numArgs) {\n";
- out.Indent();
- out << data_clazz << "* data = static_cast<" << data_clazz
- << "*>(AIBinder_getUserData(binder));\n";
- out << "return data->instance->dump(fd, args, numArgs);\n";
- out.Dedent();
- out << "};\n\n";
-
- out << "AIBinder_Class* " << data_clazz << ":: " << kClazz
- << " = ::ndk::ICInterface::defineClass(" << clazz << "::" << kDescriptor << ", " << on_create
- << ", " << on_destroy << ", " << on_transact << ", " << on_dump << ");\n\n";
+ out << "static AIBinder_Class* " << kClazz << " = ::ndk::ICInterface::defineClass(" << clazz
+ << "::" << kDescriptor << ", _aidl_onTransact);\n\n";
}
void GenerateClientSource(CodeWriter& out, const AidlTypenames& types,
const AidlInterface& defined_type, const Options& options) {
const std::string clazz = ClassName(defined_type, ClassNames::CLIENT);
- const std::string data_clazz = DataClassFor(defined_type);
-
- out << "// Source for " << clazz << "\n";
- out << "std::shared_ptr<" << clazz << "> " << clazz
- << "::associate(const ::ndk::SpAIBinder& binder) {\n";
- out.Indent();
- out << "if (!AIBinder_associateClass(binder.get(), " << data_clazz << "::" << kClazz
- << ")) { return nullptr; }\n";
- out << "return (new " << clazz << "(binder))->ref<" << clazz << ">();\n";
- out.Dedent();
- out << "}\n\n";
out << clazz << "::" << clazz << "(const ::ndk::SpAIBinder& binder) : BpCInterface(binder) {}\n";
out << clazz << "::~" << clazz << "() {}\n";
@@ -489,7 +442,6 @@
const AidlInterface& defined_type, const Options& options) {
const std::string clazz = ClassName(defined_type, ClassNames::SERVER);
const std::string iface = ClassName(defined_type, ClassNames::INTERFACE);
- const std::string data_clazz = DataClassFor(defined_type);
out << "// Source for " << clazz << "\n";
out << clazz << "::" << clazz << "() {}\n";
@@ -497,8 +449,7 @@
out << "::ndk::SpAIBinder " << clazz << "::createBinder() {\n";
out.Indent();
- out << "AIBinder* binder = AIBinder_new(" << data_clazz << "::" << kClazz
- << ", static_cast<void*>(this));\n";
+ out << "AIBinder* binder = AIBinder_new(" << kClazz << ", static_cast<void*>(this));\n";
out << "return ::ndk::SpAIBinder(binder);\n";
out.Dedent();
out << "}\n";
@@ -521,7 +472,7 @@
void GenerateInterfaceSource(CodeWriter& out, const AidlTypenames& types,
const AidlInterface& defined_type, const Options& options) {
const std::string clazz = ClassName(defined_type, ClassNames::INTERFACE);
- const std::string data_clazz = DataClassFor(defined_type);
+ const std::string bp_clazz = ClassName(defined_type, ClassNames::CLIENT);
out << "// Source for " << clazz << "\n";
out << "const char* " << clazz << "::" << kDescriptor << " = \""
@@ -535,16 +486,15 @@
out << "std::shared_ptr<" << clazz << "> " << clazz
<< "::fromBinder(const ::ndk::SpAIBinder& binder) {\n";
out.Indent();
- out << data_clazz << "* data = static_cast<" << data_clazz
- << "*>(AIBinder_getUserData(binder.get()));\n";
- out << "if (data) {\n";
+ out << "if (!AIBinder_associateClass(binder.get(), " << kClazz << ")) { return nullptr; }\n";
+ out << "std::shared_ptr<::ndk::ICInterface> interface = "
+ "::ndk::ICInterface::asInterface(binder.get());\n";
+ out << "if (interface) {\n";
out.Indent();
- out << "return data->instance;\n";
+ out << "return std::static_pointer_cast<" << clazz << ">(interface);\n";
out.Dedent();
out << "}\n";
- // If it is local, it is not an 'ndk' instance, and parceling will happen locally.
- out << "return " << NdkFullClassName(defined_type, ClassNames::CLIENT)
- << "::associate(binder);\n";
+ out << "return (new " << bp_clazz << "(binder))->ref<" << clazz << ">();\n";
out.Dedent();
out << "}\n\n";
@@ -643,16 +593,12 @@
<< ClassName(defined_type, ClassNames::INTERFACE) << "> {\n";
out << "public:\n";
out.Indent();
- out << "static std::shared_ptr<" << clazz << "> associate(const ::ndk::SpAIBinder& binder);\n";
+ out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
out << "virtual ~" << clazz << "();\n";
out << "\n";
for (const auto& method : defined_type.GetMethods()) {
out << NdkMethodDecl(types, *method) << " override;\n";
}
- out.Dedent();
- out << "private:\n";
- out.Indent();
- out << clazz << "(const ::ndk::SpAIBinder& binder);\n";
if (options.Version() > 0) {
out << "int32_t " << kCacheVariable << " = -1;\n";
@@ -718,7 +664,6 @@
out << "class " << clazz << " : public ::ndk::ICInterface {\n";
out << "public:\n";
out.Indent();
- out << "static AIBinder_Class* " << kClazz << ";\n";
out << "static const char* " << kDescriptor << ";\n";
out << clazz << "();\n";
out << "virtual ~" << clazz << "();\n";