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(
session->data->Stop();
}
void TracingControllerAndroid::GenerateTracingFilePath(
base::FilePath* file_path) {
base::FilePath TracingControllerAndroid::GenerateTracingFilePath(
const std::string& basename) {
JNIEnv* env = base::android::AttachCurrentThread();
ScopedJavaLocalRef<jstring> jfilename =
Java_TracingControllerAndroidImpl_generateTracingFilePath(env);
*file_path = base::FilePath(
Java_TracingControllerAndroidImpl_generateTracingFilePath(
env, base::android::ConvertUTF8ToJavaString(env, basename));
return base::FilePath(
base::android::ConvertJavaStringToUTF8(env, jfilename.obj()));
}
......
......@@ -39,7 +39,11 @@ class TracingControllerAndroid {
JNIEnv* env,
const base::android::JavaParamRef<jobject>& obj,
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:
~TracingControllerAndroid();
......
......@@ -213,7 +213,7 @@ base::FilePath GetStartupTraceFileName() {
trace_file = tracing::TraceStartupConfig::GetInstance()->GetResultFile();
if (trace_file.empty()) {
#if defined(OS_ANDROID)
TracingControllerAndroid::GenerateTracingFilePath(&trace_file);
trace_file = TracingControllerAndroid::GenerateTracingFilePath("");
#else
// Default to saving the startup trace into the current dir.
trace_file = base::FilePath().AppendASCII("chrometrace.log");
......
......@@ -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
private static String generateTracingFilePath() {
private static String generateTracingFilePath(String basename) {
try (StrictModeContext ignored = StrictModeContext.allowDiskWrites()) {
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
return null;
}
if (basename.isEmpty()) {
// Generate a hopefully-unique filename using the UTC timestamp.
// (Not a huge problem if it isn't unique, we'll just append more data.)
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();
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();
}
}
......@@ -169,7 +174,7 @@ public class TracingControllerAndroidImpl implements TracingControllerAndroid {
mShowToasts = showToasts;
if (filename == null) {
filename = generateTracingFilePath();
filename = generateTracingFilePath("");
if (filename == null) {
logAndToastError(mContext.getString(R.string.profiler_no_storage_toast));
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