Refactor public_libraries.*

- shorten the long const variables
- merge {oem|product}_public_libraries into extended_public_libraries

Bug: 130388701
Test: build & pass presubmit tests

Change-Id: I43be555041466118f2b2d188f5da3ff0d8ed7d8d
diff --git a/libnativeloader/library_namespaces.cpp b/libnativeloader/library_namespaces.cpp
index 0441bbb..3839a15 100644
--- a/libnativeloader/library_namespaces.cpp
+++ b/libnativeloader/library_namespaces.cpp
@@ -122,7 +122,7 @@
   // we might as well end up loading them from /system/lib or /product/lib
   // For now we rely on CTS test to catch things like this but
   // it should probably be addressed in the future.
-  for (const auto& soname : android::base::Split(system_public_libraries(), ":")) {
+  for (const auto& soname : android::base::Split(default_public_libraries(), ":")) {
     LOG_ALWAYS_FATAL_IF(dlopen(soname.c_str(), RTLD_NOW | RTLD_NODELETE) == nullptr,
                         "Error preloading public library %s: %s", soname.c_str(), dlerror());
   }
@@ -186,7 +186,7 @@
     is_native_bridge = NativeBridgeIsPathSupported(library_path.c_str());
   }
 
-  std::string system_exposed_libraries = system_public_libraries();
+  std::string system_exposed_libraries = default_public_libraries();
   const char* namespace_name = kClassloaderNamespaceName;
   android_namespace_t* vndk_ns = nullptr;
   if ((apk_origin == APK_ORIGIN_VENDOR ||
@@ -222,7 +222,7 @@
     permitted_path = permitted_path + ":" + origin_lib_path;
 
     // Also give access to LLNDK libraries since they are available to vendors
-    system_exposed_libraries = system_exposed_libraries + ":" + system_llndk_libraries().c_str();
+    system_exposed_libraries = system_exposed_libraries + ":" + llndk_libraries().c_str();
 
     // Give access to VNDK-SP libraries from the 'vndk' namespace.
     vndk_ns = android_get_exported_namespace(kVndkNamespaceName);
@@ -235,13 +235,10 @@
     ALOGD("classloader namespace configured for unbundled %s apk. library_path=%s",
           origin_partition, library_path.c_str());
   } else {
-    // oem and product public libraries are NOT available to vendor apks, otherwise it
+    // extended public libraries are NOT available to vendor apks, otherwise it
     // would be system->vendor violation.
-    if (!oem_public_libraries().empty()) {
-      system_exposed_libraries = system_exposed_libraries + ':' + oem_public_libraries();
-    }
-    if (!product_public_libraries().empty()) {
-      system_exposed_libraries = system_exposed_libraries + ':' + product_public_libraries();
+    if (!extended_public_libraries().empty()) {
+      system_exposed_libraries = system_exposed_libraries + ':' + extended_public_libraries();
     }
   }
   std::string runtime_exposed_libraries = runtime_public_libraries();
@@ -292,9 +289,9 @@
       }
     }
 
