Commit 0cffda31 authored by Eric Seckler's avatar Eric Seckler Committed by Commit Bot

tracing: Add share trace button to mobile tracing notifications

Adds functionality to share a recorded trace to the mobile tracing
feature.

Bug: 898512
Change-Id: I85107017abdad5ad508b18aecbba58607a004657
Reviewed-on: https://chromium-review.googlesource.com/c/1346070Reviewed-by: default avatarSami Kyöstilä <skyostil@chromium.org>
Commit-Queue: Eric Seckler <eseckler@chromium.org>
Cr-Commit-Position: refs/heads/master@{#610044}
parent c33c50f3
...@@ -5,12 +5,16 @@ ...@@ -5,12 +5,16 @@
package org.chromium.chrome.browser.tracing; package org.chromium.chrome.browser.tracing;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.support.annotation.IntDef; import android.support.annotation.IntDef;
import android.text.TextUtils; import android.text.TextUtils;
import org.chromium.base.ContentUriUtils;
import org.chromium.base.ContextUtils; import org.chromium.base.ContextUtils;
import org.chromium.base.Log; import org.chromium.base.Log;
import org.chromium.base.ObserverList; import org.chromium.base.ObserverList;
import org.chromium.base.ThreadUtils;
import org.chromium.base.VisibleForTesting; import org.chromium.base.VisibleForTesting;
import org.chromium.base.task.AsyncTask; import org.chromium.base.task.AsyncTask;
import org.chromium.chrome.R; import org.chromium.chrome.R;
...@@ -65,6 +69,10 @@ public class TracingController { ...@@ -65,6 +69,10 @@ public class TracingController {
private static final String TEMP_FILE_DIR = "/traces"; private static final String TEMP_FILE_DIR = "/traces";
private static final String TEMP_FILE_PREFIX = "chrome-trace-"; private static final String TEMP_FILE_PREFIX = "chrome-trace-";
private static final String TEMP_FILE_EXT = ".json.gz"; private static final String TEMP_FILE_EXT = ".json.gz";
private static final String TRACE_MIMETYPE = "application/gzip";
// Delete shared trace files after 1 hour.
private static final long DELETE_AFTER_SHARE_TIMEOUT_MILLIS = 60 * 60 * 1000;
private static TracingController sInstance; private static TracingController sInstance;
...@@ -76,7 +84,10 @@ public class TracingController { ...@@ -76,7 +84,10 @@ public class TracingController {
private Set<String> mKnownCategories; private Set<String> mKnownCategories;
private File mTracingTempFile; private File mTracingTempFile;
private TracingController() {} private TracingController() {
// Check for old chrome-trace temp files and delete them.
new DeleteOldTempFilesTask().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}
/** /**
* @return the singleton instance of TracingController, creating and initializing it if needed. * @return the singleton instance of TracingController, creating and initializing it if needed.
...@@ -229,8 +240,6 @@ public class TracingController { ...@@ -229,8 +240,6 @@ public class TracingController {
}); });
} }
// TODO(eseckler): Add a way to download and/or share the trace.
/** /**
* Discards a recorded trace and cleans up the temporary trace file. * Discards a recorded trace and cleans up the temporary trace file.
* Should only be called when in STOPPED state. * Should only be called when in STOPPED state.
...@@ -241,6 +250,36 @@ public class TracingController { ...@@ -241,6 +250,36 @@ public class TracingController {
setState(State.IDLE); setState(State.IDLE);
} }
/**
* Share a recorded trace via an Android share intent.
*/
public void shareTrace() {
assert mState == State.STOPPED;
Intent shareIntent = new Intent(Intent.ACTION_SEND);
Uri fileUri = ContentUriUtils.getContentUriFromFile(mTracingTempFile);
shareIntent.setType(TRACE_MIMETYPE);
shareIntent.putExtra(Intent.EXTRA_STREAM, fileUri);
shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
Context context = ContextUtils.getApplicationContext();
context.startActivity(Intent.createChooser(
shareIntent, context.getResources().getString(R.string.tracing_share)));
// Delete the file after an hour. This won't work if the app quits in the meantime, so we
// also check for old files when TraceController is created.
File tracingTempFile = mTracingTempFile;
ThreadUtils.postOnUiThreadDelayed(() -> {
new DeleteTempFileTask(tracingTempFile)
.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR);
}, DELETE_AFTER_SHARE_TIMEOUT_MILLIS);
mTracingTempFile = null;
setState(State.IDLE);
}
private void setState(@State int state) { private void setState(@State int state) {
Log.d(TAG, "State changing to %d", state); Log.d(TAG, "State changing to %d", state);
mState = state; mState = state;
...@@ -275,6 +314,24 @@ public class TracingController { ...@@ -275,6 +314,24 @@ public class TracingController {
} }
} }
private class DeleteOldTempFilesTask extends AsyncTask<Void> {
@Override
protected Void doInBackground() {
File cacheDir =
new File(ContextUtils.getApplicationContext().getCacheDir() + TEMP_FILE_DIR);
File[] files = cacheDir.listFiles();
if (files != null) {
long maxTime = System.currentTimeMillis() - DELETE_AFTER_SHARE_TIMEOUT_MILLIS;
for (File f : files) {
if (f.lastModified() <= maxTime) {
f.delete();
}
}
}
return null;
}
}
private void showErrorToast() { private void showErrorToast() {
Context context = ContextUtils.getApplicationContext(); Context context = ContextUtils.getApplicationContext();
Toast.makeText(context, context.getString(R.string.tracing_error_toast), Toast.LENGTH_SHORT) Toast.makeText(context, context.getString(R.string.tracing_error_toast), Toast.LENGTH_SHORT)
......
...@@ -129,12 +129,15 @@ public class TracingNotificationManager { ...@@ -129,12 +129,15 @@ public class TracingNotificationManager {
String message = String message =
context.getResources().getString(R.string.tracing_complete_notification_message); context.getResources().getString(R.string.tracing_complete_notification_message);
// TODO(eseckler): Show download / share trace buttons in this notification.
ChromeNotificationBuilder builder = ChromeNotificationBuilder builder =
createNotificationBuilder() createNotificationBuilder()
.setContentTitle(title) .setContentTitle(title)
.setContentText(message) .setContentText(message)
.setOngoing(false) .setOngoing(false)
.addAction(R.drawable.ic_share_white_24dp,
ContextUtils.getApplicationContext().getResources().getString(
R.string.tracing_share),
TracingNotificationService.getShareTraceIntent(context))
.setDeleteIntent(TracingNotificationService.getDiscardTraceIntent(context)); .setDeleteIntent(TracingNotificationService.getDiscardTraceIntent(context));
showNotification(builder.build()); showNotification(builder.build());
} }
......
...@@ -18,10 +18,13 @@ public class TracingNotificationService extends IntentService { ...@@ -18,10 +18,13 @@ public class TracingNotificationService extends IntentService {
private static final String TAG = "tracing_notification"; private static final String TAG = "tracing_notification";
private static final String ACTION_STOP_RECORDING = private static final String ACTION_STOP_RECORDING =
"com.google.android.apps.chrome.tracing.STOP_RECORDING"; "org.chromium.chrome.browser.tracing.STOP_RECORDING";
private static final String ACTION_DISCARD_TRACE = private static final String ACTION_DISCARD_TRACE =
"com.google.android.apps.chrome.tracing.DISCARD_TRACE"; "org.chromium.chrome.browser.tracing.DISCARD_TRACE";
private static final String ACTION_SHARE_TRACE =
"org.chromium.chrome.browser.tracing.SHARE_TRACE";
/** /**
* Get the intent to send to stop a trace recording. * Get the intent to send to stop a trace recording.
...@@ -47,6 +50,18 @@ public class TracingNotificationService extends IntentService { ...@@ -47,6 +50,18 @@ public class TracingNotificationService extends IntentService {
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT); return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
} }
/**
* Get the intent to share a recorded trace.
*
* @param context the application's context.
* @return the intent.
*/
public static PendingIntent getShareTraceIntent(Context context) {
Intent intent = new Intent(context, TracingNotificationService.class);
intent.setAction(ACTION_SHARE_TRACE);
return PendingIntent.getService(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
/** /**
* Construct the service. Called by Android. * Construct the service. Called by Android.
*/ */
...@@ -59,8 +74,10 @@ public class TracingNotificationService extends IntentService { ...@@ -59,8 +74,10 @@ public class TracingNotificationService extends IntentService {
if (ACTION_STOP_RECORDING.equals(intent.getAction())) { if (ACTION_STOP_RECORDING.equals(intent.getAction())) {
ThreadUtils.runOnUiThreadBlocking( ThreadUtils.runOnUiThreadBlocking(
() -> { TracingController.getInstance().stopRecording(); }); () -> { TracingController.getInstance().stopRecording(); });
} else { } else if (ACTION_SHARE_TRACE.equals(intent.getAction())) {
assert ACTION_DISCARD_TRACE.equals(intent.getAction()); ThreadUtils.runOnUiThreadBlocking(
() -> { TracingController.getInstance().shareTrace(); });
} else if (ACTION_DISCARD_TRACE.equals(intent.getAction())) {
ThreadUtils.runOnUiThreadBlocking( ThreadUtils.runOnUiThreadBlocking(
() -> { TracingController.getInstance().discardTrace(); }); () -> { TracingController.getInstance().discardTrace(); });
} }
......
...@@ -1459,6 +1459,9 @@ Your Google account may have other forms of browsing history like searches and a ...@@ -1459,6 +1459,9 @@ Your Google account may have other forms of browsing history like searches and a
<message name="IDS_TRACING_STOP" translateable="false" desc="Caption of a button that stops recording a trace."> <message name="IDS_TRACING_STOP" translateable="false" desc="Caption of a button that stops recording a trace.">
Stop recording Stop recording
</message> </message>
<message name="IDS_TRACING_SHARE" translateable="false" desc="Caption of a button that shares a recorded trace.">
Share trace
</message>
<message name="IDS_TRACING_ERROR_TOAST" translateable="false" desc="Message of a toast shown when there was an error during tracing."> <message name="IDS_TRACING_ERROR_TOAST" translateable="false" desc="Message of a toast shown when there was an error during tracing.">
Error occurred while recording Chrome trace, see log for details. Error occurred while recording Chrome trace, see log for details.
</message> </message>
......
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