Revert "Revert "Use compiler filter to determine oat file status.""

This reverts commit 845e5064580bd37ad5014f7aa0d078be7265464d.

Add an option to change what OatFileManager considers up-to-date.
In our tests we're allowed to write to the dalvik-cache, so it
cannot be kSpeed.

Bug: 27689078
Change-Id: I0c578705a9921114ed1fb00d360cc7448addc93a
diff --git a/runtime/native/dalvik_system_DexFile.cc b/runtime/native/dalvik_system_DexFile.cc
index f1e0fa7..3397989 100644
--- a/runtime/native/dalvik_system_DexFile.cc
+++ b/runtime/native/dalvik_system_DexFile.cc
@@ -348,7 +348,8 @@
 static jint GetDexOptNeeded(JNIEnv* env,
                             const char* filename,
                             const char* instruction_set,
-                            const int target_compilation_type_mask) {
+                            const char* compiler_filter_name,
+                            bool profile_changed) {
   if ((filename == nullptr) || !OS::FileExists(filename)) {
     LOG(ERROR) << "DexFile_getDexOptNeeded file '" << filename << "' does not exist";
     ScopedLocalRef<jclass> fnfe(env, env->FindClass("java/io/FileNotFoundException"));
@@ -365,17 +366,24 @@
     return -1;
   }
 
+  CompilerFilter::Filter filter;
+  if (!CompilerFilter::ParseCompilerFilter(compiler_filter_name, &filter)) {
+    ScopedLocalRef<jclass> iae(env, env->FindClass("java/lang/IllegalArgumentException"));
+    std::string message(StringPrintf("Compiler filter %s is invalid.", compiler_filter_name));
+    env->ThrowNew(iae.get(), message.c_str());
+    return -1;
+  }
+
   // TODO: Verify the dex location is well formed, and throw an IOException if
   // not?
-  OatFileAssistant oat_file_assistant(filename, target_compilation_type_mask,
-      target_instruction_set, false);
+
+  OatFileAssistant oat_file_assistant(filename, target_instruction_set, profile_changed, false);
 
   // Always treat elements of the bootclasspath as up-to-date.
   if (oat_file_assistant.IsInBootClassPath()) {
     return OatFileAssistant::kNoDexOptNeeded;
   }
-
-  return oat_file_assistant.GetDexOptNeeded();
+  return oat_file_assistant.GetDexOptNeeded(filter);
 }
 
 static jint DexFile_getDexOptNeeded(JNIEnv* env,
@@ -393,10 +401,18 @@
     return -1;
   }
 
+  // TODO: Take profile changed and compiler filter as arguments.
+  // For now, we use "speed" by default, unless EXTRACT_ONLY = 0x4 was
+  // included in the mask.
+  const char* compiler_filter = "speed";
+  if (javaTargetCompilationTypeMask & 0x4) {
+    compiler_filter = "verify-at-runtime";
+  }
   return GetDexOptNeeded(env,
                          filename.c_str(),
                          instruction_set.c_str(),
-                         javaTargetCompilationTypeMask);
+                         compiler_filter,
+                         /*profile_changed*/false);
 }
 
 // public API
@@ -407,7 +423,8 @@
       env,
       filename.c_str(),
       instruction_set,
-      OatFileAssistant::kFullCompilation | OatFileAssistant::kProfileGuideCompilation);
+      "speed-profile",
+      /*profile_changed*/false);
   return (status != OatFileAssistant::kNoDexOptNeeded) ? JNI_TRUE : JNI_FALSE;
 }