-    if (vndk_ns != nullptr && !system_vndksp_libraries().empty()) {
+    if (vndk_ns != nullptr && !vndksp_libraries().empty()) {
       // vendor apks are allowed to use VNDK-SP libraries.
-      if (!android_link_namespaces(ns, vndk_ns, system_vndksp_libraries().c_str())) {
+      if (!android_link_namespaces(ns, vndk_ns, vndksp_libraries().c_str())) {
         *error_msg = dlerror();
         return nullptr;
       }
@@ -383,7 +380,7 @@
   // code is one example) unknown to linker in which  case linker uses anonymous
   // namespace. The second argument specifies the search path for the anonymous
   // namespace which is the library_path of the classloader.
-  initialized_ = android_init_anonymous_namespace(system_public_libraries().c_str(),
+  initialized_ = android_init_anonymous_namespace(default_public_libraries().c_str(),
                                                   is_native_bridge ? nullptr : library_path);
   if (!initialized_) {
     *error_msg = dlerror();
@@ -392,7 +389,7 @@
 
   // and now initialize native bridge namespaces if necessary.
   if (NativeBridgeInitialized()) {
-    initialized_ = NativeBridgeInitAnonymousNamespace(system_public_libraries().c_str(),
+    initialized_ = NativeBridgeInitAnonymousNamespace(default_public_libraries().c_str(),
                                                       is_native_bridge ? library_path : nullptr);
     if (!initialized_) {
       *error_msg = NativeBridgeGetError();
diff --git a/libnativeloader/public_libraries.cpp b/libnativeloader/public_libraries.cpp
index e6f8448e..c473f2c 100644
--- a/libnativeloader/public_libraries.cpp
+++ b/libnativeloader/public_libraries.cpp
@@ -34,17 +34,13 @@
 using namespace std::string_literals;
 
 namespace {
-// TODO(b/130388701) simplify the names below
-constexpr const char kPublicNativeLibrariesSystemConfigPathFromRoot[] = "/etc/public.libraries.txt";
-constexpr const char kPublicNativeLibrariesExtensionConfigPrefix[] = "public.libraries-";
-constexpr const size_t kPublicNativeLibrariesExtensionConfigPrefixLen =
-    sizeof(kPublicNativeLibrariesExtensionConfigPrefix) - 1;
-constexpr const char kPublicNativeLibrariesExtensionConfigSuffix[] = ".txt";
-constexpr const size_t kPublicNativeLibrariesExtensionConfigSuffixLen =
-    sizeof(kPublicNativeLibrariesExtensionConfigSuffix) - 1;
-constexpr const char kPublicNativeLibrariesVendorConfig[] = "/vendor/etc/public.libraries.txt";
-constexpr const char kLlndkNativeLibrariesSystemConfigPathFromRoot[] = "/etc/llndk.libraries.txt";
-constexpr const char kVndkspNativeLibrariesSystemConfigPathFromRoot[] = "/etc/vndksp.libraries.txt";
+
+constexpr const char* kDefaultPublicLibrariesFile = "/etc/public.libraries.txt";
+constexpr const char* kExtendedPublicLibrariesFilePrefix = "public.libraries-";
+constexpr const char* kExtendedPublicLibrariesFileSuffix = ".txt";
+constexpr const char* kVendorPublicLibrariesFile = "/vendor/etc/public.libraries.txt";
+constexpr const char* kLlndkLibrariesFile = "/system/etc/llndk.libraries.txt";
+constexpr const char* kVndkLibrariesFile = "/system/etc/vndksp.libraries.txt";
 
 const std::vector<const std::string> kRuntimePublicLibraries = {
     "libicuuc.so",
@@ -53,26 +49,26 @@
 
 constexpr const char* kRuntimeApexLibPath = "/apex/com.android.runtime/" LIB;
 
+// TODO(b/130388701): do we need this?
 std::string root_dir() {
   static const char* android_root_env = getenv("ANDROID_ROOT");
   return android_root_env != nullptr ? android_root_env : "/system";
 }
 
 bool debuggable() {
-  bool debuggable = false;
-  debuggable = android::base::GetBoolProperty("ro.debuggable", false);
+  static bool debuggable = android::base::GetBoolProperty("ro.debuggable", false);
   return debuggable;
 }
 
 std::string vndk_version_str() {
-  std::string version = android::base::GetProperty("ro.vndk.version", "");
+  static std::string version = android::base::GetProperty("ro.vndk.version", "");
   if (version != "" && version != "current") {
     return "." + version;
   }
   return "";
 }
 
-void insert_vndk_version_str(std::string* file_name) {
+void InsertVndkVersionStr(std::string* file_name) {
   CHECK(file_name != nullptr);
   size_t insert_pos = file_name->find_last_of(".");
   if (insert_pos == std::string::npos) {
@@ -142,11 +138,10 @@
         continue;
       }
       const std::string filename(ent->d_name);
-      if (android::base::StartsWith(filename, kPublicNativeLibrariesExtensionConfigPrefix) &&
-          android::base::EndsWith(filename, kPublicNativeLibrariesExtensionConfigSuffix)) {
-        const size_t start = kPublicNativeLibrariesExtensionConfigPrefixLen;
-        const size_t end = filename.size() - kPublicNativeLibrariesExtensionConfigSuffixLen;
-        const std::string company_name = filename.substr(start, end - start);
+      std::string_view fn = filename;
+      if (android::base::ConsumePrefix(&fn, kExtendedPublicLibrariesFilePrefix) &&
+          android::base::ConsumeSuffix(&fn, kExtendedPublicLibrariesFileSuffix)) {
+        const std::string company_name(fn);
         const std::string config_file_path = dirname + "/"s + filename;
         LOG_ALWAYS_FATAL_IF(
             company_name.empty(),
@@ -178,11 +173,11 @@
 
 }  // namespace
 
-const std::string& system_public_libraries() {
+const std::string& default_public_libraries() {
   static bool cached = false;
   static std::string list;
   if (!cached) {
-    std::string config_file = root_dir() + kPublicNativeLibrariesSystemConfigPathFromRoot;
+    std::string config_file = root_dir() + kDefaultPublicLibrariesFile;
     std::vector<std::string> sonames;
     std::string error_msg;
     LOG_ALWAYS_FATAL_IF(!ReadConfig(config_file, &sonames, always_true, &error_msg),
@@ -251,35 +246,23 @@
   if (!cached) {
     // This file is optional, quietly ignore if the file does not exist.
     std::vector<std::string> sonames;
-    ReadConfig(kPublicNativeLibrariesVendorConfig, &sonames, always_true, nullptr);
+    ReadConfig(kVendorPublicLibrariesFile, &sonames, always_true, nullptr);
     list = android::base::Join(sonames, ':');
     cached = true;
   }
   return list;
 }
 
-// read /system/etc/public.libraries-<companyname>.txt which contain partner defined
+// read /system/etc/public.libraries-<companyname>.txt and
+// /product/etc/public.libraries-<companyname>.txt which contain partner defined
 // system libs that are exposed to apps. The libs in the txt files must be
 // named as lib<name>.<companyname>.so.
-const std::string& oem_public_libraries() {
+const std::string& extended_public_libraries() {
   static bool cached = false;
   static std::string list;
   if (!cached) {
     std::vector<std::string> sonames;
     ReadExtensionLibraries("/system/etc", &sonames);
-    list = android::base::Join(sonames, ':');
-    cached = true;
-  }
-  return list;
-}
-
-// read /product/etc/public.libraries-<companyname>.txt which contain partner defined
-// product libs that are exposed to apps.
-const std::string& product_public_libraries() {
-  static bool cached = false;
-  static std::string list;
-  if (!cached) {
-    std::vector<std::string> sonames;
     ReadExtensionLibraries("/product/etc", &sonames);
     list = android::base::Join(sonames, ':');
     cached = true;
@@ -287,12 +270,12 @@
   return list;
 }
 
-const std::string& system_llndk_libraries() {
+const std::string& llndk_libraries() {
   static bool cached = false;
   static std::string list;
   if (!cached) {
-    std::string config_file = root_dir() + kLlndkNativeLibrariesSystemConfigPathFromRoot;
-    insert_vndk_version_str(&config_file);
+    std::string config_file = kLlndkLibrariesFile;
+    InsertVndkVersionStr(&config_file);
     std::vector<std::string> sonames;
     ReadConfig(config_file, &sonames, always_true, nullptr);
     list = android::base::Join(sonames, ':');
@@ -301,12 +284,12 @@
   return list;
 }
 
-const std::string& system_vndksp_libraries() {
+const std::string& vndksp_libraries() {
   static bool cached = false;
   static std::string list;
   if (!cached) {
-    std::string config_file = root_dir() + kVndkspNativeLibrariesSystemConfigPathFromRoot;
-    insert_vndk_version_str(&config_file);
+    std::string config_file = kVndkLibrariesFile;
+    InsertVndkVersionStr(&config_file);
     std::vector<std::string> sonames;
     ReadConfig(config_file, &sonames, always_true, nullptr);
     list = android::base::Join(sonames, ':');
diff --git a/libnativeloader/public_libraries.h b/libnativeloader/public_libraries.h
index a211907..9b6dea8 100644
--- a/libnativeloader/public_libraries.h
+++ b/libnativeloader/public_libraries.h
@@ -22,13 +22,11 @@
 // These provide the list of libraries that are available to the namespace for apps.
 // Not all of the libraries are available to apps. Depending on the context,
 // e.g., if it is a vendor app or not, different set of libraries are made available.
-// TODO(b/130388701) rename these.
-const std::string& system_public_libraries();
+const std::string& default_public_libraries();
 const std::string& runtime_public_libraries();
 const std::string& vendor_public_libraries();
-const std::string& oem_public_libraries();
-const std::string& product_public_libraries();
-const std::string& system_llndk_libraries();
-const std::string& system_vndksp_libraries();
+const std::string& extended_public_libraries();
+const std::string& llndk_libraries();
+const std::string& vndksp_libraries();
 
 };  // namespace android::nativeloader