Commit 9419b1d5 authored by Alexander Timin's avatar Alexander Timin Committed by Chromium LUCI CQ

[tracing] Clean up trace name generation on Android.

Make generateTracingFilePath take a basename as a parameter, to allow
C++ to customise the file name while relying on Java to figure out the
correct directory.

If an empty string is passed, Java will generate an appropriate unique
name.

R=skyostil@chromium.org
BUG=1157954

Change-Id: Ie94448ef1db1f57d0f15deba5fa54753285ea429
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2588331
Commit-Queue: Alexander Timin <altimin@chromium.org>
Reviewed-by: default avatarYaron Friedman <yfriedman@chromium.org>
Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Cr-Commit-Position: refs/heads/master@{#839973}
parent d3648b0f
...@@ -161,12 +161,13 @@ void TracingControllerAndroid::StopTracing( ...@@ -161,12 +161,13 @@ void TracingControllerAndroid::StopTracing(
session->data->Stop(); session->data->Stop();
} }
void TracingControllerAndroid::GenerateTracingFilePath( base::FilePath TracingControllerAndroid::GenerateTracingFilePath(
base::FilePath* file_path) { const std::string& basename) {
JNIEnv* env = base::android::AttachCurrentThread(); JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> jfilename = ScopedJavaLocalRef<jstring> jfilename =
Java_TracingControllerAndroidImpl_generateTracingFilePath(env); Java_TracingControllerAndroidImpl_generateTracingFilePath(
*file_path = base::FilePath( env, base::android::ConvertUTF8ToJavaString(env, basename));
return base::FilePath(
base::android::ConvertJavaStringToUTF8(env, jfilename.obj())); base::android::ConvertJavaStringToUTF8(env, jfilename.obj()));
} }
......
...@@ -39,7 +39,11 @@ class TracingControllerAndroid { ...@@ -39,7 +39,11 @@ class TracingControllerAndroid {
JNIEnv* env, JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj, const base::android::JavaParamRef<jobject>& obj,
const base::android::JavaParamRef<jobject>& callback); const base::android::JavaParamRef<jobject>& callback);
static void GenerateTracingFilePath(base::FilePath* file_path);
// Locate the appropriate directory to write the trace to and use it to
// generate the path. |basename| might be empty, then TracingControllerAndroid
// will generate an appropriate one as well.
static base::FilePath GenerateTracingFilePath(const std::string& basename);
private: private:
~TracingControllerAndroid(); ~TracingControllerAndroid();
......
...@@ -213,7 +213,7 @@ base::FilePath GetStartupTraceFileName() { ...@@ -213,7 +213,7 @@ base::FilePath GetStartupTraceFileName() {
trace_file = tracing::TraceStartupConfig::GetInstance()->GetResultFile(); trace_file = tracing::TraceStartupConfig::GetInstance()->GetResultFile();
if (trace_file.empty()) { if (trace_file.empty()) {
#if defined(OS_ANDROID) #if defined(OS_ANDROID)
TracingControllerAndroid::GenerateTracingFilePath(&trace_file); trace_file = TracingControllerAndroid::GenerateTracingFilePath("");
#else #else
// Default to saving the startup trace into the current dir. // Default to saving the startup trace into the current dir.
trace_file = base::FilePath().AppendASCII("chrometrace.log"); trace_file = base::FilePath().AppendASCII("chrometrace.log");
......
...@@ -122,23 +122,28 @@ public class TracingControllerAndroidImpl implements TracingControllerAndroid { ...@@ -122,23 +122,28 @@ public class TracingControllerAndroidImpl implements TracingControllerAndroid {
} }
/** /**
* Generates a unique filename to be used for tracing in the Downloads directory. * Generates a filepath to be used for tracing in the Downloads directory.
* @param basename The basename to be used, if empty a unique one will be generated.
*/ */
@CalledByNative @CalledByNative
private static String generateTracingFilePath() { private static String generateTracingFilePath(String basename) {
try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) { try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
String state = Environment.getExternalStorageState(); String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) { if (!Environment.MEDIA_MOUNTED.equals(state)) {
return null; return null;
} }
// Generate a hopefully-unique filename using the UTC timestamp. if (basename.isEmpty()) {
// (Not a huge problem if it isn't unique, we'll just append more data.) // Generate a hopefully-unique filename using the UTC timestamp.
SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss", Locale.US); // (Not a huge problem if it isn't unique, we'll just append more data.)
formatter.setTimeZone(TimeZone.getTimeZone("UTC")); SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HHmmss", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
basename = "chrome-profile-results-" + formatter.format(new Date());
}
Context context = ContextUtils.getApplicationContext(); Context context = ContextUtils.getApplicationContext();
File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS); File dir = context.getExternalFilesDir(Environment.DIRECTORY_DOWNLOADS);
File file = new File(dir, "chrome-profile-results-" + formatter.format(new Date())); File file = new File(dir, basename);
return file.getPath(); return file.getPath();
} }
} }
...@@ -169,7 +174,7 @@ public class TracingControllerAndroidImpl implements TracingControllerAndroid { ...@@ -169,7 +174,7 @@ public class TracingControllerAndroidImpl implements TracingControllerAndroid {
mShowToasts = showToasts; mShowToasts = showToasts;
if (filename == null) { if (filename == null) {
filename = generateTracingFilePath(); filename = generateTracingFilePath("");
if (filename == null) { if (filename == null) {
logAndToastError(mContext.getString(R.string.profiler_no_storage_toast)); logAndToastError(mContext.getString(R.string.profiler_no_storage_toast));
return false; return false;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment