Use android::base::Result in libnativeloader
Remove the out parameters for error messages using Result<T>.
Bug: 130388701
Test: libnativeloader_test
Change-Id: Idbaf391c183fb20d5e1d7c96f3a4ccbf9745b7e6
diff --git a/libnativeloader/native_loader.cpp b/libnativeloader/native_loader.cpp
index 0c29324..6d3c057 100644
--- a/libnativeloader/native_loader.cpp
+++ b/libnativeloader/native_loader.cpp
@@ -92,12 +92,10 @@
jstring permitted_path) {
#if defined(__ANDROID__)
std::lock_guard<std::mutex> guard(g_namespaces_mutex);
-
- std::string error_msg;
- bool success = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
- library_path, permitted_path, &error_msg) != nullptr;
- if (!success) {
- return env->NewStringUTF(error_msg.c_str());
+ auto ns = g_namespaces->Create(env, target_sdk_version, class_loader, is_shared, dex_path,
+ library_path, permitted_path);
+ if (!ns) {
+ return env->NewStringUTF(ns.error().message().c_str());
}
#else
UNUSED(env, target_sdk_version, class_loader, is_shared, dex_path, library_path, permitted_path);
@@ -139,11 +137,14 @@
if ((ns = g_namespaces->FindNamespaceByClassLoader(env, class_loader)) == nullptr) {
// This is the case where the classloader was not created by ApplicationLoaders
// In this case we create an isolated not-shared namespace for it.
- std::string create_error_msg;
- if ((ns = g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */,
- nullptr, library_path, nullptr, &create_error_msg)) == nullptr) {
- *error_msg = strdup(create_error_msg.c_str());
+ Result<NativeLoaderNamespace*> isolated_ns =
+ g_namespaces->Create(env, target_sdk_version, class_loader, false /* is_shared */, nullptr,
+ library_path, nullptr);
+ if (!isolated_ns) {
+ *error_msg = strdup(isolated_ns.error().message().c_str());
return nullptr;
+ } else {
+ ns = *isolated_ns;
}
}
@@ -222,12 +223,14 @@
#if defined(__ANDROID__)
void* OpenNativeLibraryInNamespace(NativeLoaderNamespace* ns, const char* path,
bool* needs_native_bridge, char** error_msg) {
- void* handle = ns->Load(path);
- if (handle == nullptr) {
- *error_msg = ns->GetError();
+ auto handle = ns->Load(path);
+ if (!handle && error_msg != nullptr) {
+ *error_msg = strdup(handle.error().message().c_str());
}
- *needs_native_bridge = ns->IsBridged();
- return handle;
+ if (needs_native_bridge != nullptr) {
+ *needs_native_bridge = ns->IsBridged();
+ }
+ return handle ? *handle : nullptr;
}
// native_bridge_namespaces are not supported for callers of this function.