Commit e819edb8 authored by Sam Maier's avatar Sam Maier Committed by Commit Bot

Android: making ShareHelper usage of AsyncTask explicit

Currently, AsyncTask.execute() defaults to the SERIAL_EXECUTOR. This
exector is good for preventing concurrency errors since it guarantees
serial execution, but bad for performance since the entire app shares
this single queue.

We are using SERIAL_EXECUTOR in clearSharedImages and shareImage since
they don't appear to be thread safe, and they could potentially be
called multiple times.

We are using the UI thread in getShareableIconAndName since we
immediately call get() on it anyway. This negates any benefit for having
an AsyncTask.

Bug: 869907, 729737
Change-Id: I9ea44efba2155bb2b643f6d4d8f2b38c8365f418
Reviewed-on: https://chromium-review.googlesource.com/1161218
Commit-Queue: Sam Maier <smaier@chromium.org>
Reviewed-by: default avatarTheresa <twellington@chromium.org>
Cr-Commit-Position: refs/heads/master@{#581308}
parent 61985bcc
......@@ -42,6 +42,7 @@ import org.chromium.base.Callback;
import org.chromium.base.ContextUtils;
import org.chromium.base.Log;
import org.chromium.base.StreamUtil;
import org.chromium.base.StrictModeContext;
import org.chromium.base.VisibleForTesting;
import org.chromium.base.metrics.CachedMetrics;
import org.chromium.chrome.R;
......@@ -53,9 +54,6 @@ import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;
/**
* A helper class that helps to start an intent to share titles and URLs.
......@@ -261,20 +259,15 @@ public class ShareHelper {
* Clears all shared image files.
*/
public static void clearSharedImages() {
new AsyncTask<Void>() {
@Override
protected Void doInBackground() {
try {
File imagePath = UiUtils.getDirectoryForImageCapture(
ContextUtils.getApplicationContext());
deleteShareImageFiles(new File(imagePath, SHARE_IMAGES_DIRECTORY_NAME));
} catch (IOException ie) {
// Ignore exception.
}
return null;
AsyncTask.SERIAL_EXECUTOR.execute(() -> {
try {
File imagePath =
UiUtils.getDirectoryForImageCapture(ContextUtils.getApplicationContext());
deleteShareImageFiles(new File(imagePath, SHARE_IMAGES_DIRECTORY_NAME));
} catch (IOException ie) {
// Ignore exception.
}
}
.execute();
});
}
/**
......@@ -365,7 +358,7 @@ public class ShareHelper {
}
}
}
.execute();
.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
private static class ExternallyVisibleUriCallback implements Callback<String> {
......@@ -547,39 +540,17 @@ public class ShareHelper {
}
if (isComponentValid) {
boolean retrieved = false;
final PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
try {
final PackageManager pm = ContextUtils.getApplicationContext().getPackageManager();
AsyncTask<Pair<Drawable, CharSequence>> task =
new AsyncTask<Pair<Drawable, CharSequence>>() {
@Override
protected Pair<Drawable, CharSequence> doInBackground() {
Drawable directShareIcon = null;
CharSequence directShareTitle = null;
try {
directShareIcon = pm.getActivityIcon(component);
ApplicationInfo ai =
pm.getApplicationInfo(component.getPackageName(), 0);
directShareTitle = pm.getApplicationLabel(ai);
} catch (NameNotFoundException exception) {
// Use the default null values.
}
return new Pair<Drawable, CharSequence>(
directShareIcon, directShareTitle);
}
};
task.execute();
// TODO(ltian): Return nothing for the AsyncTask and have a callback to update the
// the menu.
Pair<Drawable, CharSequence> result =
task.get(COMPONENT_INFO_READ_TIMEOUT_IN_MS, TimeUnit.MILLISECONDS);
directShareIcon = result.first;
directShareTitle = result.second;
// TODO(dtrainor): Make asynchronous and have a callback to update the menu.
// https://crbug.com/729737
try (StrictModeContext unused = StrictModeContext.allowDiskReads()) {
directShareIcon = pm.getActivityIcon(component);
ApplicationInfo ai = pm.getApplicationInfo(component.getPackageName(), 0);
directShareTitle = pm.getApplicationLabel(ai);
}
retrieved = true;
} catch (InterruptedException ie) {
// Use the default null values.
} catch (ExecutionException ee) {
// Use the default null values.
} catch (TimeoutException te) {
} catch (NameNotFoundException exception) {
// Use the default null values.
}
CachedMetrics.BooleanHistogramSample isLastSharedAppInfoRetrieved =
......
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