This source file includes following definitions.
- JNINamespace
- getBroadcastReceiver
- getIntentFilter
- registerReceiver
- unregisterReceiver
- isTracing
- getOutputPath
- startTracing
- startTracing
- stopTracing
- onTracingStopped
- finalize
- logAndToastError
- logAndToastInfo
- onReceive
- nativeInit
- nativeDestroy
- nativeStartTracing
- nativeStopTracing
- nativeGetDefaultCategories
package org.chromium.content.browser;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.Toast;
import org.chromium.base.CalledByNative;
import org.chromium.base.JNINamespace;
import org.chromium.base.TraceEvent;
import org.chromium.content.R;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.TimeZone;
@JNINamespace("content")
public class TracingControllerAndroid {
private static final String TAG = "TracingControllerAndroid";
private static final String ACTION_START = "GPU_PROFILER_START";
private static final String ACTION_STOP = "GPU_PROFILER_STOP";
private static final String FILE_EXTRA = "file";
private static final String CATEGORIES_EXTRA = "categories";
private static final String RECORD_CONTINUOUSLY_EXTRA = "continuous";
private static final String DEFAULT_CHROME_CATEGORIES_PLACE_HOLDER =
"_DEFAULT_CHROME_CATEGORIES";
private final Context mContext;
private final TracingBroadcastReceiver mBroadcastReceiver;
private final TracingIntentFilter mIntentFilter;
private boolean mIsTracing;
private boolean mShowToasts = true;
private String mFilename;
public TracingControllerAndroid(Context context) {
mContext = context;
mBroadcastReceiver = new TracingBroadcastReceiver();
mIntentFilter = new TracingIntentFilter(context);
}
public BroadcastReceiver getBroadcastReceiver() {
return mBroadcastReceiver;
}
public IntentFilter getIntentFilter() {
return mIntentFilter;
}
public void registerReceiver(Context context) {
context.registerReceiver(getBroadcastReceiver(), getIntentFilter());
}
public void unregisterReceiver(Context context) {
context.unregisterReceiver(getBroadcastReceiver());
}
public boolean isTracing() {
return mIsTracing;
}
public String getOutputPath() {
return mFilename;
}
public boolean startTracing(boolean showToasts, String categories,
boolean recordContinuously) {
mShowToasts = showToasts;
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)) {
logAndToastError(
mContext.getString(R.string.profiler_no_storage_toast));
return false;
}
SimpleDateFormat formatter = new SimpleDateFormat(
"yyyy-MM-dd-HHmmss", Locale.US);
formatter.setTimeZone(TimeZone.getTimeZone("UTC"));
File dir = Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_DOWNLOADS);
File file = new File(
dir, "chrome-profile-results-" + formatter.format(new Date()));
return startTracing(file.getPath(), showToasts, categories, recordContinuously);
}
public boolean startTracing(String filename, boolean showToasts, String categories,
boolean recordContinuously) {
mShowToasts = showToasts;
if (isTracing()) {
Log.e(TAG, "Received startTracing, but we're already tracing");
return false;
}
if (mNativeTracingControllerAndroid == 0) {
mNativeTracingControllerAndroid = nativeInit();
}
if (!nativeStartTracing(mNativeTracingControllerAndroid, filename, categories,
recordContinuously)) {
logAndToastError(mContext.getString(R.string.profiler_error_toast));
return false;
}
logAndToastInfo(mContext.getString(R.string.profiler_started_toast) + ": " + categories);
TraceEvent.setEnabledToMatchNative();
mFilename = filename;
mIsTracing = true;
return true;
}
public void stopTracing() {
if (isTracing()) {
nativeStopTracing(mNativeTracingControllerAndroid);
}
}
@CalledByNative
protected void onTracingStopped() {
if (!isTracing()) {
Log.e(TAG, "Received onTracingStopped, but we aren't tracing");
return;
}
logAndToastInfo(
mContext.getString(R.string.profiler_stopped_toast, mFilename));
TraceEvent.setEnabledToMatchNative();
mIsTracing = false;
mFilename = null;
}
@Override
protected void finalize() {
if (mNativeTracingControllerAndroid != 0) {
nativeDestroy(mNativeTracingControllerAndroid);
mNativeTracingControllerAndroid = 0;
}
}
void logAndToastError(String str) {
Log.e(TAG, str);
if (mShowToasts) Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
}
void logAndToastInfo(String str) {
Log.i(TAG, str);
if (mShowToasts) Toast.makeText(mContext, str, Toast.LENGTH_SHORT).show();
}
private static class TracingIntentFilter extends IntentFilter {
TracingIntentFilter(Context context) {
addAction(context.getPackageName() + "." + ACTION_START);
addAction(context.getPackageName() + "." + ACTION_STOP);
}
}
class TracingBroadcastReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().endsWith(ACTION_START)) {
String categories = intent.getStringExtra(CATEGORIES_EXTRA);
if (TextUtils.isEmpty(categories)) {
categories = nativeGetDefaultCategories();
} else {
categories = categories.replaceFirst(
DEFAULT_CHROME_CATEGORIES_PLACE_HOLDER, nativeGetDefaultCategories());
}
boolean recordContinuously =
intent.getStringExtra(RECORD_CONTINUOUSLY_EXTRA) != null;
String filename = intent.getStringExtra(FILE_EXTRA);
if (filename != null) {
startTracing(filename, true, categories, recordContinuously);
} else {
startTracing(true, categories, recordContinuously);
}
} else if (intent.getAction().endsWith(ACTION_STOP)) {
stopTracing();
} else {
Log.e(TAG, "Unexpected intent: " + intent);
}
}
}
private long mNativeTracingControllerAndroid;
private native long nativeInit();
private native void nativeDestroy(long nativeTracingControllerAndroid);
private native boolean nativeStartTracing(long nativeTracingControllerAndroid, String filename,
String categories, boolean recordContinuously);
private native void nativeStopTracing(long nativeTracingControllerAndroid);
private native String nativeGetDefaultCategories();
}