Allow OatFileAssistant::Create to take std::nullopt for the context.

Partially cherry picked from
commit 12f45c41158de6f6737efb102161f86f462d9290.

Bug: 249984283
Test: m test-art-host-gtest-art_runtime_tests
Change-Id: I7ebe2aa745d0da31242034a27f92b24dbdb08740
Merged-In: I7ebe2aa745d0da31242034a27f92b24dbdb08740
diff --git a/runtime/oat_file_assistant.cc b/runtime/oat_file_assistant.cc
index 389479c..9e0c173 100644
--- a/runtime/oat_file_assistant.cc
+++ b/runtime/oat_file_assistant.cc
@@ -226,7 +226,7 @@
 std::unique_ptr<OatFileAssistant> OatFileAssistant::Create(
     const std::string& filename,
     const std::string& isa_str,
-    const std::string& context_str,
+    const std::optional<std::string>& context_str,
     bool load_executable,
     bool only_load_trusted_executable,
     OatFileAssistantContext* ofa_context,
@@ -238,20 +238,23 @@
     return nullptr;
   }
 
-  std::unique_ptr<ClassLoaderContext> tmp_context = ClassLoaderContext::Create(context_str.c_str());
-  if (tmp_context == nullptr) {
-    *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str.c_str());
-    return nullptr;
-  }
+  std::unique_ptr<ClassLoaderContext> tmp_context = nullptr;
+  if (context_str.has_value()) {
+    tmp_context = ClassLoaderContext::Create(context_str->c_str());
+    if (tmp_context == nullptr) {
+      *error_msg = StringPrintf("Class loader context '%s' is invalid", context_str->c_str());
+      return nullptr;
+    }
 
-  if (!tmp_context->OpenDexFiles(android::base::Dirname(filename.c_str()),
-                                 /*context_fds=*/{},
-                                 /*only_read_checksums=*/true)) {
-    *error_msg =
-        StringPrintf("Failed to load class loader context files for '%s' with context '%s'",
-                     filename.c_str(),
-                     context_str.c_str());
-    return nullptr;
+    if (!tmp_context->OpenDexFiles(android::base::Dirname(filename.c_str()),
+                                   /*context_fds=*/{},
+                                   /*only_read_checksums=*/true)) {
+      *error_msg =
+          StringPrintf("Failed to load class loader context files for '%s' with context '%s'",
+                       filename.c_str(),
+                       context_str->c_str());
+      return nullptr;
+    }
   }
 
   auto assistant = std::make_unique<OatFileAssistant>(filename.c_str(),
@@ -1182,6 +1185,11 @@
 }
 
 bool OatFileAssistant::ClassLoaderContextIsOkay(const OatFile& oat_file) const {
+  if (context_ == nullptr) {
+    // The caller requests to skip the check.
+    return true;
+  }
+
   if (oat_file.IsBackedByVdexOnly()) {
     // Only a vdex file, we don't depend on the class loader context.
     return true;
@@ -1193,12 +1201,6 @@
     return true;
   }
 
-  if (context_ == nullptr) {
-    // When no class loader context is provided (which happens for deprecated
-    // DexFile APIs), just assume it is OK.
-    return true;
-  }
-
   ClassLoaderContext::VerificationResult matches = context_->VerifyClassLoaderContextMatch(
       oat_file.GetClassLoaderContext(),
       /*verify_names=*/ true,